Check input parameter
[platform/core/api/image-util.git] / src / image_util.c
index 74ddf4d..510f251 100755 (executable)
 * limitations under the License.
 */
 
-#include <dlog.h>
-
+#include <gmodule.h>
+#include <inttypes.h>
 #include <mm_util_imgp.h>
-#include <mm_util_jpeg.h>
-#include <mm_util_imgcv.h>
+#include <mm_util_common.h>
+
 #include <image_util.h>
 #include <image_util_private.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#define IMAGE_UTIL_STRING_VALID(str)   \
-       ((str != NULL && strlen(str) > 0) ? true : false)
-
-#define IMAGE_UTIL_SAFE_FREE(src)      { if (src) {free(src); src = NULL; } }
-
-
-static int _convert_colorspace_tbl[] = {
-       MM_UTIL_IMG_FMT_YUV420,         /* IMAGE_UTIL_COLORSPACE_YUV420 */
-       MM_UTIL_IMG_FMT_YUV422,         /* IMAGE_UTIL_COLORSPACE_YUV422 */
-       MM_UTIL_IMG_FMT_I420,                   /* IMAGE_UTIL_COLORSPACE_I420 */
-       MM_UTIL_IMG_FMT_NV12,                   /* IMAGE_UTIL_COLORSPACE_NV12 */
-       MM_UTIL_IMG_FMT_UYVY,                   /* IMAGE_UTIL_COLORSPACE_UYVY */
-       MM_UTIL_IMG_FMT_YUYV,                   /* IMAGE_UTIL_COLORSPACE_YUYV */
-       MM_UTIL_IMG_FMT_RGB565,         /* IMAGE_UTIL_COLORSPACE_RGB565 */
-       MM_UTIL_IMG_FMT_RGB888,         /* IMAGE_UTIL_COLORSPACE_RGB888 */
-       MM_UTIL_IMG_FMT_ARGB8888,               /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
-       MM_UTIL_IMG_FMT_BGRA8888,               /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
-       MM_UTIL_IMG_FMT_RGBA8888,               /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
-       MM_UTIL_IMG_FMT_BGRX8888,               /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
-       MM_UTIL_JPEG_FMT_NV21,                  /* IMAGE_UTIL_COLORSPACE_NV12 */
-       MM_UTIL_JPEG_FMT_NV16,                  /* IMAGE_UTIL_COLORSPACE_NV16 */
-       MM_UTIL_JPEG_FMT_NV61,                  /* IMAGE_UTIL_COLORSPACE_NV61 */
-};
 
-static int _convert_encode_colorspace_tbl[] = {
-       MM_UTIL_JPEG_FMT_YUV420                                         ,       /* IMAGE_UTIL_COLORSPACE_YUV420 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_YUV422 */
-       MM_UTIL_JPEG_FMT_YUV420                                         ,       /* IMAGE_UTIL_COLORSPACE_I420 */
-       MM_UTIL_JPEG_FMT_NV12                                           ,       /* IMAGE_UTIL_COLORSPACE_NV12 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_UYVY */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_YUYV */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_RGB565 */
-       MM_UTIL_JPEG_FMT_RGB888                                         ,       /* IMAGE_UTIL_COLORSPACE_RGB888 */
-       MM_UTIL_JPEG_FMT_ARGB8888                                       ,       /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
-       MM_UTIL_JPEG_FMT_BGRA8888                                       ,       /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
-       MM_UTIL_JPEG_FMT_RGBA8888                                       ,       /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_NV21 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_NV16 */
-       -1                                                                                      ,       /* IMAGE_UTIL_COLORSPACE_NV61 */
+typedef struct {
+       mm_util_color_format_e image_format;
+       media_format_mimetype_e mimetype;
+       const char *mimetype_name;
+} image_format_mimetype_pair_s;
+
+static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_COLOR_NUM] = {
+       { MM_UTIL_COLOR_YUV420, MEDIA_FORMAT_YV12,              "MEDIA_FORMAT_YV12" },
+       { MM_UTIL_COLOR_YUV422, MEDIA_FORMAT_422P,              "MEDIA_FORMAT_422P" },
+       { MM_UTIL_COLOR_I420,           MEDIA_FORMAT_I420,              "MEDIA_FORMAT_I420" },
+       { MM_UTIL_COLOR_NV12,           MEDIA_FORMAT_NV12,                                              "Not support" },
+       { MM_UTIL_COLOR_UYVY,           MEDIA_FORMAT_UYVY,              "MEDIA_FORMAT_UYVY" },
+       { MM_UTIL_COLOR_YUYV,           MEDIA_FORMAT_YUYV,              "MEDIA_FORMAT_YUYV" },
+       { MM_UTIL_COLOR_RGB16,  MEDIA_FORMAT_RGB565,    "MEDIA_FORMAT_RGB565" },
+       { MM_UTIL_COLOR_RGB24,  MEDIA_FORMAT_RGB888,    "MEDIA_FORMAT_RGB888" },
+       { MM_UTIL_COLOR_ARGB,   MEDIA_FORMAT_ARGB,              "MEDIA_FORMAT_ARGB" },
+       { MM_UTIL_COLOR_BGRA,   MEDIA_FORMAT_BGRA,              "MEDIA_FORMAT_BGRA" },
+       { MM_UTIL_COLOR_RGBA,   MEDIA_FORMAT_RGBA,              "MEDIA_FORMAT_RGBA" },
+       { MM_UTIL_COLOR_BGRX,   -1,                                             "Not support" },
+       { MM_UTIL_COLOR_NV12_TILED,     MEDIA_FORMAT_NV12T,     "MEDIA_FORMAT_NV12T" },
+       { MM_UTIL_COLOR_NV16,           MEDIA_FORMAT_NV16,              "MEDIA_FORMAT_NV16" },
+       { MM_UTIL_COLOR_NV61,           -1,                                             "Not support" }
 };
 
