Modify duplicated code for error handler and resource release. 42/215142/1
authorjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 00:44:53 +0000 (09:44 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 00:44:53 +0000 (09:44 +0900)
Change-Id: I3f5d1b01ccfedf81fff80489a74c2a2459a90b85

jpeg/mm_util_jpeg.c

index 01633bb..b84116f 100644 (file)
@@ -38,12 +38,13 @@ typedef enum {
        MM_UTIL_JPEG_MEM,
 } mm_util_jpeg_cont_format_e;
 
-struct my_error_mgr_s {
+typedef struct my_error_mgr_s {
        struct jpeg_error_mgr pub; /* "public" fields */
        jmp_buf setjmp_buffer; /* for return to caller */
 } my_error_mgr_s;
 
 typedef struct my_error_mgr_s *my_error_ptr;
+typedef struct jpeg_error_mgr *jpeg_error_ptr;
 
 static void __my_error_exit(j_common_ptr cinfo)
 {
@@ -88,6 +89,25 @@ static gboolean _mm_util_is_supported_color_format(mm_util_color_format_e color_
        return _bool;
 }
 
+static int __jpeg_set_error_handler(my_error_ptr jerr, jpeg_error_ptr *err)
+{
+       (jerr->pub).error_exit = __my_error_exit;
+       mm_util_debug("jerr.pub.error_exit ");
+
+       *err = jpeg_std_error((jpeg_error_ptr)&(jerr->pub));
+       mm_util_debug("jpeg_std_error ");
+
+       /* Establish the setjmp return context for __my_error_exit to use. */
+       if (setjmp(jerr->setjmp_buffer)) {
+               /* If we get here, the JPEG code has signaled an error.  We need to clean up the JPEG object, close the input file, and return.*/
+               mm_util_error("ERROR setjmp");
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
+       mm_util_debug("if (setjmp)");
+
+       return MM_UTIL_ERROR_NONE;
+}
+
 static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_util_image_h decoded, int quality, FILE *fp, void **mem, size_t *csize)
 {
        int ret = MM_UTIL_ERROR_NONE;
@@ -96,7 +116,7 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
        JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
 
        struct jpeg_compress_struct cinfo;
-       struct my_error_mgr_s jerr;
+       my_error_mgr_s jerr;
        int i, j, flag, _width, _height;
        unsigned long size = 0;
 
@@ -111,18 +131,8 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
 
        mm_util_debug("quality[%d]", quality);
 
-       /*      Errors get written to stderr */
-       cinfo.err = jpeg_std_error(&jerr.pub);
-       mm_util_debug("jpeg_std_error ");
-       jerr.pub.error_exit = __my_error_exit;
-       mm_util_debug("jerr.pub.error_exit ");
-
-       /* Establish the setjmp return context for __my_error_exit to use. */
-       if (setjmp(jerr.setjmp_buffer)) {
-               /* If we get here, the JPEG code has signaled an error.  We need to clean up the JPEG object, close the input file, and return.*/
-               mm_util_error("ERROR setjmp");
-               return MM_UTIL_ERROR_INVALID_OPERATION;
-       }
+       ret = __jpeg_set_error_handler(&jerr, &cinfo.err);
+       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "Can't set error handler");
 
        jpeg_create_compress(&cinfo);
 
@@ -222,12 +232,6 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
                        }
                }
                mm_util_debug("#for loop#");
-
-               jpeg_finish_compress(&cinfo);
-               mm_util_debug("jpeg_finish_compress");
-
-               jpeg_destroy_compress(&cinfo);
-               mm_util_debug("jpeg_destroy_compress");
        }
 
        else if (_decoded->color == MM_UTIL_COLOR_RGB24 || _decoded->color == MM_UTIL_COLOR_GRAYSCALE || _decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB) {
@@ -275,20 +279,21 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
                        jpeg_write_scanlines(&cinfo, row_pointer, 1);
                }
                mm_util_debug("while");
-
-               jpeg_finish_compress(&cinfo);
-               mm_util_debug("jpeg_finish_compress");
-
-               jpeg_destroy_compress(&cinfo);
-               mm_util_debug("jpeg_destroy_compress");
        } else {
                mm_util_error("We can't encode the IMAGE format");
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        }
 
+       jpeg_finish_compress(&cinfo);
+       mm_util_debug("jpeg_finish_compress");
+
+       /* The size should been updated after compress finished. */
        if (control_format == MM_UTIL_JPEG_MEM)
                *csize = (size_t)size;
 
+       jpeg_destroy_compress(&cinfo);
+       mm_util_debug("jpeg_destroy_compress");
+
        return ret;
 }
 
@@ -296,7 +301,7 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
 {
        int ret = MM_UTIL_ERROR_NONE;
        struct jpeg_decompress_struct dinfo;
-       struct my_error_mgr_s jerr;
+       my_error_mgr_s jerr;
        JSAMPARRAY buffer; /* Output row buffer */
        int row_stride = 0; /* physical row width in output buffer */
        JSAMPROW image, u_image, v_image;
@@ -312,19 +317,9 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
        mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
 
        /* allocate and initialize JPEG decompression object   We set up the normal JPEG error routines, then override error_exit. */
-       dinfo.err = jpeg_std_error(&jerr.pub);
-       mm_util_debug("jpeg_std_error ");
-       jerr.pub.error_exit = __my_error_exit;
-       mm_util_debug("jerr.pub.error_exit ");
+       ret = __jpeg_set_error_handler(&jerr, &dinfo.err);
+       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "Can't set error handler");
 
-       /* Establish the setjmp return context for __my_error_exit to use. */
-       if (setjmp(jerr.setjmp_buffer)) {
-               /* If we get here, the JPEG code has signaled an error.  We need to clean up the JPEG object, close the input file, and return.*/
-               mm_util_error("ERROR setjmp");
-               return MM_UTIL_ERROR_INVALID_OPERATION;
-       }
-
-       mm_util_debug("if (setjmp)");
        /* Now we can initialize the JPEG decompression object. */
        jpeg_create_decompress(&dinfo);
        mm_util_debug("jpeg_create_decompress");
@@ -416,10 +411,9 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
 
        _data = (void *) calloc(1, _size);
        if (!_data) {
-               jpeg_finish_decompress(&dinfo);
-               jpeg_destroy_decompress(&dinfo);
                mm_util_error("_data is NULL");
-               return MM_UTIL_ERROR_OUT_OF_MEMORY;
+               ret = MM_UTIL_ERROR_OUT_OF_MEMORY;
+               goto END;
        }
        mm_util_debug("decoded_data->data");
 
@@ -461,6 +455,7 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
        ret = mm_image_create_image(dinfo.output_width, dinfo.output_height, color_format, _data, _size, decoded);
        MMUTIL_SAFE_FREE(_data);
 
+END:
        /* Finish decompression */
        jpeg_finish_decompress(&dinfo);
        mm_util_debug("jpeg_finish_decompress");