Refactoring. Integrate redundant 'get_image_size' functions 19/276619/6
authorjiyong.min <jiyong.min@samsung.com>
Wed, 22 Jun 2022 03:55:24 +0000 (12:55 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Mon, 27 Jun 2022 06:50:18 +0000 (15:50 +0900)
Change-Id: I9a262e261436e265c023060cec8433935031ea94

imgp/mm_util_imgp.c

index 2bd90bc..465487b 100644 (file)
@@ -88,7 +88,7 @@ static gboolean __mm_gst_can_rotate_format(mm_util_color_format_e color_format)
        return _bool;
 }
 
-static int __mm_util_get_crop_image_size(mm_util_color_format_e format, unsigned int width, unsigned int height, size_t *imgsize)
+static int __mm_util_get_image_size(mm_util_color_format_e format, unsigned int width, unsigned int height, bool is_crop, size_t *imgsize)
 {
        unsigned char x_chroma_shift = 0;
        unsigned char y_chroma_shift = 0;
@@ -98,9 +98,8 @@ static int __mm_util_get_crop_image_size(mm_util_color_format_e format, unsigned
        mm_util_fenter();
 
        mm_util_retvm_if(imgsize == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid imgsize");
-       mm_util_retvm_if(__check_valid_picture_size(width, height) != MM_UTIL_ERROR_NONE, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid width and height");
-
-       *imgsize = 0;
+       mm_util_retvm_if(IS_VALID_COLOR(format) == FALSE, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid format(%u)", format);
+       mm_util_retvm_if(__check_valid_picture_size(width, height) != MM_UTIL_ERROR_NONE, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid width(%u) or height(%u)", width, height);
 
        switch (format) {
        case MM_UTIL_COLOR_I420:
@@ -127,13 +126,13 @@ static int __mm_util_get_crop_image_size(mm_util_color_format_e format, unsigned
                break;
 
        case MM_UTIL_COLOR_RGB16:
-               stride = width * 2;
+               stride = 2 * (is_crop ? width : MM_UTIL_ROUND_UP_4(width));
                size = stride * height;
                *imgsize = size;
                break;
 
        case MM_UTIL_COLOR_RGB24:
-               stride = width * 3;
+               stride = 3 * (is_crop ? width : MM_UTIL_ROUND_UP_4(width));
                size = stride * height;
                *imgsize = size;
                break;
@@ -149,6 +148,7 @@ static int __mm_util_get_crop_image_size(mm_util_color_format_e format, unsigned
 
        case MM_UTIL_COLOR_NV12:
        case MM_UTIL_COLOR_NV12_TILED:
+       case MM_UTIL_COLOR_NV21:
                x_chroma_shift = 1;
                y_chroma_shift = 1;
                stride = MM_UTIL_ROUND_UP_4(width);
@@ -162,11 +162,11 @@ static int __mm_util_get_crop_image_size(mm_util_color_format_e format, unsigned
                break;
 
        default:
-               mm_util_error("Not supported format [%d]", format);
+               mm_util_error("Not supported format [%u]", format);
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        }
 
-       mm_util_debug("format: %d, *imgsize: %zu", format, *imgsize);
+       mm_util_debug("format: %u, *imgsize: %zu", format, *imgsize);
 
        return MM_UTIL_ERROR_NONE;
 }
@@ -593,7 +593,7 @@ int mm_util_crop_image(mm_util_image_h src, unsigned int start_x, unsigned int s
                }
        }
 
-       __mm_util_get_crop_image_size(_src->color, _width, _height, &_buffer_size);
+       __mm_util_get_image_size(_src->color, _width, _height, true, &_buffer_size);
        mm_util_retvm_if(!_buffer_size, MM_UTIL_ERROR_INVALID_OPERATION, "fail to get dst_buf_size");
 
        _buffer = g_malloc0(_buffer_size);
@@ -635,86 +635,5 @@ int mm_util_crop_image(mm_util_image_h src, unsigned int start_x, unsigned int s
 
 int mm_util_get_image_size(mm_util_color_format_e format, unsigned int width, unsigned int height, size_t *imgsize)
 {
-       unsigned char x_chroma_shift = 0;
-       unsigned char y_chroma_shift = 0;
-       unsigned int w2, h2, stride, stride2;
-       size_t size, size2;
-
-       mm_util_fenter();
-
-       mm_util_retvm_if((imgsize == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid imgsize");
-       mm_util_retvm_if((IS_VALID_COLOR(format) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid format [%d]", format);
-       mm_util_retvm_if((__check_valid_picture_size(width, height) != MM_UTIL_ERROR_NONE), MM_UTIL_ERROR_INVALID_PARAMETER, "image width & height is too big");
-
-       *imgsize = 0;
-
-       switch (format) {
-       case MM_UTIL_COLOR_I420:
-       case MM_UTIL_COLOR_YUV420:
-               x_chroma_shift = 1;
-               y_chroma_shift = 1;
-               stride = MM_UTIL_ROUND_UP_4(width);
-               h2 = ROUND_UP_X(height, x_chroma_shift);
-               size = stride * h2;
-               w2 = DIV_ROUND_UP_X(width, x_chroma_shift);
-               stride2 = MM_UTIL_ROUND_UP_4(w2);
-               h2 = DIV_ROUND_UP_X(height, y_chroma_shift);
-               size2 = stride2 * h2;
-               *imgsize = size + 2 * size2;
-               break;
-       case MM_UTIL_COLOR_YUV422:
-       case MM_UTIL_COLOR_YUYV:
-       case MM_UTIL_COLOR_UYVY:
-       case MM_UTIL_COLOR_NV16:
-       case MM_UTIL_COLOR_NV61:
-               stride = MM_UTIL_ROUND_UP_4(width) * 2;
-               size = stride * height;
-               *imgsize = size;
-               break;
-
-       case MM_UTIL_COLOR_RGB16:
-               stride = MM_UTIL_ROUND_UP_4(width) * 2;
-               size = stride * height;
-               *imgsize = size;
-               break;
-
-       case MM_UTIL_COLOR_RGB24:
-               stride = MM_UTIL_ROUND_UP_4(width) * 3;
-               size = stride * height;
-               *imgsize = size;
-               break;
-
-       case MM_UTIL_COLOR_ARGB:
-       case MM_UTIL_COLOR_BGRA:
-       case MM_UTIL_COLOR_RGBA:
-       case MM_UTIL_COLOR_BGRX:
-               stride = width * 4;
-               size = stride * height;
-               *imgsize = size;
-               break;
-
-       case MM_UTIL_COLOR_NV12:
-       case MM_UTIL_COLOR_NV12_TILED:
-       case MM_UTIL_COLOR_NV21:
-               x_chroma_shift = 1;
-               y_chroma_shift = 1;
-               stride = MM_UTIL_ROUND_UP_4(width);
-               h2 = ROUND_UP_X(height, y_chroma_shift);
-               size = stride * h2;
-               w2 = 2 * DIV_ROUND_UP_X(width, x_chroma_shift);
-               stride2 = MM_UTIL_ROUND_UP_4(w2);
-               h2 = DIV_ROUND_UP_X(height, y_chroma_shift);
-               size2 = stride2 * h2;
-               *imgsize = size + size2;
-               break;
-
-       default:
-               mm_util_error("Not supported format [%d]", format);
-               return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
-       }
-
-       mm_util_debug("format: %u, *imgsize: %zu", format, *imgsize);
-
-       return MM_UTIL_ERROR_NONE;
+       return __mm_util_get_image_size(format, width, height, false, imgsize);
 }
-