+static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e format)
+{
+       unsigned int i = 0;
+       media_format_mimetype_e mimetype = -1;
+
+       for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
+               if (image_format_mimetype_table[i].image_format == format) {
+                       mimetype = image_format_mimetype_table[i].mimetype;
+                       break;
+               }
+       }
 
-static int _convert_decode_scale_tbl[] = {
-       MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1,
-       MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2,
-       MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4,
-       MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8,
-};
+       image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name);
 
-static int _convert_image_util_error_code(const char *func, int code)
+       return mimetype;
+}
+
+static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e mimetype)
 {
-       int ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       char *errorstr = NULL;
-       switch (code) {
-       case MM_UTIL_ERROR_NONE:
-               ret = IMAGE_UTIL_ERROR_NONE;
-               errorstr = strdup("ERROR_NONE");
-               break;
-       case MM_UTIL_ERROR_NO_SUCH_FILE:
-               ret = IMAGE_UTIL_ERROR_NO_SUCH_FILE;
-               errorstr = strdup("NO_SUCH_FILE");
-               break;
-       case MM_UTIL_ERROR_INVALID_PARAMETER:
-               ret = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-               errorstr = strdup("INVALID_PARAMETER");
-               break;
-       case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
-               ret = IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
-               errorstr = strdup("NOT_SUPPORTED_FORMAT");
-               break;
-       case MM_UTIL_ERROR_OUT_OF_MEMORY:
-               ret = IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
-               errorstr = strdup("OUT_OF_MEMORY");
-               break;
-       case MM_UTIL_ERROR_INVALID_OPERATION:
-       default:
-               ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
-               errorstr = strdup("INVALID_OPERATION");
+       unsigned int i = 0;
+       mm_util_color_format_e format = -1;
 
+       for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
+               if (image_format_mimetype_table[i].mimetype == mimetype) {
+                       format = image_format_mimetype_table[i].image_format;
+                       break;
+               }
        }
 
-       image_util_debug("[%s] %s(0x%08x)", func, errorstr, ret);
-       IMAGE_UTIL_SAFE_FREE(errorstr);
-       return ret;
+       image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, format);
+
+       return format;
 }
 
