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