diff --git a/src/mux/muxedit.c b/src/mux/muxedit.c index 32ce3d7b..d074be61 100644 --- a/src/mux/muxedit.c +++ b/src/mux/muxedit.c @@ -43,9 +43,16 @@ static void DeleteAllChunks(WebPChunk** const chunk_list) { } } +// Delete all images in 'wpi_list'. +static void DeleteAllImages(WebPMuxImage** const wpi_list) { + while (*wpi_list != NULL) { + *wpi_list = MuxImageDelete(*wpi_list); + } +} + static void MuxRelease(WebPMux* const mux) { if (mux == NULL) return; - MuxImageDeleteAll(&mux->images_); + DeleteAllImages(&mux->images_); DeleteAllChunks(&mux->vp8x_); DeleteAllChunks(&mux->iccp_); DeleteAllChunks(&mux->anim_); @@ -273,7 +280,7 @@ WebPMuxError WebPMuxSetImage(WebPMux* mux, const WebPData* bitstream, if (mux->images_ != NULL) { // Only one 'simple image' can be added in mux. So, remove present images. - MuxImageDeleteAll(&mux->images_); + DeleteAllImages(&mux->images_); } MuxImageInit(&wpi); @@ -630,6 +637,35 @@ static WebPMuxError MuxCleanup(WebPMux* const mux) { return WEBP_MUX_OK; } +// Total size of a list of chunks. +static size_t ChunkListDiskSize(const WebPChunk* chunk_list) { + size_t size = 0; + while (chunk_list != NULL) { + size += ChunkDiskSize(chunk_list); + chunk_list = chunk_list->next_; + } + return size; +} + +// Total size of a list of images. +static size_t ImageListDiskSize(const WebPMuxImage* wpi_list) { + size_t size = 0; + while (wpi_list != NULL) { + size += MuxImageDiskSize(wpi_list); + wpi_list = wpi_list->next_; + } + return size; +} + +// Write out the given list of images into 'dst'. +static uint8_t* ImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst) { + while (wpi_list != NULL) { + dst = MuxImageEmit(wpi_list, dst); + wpi_list = wpi_list->next_; + } + return dst; +} + WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data) { size_t size = 0; uint8_t* data = NULL; @@ -647,10 +683,10 @@ WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data) { if (err != WEBP_MUX_OK) return err; // Allocate data. - size = ChunksListDiskSize(mux->vp8x_) + ChunksListDiskSize(mux->iccp_) - + ChunksListDiskSize(mux->anim_) + MuxImageListDiskSize(mux->images_) - + ChunksListDiskSize(mux->exif_) + ChunksListDiskSize(mux->xmp_) - + ChunksListDiskSize(mux->unknown_) + RIFF_HEADER_SIZE; + size = ChunkListDiskSize(mux->vp8x_) + ChunkListDiskSize(mux->iccp_) + + ChunkListDiskSize(mux->anim_) + ImageListDiskSize(mux->images_) + + ChunkListDiskSize(mux->exif_) + ChunkListDiskSize(mux->xmp_) + + ChunkListDiskSize(mux->unknown_) + RIFF_HEADER_SIZE; data = (uint8_t*)malloc(size); if (data == NULL) return WEBP_MUX_MEMORY_ERROR; @@ -660,7 +696,7 @@ WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data) { dst = ChunkListEmit(mux->vp8x_, dst); dst = ChunkListEmit(mux->iccp_, dst); dst = ChunkListEmit(mux->anim_, dst); - dst = MuxImageListEmit(mux->images_, dst); + dst = ImageListEmit(mux->images_, dst); dst = ChunkListEmit(mux->exif_, dst); dst = ChunkListEmit(mux->xmp_, dst); dst = ChunkListEmit(mux->unknown_, dst); diff --git a/src/mux/muxi.h b/src/mux/muxi.h index 0aa89707..3a5044ec 100644 --- a/src/mux/muxi.h +++ b/src/mux/muxi.h @@ -143,9 +143,6 @@ static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) { return SizeWithPadding(data_size); } -// Total size of a list of chunks. -size_t ChunksListDiskSize(const WebPChunk* chunk_list); - // Write out the given list of chunks into 'dst'. uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst); @@ -167,9 +164,6 @@ WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi); // 'wpi' can be NULL. WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi); -// Delete all images in 'wpi_list'. -void MuxImageDeleteAll(WebPMuxImage** const wpi_list); - // Count number of images matching the given tag id in the 'wpi_list'. // If id == WEBP_CHUNK_NIL, all images will be matched. int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id); @@ -185,19 +179,6 @@ static WEBP_INLINE int IsWPI(WebPChunkId id) { } } -// Get a reference to appropriate chunk list within an image given chunk tag. -static WEBP_INLINE WebPChunk** MuxImageGetListFromId( - const WebPMuxImage* const wpi, WebPChunkId id) { - assert(wpi != NULL); - switch (id) { - case WEBP_CHUNK_ANMF: - case WEBP_CHUNK_FRGM: return (WebPChunk**)&wpi->header_; - case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_; - case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_; - default: return NULL; - } -} - // Pushes 'wpi' at the end of 'wpi_list'. WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list); @@ -211,15 +192,9 @@ WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth, // Total size of the given image. size_t MuxImageDiskSize(const WebPMuxImage* const wpi); -// Total size of a list of images. -size_t MuxImageListDiskSize(const WebPMuxImage* wpi_list); - // Write out the given image into 'dst'. uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst); -// Write out the given list of images into 'dst'. -uint8_t* MuxImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst); - //------------------------------------------------------------------------------ // Helper methods for mux. diff --git a/src/mux/muxinternal.c b/src/mux/muxinternal.c index 2428c597..8ea90bbd 100644 --- a/src/mux/muxinternal.c +++ b/src/mux/muxinternal.c @@ -189,15 +189,6 @@ WebPChunk* ChunkDelete(WebPChunk* const chunk) { //------------------------------------------------------------------------------ // Chunk serialization methods. -size_t ChunksListDiskSize(const WebPChunk* chunk_list) { - size_t size = 0; - while (chunk_list != NULL) { - size += ChunkDiskSize(chunk_list); - chunk_list = chunk_list->next_; - } - return size; -} - static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) { const size_t chunk_size = chunk->data_.size; assert(chunk); @@ -242,6 +233,19 @@ WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) { //------------------------------------------------------------------------------ // MuxImage search methods. +// Get a reference to appropriate chunk list within an image given chunk tag. +static WebPChunk** GetChunkListFromId(const WebPMuxImage* const wpi, + WebPChunkId id) { + assert(wpi != NULL); + switch (id) { + case WEBP_CHUNK_ANMF: + case WEBP_CHUNK_FRGM: return (WebPChunk**)&wpi->header_; + case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_; + case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_; + default: return NULL; + } +} + int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) { int count = 0; const WebPMuxImage* current; @@ -249,7 +253,7 @@ int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) { if (id == WEBP_CHUNK_NIL) { ++count; // Special case: count all images. } else { - const WebPChunk* const wpi_chunk = *MuxImageGetListFromId(current, id); + const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id); if (wpi_chunk != NULL) { const WebPChunkId wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_); if (wpi_chunk_id == id) ++count; // Count images with a matching 'id'. @@ -318,12 +322,6 @@ WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) { return next; } -void MuxImageDeleteAll(WebPMuxImage** const wpi_list) { - while (*wpi_list != NULL) { - *wpi_list = MuxImageDelete(*wpi_list); - } -} - WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) { assert(wpi_list); if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) { @@ -360,15 +358,6 @@ size_t MuxImageDiskSize(const WebPMuxImage* const wpi) { return size; } -size_t MuxImageListDiskSize(const WebPMuxImage* wpi_list) { - size_t size = 0; - while (wpi_list != NULL) { - size += MuxImageDiskSize(wpi_list); - wpi_list = wpi_list->next_; - } - return size; -} - // Special case as ANMF/FRGM chunk encapsulates other image chunks. static uint8_t* ChunkEmitSpecial(const WebPChunk* const header, size_t total_size, uint8_t* dst) { @@ -400,14 +389,6 @@ uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) { return dst; } -uint8_t* MuxImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst) { - while (wpi_list != NULL) { - dst = MuxImageEmit(wpi_list, dst); - wpi_list = wpi_list->next_; - } - return dst; -} - //------------------------------------------------------------------------------ // Helper methods for mux.