-static image_util_error_e _image_util_error_convert(int error)
+static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *new_fmt)
 {
-       switch (error) {
-       case MM_UTIL_ERROR_NONE:
-               image_util_debug("Error None");
-               return IMAGE_UTIL_ERROR_NONE;
-       case MM_UTIL_ERROR_INVALID_PARAMETER:
-               image_util_error("INVALID_PARAMETER(0x%08x)", IMAGE_UTIL_ERROR_INVALID_PARAMETER);
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-       case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
-               image_util_error("NOT_SUPPORTED_FORMAT(0x%08x)", IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT);
-               return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
-       case MM_UTIL_ERROR_OUT_OF_MEMORY:
-               image_util_error("OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
-               return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
-               break;
-       case MM_UTIL_ERROR_INVALID_OPERATION:
-       default:
-               image_util_error("INVALID_OPERATION(0x%08x)", error);
+       int err = MEDIA_FORMAT_ERROR_NONE;
+
+       image_util_retvm_if((new_fmt == NULL) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
+
+       err = media_format_create(new_fmt);
+       image_util_retvm_if((err != MEDIA_FORMAT_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_format_make_writable failed (%d)", err);
+
+       err = media_format_set_video_mime(*new_fmt, mimetype);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               image_util_error("media_format_set_video_mime failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_format_set_video_width(*new_fmt, width);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               image_util_error("media_format_set_video_width failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_format_set_video_height(*new_fmt, height);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               image_util_error("media_format_set_video_height failed (%d)", err);
                return IMAGE_UTIL_ERROR_INVALID_OPERATION;
        }
+
+       return IMAGE_UTIL_ERROR_NONE;
+}
+
+static void __mm_util_destroy_color_image(mm_image_info_s *image)
+{
+       if (image == NULL) {
+               image_util_error("[ERROR] - image");
+               return;
+       }
+
+       IMAGE_UTIL_SAFE_FREE(image->data);
+       IMAGE_UTIL_SAFE_FREE(image);
 }
 
-static void _image_util_transform_completed_cb(media_packet_h *dst, int error, void *user_data)
+static int __mm_util_create_color_image(mm_image_info_s **image, unsigned long width, unsigned long height, mm_util_color_format_e color, void *data, size_t size)
 {
-       int error_value = IMAGE_UTIL_ERROR_NONE;
-       image_util_cb_s *_util_cb = (image_util_cb_s *)user_data;
+       int ret = IMAGE_UTIL_ERROR_NONE;
+       mm_image_info_s *_color_image = NULL;
+
+       image_util_retvm_if((image == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle");
+       image_util_retvm_if((color >= MM_UTIL_COLOR_NUM), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid color");
+       image_util_retvm_if((data == NULL || size == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid data");
+
+       _color_image = (mm_image_info_s *)calloc(1, sizeof(mm_image_info_s));
+       image_util_retvm_if((_color_image == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed");
 
-       if ((_util_cb != NULL) && (_util_cb->image_processing_completed_cb != NULL)) {
-               error_value = _image_util_error_convert(error);
-               _util_cb->image_processing_completed_cb(dst, error_value, _util_cb->user_data);
+       _color_image->data = calloc(1, size);
+       if (_color_image->data == NULL) {
+               image_util_error("Memory allocation failed");
+               __mm_util_destroy_color_image(_color_image);
+               *image = NULL;
+               return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
        }
 
-       return;
+       memcpy(_color_image->data, data, size);
+
+       _color_image->size = size;
+       _color_image->width = width;
+       _color_image->height = height;
+       _color_image->color = color;
+
+       image_util_debug("w [%lu], h [%lu], color [%u], size [%zu], data [%p]", _color_image->width, _color_image->height, _color_image->color, _color_image->size, _color_image->data);
+
+       *image = _color_image;
+
+       return ret;
 }
 
-static int _image_util_create_transform_handle(transformation_s *handle)
+static void __mm_destroy_temp_buffer(unsigned char *buffer[])
 {
-       int err = MM_UTIL_ERROR_NONE;
-       MMHandleType image_h;
+       int i = 0;
 
-       err = mm_util_create(&image_h);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - mm_util_create");
-               return _convert_image_util_error_code(__func__, err);
+       for (i = 0; i < 4; i++)
+               IMAGE_UTIL_SAFE_FREE(buffer[i]);
+}
+
+static int __mm_util_transform(transformation_s *handle)
+{
+       int ret = IMAGE_UTIL_ERROR_NONE;
+       unsigned char *dst_buf[4] = {NULL,};
+       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;
+       unsigned char *res_buffer_conv = NULL;
+       unsigned char *res_buffer_rotate = NULL;
+       size_t res_buffer_size = 0;
+
+       image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
+
+       image_util_debug("src: %p, dst: %p", handle->src, handle->dst);
+
+       dst_buf[src_index] = calloc(1, handle->src->size);
+       src_width = handle->src->width;
+       src_height = handle->src->height;
+       src_format = handle->src->color;
+       if (dst_buf[src_index] == NULL) {
+               image_util_error("[multi func] memory allocation error");
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+       memcpy(dst_buf[src_index], handle->src->data, handle->src->size);
+
+       if (handle->set_crop) {
+               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);
+                       image_util_error("mm_util_crop_image failed");
+                       return _image_error_capi(ret);
+               }
+
+               dst_buf[dst_index] = res_buffer;
+               src_index = dst_index;
+               src_width = res_w;
+               src_height = res_h;
+       } else if (handle->set_resize) {
+               dst_index++;
+
+               ret = mm_util_resize_image(dst_buf[src_index], src_width, src_height, src_format, 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);
+                       image_util_error("mm_util_resize_image failed");
+                       return _image_error_capi(ret);
+               }
+
+               dst_buf[dst_index] = res_buffer;
+               src_index = dst_index;
+               src_width = res_w;
+               src_height = res_h;
+       }
+
+       if (handle->set_convert) {
+               dst_index++;
+
+               ret = mm_util_convert_colorspace(dst_buf[src_index], src_width, src_height, src_format, handle->dst_format, &res_buffer_conv, &res_w, &res_h, &res_buffer_size);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       __mm_destroy_temp_buffer(dst_buf);
+                       image_util_error("mm_util_convert_colorspace failed");
+                       return _image_error_capi(ret);
+               }
+
+               dst_buf[dst_index] = res_buffer_conv;
+               src_index = dst_index;
+               src_format = handle->dst_format;
+               src_width = res_w;
+               src_height = res_h;
+       }
+
+       if (handle->set_rotate) {
+               dst_index++;
+
+               ret = mm_util_rotate_image(dst_buf[src_index], src_width, src_height, src_format, handle->rotation, &res_buffer_rotate, &res_w, &res_h, &res_buffer_size);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       __mm_destroy_temp_buffer(dst_buf);
+                       image_util_error("mm_util_rotate_image failed");
+                       return _image_error_capi(ret);
+               }
+
+               dst_buf[dst_index] = res_buffer_rotate;
+               src_index = dst_index;
+               src_width = res_w;
+               src_height = res_h;
+       }
+
+       if (dst_buf[dst_index] != NULL && res_buffer_size != 0) {
+               ret = __mm_util_create_color_image(&(handle->dst), (unsigned long)src_width, (unsigned long)src_height, src_format, (void *)dst_buf[dst_index], res_buffer_size);
+               if (ret != IMAGE_UTIL_ERROR_NONE)
+                       image_util_error("__mm_util_create_color_image failed");
+       } else {
+               image_util_error("invalid result %p %zu", dst_buf[dst_index], res_buffer_size);
+               ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
        }
+       __mm_destroy_temp_buffer(dst_buf);
 
-       handle->image_h = image_h;
+       image_util_error("mm_util_processing was finished");
 
-       return _convert_image_util_error_code(__func__, err);
+       return ret;
 }
 
-static bool _image_util_check_resolution(int width, int height)
+static int __image_util_image_to_packet(mm_image_info_s *image, media_packet_h *packet)
 {
-       if (width <= 0) {
-               image_util_error("invalid width [%d]", width);
-               return false;
+       int err = IMAGE_UTIL_ERROR_NONE;
+       mm_util_color_format_e format = 0;
+       unsigned long width = 0, height = 0;
+       void *buffer = NULL;
+       size_t buffer_size = 0;
+       media_format_h fmt = NULL;
+       void *packet_ptr = NULL;
+       uint64_t packet_size = 0;
+       size_t size = 0;
+
+       image_util_fenter();
+
+       image_util_retvm_if((image == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle");
+
+       width = image->width;
+       height = image->height;
+       format = image->color;
+       buffer = image->data;
+       buffer_size = image->size;
+
+       err = __create_media_format(__image_format_to_mimetype(format), (unsigned int)width, (unsigned int)height, &fmt);
+       image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "__create_media_format failed (%d)", err);
+
+       err = media_packet_create_alloc(fmt, NULL, NULL, packet);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_create_alloc failed (%d)", err);
+               media_format_unref(fmt);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_packet_get_buffer_size(*packet, &packet_size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_buffer_size failed (%d)", err);
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_packet_get_buffer_data_ptr(*packet, &packet_ptr);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_buffer_data_ptr failed");
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       if (packet_ptr == NULL || packet_size == 0) {
+               image_util_error("media_packet creation failed (%p, %" PRIu64 ")", packet_ptr, packet_size);
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
        }
+       image_util_debug("Success - media_packet is created (%p, %" PRIu64 ")", packet_ptr, packet_size);
 
-       if (height <= 0) {
-               image_util_error("invalid height [%d]", height);
-               return false;
+       if ((uint64_t)buffer_size < packet_size) {
+               size = (size_t)buffer_size;
+       } else {
+               size = (size_t)packet_size;
        }
 
-       return true;
+       image_util_debug("Size: result(%u) media_packet(%" PRIu64 ") copied(%zu)", buffer_size, packet_size, size);
+       memcpy(packet_ptr, buffer, size);
+
+       err = media_packet_set_buffer_size(*packet, (uint64_t)size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_set_buffer_size failed (%d)", err);
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       image_util_fleave();
+
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
-int image_util_foreach_supported_jpeg_colorspace(image_util_supported_jpeg_colorspace_cb callback, void *user_data)
+gpointer __mm_util_thread_repeate(gpointer data)
 {
-       int i = 0;
+       transformation_s *handle = (transformation_s *) data;
+       int ret = IMAGE_UTIL_ERROR_NONE;
+       mm_image_info_s *pop_data = NULL;
+       media_packet_h packet = NULL;
 
-       image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
+       image_util_retvm_if(handle == NULL, NULL, "invalid handle");
 
-       /* RGB has higher precedence than YUV */
-       for (i = sizeof(_convert_encode_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
-               if (_convert_encode_colorspace_tbl[i] != -1)
-                       if (false == callback(i, user_data))
-                               return IMAGE_UTIL_ERROR_NONE;
+       while (!handle->is_finish) {
+               image_util_debug("waiting...");
+               pop_data = g_async_queue_timeout_pop(handle->queue, 300 * G_TIME_SPAN_MILLISECOND);
+               image_util_debug("get from data or timeout");
+
+               if (pop_data == NULL) {
+                       image_util_error("The data is null");
+                       continue;
+               }
+
+               handle->src = pop_data;
+               handle->dst = NULL;
+
+               image_util_debug("orig_image: %p [%zu] %lu X %lu (%u)", pop_data->data, pop_data->size, pop_data->width, pop_data->height, pop_data->color);
+
+               ret = __mm_util_transform(handle);
+
+               image_util_debug("result_image: %p [%zu] %lu X %lu (%u)", handle->dst->data, handle->dst->size, handle->dst->width, handle->dst->height, handle->dst->color);
 
+               if (ret == IMAGE_UTIL_ERROR_NONE)
+                       image_util_debug("Success - transform");
+               else
+                       image_util_error("Error - transform");
+
+               if ((handle->_util_cb != NULL) && (handle->_util_cb->completed_cb != NULL)) {
+                       image_util_debug("completed_cb is called");
+                       ret = __image_util_image_to_packet(handle->dst, &packet);
+                       if (ret != IMAGE_UTIL_ERROR_NONE) {
+                               image_util_error("__image_util_image_to_packet failed (%d)", ret);
+                               handle->_util_cb->completed_cb(NULL, ret, handle->_util_cb->user_data);
+                       } else {
+                               handle->_util_cb->completed_cb(&packet, ret, handle->_util_cb->user_data);
+                       }
+               } else {
+                       image_util_error("There is no callback");
+               }
+               __mm_util_destroy_color_image(pop_data);
+               __mm_util_destroy_color_image(handle->dst);
        }
 
-       return IMAGE_UTIL_ERROR_NONE;
+       image_util_debug("exit thread");
+       handle->thread = NULL;
+
+       return NULL;
 }
 
-int image_util_transform_create(transformation_h *handle)
+static int _image_util_packet_to_image(media_packet_h packet, mm_image_info_s **color_image)
 {
-       int err = MM_UTIL_ERROR_NONE;
+       int err = IMAGE_UTIL_ERROR_NONE;
+       media_format_mimetype_e mimetype = 0;
+       int width = 0, height = 0;
+       uint64_t size = 0;
+       void *ptr = NULL;
+       media_format_h fmt = NULL;
+
+       image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
+
+       err = media_packet_get_format(packet, &fmt);
+       image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", err);
+
+       err = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               image_util_error("media_packet_get_format failed (%d)", err);
+               media_format_unref(fmt);
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+       media_format_unref(fmt);
+
+       err = media_packet_get_buffer_size(packet, &size);
+       image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_size failed (%d)", err);
+
+       if (size) {
+               err = media_packet_get_buffer_data_ptr(packet, &ptr);
+               image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_data_ptr failed (%d)", err);
+       }
+
+       image_util_debug("[Fotmat: %u] W x H : %d x %d", mimetype, width, height);
+       image_util_retvm_if(((width == 0) || (height == 0) || (size == 0) || (ptr == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source packet");
+
+       err = __mm_util_create_color_image(color_image, (unsigned long)width, (unsigned long)height, __mimetype_to_image_format(mimetype), ptr, (size_t)size);
+       image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "__mm_util_create_color_image failed (%d)", err);
+
+       image_util_debug("_image_util_packet_to_image succeed");
+
+       return IMAGE_UTIL_ERROR_NONE;
+}
 
-       image_util_debug("image_util_create");
+int image_util_transform_create(transformation_h * handle)
+{
+       image_util_fenter();
 
        image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
 
-       transformation_s *_handle = (transformation_s *)calloc(1, sizeof(transformation_s));
-       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
-
-       _handle->colorspace = -1;
-       _handle->_util_cb = NULL;
-       _handle->image_h = 0;
-       _handle->hardware_acceleration = false;
-       _handle->set_convert = false;
-       _handle->set_resize  = false;
-       _handle->set_rotate = false;
-       _handle->set_crop  = false;
-
-       err = _image_util_create_transform_handle(_handle);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - create transform handle");
+       transformation_s *_handle = (transformation_s *) calloc(1, sizeof(transformation_s));
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
+
+       /* private values init */
+       _handle->dst = NULL;
+       _handle->dst_format = MM_UTIL_COLOR_NUM;
+       _handle->rotation = MM_UTIL_ROTATE_0;
+
+       _handle->start_x = -1;
+       _handle->start_y = -1;
+       _handle->dst_width = 0;
+       _handle->dst_height = 0;
+       _handle->is_finish = FALSE;
+
+       _handle->set_convert = FALSE;
+       _handle->set_crop = FALSE;
+       _handle->set_resize = FALSE;
+       _handle->set_rotate = FALSE;
+
+       /*These are a communicator for thread*/
+       if (!_handle->queue)
+               _handle->queue = g_async_queue_new();
+
+       if (_handle->queue == NULL) {
+               image_util_error("g_async_queue_new failed");
                IMAGE_UTIL_SAFE_FREE(_handle);
-               return _convert_image_util_error_code(__func__, err);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
        }
 
-       *handle = (transformation_h)_handle;
+       *handle = (transformation_h) _handle;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
-int  image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
+int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
+       image_util_warning("DEPRECATION WARNING: image_util_transform_set_hardware_acceleration() is deprecated and will be removed from next release.");
        image_util_debug("Set hardware_acceleration %d", mode);
 
        image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
@@ -239,43 +494,29 @@ int  image_util_transform_set_hardware_acceleration(transformation_h handle, boo
        image_util_retvm_if((mode == true), IMAGE_UTIL_ERROR_NOT_SUPPORTED, "hardware acceleration is not supported");
 #endif
 
-       err = mm_util_set_hardware_acceleration(_handle->image_h, mode);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - Set hardware_acceleration");
-               return _convert_image_util_error_code(__func__, err);
-       }
-
        image_util_debug("Set hardware_acceleration %d", mode);
-       _handle->hardware_acceleration = mode;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
        image_util_debug("Set colorspace_convert_info [%d]", colorspace);
 
        image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+       image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
 
-       err = mm_util_set_colorspace_convert(_handle->image_h, colorspace);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - Set colorspace convert");
-               return _convert_image_util_error_code(__func__, err);
-       }
-
-       _handle->colorspace = colorspace;
+       _handle->dst_format = colorspace;
        _handle->set_convert = true;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
        image_util_debug("Set resize_info w[%d] h[%d]", width, height);
 
@@ -283,44 +524,32 @@ int image_util_transform_set_resolution(transformation_h handle, unsigned int wi
        image_util_retvm_if((_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
        image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
 
-       err = mm_util_set_resolution(_handle->image_h, width, height);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - Set resolution");
-               return _convert_image_util_error_code(__func__, err);
-       }
-       _handle->width = width;
-       _handle->height = height;
-       _handle->set_resize  = true;
+       _handle->dst_width = width;
+       _handle->dst_height = height;
+       _handle->set_resize = true;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
        image_util_debug("Set rotate_info [%d]", rotation);
 
        image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
 
-       err = mm_util_set_rotation(_handle->image_h, rotation);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - Set rotation");
-               return _convert_image_util_error_code(__func__, err);
-       }
        _handle->rotation = rotation;
        _handle->set_rotate = true;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_set_crop_area(transformation_h handle, unsigned int start_x, unsigned int start_y, unsigned int end_x, unsigned int end_y)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
-       int dest_width;
-       int dest_height;
+       transformation_s *_handle = (transformation_s *) handle;
+       int dest_width = 0;
+       int dest_height = 0;
 
        image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
        image_util_retvm_if((_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
@@ -331,423 +560,220 @@ int image_util_transform_set_crop_area(transformation_h handle, unsigned int sta
        image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
 
        image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
-       err = mm_util_set_crop_area(_handle->image_h, start_x, start_y, end_x, end_y);
-       if (err != MM_UTIL_ERROR_NONE) {
-               image_util_error("Error - Set crop area");
-               return _convert_image_util_error_code(__func__, err);
-       }
+
        _handle->start_x = start_x;
        _handle->start_y = start_y;
-       _handle->end_x = end_x;
-       _handle->end_y = end_y;
-       _handle->set_crop  = true;
+       _handle->dst_width = dest_width;
+       _handle->dst_height = dest_height;
+       _handle->set_crop = true;
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
-int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e *colorspace)
+int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
 {
-       int ret = IMAGE_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
-       image_util_debug("Get colorspace_convert_info [%d]", colorspace);
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+       image_util_retvm_if((colorspace == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "colorspace parameter error");
+       image_util_retvm_if((!_handle->set_convert), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set colorspace before");
 
-       if (_handle == NULL) {
-               image_util_error("Invalid Handle");
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-       }
+       image_util_debug("Get colorspace_convert_info [%d]", _handle->dst_format);
 
-       if (!colorspace) {
-               image_util_error("colorspace area parameter error");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
+       *colorspace = _handle->dst_format;
 
-       if (!_handle->set_convert) {
-               image_util_error("Did not set colorspace before");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
-
-       *colorspace = _handle->colorspace;
-       return ret;
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
 {
-       int ret = IMAGE_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
-       image_util_debug("Set resize_info w[%d] h[%d]", width, height);
-
-       if (_handle == NULL) {
-               image_util_error("Invalid Handle");
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-       }
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+       image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "width or height parameter error");
+       image_util_retvm_if((!_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set resolution before");
 
-       if (!width || !height) {
-               image_util_error("resolution area parameter error");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
+       image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->dst_width, _handle->dst_height);
 
-       if (!_handle->set_resize) {
-               image_util_error("Did not set resolution before");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
-
-       *width = _handle->width;
-       *height = _handle->height;
+       *width = _handle->dst_width;
+       *height = _handle->dst_height;
 
-       return ret;
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
-int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e *rotation)
+int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
 {
-       int ret = IMAGE_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
-
-       image_util_debug("Set rotate_info [%d]", rotation);
-
-       if (_handle == NULL) {
-               image_util_error("Invalid Handle");
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-       }
+       transformation_s *_handle = (transformation_s *) handle;
 
-       if (!rotation) {
-               image_util_error("rotation area parameter error");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+       image_util_retvm_if((rotation == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "rotation parameter error");
+       image_util_retvm_if((!_handle->set_rotate), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set rotation before");
 
-       if (!_handle->set_rotate) {
-               image_util_error("Did not set rotation before");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
+       image_util_debug("Get rotate_info [%d]", _handle->rotation);
 
        *rotation = _handle->rotation;
 
-       return ret;
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_get_crop_area(transformation_h handle, unsigned int *start_x, unsigned int *start_y, unsigned int *end_x, unsigned int *end_y)
 {
-       int ret = IMAGE_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
-
-       if (_handle == NULL) {
-               image_util_error("Invalid Handle");
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
-       }
+       transformation_s *_handle = (transformation_s *) handle;
 
-       if (!start_x || !start_y || !end_x || !end_y) {
-               image_util_error("crop area parameter error");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
-
-       if (!_handle->set_crop) {
-               image_util_error("Did not set crop area before");
-               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
-       }
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+       image_util_retvm_if((start_x == NULL || start_y == NULL || end_x == NULL || end_y == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "crop area parameter error");
+       image_util_retvm_if((!_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set crop area before");
 
        *start_x = _handle->start_x;
        *start_y = _handle->start_y;
-       *end_x = _handle->end_x;
-       *end_y = _handle->end_y;
+       *end_x = _handle->start_x + _handle->dst_width;
+       *end_y = _handle->start_x + _handle->dst_height;
 
-       return ret;
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       int err = IMAGE_UTIL_ERROR_NONE;
+       transformation_s *_handle = (transformation_s *) handle;
+       mm_image_info_s *color_image = NULL;
 
-       image_util_debug("image_util_transform");
+       image_util_fenter();
 
        image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
        image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
        image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source");
+       image_util_retvm_if((_handle->queue == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid queue");
        image_util_retvm_if((!_handle->set_convert && !_handle->set_resize && !_handle->set_rotate && !_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid transform");
 
-       if (_handle->_util_cb != NULL) {
-               IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
-               _handle->_util_cb = NULL;
+       err = _image_util_packet_to_image(src, &color_image);
+       image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "_image_util_packet_to_image failed");
+
+       IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
+
+       _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
+       if (_handle->_util_cb == NULL) {
+               image_util_error("Memory allocation failed");
+               __mm_util_destroy_color_image(color_image);
+               return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
        }
-       _handle->_util_cb = (image_util_cb_s *)calloc(1, sizeof(image_util_cb_s));
-       image_util_retvm_if((_handle->_util_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
 
        _handle->_util_cb->user_data = user_data;
-       _handle->_util_cb->image_processing_completed_cb = completed_cb;
+       _handle->_util_cb->completed_cb = completed_cb;
+
+       image_util_debug("g_async_queue_push");
+       g_async_queue_push(_handle->queue, GINT_TO_POINTER(color_image));
 
-       if (_handle->_util_cb)
-               err = mm_util_transform(_handle->image_h, src, (mm_util_completed_callback)_image_util_transform_completed_cb, (void *)_handle->_util_cb);
+       if (!_handle->thread) {
+               /*create threads*/
+               _handle->thread = g_thread_new("transform_thread", (GThreadFunc)__mm_util_thread_repeate, (gpointer)_handle);
+               if (_handle->thread == NULL) {
+                       image_util_error("Fail - Create thread");
+                       __mm_util_destroy_color_image(color_image);
+                       return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+               } else {
+                       image_util_debug("Success - Create thread");
+               }
+       } else {
+               image_util_debug("Thread alreay exist");
+       }
 
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_destroy(transformation_h handle)
 {
-       int err = MM_UTIL_ERROR_NONE;
-       transformation_s *_handle = (transformation_s *)handle;
+       transformation_s *_handle = (transformation_s *) handle;
 
-       image_util_debug("image_util_destroy");
+       image_util_fenter();
 
-       if (_handle == NULL) {
-               image_util_error("Invalid Handle");
-               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
+
+       if (_handle->thread) {
+               _handle->is_finish = TRUE;
+               g_thread_join(_handle->thread);
        }
 
-       err = mm_util_destroy(_handle->image_h);
+       if (_handle->queue) {
+               g_async_queue_unref(_handle->queue);
+               _handle->queue = NULL;
+       }
 
        IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
        IMAGE_UTIL_SAFE_FREE(_handle);
 
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_convert_colorspace(unsigned char *dest, image_util_colorspace_e dest_colorspace, const unsigned char *src,  int width, int height, image_util_colorspace_e src_colorspace)
-{
-       int err = MM_UTIL_ERROR_NONE;
+       image_util_fleave();
 
-       image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
-       image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
-       image_util_retvm_if((dest_colorspace < 0 || dest_colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dst_colorspace");
-       image_util_retvm_if((src_colorspace < 0 || src_colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid src_colorspace");
-
-       err = mm_util_convert_colorspace(src, width, height, _convert_colorspace_tbl[src_colorspace], dest, _convert_colorspace_tbl[dest_colorspace]);
-
-       return _convert_image_util_error_code(__func__, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
-
-int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace , unsigned int *size)
+int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
 {
-       int err = MM_UTIL_ERROR_NONE;
+       int err = IMAGE_UTIL_ERROR_NONE;
+       size_t size_ptr = 0;
 
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
+       image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
        image_util_retvm_if((width <= 0 || height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid width or Invalid height");
        image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
 
-       err = mm_util_get_image_size(_convert_colorspace_tbl[colorspace], width, height, size);
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_resize(unsigned char *dest, int *dest_width, int *dest_height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
-{
-       int err = MM_UTIL_ERROR_NONE;
-
-       image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
-       image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
-       image_util_retvm_if((*dest_width <= 0 || *dest_height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest_width or Invalid dest_height");
-
-       unsigned int dest_w, dest_h;
-       dest_w = *dest_width;
-       dest_h = *dest_height;
-       err = mm_util_resize_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], dest, &dest_w, &dest_h);
-       if (err == MM_UTIL_ERROR_NONE) {
-               *dest_width = (int)dest_w;
-               *dest_height = (int)dest_h;
-       }
+       err = mm_util_get_image_size(TYPECAST_COLOR(colorspace), width, height, &size_ptr);
 
-       return _convert_image_util_error_code(__func__, err);
-}
+       *size = (unsigned int)size_ptr;
 
-int image_util_rotate(unsigned char *dest, int *dest_width, int *dest_height, image_util_rotation_e dest_rotation, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
-{
-       int err = MM_UTIL_ERROR_NONE;
-
-       image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
-       image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((dest_rotation < 0 || dest_rotation > IMAGE_UTIL_ROTATION_FLIP_VERT), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid rotation");
-       image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
-
-       unsigned int dest_w, dest_h;
-       err = mm_util_rotate_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], dest, &dest_w, &dest_h, dest_rotation);
-       if (err == MM_UTIL_ERROR_NONE) {
-               *dest_width = (int)dest_w;
-               *dest_height = (int)dest_h;
-       }
-       return _convert_image_util_error_code(__func__, err);
+       return _image_error_capi(err);
 }
 
-int image_util_crop(unsigned char *dest, int x, int y, int *width, int *height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
-{
-       int err = MM_UTIL_ERROR_NONE;
-
-       image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
-       image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "width or height is null");
-       image_util_retvm_if((src_width <= x  || src_height <= y || src_width < x + *width || src_height < y + *height), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid crop area");
-
-       unsigned int dest_w, dest_h;
-       dest_w = *width;
-       dest_h = *height;
-       err = mm_util_crop_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], x, y, &dest_w, &dest_h, dest);
-       if (err == MM_UTIL_ERROR_NONE) {
-               *width = (int)dest_w;
-               *height = (int)dest_h;
-       }
-
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_decode_jpeg(const char *path, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
-{
-       int err = MM_UTIL_ERROR_NONE;
-
-       image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
-       image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
-       image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
-       image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
-
-       mm_util_jpeg_yuv_data decoded;
-       memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
-
-       err = mm_util_decode_from_jpeg_file(&decoded, path, _convert_encode_colorspace_tbl[colorspace]);
-       if (err == MM_UTIL_ERROR_NONE) {
-               *image_buffer = decoded.data;
-               if (width)
-                       *width = decoded.width;
-               if (height)
-                       *height = decoded.height;
-               if (size)
-                       *size = (unsigned int)decoded.size;
-       }
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_decode_jpeg_from_memory(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
+int image_util_extract_color_from_memory(const unsigned char *image_buffer, int width, int height, unsigned char *rgb_r, unsigned char *rgb_g, unsigned char *rgb_b)
 {
-       int err = MM_UTIL_ERROR_NONE;
+       int ret = IMAGE_UTIL_ERROR_NONE;
 
-       image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
        image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
-       image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
-
-       mm_util_jpeg_yuv_data decoded;
-       memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
-
-       err = mm_util_decode_from_jpeg_memory(&decoded, (void *)jpeg_buffer, jpeg_size, _convert_encode_colorspace_tbl[colorspace]);
-
-       if (err == MM_UTIL_ERROR_NONE) {
-               *image_buffer = decoded.data;
-               if (width)
-                       *width = decoded.width;
-               if (height)
-                       *height = decoded.height;
-               if (size)
-                       *size = (unsigned int)decoded.size;
-       }
 
-       return _convert_image_util_error_code(__func__, err);
-}
+       GModule *module = NULL;
+       ModuleFunc mmutil_imgcv_module_func = NULL;
+       module = g_module_open(PATH_MMUTIL_IMGCV_LIB, G_MODULE_BIND_LAZY);
+       image_util_retvm_if((module == NULL), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "fail to open module");
 
-int image_util_decode_jpeg_with_downscale(const char *path, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
-{
-       int err = MM_UTIL_ERROR_NONE;
+       if (!g_module_symbol(module, IMGCV_FUNC_NAME, (gpointer *)&mmutil_imgcv_module_func)) {
+               image_util_error("fail to g_module_symbol");
+               g_module_close(module);
 
-       image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
-       image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
-       image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
-       image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
-       image_util_retvm_if((downscale < 0 || downscale >= sizeof(_convert_decode_scale_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downsacle is invalid");
-
-       mm_util_jpeg_yuv_data decoded;
-       memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
-
-       err = mm_util_decode_from_jpeg_file_with_downscale(&decoded, path, _convert_encode_colorspace_tbl[colorspace], _convert_decode_scale_tbl[downscale]);
-       if (err == MM_UTIL_ERROR_NONE) {
-               *image_buffer = decoded.data;
-               if (width)
-                       *width = decoded.width;
-               if (height)
-                       *height = decoded.height;
-               if (size)
-                       *size = (unsigned int)decoded.size;
-       }
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_decode_jpeg_from_memory_with_downscale(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
-{
-       int err = MM_UTIL_ERROR_NONE;
-
-       image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
-       image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
-       image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
-       image_util_retvm_if((downscale < 0 || downscale >= sizeof(_convert_decode_scale_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downsacle is invalid");
-
-       mm_util_jpeg_yuv_data decoded;
-       memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
-
-       err = mm_util_decode_from_jpeg_memory_with_downscale(&decoded, (void *)jpeg_buffer, jpeg_size, _convert_encode_colorspace_tbl[colorspace], _convert_decode_scale_tbl[downscale]);
-
-       if (err == MM_UTIL_ERROR_NONE) {
-               *image_buffer = decoded.data;
-               if (width)
-                       *width = decoded.width;
-               if (height)
-                       *height = decoded.height;
-               if (size)
-                       *size = (unsigned int)decoded.size;
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
        }
 
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace,  int quality, const char *path)
-{
-       int err = MM_UTIL_ERROR_NONE;
+       if (!mmutil_imgcv_module_func)
+               g_module_close(module);
 
-       image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
-       image_util_retvm_if((buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "buffer is null");
-       image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
+       image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
 
-       err = mm_util_jpeg_encode_to_file(path, (void *)buffer, width, height, _convert_encode_colorspace_tbl[colorspace], quality);
-       return _convert_image_util_error_code(__func__, err);
-}
-
-int image_util_encode_jpeg_to_memory(const unsigned char *image_buffer, int width, int height, image_util_colorspace_e colorspace, int quality,  unsigned char **jpeg_buffer, unsigned int *jpeg_size)
-{
-       int err = MM_UTIL_ERROR_NONE;
-       int isize = 0;
+       unsigned char r_color, g_color, b_color;
+       ret = mmutil_imgcv_module_func((void *)image_buffer, width, height, &r_color, &g_color, &b_color);
 
-       image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
-       image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
-       image_util_retvm_if((jpeg_size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_size is null");
-       image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
-       image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
+       *rgb_r = r_color;
+       *rgb_g = g_color;
+       *rgb_b = b_color;
 
-       err = mm_util_jpeg_encode_to_memory((void **)jpeg_buffer, &isize, (void *)image_buffer, width, height, _convert_encode_colorspace_tbl[colorspace], quality);
-       if (err == MM_UTIL_ERROR_NONE)
-               *jpeg_size = (unsigned int)isize;
+       if (module) {
+               g_module_close(module);
+               module = NULL;
+       }
 
-       return _convert_image_util_error_code(__func__, err);
+       return _image_error_capi(ret);
 }
 
-int image_util_extract_color_from_memory(const unsigned char *image_buffer, int width, int height, unsigned char *rgb_r, unsigned char *rgb_g, unsigned char *rgb_b)
+int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
 {
-       int ret = MM_UTIL_ERROR_NONE;
+       int idx = 0;
 
-       image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer     is null");
+       IMAGE_UTIL_TYPE_CHECK(image_type);
+       image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
 
-       unsigned char r_color, g_color, b_color;
-       ret = mm_util_cv_extract_representative_color((void *)image_buffer, width, height, &r_color, &    g_color, &b_color);
+       for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) {
+               if (is_supported_colorspace(idx, image_type))
+                       if (false == callback(idx, user_data))
+                               return IMAGE_UTIL_ERROR_NONE;
 
-       *rgb_r = r_color;
-       *rgb_g = g_color;
-       *rgb_b = b_color;
+       }
 
-       return _convert_image_util_error_code(__func__, ret);
+       return IMAGE_UTIL_ERROR_NONE;
 }