fix a memory leak in gif2webp

(rgba->yuv allocates memory)
Also fixed few warning and cleaned the code up.

Change-Id: Id904ad3ad8802ea9fc3d34247d27193dfa7b0b99
(cherry picked from commit 764fdffaac)
This commit is contained in:
skal 2013-05-22 23:49:24 +02:00 committed by James Zern
parent 0b18b9eef6
commit e190843029

View File

@ -188,8 +188,6 @@ int main(int argc, const char *argv[]) {
FILE* out = NULL; FILE* out = NULL;
GifFileType* gif = NULL; GifFileType* gif = NULL;
WebPPicture picture; WebPPicture picture;
WebPPicture view;
WebPMemoryWriter memory;
WebPMuxFrameInfo frame; WebPMuxFrameInfo frame;
WebPMuxAnimParams anim = { WHITE_COLOR, 0 }; WebPMuxAnimParams anim = { WHITE_COLOR, 0 };
@ -276,9 +274,7 @@ int main(int argc, const char *argv[]) {
picture.width = gif->SWidth; picture.width = gif->SWidth;
picture.height = gif->SHeight; picture.height = gif->SHeight;
picture.use_argb = 1; picture.use_argb = 1;
picture.writer = WebPMemoryWrite; if (!WebPPictureAlloc(&picture)) goto End;
picture.custom_ptr = &memory;
if (!WebPPictureAlloc(&picture)) goto End;
mux = WebPMuxNew(); mux = WebPMuxNew();
if (mux == NULL) { if (mux == NULL) {
@ -294,27 +290,33 @@ int main(int argc, const char *argv[]) {
switch (type) { switch (type) {
case IMAGE_DESC_RECORD_TYPE: { case IMAGE_DESC_RECORD_TYPE: {
WebPPicture sub_image;
WebPMemoryWriter memory;
if (frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) { if (frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
ClearPicture(&picture, anim.bgcolor); ClearPicture(&picture, anim.bgcolor);
} }
if (!DGifGetImageDesc(gif)) goto End; if (!DGifGetImageDesc(gif)) goto End;
if (!ReadSubImage(gif, &picture, &view)) goto End; if (!ReadSubImage(gif, &picture, &sub_image)) goto End;
WebPMemoryWriterInit(&memory);
if (!config.lossless) { if (!config.lossless) {
// We need to call BGRA variant because of the way we do Remap(). // We need to call BGRA variant because of the way we do Remap(). Note
// TODO(later): This works for little-endian only due to uint32_t to // that 'sub_image' will no longer be a view and own some memory.
// uint8_t conversion. Make it work for big-endian too. WebPPictureImportBGRA(
WebPPictureImportBGRA(&view, (uint8_t*)view.argb, &sub_image, (uint8_t*)sub_image.argb,
view.argb_stride * sizeof(*view.argb)); sub_image.argb_stride * sizeof(*sub_image.argb));
view.use_argb = 0; sub_image.use_argb = 0;
} else { } else {
view.use_argb = 1; sub_image.use_argb = 1;
} }
if (!WebPEncode(&config, &view)) {
sub_image.writer = WebPMemoryWrite;
sub_image.custom_ptr = &memory;
WebPMemoryWriterInit(&memory);
if (!WebPEncode(&config, &sub_image)) {
fprintf(stderr, "Error! Cannot encode picture as WebP\n"); fprintf(stderr, "Error! Cannot encode picture as WebP\n");
fprintf(stderr, "Error code: %d\n", view.error_code); fprintf(stderr, "Error code: %d\n", sub_image.error_code);
goto End; goto End;
} }
@ -333,12 +335,14 @@ int main(int argc, const char *argv[]) {
} }
if (verbose) { if (verbose) {
printf("Added frame %dx%d (offset:%d,%d duration:%d) ", printf("Added frame %dx%d (offset:%d,%d duration:%d) ",
view.width, view.height, frame.x_offset, frame.y_offset, sub_image.width, sub_image.height,
frame.x_offset, frame.y_offset,
frame.duration); frame.duration);
printf("dispose:%d transparent index:%d\n", printf("dispose:%d transparent index:%d\n",
frame.dispose_method, transparent_index); frame.dispose_method, transparent_index);
} }
WebPDataClear(&frame.bitstream); WebPDataClear(&frame.bitstream);
WebPPictureFree(&sub_image);
break; break;
} }
case EXTENSION_RECORD_TYPE: { case EXTENSION_RECORD_TYPE: {
@ -398,7 +402,7 @@ int main(int argc, const char *argv[]) {
const int is_icc = const int is_icc =
!stored_icc && !memcmp(data + 1, "ICCRGBG1012", 11); !stored_icc && !memcmp(data + 1, "ICCRGBG1012", 11);
if (is_xmp || is_icc) { if (is_xmp || is_icc) {
const char fourccs[2][4] = { "XMP " , "ICCP" }; const char* const fourccs[2] = { "XMP " , "ICCP" };
const char* const features[2] = { "XMP" , "ICC" }; const char* const features[2] = { "XMP" , "ICC" };
WebPData metadata = { NULL, 0 }; WebPData metadata = { NULL, 0 };
// Construct metadata from sub-blocks. // Construct metadata from sub-blocks.
@ -427,7 +431,7 @@ int main(int argc, const char *argv[]) {
goto End; goto End;
} }
metadata.size += subblock.size; metadata.size += subblock.size;
memcpy((void*)metadata.bytes + prev_metadata.size, memcpy((void*)(metadata.bytes + prev_metadata.size),
subblock.bytes, subblock.size); subblock.bytes, subblock.size);
} }
if (is_xmp) { if (is_xmp) {