Fix memory leak for the image_util_frame_h by using image_util_frame_create()
[platform/core/api/image-util.git] / decode-test / image_util_decode_encode_testsuite.c
1 /*
2  * image-utility
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Vineeth T M <vineeth.tm@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <image_util.h>
29 #include <image_util_internal.h>
30 #include <glib.h>
31
32 #define DECODE_RESULT_PATH "/home/owner/media/decode_test."
33 #define ENCODE_RESULT_PATH "/home/owner/media/encode_test."
34
35 #define ARGC_MIN 3
36 #define CMD_MAX 30
37 #define TEST_FILE_MAX 100
38
39 typedef enum {
40         TEST_DECODE_FILE = 0,
41         TEST_DECODE_MEM,
42         TEST_DECODE_ASYNC,
43         TEST_DECODE_MEM_ASYNC,
44         LAST_DECODE_TEST = TEST_DECODE_MEM_ASYNC,
45         FIRST_GIF_TEST,
46         GIFTEST_ENCODE_FILE = FIRST_GIF_TEST,
47         GIFTEST_ENCODE_MEM,
48         GIFTEST_ENCODE_FRAME_FILE,              /* interanl */
49         GIFTEST_ENCODE_FRAME_MEM,       /* interanl */
50         TEST_COMMAND_NUM,
51 } test_command_e;
52
53 typedef struct {
54         test_command_e cmd;
55         char *path;
56         unsigned int image_type;
57         void *buffer;
58         unsigned long long buffer_size;
59 } test_inputs_s;
60
61 typedef struct {
62         gboolean decode_result;
63         char filepath[PATH_MAX];
64         unsigned long width;
65         unsigned long height;
66         unsigned char *decoded;
67         unsigned long long decode_size;
68 } test_decode_s;
69
70 typedef struct {
71         char out_path[PATH_MAX];
72         unsigned char *encoded;
73         unsigned long long encode_size;
74 } test_encode_s;
75
76 static unsigned int g_num_of_files;
77 static unsigned int g_num_of_decoded;
78
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;
82
83 static char TEST_CMD[][CMD_MAX] = {
84         "decode",
85         "decode-mem",
86         "decode-async",
87         "decode-mem-async",
88         "encode-gif",
89         "encode-gif-mem",
90         "encode-gif-frame",                     /* internal */
91         "encode-gif-frame-mem", /* internal */
92 };
93
94 GCond g_thread_cond;
95 GMutex g_thread_mutex;
96
97 void _wait()
98 {
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);
104 }
105
106 void _signal()
107 {
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);
112 }
113
114 static inline void flush_stdin()
115 {
116         int ch;
117         while ((ch = getchar()) != EOF && ch != '\n') ;
118 }
119
120 static gboolean _read_file(char *file_name, void **data, unsigned long long *data_size)
121 {
122         FILE *fp = NULL;
123         long file_size = 0;
124
125         if (!file_name || !data || !data_size) {
126                 fprintf(stderr, "\tNULL pointer\n");
127                 return FALSE;
128         }
129
130         fprintf(stderr, "\tTry to open %s to read\n", file_name);
131
132         fp = fopen(file_name, "r");
133         if (fp == NULL) {
134                 fprintf(stderr, "\tfile open failed %d\n", errno);
135                 return FALSE;
136         }
137
138         fseek(fp, 0, SEEK_END);
139         file_size = ftell(fp);
140         if (file_size > -1L) {
141                 rewind(fp);
142                 *data = (void *)malloc(file_size);
143                 if (*data == NULL) {
144                         fprintf(stderr, "\tmalloc failed %d\n", errno);
145                         fclose(fp);
146                         fp = NULL;
147                         return FALSE;
148                 } else {
149                         if (fread(*data, 1, file_size, fp) == (size_t)file_size) {
150                                 fprintf(stderr, "#Success# fread\n");
151                         } else {
152                                 fprintf(stderr, "#Error# fread\n");
153                                 fclose(fp);
154                                 fp = NULL;
155                                 return FALSE;
156                         }
157                 }
158                 fclose(fp);
159                 fp = NULL;
160
161                 if (*data) {
162                         *data_size = (unsigned long long)file_size;
163                         return TRUE;
164                 } else {
165                         *data_size = 0;
166                         return FALSE;
167                 }
168         } else {
169                 fprintf(stderr, "#Error# ftell\n");
170                 fclose(fp);
171                 fp = NULL;
172                 return FALSE;
173         }
174 }
175
176 static gboolean _write_file(const char *file_name, void *data, unsigned long long data_size)
177 {
178         FILE *fp = NULL;
179
180         if (!file_name || !data || data_size <= 0) {
181                 fprintf(stderr, "\tinvalid data %s %p size:%lld\n", file_name, data, data_size);
182                 return FALSE;
183         }
184
185         fprintf(stderr, "\tTry to open %s to write\n", file_name);
186
187         fp = fopen(file_name, "w");
188         if (fp == NULL) {
189                 fprintf(stderr, "\tfile open failed %d\n", errno);
190                 return FALSE;
191         }
192
193         fwrite(data, 1, data_size, fp);
194         fclose(fp);
195         fp = NULL;
196
197         fprintf(stderr, "\tfile [%s] write DONE\n", file_name);
198
199         return TRUE;
200 }
201
202 bool decode_completed_cb(int error, void *user_param, unsigned long width, unsigned long height, unsigned long long size)
203 {
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;
208         _signal();
209
210         return TRUE;
211 }
212
213 bool encode_completed_cb(int error, void *user_param, unsigned long long size)
214 {
215         test_encode_s *user_data = (test_encode_s *)user_param;
216         user_data->encode_size = size;
217         _signal();
218
219         return TRUE;
220 }
221
222 void _free_inputs()
223 {
224         if (g_test_input.path != NULL) {
225                 g_free(g_test_input.path);
226                 g_test_input.path = NULL;
227         }
228         if (g_test_input.buffer != NULL) {
229                 g_free(g_test_input.buffer);
230                 g_test_input.buffer = NULL;
231         }
232 }
233
234 void _free_decode()
235 {
236         unsigned int i = 0;
237
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;
242                 }
243         }
244 }
245
246 void _free_encode()
247 {
248         if (g_test_encode.encoded != NULL) {
249                 free(g_test_encode.encoded);
250                 g_test_encode.encoded = NULL;
251         }
252 }
253
254 void _free_datas()
255 {
256         _free_inputs();
257         _free_decode();
258         _free_encode();
259 }
260
261 gboolean _init_datas()
262 {
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));
266
267         g_num_of_files = 0;
268         g_num_of_decoded = 0;
269
270         return TRUE;
271 }
272
273 gboolean _read_dir()
274 {
275         struct dirent *dp = NULL;
276         DIR *fd = opendir(g_test_input.\rpath);
277         unsigned int i = 0, j = 0;
278         if (fd == NULL) {
279                 fprintf(stderr, "\tlistdir: can't open %s\n", g_test_input.\rpath);
280                 return FALSE;
281         }
282
283         while ((dp = readdir(fd)) != NULL) {
284                 if (strlen(dp->d_name) == 0)
285                         continue;
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);
289                 if (nbytes == 0)
290                         continue;
291                 g_num_of_files++;
292         }
293
294         closedir(fd);
295
296         if (g_num_of_files == 0) {
297                 fprintf(stderr, "\tNo Test File!\n");
298                 return FALSE;
299         }
300
301         /* sort file_name */
302         char temp[PATH_MAX];
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));
311                         }
312                 }
313         }
314
315         return TRUE;
316 }
317
318 gboolean _parse_inputs(int argc, char *argv[])
319 {
320         unsigned int i = 0;
321         gboolean result = FALSE;
322
323         if (argv[1] == NULL || strlen(argv[1]) == 0) return FALSE;
324
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;
328                         result = TRUE;
329                 }
330         }
331         if (!result) return FALSE;
332
333         if (argv[2] == NULL || strlen(argv[2]) == 0) return FALSE;
334
335         g_test_input.path = g_strdup(argv[2]);
336         if (g_test_input.path == NULL)
337                 return FALSE;
338
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");
342                 if (nbytes == 0)
343                         return FALSE;
344                 return result;
345         }
346
347         if (argv[3] == NULL || strlen(argv[3]) == 0) return FALSE;
348
349         long temp = g_ascii_strtoll(argv[3], NULL, 10);
350         if (temp < 0 || temp > (long)IMAGE_UTIL_BMP)
351                 return FALSE;
352         g_test_input.image_type  = (unsigned int)temp;
353
354         char ext[4];
355         memset(g_test_encode.out_path, 0, PATH_MAX);
356         memset(ext, 0, 4);
357         switch (g_test_input.image_type) {
358         case IMAGE_UTIL_JPEG:
359                 snprintf(ext, 4, "%s", "jpg");
360                 break;
361         case IMAGE_UTIL_PNG:
362                 snprintf(ext, 4, "%s", "png");
363                 break;
364         case IMAGE_UTIL_GIF:
365                 snprintf(ext, 4, "%s", "gif");
366                 break;
367         case IMAGE_UTIL_BMP:
368                 snprintf(ext, 4, "%s", "bmp");
369                 break;
370         default:
371                 fprintf(stderr, "\tNot supported image type!\n");
372                 _free_datas();
373                 return FALSE;
374         }
375         snprintf(g_test_encode.out_path, PATH_MAX, "%s%s", ENCODE_RESULT_PATH, ext);
376
377         return result;
378 }
379
380 void _print_help(int argc, char *argv[])
381 {
382         unsigned int i = 0;
383
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, "/");
390         }
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, "/");
397         }
398         fprintf(stderr, " folderpath containing png images named \
399                                 with number prefix according to the animation order'\n");
400 }
401
402 gboolean _read_test_files()
403 {
404         if (g_test_input.cmd >= FIRST_GIF_TEST) {
405                 if (_read_dir() == FALSE)
406                         return FALSE;
407         } else {
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");
411                                 return FALSE;
412                         }
413                 } else {
414                         size_t nbytes = g_snprintf(g_test_decode[0].filepath, PATH_MAX, "%s", g_test_input.\rpath);
415                         if (nbytes == 0) return FALSE;
416                 }
417                 g_num_of_files = 1;
418         }
419
420         fprintf(stderr, "\tThe %d files are readed!\n", g_num_of_files);
421
422         return TRUE;
423 }
424
425 gboolean test_decode()
426 {
427         int ret = 0;
428         unsigned int i = 0;
429         image_util_decode_h decoded = NULL;
430
431         for (i = 0; i < g_num_of_files; i++) {
432                 g_test_decode[i].decode_result = FALSE;
433
434                 ret = image_util_decode_create(&decoded);
435                 if (ret != IMAGE_UTIL_ERROR_NONE)
436                         return FALSE;
437
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);
440                 else
441                         ret = image_util_decode_set_input_path(decoded, g_test_decode[i].filepath);
442
443                 if (ret != IMAGE_UTIL_ERROR_NONE) {
444                         image_util_decode_destroy(decoded);
445                         return FALSE;
446                 }
447
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);
451                         return FALSE;
452                 }
453
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)
457                                 _wait();
458                 } else {
459                         ret = image_util_decode_run(decoded, &g_test_decode[i].width, &g_test_decode[i].height, &g_test_decode[i].decode_size);
460                 }
461                 if (ret != IMAGE_UTIL_ERROR_NONE) {
462                         image_util_decode_destroy(decoded);
463                         return FALSE;
464                 }
465
466                 image_util_decode_destroy(decoded);
467                 g_test_decode[i].decode_result = TRUE;
468                 g_num_of_decoded++;
469         }
470
471         if (g_num_of_decoded == 0) {
472                 fprintf(stderr, "\tNo decoded data!\n");
473                 return FALSE;
474         } else {
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);
476         }
477
478         /* write the decoded result to the file */
479         if (g_test_input.cmd <= LAST_DECODE_TEST) {
480                 char temp[PATH_MAX];
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");
485                         return FALSE;
486                 }
487         }
488
489         return TRUE;
490 }
491
492 gboolean test_encode()
493 {
494         int ret = 0;
495         image_util_encode_h encoded = NULL;
496
497         ret = image_util_encode_create(g_test_input.image_type, &encoded);
498         if (ret != IMAGE_UTIL_ERROR_NONE)
499                 return FALSE;
500
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);
504                 return FALSE;
505         }
506
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);
510                 return FALSE;
511         }
512
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);
515         else
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);
519                 return FALSE;
520         }
521
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)
525                         _wait();
526         } else {
527                 ret = image_util_encode_run(encoded, &g_test_encode.encode_size);
528         }
529         if (ret != IMAGE_UTIL_ERROR_NONE) {
530                 image_util_encode_destroy(encoded);
531                 return FALSE;
532         }
533
534         image_util_encode_destroy(encoded);
535
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");
540                         return FALSE;
541                 }
542         }
543
544         return TRUE;
545 }
546
547 gboolean test_encode_gif()
548 {
549         int ret = 0;
550         unsigned int i = 0;
551         image_util_encode_h encoded = NULL;
552
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);
556                 return FALSE;
557         }
558
559         for (i = 0; i < g_num_of_files; i++) {
560                 if (g_test_decode[i].decode_result == FALSE)
561                         continue;
562
563                 ret = image_util_encode_set_input_buffer(encoded,  g_test_decode[i].decoded);
564                 if (ret != IMAGE_UTIL_ERROR_NONE)
565                         continue;
566
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)
569                         continue;
570
571                 ret = image_util_encode_set_gif_frame_delay_time(encoded,  50); /* 500ms */
572                 if (ret != IMAGE_UTIL_ERROR_NONE)
573                         continue;
574         }
575
576         if (g_test_input.cmd == GIFTEST_ENCODE_MEM)
577                 ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded);
578         else
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);
582                 return FALSE;
583         }
584
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);
588                 return FALSE;
589         }
590
591         image_util_encode_destroy(encoded);
592
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");
596                         return FALSE;
597                 }
598         }
599
600         return TRUE;
601 }
602
603 gboolean test_encode_gif_frame_by_frame()
604 {
605         int ret = 0;
606         unsigned int i = 0;
607         image_util_encode_h encoded = NULL;
608
609         ret = image_util_encode_create(g_test_input.image_type, &encoded);
610         if (ret != IMAGE_UTIL_ERROR_NONE)
611                 return FALSE;
612
613         if (g_test_input.cmd == GIFTEST_ENCODE_FRAME_MEM)
614                 ret = image_util_encode_set_output_buffer(encoded, &g_test_encode.encoded);
615         else
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);
619                 return FALSE;
620         }
621
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);
625                 return FALSE;
626         }
627
628         for (i = 0; i < g_num_of_files; i++) {
629                 if (g_test_decode[i].decode_result == FALSE)
630                         continue;
631
632                 image_util_frame_h frame = NULL;
633                 ret = image_util_frame_create(encoded, &frame);
634                 if (ret != IMAGE_UTIL_ERROR_NONE)
635                         continue;
636
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);
640                         continue;
641                 }
642
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);
646                         continue;
647                 }
648
649                 ret = image_util_frame_set_gif_delay(frame, 50);
650                 if (ret != IMAGE_UTIL_ERROR_NONE) {
651                         image_util_frame_destroy(frame);
652                         continue;
653                 }
654
655                 ret = image_util_encode_add_frame(encoded, frame);
656                 if (ret != IMAGE_UTIL_ERROR_NONE) {
657                         image_util_frame_destroy(frame);
658                         continue;
659                 }
660                 image_util_frame_destroy(frame);
661         }
662
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);
666                 return FALSE;
667         }
668
669         image_util_encode_destroy(encoded);
670
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");
674                         return FALSE;
675                 }
676         }
677
678         return TRUE;
679 }
680
681 int main(int argc, char *argv[])
682 {
683         if (argc < ARGC_MIN) {
684                 _print_help(argc, argv);
685                 return 0;
686         }
687
688         if (_init_datas() == FALSE) {
689                 fprintf(stderr, "\tInit failed!\n");
690                 _free_datas();
691                 return 0;
692         }
693
694         if (_parse_inputs(argc, argv) == FALSE) {
695                 fprintf(stderr, "\tInput was wrong!\n");
696                 _free_datas();
697                 return 0;
698         }
699
700         if (_read_test_files() == FALSE) {
701                 fprintf(stderr, "\tCan not read the test files!\n");
702                 _free_datas();
703                 return 0;
704         }
705
706         fprintf(stderr, "\tTests Start!\n");
707
708         if (test_decode() == FALSE) {
709                 fprintf(stderr, "\tDecode Tests failed!\n");
710                 _free_datas();
711                 return 0;
712         }
713
714         fprintf(stderr, "\tDecoding is done!\n");
715
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");
719                         _free_datas();
720                         return 0;
721                 }
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");
725                         _free_datas();
726                         return 0;
727                 }
728         } else {
729                 if (test_encode() == FALSE) {
730                         fprintf(stderr, "\tEncode(default) Tests failed!\n");
731                         _free_datas();
732                         return 0;
733                 }
734         }
735
736         fprintf(stderr, "\tEncoding is done!\n");
737
738         fprintf(stderr, "\tTests Finished!\n");
739
740         return 0;
741 }