// Validates that the given mux has a single image. static WebPMuxError ValidateForSingleImage(const WebPMux* const mux) { const int num_images = MuxImageCount(mux->images_, WEBP_CHUNK_IMAGE); const int num_frames = MuxImageCount(mux->images_, WEBP_CHUNK_ANMF); const int num_fragments = MuxImageCount(mux->images_, WEBP_CHUNK_FRGM); if (num_images == 0) { // No images in mux. return WEBP_MUX_NOT_FOUND; } else if (num_images == 1 && num_frames == 0 && num_fragments == 0) { // Valid case (single image). return WEBP_MUX_OK; } else { // Frame/Fragment case OR an invalid mux. return WEBP_MUX_INVALID_ARGUMENT; } }
WebPMuxError WebPMuxNumChunks(const WebPMux* mux, WebPChunkId id, int* num_elements) { if (mux == NULL || num_elements == NULL) { return WEBP_MUX_INVALID_ARGUMENT; } if (IsWPI(id)) { *num_elements = MuxImageCount(mux->images_, id); } else { WebPChunk* const* chunk_list = MuxGetChunkListFromId(mux, id); const CHUNK_INDEX idx = ChunkGetIndexFromId(id); *num_elements = CountChunks(*chunk_list, kChunks[idx].tag); } return WEBP_MUX_OK; }
// Outputs a pointer to 'prev_wpi->next_', // where 'prev_wpi' is the pointer to the image at position (nth - 1). // Returns true if nth image was found. static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth, WebPMuxImage*** const location) { uint32_t count = 0; assert(wpi_list); *location = wpi_list; if (nth == 0) { nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL); if (nth == 0) return 0; // Not found. } while (*wpi_list != NULL) { WebPMuxImage* const cur_wpi = *wpi_list; ++count; if (count == nth) return 1; // Found. wpi_list = &cur_wpi->next_; *location = wpi_list; } return 0; // Not found. }
// 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); }
// 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 size_t data_size = 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, IDX_VP8X); 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->meta_ != NULL && mux->meta_->data_.bytes_ != NULL) { flags |= META_FLAG; } if (images->header_ != NULL) { if (images->header_->tag_ == kChunks[IDX_TILE].tag) { // This is a tiled image. flags |= TILE_FLAG; } else if (images->header_->tag_ == kChunks[IDX_FRAME].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 (MuxHasLosslessImages(images)) { // We have a file with a VP8X chunk having some lossless images. // As lossless images implicitly contain alpha, force ALPHA_FLAG to be true. // 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. err = MuxAddChunk(mux, 1, kChunks[IDX_VP8X].tag, data, data_size, 1); return err; }