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_transform(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(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(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(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(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_transform(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 _image_util_packet_to_image(media_packet_h packet, mm_image_info_s **color_image)
406 int err = IMAGE_UTIL_ERROR_NONE;
407 media_format_mimetype_e mimetype = 0;
408 int width = 0, height = 0;
411 media_format_h fmt = NULL;
413 image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
415 err = media_packet_get_format(packet, &fmt);
416 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", err);
418 err = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
419 if (err != MEDIA_FORMAT_ERROR_NONE) {
420 image_util_error("media_packet_get_format failed (%d)", err);
421 media_format_unref(fmt);
422 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
424 media_format_unref(fmt);
426 err = media_packet_get_buffer_size(packet, &size);
427 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_size failed (%d)", err);
430 err = media_packet_get_buffer_data_ptr(packet, &ptr);
431 image_util_retvm_if((err != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_buffer_data_ptr failed (%d)", err);
434 image_util_debug("[Fotmat: %u] W x H : %d x %d", mimetype, width, height);
435 image_util_retvm_if(((width == 0) || (height == 0) || (size == 0) || (ptr == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source packet");
437 err = __mm_util_create_color_image(color_image, (unsigned long)width, (unsigned long)height, __mimetype_to_image_format(mimetype), ptr, (size_t)size);
438 image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "__mm_util_create_color_image failed (%d)", err);
440 image_util_debug("_image_util_packet_to_image succeed");
442 return IMAGE_UTIL_ERROR_NONE;
445 int image_util_transform_create(transformation_h * handle)
449 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
451 transformation_s *_handle = (transformation_s *) calloc(1, sizeof(transformation_s));
452 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
454 /* private values init */
456 _handle->dst_format = MM_UTIL_COLOR_NUM;
457 _handle->rotation = MM_UTIL_ROTATE_0;
459 _handle->start_x = -1;
460 _handle->start_y = -1;
461 _handle->dst_width = 0;
462 _handle->dst_height = 0;
463 _handle->is_finish = FALSE;
465 _handle->set_convert = FALSE;
466 _handle->set_crop = FALSE;
467 _handle->set_resize = FALSE;
468 _handle->set_rotate = FALSE;
470 /*These are a communicator for thread*/
472 _handle->queue = g_async_queue_new();
474 if (_handle->queue == NULL) {
475 image_util_error("g_async_queue_new failed");
476 IMAGE_UTIL_SAFE_FREE(_handle);
477 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
480 *handle = (transformation_h) _handle;
482 return IMAGE_UTIL_ERROR_NONE;
485 int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
487 transformation_s *_handle = (transformation_s *) handle;
489 image_util_warning("DEPRECATION WARNING: image_util_transform_set_hardware_acceleration() is deprecated and will be removed from next release.");
490 image_util_debug("Set hardware_acceleration %d", mode);
492 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
493 #ifndef ENABLE_HW_ACCELERATION
494 image_util_retvm_if((mode == true), IMAGE_UTIL_ERROR_NOT_SUPPORTED, "hardware acceleration is not supported");
497 image_util_debug("Set hardware_acceleration %d", mode);
499 return IMAGE_UTIL_ERROR_NONE;
502 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
504 transformation_s *_handle = (transformation_s *) handle;
506 image_util_debug("Set colorspace_convert_info [%d]", colorspace);
508 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
510 _handle->dst_format = colorspace;
511 _handle->set_convert = true;
513 return IMAGE_UTIL_ERROR_NONE;
516 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
518 transformation_s *_handle = (transformation_s *) handle;
520 image_util_debug("Set resize_info w[%d] h[%d]", width, height);
522 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
523 image_util_retvm_if((_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
524 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
526 _handle->dst_width = width;
527 _handle->dst_height = height;
528 _handle->set_resize = true;
530 return IMAGE_UTIL_ERROR_NONE;
533 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
535 transformation_s *_handle = (transformation_s *) handle;
537 image_util_debug("Set rotate_info [%d]", rotation);
539 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
541 _handle->rotation = rotation;
542 _handle->set_rotate = true;
544 return IMAGE_UTIL_ERROR_NONE;
547 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)
549 transformation_s *_handle = (transformation_s *) handle;
553 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
554 image_util_retvm_if((_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
556 dest_width = end_x - start_x;
557 dest_height = end_y - start_y;
559 image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
561 image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
563 _handle->start_x = start_x;
564 _handle->start_y = start_y;
565 _handle->dst_width = dest_width;
566 _handle->dst_height = dest_height;
567 _handle->set_crop = true;
569 return IMAGE_UTIL_ERROR_NONE;
572 int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
574 transformation_s *_handle = (transformation_s *) handle;
576 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
577 image_util_retvm_if((colorspace == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "colorspace parameter error");
578 image_util_retvm_if((!_handle->set_convert), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set colorspace before");
580 image_util_debug("Get colorspace_convert_info [%d]", _handle->dst_format);
582 *colorspace = _handle->dst_format;
584 return IMAGE_UTIL_ERROR_NONE;
587 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
589 transformation_s *_handle = (transformation_s *) handle;
591 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
592 image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "width or height parameter error");
593 image_util_retvm_if((!_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set resolution before");
595 image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->dst_width, _handle->dst_height);
597 *width = _handle->dst_width;
598 *height = _handle->dst_height;
600 return IMAGE_UTIL_ERROR_NONE;
603 int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
605 transformation_s *_handle = (transformation_s *) handle;
607 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
608 image_util_retvm_if((rotation == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "rotation parameter error");
609 image_util_retvm_if((!_handle->set_rotate), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set rotation before");
611 image_util_debug("Get rotate_info [%d]", _handle->rotation);
613 *rotation = _handle->rotation;
615 return IMAGE_UTIL_ERROR_NONE;
618 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)
620 transformation_s *_handle = (transformation_s *) handle;
622 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
623 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");
624 image_util_retvm_if((!_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Did not set crop area before");
626 *start_x = _handle->start_x;
627 *start_y = _handle->start_y;
628 *end_x = _handle->start_x + _handle->dst_width;
629 *end_y = _handle->start_x + _handle->dst_height;
631 return IMAGE_UTIL_ERROR_NONE;
634 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
636 int err = IMAGE_UTIL_ERROR_NONE;
637 transformation_s *_handle = (transformation_s *) handle;
638 mm_image_info_s *color_image = NULL;
642 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
643 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
644 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source");
645 image_util_retvm_if((_handle->queue == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid queue");
646 image_util_retvm_if((!_handle->set_convert && !_handle->set_resize && !_handle->set_rotate && !_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid transform");
648 err = _image_util_packet_to_image(src, &color_image);
649 image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "_image_util_packet_to_image failed");
651 IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
653 _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
654 if (_handle->_util_cb == NULL) {
655 image_util_error("Memory allocation failed");
656 __mm_util_destroy_color_image(color_image);
657 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
660 _handle->_util_cb->user_data = user_data;
661 _handle->_util_cb->completed_cb = completed_cb;
663 image_util_debug("g_async_queue_push");
664 g_async_queue_push(_handle->queue, GINT_TO_POINTER(color_image));
666 if (!_handle->thread) {
668 _handle->thread = g_thread_new("transform_thread", (GThreadFunc)__mm_util_thread_repeate, (gpointer)_handle);
669 if (_handle->thread == NULL) {
670 image_util_error("Fail - Create thread");
671 __mm_util_destroy_color_image(color_image);
672 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
674 image_util_debug("Success - Create thread");
677 image_util_debug("Thread alreay exist");
680 return IMAGE_UTIL_ERROR_NONE;
683 int image_util_transform_destroy(transformation_h handle)
685 transformation_s *_handle = (transformation_s *) handle;
689 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
691 if (_handle->thread) {
692 _handle->is_finish = TRUE;
693 g_thread_join(_handle->thread);
696 if (_handle->queue) {
697 g_async_queue_unref(_handle->queue);
698 _handle->queue = NULL;
701 IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
702 IMAGE_UTIL_SAFE_FREE(_handle);
706 return IMAGE_UTIL_ERROR_NONE;
709 int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
711 int err = IMAGE_UTIL_ERROR_NONE;
714 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
715 image_util_retvm_if((width <= 0 || height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid width or Invalid height");
716 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
718 err = mm_util_get_image_size(TYPECAST_COLOR(colorspace), width, height, &size_ptr);
720 *size = (unsigned int)size_ptr;
722 return _image_error_capi(err);
725 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)
727 int ret = IMAGE_UTIL_ERROR_NONE;
729 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
731 GModule *module = NULL;
732 ModuleFunc mmutil_imgcv_module_func = NULL;
733 module = g_module_open(PATH_MMUTIL_IMGCV_LIB, G_MODULE_BIND_LAZY);
734 image_util_retvm_if((module == NULL), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "fail to open module");
736 if (!g_module_symbol(module, IMGCV_FUNC_NAME, (gpointer *)&mmutil_imgcv_module_func)) {
737 image_util_error("fail to g_module_symbol");
738 g_module_close(module);
740 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
743 if (!mmutil_imgcv_module_func)
744 g_module_close(module);
746 image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
748 unsigned char r_color, g_color, b_color;
749 ret = mmutil_imgcv_module_func((void *)image_buffer, width, height, &r_color, &g_color, &b_color);
756 g_module_close(module);
760 return _image_error_capi(ret);
763 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
767 IMAGE_UTIL_TYPE_CHECK(image_type);
768 image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
770 for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) {
771 if (is_supported_colorspace(idx, image_type))
772 if (false == callback(idx, user_data))
773 return IMAGE_UTIL_ERROR_NONE;
777 return IMAGE_UTIL_ERROR_NONE;