Modify mm_util_crop_image() API to receive allocated buffer and buffer info 38/170338/2
authorhj kim <backto.kim@samsung.com>
Mon, 19 Feb 2018 08:07:12 +0000 (17:07 +0900)
committerhj kim <backto.kim@samsung.com>
Mon, 19 Feb 2018 08:07:33 +0000 (17:07 +0900)
Change-Id: If059b7e93e7a13bbb859e58d64d92c45c62674ed

imgp/include/mm_util_imgp.h
imgp/mm_util_imgp.c
imgp/test/mm_util_imgp_testsuite.c
packaging/libmm-utility.spec

index 61ccbbd..1f48d85 100755 (executable)
@@ -248,7 +248,9 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
  * @since       R1, 1.0
  */
 int mm_util_crop_image(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format,
-                       unsigned int crop_start_x, unsigned int crop_start_y, unsigned int *crop_dest_width, unsigned int *crop_dest_height, unsigned char *dst);
+                       unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height,
+                       unsigned char **dst, unsigned int *result_buf_width, unsigned int *result_buf_height, size_t *result_buf_size);
+
 
 #ifdef __cplusplus
 }
index 6eca295..c93c579 100755 (executable)
@@ -414,6 +414,8 @@ static int __mm_confirm_dst_width_height(unsigned int src_width, unsigned int sr
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
+       mm_util_fenter();
+
        switch (angle) {
        case MM_UTIL_ROTATE_0:
        case MM_UTIL_ROTATE_180:
@@ -471,7 +473,7 @@ static int __mm_set_imgp_info_s(imgp_info_s *_imgp_info_s, mm_util_color_format_
        _imgp_info_s->buffer_size = 0;
        _imgp_info_s->angle = angle;
 
-       mm_util_debug("src_width[%u] src_height[%u] dst_width[%u] dst_height[%u] rotation_value[%d]", _imgp_info_s->src_width, _imgp_info_s->src_height, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->angle);
+       mm_util_debug("src_w[%u] src_h[%u] dst_w[%u] dst_h[%u] rotation[%d]", _imgp_info_s->src_width, _imgp_info_s->src_height, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->angle);
 
        return ret;
 }
@@ -712,6 +714,10 @@ static int __mm_util_processing(mm_util_s *handle)
        unsigned int src_width = 0, src_height = 0;
        mm_util_color_format_e src_format = -1;
        unsigned int src_index = 0, dst_index = 0;
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
+       unsigned char *res_buffer = NULL;
+       size_t res_buffer_size = 0;
 
        if (handle == NULL) {
                mm_util_error("Invalid arguments [tag null]");
@@ -729,22 +735,18 @@ static int __mm_util_processing(mm_util_s *handle)
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
        memcpy(dst_buf[src_index], handle->src->data, handle->src->size);
+
        if (handle->set_crop) {
                dst_index++;
-               __mm_util_get_crop_image_size(src_format, handle->dst_width, handle->dst_height, &dst_buf_size);
-               dst_buf[dst_index] = calloc(1, dst_buf_size);
-               if (dst_buf[dst_index] == NULL) {
-                       mm_util_error("[multi func] memory allocation error");
-                       __mm_destroy_temp_buffer(dst_buf);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-               ret = mm_util_crop_image(dst_buf[src_index], src_width, src_height, src_format,
-               handle->start_x, handle->start_y, &handle->dst_width, &handle->dst_height, dst_buf[dst_index]);
+
+               ret = mm_util_crop_image(dst_buf[src_index], src_width, src_height, src_format, handle->start_x, handle->start_y, handle->dst_width, handle->dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        __mm_destroy_temp_buffer(dst_buf);
                        mm_util_error("mm_util_crop_image failed");
                        return ret;
                }
+               dst_buf[dst_index] = res_buffer;
+
                src_index = dst_index;
                src_width = handle->dst_width;
                src_height = handle->dst_height;
@@ -1145,6 +1147,10 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
        GModule *_module = NULL;
        unsigned char *output_buffer = NULL;
        unsigned int output_buffer_size = 0;
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
+       unsigned char *res_buffer = NULL;
+       size_t res_buffer_size = 0;
 
        mm_util_retvm_if(src == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
        mm_util_retvm_if(dst == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst");
@@ -1175,8 +1181,6 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
                goto ERROR;
        }
 
-       mm_util_debug("Sucess __mm_set_imgp_info_s");
-
        ret = __mm_util_get_buffer_size(dst_format, src_width, src_height, &output_buffer_size);
        if (ret != MM_UTIL_ERROR_NONE) {
                mm_util_error("__mm_util_get_buffer_size failed");
@@ -1190,12 +1194,13 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
        }
 
        if ((_imgp_info_s->dst_width != _imgp_info_s->output_stride || _imgp_info_s->dst_height != _imgp_info_s->output_elevation) && __mm_is_rgb_format(src_format)) {
-               ret = mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, dst_format, 0, 0, &_imgp_info_s->dst_width, &_imgp_info_s->dst_height, dst);
+               ret = mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, dst_format, 0, 0, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        mm_util_error("mm_util_crop_image failed");
                        ret = MM_UTIL_ERROR_INVALID_OPERATION;
                        goto ERROR;
                }
+               memcpy(dst, res_buffer, res_buffer_size);
        } else {
                memcpy(dst, output_buffer, _imgp_info_s->buffer_size);
        }
@@ -1209,6 +1214,7 @@ ERROR:
        __mm_util_imgp_finalize(_module, _imgp_info_s);
 
        MMUTIL_SAFE_FREE(output_buffer);
+       MMUTIL_SAFE_FREE(res_buffer);
 
        mm_util_fleave();
 
@@ -1222,6 +1228,10 @@ int mm_util_resize_image(const unsigned char *src, unsigned int src_width, unsig
        GModule *_module = NULL;
        unsigned char *output_buffer = NULL;
        unsigned int output_buffer_size = 0;
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
+       unsigned char *res_buffer = NULL;
+       size_t res_buffer_size = 0;
 
        mm_util_retvm_if(src == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
        mm_util_retvm_if(src_width == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_width");
@@ -1255,8 +1265,6 @@ int mm_util_resize_image(const unsigned char *src, unsigned int src_width, unsig
                goto ERROR;
        }
 
-       mm_util_debug("Sucess __mm_set_imgp_info_s");
-
        if (g_strrstr(g_module_name(_module), GST)) {
                if (__mm_gst_can_resize_format(_imgp_info_s->src_format) == FALSE) {
                        mm_util_error("#RESIZE ERROR# IMAGE_NOT_SUPPORT_FORMAT");
@@ -1279,9 +1287,15 @@ int mm_util_resize_image(const unsigned char *src, unsigned int src_width, unsig
        }
 
        if ((_imgp_info_s->dst_width != _imgp_info_s->output_stride || _imgp_info_s->dst_height != _imgp_info_s->output_elevation) && __mm_is_rgb_format(src_format)) {
-               mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, src_format, 0, 0, &_imgp_info_s->dst_width, &_imgp_info_s->dst_height, dst);
-               *dst_width = _imgp_info_s->dst_width;
-               *dst_height = _imgp_info_s->dst_height;
+               ret = mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, src_format, 0, 0, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       mm_util_error("mm_util_crop_image failed");
+                       ret = MM_UTIL_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
+               memcpy(dst, res_buffer, res_buffer_size);
+               *dst_width = res_w;
+               *dst_height = res_h;
        } else {
                memcpy(dst, output_buffer, _imgp_info_s->buffer_size);
                *dst_width = _imgp_info_s->dst_width;
@@ -1297,6 +1311,7 @@ ERROR:
        __mm_util_imgp_finalize(_module, _imgp_info_s);
 
        MMUTIL_SAFE_FREE(output_buffer);
+       MMUTIL_SAFE_FREE(res_buffer);
 
        mm_util_fleave();
 
@@ -1310,6 +1325,10 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
        GModule *_module = NULL;
        unsigned char *output_buffer = NULL;
        unsigned int output_buffer_size = 0;
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
+       unsigned char *res_buffer = NULL;
+       size_t res_buffer_size = 0;
 
        mm_util_retvm_if(src == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
        mm_util_retvm_if(src_width == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_width");
@@ -1321,7 +1340,7 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
        mm_util_retvm_if(dst_height == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_height");
        mm_util_retvm_if((angle < MM_UTIL_ROTATE_0) || (angle >= MM_UTIL_ROTATE_NUM), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid angle [%d]", angle);
 
-       mm_util_debug("src_width [%d] src_height [%d] src_format[%d]", src_width, src_height, src_format);
+       mm_util_debug("src_w[%u] src_h[%u] src_format[%u] angle[%u]", src_width, src_height, src_format, angle);
 
        _mm_util_imgp_func = __mm_util_initialize(IMGP_ROT, src_format, 0, &_module);
        if (_mm_util_imgp_func == NULL) {
@@ -1336,7 +1355,6 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
                goto ERROR;
        }
 
-       mm_util_debug("__mm_confirm_dst_width_height");
        ret = __mm_confirm_dst_width_height(src_width, src_height, dst_width, dst_height, angle);
        if (ret != MM_UTIL_ERROR_NONE) {
                mm_util_error("dst_width || dest_height size Error");
@@ -1351,7 +1369,6 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
                ret = MM_UTIL_ERROR_INVALID_OPERATION;
                goto ERROR;
        }
-       mm_util_debug("Sucess __mm_set_imgp_info_s");
 
        if (g_strrstr(g_module_name(_module), GST)) {
                if (__mm_gst_can_rotate_format(_imgp_info_s->src_format) == FALSE) {
@@ -1386,7 +1403,13 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
                        start_x = 0;
                        start_y = _imgp_info_s->output_elevation - _imgp_info_s->dst_height;
                }
-               mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, src_format, start_x, start_y, &_imgp_info_s->dst_width, &_imgp_info_s->dst_height, dst);
+               ret = mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, src_format, start_x, start_y, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       mm_util_error("mm_util_crop_image failed");
+                       ret = MM_UTIL_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
+               memcpy(dst, res_buffer, res_buffer_size);
                *dst_width = _imgp_info_s->dst_width;
                *dst_height = _imgp_info_s->dst_height;
        } else {
@@ -1396,14 +1419,14 @@ int mm_util_rotate_image(const unsigned char *src, unsigned int src_width, unsig
        }
 
        /* Output result*/
-       mm_util_debug("dst: %p dst_width: %d, dst_height: %d, output_stride: %d, output_elevation: %d",
-                       dst, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
+       mm_util_debug("dst[%p] dst_w[%u] dst_h[%u] output_stride[%u] output_elevation[%u]", dst, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
 
 ERROR:
        /* Finalize */
        __mm_util_imgp_finalize(_module, _imgp_info_s);
 
        MMUTIL_SAFE_FREE(output_buffer);
+       MMUTIL_SAFE_FREE(res_buffer);
 
        mm_util_fleave();
 
@@ -1411,9 +1434,13 @@ ERROR:
 }
 
 int mm_util_crop_image(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format,
-unsigned int crop_start_x, unsigned int crop_start_y, unsigned int *crop_dest_width, unsigned int *crop_dest_height, unsigned char *dst)
+unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height, unsigned char **dst, unsigned int *result_buf_width, unsigned int *result_buf_height, size_t *result_buf_size)
 {
        int ret = MM_UTIL_ERROR_NONE;
+       unsigned int dst_buf_size = 0;
+       unsigned char *dst_buffer = NULL;
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
 
        mm_util_retvm_if(src == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src");
        mm_util_retvm_if(src_width == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_width");
@@ -1421,50 +1448,72 @@ unsigned int crop_start_x, unsigned int crop_start_y, unsigned int *crop_dest_wi
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(src_format) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_format [%d]", src_format);
        mm_util_retvm_if((__mm_util_check_format(src_format) == FALSE), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported src_format [%d]", src_format);
        mm_util_retvm_if(dst == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst");
-       mm_util_retvm_if((crop_start_x + *crop_dest_width > src_width), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid position [%d]]", crop_start_x);
-       mm_util_retvm_if((crop_start_y + *crop_dest_height > src_height), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid position [%d]", crop_start_y);
+       mm_util_retvm_if((crop_start_x + crop_dest_width > src_width), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid position [%d]]", crop_start_x);
+       mm_util_retvm_if((crop_start_y + crop_dest_height > src_height), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid position [%d]", crop_start_y);
 
        mm_util_debug("[Input] src[%p] src_width[%u] src_height[%u] src_format[%d] crop_start_x[%u] crop_start_y[%u] crop_dest_width[%u] crop_dest_height[%u]",
-                                               src, src_width, src_height, src_format, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height);
+                                               src, src_width, src_height, src_format, crop_start_x, crop_start_y, crop_dest_width, crop_dest_height);
+
+       __mm_util_get_crop_image_size(src_format, crop_dest_width, crop_dest_height, &dst_buf_size);
+       mm_util_retvm_if(dst_buf_size == 0, MM_UTIL_ERROR_INVALID_OPERATION, "fail to get dst_buf_size");
+
+       dst_buffer = calloc(1, dst_buf_size);
+       mm_util_retvm_if(dst_buffer == NULL, MM_UTIL_ERROR_OUT_OF_MEMORY, "memory alloc fail");
+
+       res_w = crop_dest_width;
+       res_h = crop_dest_height;
 
        switch (src_format) {
        case MM_UTIL_COLOR_RGB24: {
-               ret = __mm_util_crop_rgb888(src, src_width, src_height, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
+               ret = __mm_util_crop_rgb888(src, src_width, src_height, crop_start_x, crop_start_y, res_w, res_h, dst_buffer);
                break;
                }
        case MM_UTIL_COLOR_RGB16: {
-               ret = __mm_util_crop_rgb565(src, src_width, src_height, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
+               ret = __mm_util_crop_rgb565(src, src_width, src_height, crop_start_x, crop_start_y, res_w, res_h, dst_buffer);
                break;
                }
        case MM_UTIL_COLOR_ARGB:
        case MM_UTIL_COLOR_BGRA:
        case MM_UTIL_COLOR_RGBA:
        case MM_UTIL_COLOR_BGRX: {
-               ret = __mm_util_crop_rgba32(src, src_width, src_height, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
+               ret = __mm_util_crop_rgba32(src, src_width, src_height, crop_start_x, crop_start_y, res_w, res_h, dst_buffer);
                break;
                }
        case MM_UTIL_COLOR_I420:
        case MM_UTIL_COLOR_YUV420: {
-               if ((*crop_dest_width % 2) != 0) {
-                       mm_util_warn("#YUV Width value(%d) must be even at least# ", *crop_dest_width);
-                       *crop_dest_width = ((*crop_dest_width+1)>>1)<<1;
-                       mm_util_debug("Image isplay is suceeded when YUV crop width value %d", *crop_dest_width);
+               if ((crop_dest_width % 2) != 0) {
+                       mm_util_warn("#YUV Width value(%d) must be even at least# ", crop_dest_width);
+                       res_w = ((crop_dest_width+1)>>1)<<1;
+                       mm_util_debug("Image isplay is suceeded when YUV crop width value %d", res_w);
                }
 
-               if ((*crop_dest_height % 2) != 0) { /* height value must be also even when crop yuv image */
-                       mm_util_warn("#YUV Height value(%d) must be even at least# ", *crop_dest_height);
-                       *crop_dest_height = ((*crop_dest_height+1)>>1)<<1;
-                       mm_util_debug("Image isplay is suceeded when YUV crop height value %d", *crop_dest_height);
+               if ((crop_dest_height % 2) != 0) { /* height value must be also even when crop yuv image */
+                       mm_util_warn("#YUV Height value(%d) must be even at least# ", crop_dest_height);
+                       res_h = ((crop_dest_height+1)>>1)<<1;
+                       mm_util_debug("Image isplay is suceeded when YUV crop height value %d", res_h);
                }
 
-               ret = __mm_util_crop_yuv420(src, src_width, src_height, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
+               MMUTIL_SAFE_FREE(dst_buffer);
+               __mm_util_get_crop_image_size(src_format, res_w, res_h, &dst_buf_size);
+               mm_util_retvm_if(dst_buf_size == 0, MM_UTIL_ERROR_INVALID_OPERATION, "fail to get dst_buf_size");
+
+               dst_buffer = calloc(1, dst_buf_size);
+               mm_util_retvm_if(dst_buffer == NULL, MM_UTIL_ERROR_OUT_OF_MEMORY, "memory alloc fail");
+
+               ret = __mm_util_crop_yuv420(src, src_width, src_height, crop_start_x, crop_start_y, res_w, res_h, dst_buffer);
                break;
                }
        default:
                mm_util_debug("Not supported format");
+               MMUTIL_SAFE_FREE(dst_buffer);
                ret = MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        }
 
+       *result_buf_size = (size_t)dst_buf_size;
+       *dst = dst_buffer;
+       *result_buf_width = res_w;
+       *result_buf_height = res_h;
+
        mm_util_fleave();
 
        return ret;
@@ -1531,7 +1580,6 @@ int mm_util_get_image_size(mm_util_color_format_e format, unsigned int width, un
                *imgsize = size;
                break;
 
-
        case MM_UTIL_COLOR_NV12:
        case MM_UTIL_COLOR_NV12_TILED:
        case MM_UTIL_COLOR_NV21:
index 059a34b..fed08fe 100755 (executable)
@@ -124,6 +124,10 @@ int main(int argc, char *argv[])
        unsigned int start_x = (unsigned int)atoi(argv[11]);
        unsigned int start_y = (unsigned int)atoi(argv[12]);
        char output_file[40] = {};
+       unsigned int res_w = 0;
+       unsigned int res_h = 0;
+       unsigned char *res_buffer = NULL;
+       size_t res_buffer_size = 0;
 
        /* async mode */
        mm_util_color_image_h orig_image = NULL;
@@ -193,8 +197,11 @@ int main(int argc, char *argv[])
                        ret = mm_util_resize_image(src, src_width, src_height, src_format, dst, &dst_width, &dst_height);
                else if (strcmp(command, "rotate") == 0)
                        ret = mm_util_rotate_image(src, src_width, src_height, src_format, dst, &dst_width, &dst_height, rotation);
-               else if (strcmp(command, "crop") == 0)
-                       ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, &dst_width, &dst_height, dst);
+               else if (strcmp(command, "crop") == 0) {
+                       ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, dst_width, dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
+                       IMGP_FREE(dst);
+                       dst = res_buffer;
+               }
 
                if (ret == MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "Success - %s\n", command);
index 87e9cc9..78c0461 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       libmm-utility
 Summary:    Multimedia Framework Utility Library
-Version:    0.1.13
+Version:    0.1.14
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0