2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <glib/gprintf.h>
23 #include <image_util.h>
24 #include <image_util_type.h>
26 #define MAX_STRING_LEN 128
27 #define IMAGE_FORMAT_LABEL_BUFFER_SIZE 4
28 #define IMAGE_TEST_MAX_REPEAT_COUNT 100
30 #define IMAGE_UTIL_SAFE_FREE(src) { if (src) {free(src); src = NULL; } }
33 GMainLoop *g_loop = NULL;
34 transformation_h g_handle = NULL;
35 media_packet_h g_src = NULL;
37 unsigned int g_width = 0;
38 unsigned int g_height = 0;
39 image_util_rotation_e g_angle = IMAGE_UTIL_ROTATION_NONE;
43 GMutex g_thread_mutex;
46 CURRENT_STATE_MAIN_MENU,
47 CURRENT_STATE_SET_IMAGE_MENU,
48 CURRENT_STATE_SET_ROTATION_PARAMETER_MENU,
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,
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;
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);
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);
80 media_format_mimetype_e
81 _image_util_mapping_imgp_format_to_mime(image_util_colorspace_e colorspace)
83 media_format_mimetype_e mimetype = -1;
86 case IMAGE_UTIL_COLORSPACE_NV12:
87 mimetype = MEDIA_FORMAT_NV12;
89 case IMAGE_UTIL_COLORSPACE_NV16:
90 mimetype = MEDIA_FORMAT_NV16;
92 case IMAGE_UTIL_COLORSPACE_YUYV:
93 mimetype = MEDIA_FORMAT_YUYV;
95 case IMAGE_UTIL_COLORSPACE_UYVY:
96 mimetype = MEDIA_FORMAT_UYVY;
98 case IMAGE_UTIL_COLORSPACE_YUV422:
99 mimetype = MEDIA_FORMAT_422P;
101 case IMAGE_UTIL_COLORSPACE_I420:
102 mimetype = MEDIA_FORMAT_I420;
104 case IMAGE_UTIL_COLORSPACE_NV21:
105 mimetype = MEDIA_FORMAT_YV12;
107 case IMAGE_UTIL_COLORSPACE_RGB565:
108 mimetype = MEDIA_FORMAT_RGB565;
110 case IMAGE_UTIL_COLORSPACE_RGB888:
111 mimetype = MEDIA_FORMAT_RGB888;
113 case IMAGE_UTIL_COLORSPACE_RGBA8888:
114 mimetype = MEDIA_FORMAT_RGBA;
116 case IMAGE_UTIL_COLORSPACE_ARGB8888:
117 mimetype = MEDIA_FORMAT_ARGB;
119 case IMAGE_UTIL_COLORSPACE_BGRA8888:
120 case IMAGE_UTIL_COLORSPACE_BGRX8888:
121 case IMAGE_UTIL_COLORSPACE_NV61:
124 g_printf("Not Supported Format");
128 g_printf("imgp fmt[%d] mimetype fmt[%d]\n", colorspace, mimetype);
133 bool test_transform_completed_cb(media_packet_h *packet, image_util_error_e error, void *user_data)
136 char output_file[25] = {};
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;
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);
148 if (media_packet_get_format(*packet, &dst_fmt) != MEDIA_PACKET_ERROR_NONE) {
149 g_printf("Imedia_packet_get_format");
151 IMAGE_UTIL_SAFE_FREE(output_fmt);
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"));
161 strncpy(output_fmt, "rgb", strlen("rgb"));
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);
168 FILE *fpout = fopen(output_file, "w");
170 media_packet_get_buffer_size(*packet, &size);
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);
176 g_printf("[dst] media_packet_get_extra \n");
180 //g_printf("dst: %p [%llu] \n", dst, size);
181 fwrite(dst, 1, size, fpout);
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);
190 g_printf("<<<<< ERROR >>>>> complete cb");
195 media_packet_destroy(*packet);
203 create_media_packet()
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;
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;
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;
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;
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;
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));
242 g_printf("media_format_create failed...");
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");
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");
255 g_printf("\tfile open failed %d\n", errno);
256 return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
260 g_printf("\tmemory allocation failed\n");
262 return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
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");
270 g_printf("#Error# fread readed[%zu] size[%" PRIu64 "]\n", readed, size);
273 IMAGE_UTIL_SAFE_FREE(src);
277 media_format_unref(fmt);
278 g_printf("ERROR - media_packet_create_alloc");
281 media_format_unref(fmt);
285 static void _create()
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);
293 g_printf("[%d]Success image_util_transform_destroy [%d]\n", __LINE__, ret);
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);
299 g_printf("[%d]Success image_util_transform_create [%d]\n", __LINE__, ret);
302 static void _set_image()
306 media_packet_destroy(g_src);
307 g_printf("[%d]Success source packet is destroyed \n", __LINE__);
309 ret = create_media_packet();
310 if (ret == MEDIA_PACKET_ERROR_NONE)
311 g_printf("Success - Create_media_packet\n");
313 g_printf("Error - Create_media_packet\n");
316 static void _destroy()
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);
325 g_printf("[%d]Success image_util_transform_destroy [%d]\n", __LINE__, ret);
327 g_printf("[%d]Error handle was already destroyed \n", __LINE__);
332 static void _transform(const char *cmd)
335 unsigned int width = 0;
336 unsigned int height = 0;
337 image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_ARGB8888;
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);
351 if (!strcmp("resize", cmd)) {
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);
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);
370 if (!strcmp("crop", cmd)) {
372 start_y = g_height/3;
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);
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);
394 static void _loop_test(const int count)
397 for (i = 0; i < count; i++) {
400 g_printf("<<<<< %03d >>>>>\n", i);
407 g_main_loop_quit(g_loop);
410 void reset_current_menu_state()
412 g_menu_state = CURRENT_STATE_MAIN_MENU;
416 static void display_set_rotation_parameter_menu(void)
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");
431 static void display_set_image_menu(void)
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");
447 static void display_menu(void)
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");
468 static void interpret_rotation_parameter_cmd(char *cmd)
470 int choice = atoi(cmd);
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();
479 static void interpret_set_image_cmd(char *cmd)
481 int len = strlen(cmd);
482 if (g_menu_set_image_state == CURRENT_STATE_SET_IMAGE_NONE) {
484 if (!strncmp(cmd, "1", len)) {
485 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_PATH;
487 } else if (!strncmp(cmd, "2", len)) {
488 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_WIDTH;
490 } else if (!strncmp(cmd, "3", len)) {
491 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_HEIGHT;
493 } else if (!strncmp(cmd, "4", len)) {
494 g_menu_set_image_state = CURRENT_STATE_SET_IMAGE_FORMAT;
496 } else if (!strncmp(cmd, "0", len)) {
498 reset_current_menu_state();
502 g_print("wrong command\n");
503 display_set_image_menu();
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");
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) {
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();
531 static void interpret_cmd(char *cmd)
533 int len = strlen(cmd);
534 if (g_menu_state == CURRENT_STATE_MAIN_MENU) {
536 if (!strncmp(cmd, "1", len)) {
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();
543 } else if (!strncmp(cmd, "3", len)) {
544 _transform("convert");
545 } else if (!strncmp(cmd, "4", len)) {
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();
553 } else if (!strncmp(cmd, "7", len)) {
555 } else if (!strncmp(cmd, "8", len)) {
556 _loop_test(IMAGE_TEST_MAX_REPEAT_COUNT);
557 } else if (!strncmp(cmd, "9", len)) {
559 } else if (!strncmp(cmd, "0", len)) {
563 g_print("wrong command\n");
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();
575 g_print("wrong menu state\n");
579 gboolean input(GIOChannel *channel)
581 gchar buf[MAX_STRING_LEN];
583 GError *error = NULL;
585 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
594 int main(int argc, char **argv)
596 int ret = IMAGE_UTIL_ERROR_NONE;
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);
604 g_printf("Do: export XDG_RUNTIME_DIR=/run \n");
605 g_printf("Usage: image_utl_test filename width height format \n");
611 g_path = (char *)g_malloc(MAX_STRING_LEN * sizeof(char));
612 if (g_path == NULL) {
613 g_printf("memory allocation fail. \n");
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]);
622 g_mutex_init(&g_thread_mutex);
623 g_cond_init(&g_thread_cond);
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);
631 ret = create_media_packet();
632 if (ret == MEDIA_PACKET_ERROR_NONE) {
633 g_printf("Success - Create_media_packet\n");
635 g_printf("Error - Create_media_packet\n");
641 g_loop = g_main_loop_new(NULL, FALSE);
643 g_main_loop_run(g_loop);
644 g_main_loop_unref(g_loop);
647 g_mutex_clear(&g_thread_mutex);
648 g_cond_clear(&g_thread_cond);
650 IMAGE_UTIL_SAFE_FREE(g_path);
651 g_printf("[%d]Success file path is destroyed \n", __LINE__);
653 g_printf("[%d]Error file path was already destroyed \n", __LINE__);
656 media_packet_destroy(g_src);
657 g_printf("[%d]Success source packet is destroyed \n", __LINE__);
659 g_printf("[%d]Error source packet was already destroyed \n", __LINE__);