Modify parameters in encoding APIs have been changed from mm_image_info_s to mm_util_...
[platform/core/multimedia/libmm-utility.git] / jpeg / mm_util_jpeg.c
index 82b9300..4d2ead5 100644 (file)
@@ -88,28 +88,29 @@ static gboolean _mm_util_is_supported_color_format(mm_util_color_format_e color_
        return _bool;
 }
 
-static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, void *rawdata, unsigned int width, unsigned int height, mm_util_color_format_e color_format, int quality, FILE *fp, void **mem, size_t *csize)
+static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, mm_util_image_h decoded, int quality, FILE *fp, void **mem, size_t *csize)
 {
-       int iErrorCode = MM_UTIL_ERROR_NONE;
+       int ret = MM_UTIL_ERROR_NONE;
+       mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
 
        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);
        mm_util_retvm_if((control_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
        mm_util_retvm_if((control_format == MM_UTIL_JPEG_MEM) && ((mem == NULL) || (csize == NULL)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or csize");
-       mm_util_retvm_if(rawdata == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid rawdata");
+       mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
 
        JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
        JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
 
        struct jpeg_compress_struct cinfo;
        struct jpeg_error_mgr jerr;
-       int i, j, flag, _height;
+       int i, j, flag, _width, _height;
        unsigned long size = 0;
 
        data[0] = y;
        data[1] = cb;
        data[2] = cr;
 
-       mm_util_debug("rawdata[%p] width[%u] height[%u] color_format[%u] quality[%d]", rawdata, width, height, color_format, quality);
+       mm_util_debug("quality[%d]", quality);
 
        cinfo.err = jpeg_std_error(&jerr); /*  Errors get written to stderr */
 
@@ -123,11 +124,10 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                mm_util_debug("jpeg_mem_dest");
        }
 
-       cinfo.image_width = width;
-       cinfo.image_height = height;
-       if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
-               _height = MM_UTIL_ROUND_DOWN_16(height);
-               flag = height - _height;
+       _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;
@@ -138,9 +138,9 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                cinfo.do_fancy_downsampling = FALSE;
 
                cinfo.comp_info[0].h_samp_factor = 2;
-               if (color_format == MM_UTIL_COLOR_YUV420)
+               if (_decoded->color == MM_UTIL_COLOR_YUV420)
                        cinfo.comp_info[0].v_samp_factor = 2;
-               else if (color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY)
+               else if (_decoded->color == MM_UTIL_COLOR_YUV422 || _decoded->color == MM_UTIL_COLOR_UYVY)
                        cinfo.comp_info[0].v_samp_factor = 1;
                cinfo.comp_info[1].h_samp_factor = 1;
                cinfo.comp_info[1].v_samp_factor = 1;
@@ -155,17 +155,17 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                mm_util_debug("jpeg_start_compress");
 
                if (flag) {
-                       void *large_rect = calloc(1, width);
-                       void *small_rect = calloc(1, width);
+                       void *large_rect = calloc(1, _width);
+                       void *small_rect = calloc(1, _width);
                        if (large_rect) {
-                               memset(large_rect, 0x10, width);
+                               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);
+                               memset(small_rect, 0x80, _width);
                        } else {
                                MMUTIL_SAFE_FREE(large_rect);
                                mm_util_error("small rectangle memory");
@@ -174,19 +174,19 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
 
                        for (j = 0; j < _height; j += 16) {
                                for (i = 0; i < 16; i++) {
-                                       y[i] = (JSAMPROW)rawdata + width * (i + j);
+                                       y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
                                        if (i % 2 == 0) {
-                                               cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
-                                               cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                                               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)rawdata + width * (i + j);
+                               y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
                                if (i % 2 == 0) {
-                                       cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
-                                       cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                                       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++) {
@@ -200,12 +200,12 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                        MMUTIL_SAFE_FREE(large_rect);
                        MMUTIL_SAFE_FREE(small_rect);
                } else {
-                       for (j = 0; j < height; j += 16) {
+                       for (j = 0; j < _height; j += 16) {
                                for (i = 0; i < 16; i++) {
-                                       y[i] = (JSAMPROW)rawdata + width * (i + j);
+                                       y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
                                        if (i % 2 == 0) {
-                                               cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
-                                               cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
+                                               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);
@@ -220,27 +220,27 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                mm_util_debug("jpeg_destroy_compress");
        }
 
-       else if (color_format == MM_UTIL_COLOR_RGB24 || color_format == MM_UTIL_COLOR_GRAYSCALE || color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB) {
+       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) {
                JSAMPROW row_pointer[1];
                unsigned int iRowStride = 0;
 
-               if (color_format == MM_UTIL_COLOR_RGB24) {
+               if (_decoded->color == MM_UTIL_COLOR_RGB24) {
                        cinfo.input_components = 3;
                        cinfo.in_color_space = JCS_RGB;
                        mm_util_debug("JCS_RGB");
-               } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
+               } 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 (color_format == MM_UTIL_COLOR_RGBA) {
+               } 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 (color_format == MM_UTIL_COLOR_BGRA) {
+               } 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 (color_format == MM_UTIL_COLOR_ARGB) {
+               } 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");
@@ -252,14 +252,14 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
                mm_util_debug("jpeg_set_quality");
                jpeg_start_compress(&cinfo, TRUE);
                mm_util_debug("jpeg_start_compress");
-               if (color_format == MM_UTIL_COLOR_RGB24)
-                       iRowStride = width * 3;
-               else if (color_format == MM_UTIL_COLOR_GRAYSCALE)
-                       iRowStride = width;
-               else if (color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB)
-                       iRowStride = width * 4;
-
-               JSAMPLE *image_buffer = (JSAMPLE *)rawdata;
+               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];
                        jpeg_write_scanlines(&cinfo, row_pointer, 1);
@@ -279,7 +279,7 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
        if (control_format == MM_UTIL_JPEG_MEM)
                *csize = (size_t)size;
 
-       return iErrorCode;
+       return ret;
 }
 
 static int __mm_image_decode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, FILE *fp, void *src, size_t size, mm_util_color_format_e color_format, mm_util_jpeg_decode_downscale downscale, mm_util_image_h *decoded)
@@ -465,66 +465,57 @@ static int __mm_image_decode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
        return ret;
 }
 
-int mm_util_jpeg_encode_to_file(mm_image_info_s *decoded, int quality, const char *filename)
+int mm_util_jpeg_encode_to_file(mm_util_image_h decoded, int quality, const char *file_path)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       mm_image_info_s *_converted_image = NULL;
+       mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
+       mm_util_image_h _converted_image = NULL;
 
        mm_util_fenter();
 
-       mm_util_retvm_if(filename == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid filename");
-       mm_util_retvm_if(decoded == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
-       mm_util_retvm_if(decoded->data == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src data");
-       mm_util_retvm_if((decoded->width <= 0) || (decoded->height <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid width[%u] height[%u]", decoded->width, decoded->height);
-       mm_util_retvm_if((IS_VALID_COLOR(decoded->color) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", decoded->color);
-       mm_util_retvm_if((!_mm_util_is_supported_color_format(decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", decoded->color);
+       mm_util_retvm_if(!file_path, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid file_path");
+       mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(_decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", _decoded->color);
        mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
 
-       mm_util_debug("#START# LIBJPEG");
        FILE *fp = NULL;
-       ret = mm_util_safe_fopen(filename, "wb", &fp);
+       ret = mm_util_safe_fopen(file_path, "wb", &fp);
        mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_safe_fopen fail (%d)", ret);
 
-       if (decoded->color == MM_UTIL_COLOR_NV12) {
-               ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, (mm_util_image_h *)&_converted_image);
+       if (_decoded->color == MM_UTIL_COLOR_NV12) {
+               ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, &_converted_image);
                if (ret != MM_UTIL_ERROR_NONE) {
                        mm_util_error("mm_util_convert_image failed (%d)", ret);
                        mm_util_safe_fclose(fp);
                        return ret;
                }
 
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, _converted_image->data, _converted_image->width, _converted_image->height, MM_UTIL_COLOR_YUV420, quality, fp, NULL, NULL);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, _converted_image, quality, fp, NULL, NULL);
                mm_image_destroy_image(_converted_image);
        } else {
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, decoded->data, decoded->width, decoded->height, decoded->color, quality, fp, NULL, NULL);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, decoded, quality, fp, NULL, NULL);
        }
 
        fsync((int)(fp->_fileno));
        mm_util_safe_fclose(fp);
 
-       mm_util_debug("#End# libjpeg, Success!! ret: %d", ret);
-
        mm_util_fleave();
 
        return ret;
 }
 
-int mm_util_jpeg_encode_to_memory(void **mem, unsigned int *size, unsigned char *src, unsigned int width, unsigned int height, mm_util_color_format_e color, int quality)
+int mm_util_jpeg_encode_to_memory(void **buffer, unsigned int *size, unsigned char *src, unsigned int width, unsigned int height, mm_util_color_format_e color, int quality)
 {
        int ret = MM_UTIL_ERROR_NONE;
        size_t encoded_size = 0;
+       mm_util_image_h decoded = NULL;
 
-       mm_image_info_s decoded;
-       memset(&decoded, 0, sizeof(mm_image_info_s));
-
-       mm_util_retvm_if(size == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
+       mm_util_retvm_if(!size, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
 
-       decoded.width = width;
-       decoded.height = height;
-       decoded.color = color;
-       decoded.data = src;
+       ret = mm_image_create_image(width, height, color, src, TEMP_DATA_SIZE, &decoded);
+       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_image_create_image fail (%d)", ret);
 
-       ret = mm_util_encode_to_jpeg_memory(&decoded, quality, mem, &encoded_size);
+       ret = mm_util_encode_to_jpeg_memory(decoded, quality, buffer, &encoded_size);
        mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_encode_to_jpeg_memory fail (%d)", ret);
 
        *size = (unsigned int)encoded_size;
@@ -532,33 +523,29 @@ int mm_util_jpeg_encode_to_memory(void **mem, unsigned int *size, unsigned char
        return ret;
 }
 
-int mm_util_encode_to_jpeg_memory(mm_image_info_s *decoded, int quality, void **mem, size_t *size)
+int mm_util_encode_to_jpeg_memory(mm_util_image_h decoded, int quality, void **buffer, size_t *size)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       mm_image_info_s *_converted_image = NULL;
+       mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
+       mm_util_image_h _converted_image = NULL;
 
        mm_util_fenter();
 
-       mm_util_retvm_if(mem == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid filename");
-       mm_util_retvm_if(size == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
-       mm_util_retvm_if(decoded == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
-       mm_util_retvm_if(decoded->data == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src data");
-       mm_util_retvm_if((decoded->width <= 0) || (decoded->height <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid width[%u] height[%u]", decoded->width, decoded->height);
-       mm_util_retvm_if((IS_VALID_COLOR(decoded->color) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", decoded->color);
-       mm_util_retvm_if((!_mm_util_is_supported_color_format(decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", decoded->color);
+       mm_util_retvm_if(!buffer, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buffer");
+       mm_util_retvm_if(!size, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
+       mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(_decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported color [%d]", _decoded->color);
        mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
 
-       mm_util_debug("#START# libjpeg");
-       if (decoded->color == MM_UTIL_COLOR_NV12) {
-               ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, (mm_util_image_h *)&_converted_image);
+       if (_decoded->color == MM_UTIL_COLOR_NV12) {
+               ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, &_converted_image);
                mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_convert_image fail (%d)", ret);
 
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, _converted_image->data, _converted_image->width, _converted_image->height, MM_UTIL_COLOR_YUV420, quality, NULL, mem, size);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, _converted_image, quality, NULL, buffer, size);
                mm_image_destroy_image(_converted_image);
        } else {
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, decoded->data, decoded->width, decoded->height, decoded->color, quality, NULL, mem, size);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, decoded, quality, NULL, buffer, size);
        }
-       mm_util_debug("#END# libjpeg, Success!! ret: %d", ret);
 
        mm_util_fleave();