Improve __mm_util_write_image_to_buffer() 87/238187/8
authorhj kim <backto.kim@samsung.com>
Thu, 9 Jul 2020 07:49:34 +0000 (16:49 +0900)
committerhj kim <backto.kim@samsung.com>
Fri, 10 Jul 2020 03:57:34 +0000 (03:57 +0000)
Unify duplicated API calls into __mm_util_write_image_to_buffer() and remove some parameters which can get from Image

Change-Id: If6110833c305fcc265618ac2f1f139ed45909c99

magick/mm_util_magick.c

index 4c0bb3d..4e5e4f4 100644 (file)
@@ -348,44 +348,62 @@ static int __mm_util_write_image_to_file(Image *image, const char *out_path)
        return ret;
 }
 
-static void * __mm_util_write_image_to_buffer(Image *image, unsigned int width, unsigned int height, const char *map, size_t *pixels_size)
+static int __mm_util_write_image_to_buffer(Image *image, mm_util_color_format_e format, mm_util_image_h *decoded_image)
 {
        int ret = MM_UTIL_ERROR_NONE;
        ExceptionInfo exception;
-       size_t _pixels_size = 0;
+       size_t pixels_size = 0;
        void *pixels = NULL;
+       unsigned int width = 0;
+       unsigned int height = 0;
+       char *map = NULL;
 
        mm_util_fenter();
 
-       mm_util_retvm_if(image == NULL, NULL, "invalid image");
-       mm_util_retvm_if(!MMUTIL_STRING_VALID(map), NULL, "invalid map");
+       mm_util_retvm_if(!image, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
+
+       ret = __mm_util_get_map(format, &map);
+       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to get map");
 
        GetExceptionInfo(&exception);
 
-       _pixels_size = sizeof(unsigned char) * strlen(map) * width *height;
-       pixels = MagickMalloc(_pixels_size);
+       width = image->columns;
+       height = image->rows;
+
+       pixels_size = sizeof(unsigned char) * strlen(map) * width * height;
+       pixels = MagickMalloc(pixels_size);
 
        if (pixels == NULL) {
                mm_util_error("Error: MagickMalloc failed.");
+               ret = MM_UTIL_ERROR_OUT_OF_MEMORY;
                goto ERROR;
        }
 
        ret = DispatchImage(image, 0, 0, width, height, map, CharPixel, pixels, &exception);
        if (ret == MagickFail) {
                mm_util_error("Failed to Dispatch Image into dst buffer");
+               if (exception.severity != UndefinedException)
+                       CatchException(&exception);
+
                MagickFree(pixels);
-               pixels = NULL;
+               ret = MM_UTIL_ERROR_INVALID_OPERATION;
                goto ERROR;
        }
 
-       *pixels_size = _pixels_size;
+       ret = mm_image_create_image(width, height, format, pixels, pixels_size, decoded_image);
+       if (ret != MM_UTIL_ERROR_NONE)
+               mm_util_error("Error: mm_image_create_image failed.");
+
+       MagickFree(pixels);
+
 ERROR:
 
        DestroyExceptionInfo(&exception);
+       g_free(map);
 
        mm_util_fleave();
 
-       return pixels;
+       return ret;
 }
 
 static bool __mm_util_check_rotation(mm_util_rotate_type_e rotation)
@@ -502,8 +520,6 @@ int mm_util_rotate_B_B(mm_util_image_h src_handle, mm_util_rotate_type_e rotatio
        Image *_image = NULL;
        Image *_processed_image = NULL;
        ExceptionInfo exception;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
        mm_util_retvm_if(!__mm_util_check_rotation(rotation), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid rotation [%d]", rotation);
@@ -530,18 +546,7 @@ int mm_util_rotate_B_B(mm_util_image_h src_handle, mm_util_rotate_type_e rotatio
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, _src_handle->color, pixels, pixels_size, dst_handle);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_processed_image, _src_handle->color, dst_handle);
 
 ERROR:
 
@@ -604,12 +609,9 @@ ERROR:
 int mm_util_rotate_P_B(const char *src_path, mm_util_rotate_type_e rotation, mm_util_color_format_e req_format, mm_util_image_h *dst_handle)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *map = NULL;
        Image *_image = NULL;
        Image *_processed_image = NULL;
        ExceptionInfo exception;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path");
        mm_util_retvm_if(!__mm_util_check_rotation(rotation), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid rotation [%d]", rotation);
@@ -617,9 +619,6 @@ int mm_util_rotate_P_B(const char *src_path, mm_util_rotate_type_e rotation, mm_
 
        mm_util_sec_debug("src_path [%s] rotation [%d] req_format [%d]", src_path, rotation, req_format);
 
-       ret = __mm_util_get_map(req_format, &map);
-       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to get map");
-
        __mm_util_init(&exception);
 
        _image = __mm_util_read_image_from_file(src_path);
@@ -636,25 +635,12 @@ int mm_util_rotate_P_B(const char *src_path, mm_util_rotate_type_e rotation, mm_
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, req_format, pixels, pixels_size, dst_handle);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_processed_image, req_format, dst_handle);
 
 ERROR:
 
        __mm_util_finalize(_image, _processed_image, &exception);
 
-       g_free(map);
-
        mm_util_fleave();
 
        return ret;
@@ -709,8 +695,6 @@ int mm_util_resize_B_B(mm_util_image_h src_handle, unsigned int req_width, unsig
        Image *_image = NULL;
        Image *_processed_image = NULL;
        ExceptionInfo exception;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
        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);
@@ -743,18 +727,7 @@ int mm_util_resize_B_B(mm_util_image_h src_handle, unsigned int req_width, unsig
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, _src_handle->color, pixels, pixels_size, dst_handle);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_processed_image, _src_handle->color, dst_handle);
 
 ERROR:
 
@@ -823,12 +796,9 @@ ERROR:
 int mm_util_resize_P_B(const char *src_path, unsigned int req_width, unsigned int req_height, mm_util_color_format_e req_format, mm_util_image_h *dst_handle)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *map = NULL;
        Image *_image = NULL;
        Image *_processed_image = NULL;
        ExceptionInfo exception;
-       size_t pixels_size = 0;
-       void *pixels = 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);
@@ -836,9 +806,6 @@ int mm_util_resize_P_B(const char *src_path, unsigned int req_width, unsigned in
 
        mm_util_sec_debug("src_path [%s] req_width [%u] req_height [%u]", src_path, req_width, req_height);
 
-       ret = __mm_util_get_map(req_format, &map);
-       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to get map");
-
        __mm_util_init(&exception);
 
        _image = __mm_util_read_image_from_file(src_path);
@@ -861,25 +828,12 @@ int mm_util_resize_P_B(const char *src_path, unsigned int req_width, unsigned in
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, req_format, pixels, pixels_size, dst_handle);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_processed_image, req_format, dst_handle);
 
 ERROR:
 
        __mm_util_finalize(_image, _processed_image, &exception);
 
-       g_free(map);
-
        mm_util_fleave();
 
        return ret;
@@ -938,8 +892,6 @@ int mm_util_convert_B_B(mm_util_image_h src_handle, mm_util_color_format_e req_f
        char *map = NULL;
        Image *_image = NULL;
        ExceptionInfo exception;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
        mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
@@ -960,31 +912,12 @@ int mm_util_convert_B_B(mm_util_image_h src_handle, mm_util_color_format_e req_f
 
        g_free(map);
 
-       ret = __mm_util_get_map(req_format, &map);
-       if (ret != MM_UTIL_ERROR_NONE) {
-               mm_util_error("Error: __mm_util_get_map failed.");
-               goto ERROR;
-       }
-
-       pixels = __mm_util_write_image_to_buffer(_image, _image->columns, _image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_image->columns, _image->rows, req_format, pixels, pixels_size, dst_handle);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_image, req_format, dst_handle);
 
 ERROR:
 
        __mm_util_finalize(_image, NULL, &exception);
 
-       g_free(map);
-
        mm_util_fleave();
 
        return ret;
@@ -993,19 +926,13 @@ ERROR:
 int mm_util_decode_image_from_file(const char *path, mm_util_color_format_e format, mm_util_image_h *decoded_image)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *map = NULL;
        Image *_image = NULL;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(!MMUTIL_STRING_VALID(path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid path");
        mm_util_retvm_if(!decoded_image, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded_image");
 
        mm_util_sec_debug("path [%s] format [%d]", path, format);
 
-       ret = __mm_util_get_map(format, &map);
-       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to get map");
-
        __mm_util_init(NULL);
 
        _image = __mm_util_read_image_from_file(path);
@@ -1015,23 +942,11 @@ int mm_util_decode_image_from_file(const char *path, mm_util_color_format_e form
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_image, _image->columns, _image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_image->columns, _image->rows, format, pixels, pixels_size, decoded_image);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_image, format, decoded_image);
 
 ERROR:
 
        __mm_util_finalize(_image, NULL, NULL);
-       g_free(map);
 
        mm_util_fleave();
 
@@ -1041,10 +956,7 @@ ERROR:
 int mm_util_decode_image_from_buffer(const void *buf, size_t buf_size, mm_util_color_format_e format, mm_util_image_h *decoded_image)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *map = NULL;
        Image *_image = NULL;
-       size_t pixels_size = 0;
-       void *pixels = 0;
 
        mm_util_retvm_if(!buf, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buf");
        mm_util_retvm_if(buf_size == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buf_size");
@@ -1052,9 +964,6 @@ int mm_util_decode_image_from_buffer(const void *buf, size_t buf_size, mm_util_c
 
        mm_util_sec_debug("path [%p] buf_size [%zu] format [%d]", buf, buf_size, format);
 
-       ret = __mm_util_get_map(format, &map);
-       mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to get map");
-
        __mm_util_init(NULL);
 
        _image = __mm_util_read_image_from_buffer(buf, buf_size);
@@ -1064,23 +973,11 @@ int mm_util_decode_image_from_buffer(const void *buf, size_t buf_size, mm_util_c
                goto ERROR;
        }
 
-       pixels = __mm_util_write_image_to_buffer(_image, _image->columns, _image->rows, map, &pixels_size);
-       if (pixels == NULL) {
-               mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
-               ret = MM_UTIL_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       ret = mm_image_create_image(_image->columns, _image->rows, format, pixels, pixels_size, decoded_image);
-       if (ret != MM_UTIL_ERROR_NONE)
-               mm_util_error("Error: mm_image_create_image failed.");
-
-       MagickFree(pixels);
+       ret = __mm_util_write_image_to_buffer(_image, format, decoded_image);
 
 ERROR:
 
        __mm_util_finalize(_image, NULL, NULL);
-       g_free(map);
 
        mm_util_fleave();