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 = (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\n");
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 (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.width)) {
194 fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[index-1]);
197 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.height)) {
198 fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[index-1]);
201 if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
202 fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[index-1]);
205 if (strcmp(g_args.cmd, "convert") == 0) {
207 _print_help(argv[0]);
211 if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
212 fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[index-1]);
215 } else if (strcmp(g_args.cmd, "resize") == 0) {
217 _print_help(argv[0]);
221 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
222 fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
225 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
226 fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
229 } else if (strcmp(g_args.cmd, "rotate") == 0) {
231 _print_help(argv[0]);
235 if (FALSE == _get_input_data(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
236 fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[index-1]);
239 } else if (strcmp(g_args.cmd, "crop") == 0) {
241 _print_help(argv[0]);
245 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.x)) {
246 fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[index-1]);
249 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.y)) {
250 fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[index-1]);
253 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
254 fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
257 if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
258 fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
263 fprintf(stderr, "\t[IMGP_testsuite] cmd: %s, w: %u, h: %u, cs:%u, rot:%u, x: %u, y: %u\n",
264 g_args.cmd, g_args.w, g_args.h, g_args.cs, g_args.rot, g_args.x, g_args.y);
269 int main(int argc, char *argv[])
272 mm_util_image_h _src = NULL;
273 mm_util_image_h _dst = NULL;
276 _print_help(argv[0]);
280 /* initialize data */
281 memset(&g_args, 0, sizeof(g_args));
282 memset(&g_transformed, 0, sizeof(g_transformed));
285 if (FALSE == _get_arguments(argc, argv)) {
286 fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
290 /* read input file */
291 if (FALSE == _read_file(g_args.path, &g_args.data, &g_args.size)) {
292 fprintf(stderr, "\t[IMGP_testsuite] reading file(%s) error\n", g_args.path);
296 ret = mm_image_create_image(g_args.width, g_args.height, g_args.colorspace,
297 (unsigned char *)g_args.data, g_args.size, &_src);
298 if (ret != MM_UTIL_ERROR_NONE) {
299 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_create_image\n");
303 mm_image_debug_image(_src, "_src");
305 /* test image processing */
306 if (strcmp(g_args.cmd, "convert") == 0)
307 ret = mm_util_convert_colorspace(_src, g_args.cs, &_dst);
308 else if (strcmp(g_args.cmd, "resize") == 0)
309 ret = mm_util_resize_image(_src, g_args.w, g_args.h, &_dst);
310 else if (strcmp(g_args.cmd, "rotate") == 0)
311 ret = mm_util_rotate_image(_src, g_args.rot, &_dst);
312 else if (strcmp(g_args.cmd, "crop") == 0)
313 ret = mm_util_crop_image(_src, g_args.x, g_args.y, g_args.w, g_args.h, &_dst);
315 fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
319 if (ret == MM_UTIL_ERROR_NONE) {
320 fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
322 fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
326 if (!mm_image_is_valid_image(_dst)) {
327 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
331 mm_image_debug_image(_dst, "_dst");
333 ret = mm_image_get_image(_dst, &g_transformed.width, &g_transformed.height,
334 NULL, &g_transformed.data, &g_transformed.size);
335 if (ret != MM_UTIL_ERROR_NONE) {
336 fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_get_image\n");
340 /* ready output file */
341 snprintf(g_transformed.path, PATH_MAX, "result_%s_%ux%u.raw", g_args.cmd, g_transformed.width, g_transformed.height);
343 /* write output file */
344 if (FALSE == _write_file(g_transformed.path, (void *)g_transformed.data, g_transformed.size)) {
345 fprintf(stderr, "\t[IMGP_testsuite] writing file(%s) error\n", g_transformed.path);
350 SAFE_IMAGE_FREE(_src);
351 SAFE_IMAGE_FREE(_dst);
352 IMGP_FREE(g_transformed.data);
353 IMGP_FREE(g_args.data);