7589011c7e6f1d1b3a830cf2b00b1809c0ccffff
[platform/core/multimedia/libmm-utility.git] / imgp / test / mm_util_imgp_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 <glib.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <errno.h>
28 #include <mm_util_imgp.h>
29 #include <mm_util_image.h>
30 #include <limits.h>
31 #include <tzplatform_config.h>
32
33 #define IMGP_FREE(src)          { if (src != NULL) {g_free(src); src = NULL; } }
34 #define SAFE_IMAGE_FREE(x)      { if (x != NULL) { mm_image_destroy_image(x); x = NULL; } }
35
36 typedef struct {
37         /* for source image */
38         char *path;
39         unsigned int width;
40         unsigned int height;
41         unsigned int colorspace;
42         void *data;
43         size_t size;
44
45         /* for parameter in image processing */
46         char *cmd;
47         unsigned int x;
48         unsigned int y;
49         unsigned int w;
50         unsigned int h;
51         unsigned int rot;
52         unsigned int cs;
53 } input_params;
54
55 typedef struct {
56         /* for destination image */
57         char path[PATH_MAX];
58         unsigned int width;
59         unsigned int height;
60         unsigned char *data;
61         size_t size;
62 } output_values;
63
64 static input_params g_args;
65 static output_values g_transformed;
66
67 static gboolean _read_file(char *path, void **data, size_t *length)
68 {
69         FILE *fp = NULL;
70         long len = 0;
71
72         if (!path || !data || length == 0) {
73                 fprintf(stderr, "\t[IMGP_testsuite] invalid data\n");
74                 return FALSE;
75         }
76
77         fprintf(stderr, "\t[IMGP_testsuite] %s read\n", path);
78
79         fp = fopen(path, "r");
80         if (fp == NULL) {
81                 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
82                 return FALSE;
83         }
84
85         if (fseek(fp, 0, SEEK_END) < 0) {
86                 fprintf(stderr, "\t[IMGP_testsuite] fseek failed \n");
87                 fclose(fp);
88                 return FALSE;
89         }
90
91         len = ftell(fp);
92         if (len < 0) {
93                 fprintf(stderr, "\t[IMGP_testsuite] ftell failed \n");
94                 fclose(fp);
95                 return FALSE;
96         }
97
98         rewind(fp);
99         *data = g_malloc0(len);
100         *length = fread(*data, 1, (size_t)len, fp);
101         if (*length != len) {
102                 fprintf(stderr, "\t[IMGP_testsuite] fread failed \n");
103         }
104
105         fclose(fp);
106
107         *length = (size_t)len;
108
109         fprintf(stderr, "\t[IMGP_testsuite] %s %zu read DONE\n", path, *length);
110
111         return TRUE;
112 }
113
114 static gboolean _write_file(const char *path, void *data, size_t length)
115 {
116         FILE *fp = NULL;
117         size_t len = 0;
118
119         if (!path || !data || length == 0) {
120                 fprintf(stderr, "\t[IMGP_testsuite] invalid data\n");
121                 return FALSE;
122         }
123
124         fprintf(stderr, "\t[IMGP_testsuite] %s %p %zu write\n", path, data, length);
125
126         fp = fopen(path, "w");
127         if (fp == NULL) {
128                 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
129                 return FALSE;
130         }
131
132         len = fwrite(data, 1, length, fp);
133         if (len != length) {
134                 fprintf(stderr, "\t[IMGP_testsuite] fwrite failed \n");
135         }
136
137         fclose(fp);
138         fp = NULL;
139
140         fprintf(stderr, "\t[IMGP_testsuite] %s write DONE\n", path);
141
142         return TRUE;
143 }
144
145 gboolean _get_input_data(const char *argv, const unsigned long min, const unsigned long max, unsigned int *data)
146 {
147         if (argv == NULL || strlen(argv) == 0)
148                 return FALSE;
149
150         unsigned long temp = g_ascii_strtoll(argv, NULL, 10);
151
152         if (temp < min || temp > max)
153                 return FALSE;
154
155         *data  = (unsigned int)temp;
156
157         return TRUE;
158 }
159
160 void _print_help(const char *argv0)
161 {
162         fprintf(stderr, "\t[usage]\n");
163         fprintf(stderr, "\t\t0. %s {path} {command} src_width src_height src_foramt {params} \n", argv0);
164         fprintf(stderr, "\t\t1. command - convert, resize, rotate and crop \n");
165         fprintf(stderr, "\t\t2. %s {path} convert src_width src_height src_foramt format \n", argv0);
166         fprintf(stderr, "\t\t3. ex: %s test.rgb convert 1920 1080 7 0 \n", argv0);
167         fprintf(stderr, "\t\t4. %s {path} resize src_width src_height src_foramt width height \n", argv0);
168         fprintf(stderr, "\t\t5. ex: %s test.rgb resize 1920 1080 7 1280 720 \n", argv0);
169         fprintf(stderr, "\t\t6. %s {path} rotate src_width src_height src_foramt rotation \n", argv0);
170         fprintf(stderr, "\t\t7. ex: %s test.rgb rotate 1920 1080 7 1 \n", argv0);
171         fprintf(stderr, "\t\t8. %s {path} crop src_width src_height src_foramt start_x start_y width height \n", argv0);
172         fprintf(stderr, "\t\t9. ex: %s test.rgb crop 1920 1080 7 100 100 640 480 \n", argv0);
173 }
174
175 gboolean _get_arguments(int argc, char *argv[])
176 {
177         unsigned int index = 1;
178
179         g_args.path = g_strdup(argv[index++]);
180         g_args.cmd = g_strdup(argv[index++]);
181
182         if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.width)) {
183                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[index-1]);
184                 return FALSE;
185         }
186         if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.height)) {
187                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[index-1]);
188                 return FALSE;
189         }
190         if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
191                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[index-1]);
192                 return FALSE;
193         }
194         if (strcmp(g_args.cmd, "convert") == 0) {
195                 if (argc < 7) {
196                         _print_help(argv[0]);
197                         return FALSE;
198                 }
199
200                 if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
201                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[index-1]);
202                         return FALSE;
203                 }
204         } else if (strcmp(g_args.cmd, "resize") == 0) {
205                 if (argc < 8) {
206                         _print_help(argv[0]);
207                         return FALSE;
208                 }
209
210                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
211                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
212                         return FALSE;
213                 }
214                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
215                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
216                         return FALSE;
217                 }
218         } else if (strcmp(g_args.cmd, "rotate") == 0) {
219                 if (argc < 7) {
220                         _print_help(argv[0]);
221                         return FALSE;
222                 }
223
224                 if (FALSE == _get_input_data(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
225                         fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[index-1]);
226                         return FALSE;
227                 }
228         } else if (strcmp(g_args.cmd, "crop") == 0) {
229                 if (argc < 10) {
230                         _print_help(argv[0]);
231                         return FALSE;
232                 }
233
234                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.x)) {
235                         fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[index-1]);
236                         return FALSE;
237                 }
238                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.y)) {
239                         fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[index-1]);
240                         return FALSE;
241                 }
242                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
243                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
244                         return FALSE;
245                 }
246                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
247                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
248                         return FALSE;
249                 }
250         }
251
252         fprintf(stderr, "\t[IMGP_testsuite] cmd: %s, w: %u, h: %u, cs:%u, rot:%u, x: %u, y: %u\n",
253                                 g_args.cmd, g_args.w, g_args.h, g_args.cs, g_args.rot, g_args.x, g_args.y);
254
255         return TRUE;
256 }
257
258 int main(int argc, char *argv[])
259 {
260         int ret = 0;
261         mm_util_image_h _src = NULL;
262         mm_util_image_h _dst = NULL;
263
264         if (argc < 6) {
265                 _print_help(argv[0]);
266                 return 0;
267         }
268
269         /* initialize data */
270         memset(&g_args, 0, sizeof(g_args));
271         memset(&g_transformed, 0, sizeof(g_transformed));
272
273         /* get arguments */
274         if (FALSE == _get_arguments(argc, argv)) {
275                 fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
276                 goto TEST_FAIL;
277         }
278
279         /* read input file */
280         if (FALSE == _read_file(g_args.path, &g_args.data, &g_args.size)) {
281                 fprintf(stderr, "\t[IMGP_testsuite] reading file(%s) error\n", g_args.path);
282                 goto TEST_FAIL;
283         }
284
285         ret = mm_image_create_image(g_args.width, g_args.height, g_args.colorspace,
286                         (unsigned char *)g_args.data, g_args.size, &_src);
287         if (ret != MM_UTIL_ERROR_NONE) {
288                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_create_image\n");
289                 goto TEST_FAIL;
290         }
291
292         mm_image_debug_image(_src, "_src");
293
294         /* test image processing */
295         if (strcmp(g_args.cmd, "convert") == 0)
296                 ret = mm_util_convert_colorspace(_src, g_args.cs, &_dst);
297         else if (strcmp(g_args.cmd, "resize") == 0)
298                 ret = mm_util_resize_image(_src, g_args.w, g_args.h, &_dst);
299         else if (strcmp(g_args.cmd, "rotate") == 0)
300                 ret = mm_util_rotate_image(_src, g_args.rot, &_dst);
301         else if (strcmp(g_args.cmd, "crop") == 0)
302                 ret = mm_util_crop_image(_src, g_args.x, g_args.y, g_args.w, g_args.h, &_dst);
303         else {
304                 fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
305                 goto TEST_FAIL;
306         }
307
308         if (ret == MM_UTIL_ERROR_NONE) {
309                 fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
310         } else {
311                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
312                 goto TEST_FAIL;
313         }
314
315         if (!mm_image_is_valid_image(_dst)) {
316                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
317                 goto TEST_FAIL;
318         }
319
320         mm_image_debug_image(_dst, "_dst");
321
322         ret = mm_image_get_image(_dst, &g_transformed.width, &g_transformed.height,
323                         NULL, &g_transformed.data, &g_transformed.size);
324         if (ret != MM_UTIL_ERROR_NONE) {
325                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_get_image\n");
326                 goto TEST_FAIL;
327         }
328
329         /* ready output file */
330         snprintf(g_transformed.path, PATH_MAX, "result_%s_%ux%u.raw", g_args.cmd, g_transformed.width, g_transformed.height);
331
332         /* write output file */
333         if (FALSE == _write_file(g_transformed.path, (void *)g_transformed.data, g_transformed.size)) {
334                 fprintf(stderr, "\t[IMGP_testsuite] writing file(%s) error\n", g_transformed.path);
335                 goto TEST_FAIL;
336         }
337
338 TEST_FAIL:
339         SAFE_IMAGE_FREE(_src);
340         SAFE_IMAGE_FREE(_dst);
341         IMGP_FREE(g_transformed.data);
342         IMGP_FREE(g_args.data);
343         g_free(g_args.cmd);
344         g_free(g_args.path);
345
346         return 0;
347 }
348