Modify parameters in encoding APIs have been changed from mm_image_info_s to mm_util_...
[platform/core/multimedia/libmm-utility.git] / jpeg / test / mm_util_jpeg_testsuite.c
1 /*
2  * libmm-utility
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@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_jpeg.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, "jpeg_test_dec_file.raw")
37 #define DECODE_MEM_PATH         tzplatform_mkpath(TZ_USER_CONTENT, "jpeg_test_dec_mem.raw")
38 #define ENCODE_FILE_PATH        tzplatform_mkpath(TZ_USER_CONTENT, "jpeg_test_enc_file.jpg")
39 #define ENCODE_MEM_PATH         tzplatform_mkpath(TZ_USER_CONTENT, "jpeg_test_enc_mem.jpg")
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 } jpeg_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_color = MM_UTIL_COLOR_RGB24;
65 static int g_quality = 75;
66 static int g_downscale = MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1;
67
68 /* for reading file */
69 static void *g_readed_data = NULL;
70 static size_t g_readed_size = 0;
71
72 static mm_util_image_h g_decoded_data = NULL;
73
74 static gboolean _read_file(char *path, void **data, size_t *length)
75 {
76         FILE *fp = NULL;
77         long len = 0;
78
79         if (!path || !data || length == 0) {
80                 fprintf(stderr, "\t[JPEG_testsuite] invalid data %s %p %p\n", path, data, length);
81                 return FALSE;
82         }
83
84         fprintf(stderr, "\t[JPEG_testsuite] %s read\n", path);
85
86         fp = fopen(path, "r");
87         if (fp == NULL) {
88                 fprintf(stderr, "\t[JPEG_testsuite] fopen failed (%d) \n", errno);
89                 return FALSE;
90         }
91
92         if (fseek(fp, 0, SEEK_END) < 0) {
93                 fprintf(stderr, "\t[JPEG_testsuite] fseek failed \n");
94                 fclose(fp);
95                 return FALSE;
96         }
97
98         len = ftell(fp);
99         if (len < 0) {
100                 fprintf(stderr, "\t[JPEG_testsuite] ftell failed \n");
101                 fclose(fp);
102                 return FALSE;
103         }
104
105         rewind(fp);
106         *data = (void *)calloc(1, len);
107         if (*data == NULL) {
108                 fprintf(stderr, "\tmemory allocation failed \n");
109                 fclose(fp);
110                 return FALSE;
111         }
112
113         *length = fread(*data, 1, (size_t)len, fp);
114         if (*length != len) {
115                 fprintf(stderr, "\t[JPEG_testsuite] fread failed \n");
116         }
117
118         fclose(fp);
119
120         if (*data == NULL) {
121                 *length = 0;
122                 return FALSE;
123         }
124
125         *length = (size_t)len;
126
127         fprintf(stderr, "\t[JPEG_testsuite] %s %zu read DONE\n", path, *length);
128
129         return TRUE;
130 }
131
132 static gboolean _write_file(const char *path, void *data, size_t length)
133 {
134         FILE *fp = NULL;
135         size_t len = 0;
136
137         if (!path || !data || length == 0) {
138                 fprintf(stderr, "\t[JPEG_testsuite] invalid data %s %p %zu\n", path, data, length);
139                 return FALSE;
140         }
141
142         fprintf(stderr, "\t[JPEG_testsuite] %s %p %zu write\n", path, data, length);
143
144         fp = fopen(path, "w");
145         if (fp == NULL) {
146                 fprintf(stderr, "\t[JPEG_testsuite] fopen failed (%d) \n", errno);
147                 return FALSE;
148         }
149
150         len = fwrite(data, 1, length, fp);
151         if (len != length) {
152                 fprintf(stderr, "\t[JPEG_testsuite] fwrite failed \n");
153         }
154
155         fclose(fp);
156         fp = NULL;
157
158         fprintf(stderr, "\t[JPEG_testsuite] %s write DONE\n", path);
159
160         return TRUE;
161 }
162
163 gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
164 {
165         if (argv == NULL || strlen(argv) == 0)
166                 return FALSE;
167
168         long temp = g_ascii_strtoll(argv, NULL, 10);
169         if (temp < min || temp > max)
170                 return FALSE;
171
172         *data  = (int)temp;
173
174         return TRUE;
175 }
176
177 void _print_help(const char *argv0)
178 {
179         fprintf(stderr, "\t[usage]\n");
180         fprintf(stderr, "\t\t1. decode & encode : %s mode path color_format(opt.) quality(opt.) downscale(opt.)\n", argv0);
181         fprintf(stderr, "\t\t2. decode : %s mode path color_format(opt.)\n", argv0);
182         fprintf(stderr, "\t\t3. encode : %s mode path width(mand.) height(mand.) color_format(mand.) quality(opt.) downscale(opt.)\n", argv0);
183         fprintf(stderr, "\t\t4. mode : 0 - auto, 1 - decode from file, 2 - decode from memory, 3 - encode to file, 4 - encode to memeory\n");
184 }
185
186 gboolean _get_arguments(int argc, char *argv[])
187 {
188         int width = 0, height = 0;
189
190         if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
191                 fprintf(stderr, "\t[JPEG_testsuite] wrong mode(%s) for test\n", argv[1]);
192                 _print_help(argv[0]);
193                 return FALSE;
194         }
195
196         g_path = g_strdup(argv[2]);
197         if (g_path == NULL) {
198                 fprintf(stderr, "\t[JPEG_testsuite] Not enough memory\n");
199                 return FALSE;
200         }
201
202         if (g_test_mode == TEST_AUTO) {
203                 if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
204                         fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
205                 if (FALSE == _get_input_data(argv[4], 1, 100, &g_quality))
206                         fprintf(stderr, "\t[JPEG_testsuite] quality is default(%d)\n", g_quality);
207                 /* min: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1, max: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8 */
208                 if (FALSE == _get_input_data(argv[5], 1, 8, &g_downscale))
209                         fprintf(stderr, "\t[JPEG_testsuite] downscale is default(%d)\n", g_downscale);
210         } else if (g_test_mode == TEST_DECODE_FILE) {
211                 if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
212                         fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
213         } else if (g_test_mode == TEST_DECODE_MEMORY) {
214                 if (FALSE == _get_input_data(argv[3], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color))
215                         fprintf(stderr, "\t[JPEG_testsuite] color is default(%d)\n", g_color);
216         } else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_MEMORY) {
217                 if (argc < 5) {
218                         fprintf(stderr, "\t[JPEG_testsuite] not enough args\n");
219                         _print_help(argv[0]);
220                         return FALSE;
221                 }
222                 if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
223                         fprintf(stderr, "\t[JPEG_testsuite] wrong width %s\n", argv[3]);
224                         return FALSE;
225                 }
226                 g_width = (unsigned int)width;
227                 if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
228                         fprintf(stderr, "\t[JPEG_testsuite] wrong height %s\n", argv[4]);
229                         return FALSE;
230                 }
231                 g_height = (unsigned int)height;
232                 if (FALSE == _get_input_data(argv[5], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_color)) {
233                         fprintf(stderr, "\t[JPEG_testsuite] wrong color %s\n", argv[5]);
234                         return FALSE;
235                 }
236                 if (FALSE == _get_input_data(argv[6], 1, 100, &g_quality))
237                         fprintf(stderr, "\t[JPEG_testsuite] quality is default(%d)\n", g_quality);
238
239                 /* min: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1, max: MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8 */
240                 if (FALSE == _get_input_data(argv[7], 1, 8, &g_downscale))
241                         fprintf(stderr, "\t[JPEG_testsuite] downscale is default(%d)\n", g_downscale);
242         } else {
243                 fprintf(stderr, "\t[JPEG_testsuite] wrong mode for test %s\n", argv[1]);
244                 return FALSE;
245         }
246
247         return TRUE;
248 }
249
250 gboolean _test_decode(const jpeg_test_mode_e mode)
251 {
252         int ret = 0;
253         unsigned char *data = NULL;
254         size_t size = 0;
255
256         /* test decoding jpeg */
257         if (mode == TEST_DECODE_FILE) {
258                 ret = mm_util_decode_from_jpeg_file(g_path, g_color, g_downscale, &g_decoded_data);
259                 if (ret != MM_UTIL_ERROR_NONE) {
260                         fprintf(stderr, "\t[JPEG_testsuite] mm_util_decode_from_jpeg_file failed %d\n", ret);
261                         return FALSE;
262                 }
263                 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
264                 if (ret != MM_UTIL_ERROR_NONE) {
265                         fprintf(stderr, "\t[JPEG_testsuite] mm_image_get_image failed %d\n", ret);
266                         return FALSE;
267                 }
268                 if (FALSE == _write_file(DECODE_FILE_PATH, data, size)) {
269                         fprintf(stderr, "\t[JPEG_testsuite] writing decoded data failed : %s\n", DECODE_FILE_PATH);
270                         SAFE_FREE(data);
271                         return FALSE;
272                 }
273                 SAFE_FREE(data);
274         } else if (mode == TEST_DECODE_MEMORY) {
275                 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
276                         fprintf(stderr, "\t[JPEG_testsuite] reading file error\n");
277                         return FALSE;
278                 }
279
280                 ret = mm_util_decode_from_jpeg_memory(g_readed_data, g_readed_size, g_color, g_downscale, &g_decoded_data);
281                 if (ret != MM_UTIL_ERROR_NONE) {
282                         fprintf(stderr, "\t[JPEG_testsuite] mm_util_decode_from_jpeg_memory failed %d\n", ret);
283                         return FALSE;
284                 }
285                 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
286                 if (ret != MM_UTIL_ERROR_NONE) {
287                         fprintf(stderr, "\t[JPEG_testsuite] mm_image_get_image failed %d\n", ret);
288                         return FALSE;
289                 }
290                 if (FALSE == _write_file(DECODE_MEM_PATH, data, size)) {
291                         fprintf(stderr, "\t[JPEG_testsuite] writing decoded data failed : %s\n", DECODE_MEM_PATH);
292                         SAFE_FREE(data);
293                         return FALSE;
294                 }
295                 SAFE_FREE(data);
296         }
297
298         return TRUE;
299 }
300
301 gboolean _test_encode(const jpeg_test_mode_e mode)
302 {
303         int ret = 0;
304         /* for encoding jpeg to memory */
305         void *encoded_data = NULL;
306         size_t encoded_size = 0;
307
308         if ((mode != TEST_ENCODE_FILE) && (mode != TEST_ENCODE_MEMORY))
309                 return TRUE;
310
311         if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
312                 fprintf(stderr, "\t[JPEG_testsuite] reading file error\n");
313                 return FALSE;
314         }
315         ret = mm_image_create_image(g_width, g_height, g_color, (unsigned char *)g_readed_data,
316                         g_readed_size, &g_decoded_data);
317         if (ret != MM_UTIL_ERROR_NONE) {
318                 fprintf(stderr, "\t[JPEG_testsuite] mm_image_create_image failed : %d\n", ret);
319                 return FALSE;
320         }
321
322         /* test encoding jpeg */
323         if (mode == TEST_ENCODE_FILE) {
324                 ret = mm_util_jpeg_encode_to_file(g_decoded_data, g_quality, ENCODE_FILE_PATH);
325                 if (ret != MM_UTIL_ERROR_NONE) {
326                         fprintf(stderr, "\t[JPEG_testsuite] mm_util_jpeg_encode_to_file failed : %d\n", ret);
327                         return FALSE;
328                 }
329         } else if (mode == TEST_ENCODE_MEMORY) {
330                 ret = mm_util_encode_to_jpeg_memory(g_decoded_data, g_quality, &encoded_data, &encoded_size);
331                 if (ret != MM_UTIL_ERROR_NONE) {
332                         fprintf(stderr, "\t[JPEG_testsuite] mm_util_jpeg_encode_to_file failed : %d\n", ret);
333                         SAFE_FREE(encoded_data);
334                         return FALSE;
335                 }
336                 if (FALSE == _write_file(ENCODE_MEM_PATH, encoded_data, encoded_size)) {
337                         fprintf(stderr, "\t[JPEG_testsuite] writing decoded data failed : %s\n", ENCODE_MEM_PATH);
338                         SAFE_FREE(encoded_data);
339                         return FALSE;
340                 }
341         }
342
343         SAFE_FREE(encoded_data);
344         return TRUE;
345 }
346
347 gboolean _test_auto()
348 {
349         jpeg_test_mode_e test_mode = TEST_DECODE_FILE;
350
351         while (test_mode < TEST_NUM) {
352                 fprintf(stderr, "\t[JPEG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST START\n", MODE_TO_STR[test_mode]);
353                 if (TEST_ENCODE_FILE == test_mode) {
354                         SAFE_G_FREE(g_path);
355                         g_path = g_strdup(DECODE_FILE_PATH);
356                         if (g_path == NULL) {
357                                 fprintf(stderr, "\t[JPEG_testsuite] Not enough memory\n");
358                                 return FALSE;
359                         }
360                 }
361                 /* test decoding jpeg */
362                 if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_MEMORY)) {
363                         if (FALSE == _test_decode(test_mode)) {
364                                 fprintf(stderr, "\t[JPEG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
365                                 return FALSE;
366                         }
367                 }
368
369                 /* test encoding jpeg */
370                 if ((test_mode == TEST_ENCODE_FILE) || (test_mode == TEST_ENCODE_MEMORY)) {
371                         if (FALSE == _test_encode(test_mode)) {
372                                 fprintf(stderr, "\t[JPEG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
373                                 return FALSE;
374                         }
375                 }
376
377                 fprintf(stderr, "\t[JPEG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
378
379                 SAFE_FREE(g_readed_data);
380                 SAFE_IMAGE_FREE(g_decoded_data);
381                 test_mode++;
382         }
383
384         return TRUE;
385 }
386
387 int main(int argc, char *argv[])
388 {
389         if (argc < 2) {
390                 _print_help(argv[0]);
391                 return 0;
392         }
393
394         if (FALSE == _get_arguments(argc, argv)) {
395                 fprintf(stderr, "\t[JPEG_testsuite] _get_arguments failed\n");
396                 goto out;
397         }
398
399         /* test all functions automatically */
400         if (g_test_mode == TEST_AUTO) {
401                 if (FALSE == _test_auto())
402                         fprintf(stderr, "\t[JPEG_testsuite] _test_auto failed\n");
403                 goto out;
404         }
405
406         /* test decoding jpeg */
407         if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
408                 if (FALSE == _test_decode(g_test_mode)) {
409                         fprintf(stderr, "\t[JPEG_testsuite] _test_decode(%s) failed\n", MODE_TO_STR[g_test_mode]);
410                         goto out;
411                 }
412         }
413
414         /* test encoding jpeg */
415         if ((g_test_mode == TEST_ENCODE_FILE) || (g_test_mode == TEST_ENCODE_MEMORY)) {
416                 if (FALSE == _test_encode(g_test_mode)) {
417                         fprintf(stderr, "\t[JPEG_testsuite] _test_encode(%s) failed\n", MODE_TO_STR[g_test_mode]);
418                         goto out;
419                 }
420         }
421
422 out:
423         SAFE_G_FREE(g_path);
424         SAFE_FREE(g_readed_data);
425         SAFE_IMAGE_FREE(g_decoded_data);
426
427         return 0;
428 }