50afd428eee14c47fc42948d21f21e32c586c1cd
[platform/core/api/image-util.git] / src / image_util_internal.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <image_util.h>
18 #include <image_util_internal.h>
19 #include <image_util_private.h>
20
21 #include <mm_util_gif.h>
22
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)
24 {
25         int err = MM_UTIL_ERROR_NONE;
26
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");
31
32         err = mm_util_convert_colorspace(src, width, height, TYPECAST_COLOR(src_colorspace), dest, TYPECAST_COLOR(dest_colorspace));
33
34         return _convert_image_util_error_code(__func__, err);
35 }
36
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)
38 {
39         int err = MM_UTIL_ERROR_NONE;
40
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");
46
47         unsigned int dest_w, dest_h;
48         dest_w = *dest_width;
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;
54         }
55
56         return _convert_image_util_error_code(__func__, err);
57 }
58
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)
60 {
61         int err = MM_UTIL_ERROR_NONE;
62
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");
68
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;
74         }
75         return _convert_image_util_error_code(__func__, err);
76 }
77
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)
79 {
80         int err = MM_UTIL_ERROR_NONE;
81
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");
87
88         unsigned int dest_w, dest_h;
89         dest_w = *width;
90         dest_h = *height;
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) {
93                 *width = (int)dest_w;
94                 *height = (int)dest_h;
95         }
96
97         return _convert_image_util_error_code(__func__, err);
98 }
99
100 int image_util_frame_create(void *decode_encode_h, image_util_frame_h *frame_h)
101 {
102         int ret = IMAGE_UTIL_ERROR_NONE;
103
104         image_util_retvm_if((decode_encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
105         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
106
107         decode_encode_s *image = (decode_encode_s *)decode_encode_h;
108         image_util_retvm_if((image->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
109
110         frame_s *frame = calloc(1, sizeof(frame_s));
111         if (frame == NULL) {
112                 image_util_error("Memory allocation is failed.");
113                 return MM_UTIL_ERROR_OUT_OF_MEMORY;
114         }
115
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;
119
120                 ret = mm_util_gif_image_create(gif_data, &gif_frame);
121                 if (ret != IMAGE_UTIL_ERROR_NONE) {
122                         image_util_error("mm_util_gif_image_create is failed(%d).", ret);
123                         IMAGE_UTIL_SAFE_FREE(frame);
124                         return MM_UTIL_ERROR_INVALID_OPERATION;
125                 }
126                 frame->frame_h = gif_frame;
127         } else {
128                 image_util_error("The image type(%d) is not supported.", image->image_type);
129                 IMAGE_UTIL_SAFE_FREE(frame);
130                 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
131         }
132
133         *frame_h = frame;
134
135         return IMAGE_UTIL_ERROR_NONE;
136 }
137
138 int image_util_frame_set_resolution(image_util_frame_h frame_h, const int width, const int height)
139 {
140         int ret = IMAGE_UTIL_ERROR_NONE;
141
142         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
143         image_util_retvm_if((width <= 0) || (height <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Width or Height");
144
145         frame_s *frame = (frame_s *)frame_h;
146
147         ret = mm_util_gif_image_set_position(frame->frame_h, 0, 0, width, height);
148         if (ret != IMAGE_UTIL_ERROR_NONE) {
149                 image_util_error("mm_util_gif_image_set_position is failed(%d).", ret);
150                 return MM_UTIL_ERROR_INVALID_OPERATION;
151         }
152
153         return IMAGE_UTIL_ERROR_NONE;
154 }
155
156 int image_util_frame_set_gif_delay(image_util_frame_h frame_h, const int delay_time)
157 {
158         int ret = IMAGE_UTIL_ERROR_NONE;
159
160         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
161         image_util_retvm_if((delay_time <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Delay Time");
162
163         frame_s *frame = (frame_s *)frame_h;
164
165         ret = mm_util_gif_image_set_delay_time(frame->frame_h, delay_time);
166         if (ret != IMAGE_UTIL_ERROR_NONE) {
167                 image_util_error("mm_util_gif_image_set_delay_time is failed(%d).", ret);
168                 return MM_UTIL_ERROR_INVALID_OPERATION;
169         }
170
171         return IMAGE_UTIL_ERROR_NONE;
172 }
173
174 int image_util_frame_set_frame(image_util_frame_h frame_h, unsigned char *buffer)
175 {
176         int ret = IMAGE_UTIL_ERROR_NONE;
177
178         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
179         image_util_retvm_if((buffer == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Buffer");
180
181         frame_s *frame = (frame_s *)frame_h;
182
183         ret = mm_util_gif_image_set_image(frame->frame_h, buffer);
184         if (ret != IMAGE_UTIL_ERROR_NONE) {
185                 image_util_error("mm_util_gif_image_set_image is failed(%d).", ret);
186                 return MM_UTIL_ERROR_INVALID_OPERATION;
187         }
188
189         return IMAGE_UTIL_ERROR_NONE;
190 }
191
192 void image_util_frame_destroy(image_util_frame_h frame_h)
193 {
194         image_util_retm_if((frame_h == NULL), "Invalid Handle");
195
196         frame_s *frame = (frame_s *)frame_h;
197         mm_util_gif_image_destory(frame->frame_h);
198 }
199
200 int image_util_encode_add_frame(image_util_encode_h encode_h, image_util_frame_h frame_h)
201 {
202         int ret = IMAGE_UTIL_ERROR_NONE;
203
204         image_util_retvm_if((encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
205         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
206
207         decode_encode_s *encode = (decode_encode_s *)encode_h;
208         frame_s *frame = (frame_s *)frame_h;
209         image_util_retvm_if((encode->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
210         image_util_retvm_if((encode->image_type != IMAGE_UTIL_GIF), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "The image type(%d) is not supported.", encode->image_type);
211         image_util_retvm_if((frame->frame_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The frame handle is wrong");
212
213         mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
214
215         if (encode->current_buffer_count == 0) {
216                 if (encode->path)
217                         ret = mm_util_gif_encode_set_file(gif_data, encode->path);
218                 else
219                         ret = mm_util_gif_encode_set_mem(gif_data, encode->dst_buffer, &encode->gif_encode_size);
220         }
221         if (ret != IMAGE_UTIL_ERROR_NONE) {
222                 image_util_error("mm_util_gif_encode_add_image is failed(%d).", ret);
223                 return MM_UTIL_ERROR_INVALID_OPERATION;
224         }
225
226         ret = mm_util_gif_encode_add_image(gif_data, (mm_gif_image_h)frame->frame_h);
227         if (ret != IMAGE_UTIL_ERROR_NONE) {
228                 image_util_error("mm_util_gif_encode_add_image is failed(%d).", ret);
229                 return MM_UTIL_ERROR_INVALID_OPERATION;
230         }
231         encode->current_buffer_count++;
232
233         return IMAGE_UTIL_ERROR_NONE;
234 }
235
236 int image_util_encode_save(image_util_encode_h encode_h, unsigned long long *size)
237 {
238         int ret = IMAGE_UTIL_ERROR_NONE;
239
240         image_util_retvm_if((encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
241
242         decode_encode_s *encode = (decode_encode_s *)encode_h;
243         image_util_retvm_if((encode->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
244         image_util_retvm_if((encode->image_type != IMAGE_UTIL_GIF), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "The image type(%d) is not supported.", encode->image_type);
245
246         mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
247
248         ret = mm_util_gif_encode_save(gif_data);
249         if (ret != IMAGE_UTIL_ERROR_NONE) {
250                 image_util_error("mm_util_gif_encode_save is failed(%d).", ret);
251                 mm_util_gif_encode_destroy(gif_data);
252                 return MM_UTIL_ERROR_INVALID_OPERATION;
253         }
254
255         *size = (unsigned long long)encode->gif_encode_size;
256         encode->current_buffer_count = 0;
257
258         return IMAGE_UTIL_ERROR_NONE;
259 }