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_common.h>
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; } }
35 mm_util_imgp_h imgp_handle = NULL;
36 bool completed = false;
39 GMutex g_thread_mutex;
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);
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);
58 bool _transform_completed_cb(mm_util_color_image_h image, int error, void *user_data)
60 int err = MM_UTIL_ERROR_NONE;
62 char* output_file = (char *)user_data;
63 fprintf(stderr, "imgp_handle: %p\n", imgp_handle);
65 fprintf(stderr, "output_file: %s\n", output_file);
67 if (error == MM_UTIL_ERROR_NONE) {
68 fprintf(stderr, "completed\n");
69 FILE *fp = fopen(output_file, "w");
72 err = mm_util_get_color_image(image, NULL, NULL, NULL, &dst, &size);
73 if (err != MM_UTIL_ERROR_NONE) {
76 fprintf(stderr, "Error mm_util_get_color_image (%d)\n", err);
80 fprintf(stderr, "dst: %p [%zu]\n", dst, size);
81 fwrite(dst, 1, size, fp);
82 fprintf(stderr, "FREE\n");
86 fprintf(stderr, "write result\n");
88 fprintf(stderr, "[Error] complete cb (%d)\n", error);
92 fprintf(stderr, "Destory - destroy image\n");
99 int main(int argc, char *argv[])
103 unsigned char *dst = NULL;
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]);
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;
133 mm_util_color_image_h orig_image = NULL;
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);
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");
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);
150 { /* read input file */
151 FILE *fp = fopen(filename, "r");
153 fprintf(stderr, "\tfile open failed %d\n", errno);
158 fprintf(stderr, "\tmemory allocation failed\n");
162 if (fread(src, 1, src_size, fp) == src_size)
163 fprintf(stderr, "#Success# fread\n");
165 fprintf(stderr, "#Error# fread\n");
167 if (src == NULL || src_size <= 0) {
168 fprintf(stderr, "#Error# fread\n");
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"));
182 //strncpy(output_fmt, "rgb", strlen("rgb"));
183 strncpy(output_fmt, "rgb", strlen("raw"));
185 snprintf(output_file, 40, "result_%s_%dx%d.%s", command, dst_width, dst_height, output_fmt);
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);
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);
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);
206 if (ret == MM_UTIL_ERROR_NONE) {
207 fprintf(stderr, "Success - %s\n", command);
209 fprintf(stderr, "ERROR - %s\n", command);
213 { /* write output file */
214 FILE *fpout = fopen(output_file, "w");
216 fprintf(stderr, "dst: %p [%zu]\n", dst, dst_size);
217 fwrite(dst, 1, dst_size, fpout);
218 fprintf(stderr, "FREE\n");
222 } else { /* Async mode */
223 fprintf(stderr, "ASYNC\n");
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);
230 fprintf(stderr, "ERROR - Create Transcode Handle\n");
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");
238 fprintf(stderr, "ERROR - mm_util_create_color_image\n");
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");
247 fprintf(stderr, "ERROR - Set colorspace Info\n");
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");
257 fprintf(stderr, "ERROR - Set crop Info\n");
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");
267 fprintf(stderr, "ERROR - Set resolution Info\n");
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");
277 fprintf(stderr, "ERROR - Set rotation Info\n");
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");
287 fprintf(stderr, "ERROR - Transform\n");
291 fprintf(stderr, "Wait...\n");
292 g_mutex_init(&g_thread_mutex);
293 g_cond_init(&g_thread_cond);
295 g_mutex_clear(&g_thread_mutex);
296 g_cond_clear(&g_thread_cond);
298 ret = mm_util_destroy(imgp_handle);
299 if (ret == MM_UTIL_ERROR_NONE) {
300 fprintf(stderr, "Success - Destroy\n");
302 fprintf(stderr, "ERROR - Destroy\n");
314 mm_util_destroy_color_image(orig_image);
315 fprintf(stderr, "destroy\n");