Code Refactoring - wrong parameter usages 86/171286/2
authorJiyong Min <jiyong.min@samsung.com>
Wed, 28 Feb 2018 07:35:04 +0000 (16:35 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Wed, 28 Feb 2018 07:49:03 +0000 (16:49 +0900)
Change-Id: Ic48a798223eb64b817302520a26a63320b24fc31
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
png/include/mm_util_png.h
png/mm_util_png.c
png/test/mm_util_png_testsuite.c

index b028359..a5d82e6 100755 (executable)
@@ -51,21 +51,8 @@ typedef enum {
 } mm_util_png_compression;
 
 typedef struct {
-       int bit_depth;                          /**< Bit depth*/
-       int color_type;                         /**< Color type*/
-       int interlace_type;                     /**< Interlace type */
-       int filter_type;                        /**< Filter type*/
-       size_t rowbytes;                        /**< Number or bytes in each row*/
-
-       int transform;                          /**< Transform masks for decoding */
-       int compression_level;                  /**< Compression level of encoding */
-       int filter;                             /**< Filter */
-} mm_util_png;
-
-typedef struct {
-       mm_util_png png;
-       uint32_t width;                 /**< Width*/
-       uint32_t height;                        /**< Height*/
+       unsigned long width;                    /**< Width*/
+       unsigned long height;                   /**< Height*/
        size_t size;                    /**< size */
        void *data;                             /**< data */
 } mm_util_png_data;
@@ -100,31 +87,30 @@ int mm_util_decode_from_png_memory(mm_util_png_data * decoded, void *memory, uns
 /**
  * This function extracts raw data to png file
  *
- * @param data     [in]     address of the raw data.
- *                          After using it, please free the allocated memory.
- * @param encoded  [out]    pointer of mm_util_png_data.
- *                          After using it, please free the allocated memory.
- * @param filename [out]    output file name on to which the encoded stream will be written.
+ * @param decoded  [in]     information of the raw data.
+ * @param compression_level  [in]     compression level
+ * @param fpath [out]    output file name on to which the encoded stream will be written.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
  * @see                     mm_util_png_data, mm_util_init_encode_png
  * @since                   R1, 1.0
  */
-int mm_util_encode_to_png_file(void **data, mm_util_png_data * encoded, const char *fpath, mm_util_png_compression compression_level);
+int mm_util_encode_to_png_file(mm_util_png_data *decoded, mm_util_png_compression compression_level, const char *fpath);
 
 /**
  * This function extracts raw data to memory
  *
- * @param data     [in]     address of the raw data.
- *                          After using it, please free the allocated memory.
- * @param encoded  [out]    pointer of mm_util_png_data.
+ * @param decoded  [in]     information of the raw data.
+ * @param compression_level  [in]     compression level
+ * @param data  [out]       pointer of encoded data.
+ * @param size  [out]       size of encoded data.
  *                          After using it, please free the allocated memory.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
  * @see                     mm_util_png_data, mm_util_init_encode_png
  * @since                   R1, 1.0
  */
-int mm_util_encode_to_png_memory(void **data, mm_util_png_data * encoded, mm_util_png_compression compression_level);
+int mm_util_encode_to_png_memory(mm_util_png_data *decoded, mm_util_png_compression compression_level, void **data, size_t *size);
 
 #ifdef __cplusplus
 }
index 58599f4..b66fb62 100755 (executable)
 #define png_infopp_NULL (png_infopp)NULL
 
 typedef struct {
+       uint32_t width;
+       uint32_t height;
+       int bit_depth;                          /**< Bit depth*/
+       int color_type;                         /**< Color type*/
+       int interlace_type;                     /**< Interlace type */
+       int filter_type;                        /**< Filter type*/
+
+       void *data;
+       size_t size;
+       size_t rowbytes;                        /**< Number or bytes in each row*/
+
+       int transform;                          /**< Transform masks for decoding */
+       int compression_level;                  /**< Compression level of encoding */
+       int filter;                             /**< Filter */
+} png_data_s;
+
+
+typedef struct {
        void *mem;
        png_size_t size;
-} read_data;
+} png_mem_s;
 
 static void __user_error_fn(png_structp png_ptr, png_const_charp error_msg)
 {
@@ -47,39 +65,37 @@ static void __user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
        mm_util_error("%s", warning_msg);
 }
 
