5f28a771fc8f96f9dd80fa714c076af9123c302a
[platform/core/multimedia/libmm-utility.git] / webp / test / mm_util_webp_testsuite.c
1 /*
2 * Copyright (c) 2020 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <glib.h>
22
23 #include <mm_util_image.h>
24 #include <mm_util_magick.h>
25 #include <mm_util_webp.h>
26 #include <mm_util_private.h>
27 #include <tzplatform_config.h>
28
29
30 #define ENCODE_FILE_PATH        tzplatform_mkpath(TZ_USER_CONTENT, "webp_test_enc_file.webp")
31 #define ENCODE_MEM_PATH         tzplatform_mkpath(TZ_USER_CONTENT, "webp_test_enc_mem.webp")
32
33 static char *g_path = NULL;
34
35 /* for reading files */
36 static GQueue *g_queue_files = NULL;
37 static GQueue *g_queue_images = NULL;
38
39 static const mm_util_color_format_e g_color = MM_UTIL_COLOR_ARGB;
40 static const unsigned int g_delay_time = 500;  // 500ms
41 static const int g_loop_count = 0;  // if loop_count is 0, it is infinite loop
42 static uint8_t g_bgcolor[] = { 0xFF, 0x00, 0x00, 0x00 };  // black
43
44 static void __decode_func(gpointer data, gpointer user_data)
45 {
46         int ret = MM_UTIL_ERROR_NONE;
47         char *path = (char *)data;
48         mm_util_image_h image = NULL;
49
50         if (!MMUTIL_STRING_VALID(path)) {
51                 g_print("invalid path %s\n", path);
52                 return;
53         }
54
55         ret = mm_util_decode_image_from_file(path, g_color, &image);
56         if (ret != MM_UTIL_ERROR_NONE) {
57                 g_print("mm_util_decode_image_from_file failed %d\n", ret);
58                 return;
59         }
60
61         ret = mm_image_set_delay_time(image, g_delay_time);
62         if (ret != MM_UTIL_ERROR_NONE) {
63                 g_print("mm_image_set_delay_time failed %d\n", ret);
64                 return;
65         }
66
67         g_queue_push_tail(g_queue_images, image);
68 }
69
70 static gboolean __decode_files_in_queue()
71 {
72         if (g_queue_images)
73                 g_queue_free_full(g_queue_images, mm_image_destroy_image);
74
75         g_queue_images = g_queue_new();
76
77         g_queue_foreach(g_queue_files, __decode_func, NULL);
78
79         if (g_queue_images->length == 0) {
80                 g_print("No valid image\n");
81                 return FALSE;
82         }
83
84         g_print("%u valid image have been decoded.\n", g_queue_images->length);
85
86         return TRUE;
87 }
88
89 static int __sort_compare(gconstpointer a, gconstpointer b, gpointer user_data)
90 {
91         const char *_a = (const char *)a;
92         const char *_b = (const char *)b;
93         if (strlen(_a) < strlen(_b))
94                 return -1;
95
96         if (strlen(_a) > strlen(_b))
97                 return 1;
98
99         return g_ascii_strcasecmp(_a, _b);
100 }
101
102 static gboolean __set_input_dir(const char *path)
103 {
104         GDir *dir = NULL;
105         const gchar *filename = NULL;
106         GError *g_error = NULL;
107
108         dir = g_dir_open(path, 0, &g_error);
109         if (!dir) {
110                 g_print("invalid dir %s (%s)\n", path, g_error ? g_error->message : "none");
111                 g_error_free(g_error);
112                 return FALSE;
113         }
114
115         if (g_queue_files)
116                 g_queue_free_full(g_queue_files, g_free);
117
118         g_queue_files = g_queue_new();
119
120         /* push files of dir into queue */
121         while ((filename = g_dir_read_name(dir)) != NULL)
122                 g_queue_insert_sorted(g_queue_files, g_strdup_printf("%s/%s", path, filename), __sort_compare, NULL);
123
124         g_dir_close(dir);
125
126         if (g_queue_files->length == 0) {
127                 g_print("\tNo test file in directory(%s)!\n", path);
128                 return FALSE;
129         }
130
131         /* decode files of dir */
132         if (!__decode_files_in_queue()) {
133                 g_print("Fail to decode files from dir! %s\n", path);
134                 return FALSE;
135         }
136
137         return TRUE;
138 }
139
140 static void __print_help(const char *argv0)
141 {
142         fprintf(stderr, "\t[usage] Encode animation webp with images\n");
143         fprintf(stderr, "\t\t1. encode : %s directory(has numeric named files)\n", argv0);
144         fprintf(stderr, "\t\t2. support jpeg/png/gif/bmp/webp files to animation webp\n");
145 }
146
147 static gboolean __get_arguments(int argc, char *argv[])
148 {
149         g_path = g_strdup(argv[1]);
150
151         if (!MMUTIL_STRING_VALID(g_path)) {
152                 fprintf(stderr, "\t[WEBP_testsuite] invalid path %s\n", argv[1]);
153                 return FALSE;
154         }
155
156         if (!__set_input_dir(g_path)) {
157                 fprintf(stderr, "\t[WEBP_testsuite] __set_input_dir failed\n");
158                 return FALSE;
159         }
160
161         return TRUE;
162 }
163
164 static void __add_image(gpointer data, gpointer user_data)
165 {
166         int ret = 0;
167
168         ret = mm_util_webp_anim_enc_add_image((mm_util_webp_anim_enc_h)user_data, (mm_util_image_h)data);
169         if (ret != MM_UTIL_ERROR_NONE)
170                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_webp_anim_enc_add_image failed : %d\n", ret);
171 }
172
173 static int __webp_encode_setup(mm_util_webp_anim_enc_h *webp_encode)
174 {
175         int ret = 0;
176         mm_util_webp_anim_enc_h webp_enc = NULL;
177
178         mm_util_retvm_if(!webp_encode, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid webp_encode");
179
180         ret = mm_util_webp_anim_enc_create(&webp_enc);
181         if (ret != MM_UTIL_ERROR_NONE) {
182                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_webp_anim_enc_create failed : %d\n", ret);
183                 return ret;
184         }
185
186         ret = mm_util_webp_anim_enc_set_bgcolor(webp_enc, g_bgcolor[0], g_bgcolor[1], g_bgcolor[2], g_bgcolor[3]);
187         if (ret != MM_UTIL_ERROR_NONE) {
188                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_webp_anim_enc_set_bgcolor failed : %d\n", ret);
189                 goto END;
190         }
191
192         ret = mm_util_webp_anim_enc_set_loop_count(webp_enc, g_loop_count);
193         if (ret != MM_UTIL_ERROR_NONE) {
194                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_webp_anim_enc_set_loop_count failed : %d\n", ret);
195                 goto END;
196         }
197
198         *webp_encode = webp_enc;
199 END:
200         if (ret != MM_UTIL_ERROR_NONE)
201                 mm_util_webp_anim_enc_destroy(webp_enc);
202
203         return ret;
204 }
205
206 static gboolean __test_encode_to_file()
207 {
208         int ret = 0;
209         mm_util_webp_anim_enc_h anim_enc = NULL;
210
211         ret = __webp_encode_setup(&anim_enc);
212         if (ret != MM_UTIL_ERROR_NONE) {
213                 fprintf(stderr, "\t[WEBP_testsuite] __webp_encode_setup failed : %d\n", ret);
214                 return FALSE;
215         }
216
217         g_queue_foreach(g_queue_images, __add_image, anim_enc);
218
219         ret = mm_util_webp_anim_enc_save_to_file(anim_enc, ENCODE_FILE_PATH);
220         if (ret != MM_UTIL_ERROR_NONE) {
221                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_encode_webp_to_file failed : %d\n", ret);
222                 goto END;
223         }
224
225 END:
226         mm_util_webp_anim_enc_destroy(anim_enc);
227
228         return (ret == MM_UTIL_ERROR_NONE);
229 }
230
231 static gboolean __test_encode_to_buffer()
232 {
233         int ret = 0;
234         mm_util_webp_anim_enc_h anim_enc = NULL;
235         /* for encoding webp to memory */
236         void *encoded_data = NULL;
237         size_t encoded_size = 0;
238
239         ret = __webp_encode_setup(&anim_enc);
240         if (ret != MM_UTIL_ERROR_NONE) {
241                 fprintf(stderr, "\t[WEBP_testsuite] __webp_encode_setup failed : %d\n", ret);
242                 return FALSE;
243         }
244
245         g_queue_foreach(g_queue_images, __add_image, anim_enc);
246
247         ret = mm_util_webp_anim_enc_save_to_buffer(anim_enc, &encoded_data, &encoded_size);
248         if (ret != MM_UTIL_ERROR_NONE) {
249                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_webp_anim_enc_save_to_buffer failed : %d\n", ret);
250                 goto END;
251         }
252
253         ret = mm_util_file_write(ENCODE_MEM_PATH, encoded_data, encoded_size);
254         if (ret != MM_UTIL_ERROR_NONE)
255                 fprintf(stderr, "\t[WEBP_testsuite] mm_util_file_write failed : %d\n", ret);
256
257 END:
258         mm_util_webp_anim_enc_destroy(anim_enc);
259
260         g_free(encoded_data);
261
262         return (ret == MM_UTIL_ERROR_NONE);
263 }
264
265 int main(int argc, char *argv[])
266 {
267         if (argc < 2) {
268                 __print_help(argv[0]);
269                 return 0;
270         }
271
272         if (!__get_arguments(argc, argv)) {
273                 fprintf(stderr, "\t[WEBP_testsuite] _get_arguments failed\n");
274                 goto out;
275         }
276
277         /* test encoding animation webp to file */
278         if (!__test_encode_to_file()) {
279                 fprintf(stderr, "\t[WEBP_testsuite] __test_encode_to_file failed\n");
280                 goto out;
281         }
282
283         /* test encoding animation webp to buffer */
284         if (!__test_encode_to_buffer()) {
285                 fprintf(stderr, "\t[WEBP_testsuite] __test_encode_to_buffer failed\n");
286                 goto out;
287         }
288
289 out:
290         g_free(g_path);
291         g_queue_free_full(g_queue_images, mm_image_destroy_image);
292         g_queue_free_full(g_queue_files, g_free);
293
294         return 0;
295 }
296