Modify getting rgb colorspace and buffer size 43/215143/1
authorjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 01:17:27 +0000 (10:17 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Wed, 2 Oct 2019 01:46:20 +0000 (10:46 +0900)
Change-Id: Icc70a773c0b3214499726d3387fb8202b9420bd0

jpeg/mm_util_jpeg.c

index b84116f..101709a 100644 (file)
@@ -108,6 +108,56 @@ static int __jpeg_set_error_handler(my_error_ptr jerr, jpeg_error_ptr *err)
        return MM_UTIL_ERROR_NONE;
 }
 
+static void __jpeg_convert_rgb_colorspace(mm_util_color_format_e mm_color_format, J_COLOR_SPACE *j_color_space, int *j_color_comp)
+{
+       int pixel_depth = 0;
+
+       if (mm_color_format == MM_UTIL_COLOR_RGB24) {
+               pixel_depth = 3;
+               *j_color_space = JCS_RGB;
+               mm_util_debug("JCS_RGB");
+       } else if (mm_color_format == MM_UTIL_COLOR_GRAYSCALE) {
+               pixel_depth = 1; /* one colour component */
+               *j_color_space = JCS_GRAYSCALE;
+               mm_util_debug("JCS_GRAYSCALE");
+       } else if (mm_color_format == MM_UTIL_COLOR_RGBA) {
+               pixel_depth = 4;
+               *j_color_space = JCS_EXT_RGBA;
+               mm_util_debug("JCS_EXT_RGBA");
+       } else if (mm_color_format == MM_UTIL_COLOR_BGRA) {
+               pixel_depth = 4;
+               *j_color_space = JCS_EXT_BGRA;
+               mm_util_debug("JCS_EXT_BGRA");
+       } else if (mm_color_format == MM_UTIL_COLOR_ARGB) {
+               pixel_depth = 4;
+               *j_color_space = JCS_EXT_ARGB;
+               mm_util_debug("JCS_EXT_ARGB");
+       }
+
+       if (j_color_comp)
+               *j_color_comp = pixel_depth;
+}
+
+static int __jpeg_decode_get_buffer_size(j_decompress_ptr dinfo, int row_stride, mm_util_color_format_e color_format, size_t *size)
+{
+       *size = 0;
+
+       if (color_format == MM_UTIL_COLOR_RGB24 || color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB) {
+               *size = dinfo->output_height * row_stride;
+       } else if (color_format == MM_UTIL_COLOR_YUV420) {
+               *size = dinfo->output_height * row_stride / 2;
+       } else if (color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
+               *size = dinfo->output_height * dinfo->output_width * 2;
+       } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
+               *size = dinfo->output_height * dinfo->output_width;
+       } else{
+               mm_util_error("[%d] We can't decode the IMAGE format", color_format);
+               return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
+       }
+
+       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;
@@ -235,30 +285,11 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
        }
 
        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) {
+               unsigned int row_stride = 0;
                JSAMPROW row_pointer[1];
-               unsigned int iRowStride = 0;
-
-               if (_decoded->color == MM_UTIL_COLOR_RGB24) {
-                       cinfo.input_components = 3;
-                       cinfo.in_color_space = JCS_RGB;
-                       mm_util_debug("JCS_RGB");
-               } else if (_decoded->color == MM_UTIL_COLOR_GRAYSCALE) {
-                       cinfo.input_components = 1; /* one colour component */
-                       cinfo.in_color_space = JCS_GRAYSCALE;
-                       mm_util_debug("JCS_GRAYSCALE");
-               } else if (_decoded->color == MM_UTIL_COLOR_RGBA) {
-                       cinfo.input_components = 4;
-                       cinfo.in_color_space = JCS_EXT_RGBA;
-                       mm_util_debug("JCS_EXT_RGBA");
-               } else if (_decoded->color == MM_UTIL_COLOR_BGRA) {
-                       cinfo.input_components = 4;
-                       cinfo.in_color_space = JCS_EXT_BGRA;
-                       mm_util_debug("JCS_EXT_BGRA");
-               } else if (_decoded->color == MM_UTIL_COLOR_ARGB) {
-                       cinfo.input_components = 4;
-                       cinfo.in_color_space = JCS_EXT_ARGB;
-                       mm_util_debug("JCS_EXT_ARGB");
-               }
+
+               __jpeg_convert_rgb_colorspace(_decoded->color, &(cinfo.in_color_space), &(cinfo.input_components));
+               row_stride = _width * cinfo.input_components;
 
                jpeg_set_defaults(&cinfo);
                mm_util_debug("jpeg_set_defaults");
@@ -266,16 +297,10 @@ static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_ut
                mm_util_debug("jpeg_set_quality");
                jpeg_start_compress(&cinfo, TRUE);
                mm_util_debug("jpeg_start_compress");
-               if (_decoded->color == MM_UTIL_COLOR_RGB24)
-                       iRowStride = _width * 3;
-               else if (_decoded->color == MM_UTIL_COLOR_GRAYSCALE)
-                       iRowStride = _width;
-               else if (_decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB)
-                       iRowStride = _width * 4;
 
                JSAMPLE *image_buffer = (JSAMPLE *)_decoded->data;
                while (cinfo.next_scanline < cinfo.image_height) {
-                       row_pointer[0] = &image_buffer[cinfo.next_scanline * iRowStride];
+                       row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
                        jpeg_write_scanlines(&cinfo, row_pointer, 1);
                }
                mm_util_debug("while");
@@ -355,25 +380,7 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
        dinfo.dct_method = JDCT_FASTEST;
 
        /* set parameters for decompression */
-       if (color_format == MM_UTIL_COLOR_RGB24) {
-               dinfo.out_color_space = JCS_RGB;
-               mm_util_debug("cinfo.out_color_space = JCS_RGB");
-       } else if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
-               dinfo.out_color_space = JCS_YCbCr;
-               mm_util_debug("cinfo.out_color_space = JCS_YCbCr");
-       } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
-               dinfo.out_color_space = JCS_GRAYSCALE;
-               mm_util_debug("cinfo.out_color_space = JCS_GRAYSCALE");
-       } else if (color_format == MM_UTIL_COLOR_RGBA) {
-               dinfo.out_color_space = JCS_EXT_RGBA;
-               mm_util_debug("cinfo.out_color_space = JCS_EXT_RGBA");
-       } else if (color_format == MM_UTIL_COLOR_BGRA) {
-               dinfo.out_color_space = JCS_EXT_BGRA;
-               mm_util_debug("cinfo.out_color_space = JCS_EXT_BGRA");
-       } else if (color_format == MM_UTIL_COLOR_ARGB) {
-               dinfo.out_color_space = JCS_EXT_ARGB;
-               mm_util_debug("cinfo.out_color_space = JCS_EXT_ARGB");
-       }
+       __jpeg_convert_rgb_colorspace(color_format, &dinfo.out_color_space, NULL);
 
        /* Start decompressor */
        jpeg_start_decompress(&dinfo);
@@ -394,19 +401,10 @@ static int _mm_util_jpeg_decode(mm_util_jpeg_cont_format_e control_format, FILE
        buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo, JPOOL_IMAGE, row_stride, 1);
        mm_util_debug("JPOOL_IMAGE BUFFER (color_format: %d)", color_format);
 
-       if (color_format == MM_UTIL_COLOR_RGB24 || color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB) {
-               _size = dinfo.output_height * row_stride;
-       } else if (color_format == MM_UTIL_COLOR_YUV420) {
-               _size = dinfo.output_height * row_stride / 2;
-       } else if (color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
-               _size = dinfo.output_height * dinfo.output_width * 2;
-       } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
-               _size = dinfo.output_height * dinfo.output_width;
-       } else{
-               jpeg_finish_decompress(&dinfo);
-               jpeg_destroy_decompress(&dinfo);
-               mm_util_error("[%d] We can't decode the IMAGE format", color_format);
-               return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
+       ret = __jpeg_decode_get_buffer_size(&dinfo, row_stride, color_format, &_size);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__jpeg_decode_calc_buffer_size failed");
+               goto END;
        }
 
        _data = (void *) calloc(1, _size);