From: Jiyong Min Date: Wed, 6 Dec 2017 05:52:10 +0000 (+0900) Subject: Code clean up of modifying typo and removing duplicated code X-Git-Tag: submit/tizen/20171211.080624^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=32cf830af01f1b6385fdcb03b914f57c4adc9577;p=platform%2Fcore%2Fapi%2Fimage-util.git Code clean up of modifying typo and removing duplicated code Change-Id: Icb0730c9c93aa23090b746df45d41422d0dbeb5e Signed-off-by: Jiyong Min --- diff --git a/include/image_util_private.h b/include/image_util_private.h index 734c19b..39c54b3 100755 --- a/include/image_util_private.h +++ b/include/image_util_private.h @@ -21,7 +21,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" @@ -72,11 +71,18 @@ extern "C" #define IMAGE_UTIL_SAFE_FREE(src) { if (src) {free(src); src = NULL; } } +#define _NOT_SUPPORTED_COLORSPACE (-1) + +#define MODE_DECODE 1 +#define MODE_ENCODE 2 + +#define DECODE_ENCODE_CAST(obj) ((decode_encode_s *)(obj)) +#define IS_DECODE_MODE(x) (x == MODE_DECODE) +#define IS_ENCODE_MODE(x) (x == MODE_ENCODE) + #define IMGCV_FUNC_NAME "mm_util_cv_extract_representative_color" #define PATH_MMUTIL_IMGCV_LIB PATH_LIBDIR"/libmmutil_imgcv.so" -#define _NOT_SUPPORTED_COLORSPACE (-1) - #define TYPECAST_COLOR(c) convert_type_of_colorspace(c) #define TYPECAST_COLOR_BY_TYPE(c, t) convert_type_of_colorspace_with_image_type(c, t) @@ -131,8 +137,7 @@ typedef struct { void *image_h; unsigned long width; unsigned long height; - bool is_decode; - bool is_encoded; + unsigned int mode; int quality; unsigned int current_buffer_count; unsigned int current_resolution_count; @@ -157,6 +162,22 @@ typedef enum { ERR_TYPE_ENCODE, } image_util_error_type_e; +#define IMAGE_UTIL_TYPE_CHECK(type) \ + image_util_retvm_if((type < IMAGE_UTIL_JPEG || type > IMAGE_UTIL_BMP), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter") + +#define IMAGE_UTIL_SUPPORT_TYPE_CHECK(value, support) \ + image_util_retvm_if((value != support), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "Not supported format") + +#define IMAGE_UTIL_DECODE_HANDLE_CHECK(obj) do { \ + image_util_retvm_if(obj == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid decode handle"); \ + image_util_retvm_if(!IS_DECODE_MODE(DECODE_ENCODE_CAST(obj)->mode), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid decode handle"); \ + } while(0) + +#define IMAGE_UTIL_ENCODE_HANDLE_CHECK(obj) do { \ + image_util_retvm_if(obj == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid encode handle"); \ + image_util_retvm_if(!IS_ENCODE_MODE(DECODE_ENCODE_CAST(obj)->mode), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid encode handle"); \ + } while(0) + gboolean is_valid_colorspace(image_util_colorspace_e colorspace); gboolean is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type); diff --git a/src/image_util.c b/src/image_util.c index cb9f025..e87ff9f 100755 --- a/src/image_util.c +++ b/src/image_util.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include @@ -82,10 +83,7 @@ static int __create_media_format(media_format_mimetype_e mimetype, unsigned int { int err = MEDIA_FORMAT_ERROR_NONE; - if ((new_fmt == NULL) || (width == 0) || (height == 0)) { - image_util_error("Invalid parameter"); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } + image_util_retvm_if((new_fmt == NULL) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); err = media_format_create(new_fmt); if (err != MEDIA_FORMAT_ERROR_NONE) { @@ -126,10 +124,7 @@ static int _image_util_packet_to_image(media_packet_h packet, mm_util_color_imag void *ptr = NULL; media_format_h fmt = NULL; - if ((packet == NULL) || (color_image == NULL)) { - image_util_error("Invalid parameter"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); err = media_packet_get_format(packet, &fmt); if (err != MEDIA_PACKET_ERROR_NONE) { @@ -314,7 +309,7 @@ int image_util_transform_create(transformation_h * handle) image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); transformation_s *_handle = (transformation_s *) calloc(1, sizeof(transformation_s)); - image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY); + image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); _handle->colorspace = _NOT_SUPPORTED_COLORSPACE; _handle->_util_cb = NULL; @@ -597,7 +592,7 @@ int image_util_extract_color_from_memory(const unsigned char *image_buffer, int { int ret = MM_UTIL_ERROR_NONE; - image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null"); + image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null"); GModule *module = NULL; ModuleFunc mmutil_imgcv_module_func = NULL; @@ -629,18 +624,14 @@ int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_ { int idx = 0; + IMAGE_UTIL_TYPE_CHECK(image_type); image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null"); - if ((image_type == IMAGE_UTIL_JPEG) || (image_type == IMAGE_UTIL_PNG) - || (image_type == IMAGE_UTIL_GIF) || (image_type == IMAGE_UTIL_BMP)) { - for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) { - if (is_supported_colorspace(idx, image_type)) - if (false == callback(idx, user_data)) - return IMAGE_UTIL_ERROR_NONE; + for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) { + if (is_supported_colorspace(idx, image_type)) + if (false == callback(idx, user_data)) + return IMAGE_UTIL_ERROR_NONE; - } - } else { - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; } return IMAGE_UTIL_ERROR_NONE; diff --git a/src/image_util_decode.c b/src/image_util_decode.c index 7fbe09d..dbee03c 100755 --- a/src/image_util_decode.c +++ b/src/image_util_decode.c @@ -177,13 +177,13 @@ int image_util_decode_create(image_util_decode_h * handle) image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s)); - image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY); + image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); _handle->src_buffer = NULL; _handle->dst_buffer = NULL; _handle->path = NULL; _handle->image_h = NULL; - _handle->is_decode = TRUE; + _handle->mode = MODE_DECODE; _handle->image_type = _NOT_SUPPORTED_IMAGE_TYPE; *handle = (image_util_decode_h) _handle; @@ -196,10 +196,7 @@ static int _image_util_decode_create_image_handle(image_util_decode_h handle) 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_DECODE_HANDLE_CHECK(handle); switch (_handle->image_type) { case IMAGE_UTIL_JPEG: @@ -231,7 +228,7 @@ int image_util_decode_set_input_path(image_util_decode_h handle, const char *pat decode_encode_s *_handle = (decode_encode_s *) handle; unsigned char *image_header = NULL; - image_util_retvm_if(((_handle == NULL) || (_handle->is_decode == FALSE)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); image_util_retvm_if(!IMAGE_UTIL_STRING_VALID(path), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path"); if (_handle->src_buffer) @@ -259,6 +256,7 @@ int image_util_decode_set_input_path(image_util_decode_h handle, const char *pat } _handle->path = g_strndup(path, strlen(path)); + image_util_retvm_if(_handle->path == NULL, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); return err; } @@ -268,14 +266,8 @@ int image_util_decode_set_input_buffer(image_util_decode_h handle, const unsigne 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 (src_buffer == NULL || src_size == 0) { - image_util_error("Invalid input buffer"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); + image_util_retvm_if((src_buffer == NULL || src_size == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid input buffer"); IMAGE_UTIL_SAFE_FREE(_handle->path); @@ -310,14 +302,8 @@ int image_util_decode_set_output_buffer(image_util_decode_h handle, unsigned cha { 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 (dst_buffer == NULL) { - image_util_error("Invalid output buffer"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); + image_util_retvm_if(dst_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); _handle->dst_buffer = (void **)dst_buffer; @@ -328,19 +314,11 @@ int image_util_decode_set_colorspace(image_util_encode_h handle, image_util_colo { 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_DECODE_HANDLE_CHECK(handle); + IMAGE_UTIL_TYPE_CHECK(_handle->image_type); image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); - if ((_handle->image_type == IMAGE_UTIL_JPEG) || (_handle->image_type == IMAGE_UTIL_PNG) - || (_handle->image_type == IMAGE_UTIL_GIF) || (_handle->image_type == IMAGE_UTIL_BMP)) { - image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); - } else { - image_util_error("Invalid image type"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); _handle->colorspace = colorspace; @@ -351,14 +329,9 @@ int image_util_decode_set_jpeg_downscale(image_util_encode_h handle, image_util_ { 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_DECODE_HANDLE_CHECK(handle); + IMAGE_UTIL_SUPPORT_TYPE_CHECK(_handle->image_type, IMAGE_UTIL_JPEG); + image_util_retvm_if((down_scale < 0 || down_scale >= _NUM_OF_SCALE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downscale is invalid"); _handle->down_scale = down_scale; @@ -370,16 +343,11 @@ static int _image_util_decode_internal(decode_encode_s * _handle) { int err = MM_UTIL_ERROR_NONE; + image_util_retvm_if((_handle == NULL || _handle->image_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid parameter"); 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; - } + mm_util_jpeg_yuv_data *jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; if (_handle->path) { if (_handle->down_scale < sizeof(image_util_scale_e)) @@ -403,13 +371,7 @@ static int _image_util_decode_internal(decode_encode_s * _handle) break; case IMAGE_UTIL_PNG: { - mm_util_png_data *png_data; - - png_data = (mm_util_png_data *) _handle->image_h; - if (png_data == NULL) { - image_util_error("Invalid png data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_png_data *png_data = (mm_util_png_data *) _handle->image_h; if (_handle->path) err = mm_util_decode_from_png_file(png_data, _handle->path); @@ -426,13 +388,7 @@ static int _image_util_decode_internal(decode_encode_s * _handle) break; case IMAGE_UTIL_GIF: { - mm_util_gif_data *gif_data; - - gif_data = (mm_util_gif_data *) _handle->image_h; - if (gif_data == NULL) { - image_util_error("Invalid gif data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_gif_data *gif_data = (mm_util_gif_data *) _handle->image_h; if (_handle->path) err = mm_util_decode_from_gif_file(gif_data, _handle->path); @@ -449,13 +405,7 @@ static int _image_util_decode_internal(decode_encode_s * _handle) break; case IMAGE_UTIL_BMP: { - mm_util_bmp_data *bmp_data; - - bmp_data = (mm_util_bmp_data *) _handle->image_h; - if (bmp_data == NULL) { - image_util_error("Invalid bmp data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_bmp_data *bmp_data = (mm_util_bmp_data *) _handle->image_h; if (_handle->path) err = mm_util_decode_from_bmp_file(bmp_data, _handle->path); @@ -483,14 +433,10 @@ int image_util_decode_run(image_util_decode_h handle, unsigned long *width, unsi 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->path == NULL && _handle->src_buffer == NULL) || _handle->dst_buffer == NULL) { - image_util_error("Invalid input/output"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); + image_util_retvm_if((_handle->path == NULL && _handle->src_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid input"); + image_util_retvm_if(_handle->dst_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid output"); + err = _image_util_decode_internal(_handle); @@ -555,14 +501,9 @@ int image_util_decode_run_async(image_util_decode_h handle, image_util_decode_co 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->path == NULL && _handle->src_buffer == NULL) || _handle->dst_buffer == NULL) { - image_util_error("Invalid input/output"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); + image_util_retvm_if((_handle->path == NULL && _handle->src_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid input"); + image_util_retvm_if(_handle->dst_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid output"); image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback"); image_util_retvm_if((_handle->thread != NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "A thread is alread running"); @@ -592,10 +533,7 @@ int image_util_decode_destroy(image_util_decode_h handle) image_util_debug("image_util_decode_destroy"); - if (_handle == NULL || _handle->is_decode == FALSE) { - image_util_error("Invalid Handle"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_DECODE_HANDLE_CHECK(handle); IMAGE_UTIL_SAFE_FREE(_handle->image_h); diff --git a/src/image_util_encode.c b/src/image_util_encode.c index 1b52abe..b2338a0 100755 --- a/src/image_util_encode.c +++ b/src/image_util_encode.c @@ -122,19 +122,17 @@ int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * int err = IMAGE_UTIL_ERROR_NONE; image_util_debug("image_util_encode_create"); - - image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); + image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s)); - image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY); + image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); _handle->image_type = image_type; _handle->src_buffer = NULL; _handle->dst_buffer = NULL; _handle->path = NULL; _handle->image_h = NULL; - _handle->is_decode = FALSE; - _handle->is_encoded = FALSE; + _handle->mode = MODE_ENCODE; _handle->current_buffer_count = 0; _handle->current_resolution_count = 0; _handle->current_delay_count = 0; @@ -173,35 +171,21 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w 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((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution"); + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + image_util_retvm_if(_handle->image_h == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + image_util_retvm_if((width == 0 || height == 0), 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; - } + mm_util_jpeg_yuv_data *jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h; jpeg_data->width = width; jpeg_data->height = height; } break; case IMAGE_UTIL_PNG: { - mm_util_png_data *png_data; - - png_data = (mm_util_png_data *) _handle->image_h; - if (png_data == NULL) { - image_util_error("Invalid png data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_png_data *png_data = (mm_util_png_data *) _handle->image_h; mm_util_png_encode_set_width(png_data, width); mm_util_png_encode_set_height(png_data, height); } @@ -209,10 +193,6 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w case IMAGE_UTIL_GIF: { mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h; - if (gif_data == NULL) { - image_util_error("Invalid gif data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } image_util_retvm_if((width > INT_MAX) || (height > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution"); @@ -239,13 +219,7 @@ int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long w break; case IMAGE_UTIL_BMP: { - mm_util_bmp_data *bmp_data; - - bmp_data = (mm_util_bmp_data *) _handle->image_h; - if (bmp_data == NULL) { - image_util_error("Invalid bmp data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_bmp_data *bmp_data = (mm_util_bmp_data *) _handle->image_h; mm_util_bmp_encode_set_width(bmp_data, width); mm_util_bmp_encode_set_height(bmp_data, height); } @@ -266,20 +240,11 @@ int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colo 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_ENCODE_HANDLE_CHECK(_handle); + IMAGE_UTIL_TYPE_CHECK(_handle->image_type); image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); - - if ((_handle->image_type == IMAGE_UTIL_JPEG) || (_handle->image_type == IMAGE_UTIL_PNG) - || (_handle->image_type == IMAGE_UTIL_GIF) || (_handle->image_type == IMAGE_UTIL_BMP)) { - image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); - } else { - image_util_error("Invalid image type"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); _handle->colorspace = colorspace; @@ -291,14 +256,9 @@ int image_util_encode_set_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_ENCODE_HANDLE_CHECK(_handle); + IMAGE_UTIL_SUPPORT_TYPE_CHECK(_handle->image_type, IMAGE_UTIL_JPEG); + image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality"); _handle->quality = quality; @@ -312,21 +272,13 @@ int image_util_encode_set_png_compression(image_util_encode_h handle, image_util decode_encode_s *_handle = (decode_encode_s *) handle; mm_util_png_data *png_data; - if (_handle == NULL || _handle->is_decode == TRUE) { - image_util_error("Invalid Handle"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - if (_handle->image_type != IMAGE_UTIL_PNG) { - image_util_error("Wrong image format"); - return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + IMAGE_UTIL_SUPPORT_TYPE_CHECK(_handle->image_type, IMAGE_UTIL_PNG); + 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"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if(png_data == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid png data"); mm_util_png_encode_set_compression_level(png_data, compression); @@ -339,20 +291,11 @@ int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsig decode_encode_s *_handle = (decode_encode_s *) handle; mm_gif_file_h gif_data = NULL; - if (_handle == NULL || _handle->is_decode == TRUE) { - image_util_error("Invalid Handle"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - if (_handle->image_type != IMAGE_UTIL_GIF) { - image_util_error("Wrong image format"); - return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + IMAGE_UTIL_SUPPORT_TYPE_CHECK(_handle->image_type, IMAGE_UTIL_GIF); gif_data = (mm_gif_file_h) _handle->image_h; - if (gif_data == NULL) { - image_util_error("Invalid gif data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if(gif_data == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid gif data"); image_util_retvm_if((delay_time > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay time"); @@ -377,10 +320,8 @@ int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigne 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_ENCODE_HANDLE_CHECK(_handle); + if (src_buffer == NULL) { image_util_error("Invalid input buffer"); return IMAGE_UTIL_ERROR_INVALID_PARAMETER; @@ -390,10 +331,8 @@ int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigne if (_handle->image_type == IMAGE_UTIL_GIF) { mm_gif_file_h gif_data = (mm_gif_file_h) _handle->image_h; mm_gif_image_h frame = NULL; - if (gif_data == NULL) { - image_util_error("Invalid gif data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_retvm_if(gif_data == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid gif data"); + err = _image_util_encode_get_gif_frame(gif_data, _handle->current_buffer_count, &frame); if (err != IMAGE_UTIL_ERROR_NONE) { image_util_error("_image_util_encode_get_gif_frame is failed %d", err); @@ -421,10 +360,8 @@ int image_util_encode_set_output_path(image_util_encode_h handle, const char *pa 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_ENCODE_HANDLE_CHECK(_handle); + image_util_retvm_if(!IMAGE_UTIL_STRING_VALID(path), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path"); if (_handle->dst_buffer) @@ -433,6 +370,7 @@ int image_util_encode_set_output_path(image_util_encode_h handle, const char *pa IMAGE_UTIL_SAFE_FREE(_handle->path); _handle->path = g_strndup(path, strlen(path)); + image_util_retvm_if(_handle->path == NULL, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY"); return err; } @@ -442,14 +380,9 @@ int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned cha 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 (dst_buffer == NULL) { - image_util_error("Invalid output buffer"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + + image_util_retvm_if(dst_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid output buffer"); IMAGE_UTIL_SAFE_FREE(_handle->path); @@ -462,6 +395,7 @@ static int _image_util_encode_internal(decode_encode_s * _handle) { int err = MM_UTIL_ERROR_NONE; + image_util_retvm_if((_handle == NULL || _handle->image_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); switch (_handle->image_type) { case IMAGE_UTIL_JPEG: { @@ -473,13 +407,7 @@ static int _image_util_encode_internal(decode_encode_s * _handle) break; case IMAGE_UTIL_PNG: { - mm_util_png_data *png_data; - - png_data = (mm_util_png_data *) _handle->image_h; - if (png_data == NULL) { - image_util_error("Invalid png data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_png_data *png_data = (mm_util_png_data *) _handle->image_h; if (_handle->path) err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path); @@ -499,10 +427,7 @@ static int _image_util_encode_internal(decode_encode_s * _handle) { mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h; unsigned long encoded_buffer_size = 0; - if (gif_data == NULL) { - image_util_error("Invalid gif data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + image_util_debug("[Count] buffer:%d, resolution:%d, delay:%d", _handle->current_buffer_count, _handle->current_resolution_count, _handle->current_delay_count); if ((_handle->current_buffer_count > 1) && ((_handle->current_buffer_count != _handle->current_resolution_count) || (_handle->current_buffer_count != _handle->current_delay_count))) { image_util_error("Total frame count does not match with the data set, for animated gif encoding"); @@ -530,13 +455,7 @@ static int _image_util_encode_internal(decode_encode_s * _handle) break; case IMAGE_UTIL_BMP: { - mm_util_bmp_data *bmp_data; - - bmp_data = (mm_util_bmp_data *) _handle->image_h; - if (bmp_data == NULL) { - image_util_error("Invalid bmp data"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + mm_util_bmp_data *bmp_data = (mm_util_bmp_data *) _handle->image_h; bmp_data->data = _handle->src_buffer[0]; if (_handle->path) { @@ -571,23 +490,12 @@ int image_util_encode_run(image_util_encode_h handle, unsigned long long *size) 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->path == NULL && _handle->dst_buffer == NULL) { - image_util_error("Invalid output"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) { - image_util_error("Invalid input"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + image_util_retvm_if((_handle->path == NULL && _handle->dst_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid output"); + image_util_retvm_if(_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid input"); image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution"); err = _image_util_encode_internal(_handle); - _handle->is_encoded = TRUE; - if (err != IMAGE_UTIL_ERROR_NONE) { image_util_error("Error - encode run"); return err; @@ -622,7 +530,6 @@ gpointer _image_util_encode_thread(gpointer data) IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb); _handle->thread = NULL; - _handle->is_encoded = TRUE; image_util_debug("exit thread"); return NULL; @@ -649,18 +556,9 @@ int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_co 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->path == NULL && _handle->dst_buffer == NULL) { - image_util_error("Invalid output"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) { - image_util_error("Invalid input"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); + image_util_retvm_if((_handle->path == NULL && _handle->dst_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid output"); + image_util_retvm_if(_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid input"); image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution"); image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback"); image_util_retvm_if((_handle->thread != NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running"); @@ -691,10 +589,7 @@ int image_util_encode_destroy(image_util_encode_h handle) image_util_debug("image_util_encode_destroy"); - if (_handle == NULL || _handle->is_decode == TRUE) { - image_util_error("Invalid Handle"); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } + IMAGE_UTIL_ENCODE_HANDLE_CHECK(_handle); _image_util_encode_destroy_image_handle(_handle);