Modify parameter in imgage processing APIs to mm_util_image_h
[platform/core/multimedia/libmm-utility.git] / imgp / test / mm_util_imgp_testsuite.c
index a67124e..4d4bfd7 100644 (file)
 #include <inttypes.h>
 #include <errno.h>
 #include <mm_util_imgp.h>
+#include <mm_util_image.h>
 #include <limits.h>
 #include <tzplatform_config.h>
 
-#define IMGP_FREE(src) { if (src != NULL) {g_free(src); src = NULL; } }
+#define IMGP_FREE(src)         { if (src != NULL) {g_free(src); src = NULL; } }
+#define SAFE_IMAGE_FREE(x)     { if (x != NULL) { mm_image_destroy_image(x); x = NULL; } }
+
+typedef struct {
+       /* for source image */
+       char *path;
+       unsigned int width;
+       unsigned int height;
+       unsigned int colorspace;
+       void *data;
+       size_t size;
+
+       /* for parameter in image processing */
+       char *cmd;
+       unsigned int x;
+       unsigned int y;
+       unsigned int w;
+       unsigned int h;
+       unsigned int rot;
+       unsigned int cs;
+} input_params;
+
+typedef struct {
+       /* for destination image */
+       char path[PATH_MAX];
+       unsigned int width;
+       unsigned int height;
+       unsigned char *data;
+       size_t size;
+} output_values;
+
+static input_params g_args;
+static output_values g_transformed;
 
 static gboolean _read_file(char *path, void **data, size_t *length)
 {
@@ -37,27 +70,27 @@ static gboolean _read_file(char *path, void **data, size_t *length)
        long len = 0;
 
        if (!path || !data || length == 0) {
-               fprintf(stderr, "\t[JPEG_testsuite] invalid data %s %p %p\n", path, data, length);
+               fprintf(stderr, "\t[IMGP_testsuite] invalid data %s %p %p\n", path, data, length);
                return FALSE;
        }
 
-       fprintf(stderr, "\t[JPEG_testsuite] %s read\n", path);
+       fprintf(stderr, "\t[IMGP_testsuite] %s read\n", path);
 
        fp = fopen(path, "r");
        if (fp == NULL) {
-               fprintf(stderr, "\t[JPEG_testsuite] fopen failed (%d) \n", errno);
+               fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
                return FALSE;
        }
 
        if (fseek(fp, 0, SEEK_END) < 0) {
-               fprintf(stderr, "\t[JPEG_testsuite] fseek failed \n");
+               fprintf(stderr, "\t[IMGP_testsuite] fseek failed \n");
                fclose(fp);
                return FALSE;
        }
 
        len = ftell(fp);
        if (len < 0) {
-               fprintf(stderr, "\t[JPEG_testsuite] ftell failed \n");
+               fprintf(stderr, "\t[IMGP_testsuite] ftell failed \n");
                fclose(fp);
                return FALSE;
        }
@@ -72,7 +105,7 @@ static gboolean _read_file(char *path, void **data, size_t *length)
 
        *length = fread(*data, 1, (size_t)len, fp);
        if (*length != len) {
-               fprintf(stderr, "\t[JPEG_testsuite] fread failed \n");
+               fprintf(stderr, "\t[IMGP_testsuite] fread failed \n");
        }
 
        fclose(fp);
@@ -84,7 +117,7 @@ static gboolean _read_file(char *path, void **data, size_t *length)
 
        *length = (size_t)len;
 
-       fprintf(stderr, "\t[JPEG_testsuite] %s %zu read DONE\n", path, *length);
+       fprintf(stderr, "\t[IMGP_testsuite] %s %zu read DONE\n", path, *length);
 
        return TRUE;
 }
@@ -95,27 +128,27 @@ static gboolean _write_file(const char *path, void *data, size_t length)
        size_t len = 0;
 
        if (!path || !data || length == 0) {
-               fprintf(stderr, "\t[JPEG_testsuite] invalid data %s %p %zu\n", path, data, length);
+               fprintf(stderr, "\t[IMGP_testsuite] invalid data %s %p %zu\n", path, data, length);
                return FALSE;
        }
 
