From fd6ca8500121a4936c2df86732835261fdcd957a Mon Sep 17 00:00:00 2001 From: Jiyong Min Date: Thu, 24 Aug 2017 17:13:06 +0900 Subject: [PATCH] Decode/Encode test code refactoring & Add the testcase of internal API(encode-gif-frame command) Change-Id: Id3ecd348bcb1f1b5f98d143766d9178588322d55 Signed-off-by: Jiyong Min --- .../image_util_decode_encode_testsuite.c | 771 ++++++++++++------ 1 file changed, 532 insertions(+), 239 deletions(-) diff --git a/decode-test/image_util_decode_encode_testsuite.c b/decode-test/image_util_decode_encode_testsuite.c index 657b86a..bc00d56 100755 --- a/decode-test/image_util_decode_encode_testsuite.c +++ b/decode-test/image_util_decode_encode_testsuite.c @@ -26,13 +26,70 @@ #include #include #include +#include #include #define DECODE_RESULT_PATH "/home/owner/media/decode_test." -#define BUFFER_SIZE 128 - -unsigned long image_width = 0, image_height = 0; -unsigned long long image_size = 0; +#define ENCODE_RESULT_PATH "/home/owner/media/encode_test." + +#define ARGC_MIN 3 +#define CMD_MAX 30 +#define TEST_FILE_MAX 100 + +typedef enum { + TEST_DECODE_FILE = 0, + TEST_DECODE_MEM, + TEST_DECODE_ASYNC, + TEST_DECODE_MEM_ASYNC, + LAST_DECODE_TEST = TEST_DECODE_MEM_ASYNC, + FIRST_GIF_TEST, + GIFTEST_ENCODE_FILE = FIRST_GIF_TEST, + GIFTEST_ENCODE_MEM, + GIFTEST_ENCODE_FRAME_FILE, /* interanl */ + GIFTEST_ENCODE_FRAME_MEM, /* interanl */ + TEST_COMMAND_NUM, +} test_command_e; + +typedef struct { + test_command_e cmd; + char *path; + unsigned int image_type; + void *buffer; + unsigned long long buffer_size; +} test_inputs_s; + +typedef struct { + gboolean decode_result; + char filepath[PATH_MAX]; + unsigned long width; + unsigned long height; + unsigned char *decoded; + unsigned long long decode_size; +} test_decode_s; + +typedef struct { + char out_path[PATH_MAX]; + unsigned char *encoded; + unsigned long long encode_size; +} test_encode_s; + +static unsigned int g_num_of_files; +static unsigned int g_num_of_decoded; + +static test_inputs_s g_test_input; +static test_decode_s g_test_decode[TEST_FILE_MAX]; +static test_encode_s g_test_encode; + +static char TEST_CMD[][CMD_MAX] = { + "decode", + "decode-mem", + "decode-async", + "decode-mem-async", + "encode-gif", + "encode-gif-mem", + "encode-gif-frame", /* internal */ + "encode-gif-frame-mem", /* internal */ +}; GCond g_thread_cond; GMutex g_thread_mutex; @@ -60,7 +117,7 @@ static inline void flush_stdin() while ((ch = getchar()) != EOF && ch != '\n') ; } -static int _read_file(char *file_name, void **data, unsigned long long *data_size) +static gboolean _read_file(char *file_name, void **data, unsigned long long *data_size) { FILE *fp = NULL; long file_size = 0; @@ -116,7 +173,7 @@ static int _read_file(char *file_name, void **data, unsigned long long *data_siz } } -static int _write_file(const char *file_name, void *data, unsigned long long data_size) +static gboolean _write_file(const char *file_name, void *data, unsigned long long data_size) { FILE *fp = NULL; @@ -144,8 +201,10 @@ static int _write_file(const char *file_name, void *data, unsigned long long dat bool decode_completed_cb(int error, void *user_param, unsigned long width, unsigned long height, unsigned long long size) { - image_width = width; - image_height = height; + test_decode_s *user_data = (test_decode_s *)user_param; + user_data->width = width; + user_data->height = height; + user_data->decode_size = size; _signal(); return TRUE; @@ -153,297 +212,531 @@ bool decode_completed_cb(int error, void *user_param, unsigned long width, unsig bool encode_completed_cb(int error, void *user_param, unsigned long long size) { - image_size = size; + test_encode_s *user_data = (test_encode_s *)user_param; + user_data->encode_size = size; _signal(); return TRUE; } -int main(int argc, char *argv[]) +void _free_inputs() { - int ret = 0; - image_util_decode_h decoded = NULL; - image_util_encode_h encoded = NULL; - void *src = NULL; - unsigned char *data = NULL; - unsigned char *dst = NULL; - unsigned long long src_size = 0; - int encode_image_type = -1; - - if (argc < 3) { - fprintf(stderr, "\t[usage]\n"); - fprintf(stderr, "\t\t1. decode/encode : capi-media-image-util-decode-test decode/decode-mem/decode-async filepath encode_image_type\n"); - fprintf(stderr, "\t\t2. encode gif : capi-media-image-util-decode-test encode-gif/encode-gif-mem 'folderpath containing png images named \ - with number prefix according to the animation order'\n"); - return 0; + if (g_test_input.path != NULL) { + g_free(g_test_input.path); + g_test_input.path = NULL; } + if (g_test_input.buffer != NULL) { + g_free(g_test_input.buffer); + g_test_input.buffer = NULL; + } +} - if (!strcmp("encode-gif", argv[1]) || !strcmp("encode-gif-mem", argv[1])) { - struct dirent *dp; - DIR *fd; - int number_files = 0, i = 0; - char gif_filename[BUFFER_SIZE] = { 0, }, temp_filename[BUFFER_SIZE] = { - 0,}, temp[BUFFER_SIZE] = {0,}, file_format[BUFFER_SIZE] = {0,}; - unsigned long gif_image_width[1000] = { 0, }, gif_image_height[1000] = { - 0,}; - unsigned char *animated_data[1000] = {NULL, }; - memset(gif_filename, 0, BUFFER_SIZE); - snprintf(gif_filename, BUFFER_SIZE, "%s%s", DECODE_RESULT_PATH, "gif"); - - if ((fd = opendir(argv[2])) == NULL) { - fprintf(stderr, "listdir: can't open %s\n", argv[2]); - return 0; +void _free_decode() +{ + unsigned int i = 0; + + for (i = 0; i < g_num_of_files; i++) { + if (g_test_decode[i].decoded != NULL) { + free(g_test_decode[i].decoded); + g_test_decode[i].decoded = NULL; } + } +} + +void _free_encode() +{ + if (g_test_encode.encoded != NULL) { + free(g_test_encode.encoded); + g_test_encode.encoded = NULL; + } +} + +void _free_datas() +{ + _free_inputs(); + _free_decode(); + _free_encode(); +} + +gboolean _init_datas() +{ + memset(&g_test_input, 0, sizeof(test_inputs_s)); + memset(&g_test_decode, 0, sizeof(test_decode_s) * TEST_FILE_MAX); + memset(&g_test_encode, 0, sizeof(test_encode_s)); + + g_num_of_files = 0; + g_num_of_decoded = 0; + + return TRUE; +} + +gboolean _read_dir() +{ + struct dirent *dp = NULL; + DIR *fd = opendir(g_test_input. path); + unsigned int i = 0, j = 0; + if (fd == NULL) { + fprintf(stderr, "\tlistdir: can't open %s\n", g_test_input. path); + return FALSE; + } - while ((dp = readdir(fd)) != NULL) { - if (strlen(dp->d_name) == 0) - continue; - if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) - continue; /* skip self and parent */ - if (!number_files) { - char *temp1 = strstr(dp->d_name, "-"); - char *temp2 = strstr(dp->d_name, "."); - - if (temp1 == NULL || temp2 == NULL) { - fprintf(stderr, "\t\tCannot find test format\n"); - continue; - } - - strncpy(temp_filename, dp->d_name, strlen(dp->d_name) - strlen(temp1)); - strncpy(file_format, temp2, strlen(temp2)); + while ((dp = readdir(fd)) != NULL) { + if (strlen(dp->d_name) == 0) + continue; + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) + continue; /* skip self and parent */ + 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. path, dp->d_name); + if (nbytes == 0) + continue; + g_num_of_files++; + } + + closedir(fd); + + if (g_num_of_files == 0) { + fprintf(stderr, "\tNo Test File!\n"); + return FALSE; + } + + /* sort file_name */ + char temp[PATH_MAX]; + for (i = 0; i < g_num_of_files; i++) { + for (j = 0; j < g_num_of_files - 1; j++) { + if ((strlen(g_test_decode[j].filepath) > strlen(g_test_decode[j + 1].filepath)) || + (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))) { + memset(temp, 0, PATH_MAX); + g_strlcpy(temp, g_test_decode[j].filepath, sizeof(temp)); + g_strlcpy(g_test_decode[j].filepath, g_test_decode[j + 1].filepath, sizeof(g_test_decode[j].filepath)); + g_strlcpy(g_test_decode[j + 1].filepath, temp, sizeof(g_test_decode[j + 1].filepath)); } - number_files++; - } - closedir(fd); - if (!number_files) { - fprintf(stderr, "\t\tCannot open directory\n"); - return 0; } + } - ret = image_util_encode_create(IMAGE_UTIL_GIF, &encoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + return TRUE; +} - if (!strcmp("encode-gif-mem", argv[1])) { - ret = image_util_encode_set_output_buffer(encoded, &dst); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; - } else { - ret = image_util_encode_set_output_path(encoded, gif_filename); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; +gboolean _parse_inputs(int argc, char *argv[]) +{ + unsigned int i = 0; + gboolean result = FALSE; + + if (argv[1] == NULL || strlen(argv[1]) == 0) return FALSE; + + for (i = 0; i < TEST_COMMAND_NUM; i++) { + if (g_strcmp0(argv[1], TEST_CMD[i]) == 0) { + g_test_input.cmd = i; + result = TRUE; } + } + if (!result) return FALSE; - for (i = 0; i < number_files; i++) { - ret = image_util_decode_create(&decoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + if (argv[2] == NULL || strlen(argv[2]) == 0) return FALSE; - snprintf(temp, BUFFER_SIZE, "%s%s-%d%s", argv[2], temp_filename, i, file_format); + g_test_input.path = g_strdup(argv[2]); + if (g_test_input.path == NULL) + return FALSE; - ret = image_util_decode_set_input_path(decoded, temp); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } + if (g_test_input.cmd >= FIRST_GIF_TEST) { + g_test_input.image_type = IMAGE_UTIL_GIF; + size_t nbytes = g_snprintf(g_test_encode.out_path, PATH_MAX, "%s%s", ENCODE_RESULT_PATH, "gif"); + if (nbytes == 0) + return FALSE; + return result; + } - ret = image_util_decode_set_output_buffer(decoded, &animated_data[i]); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } + if (argv[3] == NULL || strlen(argv[3]) == 0) return FALSE; - ret = image_util_decode_run(decoded, &gif_image_width[i], &gif_image_height[i], NULL); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } + long temp = g_ascii_strtoll(argv[3], NULL, 10); + if (temp < 0 || temp > (long)IMAGE_UTIL_BMP) + return FALSE; + g_test_input.image_type = (unsigned int)temp; + + char ext[4]; + memset(g_test_encode.out_path, 0, PATH_MAX); + memset(ext, 0, 4); + switch (g_test_input.image_type) { + case IMAGE_UTIL_JPEG: + snprintf(ext, 4, "%s", "jpg"); + break; + case IMAGE_UTIL_PNG: + snprintf(ext, 4, "%s", "png"); + break; + case IMAGE_UTIL_GIF: + snprintf(ext, 4, "%s", "gif"); + break; + case IMAGE_UTIL_BMP: + snprintf(ext, 4, "%s", "bmp"); + break; + default: + fprintf(stderr, "\tNot supported image type!\n"); + _free_datas(); + return FALSE; + } + snprintf(g_test_encode.out_path, PATH_MAX, "%s%s", ENCODE_RESULT_PATH, ext); - ret = image_util_decode_destroy(decoded); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } + return result; +} - ret = image_util_encode_set_input_buffer(encoded, animated_data[i]); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } +void _print_help(int argc, char *argv[]) +{ + unsigned int i = 0; + + fprintf(stderr, "\t[usage]\n"); + fprintf(stderr, "\t\t1. decode & encode : %s ", argv[0]); + for (i = 0; i < LAST_DECODE_TEST; i++) { + fprintf(stderr, "%s", TEST_CMD[i]); + if (i != LAST_DECODE_TEST - 1) + fprintf(stderr, "/"); + } + fprintf(stderr, " filepath encode_image_type\n"); + fprintf(stderr, "\t\t2. encode animated-gif : %s ", argv[0]); + for (i = FIRST_GIF_TEST; i < TEST_COMMAND_NUM; i++) { + fprintf(stderr, "%s", TEST_CMD[i]); + if (i != TEST_COMMAND_NUM - 1) + fprintf(stderr, "/"); + } + fprintf(stderr, " folderpath containing png images named \ + with number prefix according to the animation order'\n"); +} - ret = image_util_encode_set_resolution(encoded, gif_image_width[i], gif_image_height[i]); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; +gboolean _read_test_files() +{ + if (g_test_input.cmd >= FIRST_GIF_TEST) { + if (_read_dir() == FALSE) + return FALSE; + } else { + if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) { + if (_read_file(g_test_input.path, &g_test_input.buffer, &g_test_input.buffer_size) == FALSE) { + fprintf(stderr, "\tRead test file failed!\n"); + return FALSE; } + } else { + size_t nbytes = g_snprintf(g_test_decode[0].filepath, PATH_MAX, "%s", g_test_input. path); + if (nbytes == 0) return FALSE; + } + g_num_of_files = 1; + } - ret = image_util_encode_set_gif_frame_delay_time(encoded, 10); - if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; - } + fprintf(stderr, "\tThe %d files are readed!\n", g_num_of_files); + + return TRUE; +} + +gboolean test_decode() +{ + int ret = 0; + unsigned int i = 0; + image_util_decode_h decoded = NULL; + + for (i = 0; i < g_num_of_files; i++) { + g_test_decode[i].decode_result = FALSE; + + ret = image_util_decode_create(&decoded); + if (ret != IMAGE_UTIL_ERROR_NONE) + return FALSE; + + if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) { + ret = image_util_decode_set_input_buffer(decoded, (unsigned char *)g_test_input.buffer, g_test_input.buffer_size); + } else { + ret = image_util_decode_set_input_path(decoded, g_test_decode[i].filepath); } - ret = image_util_encode_run(encoded, &image_size); if (ret != IMAGE_UTIL_ERROR_NONE) { - for (i = 0; i < number_files; i++) { - free(animated_data[i]); - } - return 0; + image_util_decode_destroy(decoded); + return FALSE; } - for (i = 0; i < number_files; i++) { - free(animated_data[i]); + ret = image_util_decode_set_output_buffer(decoded, &(g_test_decode[i].decoded)); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_decode_destroy(decoded); + return FALSE; } - ret = image_util_encode_destroy(encoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; - - if (!strcmp("encode-gif-mem", argv[1])) { - _write_file(gif_filename, (void *)dst, image_size); - free(dst); - dst = NULL; + if ((g_test_input.cmd == TEST_DECODE_ASYNC) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) { + ret = image_util_decode_run_async(decoded, (image_util_decode_completed_cb) decode_completed_cb, &g_test_decode[i]); + if (ret == IMAGE_UTIL_ERROR_NONE) + _wait(); + } else { + ret = image_util_decode_run(decoded, &g_test_decode[i].width, &g_test_decode[i].height, &g_test_decode[i].decode_size); } - return 0; - } else if (!strcmp("decode", argv[1]) || !strcmp("decode-mem", argv[1]) || !strcmp("decode-async", argv[1])) { - if (argc < 4) { - fprintf(stderr, "\t[usage]\n"); - fprintf(stderr, "\t\t1. decode/encode : capi-media-image-util-decode-test decode/decode-mem/decode-async filepath encode_image_type\n"); - return 0; + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_decode_destroy(decoded); + return FALSE; } - if (argv[3]) - encode_image_type = atoi(argv[3]); + image_util_decode_destroy(decoded); + g_test_decode[i].decode_result = TRUE; + g_num_of_decoded++; + } - ret = image_util_decode_create(&decoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + if (g_num_of_decoded == 0) { + fprintf(stderr, "\tNo decoded data!\n"); + return FALSE; + } else { + 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); + } - if (!strcmp("decode", argv[1])) { - ret = image_util_decode_set_input_path(decoded, argv[2]); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; - } else { - if (_read_file(argv[2], &src, &src_size)) { - ret = image_util_decode_set_input_buffer(decoded, (unsigned char *)src, src_size); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; - } else - return 0; + /* write the decoded result to the file */ + if (g_test_input.cmd <= LAST_DECODE_TEST) { + char temp[PATH_MAX]; + memset(temp, 0, PATH_MAX); + snprintf(temp, PATH_MAX, "%s%s", DECODE_RESULT_PATH, "raw"); + if (_write_file(temp, g_test_decode[0].decoded, g_test_decode[0].decode_size) == FALSE) { + fprintf(stderr, "\tWrite the decoded result failed!\n"); + return FALSE; } - ret = image_util_decode_set_output_buffer(decoded, &data); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + } - if (!strcmp("decode-async", argv[1])) { - ret = image_util_decode_run_async(decoded, (image_util_decode_completed_cb) decode_completed_cb, NULL); - _wait(); - } else - ret = image_util_decode_run(decoded, &image_width, &image_height, &image_size); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + return TRUE; +} - free(src); - ret = image_util_decode_destroy(decoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; +gboolean test_encode() +{ + int ret = 0; + image_util_encode_h encoded = NULL; + + ret = image_util_encode_create(g_test_input.image_type, &encoded); + if (ret != IMAGE_UTIL_ERROR_NONE) + return FALSE; + + ret = image_util_encode_set_input_buffer(encoded, g_test_decode[0].decoded); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + ret = image_util_encode_set_resolution(encoded, g_test_decode[0].width, g_test_decode[0].height); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + if ((g_test_input.cmd == TEST_DECODE_MEM) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) + ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded); + else + ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + if ((g_test_input.cmd == TEST_DECODE_ASYNC) || (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) { + ret = image_util_encode_run_async(encoded, (image_util_encode_completed_cb) encode_completed_cb, &g_test_encode); + if (ret == IMAGE_UTIL_ERROR_NONE) + _wait(); } else { - fprintf(stderr, "\tunknown command [%s]\n", argv[1]); - return 0; + ret = image_util_encode_run(encoded, &g_test_encode.encode_size); + } + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; } - fprintf(stderr, "\tIMAGE OPERATION SUCCESS\n"); - if (data) { - fprintf(stderr, "\t##Decoded data##: %p\t width: %lu\t height:%lu\n", data, (long unsigned int)image_width, (long unsigned int)image_height); - char filename[BUFFER_SIZE] = { 0, }, type[4] = { - 0,}; - memset(filename, 0, BUFFER_SIZE); - - snprintf(filename, BUFFER_SIZE, "%s%s", DECODE_RESULT_PATH, "raw"); - _write_file(filename, (void *)data, image_size); - memset(filename, 0, BUFFER_SIZE); - - switch (encode_image_type) { - case IMAGE_UTIL_JPEG: - snprintf(type, 4, "%s", "jpg"); - break; - case IMAGE_UTIL_PNG: - snprintf(type, 4, "%s", "png"); - break; - case IMAGE_UTIL_GIF: - snprintf(type, 4, "%s", "gif"); - break; - case IMAGE_UTIL_BMP: - snprintf(type, 4, "%s", "bmp"); - break; - default: - break; + image_util_encode_destroy(encoded); + + if ((g_test_input.cmd == TEST_DECODE_MEM) || + (g_test_input.cmd == TEST_DECODE_MEM_ASYNC)) { + if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) { + fprintf(stderr, "\tWrite the encoded result failed!\n"); + return FALSE; } - snprintf(filename, BUFFER_SIZE, "%s%s", DECODE_RESULT_PATH, type); + } + + return TRUE; +} + +gboolean test_encode_gif() +{ + int ret = 0; + unsigned int i = 0; + image_util_encode_h encoded = NULL; - ret = image_util_encode_create(encode_image_type, &encoded); + ret = image_util_encode_create(g_test_input.image_type, &encoded); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + for (i = 0; i < g_num_of_files; i++) { + if (g_test_decode[i].decode_result == FALSE) + continue; + + ret = image_util_encode_set_input_buffer(encoded, g_test_decode[i].decoded); if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + continue; - ret = image_util_encode_set_resolution(encoded, image_width, image_height); + ret = image_util_encode_set_resolution(encoded, g_test_decode[i].width, g_test_decode[i].height); if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + continue; + + ret = image_util_encode_set_gif_frame_delay_time(encoded, 50); /* 500ms */ + if (ret != IMAGE_UTIL_ERROR_NONE) + continue; + } + + if (g_test_input.cmd == GIFTEST_ENCODE_MEM) + ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded); + else + ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + ret = image_util_encode_run(encoded, &g_test_encode.encode_size); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } - if (encode_image_type == IMAGE_UTIL_JPEG) { - ret = image_util_encode_set_quality(encoded, 100); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + image_util_encode_destroy(encoded); + + if (g_test_input.cmd == GIFTEST_ENCODE_MEM) { + if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) { + fprintf(stderr, "\tWrite the encoded result failed!\n"); + return FALSE; } + } + + return TRUE; +} + +gboolean test_encode_gif_frame_by_frame() +{ + int ret = 0; + unsigned int i = 0; + image_util_encode_h encoded = NULL; - ret = image_util_encode_set_input_buffer(encoded, data); + ret = image_util_encode_create(g_test_input.image_type, &encoded); + if (ret != IMAGE_UTIL_ERROR_NONE) + return FALSE; + + if (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM) + ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded); + else + ret = image_util_encode_set_output_path(encoded, g_test_encode.out_path); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + ret = image_util_encode_set_resolution(encoded, g_test_decode[0].width, g_test_decode[0].height); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + for (i = 0; i < g_num_of_files; i++) { + if (g_test_decode[i].decode_result == FALSE) + continue; + + image_util_frame_h frame = NULL; + ret = image_util_frame_create(encoded, &frame); if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + continue; - if (!strcmp("decode-mem", argv[1])) { - ret = image_util_encode_set_output_buffer(encoded, &dst); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; - } else { - ret = image_util_encode_set_output_path(encoded, filename); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + ret = image_util_frame_set_frame(frame, g_test_decode[i].decoded, g_test_decode[i].decode_size); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_frame_destroy(frame); + continue; } - if (!strcmp("decode-async", argv[1])) { - ret = image_util_encode_run_async(encoded, (image_util_encode_completed_cb) encode_completed_cb, NULL); - _wait(); - } else - ret = image_util_encode_run(encoded, &image_size); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + ret = image_util_frame_set_resolution(frame, g_test_decode[i].width, g_test_decode[i].height); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_frame_destroy(frame); + continue; + } - ret = image_util_encode_destroy(encoded); - if (ret != IMAGE_UTIL_ERROR_NONE) - return 0; + ret = image_util_frame_set_gif_delay(frame, 50); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_frame_destroy(frame); + continue; + } + + ret = image_util_encode_add_frame(encoded, frame); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_frame_destroy(frame); + continue; + } + image_util_frame_destroy(frame); + } + + ret = image_util_encode_save(encoded, &g_test_encode.encode_size); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_encode_destroy(encoded); + return FALSE; + } + + image_util_encode_destroy(encoded); + + if (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM) { + if (_write_file(g_test_encode.out_path, g_test_encode.encoded, g_test_encode.encode_size) == FALSE) { + fprintf(stderr, "\tWrite the encoded result failed!\n"); + return FALSE; + } + } - if (!strcmp("decode-mem", argv[1])) { - _write_file(filename, (void *)dst, image_size); - free(dst); + return TRUE; +} + +int main(int argc, char *argv[]) +{ + if (argc < ARGC_MIN) { + _print_help(argc, argv); + return 0; + } + + if (_init_datas() == FALSE) { + fprintf(stderr, "\tInit failed!\n"); + _free_datas(); + return 0; + } + + if (_parse_inputs(argc, argv) == FALSE) { + fprintf(stderr, "\tInput was wrong!\n"); + _free_datas(); + return 0; + } + + if (_read_test_files() == FALSE) { + fprintf(stderr, "\tCan not read the test files!\n"); + _free_datas(); + return 0; + } + + fprintf(stderr, "\tTests Start!\n"); + + if (test_decode() == FALSE) { + fprintf(stderr, "\tDecode Tests failed!\n"); + _free_datas(); + return 0; + } + + fprintf(stderr, "\tDecoding is done!\n"); + + if ((g_test_input.cmd == GIFTEST_ENCODE_FILE) || (g_test_input.cmd == GIFTEST_ENCODE_MEM)) { + if (test_encode_gif() == FALSE) { + fprintf(stderr, "\tEncode(gif) Tests failed!\n"); + _free_datas(); + return 0; + } + } else if ((g_test_input.cmd == GIFTEST_ENCODE_FRAME_FILE) || (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM)) { + if (test_encode_gif_frame_by_frame() == FALSE) { + fprintf(stderr, "\tEncode(gif frame by frame) Tests failed!\n"); + _free_datas(); + return 0; } - free(data); } else { - fprintf(stderr, "\tDECODED data is NULL\n"); + if (test_encode() == FALSE) { + fprintf(stderr, "\tEncode(default) Tests failed!\n"); + _free_datas(); + return 0; + } } + fprintf(stderr, "\tEncoding is done!\n"); + + fprintf(stderr, "\tTests Finished!\n"); + return 0; } -- 2.34.1