From f0ac55671f36f23d65c7e2ab26104bcce7a34d16 Mon Sep 17 00:00:00 2001 From: Vineeth TM Date: Thu, 19 Nov 2015 16:20:18 +0900 Subject: [PATCH] [ACR-454] Add support for JPEG image in decoding/encoding utility Change-Id: I116adaaa0076d21db98956a15bb97e7906299e57 Signed-off-by: Vineeth TM --- .../image_util_decode_encode_testsuite.c | 9 + include/image_util.h | 188 ++++++++++- include/image_util_private.h | 3 + include/image_util_type.h | 1 + src/image_util.c | 310 ++++++++++++++++++ 5 files changed, 508 insertions(+), 3 deletions(-) diff --git a/decode-test/image_util_decode_encode_testsuite.c b/decode-test/image_util_decode_encode_testsuite.c index fc7b912..ceb2d04 100755 --- a/decode-test/image_util_decode_encode_testsuite.c +++ b/decode-test/image_util_decode_encode_testsuite.c @@ -234,6 +234,9 @@ int main(int argc, char *argv[]) memset(filename, 0, BUFFER_SIZE); switch (encode_image_type) { + case IMAGE_UTIL_JPEG: + snprintf(type, 4, "%s", "jpg"); + break; case IMAGE_UTIL_PNG: snprintf(type, 4, "%s", "png"); break; @@ -253,6 +256,12 @@ int main(int argc, char *argv[]) if (ret != IMAGE_UTIL_ERROR_NONE) return 0; + if (encode_image_type == IMAGE_UTIL_JPEG) { + ret = image_util_encode_set_jpeg_quality(encoded, 100); + if (ret != IMAGE_UTIL_ERROR_NONE) + return 0; + } + ret = image_util_encode_set_input_buffer(encoded, data); if (ret != IMAGE_UTIL_ERROR_NONE) return 0; diff --git a/include/image_util.h b/include/image_util.h index bdfaa51..18cec0b 100755 --- a/include/image_util.h +++ b/include/image_util.h @@ -333,6 +333,7 @@ int image_util_transform_run(transformation_h handle, media_packet_h src, image_ int image_util_transform_destroy(transformation_h handle); /** +* @deprecated Deprecated since 3.0. Use image_util_supported_colorspace_cb() instead. * @brief Called once for each supported JPEG encode/decode colorspace. * @since_tizen 2.3 * @@ -353,6 +354,7 @@ int image_util_transform_destroy(transformation_h handle); typedef bool(*image_util_supported_jpeg_colorspace_cb)(image_util_colorspace_e colorspace, void *user_data); /** +* @deprecated Deprecated since 3.0. Use image_util_foreach_supported_colorspace() instead. * @brief Retrieves all supported JPEG encoding/decoding colorspace by invoking a callback function once for each one. * @since_tizen 2.3 * @@ -537,6 +539,7 @@ int image_util_crop(unsigned char *dest, int x, int y, int *width, int *height, /** +* @deprecated Deprecated since 3.0. Use image_util_decode_create() instead. * @brief Decodes the JPEG image to the buffer. * @since_tizen 2.3 * @@ -570,6 +573,7 @@ int image_util_crop(unsigned char *dest, int x, int y, int *width, int *height, int image_util_decode_jpeg(const char *path, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size); /** +* @deprecated Deprecated since 3.0. Use image_util_decode_create() instead. * @brief Decodes the JPEG image(in memory) to the buffer. * @since_tizen 2.3 * @@ -602,6 +606,7 @@ int image_util_decode_jpeg(const char *path, image_util_colorspace_e colorspace, int image_util_decode_jpeg_from_memory(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size); /** +* @deprecated Deprecated since 3.0. Use image_util_decode_create() instead. * @brief Decodes the JPEG image to the buffer with downscale decoding option. * @since_tizen 2.4 * @@ -636,6 +641,7 @@ int image_util_decode_jpeg_from_memory(const unsigned char *jpeg_buffer, int jpe int image_util_decode_jpeg_with_downscale(const char *path, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size); /** +* @deprecated Deprecated since 3.0. Use image_util_decode_create() instead. * @brief Decodes the JPEG image(in memory) to the buffer with downscale decoding option. * @since_tizen 2.4 * @@ -669,6 +675,7 @@ int image_util_decode_jpeg_with_downscale(const char *path, image_util_colorspac int image_util_decode_jpeg_from_memory_with_downscale(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size); /** +* @deprecated Deprecated since 3.0. Use image_util_encode_create() instead. * @brief Encodes the image to the JPEG image. * @since_tizen 2.3 * @@ -699,6 +706,7 @@ int image_util_decode_jpeg_from_memory_with_downscale(const unsigned char *jpeg_ int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path); /** +* @deprecated Deprecated since 3.0. Use image_util_encode_create() instead. * @brief Encodes the image to the JPEG image * @since_tizen 2.3 * @@ -751,6 +759,44 @@ int image_util_encode_jpeg_to_memory(const unsigned char *image_buffer, int widt */ int image_util_extract_color_from_memory(const unsigned char *image_buffer, int width, int height, unsigned char *rgb_r, unsigned char *rgb_g, unsigned char *rgb_b); +/** +* @brief Called once for each supported image encode/decode colorspace. +* @since_tizen 3.0 +* +* @param[in] colorspace The colorspace +* @param[in] user_data The user data passed from the foreach function +* +* @return @c true to continue with the next iteration of the loop, \n +* otherwise @c false to break out of the loop +* +* @pre image_util_foreach_supported_colorspace() invokes this callback. +* +* @see image_util_foreach_supported_colorspace() +* @see image_util_encode_create() +* @see image_util_decode_create() +*/ +typedef bool(*image_util_supported_colorspace_cb)(image_util_colorspace_e colorspace, void *user_data); + +/** +* @brief Retrieves all supported image encoding/decoding colorspace by invoking a callback function once for each one. +* @since_tizen 3.0 +* +* @param[in] callback The callback function to invoke +* @param[in] user_data The user data to be passed to the callback function +* @return 0 on success, +* otherwise a negative error value +* +* @retval #IMAGE_UTIL_ERROR_NONE Successful +* @retval #IMAGE_UTIL_ERROR_INVALID_PARAMETER Invalid parameter +* +* @post This function invokes image_util_supported_colorspace_cb() repeatedly to retrieve each supported image encoding/decoding colorspace. +* +* @see image_util_supported_colorspace_cb() +* @see image_util_encode_create() +* @see image_util_decode_create() +*/ +int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data); + /** * @brief Creates a handle to image util decoding. * @since_tizen 3.0 @@ -844,7 +890,8 @@ int image_util_decode_set_input_buffer(image_util_decode_h handle, const unsigne * @since_tizen 3.0 * * @remarks Either image_util_decode_set_input_path() or image_util_decode_set_input_buffer() should be set.\n -* The decoded output buffer will be RGBA format. +* By default the decoded output buffer colorspace will be IMAGE_UTIL_COLORSPACE_RGBA8888. +* Use image_util_decode_set_colorspace to change the colorspace. * * @param[in] handle The handle to image util decoding * @param[in] dst_buffer The decoded output buffer @@ -870,6 +917,72 @@ int image_util_decode_set_input_buffer(image_util_decode_h handle, const unsigne */ int image_util_decode_set_output_buffer(image_util_decode_h handle, unsigned char **dst_buffer); +/** +* @brief Sets the decoded image colorspace format. +* @since_tizen 3.0 +* +* @remarks The default colorspace is IMAGE_UTIL_COLORSPACE_RGBA8888.\n +* Use image_util_foreach_supported_colorspace to get supported colorspaces for each image format.\n +* Errors would be returned if not supported. +* +* @param[in] handle The handle to image util decoding +* @param[in] colorspace The decoded image colorspace +* +* @return @c 0 on success, +* otherwise a negative error value +* +* @retval #IMAGE_UTIL_ERROR_NONE Successful +* @retval #IMAGE_UTIL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT Format not supported +* +* @pre image_util_decode_create() +* +* @post image_util_decode_run()/image_util_decode_run_async() +* @post image_util_decode_destroy() +* +* @see image_util_supported_colorspace_cb() +* @see image_util_foreach_supported_colorspace() +* @see image_util_decode_create() +* @see image_util_decode_set_input_path() +* @see image_util_decode_set_input_buffer() +* @see image_util_decode_set_output_buffer() +* @see image_util_decode_run() +* @see image_util_decode_run_async() +* @see image_util_decode_destroy() +*/ +int image_util_decode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace); + +/** +* @brief Sets the downscale value at which JPEG image should be decoded. +* @since_tizen 3.0 +* +* @remarks This is API is supported only for JPEG decoding. +* +* @param[in] handle The handle to image util decoding +* @param[in] down_scale The downscale at which image is to be decoded +* +* @return @c 0 on success, +* otherwise a negative error value +* +* @retval #IMAGE_UTIL_ERROR_NONE Successful +* @retval #IMAGE_UTIL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT Format not supported +* +* @pre image_util_decode_create() +* +* @post image_util_decode_run()/image_util_decode_run_async() +* @post image_util_decode_destroy() +* +* @see image_util_decode_create() +* @see image_util_decode_set_input_path() +* @see image_util_decode_set_input_buffer() +* @see image_util_decode_set_output_buffer() +* @see image_util_decode_run() +* @see image_util_decode_run_async() +* @see image_util_decode_destroy() +*/ +int image_util_decode_set_jpeg_downscale(image_util_encode_h handle, image_util_scale_e down_scale); + /** * @brief Starts decoding of the image and fills the output buffer set using image_util_decode_set_output_buffer(). * @since_tizen 3.0 @@ -1011,6 +1124,74 @@ int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * */ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height); +/** +* @brief Sets the colorspace format for image encoding. +* @since_tizen 3.0 +* +* @remarks The default colorspace is IMAGE_UTIL_COLORSPACE_RGBA8888.\n +* Use image_util_foreach_supported_colorspace to get supported colorspaces for each image format.\n +* Errors would be returned if not supported. +* +* @param[in] handle The handle to image util encoding +* @param[in] colorspace The decoded image colorspace +* +* @return @c 0 on success, +* otherwise a negative error value +* +* @retval #IMAGE_UTIL_ERROR_NONE Successful +* @retval #IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT Format not supported +* @retval #IMAGE_UTIL_ERROR_INVALID_PARAMETER Invalid parameter +* +* @pre image_util_encode_create() +* +* @post image_util_encode_run()/image_util_encode_run_async() +* @post image_util_encode_destroy() +* +* @see image_util_supported_colorspace_cb() +* @see image_util_foreach_supported_colorspace() +* @see image_util_encode_create() +* @see image_util_encode_set_resolution() +* @see image_util_encode_set_input_buffer() +* @see image_util_encode_set_output_path() +* @see image_util_encode_set_output_buffer() +* @see image_util_encode_run() +* @see image_util_encode_run_async() +* @see image_util_encode_destroy() +*/ +int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace); + +/** +* @brief Sets the quality for JPEG image encoding. +* @since_tizen 3.0 +* +* @remarks If application does not set this, then by default quality of 20 is set. +* +* @param[in] handle The handle to image util encoding +* @param[in] quality Encoding quality from 1~100 +* +* @return @c 0 on success, +* otherwise a negative error value +* +* @retval #IMAGE_UTIL_ERROR_NONE Successful +* @retval #IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT Format not supported +* @retval #IMAGE_UTIL_ERROR_INVALID_PARAMETER Invalid parameter +* +* @pre image_util_encode_create() +* +* @post image_util_encode_run()/image_util_encode_run_async() +* @post image_util_encode_destroy() +* +* @see image_util_encode_create() +* @see image_util_encode_set_resolution() +* @see image_util_encode_set_input_buffer() +* @see image_util_encode_set_output_path() +* @see image_util_encode_set_output_buffer() +* @see image_util_encode_run() +* @see image_util_encode_run_async() +* @see image_util_encode_destroy() +*/ +int image_util_encode_set_jpeg_quality(image_util_encode_h handle, int quality); + /** * @brief Sets the compression value of png image encoding(0~9). * @since_tizen 3.0 @@ -1048,8 +1229,9 @@ int image_util_encode_set_png_compression(image_util_encode_h handle, image_util * @brief Sets the input buffer from which to encode. * @since_tizen 3.0 * -* @remarks Either image_util_encode_set_output_path() or image_util_encode_set_output_buffer() should be set. -* The input buffer should be RGBA format. +* @remarks Either image_util_encode_set_output_path() or image_util_encode_set_output_buffer() should be set.\n +* By default the input buffer colorspace will be considered as IMAGE_UTIL_COLORSPACE_RGBA8888.\n +* Use image_util_encode_set_colorspace to change the colorspace. * * @param[in] handle The handle to image util decoding * @param[in] src_buffer The input image buffer diff --git a/include/image_util_private.h b/include/image_util_private.h index 15be490..d21fd61 100755 --- a/include/image_util_private.h +++ b/include/image_util_private.h @@ -106,6 +106,9 @@ typedef struct { unsigned long width; unsigned long height; bool is_decode; + int quality; + image_util_colorspace_e colorspace; + image_util_scale_e down_scale; decode_cb_s *_decode_cb; encode_cb_s *_encode_cb; diff --git a/include/image_util_type.h b/include/image_util_type.h index 5d851ce..540cc66 100755 --- a/include/image_util_type.h +++ b/include/image_util_type.h @@ -129,6 +129,7 @@ typedef void(*image_util_transform_completed_cb)(media_packet_h *dst, int error_ * @since_tizen 3.0 */ typedef enum { + IMAGE_UTIL_JPEG, /**< Image format JPEG */ IMAGE_UTIL_PNG, /**< Image format PNG */ IMAGE_UTIL_GIF, /**< Image format GIF */ } image_util_type_e; diff --git a/src/image_util.c b/src/image_util.c index 5864b40..469949c 100755 --- a/src/image_util.c +++ b/src/image_util.c @@ -68,6 +68,59 @@ static int _convert_encode_colorspace_tbl[] = { -1 , /* IMAGE_UTIL_COLORSPACE_NV61 */ }; +static int _convert_jpeg_colorspace_tbl[] = { + MM_UTIL_JPEG_FMT_YUV420 , /* IMAGE_UTIL_COLORSPACE_YUV420 */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUV422 */ + MM_UTIL_JPEG_FMT_YUV420 , /* IMAGE_UTIL_COLORSPACE_I420 */ + MM_UTIL_JPEG_FMT_NV12 , /* IMAGE_UTIL_COLORSPACE_NV12 */ + -1 , /* IMAGE_UTIL_COLORSPACE_UYVY */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUYV */ + -1 , /* IMAGE_UTIL_COLORSPACE_RGB565 */ + MM_UTIL_JPEG_FMT_RGB888 , /* IMAGE_UTIL_COLORSPACE_RGB888 */ + MM_UTIL_JPEG_FMT_ARGB8888 , /* IMAGE_UTIL_COLORSPACE_ARGB8888 */ + MM_UTIL_JPEG_FMT_BGRA8888 , /* IMAGE_UTIL_COLORSPACE_BGRA8888 */ + MM_UTIL_JPEG_FMT_RGBA8888 , /* IMAGE_UTIL_COLORSPACE_RGBA8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_BGRX8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV21 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV16 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV61 */ +}; + +static int _convert_png_colorspace_tbl[] = { + -1 , /* IMAGE_UTIL_COLORSPACE_YUV420 */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUV422 */ + -1 , /* IMAGE_UTIL_COLORSPACE_I420 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV12 */ + -1 , /* IMAGE_UTIL_COLORSPACE_UYVY */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUYV */ + -1 , /* IMAGE_UTIL_COLORSPACE_RGB565 */ + -1 , /* IMAGE_UTIL_COLORSPACE_RGB888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_ARGB8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_BGRA8888 */ + MM_UTIL_PNG_COLOR_TYPE_RGB_ALPHA , /* IMAGE_UTIL_COLORSPACE_RGBA8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_BGRX8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV21 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV16 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV61 */ +}; + +static int _convert_gif_colorspace_tbl[] = { + -1 , /* IMAGE_UTIL_COLORSPACE_YUV420 */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUV422 */ + -1 , /* IMAGE_UTIL_COLORSPACE_I420 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV12 */ + -1 , /* IMAGE_UTIL_COLORSPACE_UYVY */ + -1 , /* IMAGE_UTIL_COLORSPACE_YUYV */ + -1 , /* IMAGE_UTIL_COLORSPACE_RGB565 */ + -1 , /* IMAGE_UTIL_COLORSPACE_RGB888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_ARGB8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_BGRA8888 */ + MM_UTIL_GIF_FMT_RGBA8888 , /* IMAGE_UTIL_COLORSPACE_RGBA8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_BGRX8888 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV21 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV16 */ + -1 , /* IMAGE_UTIL_COLORSPACE_NV61 */ +}; static int _convert_decode_scale_tbl[] = { MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1, @@ -754,6 +807,61 @@ int image_util_extract_color_from_memory(const unsigned char *image_buffer, int return _convert_image_util_error_code(__func__, ret); } +int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data) +{ + int i = 0; + + image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null"); + + switch (image_type) { + case IMAGE_UTIL_JPEG: + /* RGB has higher precedence than YUV */ + for (i = sizeof(_convert_jpeg_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) { + if (_convert_jpeg_colorspace_tbl[i] != -1) + if (false == callback(i, user_data)) + return IMAGE_UTIL_ERROR_NONE; + + } + break; + case IMAGE_UTIL_PNG: + for (i = sizeof(_convert_png_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) { + if (_convert_png_colorspace_tbl[i] != -1) + if (false == callback(i, user_data)) + return IMAGE_UTIL_ERROR_NONE; + + } + break; + case IMAGE_UTIL_GIF: + for (i = sizeof(_convert_gif_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) { + if (_convert_gif_colorspace_tbl[i] != -1) + if (false == callback(i, user_data)) + return IMAGE_UTIL_ERROR_NONE; + + } + break; + default: + return MM_UTIL_ERROR_INVALID_PARAMETER; + } + + return IMAGE_UTIL_ERROR_NONE; +} + +static int _image_util_decode_create_jpeg_handle(decode_encode_s * handle) +{ + int err = MM_UTIL_ERROR_NONE; + + image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + + mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data)); + image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY); + + handle->image_h = (MMHandleType) _handle; + handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888; + handle->down_scale = sizeof(_convert_decode_scale_tbl); + + return err; +} + static int _image_util_decode_create_png_handle(decode_encode_s * handle) { int err = MM_UTIL_ERROR_NONE; @@ -803,6 +911,9 @@ int image_util_decode_create(image_util_type_e image_type, image_util_decode_h * _handle->is_decode = TRUE; switch (image_type) { + case IMAGE_UTIL_JPEG: + err = _image_util_decode_create_jpeg_handle(_handle); + break; case IMAGE_UTIL_PNG: err = _image_util_decode_create_png_handle(_handle); break; @@ -886,11 +997,93 @@ int image_util_decode_set_output_buffer(image_util_decode_h handle, unsigned cha return err; } +int image_util_decode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace) +{ + int err = IMAGE_UTIL_ERROR_NONE; + decode_encode_s *_handle = (decode_encode_s *) handle; + + if (_handle == NULL || _handle->is_decode == FALSE) { + image_util_error("Invalid Handle"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + + image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(image_util_colorspace_e) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + case IMAGE_UTIL_PNG: + image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + case IMAGE_UTIL_GIF: + image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + default: + image_util_error("Invalid image type"); + return MM_UTIL_ERROR_INVALID_PARAMETER; + } + + _handle->colorspace = colorspace; + + return err; +} + +int image_util_decode_set_jpeg_downscale(image_util_encode_h handle, image_util_scale_e down_scale) +{ + int err = IMAGE_UTIL_ERROR_NONE; + decode_encode_s *_handle = (decode_encode_s *) handle; + + if (_handle == NULL || _handle->is_decode == FALSE) { + image_util_error("Invalid Handle"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + if (_handle->image_type != IMAGE_UTIL_JPEG) { + image_util_error("Wrong image format"); + return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT; + } + image_util_retvm_if((down_scale < 0 || down_scale >= sizeof(_convert_decode_scale_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downscale is invalid"); + + _handle->down_scale = down_scale; + + return err; +} + static int _image_util_decode_internal(decode_encode_s * _handle) { int err = MM_UTIL_ERROR_NONE; switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + { + mm_util_jpeg_yuv_data *jpeg_data; + + jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; + if (jpeg_data == NULL) { + image_util_error("Invalid jpeg data"); + return MM_UTIL_ERROR_INVALID_PARAMETER; + } + + if (_handle->path) { + if (_handle->down_scale < sizeof(_convert_decode_scale_tbl)) + err = mm_util_decode_from_jpeg_file_with_downscale(jpeg_data, _handle->path, _convert_jpeg_colorspace_tbl[_handle->colorspace], _convert_decode_scale_tbl[_handle->down_scale]); + else + err = mm_util_decode_from_jpeg_file(jpeg_data, _handle->path, _convert_jpeg_colorspace_tbl[_handle->colorspace]); + } + else { + if (_handle->down_scale < sizeof(_convert_decode_scale_tbl)) + err = mm_util_decode_from_jpeg_memory_with_downscale(jpeg_data, _handle->src_buffer, _handle->src_size, _convert_jpeg_colorspace_tbl[_handle->colorspace], _convert_decode_scale_tbl[_handle->down_scale]); + else + err = mm_util_decode_from_jpeg_memory(jpeg_data, _handle->src_buffer, _handle->src_size, _convert_jpeg_colorspace_tbl[_handle->colorspace]); + } + + if (err == MM_UTIL_ERROR_NONE) { + *(_handle->dst_buffer) = jpeg_data->data; + _handle->dst_size = jpeg_data->size; + _handle->width = jpeg_data->width; + _handle->height = jpeg_data->height; + } + } + break; case IMAGE_UTIL_PNG: { mm_util_png_data *png_data; @@ -1084,6 +1277,18 @@ int image_util_decode_destroy(image_util_decode_h handle) } switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + { + mm_util_jpeg_yuv_data *jpeg_data; + + jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; + if (jpeg_data == NULL) { + image_util_error("Invalid jpeg data"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + IMAGE_UTIL_SAFE_FREE(jpeg_data); + } + break; case IMAGE_UTIL_PNG: { mm_util_png_data *png_data; @@ -1133,6 +1338,22 @@ int image_util_decode_destroy(image_util_decode_h handle) return err; } +static int _image_util_encode_create_jpeg_handle(decode_encode_s * handle) +{ + int err = MM_UTIL_ERROR_NONE; + + image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + + mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data)); + image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY); + + handle->image_h = (MMHandleType) _handle; + handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888; + handle->quality = 20; + + return err; +} + static int _image_util_encode_create_png_handle(decode_encode_s * handle) { int err = MM_UTIL_ERROR_NONE; @@ -1186,6 +1407,9 @@ int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * _handle->is_decode = FALSE; switch (image_type) { + case IMAGE_UTIL_JPEG: + err = _image_util_encode_create_jpeg_handle(_handle); + break; case IMAGE_UTIL_PNG: err = _image_util_encode_create_png_handle(_handle); break; @@ -1220,6 +1444,19 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution"); switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + { + mm_util_jpeg_yuv_data *jpeg_data; + + jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; + if (jpeg_data == NULL) { + image_util_error("Invalid jpeg data"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + jpeg_data->width = width; + jpeg_data->height = height; + } + break; case IMAGE_UTIL_PNG: { mm_util_png_data *png_data; @@ -1257,6 +1494,57 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w return err; } +int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace) +{ + int err = IMAGE_UTIL_ERROR_NONE; + decode_encode_s *_handle = (decode_encode_s *) handle; + + if (_handle == NULL || _handle->is_decode == TRUE) { + image_util_error("Invalid Handle"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + + image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(image_util_colorspace_e) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + case IMAGE_UTIL_PNG: + image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + case IMAGE_UTIL_GIF: + image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + break; + default: + image_util_error("Invalid image type"); + return MM_UTIL_ERROR_INVALID_PARAMETER; + } + + _handle->colorspace = colorspace; + + return err; +} + +int image_util_encode_set_jpeg_quality(image_util_encode_h handle, int quality) +{ + int err = IMAGE_UTIL_ERROR_NONE; + decode_encode_s *_handle = (decode_encode_s *) handle; + + if (_handle == NULL || _handle->is_decode == TRUE) { + image_util_error("Invalid Handle"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + if (_handle->image_type != IMAGE_UTIL_JPEG) { + image_util_error("Wrong image format"); + return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT; + } + image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality"); + + _handle->quality = quality; + + return err; +} + int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression) { int err = IMAGE_UTIL_ERROR_NONE; @@ -1271,6 +1559,8 @@ int image_util_encode_set_png_compression(image_util_encode_h handle, image_util image_util_error("Wrong image format"); return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT; } + image_util_retvm_if((compression < IMAGE_UTIL_PNG_COMPRESSION_0 || compression > IMAGE_UTIL_PNG_COMPRESSION_9), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid compression value"); + png_data = (mm_util_png_data *) _handle->image_h; if (png_data == NULL) { image_util_error("Invalid png data"); @@ -1347,6 +1637,14 @@ static int _image_util_encode_internal(decode_encode_s * _handle) int err = MM_UTIL_ERROR_NONE; switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + { + if (_handle->path) + err = mm_util_jpeg_encode_to_file (_handle->path, _handle->src_buffer, _handle->width, _handle->height, _convert_jpeg_colorspace_tbl[_handle->colorspace], _handle->quality); + else + err = mm_util_jpeg_encode_to_memory (_handle->dst_buffer, (int *)&(_handle->dst_size), _handle->src_buffer, _handle->width, _handle->height, _convert_jpeg_colorspace_tbl[_handle->colorspace], _handle->quality); + } + break; case IMAGE_UTIL_PNG: { mm_util_png_data *png_data; @@ -1543,6 +1841,18 @@ int image_util_encode_destroy(image_util_encode_h handle) } switch (_handle->image_type) { + case IMAGE_UTIL_JPEG: + { + mm_util_jpeg_yuv_data *jpeg_data; + + jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; + if (jpeg_data == NULL) { + image_util_error("Invalid jpeg data"); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + IMAGE_UTIL_SAFE_FREE(jpeg_data); + } + break; case IMAGE_UTIL_PNG: { mm_util_png_data *png_data; -- 2.34.1