-       fprintf(stderr, "\t[JPEG_testsuite] %s %p %zu write\n", path, data, length);
+       fprintf(stderr, "\t[IMGP_testsuite] %s %p %zu write\n", path, data, length);
 
        fp = fopen(path, "w");
        if (fp == NULL) {
-               fprintf(stderr, "\t[JPEG_testsuite] fopen failed (%d) \n", errno);
+               fprintf(stderr, "\t[IMGP_testsuite] fopen failed (%d) \n", errno);
                return FALSE;
        }
 
        len = fwrite(data, 1, length, fp);
        if (len != length) {
-               fprintf(stderr, "\t[JPEG_testsuite] fwrite failed \n");
+               fprintf(stderr, "\t[IMGP_testsuite] fwrite failed \n");
        }
 
        fclose(fp);
        fp = NULL;
 
-       fprintf(stderr, "\t[JPEG_testsuite] %s write DONE\n", path);
+       fprintf(stderr, "\t[IMGP_testsuite] %s write DONE\n", path);
 
        return TRUE;
 }
@@ -135,117 +168,196 @@ gboolean _get_input_data(const char *argv, const unsigned long min, const unsign
        return TRUE;
 }
 
-int main(int argc, char *argv[])
+void _print_help(const char *argv0)
 {
-       int ret = 0;
-       void *src = NULL;
-       unsigned char *dst = NULL;
-
-       if (argc < 12) {
-               fprintf(stderr, "Usage: %s {filename} {command} src_width src_height src_foramt dst_width dst_height dst_format roation crop_start_x crop_start_y \n", argv[0]);
-               fprintf(stderr, "ex: %s test.rgb resize 1920 1080 7 1280 720 7 0 0 0 \n", argv[0]);
-               return ret;
-       }
-
-       size_t src_size = 0;
-       char *filename = g_strdup(argv[1]);
-       char *command = g_strdup(argv[2]);
-       unsigned int src_width = 0;
-       unsigned int src_height = 0;
-       mm_util_color_format_e src_format = 0;
-       unsigned int dst_width = 0;
-       unsigned int dst_height = 0;
-       mm_util_color_format_e dst_format = 0;
-       mm_util_img_rotate_type rotation = 0;
-       unsigned int start_x = 0;
-       unsigned int start_y = 0;
-       char output_file[40] = {};
-       unsigned int res_w = 0;
-       unsigned int res_h = 0;
-       size_t res_buffer_size = 0;
-
-       if (filename == NULL || command == NULL) {
-               fprintf(stderr, "\t[IMGP_testsuite] invalid filename or command\n");
-               goto TEST_FAIL;
+       fprintf(stderr, "\t[usage]\n");
+       fprintf(stderr, "\t\t0. %s {path} {command} src_width src_height src_foramt {params} \n", argv0);
+       fprintf(stderr, "\t\t1. command - convert, resize, rotate and crop \n");
+       fprintf(stderr, "\t\t2. %s {path} convert src_width src_height src_foramt format \n", argv0);
+       fprintf(stderr, "\t\t3. ex: %s test.rgb convert 1920 1080 7 0 \n", argv0);
+       fprintf(stderr, "\t\t4. %s {path} resize src_width src_height src_foramt width height \n", argv0);
+       fprintf(stderr, "\t\t5. ex: %s test.rgb resize 1920 1080 7 1280 720 \n", argv0);
+       fprintf(stderr, "\t\t6. %s {path} rotate src_width src_height src_foramt rotation \n", argv0);
+       fprintf(stderr, "\t\t7. ex: %s test.rgb rotate 1920 1080 7 1 \n", argv0);
+       fprintf(stderr, "\t\t8. %s {path} crop src_width src_height src_foramt start_x start_y width height \n", argv0);
+       fprintf(stderr, "\t\t9. ex: %s test.rgb crop 1920 1080 7 100 100 640 480 \n", argv0);
+}
+
+gboolean _get_arguments(int argc, char *argv[])
+{
+       unsigned int index = 1;
+
+       g_args.path = g_strdup(argv[index++]);
+       g_args.cmd = g_strdup(argv[index++]);
+
+       if (!g_args.path || !g_args.cmd) {
+               fprintf(stderr, "\t[IMGP_testsuite] invalid path or command\n");
+               return FALSE;
        }
 
-       /* get arguments */
-       if (FALSE == _get_input_data(argv[3], 0, UINT_MAX, &src_width)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[3]);
-               goto TEST_FAIL;
+       if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.width)) {
+               fprintf(stderr, "\t[IMGP_testsuite] wrong src_width %s\n", argv[index-1]);
+               return FALSE;
        }
