From 0cc811d7d6f8200cde698b5273e632620deb4c6b Mon Sep 17 00:00:00 2001 From: Urvang Joshi Date: Thu, 18 Sep 2014 15:07:28 -0700 Subject: [PATCH] gif2webp: Background color correction For some GIF images, the first frame is missing the corresponding graphic control extension. For such cases, we were never calling GetBackgroundColor(), and default background color value (white) was being used incorrectly. So, we call GetBackgroundColor() when we encounter the first image descriptor instead, to make sure that it is always called. Change-Id: I00fc8e943d8a0c1578dcd718f3e74dec7de4ed61 --- examples/gif2webp.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/gif2webp.c b/examples/gif2webp.c index fc21d202..b725e46b 100644 --- a/examples/gif2webp.c +++ b/examples/gif2webp.c @@ -174,21 +174,22 @@ static int ReadFrame(GifFileType* const gif, WebPFrameRect* const gif_rect, return ok; } -static int GetBackgroundColor(const ColorMapObject* const color_map, - int bgcolor_idx, uint32_t* const bgcolor) { +static void GetBackgroundColor(const ColorMapObject* const color_map, + int bgcolor_idx, uint32_t* const bgcolor) { if (transparent_index != -1 && bgcolor_idx == transparent_index) { *bgcolor = WEBP_UTIL_TRANSPARENT_COLOR; // Special case. - return 1; } else if (color_map == NULL || color_map->Colors == NULL || bgcolor_idx >= color_map->ColorCount) { - return 0; // Invalid color map or index. + *bgcolor = WHITE_COLOR; + fprintf(stderr, + "GIF decode warning: invalid background color index. Assuming " + "white background.\n"); } else { const GifColorType color = color_map->Colors[bgcolor_idx]; *bgcolor = (0xff << 24) | (color.Red << 16) | (color.Green << 8) | (color.Blue << 0); - return 1; } } @@ -481,6 +482,10 @@ int main(int argc, const char *argv[]) { cache = WebPFrameCacheNew(frame.width, frame.height, kmin, kmax, allow_mixed); if (cache == NULL) goto End; + + // Background color. + GetBackgroundColor(gif->SColorMap, gif->SBackGroundColor, + &anim.bgcolor); } // Some even more broken GIF can have sub-rect with zero width/height. if (image_desc->Width == 0 || image_desc->Height == 0) { @@ -542,13 +547,6 @@ int main(int argc, const char *argv[]) { : WEBP_MUX_DISPOSE_NONE; } transparent_index = (flags & GIF_TRANSPARENT_MASK) ? data[4] : -1; - if (is_first_frame) { - if (!GetBackgroundColor(gif->SColorMap, gif->SBackGroundColor, - &anim.bgcolor)) { - fprintf(stderr, "GIF decode warning: invalid background color " - "index. Assuming white background.\n"); - } - } break; } case PLAINTEXT_EXT_FUNC_CODE: {