Removed useless image_util_error_type_e
[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_transform(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(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(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(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(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_transform(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 _image_util_packet_to_image(media_packet_h packet, mm_image_info_s **color_image)
405 {
406         int err = IMAGE_UTIL_ERROR_NONE;
407         media_format_mimetype_e mimetype = 0;
408         int width = 0, height = 0;
409         uint64_t size = 0;
410         void *ptr = NULL;
411         media_format_h fmt = NULL;
412
413         image_util_retvm_if(((packet == NULL) || (color_image == NULL)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
414
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);
417
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;
423         }
424         media_format_unref(fmt);
425
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);
428
429         if (size) {
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);
432         }
433
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");
436
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);
439
440         image_util_debug("_image_util_packet_to_image succeed");
441
442         return IMAGE_UTIL_ERROR_NONE;
443 }
444
445 int image_util_transform_create(transformation_h * handle)
446 {
447         image_util_fenter();
448
449         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
450
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");
453
454         /* private values init */
455         _handle->dst = NULL;
456         _handle->dst_format = MM_UTIL_COLOR_NUM;
457         _handle->rotation = MM_UTIL_ROTATE_0;
458
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;
464
465         _handle->set_convert = FALSE;
466         _handle->set_crop = FALSE;
467         _handle->set_resize = FALSE;
468         _handle->set_rotate = FALSE;
469
470         /*These are a communicator for thread*/
471         if (!_handle->queue)
472                 _handle->queue = g_async_queue_new();
473
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;
478         }
479
480         *handle = (transformation_h) _handle;
481
482         return IMAGE_UTIL_ERROR_NONE;
483 }
484
485 int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
486 {
487         transformation_s *_handle = (transformation_s *) handle;
488
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);
491
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");
495 #endif
496
497         image_util_debug("Set hardware_acceleration %d", mode);
498
499         return IMAGE_UTIL_ERROR_NONE;
500 }
501
502 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
503 {
504         transformation_s *_handle = (transformation_s *) handle;
505
506         image_util_debug("Set colorspace_convert_info [%d]", colorspace);
507
508         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
509
510         _handle->dst_format = colorspace;
511         _handle->set_convert = true;
512
513         return IMAGE_UTIL_ERROR_NONE;
514 }
515
516 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
517 {
518         transformation_s *_handle = (transformation_s *) handle;
519
520         image_util_debug("Set resize_info w[%d] h[%d]", width, height);
521
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");
525
526         _handle->dst_width = width;
527         _handle->dst_height = height;
528         _handle->set_resize = true;
529
530         return IMAGE_UTIL_ERROR_NONE;
531 }
532
533 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
534 {
535         transformation_s *_handle = (transformation_s *) handle;
536
537         image_util_debug("Set rotate_info [%d]", rotation);
538
539         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
540
541         _handle->rotation = rotation;
542         _handle->set_rotate = true;
543
544         return IMAGE_UTIL_ERROR_NONE;
545 }
546
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)
548 {
549         transformation_s *_handle = (transformation_s *) handle;
550         int dest_width = 0;
551         int dest_height = 0;
552
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");
555
556         dest_width = end_x - start_x;
557         dest_height = end_y - start_y;
558
559         image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
560
561         image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
562
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;
568
569         return IMAGE_UTIL_ERROR_NONE;
570 }
571
572 int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
573 {
574         transformation_s *_handle = (transformation_s *) handle;
575
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");
579
580         image_util_debug("Get colorspace_convert_info [%d]", _handle->dst_format);
581
582         *colorspace = _handle->dst_format;
583
584         return IMAGE_UTIL_ERROR_NONE;
585 }
586
587 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
588 {
589         transformation_s *_handle = (transformation_s *) handle;
590
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");
594
595         image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->dst_width, _handle->dst_height);
596
597         *width = _handle->dst_width;
598         *height = _handle->dst_height;
599
600         return IMAGE_UTIL_ERROR_NONE;
601 }
602
603 int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
604 {
605         transformation_s *_handle = (transformation_s *) handle;
606
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");
610
611         image_util_debug("Get rotate_info [%d]", _handle->rotation);
612
613         *rotation = _handle->rotation;
614
615         return IMAGE_UTIL_ERROR_NONE;
616 }
617
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)
619 {
620         transformation_s *_handle = (transformation_s *) handle;
621
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");
625
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;
630
631         return IMAGE_UTIL_ERROR_NONE;
632 }
633
634 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
635 {
636         int err = IMAGE_UTIL_ERROR_NONE;
637         transformation_s *_handle = (transformation_s *) handle;
638         mm_image_info_s *color_image = NULL;
639
640         image_util_fenter();
641
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");
647
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");
650
651         IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
652
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;
658         }
659
660         _handle->_util_cb->user_data = user_data;
661         _handle->_util_cb->completed_cb = completed_cb;
662
663         image_util_debug("g_async_queue_push");
664         g_async_queue_push(_handle->queue, GINT_TO_POINTER(color_image));
665
666         if (!_handle->thread) {
667                 /*create threads*/
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;
673                 } else {
674                         image_util_debug("Success - Create thread");
675                 }
676         } else {
677                 image_util_debug("Thread alreay exist");
678         }
679
680         return IMAGE_UTIL_ERROR_NONE;
681 }
682
683 int image_util_transform_destroy(transformation_h handle)
684 {
685         transformation_s *_handle = (transformation_s *) handle;
686
687         image_util_fenter();
688
689         image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
690
691         if (_handle->thread) {
692                 _handle->is_finish = TRUE;
693                 g_thread_join(_handle->thread);
694         }
695
696         if (_handle->queue) {
697                 g_async_queue_unref(_handle->queue);
698                 _handle->queue = NULL;
699         }
700
701         IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
702         IMAGE_UTIL_SAFE_FREE(_handle);
703
704         image_util_fleave();
705
706         return IMAGE_UTIL_ERROR_NONE;
707 }
708
709 int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
710 {
711         int err = IMAGE_UTIL_ERROR_NONE;
712         size_t size_ptr = 0;
713
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");
717
718         err = mm_util_get_image_size(TYPECAST_COLOR(colorspace), width, height, &size_ptr);
719
720         *size = (unsigned int)size_ptr;
721
722         return _image_error_capi(err);
723 }
724
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)
726 {
727         int ret = IMAGE_UTIL_ERROR_NONE;
728
729         image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
730
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");
735
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);
739
740                 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
741         }
742
743         if (!mmutil_imgcv_module_func)
744                 g_module_close(module);
745
746         image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
747
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);
750
751         *rgb_r = r_color;
752         *rgb_g = g_color;
753         *rgb_b = b_color;
754
755         if (module) {
756                 g_module_close(module);
757                 module = NULL;
758         }
759
760         return _image_error_capi(ret);
761 }
762
763 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
764 {
765         int idx = 0;
766
767         IMAGE_UTIL_TYPE_CHECK(image_type);
768         image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
769
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;
774
775         }
776
777         return IMAGE_UTIL_ERROR_NONE;
778 }