2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <image_util.h>
18 #include <image_util_internal.h>
19 #include <image_util_private.h>
21 #include <mm_util_gif.h>
23 int image_util_convert_colorspace(unsigned char *dest, image_util_colorspace_e dest_colorspace, const unsigned char *src, int width, int height, image_util_colorspace_e src_colorspace)
25 int err = MM_UTIL_ERROR_NONE;
27 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
28 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
29 image_util_retvm_if((is_valid_colorspace(dest_colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dst_colorspace");
30 image_util_retvm_if((is_valid_colorspace(src_colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid src_colorspace");
32 err = mm_util_convert_colorspace(src, width, height, TYPECAST_COLOR(src_colorspace), dest, TYPECAST_COLOR(dest_colorspace));
34 return _image_error_capi(ERR_TYPE_TRANSFORM, err);
37 int image_util_resize(unsigned char *dest, int *dest_width, int *dest_height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
39 int err = MM_UTIL_ERROR_NONE;
41 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
42 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
43 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
44 image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
45 image_util_retvm_if((*dest_width <= 0 || *dest_height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest_width or Invalid dest_height");
47 unsigned int dest_w, dest_h;
49 dest_h = *dest_height;
50 err = mm_util_resize_image(src, src_width, src_height, TYPECAST_COLOR(colorspace), dest, &dest_w, &dest_h);
51 if (err == MM_UTIL_ERROR_NONE) {
52 *dest_width = (int)dest_w;
53 *dest_height = (int)dest_h;
56 return _image_error_capi(ERR_TYPE_TRANSFORM, err);
59 int image_util_rotate(unsigned char *dest, int *dest_width, int *dest_height, image_util_rotation_e dest_rotation, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
61 int err = MM_UTIL_ERROR_NONE;
63 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
64 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
65 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
66 image_util_retvm_if((dest_rotation < 0 || dest_rotation > IMAGE_UTIL_ROTATION_FLIP_VERT), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid rotation");
67 image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
69 unsigned int dest_w, dest_h;
70 err = mm_util_rotate_image(src, src_width, src_height, TYPECAST_COLOR(colorspace), dest, &dest_w, &dest_h, dest_rotation);
71 if (err == MM_UTIL_ERROR_NONE) {
72 *dest_width = (int)dest_w;
73 *dest_height = (int)dest_h;
75 return _image_error_capi(ERR_TYPE_TRANSFORM, err);
78 int image_util_crop(unsigned char *dest, int x, int y, int *width, int *height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
80 int err = MM_UTIL_ERROR_NONE;
82 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
83 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
84 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
85 image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "width or height is null");
86 image_util_retvm_if((src_width <= x || src_height <= y || src_width < x + *width || src_height < y + *height), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid crop area");
88 unsigned int dest_w, dest_h;
91 err = mm_util_crop_image(src, src_width, src_height, TYPECAST_COLOR(colorspace), x, y, &dest_w, &dest_h, dest);
92 if (err == MM_UTIL_ERROR_NONE) {
94 *height = (int)dest_h;
97 return _image_error_capi(ERR_TYPE_TRANSFORM, err);
100 int image_util_frame_create(void *decode_encode_h, image_util_frame_h *frame_h)
102 int ret = MM_UTIL_ERROR_NONE;
104 image_util_retvm_if((decode_encode_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
105 image_util_retvm_if((frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
107 decode_encode_s *image = (decode_encode_s *)decode_encode_h;
108 image_util_retvm_if((image->image_h == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
110 frame_s *frame = calloc(1, sizeof(frame_s));
112 image_util_error("Memory allocation is failed.");
113 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
116 if (image->image_type == IMAGE_UTIL_GIF) {
117 mm_gif_file_h gif_data = (mm_gif_file_h)image->image_h;
118 mm_gif_image_h gif_frame = NULL;
120 ret = mm_util_gif_image_create(gif_data, &gif_frame);
121 if (ret != MM_UTIL_ERROR_NONE) {
122 image_util_error("mm_util_gif_image_create is failed(%d).", ret);
123 IMAGE_UTIL_SAFE_FREE(frame);
124 return _image_error_capi(ERR_TYPE_ENCODE, ret);
126 frame->frame_h = gif_frame;
128 image_util_error("The image type(%d) is not supported.", image->image_type);
129 IMAGE_UTIL_SAFE_FREE(frame);
130 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
135 return IMAGE_UTIL_ERROR_NONE;
138 int image_util_frame_set_resolution(image_util_frame_h frame_h, const int width, const int height)
140 int ret = MM_UTIL_ERROR_NONE;
142 image_util_retvm_if((frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
143 image_util_retvm_if((width <= 0) || (height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Width or Height");
145 frame_s *frame = (frame_s *)frame_h;
147 ret = mm_util_gif_image_set_position(frame->frame_h, 0, 0, width, height);
148 if (ret != MM_UTIL_ERROR_NONE) {
149 image_util_error("mm_util_gif_image_set_position is failed(%d).", ret);
150 return _image_error_capi(ERR_TYPE_ENCODE, ret);
153 return IMAGE_UTIL_ERROR_NONE;
156 int image_util_frame_set_gif_delay(image_util_frame_h frame_h, const int delay_time)
158 int ret = MM_UTIL_ERROR_NONE;
160 image_util_retvm_if((frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
161 image_util_retvm_if((delay_time <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Delay Time");
163 frame_s *frame = (frame_s *)frame_h;
165 ret = mm_util_gif_image_set_delay_time(frame->frame_h, delay_time);
166 if (ret != MM_UTIL_ERROR_NONE) {
167 image_util_error("mm_util_gif_image_set_delay_time is failed(%d).", ret);
168 return _image_error_capi(ERR_TYPE_ENCODE, ret);
171 return IMAGE_UTIL_ERROR_NONE;
174 int image_util_frame_set_frame(image_util_frame_h frame_h, unsigned char *buffer)
176 int ret = MM_UTIL_ERROR_NONE;
178 image_util_retvm_if((frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
179 image_util_retvm_if((buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Buffer");
181 frame_s *frame = (frame_s *)frame_h;
183 ret = mm_util_gif_image_set_image(frame->frame_h, buffer);
184 if (ret != MM_UTIL_ERROR_NONE) {
185 image_util_error("mm_util_gif_image_set_image is failed(%d).", ret);
186 return _image_error_capi(ERR_TYPE_ENCODE, ret);
189 return IMAGE_UTIL_ERROR_NONE;
192 void image_util_frame_destroy(image_util_frame_h frame_h)
194 image_util_retm_if((frame_h == NULL), "Invalid Handle");
196 frame_s *frame = (frame_s *)frame_h;
197 mm_util_gif_image_destory(frame->frame_h);
199 IMAGE_UTIL_SAFE_FREE(frame);
202 int image_util_encode_add_frame(image_util_encode_h encode_h, image_util_frame_h frame_h)
204 int ret = MM_UTIL_ERROR_NONE;
206 image_util_retvm_if((encode_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
207 image_util_retvm_if((frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
209 decode_encode_s *encode = (decode_encode_s *)encode_h;
210 frame_s *frame = (frame_s *)frame_h;
211 image_util_retvm_if((encode->image_h == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
212 image_util_retvm_if((encode->image_type != IMAGE_UTIL_GIF), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "The image type(%d) is not supported.", encode->image_type);
213 image_util_retvm_if((frame->frame_h == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The frame handle is wrong");
215 mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
217 if (encode->current_buffer_count == 0) {
219 ret = mm_util_gif_encode_set_file(gif_data, encode->path);
221 ret = mm_util_gif_encode_set_mem(gif_data, encode->dst_buffer, &encode->gif_encode_size);
223 if (ret != MM_UTIL_ERROR_NONE) {
224 image_util_error("mm_util_gif_encode_add_image is failed(%d).", ret);
225 return _image_error_capi(ERR_TYPE_ENCODE, ret);
228 ret = mm_util_gif_encode_add_image(gif_data, (mm_gif_image_h)frame->frame_h);
229 if (ret != MM_UTIL_ERROR_NONE) {
230 image_util_error("mm_util_gif_encode_add_image is failed(%d).", ret);
231 return _image_error_capi(ERR_TYPE_ENCODE, ret);
233 encode->current_buffer_count++;
235 return IMAGE_UTIL_ERROR_NONE;
238 int image_util_encode_save(image_util_encode_h encode_h, unsigned long long *size)
240 int ret = MM_UTIL_ERROR_NONE;
242 image_util_retvm_if((encode_h == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
244 decode_encode_s *encode = (decode_encode_s *)encode_h;
245 image_util_retvm_if((encode->image_h == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
246 image_util_retvm_if((encode->image_type != IMAGE_UTIL_GIF), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "The image type(%d) is not supported.", encode->image_type);
248 mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
250 ret = mm_util_gif_encode_save(gif_data);
251 if (ret != MM_UTIL_ERROR_NONE) {
252 image_util_error("mm_util_gif_encode_save is failed(%d).", ret);
253 mm_util_gif_encode_destroy(gif_data);
254 return _image_error_capi(ERR_TYPE_ENCODE, ret);
257 *size = (unsigned long long)encode->gif_encode_size;
258 encode->current_buffer_count = 0;
260 return IMAGE_UTIL_ERROR_NONE;