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;
}
}
}
- *data = buffer;
-
return MM_UTIL_ERROR_NONE;
}
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");
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) {