static WebPMuxError MuxAddChunk(WebPMux* const mux, uint32_t nth, uint32_t tag, const uint8_t* data, size_t size, int copy_data) { const CHUNK_INDEX idx = ChunkGetIndexFromTag(tag); const WebPData chunk_data = { data, size }; assert(mux != NULL); assert(size <= MAX_CHUNK_PAYLOAD); assert(idx != IDX_NIL); return MuxSet(mux, idx, nth, &chunk_data, copy_data); }
WebPMuxError WebPMuxSetColorProfile(WebPMux* mux, const WebPData* color_profile, int copy_data) { WebPMuxError err; if (mux == NULL || color_profile == NULL || color_profile->bytes_ == NULL || color_profile->size_ > MAX_CHUNK_PAYLOAD) { return WEBP_MUX_INVALID_ARGUMENT; } // Delete the existing ICCP chunk(s). err = WebPMuxDeleteColorProfile(mux); if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; // Add the given ICCP chunk. return MuxSet(mux, IDX_ICCP, 1, color_profile, copy_data); }
WebPMuxError WebPMuxSetMetadata(WebPMux* mux, const WebPData* metadata, int copy_data) { WebPMuxError err; if (mux == NULL || metadata == NULL || metadata->bytes_ == NULL || metadata->size_ > MAX_CHUNK_PAYLOAD) { return WEBP_MUX_INVALID_ARGUMENT; } // Delete the existing metadata chunk(s). err = WebPMuxDeleteMetadata(mux); if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; // Add the given metadata chunk. return MuxSet(mux, IDX_META, 1, metadata, copy_data); }
WebPMuxError WebPMuxSetChunk(WebPMux* mux, const char fourcc[4], const WebPData* chunk_data, int copy_data) { uint32_t tag; WebPMuxError err; if (mux == NULL || fourcc == NULL || chunk_data == NULL || chunk_data->bytes == NULL || chunk_data->size > MAX_CHUNK_PAYLOAD) { return WEBP_MUX_INVALID_ARGUMENT; } tag = ChunkGetTagFromFourCC(fourcc); // Delete existing chunk(s) with the same 'fourcc'. err = MuxDeleteAllNamedData(mux, tag); if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; // Add the given chunk. return MuxSet(mux, tag, 1, chunk_data, copy_data); }
WebPMuxError WebPMuxSetAnimationParams(WebPMux* mux, const WebPMuxAnimParams* params) { WebPMuxError err; uint8_t data[ANIM_CHUNK_SIZE]; const WebPData anim = { data, ANIM_CHUNK_SIZE }; if (mux == NULL || params == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (params->loop_count < 0 || params->loop_count >= MAX_LOOP_COUNT) { return WEBP_MUX_INVALID_ARGUMENT; } // Delete any existing ANIM chunk(s). err = MuxDeleteAllNamedData(mux, kChunks[IDX_ANIM].tag); if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; // Set the animation parameters. PutLE32(data, params->bgcolor); PutLE16(data + 4, params->loop_count); return MuxSet(mux, kChunks[IDX_ANIM].tag, 1, &anim, 1); }
// VP8X format: // Total Size : 10, // Flags : 4 bytes, // Width : 3 bytes, // Height : 3 bytes. static WebPMuxError CreateVP8XChunk(WebPMux* const mux) { WebPMuxError err = WEBP_MUX_OK; uint32_t flags = 0; int width = 0; int height = 0; uint8_t data[VP8X_CHUNK_SIZE]; const WebPData vp8x = { data, VP8X_CHUNK_SIZE }; const WebPMuxImage* images = NULL; assert(mux != NULL); images = mux->images_; // First image. if (images == NULL || images->img_ == NULL || images->img_->data_.bytes == NULL) { return WEBP_MUX_INVALID_ARGUMENT; } // If VP8X chunk(s) is(are) already present, remove them (and later add new // VP8X chunk with updated flags). err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].tag); if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; // Set flags. if (mux->iccp_ != NULL && mux->iccp_->data_.bytes != NULL) { flags |= ICCP_FLAG; } if (mux->exif_ != NULL && mux->exif_->data_.bytes != NULL) { flags |= EXIF_FLAG; } if (mux->xmp_ != NULL && mux->xmp_->data_.bytes != NULL) { flags |= XMP_FLAG; } if (images->header_ != NULL) { if (images->header_->tag_ == kChunks[IDX_FRGM].tag) { // This is a fragmented image. flags |= FRAGMENTS_FLAG; } else if (images->header_->tag_ == kChunks[IDX_ANMF].tag) { // This is an image with animation. flags |= ANIMATION_FLAG; } } if (MuxImageCount(images, WEBP_CHUNK_ALPHA) > 0) { flags |= ALPHA_FLAG; // Some images have an alpha channel. } if (flags == 0) { // For Simple Image, VP8X chunk should not be added. return WEBP_MUX_OK; } err = GetImageCanvasWidthHeight(mux, flags, &width, &height); if (err != WEBP_MUX_OK) return err; if (width <= 0 || height <= 0) { return WEBP_MUX_INVALID_ARGUMENT; } if (width > MAX_CANVAS_SIZE || height > MAX_CANVAS_SIZE) { return WEBP_MUX_INVALID_ARGUMENT; } if (MuxHasAlpha(images)) { // This means some frames explicitly/implicitly contain alpha. // Note: This 'flags' update must NOT be done for a lossless image // without a VP8X chunk! flags |= ALPHA_FLAG; } PutLE32(data + 0, flags); // VP8X chunk flags. PutLE24(data + 4, width - 1); // canvas width. PutLE24(data + 7, height - 1); // canvas height. return MuxSet(mux, kChunks[IDX_VP8X].tag, 1, &vp8x, 1); }