Code cleanup. Separate allocating buffer from '__gif_convert_to_rgba' function 75/318675/4
authorJiyong <jiyong.min@samsung.com>
Wed, 22 Jan 2025 04:14:26 +0000 (13:14 +0900)
committerJiyong <jiyong.min@samsung.com>
Wed, 22 Jan 2025 05:22:47 +0000 (14:22 +0900)
Change-Id: I05f4fca619c7c4e28acb7e739cc36ca6c4daa9d4

gif/mm_util_gif.c

index 96d103efc2d2fcc7c1d72c90387cbd0373f03f6c..5abdee75a6bfb94ea8ed283c6ecd9984c9f6ca40 100644 (file)
@@ -184,27 +184,32 @@ static int __gif_get_extension(GifFileType *gif_image)
        return MM_UTIL_ERROR_NONE;
 }
 
-static int __gif_convert_to_rgba(void **data, ColorMapObject *color_map, GifRowType *frame_buffer, unsigned int width, unsigned int height)
+static int __gif_allocate_buffer(unsigned int width, unsigned int height, void **buffer, size_t *buffer_size)
+{
+       size_t image_size = width * height * 4;
+
+       if (!mm_util_is_proper_image_size(image_size))
+               return MM_UTIL_ERROR_OUT_OF_MEMORY;
+
+       *buffer = g_new0(GifByteType, image_size);
+       *buffer_size = image_size;
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+static int __gif_convert_to_rgba(GifByteType *buffer, ColorMapObject *color_map, GifRowType *frame_buffer, unsigned int width, unsigned int height)
 {
        unsigned int i, j;
        GifRowType gif_row;
        GifColorType *color_map_entry;
-       GifByteType *buffer;
-       size_t data_size = 0;
 
        mm_util_fenter();
 
-       data_size = width * height * 4;
-       if (!mm_util_is_proper_image_size(data_size))
-               return MM_UTIL_ERROR_OUT_OF_MEMORY;
-
-       buffer = g_new0(GifByteType, data_size);
        for (i = 0; i < height; i++) {
                gif_row = frame_buffer[i];
                for (j = 0; j < width; j++) {
                        if (gif_row[j] >= color_map->ColorCount) {
                                mm_util_error("invalid color index=%d, color count=%d", gif_row[j], color_map->ColorCount);
-                               g_free(buffer);
                                return MM_UTIL_ERROR_INVALID_OPERATION;
                        }
 
@@ -216,8 +221,6 @@ static int __gif_convert_to_rgba(void **data, ColorMapObject *color_map, GifRowT
                }
        }
 
-       *data = buffer;
-
        return MM_UTIL_ERROR_NONE;
 }
 
@@ -232,6 +235,7 @@ static int __read_gif(const char *file_path, void *memory, const size_t src_size
        ColorMapObject *ColorMap = NULL;
        gif_io_buf_s io_buf = { memory, src_size, 0 };
        void *image_buffer = NULL;
+       size_t image_buffer_size = 0;
 
        mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
 
@@ -299,17 +303,25 @@ static int __read_gif(const char *file_path, void *memory, const size_t src_size
                goto error;
        }
 
+       /* allocate image buffer */
+       ret = __gif_allocate_buffer(GifFile->SWidth, GifFile->SHeight, &image_buffer, &image_buffer_size);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__gif_alloc_buffer failed");
+               goto error;
+       }
+
        /* decompress image with colormap(256) */
-       ret = __gif_convert_to_rgba(&image_buffer, ColorMap, frame_buffer, GifFile->SWidth, GifFile->SHeight);
+       ret = __gif_convert_to_rgba(image_buffer, ColorMap, frame_buffer, GifFile->SWidth, GifFile->SHeight);
        if (ret != MM_UTIL_ERROR_NONE) {
                mm_util_error("__gif_convert_to_rgba failed");
                goto error;
        }
 
-       ret = mm_image_create_image(GifFile->SWidth, GifFile->SHeight, MM_UTIL_COLOR_RGBA, image_buffer, GifFile->SWidth * GifFile->SHeight * 4, decoded);
-       g_free(image_buffer);
+       ret = mm_image_create_image(GifFile->SWidth, GifFile->SHeight, MM_UTIL_COLOR_RGBA, image_buffer, image_buffer_size, decoded);
 
 error:
+       g_free(image_buffer);
+
        __gif_free_frame_buffer(frame_buffer, GifFile->SHeight);
 
        if (DGifCloseFile(GifFile, NULL) == GIF_ERROR) {