4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <mm_util_imgp.h>
29 #include <mm_util_image.h>
31 #include <tzplatform_config.h>
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; } }
37 /* for source image */
41 unsigned int colorspace;
45 /* for parameter in image processing */
56 /* for destination image */
64 static input_params g_args;
65 static output_values g_transformed;
67 static gboolean _read_file(char *path, void **data, size_t *length)
72 if (!path || !data || length == 0) {
73 fprintf(stderr, "\t[IMGP_testsuite] invalid data\n");
77 fprintf(stderr, "\t[IMGP_testsuite] %s read\n", path);
79 fp = fopen(path, "r");
81 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
85 if (fseek(fp, 0, SEEK_END) < 0) {
86 fprintf(stderr, "\t[IMGP_testsuite] fseek failed \n");
93 fprintf(stderr, "\t[IMGP_testsuite] ftell failed \n");
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");
107 *length = (size_t)len;
109 fprintf(stderr, "\t[IMGP_testsuite] %s %zu read DONE\n", path, *length);
114 static gboolean _write_file(const char *path, void *data, size_t length)
119 if (!path || !data || length == 0) {
120 fprintf(stderr, "\t[IMGP_testsuite] invalid data\n");
124 fprintf(stderr, "\t[IMGP_testsuite] %s %p %zu write\n", path, data, length);
126 fp = fopen(path, "w");
128 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
132 len = fwrite(data, 1, length, fp);
134 fprintf(stderr, "\t[IMGP_testsuite] fwrite failed \n");
140 fprintf(stderr, "\t[IMGP_testsuite] %s write DONE\n", path);
145 gboolean _get_input_data(const char *argv, const unsigned long min, const unsigned long max, unsigned int *data)
147 if (argv == NULL || strlen(argv) == 0)
150 unsigned long temp = g_ascii_strtoll(argv, NULL, 10);
152 if (temp < min || temp > max)
155 *data = (unsigned int)temp;
160 void _print_help(const char *argv0)
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);
175 gboolean _get_arguments(int argc, char *argv[])
177 unsigned int index = 1;
179 g_args.path = g_strdup(argv[index++]);
180 g_args.cmd = g_strdup(argv[index++]);
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]);
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]);
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]);
194 if (strcmp(g_args.cmd, "convert") == 0) {
196 _print_help(argv[0]);
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]);
204 } else if (strcmp(g_args.cmd, "resize") == 0) {
206 _print_help(argv[0]);
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]);
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]);
218 } else if (strcmp(g_args.cmd, "rotate") == 0) {
220 _print_help(argv[0]);
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]);
228 } else if (strcmp(g_args.cmd, "crop") == 0) {
230 _print_help(argv[0]);
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]);
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]);
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]);
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]);
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);
258 int main(int argc, char *argv[])
261 mm_util_image_h _src = NULL;
262 mm_util_image_h _dst = NULL;
265 _print_help(argv[0]);
269 /* initialize data */
270 memset(&g_args, 0, sizeof(g_args));
271 memset(&g_transformed, 0, sizeof(g_transformed));
274 if (FALSE == _get_arguments(argc, argv)) {
275 fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
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);
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");
292 mm_image_debug_image(_src, "_src");
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);
304 fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
308 if (ret == MM_UTIL_ERROR_NONE) {
309 fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
311 fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
315 if (!mm_image_is_valid_image(_dst)) {
316 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
320 mm_image_debug_image(_dst, "_dst");
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");
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);
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);
339 SAFE_IMAGE_FREE(_src);
340 SAFE_IMAGE_FREE(_dst);
341 IMGP_FREE(g_transformed.data);
342 IMGP_FREE(g_args.data);