Replace encode_with_libjpeg parameters from signed int to unsigned int 21/172621/2
authorJiyong Min <jiyong.min@samsung.com>
Thu, 15 Mar 2018 01:49:55 +0000 (10:49 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Thu, 15 Mar 2018 01:55:34 +0000 (10:55 +0900)
and add to check supported format generally

 - Value width & height of libjpeg is JDIMENSION(unsigned int)
 - Added to check supported format before function works

Change-Id: I9e8ffd44227df2c0e0f6614e392e0b69b12303be

jpeg/mm_util_jpeg.c

index c75b0d7..4e565e0 100755 (executable)
@@ -57,7 +57,43 @@ typedef enum {
        MM_UTIL_JPEG_MEM,
 } mm_util_jpeg_cont_format_e;
 
-static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, void *rawdata, int width, int height, mm_util_color_format_e color_format, int quality, FILE *fp, void **mem, size_t *csize)
+static gboolean __is_supported_color_format_with_libjpeg(mm_util_color_format_e color_format)
+{
+       gboolean _bool = FALSE;
+
+       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 ||
+               color_format == MM_UTIL_COLOR_YUV420 ||
+               color_format == MM_UTIL_COLOR_YUV422 ||
+               color_format == MM_UTIL_COLOR_UYVY ||
+               color_format == MM_UTIL_COLOR_GRAYSCALE) {
+               _bool = TRUE;
+       }
+
+       if (!_bool)
+               mm_util_error("not supported color format %d", color_format);
+
+       return _bool;
+}
+
+static gboolean _mm_util_is_supported_color_format(mm_util_color_format_e color_format)
+{
+       gboolean _bool = FALSE;
+
+       if (__is_supported_color_format_with_libjpeg(color_format) ||
+               color_format == MM_UTIL_COLOR_NV12) {
+               _bool = TRUE;
+       }
+
+       if (!_bool)
+               mm_util_error("not supported color format %d", color_format);
+
+       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)
 {
        int iErrorCode = MM_UTIL_ERROR_NONE;
 
@@ -191,7 +227,7 @@ static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_for
 
        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) {
                JSAMPROW row_pointer[1];
-               int iRowStride = 0;
+               unsigned int iRowStride = 0;
 
                if (color_format == MM_UTIL_COLOR_RGB24) {
                        cinfo.input_components = 3;
@@ -444,6 +480,7 @@ int mm_util_jpeg_encode_to_file(mm_util_jpeg_yuv_data *decoded, int quality, con
        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[%d] height[%d]", decoded->width, decoded->height);
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(decoded->format) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", decoded->format);
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(decoded->format)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", decoded->format);
        mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
 
        mm_util_debug("#START# LIBJPEG");
@@ -461,14 +498,10 @@ int mm_util_jpeg_encode_to_file(mm_util_jpeg_yuv_data *decoded, int quality, con
                unsigned char *dst = NULL;
 
                ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, dst, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, quality, fp, NULL, NULL);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, dst, res_w, res_h, MM_UTIL_COLOR_YUV420, quality, fp, NULL, NULL);
 
                MMUTIL_SAFE_FREE(dst);
 
-       } else if (decoded->format == MM_UTIL_COLOR_NV21) {
-               mm_util_error("Not supported format NV12");
-               ret = MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
-
        } else {
                ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, decoded->data, decoded->width, decoded->height, decoded->format, quality, fp, NULL, NULL);
        }
@@ -515,6 +548,7 @@ int mm_util_encode_to_jpeg_memory(mm_util_jpeg_yuv_data *decoded, int quality, v
        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[%d] height[%d]", decoded->width, decoded->height);
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(decoded->format) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", decoded->format);
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(decoded->format)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", decoded->format);
        mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
 
        mm_util_debug("#START# libjpeg");
@@ -525,12 +559,10 @@ int mm_util_encode_to_jpeg_memory(mm_util_jpeg_yuv_data *decoded, int quality, v
                unsigned char *dst = NULL;
 
                ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
-               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, dst, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, quality, NULL, mem, size);
+               ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, dst, res_w, res_h, MM_UTIL_COLOR_YUV420, quality, NULL, mem, size);
 
                MMUTIL_SAFE_FREE(dst);
 
-       } else if (decoded->format == MM_UTIL_COLOR_NV21) {
-               return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        } else {
                ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, decoded->data, decoded->width, decoded->height, decoded->format, quality, NULL, mem, size);
        }
@@ -550,6 +582,7 @@ int mm_util_decode_from_jpeg_file_with_downscale(const char *filename, mm_util_c
        mm_util_retvm_if(decoded == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded");
        mm_util_retvm_if(filename == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid filename");
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(fmt) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", fmt);
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(fmt)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", fmt);
 
        if ((downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2)
                 && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8)) {
@@ -602,6 +635,7 @@ int mm_util_decode_from_jpeg_memory_with_downscale(void *src, const size_t size,
        mm_util_retvm_if(decoded == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded");
        mm_util_retvm_if(src == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid filename");
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(fmt) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", fmt);
+       mm_util_retvm_if((!_mm_util_is_supported_color_format(fmt)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", fmt);
 
        if ((downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2)
                 && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8)) {