4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Vineeth T M <vineeth.tm@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
28 #include <image_util.h>
29 #include <image_util_internal.h>
32 #define DECODE_RESULT_PATH "/home/owner/media/decode_test."
33 #define ENCODE_RESULT_PATH "/home/owner/media/encode_test."
37 #define TEST_FILE_MAX 100
43 TEST_DECODE_MEM_ASYNC,
44 LAST_DECODE_TEST = TEST_DECODE_MEM_ASYNC,
46 GIFTEST_ENCODE_FILE = FIRST_GIF_TEST,
48 GIFTEST_ENCODE_FRAME_FILE, /* interanl */
49 GIFTEST_ENCODE_FRAME_MEM, /* interanl */
56 unsigned int image_type;
58 unsigned long long buffer_size;
62 gboolean decode_result;
63 char filepath[PATH_MAX];
66 unsigned char *decoded;
67 unsigned long long decode_size;
71 char out_path[PATH_MAX];
72 unsigned char *encoded;
73 unsigned long long encode_size;
76 static unsigned int g_num_of_files;
77 static unsigned int g_num_of_decoded;
79 static test_inputs_s g_test_input;
80 static test_decode_s g_test_decode[TEST_FILE_MAX];
81 static test_encode_s g_test_encode;
83 static char TEST_CMD[][CMD_MAX] = {
90 "encode-gif-frame", /* internal */
91 "encode-gif-frame-mem", /* internal */
95 GMutex g_thread_mutex;
99 g_mutex_lock(&g_thread_mutex);
100 fprintf(stderr, "waiting... until finishing \n");
101 g_cond_wait(&g_thread_cond, &g_thread_mutex);
102 fprintf(stderr, "<=== get signal from callback \n");
103 g_mutex_unlock(&g_thread_mutex);
108 g_mutex_lock(&g_thread_mutex);
109 g_cond_signal(&g_thread_cond);
110 fprintf(stderr, "===> send signal to test proc \n");
111 g_mutex_unlock(&g_thread_mutex);
114 static inline void flush_stdin()
117 while ((ch = getchar()) != EOF && ch != '\n') ;
120 static gboolean _read_file(char *file_name, void **data, unsigned long long *data_size)
125 if (!file_name || !data || !data_size) {
126 fprintf(stderr, "\tNULL pointer\n");
130 fprintf(stderr, "\tTry to open %s to read\n", file_name);
132 fp = fopen(file_name, "r");
134 fprintf(stderr, "\tfile open failed %d\n", errno);
138 fseek(fp, 0, SEEK_END);
139 file_size = ftell(fp);
140 if (file_size > -1L) {
142 *data = (void *)malloc(file_size);
144 fprintf(stderr, "\tmalloc failed %d\n", errno);
149 if (fread(*data, 1, file_size, fp) == (size_t)file_size) {
150 fprintf(stderr, "#Success# fread\n");
152 fprintf(stderr, "#Error# fread\n");
162 *data_size = (unsigned long long)file_size;
169 fprintf(stderr, "#Error# ftell\n");
176 static gboolean _write_file(const char *file_name, void *data, unsigned long long data_size)
180 if (!file_name || !data || data_size <= 0) {
181 fprintf(stderr, "\tinvalid data %s %p size:%lld\n", file_name, data, data_size);
185 fprintf(stderr, "\tTry to open %s to write\n", file_name);
187 fp = fopen(file_name, "w");
189 fprintf(stderr, "\tfile open failed %d\n", errno);
193 fwrite(data, 1, data_size, fp);
197 fprintf(stderr, "\tfile [%s] write DONE\n", file_name);
202 bool decode_completed_cb(int error, void *user_param, unsigned long width, unsigned long height, unsigned long long size)
204 test_decode_s *user_data = (test_decode_s *)user_param;
205 user_data->width = width;
206 user_data->height = height;
207 user_data->decode_size = size;
213 bool encode_completed_cb(int error, void *user_param, unsigned long long size)
215 test_encode_s *user_data = (test_encode_s *)user_param;
216 user_data->encode_size = size;
224 if (g_test_input.path != NULL) {
225 g_free(g_test_input.path);
226 g_test_input.path = NULL;
228 if (g_test_input.buffer != NULL) {
229 g_free(g_test_input.buffer);
230 g_test_input.buffer = NULL;
238 for (i = 0; i < g_num_of_files; i++) {
239 if (g_test_decode[i].decoded != NULL) {
240 free(g_test_decode[i].decoded);
241 g_test_decode[i].decoded = NULL;
248 if (g_test_encode.encoded != NULL) {
249 free(g_test_encode.encoded);
250 g_test_encode.encoded = NULL;
261 gboolean _init_datas()
263 memset(&g_test_input, 0, sizeof(test_inputs_s));
264 memset(&g_test_decode, 0, sizeof(test_decode_s) * TEST_FILE_MAX);
265 memset(&g_test_encode, 0, sizeof(test_encode_s));
268 g_num_of_decoded = 0;
275 struct dirent *dp = NULL;
276 DIR *fd = opendir(g_test_input.
\rpath);
277 unsigned int i = 0, j = 0;
279 fprintf(stderr, "\tlistdir: can't open %s\n", g_test_input.
\rpath);
283 while ((dp = readdir(fd)) != NULL) {
284 if (strlen(dp->d_name) == 0)
286 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
287 continue; /* skip self and parent */
288 size_t nbytes = g_snprintf(g_test_decode[g_num_of_files].filepath, sizeof(g_test_decode[g_num_of_files].filepath), "%s%s", g_test_input.
\rpath, dp->d_name);
296 if (g_num_of_files == 0) {
297 fprintf(stderr, "\tNo Test File!\n");
303 for (i = 0; i < g_num_of_files; i++) {
304 for (j = 0; j < g_num_of_files - 1; j++) {
305 if ((strlen(g_test_decode[j].filepath) > strlen(g_test_decode[j + 1].filepath)) ||
306 (strcmp(g_test_decode[j].filepath, g_test_decode[j + 1].filepath) > 0 && strlen(g_test_decode[j].filepath) == strlen(g_test_decode[j + 1].filepath))) {
307 memset(temp, 0, PATH_MAX);
308 g_strlcpy(temp, g_test_decode[j].filepath, sizeof(temp));
309 g_strlcpy(g_test_decode[j].filepath, g_test_decode[j + 1].filepath, sizeof(g_test_decode[j].filepath));
310 g_strlcpy(g_test_decode[j + 1].filepath, temp, sizeof(g_test_decode[j + 1].filepath));
318 gboolean _parse_inputs(int argc, char *argv[])
321 gboolean result = FALSE;
323 if (argv[1] == NULL || strlen(argv[1]) == 0) return FALSE;
325 for (i = 0; i < TEST_COMMAND_NUM; i++) {
326 if (g_strcmp0(argv[1], TEST_CMD[i]) == 0) {
327 g_test_input.cmd = i;
331 if (!result) return FALSE;
333 if (argv[2] == NULL || strlen(argv[2]) == 0) return FALSE;
335 g_test_input.path = g_strdup(argv[2]);
336 if (g_test_input.path == NULL)
339 if (g_test_input.cmd >= FIRST_GIF_TEST) {
340 g_test_input.image_type = IMAGE_UTIL_GIF;
341 size_t nbytes = g_snprintf(g_test_encode.out_path, PATH_MAX, "%s%s", ENCODE_RESULT_PATH, "gif");
347 if (argv[3] == NULL || strlen(argv[3]) == 0) return FALSE;
349 long temp = g_ascii_strtoll(argv[3], NULL, 10);
350 if (temp < 0 || temp > (long)IMAGE_UTIL_BMP)
352 g_test_input.image_type = (unsigned int)temp;
355 memset(g_test_encode.out_path, 0, PATH_MAX);
357 switch (g_test_input.image_type) {
358 case IMAGE_UTIL_JPEG:
359 snprintf(ext, 4, "%s", "jpg");
362 snprintf(ext, 4, "%s", "png");
365 snprintf(ext, 4, "%s", "gif");
368 snprintf(ext, 4, "%s", "bmp");
371 fprintf(stderr, "\tNot supported image type!\n");
375 snprintf(g_test_encode.out_path, PATH_MAX, "%s%s", ENCODE_RESULT_PATH, ext);
380 void _print_help(int argc, char *argv[])
384 fprintf(stderr, "\t[usage]\n");
385 fprintf(stderr, "\t\t1. decode & encode : %s ", argv[0]);
386 for (i = 0; i <= LAST_DECODE_TEST; i++) {
387 fprintf(stderr, "%s", TEST_CMD[i]);
388 if (i != LAST_DECODE_TEST)
389 fprintf(stderr, "/");
391 fprintf(stderr, " filepath encode_image_type\n");
392 fprintf(stderr, "\t\t2. encode animated-gif : %s ", argv[0]);
393 for (i = FIRST_GIF_TEST; i < TEST_COMMAND_NUM; i++) {
394 fprintf(stderr, "%s", TEST_CMD[i]);
395 if (i != TEST_COMMAND_NUM - 1)
396 fprintf(stderr, "/");
398 fprintf(stderr, " folderpath containing png images named \
399 with number prefix according to the animation order'\n");
402 gboolean _read_test_files()
404 if (g_test_input.cmd >= FIRST_GIF_TEST) {
405 if (_read_dir() == FALSE)
408 if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) {
409 if (_read_file(g_test_input.path, &g_test_input.buffer, &g_test_input.buffer_size) == FALSE) {
410 fprintf(stderr, "\tRead test file failed!\n");
414 size_t nbytes = g_snprintf(g_test_decode[0].filepath, PATH_MAX, "%s", g_test_input.
\rpath);
415 if (nbytes == 0) return FALSE;
420 fprintf(stderr, "\tThe %d files are readed!\n", g_num_of_files);
425 gboolean test_decode()
429 image_util_decode_h decoded = NULL;
431 for (i = 0; i < g_num_of_files; i++) {
432 g_test_decode[i].decode_result = FALSE;
434 ret = image_util_decode_create(&decoded);
435 if (ret != IMAGE_UTIL_ERROR_NONE)
438 if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC))
439 ret = image_util_decode_set_input_buffer(decoded, (unsigned char *)g_test_input.buffer, g_test_input.buffer_size);
441 ret = image_util_decode_set_input_path(decoded, g_test_decode[i].filepath);
443 if (ret != IMAGE_UTIL_ERROR_NONE) {
444 image_util_decode_destroy(decoded);
448 ret = image_util_decode_set_output_buffer(decoded, &(g_test_decode[i].decoded));
449 if (ret != IMAGE_UTIL_ERROR_NONE) {
450 image_util_decode_destroy(decoded);
454 if ((g_test_input.cmd == TEST_DECODE_ASYNC) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) {
455 ret = image_util_decode_run_async(decoded, (image_util_decode_completed_cb) decode_completed_cb, &g_test_decode[i]);
456 if (ret == IMAGE_UTIL_ERROR_NONE)
459 ret = image_util_decode_run(decoded, &g_test_decode[i].width, &g_test_decode[i].height, &g_test_decode[i].decode_size);
461 if (ret != IMAGE_UTIL_ERROR_NONE) {
462 image_util_decode_destroy(decoded);
466 image_util_decode_destroy(decoded);
467 g_test_decode[i].decode_result = TRUE;
471 if (g_num_of_decoded == 0) {
472 fprintf(stderr, "\tNo decoded data!\n");
475 fprintf(stderr, "\tThe %d images are decoded!(0: %p %llu)\n", g_num_of_decoded, g_test_decode[0].decoded, g_test_decode[0].decode_size);
478 /* write the decoded result to the file */
479 if (g_test_input.cmd <= LAST_DECODE_TEST) {
481 memset(temp, 0, PATH_MAX);
482 snprintf(temp, PATH_MAX, "%s%s", DECODE_RESULT_PATH, "raw");
483 if (_write_file(temp, g_test_decode[0].decoded, g_test_decode[0].decode_size) == FALSE) {
484 fprintf(stderr, "\tWrite the decoded result failed!\n");
492 gboolean test_encode()
495 image_util_encode_h encoded = NULL;
497 ret = image_util_encode_create(g_test_input.image_type, &encoded);
498 if (ret != IMAGE_UTIL_ERROR_NONE)
501 ret = image_util_encode_set_input_buffer(encoded, g_test_decode[0].decoded);
502 if (ret != IMAGE_UTIL_ERROR_NONE) {
503 image_util_encode_destroy(encoded);
507 ret = image_util_encode_set_resolution(encoded, g_test_decode[0].width, g_test_decode[0].height);
508 if (ret != IMAGE_UTIL_ERROR_NONE) {
509 image_util_encode_destroy(encoded);
513 if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC))
514 ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded);
516 ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path);
517 if (ret != IMAGE_UTIL_ERROR_NONE) {
518 image_util_encode_destroy(encoded);
522 if ((g_test_input.cmd == TEST_DECODE_ASYNC) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) {
523 ret = image_util_encode_run_async(encoded, (image_util_encode_completed_cb) encode_completed_cb, &g_test_encode);
524 if (ret == IMAGE_UTIL_ERROR_NONE)
527 ret = image_util_encode_run(encoded, &g_test_encode.encode_size);
529 if (ret != IMAGE_UTIL_ERROR_NONE) {
530 image_util_encode_destroy(encoded);
534 image_util_encode_destroy(encoded);
536 if ((g_test_input.cmd == TEST_DECODE_MEM) ||
537 (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) {
538 if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) {
539 fprintf(stderr, "\tWrite the encoded result failed!\n");
547 gboolean test_encode_gif()
551 image_util_encode_h encoded = NULL;
553 ret = image_util_encode_create(g_test_input.image_type, &encoded);
554 if (ret != IMAGE_UTIL_ERROR_NONE) {
555 image_util_encode_destroy(encoded);
559 for (i = 0; i < g_num_of_files; i++) {
560 if (g_test_decode[i].decode_result == FALSE)
563 ret = image_util_encode_set_input_buffer(encoded, g_test_decode[i].decoded);
564 if (ret != IMAGE_UTIL_ERROR_NONE)
567 ret = image_util_encode_set_resolution(encoded, g_test_decode[i].width, g_test_decode[i].height);
568 if (ret != IMAGE_UTIL_ERROR_NONE)
571 ret = image_util_encode_set_gif_frame_delay_time(encoded, 50); /* 500ms */
572 if (ret != IMAGE_UTIL_ERROR_NONE)
576 if (g_test_input.cmd == GIFTEST_ENCODE_MEM)
577 ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded);
579 ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path);
580 if (ret != IMAGE_UTIL_ERROR_NONE) {
581 image_util_encode_destroy(encoded);
585 ret = image_util_encode_run(encoded, &g_test_encode.encode_size);
586 if (ret != IMAGE_UTIL_ERROR_NONE) {
587 image_util_encode_destroy(encoded);
591 image_util_encode_destroy(encoded);
593 if (g_test_input.cmd == GIFTEST_ENCODE_MEM) {
594 if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) {
595 fprintf(stderr, "\tWrite the encoded result failed!\n");
603 gboolean test_encode_gif_frame_by_frame()
607 image_util_encode_h encoded = NULL;
609 ret = image_util_encode_create(g_test_input.image_type, &encoded);
610 if (ret != IMAGE_UTIL_ERROR_NONE)
613 if (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM)
614 ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded);
616 ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path);
617 if (ret != IMAGE_UTIL_ERROR_NONE) {
618 image_util_encode_destroy(encoded);
622 ret = image_util_encode_set_resolution(encoded, g_test_decode[0].width, g_test_decode[0].height);
623 if (ret != IMAGE_UTIL_ERROR_NONE) {
624 image_util_encode_destroy(encoded);
628 for (i = 0; i < g_num_of_files; i++) {
629 if (g_test_decode[i].decode_result == FALSE)
632 image_util_frame_h frame = NULL;
633 ret = image_util_frame_create(encoded, &frame);
634 if (ret != IMAGE_UTIL_ERROR_NONE)
637 ret = image_util_frame_set_frame(frame, g_test_decode[i].decoded);
638 if (ret != IMAGE_UTIL_ERROR_NONE) {
639 image_util_frame_destroy(frame);
643 ret = image_util_frame_set_resolution(frame, g_test_decode[i].width, g_test_decode[i].height);
644 if (ret != IMAGE_UTIL_ERROR_NONE) {
645 image_util_frame_destroy(frame);
649 ret = image_util_frame_set_gif_delay(frame, 50);
650 if (ret != IMAGE_UTIL_ERROR_NONE) {
651 image_util_frame_destroy(frame);
655 ret = image_util_encode_add_frame(encoded, frame);
656 if (ret != IMAGE_UTIL_ERROR_NONE) {
657 image_util_frame_destroy(frame);
660 image_util_frame_destroy(frame);
663 ret = image_util_encode_save(encoded, &g_test_encode.encode_size);
664 if (ret != IMAGE_UTIL_ERROR_NONE) {
665 image_util_encode_destroy(encoded);
669 image_util_encode_destroy(encoded);
671 if (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM) {
672 if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) {
673 fprintf(stderr, "\tWrite the encoded result failed!\n");
681 int main(int argc, char *argv[])
683 if (argc < ARGC_MIN) {
684 _print_help(argc, argv);
688 if (_init_datas() == FALSE) {
689 fprintf(stderr, "\tInit failed!\n");
694 if (_parse_inputs(argc, argv) == FALSE) {
695 fprintf(stderr, "\tInput was wrong!\n");
700 if (_read_test_files() == FALSE) {
701 fprintf(stderr, "\tCan not read the test files!\n");
706 fprintf(stderr, "\tTests Start!\n");
708 if (test_decode() == FALSE) {
709 fprintf(stderr, "\tDecode Tests failed!\n");
714 fprintf(stderr, "\tDecoding is done!\n");
716 if ((g_test_input.cmd == GIFTEST_ENCODE_FILE) || (g_test_input.cmd == GIFTEST_ENCODE_MEM)) {
717 if (test_encode_gif() == FALSE) {
718 fprintf(stderr, "\tEncode(gif) Tests failed!\n");
722 } else if ((g_test_input.cmd == GIFTEST_ENCODE_FRAME_FILE) || (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM)) {
723 if (test_encode_gif_frame_by_frame() == FALSE) {
724 fprintf(stderr, "\tEncode(gif frame by frame) Tests failed!\n");
729 if (test_encode() == FALSE) {
730 fprintf(stderr, "\tEncode(default) Tests failed!\n");
736 fprintf(stderr, "\tEncoding is done!\n");
738 fprintf(stderr, "\tTests Finished!\n");