Modify parameter in imgage processing APIs to mm_util_image_h
[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 %s %p %p\n", path, data, length);
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 = (void *)calloc(1, len);
100         if (*data == NULL) {
101                 fprintf(stderr, "\tmemory allocation failed \n");
102                 fclose(fp);
103                 return FALSE;
104         }
105
106         *length = fread(*data, 1, (size_t)len, fp);
107         if (*length != len) {
108                 fprintf(stderr, "\t[IMGP_testsuite] fread failed \n");
109         }
110
111         fclose(fp);
112
113         if (*data == NULL) {
114                 *length = 0;
115                 return FALSE;
116         }
117
118         *length = (size_t)len;
119
120         fprintf(stderr, "\t[IMGP_testsuite] %s %zu read DONE\n", path, *length);
121
122         return TRUE;
123 }
124
125 static gboolean _write_file(const char *path, void *data, size_t length)
126 {
127         FILE *fp = NULL;
128         size_t len = 0;
129
130         if (!path || !data || length == 0) {
131                 fprintf(stderr, "\t[IMGP_testsuite] invalid data %s %p %zu\n", path, data, length);
132                 return FALSE;
133         }
134
135         fprintf(stderr, "\t[IMGP_testsuite] %s %p %zu write\n", path, data, length);
136
137         fp = fopen(path, "w");
138         if (fp == NULL) {
139                 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
140                 return FALSE;
141         }
142
143         len = fwrite(data, 1, length, fp);
144         if (len != length) {
145                 fprintf(stderr, "\t[IMGP_testsuite] fwrite failed \n");
146         }
147
148         fclose(fp);
149         fp = NULL;
150
151         fprintf(stderr, "\t[IMGP_testsuite] %s write DONE\n", path);
152
153         return TRUE;
154 }
155
156 gboolean _get_input_data(const char *argv, const unsigned long min, const unsigned long max, unsigned int *data)
157 {
158         if (argv == NULL || strlen(argv) == 0)
159                 return FALSE;
160
161         unsigned long temp = g_ascii_strtoll(argv, NULL, 10);
162
163         if (temp < min || temp > max)
164                 return FALSE;
165
166         *data  = (unsigned int)temp;
167
168         return TRUE;
169 }
170
171 void _print_help(const char *argv0)
172 {
173         fprintf(stderr, "\t[usage]\n");
174         fprintf(stderr, "\t\t0. %s {path} {command} src_width src_height src_foramt {params} \n", argv0);
175         fprintf(stderr, "\t\t1. command - convert, resize, rotate and crop \n");
176         fprintf(stderr, "\t\t2. %s {path} convert src_width src_height src_foramt format \n", argv0);
177         fprintf(stderr, "\t\t3. ex: %s test.rgb convert 1920 1080 7 0 \n", argv0);
178         fprintf(stderr, "\t\t4. %s {path} resize src_width src_height src_foramt width height \n", argv0);
179         fprintf(stderr, "\t\t5. ex: %s test.rgb resize 1920 1080 7 1280 720 \n", argv0);
180         fprintf(stderr, "\t\t6. %s {path} rotate src_width src_height src_foramt rotation \n", argv0);
181         fprintf(stderr, "\t\t7. ex: %s test.rgb rotate 1920 1080 7 1 \n", argv0);
182         fprintf(stderr, "\t\t8. %s {path} crop src_width src_height src_foramt start_x start_y width height \n", argv0);
183         fprintf(stderr, "\t\t9. ex: %s test.rgb crop 1920 1080 7 100 100 640 480 \n", argv0);
184 }
185
186 gboolean _get_arguments(int argc, char *argv[])
187 {
188         unsigned int index = 1;
189
190         g_args.path = g_strdup(argv[index++]);
191         g_args.cmd = g_strdup(argv[index++]);
192
193         if (!g_args.path || !g_args.cmd) {
194                 fprintf(stderr, "\t[IMGP_testsuite] invalid path or command\n");
195                 return FALSE;
196         }
197
198         if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.width)) {
199                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[index-1]);
200                 return FALSE;
201         }
202         if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.height)) {
203                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[index-1]);
204                 return FALSE;
205         }
206         if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
207                 fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[index-1]);
208                 return FALSE;
209         }
210         if (strcmp(g_args.cmd, "convert") == 0) {
211                 if (argc < 7) {
212                         _print_help(argv[0]);
213                         return FALSE;
214                 }
215
216                 if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
217                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[index-1]);
218                         return FALSE;
219                 }
220         } else if (strcmp(g_args.cmd, "resize") == 0) {
221                 if (argc < 8) {
222                         _print_help(argv[0]);
223                         return FALSE;
224                 }
225
226                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
227                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
228                         return FALSE;
229                 }
230                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
231                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
232                         return FALSE;
233                 }
234         } else if (strcmp(g_args.cmd, "rotate") == 0) {
235                 if (argc < 7) {
236                         _print_help(argv[0]);
237                         return FALSE;
238                 }
239
240                 if (FALSE == _get_input_data(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
241                         fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[index-1]);
242                         return FALSE;
243                 }
244         } else if (strcmp(g_args.cmd, "crop") == 0) {
245                 if (argc < 10) {
246                         _print_help(argv[0]);
247                         return FALSE;
248                 }
249
250                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.x)) {
251                         fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[index-1]);
252                         return FALSE;
253                 }
254                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.y)) {
255                         fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[index-1]);
256                         return FALSE;
257                 }
258                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
259                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
260                         return FALSE;
261                 }
262                 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
263                         fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
264                         return FALSE;
265                 }
266         }
267
268         fprintf(stderr, "\t[IMGP_testsuite] cmd: %s, w: %u, h: %u, cs:%u, rot:%u, x: %u, y: %u\n",
269                                 g_args.cmd, g_args.w, g_args.h, g_args.cs, g_args.rot, g_args.x, g_args.y);
270
271         return TRUE;
272 }
273
274 int main(int argc, char *argv[])
275 {
276         int ret = 0;
277         mm_util_image_h _src = NULL;
278         mm_util_image_h _dst = NULL;
279
280         if (argc < 6) {
281                 _print_help(argv[0]);
282                 return 0;
283         }
284
285         /* initialize data */
286         memset(&g_args, 0, sizeof(g_args));
287         memset(&g_transformed, 0, sizeof(g_transformed));
288
289         /* get arguments */
290         if (FALSE == _get_arguments(argc, argv)) {
291                 fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
292                 goto TEST_FAIL;
293         }
294
295         /* read input file */
296         if (FALSE == _read_file(g_args.path, &g_args.data, &g_args.size)) {
297                 fprintf(stderr, "\t[IMGP_testsuite] reading file(%s) error\n", g_args.path);
298                 goto TEST_FAIL;
299         }
300
301         ret = mm_image_create_image(g_args.width, g_args.height, g_args.colorspace,
302                         (unsigned char *)g_args.data, g_args.size, &_src);
303         if (ret != MM_UTIL_ERROR_NONE) {
304                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_create_image\n");
305                 goto TEST_FAIL;
306         }
307
308         mm_image_debug_image(_src, "_src");
309
310         /* test image processing */
311         if (strcmp(g_args.cmd, "convert") == 0)
312                 ret = mm_util_convert_colorspace(_src, g_args.cs, &_dst);
313         else if (strcmp(g_args.cmd, "resize") == 0)
314                 ret = mm_util_resize_image(_src, g_args.w, g_args.h, &_dst);
315         else if (strcmp(g_args.cmd, "rotate") == 0)
316                 ret = mm_util_rotate_image(_src, g_args.rot, &_dst);
317         else if (strcmp(g_args.cmd, "crop") == 0)
318                 ret = mm_util_crop_image(_src, g_args.x, g_args.y, g_args.w, g_args.h, &_dst);
319         else {
320                 fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
321                 goto TEST_FAIL;
322         }
323
324         if (ret == MM_UTIL_ERROR_NONE) {
325                 fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
326         } else {
327                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
328                 goto TEST_FAIL;
329         }
330
331         if (!mm_image_is_valid_image(_dst)) {
332                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
333                 goto TEST_FAIL;
334         }
335
336         mm_image_debug_image(_dst, "_dst");
337
338         ret = mm_image_get_image(_dst, &g_transformed.width, &g_transformed.height,
339                         NULL, &g_transformed.data, &g_transformed.size);
340         if (ret != MM_UTIL_ERROR_NONE) {
341                 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_get_image\n");
342                 goto TEST_FAIL;
343         }
344
345         /* ready output file */
346         snprintf(g_transformed.path, PATH_MAX, "result_%s_%ux%u.raw", g_args.cmd, g_transformed.width, g_transformed.height);
347
348         /* write output file */
349         if (FALSE == _write_file(g_transformed.path, (void *)g_transformed.data, g_transformed.size)) {
350                 fprintf(stderr, "\t[IMGP_testsuite] writing file(%s) error\n", g_transformed.path);
351                 goto TEST_FAIL;
352         }
353
354 TEST_FAIL:
355         SAFE_IMAGE_FREE(_src);
356         SAFE_IMAGE_FREE(_dst);
357         IMGP_FREE(g_transformed.data);
358         IMGP_FREE(g_args.data);
359         IMGP_FREE(g_args.cmd);
360         IMGP_FREE(g_args.path);
361
362         return 0;
363 }
364