Remove unused variable 'is_completed'
[platform/core/api/image-util.git] / src / image_util.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <gmodule.h>
18 #include <inttypes.h>
19 #include <mm_util_imgp.h>
20 #include <mm_util_common.h>
21
22 #include <image_util.h>
23 #include <image_util_private.h>
24
25 typedef struct {
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;
30
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" }
47 };
48
49 static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e format)
50 {
51         unsigned int i = 0;
52         media_format_mimetype_e mimetype = -1;
53
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;
57                         break;
58                 }
59         }
60
61         image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name);
62
63         return mimetype;
64 }
65
66 static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e mimetype)
67 {
68         unsigned int i = 0;
69         mm_util_color_format_e format = -1;
70
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;
74                         break;
75                 }
76         }
77
78         image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, format);
79
80         return format;
81 }
82
83 static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *new_fmt)
84 {
85         int err = MEDIA_FORMAT_ERROR_NONE;
86
87         image_util_retvm_if((new_fmt == NULL) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
88
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);
91
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;
97         }
98
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;
104         }
105
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;
111         }
112
113         return IMAGE_UTIL_ERROR_NONE;
114 }
115
116 static void __mm_util_destroy_color_image(mm_image_info_s *image)
117 {
118         if (image == NULL) {
119                 image_util_error("[ERROR] - image");
120                 return;
121         }
122
123         IMAGE_UTIL_SAFE_FREE(image->data);
124         IMAGE_UTIL_SAFE_FREE(image);
125 }
126
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)
128 {
129         int ret = IMAGE_UTIL_ERROR_NONE;
130         mm_image_info_s *_color_image = NULL;
131
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");
135
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");
138
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);
143                 *image = NULL;
144                 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
145         }
146
147         memcpy(_color_image->data, data, size);
148
149         _color_image->size = size;
150         _color_image->width = width;
151         _color_image->height = height;
152         _color_image->color = color;
153
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);
155
156         *image = _color_image;
157
158         return ret;
159 }
160
161 static void __mm_destroy_temp_buffer(unsigned char *buffer[])
162 {
163         int i = 0;
164
165         for (i = 0; i < 4; i++)
166                 IMAGE_UTIL_SAFE_FREE(buffer[i]);
167 }
168
169 static int __mm_util_processing(transformation_s *handle)
170 {
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;
182
183         image_util_retvm_if(handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
184
185         image_util_debug("src: %p, dst: %p", handle->src, handle->dst);
186
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;
194         }
195         memcpy(dst_buf[src_index], handle->src->data, handle->src->size);
196
197         if (handle->set_crop) {
198                 dst_index++;
199
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);
205                 }
206
207                 dst_buf[dst_index] = res_buffer;
208                 src_index = dst_index;
209                 src_width = res_w;
210                 src_height = res_h;
211         } else if (handle->set_resize) {
212                 dst_index++;
213
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);
219                 }
220
221                 dst_buf[dst_index] = res_buffer;
222                 src_index = dst_index;
223                 src_width = res_w;
224                 src_height = res_h;
225         }
226
227         if (handle->set_convert) {
228                 dst_index++;
229
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);
235                 }
236
237                 dst_buf[dst_index] = res_buffer_conv;
238                 src_index = dst_index;
239                 src_format = handle->dst_format;
240                 src_width = res_w;
241                 src_height = res_h;
242         }
243
244         if (handle->set_rotate) {
245                 dst_index++;
246
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);
252                 }
253
254                 dst_buf[dst_index] = res_buffer_rotate;
255                 src_index = dst_index;
256                 src_width = res_w;
257                 src_height = res_h;
258         }
259
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");
264         } else {
265                 image_util_error("invalid result %p %zu", dst_buf[dst_index], res_buffer_size);
266                 ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
267         }
268         __mm_destroy_temp_buffer(dst_buf);
269
270         image_util_error("mm_util_processing was finished");
271
272         return ret;
273 }
274
275 static int __image_util_image_to_packet(mm_image_info_s *image, media_packet_h *packet)
276 {
277         int err = IMAGE_UTIL_ERROR_NONE;
278         mm_util_color_format_e format = 0;
279         unsigned long width = 0, height = 0;
280         void *buffer = NULL;
281         size_t buffer_size = 0;
282         media_format_h fmt = NULL;
283         void *packet_ptr = NULL;
284         uint64_t packet_size = 0;
285         size_t size = 0;
286
287         image_util_fenter();
288
289         image_util_retvm_if((image == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid handle");
290
291         width = image->width;
292         height = image->height;
293         format = image->color;
294         buffer = image->data;
295         buffer_size = image->size;
296
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);
299
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;
305         }
306
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;
312         }
313
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;
319         }
320
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;
325         }
326         image_util_debug("Success - media_packet is created (%p, %" PRIu64 ")", packet_ptr, packet_size);
327
328         if ((uint64_t)buffer_size < packet_size) {
329                 size = (size_t)buffer_size;
330         } else {
331                 size = (size_t)packet_size;
332         }
333
334         image_util_debug("Size: result(%u) media_packet(%" PRIu64 ") copied(%zu)", buffer_size, packet_size, size);
335         memcpy(packet_ptr, buffer, size);
336
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;
342         }
343
344         image_util_fleave();
345
346         return IMAGE_UTIL_ERROR_NONE;
347 }
348
349 gpointer __mm_util_thread_repeate(gpointer data)
350 {
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;
355
356         image_util_retvm_if(handle == NULL, NULL, "invalid handle");
357
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");
362
363                 if (pop_data == NULL) {
364                         image_util_error("The data is null");
365                         continue;
366                 }
367
368                 handle->src = pop_data;
369                 handle->dst = NULL;
370
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);
372
373                 ret = __mm_util_processing(handle);
374
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);
376
377                 if (ret == IMAGE_UTIL_ERROR_NONE)
378                         image_util_debug("Success - transform");
379                 else
380                         image_util_error("Error - transform");
381
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);
388                         } else {
389                                 handle->_util_cb->completed_cb(&packet, ret, handle->_util_cb->user_data);
390                         }
391                 } else {
392                         image_util_error("There is no callback");
393                 }
394                 __mm_util_destroy_color_image(pop_data);
395                 __mm_util_destroy_color_image(handle->dst);
396         }
397
398         image_util_debug("exit thread");
399         handle->thread = NULL;
400
401         return NULL;
402 }
403
404 static int __mm_util_create_thread(transformation_s *handle)
405 {
406         int ret = IMAGE_UTIL_ERROR_NONE;
407
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");
410
411         /*create threads*/
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");
414
415         image_util_debug("New thread is created");
416
417         return ret;
418 }
419
420 static int __mm_util_transform(transformation_s *handle, mm_image_info_s *image)
421 {
422         int ret = IMAGE_UTIL_ERROR_NONE;
423
424         image_util_fenter();
425
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");
428
429         image_util_debug("image: %p", image);
430
431         if (handle->queue) {
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");
437                         return ret;
438                 } else {
439                         image_util_debug("Success -__mm_util_create_thread");
440                 }
441         }
442
443         image_util_fleave();
444
445         return ret;
446 }
447
448 static int _image_util_packet_to_image(media_packet_h packet, mm_image_info_s **color_image)
449 {
450         int err = IMAGE_UTIL_ERROR_NONE;
451         media_format_mimetype_e mimetype = 0;
452         int width = 0, height = 0;
453         uint64_t size = 0;
454         void *ptr = NULL;
455         media_format_h fmt = NULL;
456
457         image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
458
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);
461
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;
467         }
468         media_format_unref(fmt);
469
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);
472
473         if (size) {
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);
476         }
477
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");
480
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);
483
484         image_util_debug("_image_util_packet_to_image succeed");
485
486         return IMAGE_UTIL_ERROR_NONE;
487 }
488
489 int image_util_transform_create(transformation_h * handle)
490 {
491         image_util_fenter();
492
493         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
494
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");
497
498         /* private values init */
499         _handle->dst = NULL;
500         _handle->dst_format = MM_UTIL_COLOR_NUM;
501         _handle->rotation = MM_UTIL_ROTATE_0;
502
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;
508
509         _handle->set_convert = FALSE;
510         _handle->set_crop = FALSE;
511         _handle->set_resize = FALSE;
512         _handle->set_rotate = FALSE;
513
514         /*These are a communicator for thread*/
515         if (!_handle->queue)
516                 _handle->queue = g_async_queue_new();
517
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;
522         }
523
524         *handle = (transformation_h) _handle;
525
526         return IMAGE_UTIL_ERROR_NONE;
527 }
528
529 int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
530 {
531         transformation_s *_handle = (transformation_s *) handle;
532
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);
535
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");
539 #endif
540
541         image_util_debug("Set hardware_acceleration %d", mode);
542
543         return IMAGE_UTIL_ERROR_NONE;
544 }
545
546 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
547 {
548         transformation_s *_handle = (transformation_s *) handle;
549
550         image_util_debug("Set colorspace_convert_info [%d]", colorspace);
551
552         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
553
554         _handle->dst_format = colorspace;
555         _handle->set_convert = true;
556
557         return IMAGE_UTIL_ERROR_NONE;
558 }
559
560 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
561 {
562         transformation_s *_handle = (transformation_s *) handle;
563
564         image_util_debug("Set resize_info w[%d] h[%d]", width, height);
565
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");
569
570         _handle->dst_width = width;
571         _handle->dst_height = height;
572         _handle->set_resize = true;
573
574         return IMAGE_UTIL_ERROR_NONE;
575 }
576
577 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
578 {
579         transformation_s *_handle = (transformation_s *) handle;
580
581         image_util_debug("Set rotate_info [%d]", rotation);
582
583         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
584
585         _handle->rotation = rotation;
586         _handle->set_rotate = true;
587
588         return IMAGE_UTIL_ERROR_NONE;
589 }
590
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)
592 {
593         transformation_s *_handle = (transformation_s *) handle;
594         int dest_width = 0;
595         int dest_height = 0;
596
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");
599
600         dest_width = end_x - start_x;
601         dest_height = end_y - start_y;
602
603         image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
604
605         image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
606
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;
612
613         return IMAGE_UTIL_ERROR_NONE;
614 }
615
616 int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
617 {
618         transformation_s *_handle = (transformation_s *) handle;
619
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");
623
624         image_util_debug("Get colorspace_convert_info [%d]", _handle->dst_format);
625
626         *colorspace = _handle->dst_format;
627
628         return IMAGE_UTIL_ERROR_NONE;
629 }
630
631 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
632 {
633         transformation_s *_handle = (transformation_s *) handle;
634
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");
638
639         image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->dst_width, _handle->dst_height);
640
641         *width = _handle->dst_width;
642         *height = _handle->dst_height;
643
644         return IMAGE_UTIL_ERROR_NONE;
645 }
646
647 int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
648 {
649         transformation_s *_handle = (transformation_s *) handle;
650
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");
654
655         image_util_debug("Get rotate_info [%d]", _handle->rotation);
656
657         *rotation = _handle->rotation;
658
659         return IMAGE_UTIL_ERROR_NONE;
660 }
661
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)
663 {
664         transformation_s *_handle = (transformation_s *) handle;
665
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");
669
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;
674
675         return IMAGE_UTIL_ERROR_NONE;
676 }
677
678 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
679 {
680         int err = IMAGE_UTIL_ERROR_NONE;
681         transformation_s *_handle = (transformation_s *) handle;
682         mm_image_info_s *color_image = NULL;
683
684         image_util_fenter();
685
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");
690
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");
693
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;
699         }
700
701         _handle->_util_cb->user_data = user_data;
702         _handle->_util_cb->completed_cb = completed_cb;
703
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);
708                 return err;
709         }
710
711         return IMAGE_UTIL_ERROR_NONE;
712 }
713
714 int image_util_transform_destroy(transformation_h handle)
715 {
716         transformation_s *_handle = (transformation_s *) handle;
717
718         image_util_fenter();
719
720         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
721
722         if (_handle->thread) {
723                 _handle->is_finish = TRUE;
724                 g_thread_join(_handle->thread);
725         }
726
727         if (_handle->queue) {
728                 g_async_queue_unref(_handle->queue);
729                 _handle->queue = NULL;
730         }
731
732         IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
733         IMAGE_UTIL_SAFE_FREE(_handle);
734
735         image_util_fleave();
736
737         return IMAGE_UTIL_ERROR_NONE;
738 }
739
740 int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
741 {
742         int err = IMAGE_UTIL_ERROR_NONE;
743         size_t size_ptr = 0;
744
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");
748
749         err = mm_util_get_image_size(TYPECAST_COLOR(colorspace), width, height, &size_ptr);
750
751         *size = (unsigned int)size_ptr;
752
753         return _image_error_capi(ERR_TYPE_COMMON, err);
754 }
755
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)
757 {
758         int ret = IMAGE_UTIL_ERROR_NONE;
759
760         image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
761
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");
766
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);
770
771                 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
772         }
773
774         if (!mmutil_imgcv_module_func)
775                 g_module_close(module);
776
777         image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
778
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);
781
782         *rgb_r = r_color;
783         *rgb_g = g_color;
784         *rgb_b = b_color;
785
786         if (module) {
787                 g_module_close(module);
788                 module = NULL;
789         }
790
791         return _image_error_capi(ERR_TYPE_COMMON, ret);
792 }
793
794 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
795 {
796         int idx = 0;
797
798         IMAGE_UTIL_TYPE_CHECK(image_type);
799         image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
800
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;
805
806         }
807
808         return IMAGE_UTIL_ERROR_NONE;
809 }