-       if (FALSE == _get_input_data(argv[4], 0, UINT_MAX, &src_height)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[4]);
-               goto TEST_FAIL;
+       if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.height)) {
+               fprintf(stderr, "\t[IMGP_testsuite] wrong src_height %s\n", argv[index-1]);
+               return FALSE;
        }
-       if (FALSE == _get_input_data(argv[5], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &src_format)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[5]);
-               goto TEST_FAIL;
+       if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.colorspace)) {
+               fprintf(stderr, "\t[IMGP_testsuite] wrong src_format %s\n", argv[index-1]);
+               return FALSE;
        }
-       if (FALSE == _get_input_data(argv[6], 0, UINT_MAX, &dst_width)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[6]);
-               goto TEST_FAIL;
+       if (strcmp(g_args.cmd, "convert") == 0) {
+               if (argc < 7) {
+                       _print_help(argv[0]);
+                       return FALSE;
+               }
+
+               if (FALSE == _get_input_data(argv[index++], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &g_args.cs)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[index-1]);
+                       return FALSE;
+               }
+       } else if (strcmp(g_args.cmd, "resize") == 0) {
+               if (argc < 8) {
+                       _print_help(argv[0]);
+                       return FALSE;
+               }
+
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
+                       return FALSE;
+               }
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
+                       return FALSE;
+               }
+       } else if (strcmp(g_args.cmd, "rotate") == 0) {
+               if (argc < 7) {
+                       _print_help(argv[0]);
+                       return FALSE;
+               }
+
+               if (FALSE == _get_input_data(argv[index++], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &g_args.rot)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[index-1]);
+                       return FALSE;
+               }
+       } else if (strcmp(g_args.cmd, "crop") == 0) {
+               if (argc < 10) {
+                       _print_help(argv[0]);
+                       return FALSE;
+               }
+
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.x)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[index-1]);
+                       return FALSE;
+               }
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.y)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[index-1]);
+                       return FALSE;
+               }
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.w)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong dst_width %s\n", argv[index-1]);
+                       return FALSE;
+               }
+               if (FALSE == _get_input_data(argv[index++], 0, UINT_MAX, &g_args.h)) {
+                       fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[index-1]);
+                       return FALSE;
+               }
        }
