From 33c23566d2f6a2f1c894fa1e660c69babba972ab Mon Sep 17 00:00:00 2001 From: "jiyong.min" Date: Thu, 16 Apr 2020 13:36:28 +0900 Subject: [PATCH] Add new function to resize and rotate with exif orientation Change-Id: I9535f30314911e2ffab25978e1df874af851e570 --- magick/include/mm_util_magick.h | 2 + magick/mm_util_magick.c | 81 +++++++++++++++++++++++++++++++++- magick/test/mm_util_magick_testsuite.c | 25 +++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/magick/include/mm_util_magick.h b/magick/include/mm_util_magick.h index 7f0da4b..8c6fa7a 100644 --- a/magick/include/mm_util_magick.h +++ b/magick/include/mm_util_magick.h @@ -63,6 +63,8 @@ int mm_util_resize_P_P(const char *src_path, unsigned int req_width, unsigned in int mm_util_convert_B_B(mm_util_image_h src_handle, mm_util_color_format_e req_format, mm_util_image_h *dst_handle); +int mm_util_resize_and_rotate_P_P(const char *src_path, unsigned int req_width, unsigned int req_height, const char *dst_path); + int mm_util_extract_image_info(const char *path, mm_util_img_codec_type *type, unsigned int *width, unsigned int *height); int mm_util_decode_image_from_file(const char *path, mm_util_color_format_e format, mm_util_image_h *decoded_image); diff --git a/magick/mm_util_magick.c b/magick/mm_util_magick.c index 2ddbdf2..f017331 100644 --- a/magick/mm_util_magick.c +++ b/magick/mm_util_magick.c @@ -353,7 +353,7 @@ static void * __mm_util_write_image_to_buffer(Image *image, unsigned int width, pixels = MagickMalloc(_pixels_size); if (pixels == NULL) { - mm_util_error("Error: calloc failed."); + mm_util_error("Error: MagickMalloc failed."); goto ERROR; } @@ -1173,3 +1173,82 @@ ERROR: return ret; } + +int mm_util_resize_and_rotate_P_P(const char *src_path, unsigned int req_width, unsigned int req_height, const char *dst_path) +{ + int ret = MM_UTIL_ERROR_NONE; + Image *_image = NULL; + Image *_resized_image = NULL; + Image *_rotated_image = NULL; + ExceptionInfo exception; + mm_util_magick_rotate_type angle = MM_UTIL_ROTATE_0; + + mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path"); + mm_util_retvm_if((req_width == 0) || (req_height == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size W[%d] H[%d]", req_width, req_height); + mm_util_retvm_if(!MMUTIL_STRING_VALID(dst_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_path"); + + mm_util_sec_debug("src_path [%s] req_width [%u] req_height [%u] dst_path [%s]", src_path, req_width, req_height, dst_path); + + __mm_util_init(&exception); + + _image = __mm_util_read_image_from_file(src_path); + if (_image == NULL) { + mm_util_error("Error: __mm_util_read_image_from_file failed."); + ret = MM_UTIL_ERROR_INVALID_OPERATION; + goto ERROR; + } + + if ((_image->columns < req_width) || (_image->rows < req_height)) { + mm_util_error("Wrong Size. image [%lu * %lu], request [%u * %u]", _image->columns, _image->rows, req_width, req_height); + ret = MM_UTIL_ERROR_INVALID_PARAMETER; + goto ERROR; + } + + _resized_image = __mm_util_resize_image(_image, req_width, req_height); + if (_resized_image == NULL) { + mm_util_error("Error: __mm_util_resize_image failed."); + ret = MM_UTIL_ERROR_INVALID_OPERATION; + goto ERROR; + } + + switch(_resized_image->orientation) { + case TopRightOrientation: + case RightTopOrientation: + angle = MM_UTIL_ROTATE_90; + break; + case BottomRightOrientation: + case RightBottomOrientation: + angle = MM_UTIL_ROTATE_180; + break; + case BottomLeftOrientation: + case LeftBottomOrientation: + angle = MM_UTIL_ROTATE_270; + break; + case TopLeftOrientation: + case LeftTopOrientation: + break; + default: + mm_util_warn("Not supported orientation %d. so apply MM_UTIL_ROTATE_0", _resized_image->orientation); + break; + } + + if (angle != MM_UTIL_ROTATE_0) { + _rotated_image = __mm_util_rotate_image(_resized_image, angle); + if (_rotated_image == NULL) { + mm_util_error("Error: __mm_util_resize_image failed."); + ret = MM_UTIL_ERROR_INVALID_OPERATION; + goto ERROR; + } + } + + ret = __mm_util_write_image_to_file(((_rotated_image) ? _rotated_image : _resized_image), dst_path); + +ERROR: + + DestroyImageList(_image); + __mm_util_finalize(_resized_image, _rotated_image, &exception); + + mm_util_fleave(); + + return ret; +} diff --git a/magick/test/mm_util_magick_testsuite.c b/magick/test/mm_util_magick_testsuite.c index 8d28563..9999ff7 100644 --- a/magick/test/mm_util_magick_testsuite.c +++ b/magick/test/mm_util_magick_testsuite.c @@ -40,6 +40,7 @@ static int _magick_resize_P_B_test(unsigned int req_width, unsigned int req_heig static int _magick_resize_B_P_test(unsigned int req_width, unsigned int req_height, mm_util_color_format_e req_format); static int _magick_resize_B_B_test(unsigned int req_width, unsigned int req_height, mm_util_color_format_e req_format); static int _magick_convert_B_B_test(mm_util_color_format_e in_format, mm_util_color_format_e out_format); +static int _magick_resize_and_rotate_P_P_test(unsigned int req_width, unsigned int req_height); static char *__get_dst_path(mm_util_magick_rotate_type angle) { @@ -430,6 +431,23 @@ static int _magick_convert_B_B_test(mm_util_color_format_e in_format, mm_util_co return ret; } +static int _magick_resize_and_rotate_P_P_test(unsigned int req_width, unsigned int req_height) +{ + int ret = MM_UTIL_ERROR_NONE; + char * src_path = "/opt/usr/home/owner/origin.jpg"; + char dst_path[1024] = {0, }; + memset(dst_path, 0x00, sizeof(dst_path)); + snprintf(dst_path, sizeof(dst_path), "/opt/usr/home/owner/media/resize_rotate_pp_%u_%u.jpg", req_width, req_height); + + printf("* Resize P P Test * W * H = [%u * %u], path = [%s] \n", req_width, req_height, dst_path); + + ret = mm_util_resize_and_rotate_P_P(src_path, req_width, req_height, dst_path); + if (ret != MM_UTIL_ERROR_NONE) + printf("Fail mm_util_resize_P_P [%d]\n", ret); + + return ret; +} + int main(int argc, char *argv[]) { int ret = MM_UTIL_ERROR_NONE; @@ -506,6 +524,13 @@ int main(int argc, char *argv[]) ret = _magick_convert_B_B_test(MM_UTIL_COLOR_RGBA, MM_UTIL_COLOR_RGB24); ret = _magick_convert_B_B_test(MM_UTIL_COLOR_BGRA, MM_UTIL_COLOR_RGB24); + } else if (strcmp(argv[1], "10") == 0) { + ret = _magick_resize_and_rotate_P_P_test(3000, 2000); + ret = _magick_resize_and_rotate_P_P_test(199, 299); + ret = _magick_resize_and_rotate_P_P_test(3, 2); + ret = _magick_resize_and_rotate_P_P_test(10000, 10000); + ret = _magick_resize_and_rotate_P_P_test(0, 2000); //invalid parameter + } else { printf("Invalid parameter\n"); } -- 2.7.4