Modify jpeg encode for yuv to reduce Cyclomatic Complexity 46/215146/1
authorjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 04:11:02 +0000 (13:11 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 04:11:02 +0000 (13:11 +0900)
Change-Id: I1c39262ca93e0fe1daba718cfd9f1f4d3a64b598

jpeg/mm_util_jpeg.c

index 3aeabe1..4a876c2 100644 (file)
@@ -202,16 +202,81 @@ static int __jpeg_decode_get_buffer_size(j_decompress_ptr dinfo, unsigned int ro
        return MM_UTIL_ERROR_NONE;
 }
 
+static int __jpeg_encode_yuv(j_compress_ptr cinfo, unsigned int width, unsigned int height, mm_image_info_s* decoded)
+{
+       JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
+       JSAMPARRAY data[3] = { y, cb, cr }; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
+       unsigned int i = 0, j = 0;
+       void *large_rect = NULL;
+       void *small_rect= NULL;
+
+       if (cinfo->image_height - MM_UTIL_ROUND_DOWN_16(height)) {
+               large_rect = calloc(1, width);
+               small_rect = calloc(1, width);
+
+               if (!large_rect || !small_rect) {
+                       MMUTIL_SAFE_FREE(large_rect);
+                       MMUTIL_SAFE_FREE(small_rect);
+                       mm_util_error("Temprary rectangles are not allocated");
+                       return MM_UTIL_ERROR_INVALID_PARAMETER;
+               }
+
+               memset(large_rect, 0x10, width);
+               memset(small_rect, 0x80, width);
+
+               for (j = 0; j < height; j += 16) {
+                       for (i = 0; i < 16; i++) {
+                               y[i] = (JSAMPROW)decoded->data + width * (i + j);
+                               if (i % 2 == 0) {
+                                       cb[i / 2] = (JSAMPROW)decoded->data + width * height + width / 2 * ((i + j) / 2);
+                                       cr[i / 2] = (JSAMPROW)decoded->data + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                               }
+                       }
+                       jpeg_write_raw_data(cinfo, data, 16);
+               }
+               for (i = 0; i < cinfo->image_height - MM_UTIL_ROUND_DOWN_16(height); i++) {
+                       y[i] = (JSAMPROW)decoded->data + width * (i + j);
+                       if (i % 2 == 0) {
+                               cb[i / 2] = (JSAMPROW)decoded->data + width * height + width / 2 * ((i + j) / 2);
+                               cr[i / 2] = (JSAMPROW)decoded->data + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                       }
+               }
+               for (; i < 16; i++) {
+                       y[i] = (JSAMPROW)large_rect;
+                       if (i % 2 == 0) {
+                               cb[i / 2] = (JSAMPROW)small_rect;
+                               cr[i / 2] = (JSAMPROW)small_rect;
+                       }
+               }
+               jpeg_write_raw_data(cinfo, data, 16);
+               MMUTIL_SAFE_FREE(large_rect);
+               MMUTIL_SAFE_FREE(small_rect);
+
+               return MM_UTIL_ERROR_NONE;
+       } else {
+               for (j = 0; j < height; j += 16) {
+                       for (i = 0; i < 16; i++) {
+                               y[i] = (JSAMPROW)decoded->data + width * (i + j);
+                               if (i % 2 == 0) {
+                                       cb[i / 2] = (JSAMPROW)decoded->data + width * height + width / 2 * ((i + j) / 2);
+                                       cr[i / 2] = (JSAMPROW)decoded->data + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                               }
+                       }
+                       jpeg_write_raw_data(cinfo, data, 16);
+               }
+       }
+
+       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;
        mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
-       JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
-       JSAMPARRAY data[3] = { y, cb, cr }; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
 
        struct jpeg_compress_struct cinfo;
        my_error_mgr_s jerr;
-       unsigned int i, j, flag, _width, _height;
+       unsigned int _width, _height;
        unsigned long size = 0;
 
        mm_util_retvm_if((control_format != MM_UTIL_JPEG_FILE) && (control_format != MM_UTIL_JPEG_MEM), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid control_format [%u]", control_format);
@@ -232,8 +297,6 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
        _width = cinfo.image_width = _decoded->width;
        _height = cinfo.image_height = _decoded->height;
        if (_decoded->color == MM_UTIL_COLOR_YUV420 || _decoded->color == MM_UTIL_COLOR_YUV422 || _decoded->color == MM_UTIL_COLOR_UYVY) {
-               flag = cinfo.image_height - MM_UTIL_ROUND_DOWN_16(_height);
-
                cinfo.input_components = 3;
                cinfo.in_color_space = JCS_YCbCr;
                jpeg_set_defaults(&cinfo);
@@ -259,64 +322,9 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
                jpeg_start_compress(&cinfo, TRUE);
                mm_util_debug("jpeg_start_compress");
 
-               if (flag) {
-                       void *large_rect = calloc(1, _width);
-                       void *small_rect = calloc(1, _width);
-                       if (large_rect) {
-                               memset(large_rect, 0x10, _width);
-                       } else {
-                               MMUTIL_SAFE_FREE(small_rect);
-                               mm_util_error("large rectangle memory");
-                               return MM_UTIL_ERROR_INVALID_PARAMETER;
-                       }
-                       if (small_rect) {
-                               memset(small_rect, 0x80, _width);
-                       } else {
-                               MMUTIL_SAFE_FREE(large_rect);
-                               mm_util_error("small rectangle memory");
-                               return MM_UTIL_ERROR_INVALID_PARAMETER;
-                       }
-
-                       for (j = 0; j < _height; j += 16) {
-                               for (i = 0; i < 16; i++) {
-                                       y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
-                                       if (i % 2 == 0) {
-                                               cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
-                                               cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
-                                       }
-                               }
-                               jpeg_write_raw_data(&cinfo, data, 16);
-                       }
-                       for (i = 0; i < flag; i++) {
-                               y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
-                               if (i % 2 == 0) {
-                                       cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
-                                       cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
-                               }
-                       }
-                       for (; i < 16; i++) {
-                               y[i] = (JSAMPROW)large_rect;
-                               if (i % 2 == 0) {
-                                       cb[i / 2] = (JSAMPROW)small_rect;
-                                       cr[i / 2] = (JSAMPROW)small_rect;
-                               }
-                       }
-                       jpeg_write_raw_data(&cinfo, data, 16);
-                       MMUTIL_SAFE_FREE(large_rect);
-                       MMUTIL_SAFE_FREE(small_rect);
-               } else {
-                       for (j = 0; j < _height; j += 16) {
-                               for (i = 0; i < 16; i++) {
-                                       y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
-                                       if (i % 2 == 0) {
-                                               cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
-                                               cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
-                                       }
-                               }
-                               jpeg_write_raw_data(&cinfo, data, 16);
-                       }
-               }
-               mm_util_debug("#for loop#");
+               ret = __jpeg_encode_yuv(&cinfo, _width, _height, decoded);
+               if (ret != MM_UTIL_ERROR_NONE)
+                       mm_util_error("__jpeg_encode_yuv failed");
        }
 
        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) {
@@ -338,16 +346,17 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
                        row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
                        jpeg_write_scanlines(&cinfo, row_pointer, 1);
                }
-               mm_util_debug("while");
        } else {
                mm_util_error("We can't encode the IMAGE format");
+               jpeg_destroy_compress(&cinfo);
+               mm_util_debug("jpeg_destroy_compress");
                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. */
+       /* The size should been updated after compression is finished. */
        if (control_format == MM_UTIL_JPEG_MEM)
                *csize = (size_t)size;
 
@@ -391,9 +400,8 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
        jpeg_read_header(&dinfo, TRUE);
        mm_util_debug("jpeg_read_header");
 
-       __jpeg_decode_set_common_params(&dinfo, color_format, downscale);
-
        /* set parameters for decompression */
+       __jpeg_decode_set_common_params(&dinfo, color_format, downscale);
        __jpeg_convert_rgb_colorspace(color_format, &dinfo.out_color_space, NULL);
 
        /* Start decompressor */
@@ -417,7 +425,7 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
 
        ret = __jpeg_decode_get_buffer_size(&dinfo, row_stride, color_format, &image_buffer_size);
        if (ret != MM_UTIL_ERROR_NONE) {
-               mm_util_error("__jpeg_decode_calc_buffer_size failed");
+               mm_util_error("__jpeg_decode_get_buffer_size failed");
                goto END;
        }