improve __gif_make_color_map() not to use input parameter directly 51/231551/5
authorhj kim <backto.kim@samsung.com>
Wed, 22 Apr 2020 09:24:19 +0000 (18:24 +0900)
committerhj kim <backto.kim@samsung.com>
Mon, 27 Apr 2020 03:18:19 +0000 (03:18 +0000)
Change-Id: Iad6fab9bb9e60c4967c10d2a7999c2f7063094dc

gif/mm_util_gif.c

index 968e694..3cb4299 100644 (file)
@@ -390,51 +390,47 @@ static void __gif_extract_rgb(mm_image_info_s *gif_image, unsigned long num_of_p
 
 static int __gif_make_color_map(mm_image_info_s *gif_image, ColorMapObject **color_map, GifByteType **intermediate_image, unsigned long *intermediate_image_size)
 {
-       int ret = MM_UTIL_ERROR_NONE;
+       int ret = GIF_OK;
        int colormap_size = DEFAULT_COLORMAP_SIZE;
        GifByteType *red = NULL, *green = NULL, *blue = NULL;
+       unsigned long num_of_pixels = 0;
+       ColorMapObject *gif_color_map = NULL;
+       GifByteType *out_buffer = NULL;
 
-       mm_util_retvm_if(gif_image == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
-
-       unsigned long num_of_pixels = gif_image->width * gif_image->height;
+       mm_util_retvm_if(!gif_image, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid gif_image");
+       mm_util_retvm_if(!color_map, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid color_map");
+       mm_util_retvm_if(!intermediate_image, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid intermediate_image");
+       mm_util_retvm_if(!intermediate_image_size, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid intermediate_image_size");
 
-       /* make colormap and quantization for the color table of gif */
-       __gif_extract_rgb(gif_image, num_of_pixels, &red, &green, &blue);
-
-       if ((*color_map = GifMakeMapObject(colormap_size, NULL)) == NULL) {
-               mm_util_error("To make color map is  failed");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto FAIL;
-       }
+       num_of_pixels = gif_image->width * gif_image->height;
 
-       /* allocate output buffer */
-       *intermediate_image = (GifByteType *) calloc(1, num_of_pixels * sizeof(GifByteType));
-       if (*intermediate_image == NULL) {
-               mm_util_error("Memory allocation failed");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto FAIL;
+       if ((gif_color_map = GifMakeMapObject(colormap_size, NULL)) == NULL) {
+               mm_util_error("failed to make color map");
+               return MM_UTIL_ERROR_INVALID_OPERATION;
        }
-       *intermediate_image_size = num_of_pixels * sizeof(GifByteType);
 
-       if (GifQuantizeBuffer(gif_image->width, gif_image->height, &colormap_size, red, green, blue, *intermediate_image, (*color_map)->Colors) == GIF_ERROR) {
-               mm_util_error("could not quantize buffer");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto FAIL;
-       }
-       goto SUCCESS;
+       __gif_extract_rgb(gif_image, num_of_pixels, &red, &green, &blue);
 
-FAIL:
-       if (*intermediate_image)
-               MMUTIL_SAFE_FREE(*intermediate_image);
+       /* allocate output buffer */
+       out_buffer = g_new0(GifByteType, num_of_pixels);
 
-       *intermediate_image_size = 0;
-       COLORMAP_FREE(*color_map);
-SUCCESS:
+       ret = GifQuantizeBuffer(gif_image->width, gif_image->height, &colormap_size, red, green, blue, out_buffer, gif_color_map->Colors);
        g_free(red);
        g_free(green);
        g_free(blue);
 
-       return ret;
+       if (ret == GIF_ERROR) {
+               mm_util_error("failed to quantize buffer");
+               COLORMAP_FREE(gif_color_map);
+               g_free(out_buffer);
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       *color_map = gif_color_map;
+       *intermediate_image = out_buffer;
+       *intermediate_image_size = num_of_pixels * sizeof(GifByteType);
+
+       return MM_UTIL_ERROR_NONE;
 }
 
 static int __gif_encode_open_mem(gif_file_s *gif_file)
@@ -526,7 +522,7 @@ static int __gif_image_write_image(gif_file_s *gif_file, mm_image_info_s *gif_im
                FALSE, color_map) == GIF_ERROR) {
                mm_util_error("EGifPutImageDesc failed due to %s", GifErrorString(gif_file->GifFile->Error));
                COLORMAP_FREE(color_map);
-               MMUTIL_SAFE_FREE(intermediate_image);
+               g_free(intermediate_image);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
        /* release color map */
@@ -543,7 +539,7 @@ static int __gif_image_write_image(gif_file_s *gif_file, mm_image_info_s *gif_im
        }
 
        /* release intermediate_image */
-       MMUTIL_SAFE_FREE(intermediate_image);
+       g_free(intermediate_image);
 
        return MM_UTIL_ERROR_NONE;
 }