*/
-#include <image_util.h>
-#include <image_util_internal.h>
#include <dcm_image_codec_debug.h>
#include <dcm_image_codec.h>
+#include <mm_util_magick.h>
#define OPT_IMAGE_WIDTH 1280
#define OPT_IMAGE_HEIGHT 720
ROT_270 = 8
} dcm_exif_orientation_e;
-static void __dcm_get_optimized_wh(unsigned int src_width, unsigned int src_height, unsigned int *calc_width, unsigned int *calc_height)
+static bool __dcm_get_optimized_wh(unsigned int src_width, unsigned int src_height, unsigned int *calc_width, unsigned int *calc_height)
{
*calc_width = 0;
*calc_height = 0;
dcm_debug("Original image is smaller than target image");
*calc_width = src_width;
*calc_height = src_height;
- return;
+ return FALSE;
}
if (src_width > src_height) {
*calc_width = (int)(src_width * (((double) OPT_IMAGE_HEIGHT) / ((double) src_height)));
*calc_height = OPT_IMAGE_HEIGHT;
}
-}
-
-static int __dcm_rotate_image(const unsigned char *source, const dcm_image_format_e format, dcm_exif_orientation_e orientation, unsigned char **image_buffer, unsigned long long *size, unsigned int *buff_width, unsigned int *buff_height)
-{
- int ret = IMAGE_UTIL_ERROR_NONE;
- image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
- image_util_rotation_e rotate = IMAGE_UTIL_ROTATION_NONE;
- unsigned char *rotated_buffer = NULL;
- unsigned int rotated_buffer_size = 0;
- int rotated_width = 0, rotated_height = 0;
-
- DCM_CHECK_VAL(source, MS_MEDIA_ERR_INVALID_PARAMETER);
-
- if (format == DCM_IMAGE_FORMAT_I420)
- colorspace = IMAGE_UTIL_COLORSPACE_I420;
- else if (format == DCM_IMAGE_FORMAT_RGB)
- colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
- else if (format == DCM_IMAGE_FORMAT_RGBA)
- colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
- else
- return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
-
- /* Get rotate angle enum */
- if (orientation == ROT_180)
- rotate = IMAGE_UTIL_ROTATION_180;
- else if (orientation == ROT_90)
- rotate = IMAGE_UTIL_ROTATION_90;
- else if (orientation == ROT_270)
- rotate = IMAGE_UTIL_ROTATION_270;
- else if (orientation == HFLIP)
- rotate = IMAGE_UTIL_ROTATION_FLIP_HORZ;
- else if (orientation == VFLIP)
- rotate = IMAGE_UTIL_ROTATION_FLIP_VERT;
- else
- rotate = IMAGE_UTIL_ROTATION_NONE;
-
- dcm_debug("orientation: %d, rotate: %d", orientation, rotate);
- if (rotate == IMAGE_UTIL_ROTATION_90 || rotate == IMAGE_UTIL_ROTATION_270) {
- rotated_width = *buff_height;
- rotated_height = *buff_width;
- } else {
- rotated_width = *buff_width;
- rotated_height = *buff_height;
- }
-
- /* Calculate the rotated buffer size */
- ret = image_util_calculate_buffer_size(rotated_width, rotated_height, colorspace, &rotated_buffer_size);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Failed to calculate buffer size! err: %d", ret);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- /* Allocate rotated buffer */
- if (rotated_buffer_size <= 0) {
- dcm_error("Invalid rotated buffer size!");
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- dcm_debug("rotate buffer size: %u", rotated_buffer_size);
- rotated_buffer = (unsigned char *) g_malloc0(rotated_buffer_size);
- if (rotated_buffer == NULL) {
- dcm_error("rotated_buffer is NULL!");
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- *size = rotated_buffer_size;
-
- /* Rotate input buffer */
- ret = image_util_rotate(rotated_buffer, &rotated_width, &rotated_height, rotate, source, *buff_width, *buff_height, colorspace);
-
- if (ret != IMAGE_UTIL_ERROR_NONE || rotated_buffer == NULL) {
- dcm_error("Failed to rotate image buffer! err: %d", ret);
- DCM_SAFE_FREE(rotated_buffer);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- /* Decoded buffer size is set to rotated buffer size to match buffer */
- dcm_debug("After rotation: [width: %d] [height: %d]", rotated_width, rotated_height);
- *buff_width = rotated_width;
- *buff_height = rotated_height;
-
- /* Allocated data should be freed when scanning is finished for this rotated image */
- *image_buffer = rotated_buffer;
- dcm_warn("rotated decode buffer: %p", *image_buffer);
-
- return MS_MEDIA_ERR_NONE;
+ return TRUE;
}
-static int __dcm_rotate_rgb(unsigned char *source, const unsigned long long *size, int format, unsigned int *ori_width, unsigned int *ori_height)
-{
- unsigned int dpp = 0; /* data per pixel */
- unsigned int x = 0, y = 0;
- unsigned int i = 0;
- unsigned int width = 0, height = 0;
- unsigned char *temp_buf = NULL;
- size_t _size = (size_t)*size;
-
- if (format == DCM_IMAGE_FORMAT_RGBA) {
- dpp = 4;
- } else if (format == DCM_IMAGE_FORMAT_RGB) {
- dpp = 3;
- } else {
- dcm_error("Invalid parameter");
- return MS_MEDIA_ERR_INVALID_PARAMETER;
- }
-
- temp_buf = (unsigned char*)g_malloc0(_size);
- if (temp_buf == NULL) {
- dcm_error("Failed to allocate memory");
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
-
- width = *ori_width;
- height = *ori_height;
-
- /* rotate image to 90 degree clockwise */
- for (y = 0; y < height; y++) {
- for (x = 0; x < width; x++) {
- for (i = 0; i < dpp; i++)
- temp_buf[(x * height + (height - y - 1)) * dpp + i] = source[(y * width + x) * dpp + i];
- }
- }
-
- /* copy image from temp buffer to original buffer */
- memcpy(source, temp_buf, _size);
- DCM_SAFE_FREE(temp_buf);
-
- /* swap width & height due to rotate 90 degree */
- *ori_width = height;
- *ori_height = width;
-
- return MS_MEDIA_ERR_NONE;
-}
-
-int dcm_decode_image(const char *file_path, const dcm_image_format_e format,
- const char* mimne_type, const int orientation, const gboolean resize,
- unsigned char **image_buffer, unsigned long long *size,
+int dcm_decode_image(const char *file_path, const int orientation, const gboolean resize, unsigned char **image_buffer, size_t *size,
unsigned int *buff_width, unsigned int *buff_height)
{
- int ret = IMAGE_UTIL_ERROR_NONE;
- image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
- unsigned char *decode_buffer = NULL;
- unsigned int decode_width = 0, decode_height = 0;
- unsigned char *resize_buffer = NULL;
- unsigned int buffer_size = 0;
- image_util_decode_h handle = NULL;
+ int ret = MS_MEDIA_ERR_NONE;
+ unsigned int width = 0;
+ unsigned int height = 0;
+ unsigned int resized_width = 0;
+ unsigned int resized_height = 0;
+ mm_util_img_codec_type type = IMG_CODEC_UNKNOWN_TYPE;
+ mm_util_magick_rotate_type angle = MM_UTIL_ROTATE_NUM;
+ mm_util_image_h resize_dst_handle = NULL;
+ mm_util_image_h dst_handle = NULL;
+ mm_util_magick_format format = MM_UTIL_IMG_FMT_NUM;
dcm_debug_fenter();
DCM_CHECK_VAL(file_path, MS_MEDIA_ERR_INVALID_PARAMETER);
- ret = image_util_decode_create(&handle);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_create ret : %d", ret);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- ret = image_util_decode_set_input_path(handle, file_path);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_set_input_path ret : %d", ret);
- image_util_decode_destroy(handle);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- if (strcmp(mimne_type, MIME_TYPE_JPEG) == 0) {
- if (format == DCM_IMAGE_FORMAT_I420) {
- colorspace = IMAGE_UTIL_COLORSPACE_I420;
- } else if (format == DCM_IMAGE_FORMAT_RGB) {
- colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
- } else if (format == DCM_IMAGE_FORMAT_RGBA) {
- colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
- } else {
- image_util_decode_destroy(handle);
- return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
- }
- ret = image_util_decode_set_colorspace(handle, colorspace);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_set_colorspace ret : %d", ret);
- image_util_decode_destroy(handle);
- return MS_MEDIA_ERR_INTERNAL;
- }
- }
-
- ret = image_util_decode_set_output_buffer(handle, &decode_buffer);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_set_output_buffer ret : %d", ret);
- image_util_decode_destroy(handle);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- ret = image_util_decode_run(handle, (unsigned long *)&decode_width, (unsigned long *)&decode_height, size);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_run ret : %d", ret);
- image_util_decode_destroy(handle);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- ret = image_util_decode_destroy(handle);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Error image_util_decode_destroy ret : %d", ret);
+ if (orientation == NOT_AVAILABLE || orientation == NORMAL)
+ angle = MM_UTIL_ROTATE_0;
+ else if (orientation == ROT_90)
+ angle = MM_UTIL_ROTATE_90;
+ else if (orientation == ROT_180)
+ angle = MM_UTIL_ROTATE_180;
+ else if (orientation == ROT_270)
+ angle = MM_UTIL_ROTATE_270;
+ else if (orientation == HFLIP)
+ angle = MM_UTIL_ROTATE_FLIP_HORZ;
+ else if (orientation == VFLIP)
+ angle = MM_UTIL_ROTATE_FLIP_VERT;
+ else {
+ dcm_error("Not supported angle [%d]", orientation);
return MS_MEDIA_ERR_INTERNAL;
}
-
- /* Get the optimized width/height to enhance performance for big size image */
if (resize) {
- __dcm_get_optimized_wh(decode_width, decode_height, buff_width, buff_height);
- } else {
- *buff_width = decode_width;
- *buff_height = decode_height;
- }
-
- /* Resize the big size image to enhance performance for big size image */
- if ((decode_width != *buff_width) || (decode_height != *buff_height)) {
- ret = image_util_calculate_buffer_size(*buff_width, *buff_height, colorspace, &buffer_size);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Failed to calculate image buffer size! err: %d", ret);
- return MS_MEDIA_ERR_INTERNAL;
- }
+ /* Get the optimized width/height to enhance performance for big size image */
+ ret = mm_util_extract_image_info(file_path, &type, &width, &height);
+ if (ret != MM_UTIL_ERROR_NONE)
+ dcm_error("Failed to get image info!err: %d", ret);
+
+ if (__dcm_get_optimized_wh(width, height, &resized_width, &resized_height)) {
+ /* Resize the big size image to enhance performance for big size image */
+ ret = mm_util_resize_P_B(file_path, resized_width, resized_height, MM_UTIL_IMG_FMT_RGBA8888, &resize_dst_handle);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ dcm_error("Failed to resize image!err: %d", ret);
+ ret = MS_MEDIA_ERR_INTERNAL;
+ goto ERROR;
+ }
- *size = buffer_size;
- resize_buffer = (unsigned char *)g_malloc0(sizeof(unsigned char) * (buffer_size));
- if (resize_buffer != NULL) {
- ret = image_util_resize(resize_buffer, (int *)buff_width, (int *)buff_height, decode_buffer, decode_width, decode_height, colorspace);
- DCM_SAFE_FREE(decode_buffer);
- if (ret != IMAGE_UTIL_ERROR_NONE) {
- dcm_error("Failed to image_util_resize");
- DCM_SAFE_FREE(resize_buffer);
- return MS_MEDIA_ERR_INTERNAL;
+ ret = mm_util_rotate_B_B(resize_dst_handle, angle, &dst_handle);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ dcm_error("Failed to rotate image!err: %d", ret);
+ ret = MS_MEDIA_ERR_INTERNAL;
+ goto ERROR;
}
} else {
- dcm_error("Failed to allocation");
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
+ ret = mm_util_rotate_P_B(file_path, angle, MM_UTIL_IMG_FMT_RGBA8888, &dst_handle);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ dcm_error("Failed to rotate image buffer! err: %d", ret);
+ ret = MS_MEDIA_ERR_INTERNAL;
+ goto ERROR;
+ }
}
} else {
- resize_buffer = decode_buffer;
- }
-
- /* Rotate the resized buffer according to orientation */
- if (orientation == NOT_AVAILABLE || orientation == NORMAL) {
- *image_buffer = resize_buffer;
- } else {
- if ((format == DCM_IMAGE_FORMAT_RGBA) || (format == DCM_IMAGE_FORMAT_RGB)) {
- ret = __dcm_rotate_rgb(resize_buffer, size, format, buff_width, buff_height);
- *image_buffer = resize_buffer;
- } else {
- ret = __dcm_rotate_image(resize_buffer, format, orientation, image_buffer, size, buff_width, buff_height);
- DCM_SAFE_FREE(resize_buffer);
- }
- if (ret != MS_MEDIA_ERR_NONE) {
+ ret = mm_util_rotate_P_B(file_path, angle, MM_UTIL_IMG_FMT_RGBA8888, &dst_handle);
+ if (ret != MM_UTIL_ERROR_NONE) {
dcm_error("Failed to rotate image buffer! err: %d", ret);
- return MS_MEDIA_ERR_INTERNAL;
+ ret = MS_MEDIA_ERR_INTERNAL;
+ goto ERROR;
}
}
+ mm_util_get_image(dst_handle, image_buffer, buff_width, buff_height, size, &format);
+
+ERROR:
+ if (resize_dst_handle != NULL)
+ mm_util_destroy_handle(resize_dst_handle);
+
+ if (dst_handle != NULL)
+ mm_util_destroy_handle(dst_handle);
+
dcm_debug_fleave();
- return MS_MEDIA_ERR_NONE;
+ return ret;
}