Fix coverity issue
[platform/core/api/image-util.git] / test / image_util_test.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 <glib.h>
18 #include <glib/gprintf.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <image_util.h>
24 #include <image_util_type.h>
25
26 #define MAX_STRING_LEN 128
27 #define IMAGE_FORMAT_LABEL_BUFFER_SIZE 4
28 #define IMAGE_TEST_MAX_REPEAT_COUNT 100
29
30 #define IMAGE_UTIL_SAFE_FREE(src)      { if (src) {free(src); src = NULL; } }
31
32
33 GMainLoop *g_loop = NULL;
34 transformation_h g_handle = NULL;
35 media_packet_h g_src = NULL;
36 char *g_path = NULL;
37 unsigned int g_width = 0;
38 unsigned int g_height = 0;
39 image_util_rotation_e g_angle = IMAGE_UTIL_ROTATION_NONE;
40 int g_format = -1;
41
42 GCond g_thread_cond;
43 GMutex g_thread_mutex;
44
45 enum {
46         CURRENT_STATE_MAIN_MENU,
47         CURRENT_STATE_SET_IMAGE_MENU,
48         CURRENT_STATE_SET_ROTATION_PARAMETER_MENU,
49 };
50
51 enum {
52         CURRENT_STATE_SET_IMAGE_NONE,
53         CURRENT_STATE_SET_IMAGE_PATH,
54         CURRENT_STATE_SET_IMAGE_WIDTH,
55         CURRENT_STATE_SET_IMAGE_HEIGHT,
56         CURRENT_STATE_SET_IMAGE_FORMAT,
57 };
58
59 int g_menu_state = CURRENT_STATE_MAIN_MENU;
60 int g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
61 int g_menu_rotation_parameter = IMAGE_UTIL_ROTATION_NONE;
62
63 void _wait()
64 {
65         g_mutex_lock(&g_thread_mutex);
66         g_printf("waiting... untill finishing transform \n");
67         g_cond_wait(&g_thread_cond, &g_thread_mutex);
68         g_printf("<=== get signal from callback \n");
69         g_mutex_unlock(&g_thread_mutex);
70 }
71
72 void _signal()
73 {
74         g_mutex_lock(&g_thread_mutex);
75         g_cond_signal(&g_thread_cond);
76         g_printf("===> send signal to test proc \n");
77         g_mutex_unlock(&g_thread_mutex);
78 }
79
80 media_format_mimetype_e
81 _image_util_mapping_imgp_format_to_mime(image_util_colorspace_e colorspace)
82 {
83         media_format_mimetype_e mimetype = -1;
84
85         switch (colorspace) {
86         case IMAGE_UTIL_COLORSPACE_NV12:
87                 mimetype = MEDIA_FORMAT_NV12;
88                 break;
89         case IMAGE_UTIL_COLORSPACE_NV16:
90                 mimetype = MEDIA_FORMAT_NV16;
91                 break;
92         case IMAGE_UTIL_COLORSPACE_YUYV:
93                 mimetype = MEDIA_FORMAT_YUYV;
94                 break;
95         case IMAGE_UTIL_COLORSPACE_UYVY:
96                 mimetype = MEDIA_FORMAT_UYVY;
97                 break;
98         case IMAGE_UTIL_COLORSPACE_YUV422:
99                 mimetype = MEDIA_FORMAT_422P;
100                 break;
101         case IMAGE_UTIL_COLORSPACE_I420:
102                 mimetype = MEDIA_FORMAT_I420;
103                 break;
104         case IMAGE_UTIL_COLORSPACE_NV21:
105                 mimetype = MEDIA_FORMAT_YV12;
106                 break;
107         case IMAGE_UTIL_COLORSPACE_RGB565:
108                 mimetype = MEDIA_FORMAT_RGB565;
109                 break;
110         case IMAGE_UTIL_COLORSPACE_RGB888:
111                 mimetype = MEDIA_FORMAT_RGB888;
112                 break;
113         case IMAGE_UTIL_COLORSPACE_RGBA8888:
114                 mimetype = MEDIA_FORMAT_RGBA;
115                 break;
116         case IMAGE_UTIL_COLORSPACE_ARGB8888:
117                 mimetype = MEDIA_FORMAT_ARGB;
118                 break;
119         case IMAGE_UTIL_COLORSPACE_BGRA8888:
120         case IMAGE_UTIL_COLORSPACE_BGRX8888:
121         case IMAGE_UTIL_COLORSPACE_NV61:
122         default:
123                 mimetype = -1;
124                 g_printf("Not Supported Format");
125                 break;
126         }
127
128         g_printf("imgp fmt[%d] mimetype fmt[%d]\n", colorspace, mimetype);
129
130         return mimetype;
131 }
132
133 bool test_transform_completed_cb(media_packet_h *packet, image_util_error_e error, void *user_data)
134 {
135         uint64_t size = 0;
136         char output_file[25] = {};
137
138         media_format_h dst_fmt;
139         media_format_mimetype_e dst_mimetype;
140         int dst_width, dst_height, dst_avg_bps, dst_max_bps;
141         char *output_fmt = NULL;
142
143         g_printf("test_transform_completed_cb============= [%d] \n", error);
144         if (error == IMAGE_UTIL_ERROR_NONE) {
145                 g_printf("<<<<< SUCCESS >>>>>\n");
146                 output_fmt = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
147                 if (output_fmt) {
148                         if (media_packet_get_format(*packet, &dst_fmt) != MEDIA_PACKET_ERROR_NONE) {
149                                 g_printf("Imedia_packet_get_format");
150                                 _signal();
151                                 IMAGE_UTIL_SAFE_FREE(output_fmt);
152                                 return FALSE;
153                         }
154
155                         if (media_format_get_video_info(dst_fmt, &dst_mimetype, &dst_width, &dst_height, &dst_avg_bps, &dst_max_bps) == MEDIA_FORMAT_ERROR_NONE) {
156                                 memset(output_fmt, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
157                                 if (dst_mimetype  == MEDIA_FORMAT_YV12 || dst_mimetype == MEDIA_FORMAT_422P || dst_mimetype == MEDIA_FORMAT_I420
158                                     || dst_mimetype == MEDIA_FORMAT_NV12 || dst_mimetype == MEDIA_FORMAT_UYVY || dst_mimetype == MEDIA_FORMAT_YUYV) {
159                                         strncpy(output_fmt, "yuv", strlen("yuv"));
160                                 } else {
161                                         strncpy(output_fmt, "rgb", strlen("rgb"));
162                                 }
163                                 g_printf("[mimetype: %d] W x H : %d x %d \n", dst_mimetype, dst_width, dst_height);
164                                 snprintf(output_file, 25, "result_%dx%d.%s", dst_width, dst_height, output_fmt);
165                         }
166                 }
167
168                 FILE *fpout = fopen(output_file, "w");
169                 if (fpout) {
170                         media_packet_get_buffer_size(*packet, &size);
171                         void *dst = NULL;
172                         if (media_packet_get_buffer_data_ptr(*packet, &dst) != MEDIA_PACKET_ERROR_NONE) {
173                                 IMAGE_UTIL_SAFE_FREE(dst);
174                                 IMAGE_UTIL_SAFE_FREE(output_fmt);
175                                 fclose(fpout);
176                                 g_printf("[dst] media_packet_get_extra \n");
177                                 _signal();
178                                 return FALSE;
179                         }
180                         //g_printf("dst: %p [%llu] \n", dst, size);
181                         fwrite(dst, 1, size, fpout);
182                         g_printf("FREE \n");
183                         fclose(fpout);
184                 }
185
186                 g_printf("Result is written to %s\n", output_file);
187                 g_printf("Free (output_fmt) \n");
188                 IMAGE_UTIL_SAFE_FREE(output_fmt);
189         } else {
190                 g_printf("<<<<< ERROR >>>>> complete cb");
191                 _signal();
192                 return FALSE;
193         }
194
195         media_packet_destroy(*packet);
196
197         _signal();
198
199         return TRUE;
200 }
201
202 static int
203 create_media_packet()
204 {
205         int ret = 0;
206         media_format_h fmt;
207         void *src = NULL;
208         void *ptr = NULL;
209         if (media_format_create(&fmt) == MEDIA_FORMAT_ERROR_NONE) {
210                 if (media_format_set_video_mime(fmt, _image_util_mapping_imgp_format_to_mime(g_format)) != MEDIA_FORMAT_ERROR_NONE) {
211                         media_format_unref(fmt);
212                         g_printf("[Error] Set - video mime\n");
213                         return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
214                 }
215
216                 if (media_format_set_video_width(fmt, g_width) != MEDIA_FORMAT_ERROR_NONE) {
217                         media_format_unref(fmt);
218                         g_printf("[Error] Set - video width\n");
219                         return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
220                 }
221
222                 if (media_format_set_video_height(fmt, g_height) != MEDIA_FORMAT_ERROR_NONE) {
223                         media_format_unref(fmt);
224                         g_printf("[Error] Set - video heigh\nt");
225                         return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
226                 }
227
228                 if (media_format_set_video_avg_bps(fmt, 2000000) != MEDIA_FORMAT_ERROR_NONE) {
229                         media_format_unref(fmt);
230                         g_printf("[Error] Set - video avg bps\n");
231                         return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
232                 }
233
234                 if (media_format_set_video_max_bps(fmt, 15000000) != MEDIA_FORMAT_ERROR_NONE) {
235                         media_format_unref(fmt);
236                         g_printf("[Error] Set - video max bps\n");
237                         return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
238                 }
239
240                 g_printf("media_format_set_video_info success! file[%s] w[%d] h[%d] mime[%d]\n", g_path, g_width, g_height, _image_util_mapping_imgp_format_to_mime(g_format));
241         } else {
242                 g_printf("media_format_create failed...");
243         }
244
245         ret = media_packet_create_alloc(fmt, NULL, NULL, &g_src);
246         if (ret == MEDIA_PACKET_ERROR_NONE) {
247                 g_printf("Success - media_packet_create_alloc\n");
248                 uint64_t size = 0;
249                 size_t readed = 0;
250
251                 if (media_packet_get_buffer_size(g_src, &size) == MEDIA_PACKET_ERROR_NONE) {
252                         if (media_packet_get_buffer_data_ptr(g_src, &ptr) == MEDIA_PACKET_ERROR_NONE) {
253                                 FILE *fp = fopen(g_path, "r");
254                                 if (fp == NULL) {
255                                         g_printf("\tfile open failed %d\n", errno);
256                                         return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
257                                 }
258                                 src = malloc(size);
259                                 if (src == NULL) {
260                                         g_printf("\tmemory allocation failed\n");
261                                         fclose(fp);
262                                         return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
263                                 }
264                                 readed = fread(src, 1, (size_t)size, fp);
265                                 if (readed <= size) {
266                                         g_printf("#Success# fread\n");
267                                         memcpy(ptr, src, (int)size);
268                                         g_printf("memcpy\n");
269                                 } else {
270                                         g_printf("#Error# fread readed[%zu] size[%" PRIu64 "]\n", readed, size);
271                                 }
272                                 fclose(fp);
273                                 IMAGE_UTIL_SAFE_FREE(src);
274                         }
275                 }
276         } else {
277                 media_format_unref(fmt);
278                 g_printf("ERROR - media_packet_create_alloc");
279                 return ret;
280         }
281         media_format_unref(fmt);
282         return ret;
283 }
284
285 static void _create()
286 {
287         int ret = 0;
288         if (g_handle != NULL) {
289                 ret = image_util_transform_destroy(g_handle);
290                 if (ret != IMAGE_UTIL_ERROR_NONE)
291                         g_printf("[%d]Error image_util_transform_destroy [%d]\n", __LINE__, ret);
292                 else
293                         g_printf("[%d]Success image_util_transform_destroy [%d]\n", __LINE__, ret);
294         }
295         ret = image_util_transform_create(&g_handle);
296         if (ret != IMAGE_UTIL_ERROR_NONE)
297                 g_printf("[%d]Error image_util_transform_create [%d]\n", __LINE__, ret);
298         else
299                 g_printf("[%d]Success image_util_transform_create [%d]\n", __LINE__, ret);
300 }
301
302 static void _set_image()
303 {
304         int ret = 0;
305         if (g_src) {
306                 media_packet_destroy(g_src);
307                 g_printf("[%d]Success source packet is destroyed \n", __LINE__);
308         }
309         ret = create_media_packet();
310         if (ret == MEDIA_PACKET_ERROR_NONE)
311                 g_printf("Success - Create_media_packet\n");
312         else
313                 g_printf("Error - Create_media_packet\n");
314 }
315
316 static void _destroy()
317 {
318         int ret = 0;
319
320         if (g_handle) {
321                 ret = image_util_transform_destroy(g_handle);
322                 if (ret != IMAGE_UTIL_ERROR_NONE)
323                         g_printf("[%d]Error image_util_transform_destroy [%d]\n", __LINE__, ret);
324                 else
325                         g_printf("[%d]Success image_util_transform_destroy [%d]\n", __LINE__, ret);
326         } else {
327                 g_printf("[%d]Error handle was already destroyed \n", __LINE__);
328         }
329         g_handle = NULL;
330 }
331
332 static void _transform(const char *cmd)
333 {
334         int ret = 0;
335         unsigned int width = 0;
336         unsigned int height = 0;
337         image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_ARGB8888;
338         int start_x;
339         int start_y;
340         int end_x;
341         int end_y;
342
343         if (!strcmp("convert", cmd)) {
344                 ret = image_util_transform_set_colorspace(g_handle, colorspace);
345                 if (ret != IMAGE_UTIL_ERROR_NONE) {
346                         g_printf("[%d]error image_util_transform_set_colorspace [%d]\n", __LINE__, ret);
347                         return;
348                 }
349         }
350
351         if (!strcmp("resize", cmd)) {
352                 width = g_width/2;
353                 height = g_height/2;
354
355                 ret = image_util_transform_set_resolution(g_handle, width, height);
356                 if (ret != IMAGE_UTIL_ERROR_NONE) {
357                         g_printf("[%d]Error image_util_transform_set_resolution [%d]\n", __LINE__, ret);
358                         return;
359                 }
360         }
361
362         if (!strcmp("rotate", cmd)) {
363                 ret = image_util_transform_set_rotation(g_handle, g_angle);
364                 if (ret != IMAGE_UTIL_ERROR_NONE) {
365                         g_printf("[%d]Error image_util_transform_set_rotation [%d]\n", __LINE__, ret);
366                         return;
367                 }
368         }
369
370         if (!strcmp("crop", cmd)) {
371                 start_x = g_width/4;
372                 start_y = g_height/3;
373                 end_x = g_width*3/4;
374                 end_y = g_height;
375
376                 ret = image_util_transform_set_crop_area(g_handle, start_x, start_y, end_x, end_y);
377                 if (ret != IMAGE_UTIL_ERROR_NONE) {
378                         g_printf("[%d]Error image_util_transform_set_crop_area [%d]\n", __LINE__, ret);
379                         return;
380                 }
381         }
382
383         if (!strcmp("run", cmd)) {
384                 ret = image_util_transform_run(g_handle, g_src, (image_util_transform_completed_cb)test_transform_completed_cb, NULL);
385                 if (ret != IMAGE_UTIL_ERROR_NONE) {
386                         g_printf("[%d]Error image_util_transform_run [%d]\n", __LINE__, ret);
387                         return;
388                 }
389                 _wait();
390         }
391
392 }
393
394 static void _loop_test(const int count)
395 {
396         int i = 0;
397         for (i = 0; i < count; i++) {
398                 _transform("run");
399                 _set_image();
400                 g_printf("<<<<< %03d >>>>>\n", i);
401         }
402 }
403
404 void quit(void)
405 {
406         if (g_loop)
407                 g_main_loop_quit(g_loop);
408 }
409
410 void reset_current_menu_state()
411 {
412         g_menu_state = CURRENT_STATE_MAIN_MENU;
413         return;
414 }
415
416 static void display_set_rotation_parameter_menu(void)
417 {
418         g_print("\n");
419         g_print("====================================================\n");
420         g_print("    Enter number of rotate operation parameter\n");
421         g_print("----------------------------------------------------\n");
422         g_print("0. None\n");
423         g_print("1. Rotation 90 degrees\n");
424         g_print("2. Rotation 180 degrees\n");
425         g_print("3. Rotation 270 degrees\n");
426         g_print("4. Flip horizontal\n");
427         g_print("5. Flip vertical\n");
428         g_print("====================================================\n");
429 }
430
431 static void display_set_image_menu(void)
432 {
433         g_print("\n");
434         g_print("====================================================\n");
435         g_print("    image-util Core-API test: Main menu v0.2\n");
436         g_print("----------------------------------------------------\n");
437         g_print("1. set image path \n");
438         g_print("2. set image width \n");
439         g_print("3. set image height \n");
440         g_print("4. set image format \n");
441         g_print("0. back \n");
442         g_print("----------------------------------------------------\n");
443         g_print("====================================================\n");
444
445 }
446
447 static void display_menu(void)
448 {
449         g_print("\n");
450         g_print("====================================================\n");
451         g_print("   image-util Core-API test: Main menu v0.2\n");
452         g_print("----------------------------------------------------\n");
453         g_print("1. create handle \n");
454         g_print("2. set image \n");
455         g_print("3. set convert \n");
456         g_print("4. set crop \n");
457         g_print("5. set resize \n");
458         g_print("6. set rotate \n");
459         g_print("7. run \n");
460         g_print("8. run repeatly \n");
461         g_print("9. destroy handle \n");
462         g_print("0. quit \n");
463         g_print("----------------------------------------------------\n");
464         g_print("====================================================\n");
465
466 }
467
468 static void interpret_rotation_parameter_cmd(char *cmd)
469 {
470         int choice = atoi(cmd);
471
472         if (choice < IMAGE_UTIL_ROTATION_NONE || choice > IMAGE_UTIL_ROTATION_FLIP_VERT) {
473                 g_print("wrong rotation parameter\n");
474                 display_set_rotation_parameter_menu();
475         }
476         g_angle = choice;
477 }
478
479 static void interpret_set_image_cmd(char *cmd)
480 {
481         int len = strlen(cmd);
482         if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_NONE) {
483                 if (len == 1) {
484                         if (!strncmp(cmd, "1", len)) {
485                                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_PATH;
486                                 g_print("Path: ");
487                         } else if (!strncmp(cmd, "2", len)) {
488                                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_WIDTH;
489                                 g_print("Width: ");
490                         } else if (!strncmp(cmd, "3", len)) {
491                                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_HEIGHT;
492                                 g_print("height: ");
493                         } else if (!strncmp(cmd, "4", len)) {
494                                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_FORMAT;
495                                 g_print("Format: ");
496                         } else if (!strncmp(cmd, "0", len)) {
497                                 _set_image();
498                                 reset_current_menu_state();
499                                 display_menu();
500                         }
501                 } else {
502                         g_print("wrong command\n");
503                         display_set_image_menu();
504                 }
505         } else if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_PATH) {
506                 IMAGE_UTIL_SAFE_FREE(g_path);
507                 g_path = (char *)g_malloc(MAX_STRING_LEN * sizeof(char));
508                 if (g_path == NULL) {
509                         g_printf("memory allocation fail. \n");
510                         return;
511                 }
512                 memset(g_path, 0x00, MAX_STRING_LEN);
513                 snprintf(g_path, MAX_STRING_LEN, "%s", cmd);
514                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
515                 display_set_image_menu();
516         } else if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_WIDTH) {
517                 g_width = atoi(cmd);
518                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
519                 display_set_image_menu();
520         } else if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_HEIGHT) {
521                 g_height = atoi(cmd);
522                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
523                 display_set_image_menu();
524         } else if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_FORMAT) {
525                 g_format = atoi(cmd);
526                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
527                 display_set_image_menu();
528         }
529 }
530
531 static void interpret_cmd(char *cmd)
532 {
533         int len = strlen(cmd);
534         if (g_menu_state == CURRENT_STATE_MAIN_MENU) {
535                 if (len == 1) {
536                         if (!strncmp(cmd, "1", len)) {
537                                 _create();
538                         } else if (!strncmp(cmd, "2", len)) {
539                                 g_menu_state = CURRENT_STATE_SET_IMAGE_MENU;
540                                 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_NONE;
541                                 display_set_image_menu();
542                                 return;
543                         } else if (!strncmp(cmd, "3", len)) {
544                                 _transform("convert");
545                         } else if (!strncmp(cmd, "4", len)) {
546                                 _transform("crop");
547                         } else if (!strncmp(cmd, "5", len)) {
548                                 _transform("resize");
549                         } else if (!strncmp(cmd, "6", len)) {
550                                 g_menu_state = CURRENT_STATE_SET_ROTATION_PARAMETER_MENU;
551                                 display_set_rotation_parameter_menu();
552                                 return;
553                         } else if (!strncmp(cmd, "7", len)) {
554                                 _transform("run");
555                         } else if (!strncmp(cmd, "8", len)) {
556                                 _loop_test(IMAGE_TEST_MAX_REPEAT_COUNT);
557                         } else if (!strncmp(cmd, "9", len)) {
558                                 _destroy();
559                         } else if (!strncmp(cmd, "0", len)) {
560                                 quit();
561                         }
562                 } else {
563                         g_print("wrong command\n");
564                 }
565
566                 display_menu();
567         } else if (g_menu_state == CURRENT_STATE_SET_IMAGE_MENU) {
568                 interpret_set_image_cmd(cmd);
569         } else if (g_menu_state == CURRENT_STATE_SET_ROTATION_PARAMETER_MENU) {
570                 interpret_rotation_parameter_cmd(cmd);
571                 _transform("rotate");
572                 reset_current_menu_state();
573                 display_menu();
574         } else {
575                 g_print("wrong menu state\n");
576         }
577 }
578
579 gboolean input(GIOChannel *channel)
580 {
581         gchar buf[MAX_STRING_LEN];
582         gsize read;
583         GError *error = NULL;
584
585         g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
586
587         buf[read] = '\0';
588         g_strstrip(buf);
589         interpret_cmd(buf);
590
591         return TRUE;
592 }
593
594 int main(int argc, char **argv)
595 {
596         int ret = IMAGE_UTIL_ERROR_NONE;
597
598         GIOChannel *stdin_channel = NULL;
599         stdin_channel = g_io_channel_unix_new(0);
600         g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
601         g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL);
602
603         if (argc < 4) {
604                 g_printf("Do: export XDG_RUNTIME_DIR=/run \n");
605                 g_printf("Usage: image_utl_test filename width height format \n");
606                 return ret;
607         }
608
609         g_handle = NULL;
610         g_src = NULL;
611         g_path = (char *)g_malloc(MAX_STRING_LEN * sizeof(char));
612         if (g_path == NULL) {
613                 g_printf("memory allocation fail. \n");
614                 return ret;
615         }
616
617         snprintf(g_path, MAX_STRING_LEN, "%s", argv[1]);
618         g_width = atoi(argv[2]);
619         g_height = atoi(argv[3]);
620         g_format = atoi(argv[4]);
621
622         g_mutex_init(&g_thread_mutex);
623         g_cond_init(&g_thread_cond);
624
625         ret = image_util_transform_create(&g_handle);
626         if (ret != IMAGE_UTIL_ERROR_NONE) {
627                 g_printf("[%d]Error image_util_transform_create [%d]\n", __LINE__, ret);
628                 goto Exit;
629         }
630
631         ret = create_media_packet();
632         if (ret == MEDIA_PACKET_ERROR_NONE) {
633                 g_printf("Success - Create_media_packet\n");
634         } else {
635                 g_printf("Error - Create_media_packet\n");
636                 goto Exit;
637         }
638
639         display_menu();
640
641         g_loop = g_main_loop_new(NULL, FALSE);
642
643         g_main_loop_run(g_loop);
644         g_main_loop_unref(g_loop);
645
646 Exit:
647         g_mutex_clear(&g_thread_mutex);
648         g_cond_clear(&g_thread_cond);
649         if (g_path) {
650                 IMAGE_UTIL_SAFE_FREE(g_path);
651                 g_printf("[%d]Success file path is destroyed \n", __LINE__);
652         } else {
653                 g_printf("[%d]Error file path was already destroyed \n", __LINE__);
654         }
655         if (g_src) {
656                 media_packet_destroy(g_src);
657                 g_printf("[%d]Success source packet is destroyed \n", __LINE__);
658         } else {
659                 g_printf("[%d]Error source packet was already destroyed \n", __LINE__);
660         }
661         _destroy();
662
663         return 0;
664 }