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 %s %p %p\n", path, data, length);
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 = (void *)calloc(1, len);
101 fprintf(stderr, "\tmemory allocation failed \n");
106 *length = fread(*data, 1, (size_t)len, fp);
107 if (*length != len) {
108 fprintf(stderr, "\t[IMGP_testsuite] fread failed \n");
118 *length = (size_t)len;
120 fprintf(stderr, "\t[IMGP_testsuite] %s %zu read DONE\n", path, *length);
125 static gboolean _write_file(const char *path, void *data, size_t length)
130 if (!path || !data || length == 0) {
131 fprintf(stderr, "\t[IMGP_testsuite] invalid data %s %p %zu\n", path, data, length);
135 fprintf(stderr, "\t[IMGP_testsuite] %s %p %zu write\n", path, data, length);
137 fp = fopen(path, "w");
139 fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
143 len = fwrite(data, 1, length, fp);
145 fprintf(stderr, "\t[IMGP_testsuite] fwrite failed \n");
151 fprintf(stderr, "\t[IMGP_testsuite] %s write DONE\n", path);
156 gboolean _get_input_data(const char *argv, const unsigned long min, const unsigned long max, unsigned int *data)
158 if (argv == NULL || strlen(argv) == 0)
161 unsigned long temp = g_ascii_strtoll(argv, NULL, 10);
163 if (temp < min || temp > max)
166 *data = (unsigned int)temp;
171 void _print_help(const char *argv0)
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);
186 gboolean _get_arguments(int argc, char *argv[])
188 unsigned int index = 1;
190 g_args.path = g_strdup(argv[index++]);
191 g_args.cmd = g_strdup(argv[index++]);
193 if (!g_args.path || !g_args.cmd) {
194 fprintf(stderr, "\t[IMGP_testsuite] invalid path or command\n");
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]);
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]);
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]);
210 if (strcmp(g_args.cmd, "convert") == 0) {
212 _print_help(argv[0]);
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]);
220 } else if (strcmp(g_args.cmd, "resize") == 0) {
222 _print_help(argv[0]);
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]);
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]);
234 } else if (strcmp(g_args.cmd, "rotate") == 0) {
236 _print_help(argv[0]);
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]);
244 } else if (strcmp(g_args.cmd, "crop") == 0) {
246 _print_help(argv[0]);
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]);
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]);
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]);
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]);
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);
274 int main(int argc, char *argv[])
277 mm_util_image_h _src = NULL;
278 mm_util_image_h _dst = NULL;
281 _print_help(argv[0]);
285 /* initialize data */
286 memset(&g_args, 0, sizeof(g_args));
287 memset(&g_transformed, 0, sizeof(g_transformed));
290 if (FALSE == _get_arguments(argc, argv)) {
291 fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
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);
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");
308 mm_image_debug_image(_src, "_src");
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);
320 fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
324 if (ret == MM_UTIL_ERROR_NONE) {
325 fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
327 fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
331 if (!mm_image_is_valid_image(_dst)) {
332 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
336 mm_image_debug_image(_dst, "_dst");
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");
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);
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);
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);