-       if (FALSE == _get_input_data(argv[7], 0, UINT_MAX, &dst_height)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong dst_height %s\n", argv[7]);
-               goto TEST_FAIL;
+
+       fprintf(stderr, "\t[IMGP_testsuite] cmd: %s, w: %u, h: %u, cs:%u, rot:%u, x: %u, y: %u\n",
+                               g_args.cmd, g_args.w, g_args.h, g_args.cs, g_args.rot, g_args.x, g_args.y);
+
+       return TRUE;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret = 0;
+       mm_util_image_h _src = NULL;
+       mm_util_image_h _dst = NULL;
+
+       if (argc < 6) {
+               _print_help(argv[0]);
+               return 0;
        }
-       if (FALSE == _get_input_data(argv[8], MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NUM - 1, &dst_format)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong dst_format %s\n", argv[8]);
+
+       /* initialize data */
+       memset(&g_args, 0, sizeof(g_args));
+       memset(&g_transformed, 0, sizeof(g_transformed));
+
+       /* get arguments */
+       if (FALSE == _get_arguments(argc, argv)) {
+               fprintf(stderr, "\t[IMGP_testsuite] _get_arguments failed\n");
                goto TEST_FAIL;
        }
-       if (FALSE == _get_input_data(argv[9], MM_UTIL_ROTATE_0, MM_UTIL_ROTATE_NUM - 1, &rotation)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong rotation %s\n", argv[9]);
+
+       /* read input file */
+       if (FALSE == _read_file(g_args.path, &g_args.data, &g_args.size)) {
+               fprintf(stderr, "\t[IMGP_testsuite] reading file(%s) error\n", g_args.path);
                goto TEST_FAIL;
        }
-       if (FALSE == _get_input_data(argv[10], 0, UINT_MAX, &start_x)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong start_x %s\n", argv[10]);
+
+       ret = mm_image_create_image(g_args.width, g_args.height, g_args.colorspace,
+                       (unsigned char *)g_args.data, g_args.size, &_src);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_create_image\n");
                goto TEST_FAIL;
        }
-       if (FALSE == _get_input_data(argv[11], 0, UINT_MAX, &start_y)) {
-               fprintf(stderr, "\t[IMGP_testsuite] wrong start_y %s\n", argv[11]);
+
+       mm_image_debug_image(_src, "_src");
+
+       /* test image processing */
+       if (strcmp(g_args.cmd, "convert") == 0)
+               ret = mm_util_convert_colorspace(_src, g_args.cs, &_dst);
+       else if (strcmp(g_args.cmd, "resize") == 0)
+               ret = mm_util_resize_image(_src, g_args.w, g_args.h, &_dst);
+       else if (strcmp(g_args.cmd, "rotate") == 0)
+               ret = mm_util_rotate_image(_src, g_args.rot, &_dst);
+       else if (strcmp(g_args.cmd, "crop") == 0)
+               ret = mm_util_crop_image(_src, g_args.x, g_args.y, g_args.w, g_args.h, &_dst);
+       else {
+               fprintf(stderr, "\t[IMGP_testsuite] Wrong command - %s\n", g_args.cmd);
                goto TEST_FAIL;
        }
 
-       /* read input file */
-       if (FALSE == _read_file(filename, &src, &src_size)) {
-               fprintf(stderr, "\t[IMGP_testsuite] reading file(%s) error\n", filename);
+       if (ret == MM_UTIL_ERROR_NONE) {
+               fprintf(stderr, "\t[IMGP_testsuite] Success - %s\n", g_args.cmd);
+       } else {
+               fprintf(stderr, "\t[IMGP_testsuite] ERROR - %s\n", g_args.cmd);
                goto TEST_FAIL;
        }
 
-       fprintf(stderr, "command: %s src_width: %d, src_height: %d, src_format: %d, dst_width: %d, dst_height: %d, dst_format:%d, rotation:%d\n", command, src_width, src_height, src_format, dst_width, dst_height, dst_format, rotation);
-
-       /* ready output file */
-       snprintf(output_file, 40, "result_%s_%d_%d.raw", command, dst_width, dst_height);
+       if (!mm_image_is_valid_image(_dst)) {
+               fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_is_valid_image\n");
+               goto TEST_FAIL;
+       }
 
-       if (strcmp(command, "convert") == 0)
-               ret = mm_util_convert_colorspace(src, src_width, src_height, src_format, dst_format, &dst, &res_w, &res_h, &res_buffer_size);
-       else if (strcmp(command, "resize") == 0)
-               ret = mm_util_resize_image(src, src_width, src_height, src_format, dst_width, dst_height, &dst, &res_w, &res_h, &res_buffer_size);
-       else if (strcmp(command, "rotate") == 0)
-               ret = mm_util_rotate_image(src, src_width, src_height, src_format, rotation, &dst, &res_w, &res_h, &res_buffer_size);
-       else if (strcmp(command, "crop") == 0)
-               ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, dst_width, dst_height, &dst, &res_w, &res_h, &res_buffer_size);
+       mm_image_debug_image(_dst, "_dst");
 
-       if (ret == MM_UTIL_ERROR_NONE) {
-               fprintf(stderr, "Success - %s\n", command);
-       } else {
-               fprintf(stderr, "ERROR - %s\n", command);
+       ret = mm_image_get_image(_dst, &g_transformed.width, &g_transformed.height,
+                       NULL, &g_transformed.data, &g_transformed.size);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               fprintf(stderr, "\t[IMGP_testsuite] ERROR - mm_image_get_image\n");
                goto TEST_FAIL;
        }
 
+       /* ready output file */
+       snprintf(g_transformed.path, PATH_MAX, "result_%s_%ux%u.raw", g_args.cmd, g_transformed.width, g_transformed.height);
+
        /* write output file */
-       if (FALSE == _write_file(output_file, dst, res_buffer_size)) {
-               fprintf(stderr, "\t[IMGP_testsuite] writing file(%s) error\n", output_file);
+       if (FALSE == _write_file(g_transformed.path, (void *)g_transformed.data, g_transformed.size)) {
+               fprintf(stderr, "\t[IMGP_testsuite] writing file(%s) error\n", g_transformed.path);
                goto TEST_FAIL;
        }
 
 TEST_FAIL:
-
-       IMGP_FREE(src);
-       IMGP_FREE(dst);
-       IMGP_FREE(command);
-       IMGP_FREE(filename);
+       SAFE_IMAGE_FREE(_src);
+       SAFE_IMAGE_FREE(_dst);
+       IMGP_FREE(g_transformed.data);
+       IMGP_FREE(g_args.data);
+       IMGP_FREE(g_args.cmd);
+       IMGP_FREE(g_args.path);
 
        return 0;
 }