Modify parameters in encoding APIs have been changed from mm_image_info_s to mm_util_...
[platform/core/multimedia/libmm-utility.git] / png / test / mm_util_png_testsuite.c
1 /*
2  * libmm-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 <glib.h>
27 #include <limits.h>
28 #include <mm_util_png.h>
29 #include <mm_util_image.h>
30 #include <tzplatform_config.h>
31
32 #define SAFE_FREE(x)            { if (x != NULL) { free(x); x = NULL; } }
33 #define SAFE_G_FREE(x)          { if (x != NULL) { g_free(x); x = NULL; } }
34 #define SAFE_IMAGE_FREE(x)      { if (x != NULL) { mm_image_destroy_image(x); x = NULL; } }
35
36 #define DECODE_FILE_PATH        tzplatform_mkpath(TZ_USER_CONTENT, "png_test_dec_file.raw")
37 #define DECODE_MEM_PATH         tzplatform_mkpath(TZ_USER_CONTENT, "png_test_dec_mem.raw")
38 #define ENCODE_FILE_PATH        tzplatform_mkpath(TZ_USER_CONTENT, "png_test_enc_file.png")
39 #define ENCODE_MEM_PATH         tzplatform_mkpath(TZ_USER_CONTENT, "png_test_enc_mem.png")
40
41 typedef enum {
42         TEST_AUTO,
43         TEST_DECODE_FILE,
44         TEST_DECODE_MEMORY,
45         TEST_ENCODE_FILE,
46         TEST_ENCODE_MEMORY,
47         TEST_NUM,
48 } png_test_mode_e;
49
50 static char *MODE_TO_STR[] = {
51         "AUTO",
52         "DECODE_FILE",
53         "DECODE_MEMORY",
54         "ENCODE_FILE",
55         "ENCODE_MEMORY",
56         "",
57 };
58
59 /* for arguments */
60 static int g_test_mode = 0;
61 static char *g_path = NULL;
62 static unsigned int g_width = 0;
63 static unsigned int g_height = 0;
64 static int g_compression = MM_UTIL_COMPRESSION_6;
65
66 /* for reading file */
67 static void *g_readed_data = NULL;
68 static size_t g_readed_size = 0;
69
70 static mm_util_image_h g_decoded_data = NULL;
71
72 static gboolean _read_file(char *path, void **data, size_t *length)
73 {
74         FILE *fp = NULL;
75         long len = 0;
76
77         if (!path || !data || length == 0) {
78                 fprintf(stderr, "\t[PNG_testsuite] invalid data %s %p %p\n", path, data, length);
79                 return FALSE;
80         }
81
82         fprintf(stderr, "\t[PNG_testsuite] %s read\n", path);
83
84         fp = fopen(path, "r");
85         if (fp == NULL) {
86                 fprintf(stderr, "\t[PNG_testsuite] fopen failed (%d) \n", errno);
87                 return FALSE;
88         }
89
90         if (fseek(fp, 0, SEEK_END) < 0) {
91                 fprintf(stderr, "\t[PNG_testsuite] fseek failed \n");
92                 fclose(fp);
93                 return FALSE;
94         }
95
96         len = ftell(fp);
97         if (len < 0) {
98                 fprintf(stderr, "\t[PNG_testsuite] ftell failed \n");
99                 fclose(fp);
100                 return FALSE;
101         }
102
103         rewind(fp);
104         *data = (void *)calloc(1, len);
105         if (*data == NULL) {
106                 fprintf(stderr, "\tmemory allocation failed \n");
107                 fclose(fp);
108                 return FALSE;
109         }
110
111         *length = fread(*data, 1, (size_t)len, fp);
112         if (*length != len) {
113                 fprintf(stderr, "\t[PNG_testsuite] fread failed \n");
114         }
115
116         fclose(fp);
117
118         if (*data == NULL) {
119                 *length = 0;
120                 return FALSE;
121         }
122
123         *length = (size_t)len;
124
125         fprintf(stderr, "\t[PNG_testsuite] %s %zu read DONE\n", path, *length);
126
127         return TRUE;
128 }
129
130 static gboolean _write_file(const char *path, void *data, size_t length)
131 {
132         FILE *fp = NULL;
133         size_t len = 0;
134
135         if (!path || !data || length == 0) {
136                 fprintf(stderr, "\t[PNG_testsuite] invalid data %s %p %zu\n", path, data, length);
137                 return FALSE;
138         }
139
140         fprintf(stderr, "\t[PNG_testsuite] %s %p %zu write\n", path, data, length);
141
142         fp = fopen(path, "w");
143         if (fp == NULL) {
144                 fprintf(stderr, "\t[PNG_testsuite] fopen failed (%d) \n", errno);
145                 return FALSE;
146         }
147
148         len = fwrite(data, 1, length, fp);
149         if (len != length) {
150                 fprintf(stderr, "\t[PNG_testsuite] fwrite failed \n");
151         }
152
153         fclose(fp);
154         fp = NULL;
155
156         fprintf(stderr, "\t[PNG_testsuite] %s write DONE\n", path);
157
158         return TRUE;
159 }
160
161 gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
162 {
163         if (argv == NULL || strlen(argv) == 0)
164                 return FALSE;
165
166         long temp = g_ascii_strtoll(argv, NULL, 10);
167         if (temp < min || temp > max)
168                 return FALSE;
169
170         *data  = (int)temp;
171
172         return TRUE;
173 }
174
175 void _print_help(const char *argv0)
176 {
177         fprintf(stderr, "\t[usage]\n");
178         fprintf(stderr, "\t\t1. decode & encode : %s mode path compression_level(opt.)\n", argv0);
179         fprintf(stderr, "\t\t2. decode : %s mode path\n", argv0);
180         fprintf(stderr, "\t\t3. encode : %s mode path width(mand.) height(mand.) compression_level(opt.)\n", argv0);
181         fprintf(stderr, "\t\t4. mode : 0 - auto, 1 - decode from file, 2 - decode from memory, 3 - encode to file, 4 - encode to memeory\n");
182 }
183
184 gboolean _get_arguments(int argc, char *argv[])
185 {
186         int width = 0, height = 0;
187
188         if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
189                 fprintf(stderr, "\t[PNG_testsuite] wrong mode(%s) for test\n", argv[1]);
190                 _print_help(argv[0]);
191                 return FALSE;
192         }
193
194         g_path = g_strdup(argv[2]);
195         if (g_path == NULL) {
196                 fprintf(stderr, "\t[PNG_testsuite] Not enough memory\n");
197                 return FALSE;
198         }
199
200         if (g_test_mode == TEST_AUTO) {
201                 if (FALSE == _get_input_data(argv[3], MM_UTIL_COMPRESSION_1, MM_UTIL_COMPRESSION_9, &g_compression))
202                         fprintf(stderr, "\t[PNG_testsuite] compression_level is default(%d)\n", g_compression);
203         } else if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
204                 /* do nothing */
205         } else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_MEMORY) {
206                 if (argc < 4) {
207                         fprintf(stderr, "\t[PNG_testsuite] not enough args\n");
208                         _print_help(argv[0]);
209                         return FALSE;
210                 }
211                 if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
212                         fprintf(stderr, "\t[PNG_testsuite] wrong width %s\n", argv[3]);
213                         return FALSE;
214                 }
215                 g_width = (unsigned int)width;
216                 if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
217                         fprintf(stderr, "\t[PNG_testsuite] wrong height %s\n", argv[4]);
218                         return FALSE;
219                 }
220                 g_height = (unsigned int)height;
221                 if (FALSE == _get_input_data(argv[5], MM_UTIL_COMPRESSION_1, MM_UTIL_COMPRESSION_9, &g_compression))
222                         fprintf(stderr, "\t[PNG_testsuite] wrong compression_level %s\n", argv[5]);
223         } else {
224                 fprintf(stderr, "\t[PNG_testsuite] wrong mode for test %s\n", argv[1]);
225                 return FALSE;
226         }
227
228         return TRUE;
229 }
230
231 gboolean _test_decode(const png_test_mode_e mode)
232 {
233         int ret = 0;
234         unsigned char *data = NULL;
235         size_t size = 0;
236
237         /* test decoding png */
238         if (mode == TEST_DECODE_FILE) {
239                 ret = mm_util_decode_from_png_file(g_path, &g_decoded_data);
240                 if (ret != MM_UTIL_ERROR_NONE) {
241                         fprintf(stderr, "\t[PNG_testsuite] mm_util_decode_from_png_file failed %d\n", ret);
242                         return FALSE;
243                 }
244                 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
245                 if (ret != MM_UTIL_ERROR_NONE) {
246                         fprintf(stderr, "\t[PNG_testsuite] mm_image_get_image failed %d\n", ret);
247                         return FALSE;
248                 }
249                 if (FALSE == _write_file(DECODE_FILE_PATH, data, size)) {
250                         fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", DECODE_FILE_PATH);
251                         SAFE_FREE(data);
252                         return FALSE;
253                 }
254                 SAFE_FREE(data);
255         } else if (mode == TEST_DECODE_MEMORY) {
256                 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
257                         fprintf(stderr, "\t[PNG_testsuite] reading file error\n");
258                         return FALSE;
259                 }
260
261                 ret = mm_util_decode_from_png_memory(g_readed_data, g_readed_size, &g_decoded_data);
262                 if (ret != MM_UTIL_ERROR_NONE) {
263                         fprintf(stderr, "\t[PNG_testsuite] mm_util_decode_from_png_memory failed %d\n", ret);
264                         return FALSE;
265                 }
266                 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
267                 if (ret != MM_UTIL_ERROR_NONE) {
268                         fprintf(stderr, "\t[PNG_testsuite] mm_image_get_image failed %d\n", ret);
269                         return FALSE;
270                 }
271                 if (FALSE == _write_file(DECODE_MEM_PATH, data, size)) {
272                         fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", DECODE_MEM_PATH);
273                         SAFE_FREE(data);
274                         return FALSE;
275                 }
276                 SAFE_FREE(data);
277         }
278
279         return TRUE;
280 }
281
282 gboolean _test_encode(const png_test_mode_e mode)
283 {
284         int ret = 0;
285         /* for encoding png to memory */
286         void *encoded_data = NULL;
287         size_t encoded_size = 0;
288
289         if ((mode != TEST_ENCODE_FILE) && (mode != TEST_ENCODE_MEMORY))
290                 return TRUE;
291
292         if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
293                 fprintf(stderr, "\t[PNG_testsuite] reading file error\n");
294                 return FALSE;
295         }
296         ret = mm_image_create_image(g_width, g_height, MM_UTIL_COLOR_RGBA, (unsigned char *)g_readed_data,
297                         g_readed_size, &g_decoded_data);
298         if (ret != MM_UTIL_ERROR_NONE) {
299                 fprintf(stderr, "\t[PNG_testsuite] mm_image_create_image failed : %d\n", ret);
300                 return FALSE;
301         }
302
303         /* test encoding png */
304         if (mode == TEST_ENCODE_FILE) {
305                 ret = mm_util_encode_to_png_file(g_decoded_data, g_compression, ENCODE_FILE_PATH);
306                 if (ret != MM_UTIL_ERROR_NONE) {
307                         fprintf(stderr, "\t[PNG_testsuite] mm_util_encode_to_png_file failed : %d\n", ret);
308                         return FALSE;
309                 }
310         } else if (mode == TEST_ENCODE_MEMORY) {
311                 ret = mm_util_encode_to_png_memory(g_decoded_data, g_compression, &encoded_data, &encoded_size);
312                 if (ret != MM_UTIL_ERROR_NONE) {
313                         fprintf(stderr, "\t[PNG_testsuite] mm_util_encode_to_png_memory failed : %d\n", ret);
314                         SAFE_FREE(encoded_data);
315                         return FALSE;
316                 }
317                 if (FALSE == _write_file(ENCODE_MEM_PATH, encoded_data, (size_t)encoded_size)) {
318                         fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", ENCODE_MEM_PATH);
319                         SAFE_FREE(encoded_data);
320                         return FALSE;
321                 }
322         }
323
324         SAFE_FREE(encoded_data);
325         return TRUE;
326 }
327
328 gboolean _test_auto()
329 {
330         png_test_mode_e test_mode = TEST_DECODE_FILE;
331
332         while (test_mode < TEST_NUM) {
333                 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST START\n", MODE_TO_STR[test_mode]);
334                 if (TEST_ENCODE_FILE == test_mode) {
335                         SAFE_G_FREE(g_path);
336                         g_path = g_strdup(DECODE_FILE_PATH);
337                         if (g_path == NULL) {
338                                 fprintf(stderr, "\t[PNG_testsuite] Not enough memory\n");
339                                 return FALSE;
340                         }
341                 }
342                 /* test decoding png */
343                 if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_MEMORY)) {
344                         if (FALSE == _test_decode(test_mode)) {
345                                 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
346                                 return FALSE;
347                         }
348                 }
349
350                 /* test encoding png */
351                 if ((test_mode == TEST_ENCODE_FILE) || (test_mode == TEST_ENCODE_MEMORY)) {
352                         if (FALSE == _test_encode(test_mode)) {
353                                 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
354                                 return FALSE;
355                         }
356                 }
357
358                 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
359
360                 SAFE_FREE(g_readed_data);
361                 SAFE_IMAGE_FREE(g_decoded_data);
362                 test_mode++;
363         }
364
365         return TRUE;
366 }
367
368 int main(int argc, char *argv[])
369 {
370         if (argc < 2) {
371                 _print_help(argv[0]);
372                 return 0;
373         }
374
375         if (FALSE == _get_arguments(argc, argv)) {
376                 fprintf(stderr, "\t[PNG_testsuite] _get_arguments failed\n");
377                 goto out;
378         }
379
380         /* test all functions automatically */
381         if (g_test_mode == TEST_AUTO) {
382                 if (FALSE == _test_auto())
383                         fprintf(stderr, "\t[PNG_testsuite] _test_auto failed\n");
384                 goto out;
385         }
386
387         /* test decoding png */
388         if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
389                 if (FALSE == _test_decode(g_test_mode)) {
390                         fprintf(stderr, "\t[PNG_testsuite] _test_decode(%s) failed\n", MODE_TO_STR[g_test_mode]);
391                         goto out;
392                 }
393         }
394
395         /* test encoding png */
396         if ((g_test_mode == TEST_ENCODE_FILE) || (g_test_mode == TEST_ENCODE_MEMORY)) {
397                 if (FALSE == _test_encode(g_test_mode)) {
398                         fprintf(stderr, "\t[PNG_testsuite] _test_encode(%s) failed\n", MODE_TO_STR[g_test_mode]);
399                         goto out;
400                 }
401         }
402
403 out:
404         SAFE_G_FREE(g_path);
405         SAFE_FREE(g_readed_data);
406         SAFE_IMAGE_FREE(g_decoded_data);
407
408         return 0;
409 }