Code Cleanup unneccessary variables and functions 56/158356/4 accepted/tizen/unified/20171101.064755 submit/tizen/20171101.024016
authorJiyong Min <jiyong.min@samsung.com>
Tue, 31 Oct 2017 08:14:08 +0000 (17:14 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Wed, 1 Nov 2017 02:16:10 +0000 (11:16 +0900)
 - Remove frames to fix memory leak (decoder supports 1 frame)
 - Remove GIF_ENCODER_V2 define
 - Remove unused functions after frames and define is deleted

Change-Id: Ie279fe4b83e4cb1de7b16b70ec2ac5a82f02df6f
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
gif/include/mm_util_gif.h
gif/mm_util_gif.c
gif/test/mm_util_gif_testsuite.c
packaging/libmm-utility.spec

index 7430e3b..dd7611a 100755 (executable)
@@ -83,7 +83,6 @@ typedef struct {
        void *data;                             /**< Data */
 } mm_util_gif_frame_data;
 
-#ifdef GIF_ENCODER_V2
 #define GRAPHIC_EXT_BLOCK_SIZE 4
 #define APP_EXT_BLOCK_SIZE             11
 #define APP_EXT_BLOCK_DATA             "NETSCAPE2.0"
@@ -99,7 +98,6 @@ typedef struct {
 typedef void* mm_gif_image_h;
 
 typedef void* mm_gif_file_h;
-#endif
 
 typedef struct {
        unsigned long width;                      /**< Width */
@@ -110,7 +108,7 @@ typedef struct {
        unsigned int current_count;               /**< ImageCount */
        write_data write_data_ptr;                /**< Encoded output data attached to callback */
        GifFileType *GifFile;                     /**< GifFile opened */
-       mm_util_gif_frame_data **frames;          /**< Frames */
+       void *data;       /**< Frames */
 } mm_util_gif_data;
 
 /**
@@ -190,7 +188,6 @@ int mm_util_encode_gif(mm_util_gif_data *encoded);
  */
 int mm_util_encode_close_gif(mm_util_gif_data *encoded);
 
-#ifdef GIF_ENCODER_V2
 /**
  * This function creates the handle of the single image(frame).
  *
@@ -429,7 +426,6 @@ int mm_util_gif_encode(mm_gif_file_h gif_file_h);
  * @pre                                        mm_util_gif_encode_create
  */
 void mm_util_gif_encode_destroy(mm_gif_file_h gif_file_h);
-#endif
 
 #ifdef __cplusplus
 }
index 4cf2c57..99c782c 100755 (executable)
@@ -51,24 +51,12 @@ static int __convert_gif_to_rgba(mm_util_gif_data *decoded, ColorMapObject *colo
        GifByteType *buffer;
 
        mm_util_debug("__convert_gif_to_rgba");
-       if ((decoded->frames = (mm_util_gif_frame_data **) realloc(decoded->frames, sizeof(mm_util_gif_frame_data *)))
-               == NULL) {
+       if ((decoded->data = (void *)malloc(width * height * 4)) == NULL) {
                mm_util_error("Failed to allocate memory required, aborted.");
                return MM_UTIL_ERROR_OUT_OF_MEMORY;
        }
 
-       if ((decoded->frames[0] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data)))
-               == NULL) {
-               mm_util_error("Failed to allocate memory required, aborted.");
-               return MM_UTIL_ERROR_OUT_OF_MEMORY;
-       }
-
-       if ((decoded->frames[0]->data = (void *)malloc(width * height * 4)) == NULL) {
-               mm_util_error("Failed to allocate memory required, aborted.");
-               return MM_UTIL_ERROR_OUT_OF_MEMORY;
-       }
-
-       buffer = (GifByteType *) decoded->frames[0]->data;
+       buffer = (GifByteType *) decoded->data;
        for (i = 0; i < height; i++) {
                gif_row = screen_buffer[i];
                for (j = 0; j < width; j++) {
@@ -243,7 +231,12 @@ static int __read_gif(mm_util_gif_data *decoded, const char *filename, void *mem
                goto error;
        }
 
-       __convert_gif_to_rgba(decoded, ColorMap, screen_buffer, GifFile->SWidth, GifFile->SHeight);
+       ret = __convert_gif_to_rgba(decoded, ColorMap, screen_buffer, GifFile->SWidth, GifFile->SHeight);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("could not convert gif to rgba");
+               ret = MM_UTIL_ERROR_INVALID_OPERATION;
+               goto error;
+       }
 
        ret = MM_UTIL_ERROR_NONE;
 error:
@@ -285,97 +278,6 @@ int mm_util_decode_from_gif_memory(mm_util_gif_data *decoded, void **memory)
        return ret;
 }
 
-static void __load_rgb_from_buffer(GifByteType *buffer, GifByteType **red, GifByteType **green, GifByteType **blue, unsigned long width, unsigned long height)
-{
-       unsigned long i, j;
-       unsigned long Size;
-       GifByteType *redP, *greenP, *blueP;
-
-       mm_util_debug("__load_rgb_from_buffer");
-       Size = ((long)width) * height * sizeof(GifByteType);
-
-       if ((*red = (GifByteType *) malloc((unsigned int)Size)) == NULL || (*green = (GifByteType *) malloc((unsigned int)Size)) == NULL || (*blue = (GifByteType *) malloc((unsigned int)Size)) == NULL) {
-               mm_util_error("Failed to allocate memory required, aborted.");
-               return;
-       }
-
-       redP = *red;
-       greenP = *green;
-       blueP = *blue;
-
-       for (i = 0; i < height; i++) {
-               for (j = 0; j < width; j++) {
-                       *redP++ = *buffer++;
-                       *greenP++ = *buffer++;
-                       *blueP++ = *buffer++;
-                       buffer++;
-               }
-       }
-       return;
-}
-
-static int __save_buffer_to_gif(GifFileType *GifFile, GifByteType *OutputBuffer, ColorMapObject *OutputColorMap, mm_util_gif_frame_data * frame)
-{
-       unsigned long i, j, pixels = 0;
-       GifByteType *Ptr = OutputBuffer;
-       static unsigned char extension[4] = {0, };
-       int z;
-       int transparent_index = 0, minDist = 256 * 256 * 256;
-       GifColorType *color;
-
-       mm_util_debug("__save_buffer_to_gif");
-
-       if (GifFile == NULL) {
-               mm_util_error("Invalid parameter: GifFileType");
-               return MM_UTIL_ERROR_INVALID_PARAMETER;
-       }
-
-       /* Find closest color in first color map for the transparent color. */
-       color = OutputColorMap->Colors;
-       for (z = 0; z < OutputColorMap->ColorCount; z++) {
-               int dr = ABS(color[z].Red - frame->transparent_color.Red);
-               int dg = ABS(color[z].Green - frame->transparent_color.Green);
-               int db = ABS(color[z].Blue - frame->transparent_color.Blue);
-
-               int dist = dr * dr + dg * dg + db * db;
-
-               if (minDist > dist) {
-                       minDist = dist;
-                       transparent_index = z;
-               }
-       }
-
-       extension[0] = frame->disposal_mode << 2;
-       if (frame->is_transparent) {
-               extension[0] |= 1;
-               extension[3] = transparent_index;
-       } else {
-               extension[3] = -1;
-       }
-       extension[1] = frame->delay_time % 256;
-       extension[2] = frame->delay_time / 256;
-
-       /* Dump graphics control block. */
-       EGifPutExtension(GifFile, GRAPHICS_EXT_FUNC_CODE, 4, extension);
-
-       if (EGifPutImageDesc(GifFile, frame->x, frame->y, frame->width, frame->height, false, OutputColorMap) == GIF_ERROR) {
-               mm_util_error("could not put image description");
-               return MM_UTIL_ERROR_INVALID_OPERATION;
-       }
-
-       for (i = 0; i < frame->height; i++) {
-               for (j = 0; j < frame->width; j++) {
-                       unsigned char c = Ptr[pixels++];
-                       if (EGifPutPixel(GifFile, c) == GIF_ERROR) {
-                               mm_util_error("could not put pixel");
-                               return MM_UTIL_ERROR_INVALID_OPERATION;
-                       }
-               }
-       }
-
-       return MM_UTIL_ERROR_NONE;
-}
-
 static int __write_function(GifFileType *gft, const GifByteType *data, int size)
 {
        write_data *write_data_ptr = (write_data *) gft->UserData;
@@ -437,98 +339,6 @@ int mm_util_encode_close_gif(mm_util_gif_data *encoded)
        return MM_UTIL_ERROR_NONE;
 }
 
-static int __write_gif(mm_util_gif_data *encoded)
-{
-       int ColorMapSize;
-       unsigned int i = encoded->current_count;
-       GifByteType *red = NULL, *green = NULL, *blue = NULL, *OutputBuffer = NULL;
-       ColorMapObject *OutputColorMap = NULL;
-
-       if (encoded->GifFile == NULL) {
-               mm_util_error("Invalid parameter");
-               return MM_UTIL_ERROR_INVALID_PARAMETER;
-       }
-
-       if (!encoded->screen_desc_updated) {
-               if (EGifPutScreenDesc(encoded->GifFile, encoded->frames[0]->width, encoded->frames[0]->height, 8, 0, NULL) == GIF_ERROR) {
-                       mm_util_error("could not put screen description");
-                       mm_util_encode_close_gif(encoded);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-               encoded->screen_desc_updated = true;
-       }
-
-       for (i = encoded->current_count; i < encoded->image_count; i++) {
-               /* allocate output buffer */
-               if ((OutputBuffer = (GifByteType *) malloc(encoded->frames[i]->width * encoded->frames[i]->height * sizeof(GifByteType))) == NULL) {
-                       mm_util_error("Failed to allocate memory required, aborted.");
-                       mm_util_encode_close_gif(encoded);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-
-               /* make colormap and quantization for the color table of gif */
-               ColorMapSize = 1 << 8;
-               __load_rgb_from_buffer((GifByteType *) encoded->frames[i]->data, &red, &green, &blue, encoded->frames[i]->width, encoded->frames[i]->height);
-
-               if ((OutputColorMap = GifMakeMapObject(ColorMapSize, NULL)) == NULL) {
-                       mm_util_error("could not map object");
-                       RGB_FREE(red, green, blue);
-                       MMUTIL_SAFE_FREE(OutputBuffer);
-                       mm_util_encode_close_gif(encoded);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-               if (GifQuantizeBuffer(encoded->frames[i]->width, encoded->frames[i]->height, &ColorMapSize, red, green, blue, OutputBuffer, OutputColorMap->Colors) == GIF_ERROR) {
-                       mm_util_error("could not quantize buffer");
-                       RGB_FREE(red, green, blue);
-                       MMUTIL_SAFE_FREE(OutputBuffer);
-                       GifFreeMapObject(OutputColorMap);
-                       mm_util_encode_close_gif(encoded);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-               RGB_FREE(red, green, blue);
-
-               encoded->frames[i]->transparent_color.Red = 0xff;
-               encoded->frames[i]->transparent_color.Green = 0xff;
-               encoded->frames[i]->transparent_color.Blue = 0xff;
-
-               if (!encoded->frames[i]->x && !encoded->frames[i]->y) {
-                       encoded->frames[i]->x = (encoded->frames[0]->width - encoded->frames[i]->width) / 2;
-                       encoded->frames[i]->y = (encoded->frames[0]->height - encoded->frames[i]->height) / 2;
-               }
-
-               encoded->frames[i]->disposal_mode = MM_UTIL_GIF_DISPOSAL_UNSPECIFIED;
-               encoded->frames[i]->is_transparent = false;
-
-               /* encode OutputBuffer to GifFile */
-               if (__save_buffer_to_gif(encoded->GifFile, OutputBuffer, OutputColorMap, encoded->frames[i]) != MM_UTIL_ERROR_NONE) {
-                       mm_util_error("save_buffer_to_gif is failed");
-                       MMUTIL_SAFE_FREE(OutputBuffer);
-                       GifFreeMapObject(OutputColorMap);
-                       mm_util_encode_close_gif(encoded);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-
-               MMUTIL_SAFE_FREE(OutputBuffer);
-               GifFreeMapObject(OutputColorMap);
-               encoded->current_count++;
-       }
-       encoded->size = encoded->write_data_ptr.size;
-
-       mm_util_encode_close_gif(encoded);
-       return MM_UTIL_ERROR_NONE;
-}
-
-int mm_util_encode_gif(mm_util_gif_data *encoded)
-{
-       int ret;
-
-       mm_util_debug("mm_util_encode_gif");
-       ret = __write_gif(encoded);
-
-       return ret;
-}
-
-#ifdef GIF_ENCODER_V2
 static const int DEFAULT_COLORMAP_SIZE = (1 << 8);
 
 int _gif_image_alloc_glob_ext_block(GifFileType *gif_file, int function, int byte_count, ExtensionBlock **ext_block);
@@ -1193,31 +1003,6 @@ int mm_util_gif_encode_set_resolution(mm_gif_file_h gif_file_h, const int width,
        return MM_UTIL_ERROR_NONE;
 }
 
-#if 0
-int mm_util_gif_encode_set_bg_color(mm_gif_file_h gif_file_h, const mm_util_gif_color_s bg_color)
-{
-       gif_file_s *gif_file = (gif_file_s *)gif_file_h;
-       GifColorType *colors = NULL;
-       int color_count = 1 << gif_file->color_res;
-
-       mm_util_retvm_if(gif_file == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
-
-       colors = (GifColorType *)calloc(1, color_count * sizeof(GifColorType));
-       mm_util_retvm_if(colors == NULL, MM_UTIL_ERROR_OUT_OF_MEMORY, "memory allocation failed");
-
-       colors[0].Red = bg_color.red;
-       colors[0].Green = bg_color.green;
-       colors[0].Blue = bg_color.blue;
-
-       gif_file->color_map = GifMakeMapObject(color_count, colors);
-       mm_util_retvm_if(gif_file->color_map == NULL, MM_UTIL_ERROR_INVALID_OPERATION, "could not make color map");
-
-       gif_file->background_color = 0; /* index of color map */
-
-       return MM_UTIL_ERROR_NONE;
-}
-#endif
-
 int mm_util_gif_encode_set_repeat(mm_gif_file_h gif_file_h, gboolean repeat_mode, const unsigned short repeat_count)
 {
        gif_file_s *gif_file = (gif_file_s *)gif_file_h;
@@ -1450,4 +1235,3 @@ void mm_util_gif_encode_destroy(mm_gif_file_h gif_file_h)
        MMUTIL_SAFE_FREE(gif_file->filename);
        MMUTIL_SAFE_FREE(gif_file->buffer);
 }
-#endif
index 9512e8a..d42d995 100755 (executable)
 #define ENCODED_FILEPATH               "/opt/usr/home/owner/media/mm_util_test.gif"
 #define ANIMATED_FRAME_MAX     100
 
-#ifdef GIF_ENCODER_V2
 static gboolean g_encode_mem = FALSE;
-#endif
 
 typedef struct {
        char file_name[PATH_MAX];
        mm_util_gif_data decoded;
 } gif_test_data_s;
 
-#ifdef GIF_ENCODER_V2
+
 static int _write_file(const char *file_name, void *data, size_t data_size)
 {
        FILE *fp = NULL;
@@ -68,7 +66,6 @@ static int _write_file(const char *file_name, void *data, size_t data_size)
 
        return TRUE;
 }
-#endif
 
 static inline void flush_stdin()
 {
@@ -85,9 +82,7 @@ int main(int argc, char *argv[])
        if (argc < 2) {
                fprintf(stderr, "\t[usage]\n");
                fprintf(stderr, "\t\t1. decode : %s decode filepath\n", argv[0]);
-#ifdef GIF_ENCODER_V2
                fprintf(stderr, "\t\t2. encode-agif/encode-mem-agif : %s encode-agif dirpath\n", argv[0]);
-#endif
                return 0;
        }
 
@@ -98,9 +93,7 @@ int main(int argc, char *argv[])
                        return 0;
                }
                nfiles++;
-       }
-#ifdef GIF_ENCODER_V2
-       else if (!strcmp("encode-agif", argv[1]) || !strcmp("encode-mem-agif", argv[1])) {
+       } else if (!strcmp("encode-agif", argv[1]) || !strcmp("encode-mem-agif", argv[1])) {
                if (!strcmp("encode-mem-agif", argv[1]))
                        g_encode_mem = TRUE;
 
@@ -138,9 +131,7 @@ int main(int argc, char *argv[])
                                }
                        }
                }
-       }
-#endif
-       else {
+       } else {
                fprintf(stderr, "\tunknown command [%s]\n", argv[1]);
                return 0;
        }
@@ -156,7 +147,6 @@ int main(int argc, char *argv[])
                }
        }
 
-#ifdef GIF_ENCODER_V2
        mm_gif_file_h gif_file = NULL;
        mm_gif_image_h gif_image = NULL;
        unsigned char *encoded_gif_mem = NULL;
@@ -179,33 +169,27 @@ int main(int argc, char *argv[])
        fprintf(stderr, "\t mm_util_gif_enc_set_resolution [%d]\n", ret);
        /* repeat */
        for (i = 0; i < nfiles; i++) {
-               if (files[i].decoded.frames) {
+               if (files[i].decoded.data) {
                        ret = mm_util_gif_image_create(gif_file, &gif_image);
                        fprintf(stderr, "\t mm_util_gif_image_create [%d]\n", ret);
-                       ret = mm_util_gif_image_set_image(gif_image, files[i].decoded.frames[0]->data);
+
+                       ret = mm_util_gif_image_set_image(gif_image, files[i].decoded.data);
                        fprintf(stderr, "\t mm_util_gif_image_set_image [%d]\n", ret);
+
                        ret = mm_util_gif_image_set_delay_time(gif_image, 20);
                        fprintf(stderr, "\t mm_util_gif_image_set_delay_time [%d]\n", ret);
+
                        ret = mm_util_gif_image_set_position(gif_image, 0, 0, files[i].decoded.width, files[i].decoded.height);
                        fprintf(stderr, "\t mm_util_gif_image_set_position [%d]\n", ret);
+
                        ret = mm_util_gif_image_set_disposal_mode(gif_image, MM_UTIL_GIF_DISPOSAL_UNSPECIFIED);
                        fprintf(stderr, "\t mm_util_gif_image_set_disposal_mode [%d]\n", ret);
-#if 0  /* for advanced */
-                       if (files[i].decoded.frames[0]->is_transparent) {
-                               mm_util_gif_color_s tp_color;
-                               tp_color.red = files[i].decoded.frames[0]->transparent_color.Red;
-                               tp_color.green = files[i].decoded.frames[0]->transparent_color.Green;
-                               tp_color.blue = files[i].decoded.frames[0]->transparent_color.Blue;
-                               ret = mm_util_gif_image_set_tp_color(gif_image, tp_color);
-                               fprintf(stderr, "\t mm_util_gif_image_set_tp_color [%d]\n", ret);
-                       }
-#endif
+
                        ret = mm_util_gif_encode_add_image(gif_file, gif_image);
                        fprintf(stderr, "\t mm_util_gif_enc_add_image [%d]\n", ret);
-                       mm_util_gif_image_destory(gif_image);
 
-                       free(files[i].decoded.frames[0]->data);
-                       free(files[i].decoded.frames);
+                       mm_util_gif_image_destory(gif_image);
+                       free(files[i].decoded.data);
                }
        }
        /* repeat */
@@ -214,19 +198,6 @@ int main(int argc, char *argv[])
        if (g_encode_mem)
                _write_file(ENCODED_FILEPATH, (void *)encoded_gif_mem, (size_t)encoded_gif_size);
        mm_util_gif_encode_destroy(gif_file);
-#else
-       if (files[0].decoded.frames) {
-               fprintf(stderr, "\t##Decoded data##: %p\t width: %lu\t height:%lu\t size: %llu\n", files[0].decoded.frames[0], files[0].decoded.width, files[0].decoded.height, files[0].decoded.size);
-               files[0].decoded.image_count = 1;
-               ret = mm_util_encode_open_gif_file(&files[0].decoded, ENCODED_FILEPATH);
-               ret = mm_util_encode_gif(&files[0].decoded);
-               ret = mm_util_encode_close_gif(&files[0].decoded);
-
-               free(files[0].decoded.frames[0]->data);
-               free(files[0].decoded.frames);
-       } else {
-               fprintf(stderr, "\tDECODED data is NULL\n");
-       }
-#endif
+
        return 0;
 }
index 756db84..e2b2d6c 100755 (executable)
@@ -55,7 +55,6 @@ export CFLAGS+=" -Wextra -Wno-array-bounds"
 export CFLAGS+=" -Wno-ignored-qualifiers -Wno-unused-parameter -Wshadow"
 export CFLAGS+=" -Wwrite-strings -Wswitch-default"
 export CFLAGS+=" -Werror"
-export CFLAGS+=" -DGIF_ENCODER_V2"
 CFLAGS="$CFLAGS -DEXPORT_API=\"__attribute__((visibility(\\\"default\\\")))\" -D_MM_PROJECT_FLOATER" \
 LDFLAGS="$LDFLAGS -Wl,--rpath=%{_libdir} -Wl,--hash-style=both -Wl,--as-needed" \
 %reconfigure