-static void __dec_set_prop(mm_util_png_data *decoded, png_structp png_ptr, png_infop info)
+static void __dec_set_prop(png_data_s *png_data, png_structp png_ptr, png_infop info)
 {
        png_color_16 *image_background = NULL;
-       png_uint_32 _width = 0;
-       png_uint_32 _height = 0;
 
        mm_util_debug("__dec_set_prop");
 
        png_read_info(png_ptr, info);
 
-       png_get_IHDR(png_ptr, info, &_width, &_height, &decoded->png.bit_depth, &decoded->png.color_type, &decoded->png.interlace_type, NULL, &decoded->png.filter_type);
+       png_get_IHDR(png_ptr, info, &png_data->width, &png_data->height, &png_data->bit_depth, &png_data->color_type, &png_data->interlace_type, NULL, &png_data->filter_type);
 
        /* Get bits per channel */
-       decoded->png.bit_depth = png_get_bit_depth(png_ptr, info);
+       png_data->bit_depth = png_get_bit_depth(png_ptr, info);
 
        /* Get Color type */
-       decoded->png.color_type = png_get_color_type(png_ptr, info);
+       png_data->color_type = png_get_color_type(png_ptr, info);
 
        /* Gray scale converted to upscaled to 8 bits */
-       if ((decoded->png.color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || (decoded->png.color_type == PNG_COLOR_TYPE_GRAY)) {
+       if ((png_data->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || (png_data->color_type == PNG_COLOR_TYPE_GRAY)) {
                /* Gray scale with alpha channel converted to RGB */
-               if (decoded->png.color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+               if (png_data->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
                        png_set_gray_to_rgb(png_ptr);
-               if (decoded->png.bit_depth < MM_UTIL_BIT_DEPTH_8)/* Convert to 8 bits */
+               if (png_data->bit_depth < MM_UTIL_BIT_DEPTH_8)/* Convert to 8 bits */
                        png_set_expand_gray_1_2_4_to_8(png_ptr);
        }
        /* Palette converted to RGB */
-       else if (decoded->png.color_type == PNG_COLOR_TYPE_PALETTE)
+       else if (png_data->color_type == PNG_COLOR_TYPE_PALETTE)
                png_set_palette_to_rgb(png_ptr);
 
        /* Add alpha channel, but not for GRAY images */
-       if ((decoded->png.bit_depth >= MM_UTIL_BIT_DEPTH_8)
-               && (decoded->png.color_type != PNG_COLOR_TYPE_GRAY)) {
+       if ((png_data->bit_depth >= MM_UTIL_BIT_DEPTH_8)
+               && (png_data->color_type != PNG_COLOR_TYPE_GRAY)) {
                png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
        }
 
@@ -94,38 +110,29 @@ static void __dec_set_prop(mm_util_png_data *decoded, png_structp png_ptr, png_i
        /* Update the info structure */
        png_read_update_info(png_ptr, info);
 
-       decoded->width = (uint32_t)_width;
-       decoded->height = (uint32_t)_height;
-       decoded->png.rowbytes = (size_t)png_get_rowbytes(png_ptr, info);
-       decoded->size = (size_t)((size_t)(decoded->height) * decoded->png.rowbytes);
+       png_data->rowbytes = png_get_rowbytes(png_ptr, info);
+       png_data->size = ((size_t)(png_data->height) * png_data->rowbytes);
 }
 
 static void __read_function(png_structp png_ptr, png_bytep data, png_size_t size)
 {
-       read_data *read_data_ptr = (read_data *) png_get_io_ptr(png_ptr);
+       png_mem_s *png_mem_ptr = (png_mem_s *) png_get_io_ptr(png_ptr);
 
-       if (read_data_ptr->mem && size > 0) {
-               memcpy(data, read_data_ptr->mem + read_data_ptr->size, size);
-               read_data_ptr->size += size;
+       if (png_mem_ptr->mem && size > 0) {
+               memcpy(data, png_mem_ptr->mem + png_mem_ptr->size, size);
+               png_mem_ptr->size += size;
        }
 }
 
-void __init_decode_png(mm_util_png *png)
-{
-       mm_util_debug("mm_util_init_decode_png");
-       png->transform = PNG_TRANSFORM_IDENTITY;
-}
-
 static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
 {
        png_structp png_ptr;
        png_infop info_ptr;
        guint row_index;
-       read_data read_data_ptr;
+       png_mem_s png_mem_ptr;
+       png_data_s png_data;
 
        mm_util_debug("__read_png");
-       decoded->data = NULL;
-       __init_decode_png(&decoded->png);
 
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, __user_error_fn, __user_warning_fn);
 
@@ -145,11 +152,6 @@ static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
 
        if (setjmp(png_jmpbuf(png_ptr))) {
                mm_util_error("setjmp called due to internal libpng error");
-               if (decoded->data) {
-                       png_free(png_ptr, decoded->data);
-                       decoded->data = NULL;
-                       mm_util_debug("free data");
-               }
                png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
                mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
@@ -158,36 +160,44 @@ static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
        if (fp)
                png_init_io(png_ptr, fp);
        else if (memory) {
-               read_data_ptr.mem = memory;
-               read_data_ptr.size = 0;
-               png_set_read_fn(png_ptr, &read_data_ptr, __read_function);
+               png_mem_ptr.mem = memory;
+               png_mem_ptr.size = 0;
+               png_set_read_fn(png_ptr, &png_mem_ptr, __read_function);
        } else {
                png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
-       __dec_set_prop(decoded, png_ptr, info_ptr);
+       __dec_set_prop(&png_data, png_ptr, info_ptr);
+       if (png_data.size == 0) {
+               png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
 
-       {
-               png_bytep row_pointers[decoded->height];
+       png_bytep row_pointers[png_data.height];
 
-               for (row_index = 0; row_index < decoded->height; row_index++)
-                       row_pointers[row_index] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
+       for (row_index = 0; row_index < png_data.height; row_index++)
+               row_pointers[row_index] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
 
-               png_read_image(png_ptr, row_pointers);
+       png_read_image(png_ptr, row_pointers);
 
-               decoded->data = (void *)png_malloc(png_ptr, sizeof(png_bytep) * decoded->size);
+       decoded->data = (void *)png_malloc(png_ptr, sizeof(png_bytep) * png_data.size);
 
-               for (row_index = 0; row_index < decoded->height; row_index++) {
-                       memcpy(decoded->data + (row_index * decoded->png.rowbytes), row_pointers[row_index], decoded->png.rowbytes);
-                       png_free(png_ptr, row_pointers[row_index]);
-               }
+       for (row_index = 0; row_index < png_data.height; row_index++) {
+               memcpy(decoded->data + (row_index * png_data.rowbytes), row_pointers[row_index], png_data.rowbytes);
+               png_free(png_ptr, row_pointers[row_index]);
        }
 
        png_read_end(png_ptr, info_ptr);
        png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
        mm_util_safe_fclose(fp);
 
+       if (decoded->data) {
+               decoded->width = (unsigned long)png_data.width;
+               decoded->height = (unsigned long)png_data.height;
+               decoded->size = png_data.size;
+       }
+
        return MM_UTIL_ERROR_NONE;
 }
 
@@ -219,47 +229,48 @@ int mm_util_decode_from_png_memory(mm_util_png_data *decoded, void *memory, unsi
        return ret;
 }
 
-static void user_flush_data(png_structp png_ptr G_GNUC_UNUSED)
+static void __flush_data_fn(png_structp png_ptr G_GNUC_UNUSED)
 {
        /* do nothing */
 }
 
-static void user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
+static void __write_function(png_structp png_ptr, png_bytep data, png_uint_32 length)
 {
-       mm_util_png_data *encoded = (mm_util_png_data *) png_get_io_ptr(png_ptr);
+       png_mem_s *png_mem_ptr = (png_mem_s *) png_get_io_ptr(png_ptr);
 
        if (data) {
-               encoded->data = (void *)realloc(encoded->data, sizeof(png_bytep) * (encoded->size + length));
+               png_mem_ptr->mem = (void *)realloc(png_mem_ptr->mem, sizeof(png_bytep) * (png_mem_ptr->size + length));
 
-               memcpy(encoded->data + encoded->size, data, length);
-               encoded->size += length;
+               memcpy(png_mem_ptr->mem + png_mem_ptr->size, data, length);
+               png_mem_ptr->size += length;
        }
 }
 
-void __init_encode_png(mm_util_png *png)
+void __init_encode_png(png_data_s *png_data)
 {
-       mm_util_debug("mm_util_init_encode_png");
-       png->compression_level = MM_UTIL_COMPRESSION_6;
-       png->filter = PNG_FILTER_NONE;
-       png->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-       png->filter_type = PNG_FILTER_TYPE_BASE;
-       png->interlace_type = PNG_INTERLACE_NONE;
-       png->bit_depth = MM_UTIL_BIT_DEPTH_8;
+       png_data->compression_level = MM_UTIL_COMPRESSION_6;
+       png_data->filter = PNG_FILTER_NONE;
+       png_data->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+       png_data->filter_type = PNG_FILTER_TYPE_BASE;
+       png_data->interlace_type = PNG_INTERLACE_NONE;
+       png_data->bit_depth = MM_UTIL_BIT_DEPTH_8;
 }
 
-int write_png(void **data, mm_util_png_data *encoded, FILE *fp, mm_util_png_compression compression_level)
+int write_png(mm_util_png_data *decoded, mm_util_png_compression compression_level, FILE *fp, void **buffer, size_t *size)
 {
        png_structp png_ptr;
        png_infop info_ptr;
        guint row_index;
        static png_bytepp row_pointers = NULL;
+       png_mem_s png_mem_ptr;
+       png_data_s png_data;
 
        mm_util_debug("write_png");
-       encoded->data = NULL;
-       encoded->size = 0;
+       png_mem_ptr.mem = NULL;
+       png_mem_ptr.size = 0;
 
-       __init_encode_png(&encoded->png);
-       encoded->png.compression_level = compression_level;
+       __init_encode_png(&png_data);
+       png_data.compression_level = compression_level;
 
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, __user_error_fn, __user_warning_fn);
 
@@ -279,38 +290,27 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp, mm_util_png_comp
 
        if (setjmp(png_jmpbuf(png_ptr))) {
                mm_util_error("setjmp called due to internal libpng error");
-               if (encoded->data) {
-                       png_free(png_ptr, encoded->data);
-                       encoded->data = NULL;
-                       mm_util_debug("free data");
-               }
-               if (row_pointers)
-                       png_free(png_ptr, row_pointers);
                png_destroy_write_struct(&png_ptr, &info_ptr);
                mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
-       png_set_filter(png_ptr, 0, encoded->png.filter);
-       png_set_compression_level(png_ptr, encoded->png.compression_level);
-
-       /* convert color type */
-       if (encoded->png.color_type == MM_UTIL_COLOR_RGBA)
-               encoded->png.color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+       png_set_filter(png_ptr, 0, png_data.filter);
+       png_set_compression_level(png_ptr, png_data.compression_level);
 
-       png_set_IHDR(png_ptr, info_ptr, encoded->width, encoded->height, encoded->png.bit_depth, encoded->png.color_type, encoded->png.interlace_type, PNG_COMPRESSION_TYPE_BASE, encoded->png.filter_type);
+       png_set_IHDR(png_ptr, info_ptr, decoded->width, decoded->height, png_data.bit_depth, png_data.color_type, png_data.interlace_type, PNG_COMPRESSION_TYPE_BASE, png_data.filter_type);
 
        if (fp)
                png_init_io(png_ptr, fp);
        else
-               png_set_write_fn(png_ptr, encoded, (png_rw_ptr) user_write_data, user_flush_data);
+               png_set_write_fn(png_ptr, &png_mem_ptr, (png_rw_ptr) __write_function, __flush_data_fn);
 
-       encoded->png.rowbytes = (size_t)png_get_rowbytes(png_ptr, info_ptr);
+       png_data.rowbytes = (size_t)png_get_rowbytes(png_ptr, info_ptr);
 
-       row_pointers = png_malloc(png_ptr, sizeof(png_bytep) * encoded->height);
+       row_pointers = png_malloc(png_ptr, sizeof(png_bytep) * decoded->height);
 
-       for (row_index = 0; row_index < encoded->height; row_index++)
-               row_pointers[row_index] = (*data) + (row_index * encoded->png.rowbytes);
+       for (row_index = 0; row_index < decoded->height; row_index++)
+               row_pointers[row_index] = decoded->data + (row_index * png_data.rowbytes);
 
        png_write_info(png_ptr, info_ptr);
        png_write_image(png_ptr, row_pointers);
@@ -320,10 +320,15 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp, mm_util_png_comp
        png_destroy_write_struct(&png_ptr, &info_ptr);
        mm_util_safe_fclose(fp);
 
+       if (png_mem_ptr.mem) {
+               *buffer = (void *)png_mem_ptr.mem;
+               *size = png_mem_ptr.size;
+       }
+
        return MM_UTIL_ERROR_NONE;
 }
 
-int mm_util_encode_to_png_file(void **data, mm_util_png_data *encoded, const char *fpath, mm_util_png_compression compression_level)
+int mm_util_encode_to_png_file(mm_util_png_data *decoded, mm_util_png_compression compression_level, const char *fpath)
 {
        int ret = MM_UTIL_ERROR_NONE;
        FILE *fp;
@@ -338,18 +343,18 @@ int mm_util_encode_to_png_file(void **data, mm_util_png_data *encoded, const cha
                return ret;
        }
 
-       ret = write_png(data, encoded, fp, compression_level);
+       ret = write_png(decoded, compression_level, fp, NULL, NULL);
 
        return ret;
 }
 
-int mm_util_encode_to_png_memory(void **data, mm_util_png_data *encoded, mm_util_png_compression compression_level)
+int mm_util_encode_to_png_memory(mm_util_png_data *decoded, mm_util_png_compression compression_level, void **data, size_t *size)
 {
        int ret;
 
        mm_util_debug("mm_util_encode_to_memory");
 
-       ret = write_png(data, encoded, NULL, compression_level);
+       ret = write_png(decoded, compression_level, NULL, data, size);
 
        return ret;
 }
index c279ebd..22411d3 100755 (executable)
@@ -124,8 +124,6 @@ int main(int argc, char *argv[])
 {
        int ret = 0;
        mm_util_png_data decoded_data;
-       mm_util_png_data encoded_data;
-       void *data;
        void *src = NULL;
        int src_size = 0;
        mm_util_png_compression compression = MM_UTIL_COMPRESSION_6; /* default */
@@ -165,25 +163,20 @@ int main(int argc, char *argv[])
                                        snprintf(filename, BUFFER_SIZE, "%s%s", DECODE_RESULT_PATH, "png");
                                }
 
-                               data = calloc(1, decoded_data.size);
-                               memcpy(data, decoded_data.data, decoded_data.size);
-
                                if (argv[3])
                                        compression = atoi(argv[3]);
 
-                               encoded_data.width = decoded_data.width;
-                               encoded_data.height = decoded_data.height;
-
                                if (!strcmp("decode", argv[1])) {
-                                       ret = mm_util_encode_to_png_memory(&data, &encoded_data, compression);
-                                       fprintf(stderr, "Finished encoding encoded_data.size %lu\n", (long unsigned int)encoded_data.size);
-                                       _write_file(filename, encoded_data.data, encoded_data.size);
-                                       free(encoded_data.data);
+                                       void *encoded_data;
+                                       size_t size;
+                                       ret = mm_util_encode_to_png_memory(&decoded_data, compression, &encoded_data, &size);
+                                       fprintf(stderr, "Finished encoding encoded_data.size %zu\n", size);
+                                       _write_file(filename, encoded_data, size);
+                                       free(encoded_data);
                                } else if (!strcmp("decode-mem", argv[1])) {
-                                       ret = mm_util_encode_to_png_file(&data, &encoded_data, filename, compression);
+                                       ret = mm_util_encode_to_png_file(&decoded_data, compression, filename);
                                }
                                free(decoded_data.data);
-                               free(data);
                        } else {
                                fprintf(stderr, "\tDECODED data is NULL\n");
                        }