2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <mm_util_imgp.h>
20 #include <mm_util_common.h>
22 #include <image_util.h>
23 #include <image_util_private.h>
26 mm_util_color_format_e image_format;
27 media_format_mimetype_e mimetype;
28 const char *mimetype_name;
29 } image_format_mimetype_pair_s;
31 static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_COLOR_NUM] = {
32 { MM_UTIL_COLOR_YUV420, MEDIA_FORMAT_YV12, "MEDIA_FORMAT_YV12" },
33 { MM_UTIL_COLOR_YUV422, MEDIA_FORMAT_422P, "MEDIA_FORMAT_422P" },
34 { MM_UTIL_COLOR_I420, MEDIA_FORMAT_I420, "MEDIA_FORMAT_I420" },
35 { MM_UTIL_COLOR_NV12, MEDIA_FORMAT_NV12, "Not support" },
36 { MM_UTIL_COLOR_UYVY, MEDIA_FORMAT_UYVY, "MEDIA_FORMAT_UYVY" },
37 { MM_UTIL_COLOR_YUYV, MEDIA_FORMAT_YUYV, "MEDIA_FORMAT_YUYV" },
38 { MM_UTIL_COLOR_RGB16, MEDIA_FORMAT_RGB565, "MEDIA_FORMAT_RGB565" },
39 { MM_UTIL_COLOR_RGB24, MEDIA_FORMAT_RGB888, "MEDIA_FORMAT_RGB888" },
40 { MM_UTIL_COLOR_ARGB, MEDIA_FORMAT_ARGB, "MEDIA_FORMAT_ARGB" },
41 { MM_UTIL_COLOR_BGRA, MEDIA_FORMAT_BGRA, "MEDIA_FORMAT_BGRA" },
42 { MM_UTIL_COLOR_RGBA, MEDIA_FORMAT_RGBA, "MEDIA_FORMAT_RGBA" },
43 { MM_UTIL_COLOR_BGRX, -1, "Not support" },
44 { MM_UTIL_COLOR_NV12_TILED, MEDIA_FORMAT_NV12T, "MEDIA_FORMAT_NV12T" },
45 { MM_UTIL_COLOR_NV16, MEDIA_FORMAT_NV16, "MEDIA_FORMAT_NV16" },
46 { MM_UTIL_COLOR_NV61, -1, "Not support" }
49 static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e format)
52 media_format_mimetype_e mimetype = -1;
54 for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
55 if (image_format_mimetype_table[i].image_format == format) {
56 mimetype = image_format_mimetype_table[i].mimetype;
61 image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name);
66 static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e mimetype)
69 mm_util_color_format_e format = -1;
71 for (i = 0; i < MM_UTIL_COLOR_NUM; i++) {
72 if (image_format_mimetype_table[i].mimetype == mimetype) {
73 format = image_format_mimetype_table[i].image_format;
78 image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, format);
83 static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *new_fmt)
85 int err = MEDIA_FORMAT_ERROR_NONE;
87 image_util_retvm_if((new_fmt == NULL) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
89 err = media_format_create(new_fmt);
90 image_util_retvm_if((err != MEDIA_FORMAT_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_format_make_writable failed (%d)", err);
92 err = media_format_set_video_mime(*new_fmt, mimetype);
93 if (err != MEDIA_FORMAT_ERROR_NONE) {
94 media_format_unref(*new_fmt);
95 image_util_error("media_format_set_video_mime failed (%d)", err);
96 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
99 err = media_format_set_video_width(*new_fmt, width);
100 if (err != MEDIA_FORMAT_ERROR_NONE) {
101 media_format_unref(*new_fmt);
102 image_util_error("media_format_set_video_width failed (%d)", err);
103 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
106 err = media_format_set_video_height(*new_fmt, height);
107 if (err != MEDIA_FORMAT_ERROR_NONE) {
108 media_format_unref(*new_fmt);
109 image_util_error("media_format_set_video_height failed (%d)", err);
110 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
113 return IMAGE_UTIL_ERROR_NONE;
116 static void __mm_util_destroy_color_image(mm_image_info_s *image)
119 image_util_error("[ERROR] - image");
123 IMAGE_UTIL_SAFE_FREE(image->data);
124 IMAGE_UTIL_SAFE_FREE(image);
127 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)
129 int ret = IMAGE_UTIL_ERROR_NONE;
130 mm_image_info_s *_color_image = NULL;
132 image_util_retvm_if((image == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle");
133 image_util_retvm_if((color >= MM_UTIL_COLOR_NUM), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid color");
134 image_util_retvm_if((data == NULL || size == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid data");
136 _color_image = (mm_image_info_s *)calloc(1, sizeof(mm_image_info_s));
137 image_util_retvm_if((_color_image == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed");
139 _color_image->data = calloc(1, size);
140 if (_color_image->data == NULL) {
141 image_util_error("Memory allocation failed");
142 __mm_util_destroy_color_image(_color_image);
144 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
147 memcpy(_color_image->data, data, size);
149 _color_image->size = size;
150 _color_image->width = width;
151 _color_image->height = height;
152 _color_image->color = color;
154 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);
156 *image = _color_image;
161 static void __mm_destroy_temp_buffer(unsigned char *buffer[])
165 for (i = 0; i < 4; i++)
166 IMAGE_UTIL_SAFE_FREE(buffer[i]);
169 static int __mm_util_processing(transformation_s *handle)
171 int ret = IMAGE_UTIL_ERROR_NONE;
172 unsigned char *dst_buf[4] = {NULL,};
173 unsigned int src_width = 0, src_height = 0;
174 mm_util_color_format_e src_format = -1;
175 unsigned int src_index = 0, dst_index = 0;
176 unsigned int res_w = 0;
177 unsigned int res_h = 0;
178 unsigned char *res_buffer = NULL;
179 unsigned char *res_buffer_conv = NULL;
180 unsigned char *res_buffer_rotate = NULL;
181 size_t res_buffer_size = 0;
183 image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
185 image_util_debug("src: %p, dst: %p", handle->src, handle->dst);
187 dst_buf[src_index] = calloc(1, handle->src->size);
188 src_width = handle->src->width;
189 src_height = handle->src->height;
190 src_format = handle->src->color;
191 if (dst_buf[src_index] == NULL) {
192 image_util_error("[multi func] memory allocation error");
193 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
195 memcpy(dst_buf[src_index], handle->src->data, handle->src->size);
197 if (handle->set_crop) {
200 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);
201 if (ret != MM_UTIL_ERROR_NONE) {
202 __mm_destroy_temp_buffer(dst_buf);
203 image_util_error("mm_util_crop_image failed");
204 return _image_error_capi(ERR_TYPE_TRANSFORM, ret);
207 dst_buf[dst_index] = res_buffer;
208 src_index = dst_index;
211 } else if (handle->set_resize) {
214 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);
215 if (ret != MM_UTIL_ERROR_NONE) {
216 __mm_destroy_temp_buffer(dst_buf);
217 image_util_error("mm_util_resize_image failed");
218 return _image_error_capi(ERR_TYPE_TRANSFORM, ret);
221 dst_buf[dst_index] = res_buffer;
222 src_index = dst_index;
227 if (handle->set_convert) {
230 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);
231 if (ret != MM_UTIL_ERROR_NONE) {
232 __mm_destroy_temp_buffer(dst_buf);
233 image_util_error("mm_util_convert_colorspace failed");
234 return _image_error_capi(ERR_TYPE_TRANSFORM, ret);
237 dst_buf[dst_index] = res_buffer_conv;
238 src_index = dst_index;
239 src_format = handle->dst_format;
244 if (handle->set_rotate) {
247 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);
248 if (ret != MM_UTIL_ERROR_NONE) {
249 __mm_destroy_temp_buffer(dst_buf);
250 image_util_error("mm_util_rotate_image failed");
251 return _image_error_capi(ERR_TYPE_TRANSFORM, ret);
254 dst_buf[dst_index] = res_buffer_rotate;
255 src_index = dst_index;
260 if (dst_buf[dst_index] != NULL && res_buffer_size != 0) {
261 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);
262 if (ret != IMAGE_UTIL_ERROR_NONE)
263 image_util_error("__mm_util_create_color_image failed");
265 image_util_error("invalid result %p %zu", dst_buf[dst_index], res_buffer_size);
266 ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
268 __mm_destroy_temp_buffer(dst_buf);
270 image_util_error("mm_util_processing was finished");
275 static int __image_util_image_to_packet(mm_image_info_s *image, media_packet_h *packet)
277 int err = IMAGE_UTIL_ERROR_NONE;
278 mm_util_color_format_e format = 0;
279 unsigned long width = 0, height = 0;
281 size_t buffer_size = 0;
282 media_format_h fmt = NULL;
283 void *packet_ptr = NULL;
284 uint64_t packet_size = 0;
289 image_util_retvm_if((image == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle");
291 width = image->width;
292 height = image->height;
293 format = image->color;
294 buffer = image->data;
295 buffer_size = image->size;
297 err = __create_media_format(__image_format_to_mimetype(format), (unsigned int)width, (unsigned int)height, &fmt);
298 image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "__create_media_format failed (%d)", err);
300 err = media_packet_create_alloc(fmt, NULL, NULL, packet);
301 if (err != MEDIA_PACKET_ERROR_NONE) {
302 image_util_error("media_packet_create_alloc failed (%d)", err);
303 media_format_unref(fmt);
304 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
307 err = media_packet_get_buffer_size(*packet, &packet_size);
308 if (err != MEDIA_PACKET_ERROR_NONE) {
309 image_util_error("media_packet_get_buffer_size failed (%d)", err);
310 media_packet_destroy(*packet);
311 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
314 err = media_packet_get_buffer_data_ptr(*packet, &packet_ptr);
315 if (err != MEDIA_PACKET_ERROR_NONE) {
316 image_util_error("media_packet_get_buffer_data_ptr failed");
317 media_packet_destroy(*packet);
318 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
321 if (packet_ptr == NULL || packet_size == 0) {
322 image_util_error("media_packet creation failed (%p, %" PRIu64 ")", packet_ptr, packet_size);
323 media_packet_destroy(*packet);
324 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
326 image_util_debug("Success - media_packet is created (%p, %" PRIu64 ")", packet_ptr, packet_size);
328 if ((uint64_t)buffer_size < packet_size) {
329 size = (size_t)buffer_size;
331 size = (size_t)packet_size;
334 image_util_debug("Size: result(%u) media_packet(%" PRIu64 ") copied(%zu)", buffer_size, packet_size, size);
335 memcpy(packet_ptr, buffer, size);
337 err = media_packet_set_buffer_size(*packet, (uint64_t)size);
338 if (err != MEDIA_PACKET_ERROR_NONE) {
339 image_util_error("media_packet_set_buffer_size failed (%d)", err);
340 media_packet_destroy(*packet);
341 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
346 return IMAGE_UTIL_ERROR_NONE;
349 gpointer __mm_util_thread_repeate(gpointer data)
351 transformation_s *handle = (transformation_s *) data;
352 int ret = IMAGE_UTIL_ERROR_NONE;
353 mm_image_info_s *pop_data = NULL;
354 media_packet_h packet = NULL;
356 image_util_retvm_if(handle == NULL, NULL, "invalid handle");
358 while (!handle->is_finish) {
359 image_util_debug("waiting...");
360 pop_data = g_async_queue_timeout_pop(handle->queue, 300 * G_TIME_SPAN_MILLISECOND);
361 image_util_debug("get from data or timeout");
363 if (pop_data == NULL) {
364 image_util_error("The data is null");
368 handle->src = pop_data;
371 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);
373 ret = __mm_util_processing(handle);
375 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);
377 if (ret == IMAGE_UTIL_ERROR_NONE)
378 image_util_debug("Success - transform");
380 image_util_error("Error - transform");
382 if ((handle->_util_cb != NULL) && (handle->_util_cb->completed_cb != NULL)) {
383 image_util_debug("completed_cb is called");
384 ret = __image_util_image_to_packet(handle->dst, &packet);
385 if (ret != IMAGE_UTIL_ERROR_NONE) {
386 image_util_error("__image_util_image_to_packet failed (%d)", ret);
387 handle->_util_cb->completed_cb(NULL, ret, handle->_util_cb->user_data);
389 handle->_util_cb->completed_cb(&packet, ret, handle->_util_cb->user_data);
392 image_util_error("There is no callback");
394 __mm_util_destroy_color_image(pop_data);
395 __mm_util_destroy_color_image(handle->dst);
398 image_util_debug("exit thread");
399 handle->thread = NULL;
404 static int __mm_util_create_thread(transformation_s *handle)
406 int ret = IMAGE_UTIL_ERROR_NONE;
408 image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
409 image_util_retvm_if(handle->thread != NULL, IMAGE_UTIL_ERROR_NONE, "[NO-ERROR] Thread is already created");
412 handle->thread = g_thread_new("transform_thread", (GThreadFunc)__mm_util_thread_repeate, (gpointer)handle);
413 image_util_retvm_if(handle->thread == NULL, IMAGE_UTIL_ERROR_INVALID_OPERATION, "ERROR - create thread");
415 image_util_debug("New thread is created");
420 static int __mm_util_transform(transformation_s *handle, mm_image_info_s *image)
422 int ret = IMAGE_UTIL_ERROR_NONE;
426 image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
427 image_util_retvm_if(image == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
429 image_util_debug("image: %p", image);
432 image_util_debug("g_async_queue_push");
433 g_async_queue_push(handle->queue, GINT_TO_POINTER(image));
434 ret = __mm_util_create_thread(handle);
435 if (ret != IMAGE_UTIL_ERROR_NONE) {
436 image_util_error("ERROR - Create thread");
439 image_util_debug("Success -__mm_util_create_thread");
448 static int _image_util_packet_to_image(media_packet_h packet, mm_image_info_s **color_image)
450 int err = IMAGE_UTIL_ERROR_NONE;
451 media_format_mimetype_e mimetype = 0;
452 int width = 0, height = 0;
455 media_format_h fmt = NULL;
457 image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
459 err = media_packet_get_format(packet, &fmt);
460 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", err);
462 err = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
463 if (err != MEDIA_FORMAT_ERROR_NONE) {
464 image_util_error("media_packet_get_format failed (%d)", err);
465 media_format_unref(fmt);
466 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
468 media_format_unref(fmt);
470 err = media_packet_get_buffer_size(packet, &size);
471 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_size failed (%d)", err);
474 err = media_packet_get_buffer_data_ptr(packet, &ptr);
475 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_data_ptr failed (%d)", err);
478 image_util_debug("[Fotmat: %u] W x H : %d x %d", mimetype, width, height);
479 image_util_retvm_if(((width == 0) || (height == 0) || (size == 0) || (ptr == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source packet");
481 err = __mm_util_create_color_image(color_image, (unsigned long)width, (unsigned long)height, __mimetype_to_image_format(mimetype), ptr, (size_t)size);
482 image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "__mm_util_create_color_image failed (%d)", err);
484 image_util_debug("_image_util_packet_to_image succeed");
486 return IMAGE_UTIL_ERROR_NONE;
489 int image_util_transform_create(transformation_h * handle)
493 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
495 transformation_s *_handle = (transformation_s *) calloc(1, sizeof(transformation_s));
496 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
498 /* private values init */
500 _handle->dst_format = MM_UTIL_COLOR_NUM;
501 _handle->rotation = MM_UTIL_ROTATE_0;
503 _handle->start_x = -1;
504 _handle->start_y = -1;
505 _handle->dst_width = 0;
506 _handle->dst_height = 0;
507 _handle->is_finish = FALSE;
509 _handle->set_convert = FALSE;
510 _handle->set_crop = FALSE;
511 _handle->set_resize = FALSE;
512 _handle->set_rotate = FALSE;
514 /*These are a communicator for thread*/
516 _handle->queue = g_async_queue_new();
518 if (_handle->queue == NULL) {
519 image_util_error("g_async_queue_new failed");
520 IMAGE_UTIL_SAFE_FREE(_handle);
521 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
524 *handle = (transformation_h) _handle;
526 return IMAGE_UTIL_ERROR_NONE;
529 int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
531 transformation_s *_handle = (transformation_s *) handle;
533 image_util_warning("DEPRECATION WARNING: image_util_transform_set_hardware_acceleration() is deprecated and will be removed from next release.");
534 image_util_debug("Set hardware_acceleration %d", mode);
536 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
537 #ifndef ENABLE_HW_ACCELERATION
538 image_util_retvm_if((mode == true), IMAGE_UTIL_ERROR_NOT_SUPPORTED, "hardware acceleration is not supported");
541 image_util_debug("Set hardware_acceleration %d", mode);
543 return IMAGE_UTIL_ERROR_NONE;
546 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
548 transformation_s *_handle = (transformation_s *) handle;
550 image_util_debug("Set colorspace_convert_info [%d]", colorspace);
552 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
554 _handle->dst_format = colorspace;
555 _handle->set_convert = true;
557 return IMAGE_UTIL_ERROR_NONE;
560 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
562 transformation_s *_handle = (transformation_s *) handle;
564 image_util_debug("Set resize_info w[%d] h[%d]", width, height);
566 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
567 image_util_retvm_if((_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
568 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
570 _handle->dst_width = width;
571 _handle->dst_height = height;
572 _handle->set_resize = true;
574 return IMAGE_UTIL_ERROR_NONE;
577 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
579 transformation_s *_handle = (transformation_s *) handle;
581 image_util_debug("Set rotate_info [%d]", rotation);
583 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
585 _handle->rotation = rotation;
586 _handle->set_rotate = true;
588 return IMAGE_UTIL_ERROR_NONE;
591 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)
593 transformation_s *_handle = (transformation_s *) handle;
597 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
598 image_util_retvm_if((_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
600 dest_width = end_x - start_x;
601 dest_height = end_y - start_y;
603 image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
605 image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
607 _handle->start_x = start_x;
608 _handle->start_y = start_y;
609 _handle->dst_width = dest_width;
610 _handle->dst_height = dest_height;
611 _handle->set_crop = true;
613 return IMAGE_UTIL_ERROR_NONE;
616 int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
618 transformation_s *_handle = (transformation_s *) handle;
620 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
621 image_util_retvm_if((colorspace == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "colorspace parameter error");
622 image_util_retvm_if((!_handle->set_convert), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set colorspace before");
624 image_util_debug("Get colorspace_convert_info [%d]", _handle->dst_format);
626 *colorspace = _handle->dst_format;
628 return IMAGE_UTIL_ERROR_NONE;
631 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
633 transformation_s *_handle = (transformation_s *) handle;
635 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
636 image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "width or height parameter error");
637 image_util_retvm_if((!_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set resolution before");
639 image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->dst_width, _handle->dst_height);
641 *width = _handle->dst_width;
642 *height = _handle->dst_height;
644 return IMAGE_UTIL_ERROR_NONE;
647 int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
649 transformation_s *_handle = (transformation_s *) handle;
651 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
652 image_util_retvm_if((rotation == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "rotation parameter error");
653 image_util_retvm_if((!_handle->set_rotate), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set rotation before");
655 image_util_debug("Get rotate_info [%d]", _handle->rotation);
657 *rotation = _handle->rotation;
659 return IMAGE_UTIL_ERROR_NONE;
662 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)
664 transformation_s *_handle = (transformation_s *) handle;
666 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
667 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");
668 image_util_retvm_if((!_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set crop area before");
670 *start_x = _handle->start_x;
671 *start_y = _handle->start_y;
672 *end_x = _handle->start_x + _handle->dst_width;
673 *end_y = _handle->start_x + _handle->dst_height;
675 return IMAGE_UTIL_ERROR_NONE;
678 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
680 int err = IMAGE_UTIL_ERROR_NONE;
681 transformation_s *_handle = (transformation_s *) handle;
682 mm_image_info_s *color_image = NULL;
686 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
687 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
688 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source");
689 image_util_retvm_if((!_handle->set_convert && !_handle->set_resize && !_handle->set_rotate && !_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid transform");
691 err = _image_util_packet_to_image(src, &color_image);
692 image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "_image_util_packet_to_image failed");
694 _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
695 if (_handle->_util_cb == NULL) {
696 image_util_error("Memory allocation failed");
697 __mm_util_destroy_color_image(color_image);
698 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
701 _handle->_util_cb->user_data = user_data;
702 _handle->_util_cb->completed_cb = completed_cb;
704 err = __mm_util_transform(_handle, color_image);
705 if (err != IMAGE_UTIL_ERROR_NONE) {
706 image_util_error("Error - Run transform (%d)", err);
707 __mm_util_destroy_color_image(color_image);
711 return IMAGE_UTIL_ERROR_NONE;
714 int image_util_transform_destroy(transformation_h handle)
716 transformation_s *_handle = (transformation_s *) handle;
720 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
722 if (_handle->thread) {
723 _handle->is_finish = TRUE;
724 g_thread_join(_handle->thread);
727 if (_handle->queue) {
728 g_async_queue_unref(_handle->queue);
729 _handle->queue = NULL;
732 IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
733 IMAGE_UTIL_SAFE_FREE(_handle);
737 return IMAGE_UTIL_ERROR_NONE;
740 int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
742 int err = IMAGE_UTIL_ERROR_NONE;
745 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
746 image_util_retvm_if((width <= 0 || height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid width or Invalid height");
747 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
749 err = mm_util_get_image_size(TYPECAST_COLOR(colorspace), width, height, &size_ptr);
751 *size = (unsigned int)size_ptr;
753 return _image_error_capi(ERR_TYPE_COMMON, err);
756 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)
758 int ret = IMAGE_UTIL_ERROR_NONE;
760 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
762 GModule *module = NULL;
763 ModuleFunc mmutil_imgcv_module_func = NULL;
764 module = g_module_open(PATH_MMUTIL_IMGCV_LIB, G_MODULE_BIND_LAZY);
765 image_util_retvm_if((module == NULL), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "fail to open module");
767 if (!g_module_symbol(module, IMGCV_FUNC_NAME, (gpointer *)&mmutil_imgcv_module_func)) {
768 image_util_error("fail to g_module_symbol");
769 g_module_close(module);
771 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
774 if (!mmutil_imgcv_module_func)
775 g_module_close(module);
777 image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
779 unsigned char r_color, g_color, b_color;
780 ret = mmutil_imgcv_module_func((void *)image_buffer, width, height, &r_color, &g_color, &b_color);
787 g_module_close(module);
791 return _image_error_capi(ERR_TYPE_COMMON, ret);
794 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
798 IMAGE_UTIL_TYPE_CHECK(image_type);
799 image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
801 for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) {
802 if (is_supported_colorspace(idx, image_type))
803 if (false == callback(idx, user_data))
804 return IMAGE_UTIL_ERROR_NONE;
808 return IMAGE_UTIL_ERROR_NONE;