Modify mm_util_resize_image() API to receive allocated buffer and buffer info
[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_common.h>
30
31 #define MAX_STRING_LEN 128
32 #define IMAGE_FORMAT_LABEL_BUFFER_SIZE 4
33 #define IMGP_FREE(src) { if (src != NULL) {g_free(src); src = NULL; } }
34
35 mm_util_imgp_h imgp_handle = NULL;
36 bool completed = false;
37
38 GCond g_thread_cond;
39 GMutex g_thread_mutex;
40
41 void _wait()
42 {
43         g_mutex_lock(&g_thread_mutex);
44         fprintf(stderr, "waiting... until finishing \n");
45         g_cond_wait(&g_thread_cond, &g_thread_mutex);
46         fprintf(stderr, "<=== get signal from callback \n");
47         g_mutex_unlock(&g_thread_mutex);
48 }
49
50 void _signal()
51 {
52         g_mutex_lock(&g_thread_mutex);
53         g_cond_signal(&g_thread_cond);
54         fprintf(stderr, "===> send signal to test proc \n");
55         g_mutex_unlock(&g_thread_mutex);
56 }
57
58 bool _transform_completed_cb(mm_util_color_image_h image, int error, void *user_data)
59 {
60         int err = MM_UTIL_ERROR_NONE;
61         size_t size = 0;
62         char* output_file = (char *)user_data;
63         fprintf(stderr, "imgp_handle: %p\n", imgp_handle);
64
65         fprintf(stderr, "output_file: %s\n", output_file);
66
67         if (error == MM_UTIL_ERROR_NONE) {
68                 fprintf(stderr, "completed\n");
69                 FILE *fp = fopen(output_file, "w");
70                 if (fp) {
71                         void *dst = NULL;
72                         err = mm_util_get_color_image(image, NULL, NULL, NULL, &dst, &size);
73                         if (err != MM_UTIL_ERROR_NONE) {
74                                 IMGP_FREE(dst);
75                                 fclose(fp);
76                                 fprintf(stderr, "Error mm_util_get_color_image (%d)\n", err);
77                                 _signal();
78                                 return FALSE;
79                         }
80                         fprintf(stderr, "dst: %p [%zu]\n", dst, size);
81                         fwrite(dst, 1, size, fp);
82                         fprintf(stderr, "FREE\n");
83                         fclose(fp);
84                 }
85
86                 fprintf(stderr, "write result\n");
87         } else {
88                 fprintf(stderr, "[Error] complete cb (%d)\n", error);
89         }
90
91         completed = true;
92         fprintf(stderr, "Destory - destroy image\n");
93
94         _signal();
95
96         return TRUE;
97 }
98
99 int main(int argc, char *argv[])
100 {
101         int ret = 0;
102         void *src;
103         unsigned char *dst = NULL;
104
105         if (argc < 12) {
106                 fprintf(stderr, "Usage: %s sync {filename} {command} src_width src_height src_foramt dst_width dst_height dst_format roation crop_start_x crop_start_y \n", argv[0]);
107                 fprintf(stderr, "Usage: %s async {filename} {command} src_width src_height src_foramt dst_width dst_height dst_format roation crop_start_x crop_start_y \n", argv[0]);
108                 fprintf(stderr, "ex: %s sync test.rgb resize 1920 1080 7 1280 720 7 0 0 0 \n", argv[0]);
109                 return ret;
110         }
111
112         size_t src_size = 0;
113         size_t dst_size = 0;
114         bool sync_mode = (strcmp(argv[1], "sync") == 0) ? TRUE : FALSE;
115         char *filename = strdup(argv[2]);
116         char *command = strdup(argv[3]);
117         unsigned int src_width = (unsigned int)atoi(argv[4]);
118         unsigned int src_height = (unsigned int)atoi(argv[5]);
119         mm_util_color_format_e src_format = atoi(argv[6]);
120         unsigned int dst_width = (unsigned int)atoi(argv[7]);
121         unsigned int dst_height = (unsigned int)atoi(argv[8]);
122         mm_util_color_format_e dst_format = atoi(argv[9]);
123         mm_util_img_rotate_type rotation = atoi(argv[10]);
124         unsigned int start_x = (unsigned int)atoi(argv[11]);
125         unsigned int start_y = (unsigned int)atoi(argv[12]);
126         char output_file[40] = {};
127         unsigned int res_w = 0;
128         unsigned int res_h = 0;
129         unsigned char *res_buffer = NULL;
130         size_t res_buffer_size = 0;
131
132         /* async mode */
133         mm_util_color_image_h orig_image = NULL;
134
135         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);
136
137         if (strcmp(command, "convert") == 0) {
138                 if ((src_width != dst_width) || (src_width != dst_width)) {
139                         fprintf(stderr, "Wrong input. dst w/h should be same as src w/h\n");
140                         return 0;
141                 }
142         }
143
144         /* mem allocation for src dst buffer */
145         mm_util_get_image_size(src_format, src_width, src_height, &src_size);
146         mm_util_get_image_size(dst_format, dst_width, dst_height, &dst_size);
147         src = calloc(1, src_size);
148         dst = calloc(1, dst_size);
149
150         { /* read input file */
151                 FILE *fp = fopen(filename, "r");
152                 if (fp == NULL) {
153                         fprintf(stderr, "\tfile open failed %d\n", errno);
154                         goto TEST_FAIL;
155                 }
156
157                 if (src == NULL) {
158                         fprintf(stderr, "\tmemory allocation failed\n");
159                         goto TEST_FAIL;
160                 }
161
162                 if (fread(src, 1, src_size, fp) == src_size)
163                         fprintf(stderr, "#Success# fread\n");
164                 else
165                         fprintf(stderr, "#Error# fread\n");
166
167                 if (src == NULL || src_size <= 0) {
168                         fprintf(stderr, "#Error# fread\n");
169                         goto TEST_FAIL;
170                 }
171
172         }
173
174         {  /* ready output file */
175                 char *output_fmt = (char *) calloc(1, sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
176                 memset(output_fmt, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
177                 if (dst_format == MM_UTIL_COLOR_YUV420 ||
178                         dst_format == MM_UTIL_COLOR_YUV422 ||
179                         dst_format == MM_UTIL_COLOR_I420) {
180                         strncpy(output_fmt, "yuv", strlen("yuv"));
181                 } else {
182                         //strncpy(output_fmt, "rgb", strlen("rgb"));
183                         strncpy(output_fmt, "rgb", strlen("raw"));
184                 }
185                 snprintf(output_file, 40, "result_%s_%dx%d.%s", command, dst_width, dst_height, output_fmt);
186         }
187
188         if (sync_mode) {
189                 fprintf(stderr, "SYNC\n");
190                 if (strcmp(command, "convert") == 0) {
191                         ret = mm_util_convert_colorspace(src, src_width, src_height, src_format, dst, dst_format);
192                 } else if (strcmp(command, "resize") == 0) {
193                         ret = mm_util_resize_image(src, src_width, src_height, src_format, dst_width, dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
194                         IMGP_FREE(dst);
195                         dst = res_buffer;
196                 } else if (strcmp(command, "rotate") == 0) {
197                         ret = mm_util_rotate_image(src, src_width, src_height, src_format, rotation, &res_buffer, &res_w, &res_h, &res_buffer_size);
198                         IMGP_FREE(dst);
199                         dst = res_buffer;
200                 } else if (strcmp(command, "crop") == 0) {
201                         ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, dst_width, dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
202                         IMGP_FREE(dst);
203                         dst = res_buffer;
204                 }
205
206                 if (ret == MM_UTIL_ERROR_NONE) {
207                         fprintf(stderr, "Success - %s\n", command);
208                 } else {
209                         fprintf(stderr, "ERROR - %s\n", command);
210                         goto TEST_FAIL;
211                 }
212
213                 {  /* write output file */
214                         FILE *fpout = fopen(output_file, "w");
215                         if (fpout) {
216                                 fprintf(stderr, "dst: %p [%zu]\n", dst, dst_size);
217                                 fwrite(dst, 1, dst_size, fpout);
218                                 fprintf(stderr, "FREE\n");
219                                 fclose(fpout);
220                         }
221                 }
222         } else {  /* Async mode */
223                 fprintf(stderr, "ASYNC\n");
224
225                 /* Create Transform */
226                 ret = mm_util_create(&imgp_handle);
227                 if (ret == MM_UTIL_ERROR_NONE) {
228                         fprintf(stderr, "Success - Create Transcode Handle [imgp_handle: %p]\n", imgp_handle);
229                 } else {
230                         fprintf(stderr, "ERROR - Create Transcode Handle\n");
231                         goto TEST_FAIL;
232                 }
233
234                 ret = mm_util_create_color_image(&orig_image, src_width, src_height, src_format, src, src_size);
235                 if (ret == MM_UTIL_ERROR_NONE) {
236                         fprintf(stderr, "Success - mm_util_create_color_image\n");
237                 } else {
238                         fprintf(stderr, "ERROR - mm_util_create_color_image\n");
239                         goto TEST_FAIL;
240                 }
241
242                 if (strcmp(command, "convert") == 0) {
243                         ret = mm_util_set_colorspace_convert(imgp_handle, dst_format);
244                         if (ret == MM_UTIL_ERROR_NONE) {
245                                 fprintf(stderr, "Success - Set colorspace Info\n");
246                         } else {
247                                 fprintf(stderr, "ERROR - Set colorspace Info\n");
248                                 goto TEST_FAIL;
249                         }
250                 }
251
252                 if (strcmp(command, "crop") == 0) {
253                         ret = mm_util_set_crop_area(imgp_handle, start_x, start_y, (start_x + dst_width), (start_y + dst_height));
254                         if (ret == MM_UTIL_ERROR_NONE) {
255                                 fprintf(stderr, "Success - Set crop Info\n");
256                         } else {
257                                 fprintf(stderr, "ERROR - Set crop Info\n");
258                                 goto TEST_FAIL;
259                         }
260                 }
261
262                 if (strcmp(command, "resize") == 0) {
263                         ret = mm_util_set_resolution(imgp_handle, dst_width, dst_height);
264                         if (ret == MM_UTIL_ERROR_NONE) {
265                                 fprintf(stderr, "Success - Set resolution Info\n");
266                         } else {
267                                 fprintf(stderr, "ERROR - Set resolution Info\n");
268                                 goto TEST_FAIL;
269                         }
270                 }
271
272                 if (strcmp(command, "rotate") == 0) {
273                         ret = mm_util_set_rotation(imgp_handle, rotation);
274                         if (ret == MM_UTIL_ERROR_NONE) {
275                                 fprintf(stderr, "Success - Set rotation Info\n");
276                         } else {
277                                 fprintf(stderr, "ERROR - Set rotation Info\n");
278                                 goto TEST_FAIL;
279                         }
280                 }
281
282                 /* Transform */
283                 ret = mm_util_transform(imgp_handle, orig_image, (mm_util_completed_callback) _transform_completed_cb, output_file);
284                 if (ret == MM_UTIL_ERROR_NONE) {
285                         fprintf(stderr, "Success - Transform\n");
286                 } else {
287                         fprintf(stderr, "ERROR - Transform\n");
288                         goto TEST_FAIL;
289                 }
290
291                 fprintf(stderr, "Wait...\n");
292                 g_mutex_init(&g_thread_mutex);
293                 g_cond_init(&g_thread_cond);
294                 _wait();
295                 g_mutex_clear(&g_thread_mutex);
296                 g_cond_clear(&g_thread_cond);
297
298                 ret = mm_util_destroy(imgp_handle);
299                 if (ret == MM_UTIL_ERROR_NONE) {
300                         fprintf(stderr, "Success - Destroy\n");
301                 } else {
302                         fprintf(stderr, "ERROR - Destroy\n");
303                         goto TEST_FAIL;
304                 }
305
306         }
307 TEST_FAIL:
308
309         IMGP_FREE(src);
310         IMGP_FREE(dst);
311         IMGP_FREE(command);
312         IMGP_FREE(filename);
313         if (!sync_mode) {
314                 mm_util_destroy_color_image(orig_image);
315                 fprintf(stderr, "destroy\n");
316         }
317
318         return 0;
319 }
320