8433b85472b8d788263762a90c0bb63d55ded952
[platform/core/api/image-util.git] / src / image_util_private.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_private.h>
18
19 #include <mm_util_imgp.h>
20 #include <mm_util_jpeg.h>
21 #include <mm_util_png.h>
22 #include <mm_util_gif.h>
23 #include <mm_util_bmp.h>
24
25 static int _convert_colorspace_tbl[] = {
26         MM_UTIL_IMG_FMT_YUV420,         /* IMAGE_UTIL_COLORSPACE_YUV420 */
27         MM_UTIL_IMG_FMT_YUV422,         /* IMAGE_UTIL_COLORSPACE_YUV422 */
28         MM_UTIL_IMG_FMT_I420,           /* IMAGE_UTIL_COLORSPACE_I420 */
29         MM_UTIL_IMG_FMT_NV12,           /* IMAGE_UTIL_COLORSPACE_NV12 */
30         MM_UTIL_IMG_FMT_UYVY,           /* IMAGE_UTIL_COLORSPACE_UYVY */
31         MM_UTIL_IMG_FMT_YUYV,           /* IMAGE_UTIL_COLORSPACE_YUYV */
32         MM_UTIL_IMG_FMT_RGB565,         /* IMAGE_UTIL_COLORSPACE_RGB565 */
33         MM_UTIL_IMG_FMT_RGB888,         /* IMAGE_UTIL_COLORSPACE_RGB888 */
34         MM_UTIL_IMG_FMT_ARGB8888,       /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
35         MM_UTIL_IMG_FMT_BGRA8888,       /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
36         MM_UTIL_IMG_FMT_RGBA8888,       /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
37         MM_UTIL_IMG_FMT_BGRX8888,       /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
38         MM_UTIL_JPEG_FMT_NV21,          /* IMAGE_UTIL_COLORSPACE_NV12 */
39         MM_UTIL_JPEG_FMT_NV16,          /* IMAGE_UTIL_COLORSPACE_NV16 */
40         MM_UTIL_JPEG_FMT_NV61,          /* IMAGE_UTIL_COLORSPACE_NV61 */
41 };
42
43 static int _convert_jpeg_colorspace_tbl[] = {
44         MM_UTIL_JPEG_FMT_YUV420,        /* IMAGE_UTIL_COLORSPACE_YUV420 */
45         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV422 */
46         MM_UTIL_JPEG_FMT_YUV420,        /* IMAGE_UTIL_COLORSPACE_I420 */
47         MM_UTIL_JPEG_FMT_NV12,          /* IMAGE_UTIL_COLORSPACE_NV12 */
48         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_UYVY */
49         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUYV */
50         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB565 */
51         MM_UTIL_JPEG_FMT_RGB888,        /* IMAGE_UTIL_COLORSPACE_RGB888 */
52         MM_UTIL_JPEG_FMT_ARGB8888,      /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
53         MM_UTIL_JPEG_FMT_BGRA8888,      /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
54         MM_UTIL_JPEG_FMT_RGBA8888,      /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
55         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
56         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV21 */
57         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV16 */
58         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV61 */
59 };
60
61 static int _convert_png_colorspace_tbl[] = {
62         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV420 */
63         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV422 */
64         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_I420 */
65         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV12 */
66         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_UYVY */
67         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUYV */
68         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB565 */
69         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB888 */
70         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
71         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
72         MM_UTIL_PNG_COLOR_TYPE_RGB_ALPHA,       /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
73         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
74         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV21 */
75         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV16 */
76         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV61 */
77 };
78
79 static int _convert_gif_colorspace_tbl[] = {
80         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV420 */
81         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV422 */
82         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_I420 */
83         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV12 */
84         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_UYVY */
85         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUYV */
86         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB565 */
87         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB888 */
88         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
89         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
90         MM_UTIL_GIF_FMT_RGBA8888,               /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
91         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
92         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV21 */
93         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV16 */
94         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV61 */
95 };
96
97 static int _convert_bmp_colorspace_tbl[] = {
98         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV420 */
99         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUV422 */
100         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_I420 */
101         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV12 */
102         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_UYVY */
103         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_YUYV */
104         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB565 */
105         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_RGB888 */
106         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
107         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
108         MM_UTIL_BMP_FMT_RGBA8888,       /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
109         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
110         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV21 */
111         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV16 */
112         _NOT_SUPPORTED_COLORSPACE,      /* IMAGE_UTIL_COLORSPACE_NV61 */
113 };
114
115 unsigned int get_number_of_colorspace(void)
116 {
117         return (unsigned int)(sizeof(_convert_colorspace_tbl)/sizeof(int));
118 }
119
120 gboolean is_valid_colorspace(image_util_colorspace_e colorspace)
121 {
122         if ((colorspace < IMAGE_UTIL_COLORSPACE_YV12) || (colorspace >= NUM_OF_COLORSPACE))
123                 return FALSE;
124
125         return TRUE;
126 }
127
128 gboolean is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type)
129 {
130         switch (type) {
131         case IMAGE_UTIL_JPEG:
132                 image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[jpeg] %d not supported format", colorspace);
133                 break;
134         case IMAGE_UTIL_PNG:
135                 image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[png] %d not supported format", colorspace);
136                 break;
137         case IMAGE_UTIL_GIF:
138                 image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[gif] %d not supported format", colorspace);
139                 break;
140         case IMAGE_UTIL_BMP:
141                 image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == _NOT_SUPPORTED_COLORSPACE), FALSE, "[bmp] %d not supported format", colorspace);
142                 break;
143         default:
144                 image_util_retvm_if(TRUE, FALSE, "Invalid image type");
145         }
146         return TRUE;
147 }
148
149 int convert_type_of_colorspace(const image_util_colorspace_e colorspace)
150 {
151         if (is_valid_colorspace(colorspace) == FALSE) {
152                 return _NOT_SUPPORTED_COLORSPACE;
153         }
154
155         return _convert_colorspace_tbl[colorspace];
156 }
157
158 int convert_type_of_colorspace_with_image_type(const image_util_colorspace_e colorspace, const image_util_type_e type)
159 {
160         int new_colorspace = _NOT_SUPPORTED_COLORSPACE;
161
162         if (is_valid_colorspace(colorspace) == FALSE) {
163                 return _NOT_SUPPORTED_COLORSPACE;
164         }
165
166         if (is_supported_colorspace(colorspace, type) == FALSE) {
167                 return _NOT_SUPPORTED_COLORSPACE;
168         }
169
170         switch (type) {
171         case IMAGE_UTIL_JPEG:
172                 new_colorspace = _convert_jpeg_colorspace_tbl[colorspace];
173                 break;
174         case IMAGE_UTIL_PNG:
175                 new_colorspace = _convert_png_colorspace_tbl[colorspace];
176                 break;
177         case IMAGE_UTIL_GIF:
178                 new_colorspace = _convert_gif_colorspace_tbl[colorspace];
179                 break;
180         case IMAGE_UTIL_BMP:
181                 new_colorspace = _convert_bmp_colorspace_tbl[colorspace];
182                 break;
183         default:
184                 image_util_error("Invalid image type");
185         }
186
187         return new_colorspace;
188 }
189
190 int _convert_image_util_error_code(const char *func, int code)
191 {
192         int ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
193         char *errorstr = NULL;
194         switch (code) {
195         case MM_UTIL_ERROR_NONE:
196                 ret = IMAGE_UTIL_ERROR_NONE;
197                 errorstr = strdup("ERROR_NONE");
198                 break;
199         case MM_UTIL_ERROR_NO_SUCH_FILE:
200                 ret = IMAGE_UTIL_ERROR_NO_SUCH_FILE;
201                 errorstr = strdup("NO_SUCH_FILE");
202                 break;
203         case MM_UTIL_ERROR_INVALID_PARAMETER:
204                 ret = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
205                 errorstr = strdup("INVALID_PARAMETER");
206                 break;
207         case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
208                 ret = IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
209                 errorstr = strdup("NOT_SUPPORTED_FORMAT");
210                 break;
211         case MM_UTIL_ERROR_OUT_OF_MEMORY:
212                 ret = IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
213                 errorstr = strdup("OUT_OF_MEMORY");
214                 break;
215         case MM_UTIL_ERROR_INVALID_OPERATION:
216         default:
217                 ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
218                 errorstr = strdup("INVALID_OPERATION");
219
220         }
221
222         image_util_debug("[%s] %s(0x%08x)", func, errorstr, ret);
223         IMAGE_UTIL_SAFE_FREE(errorstr);
224         return ret;
225 }
226
227 image_util_error_e _image_util_error_convert(int error)
228 {
229         switch (error) {
230         case MM_UTIL_ERROR_NONE:
231                 image_util_debug("Error None");
232                 return IMAGE_UTIL_ERROR_NONE;
233         case MM_UTIL_ERROR_INVALID_PARAMETER:
234                 image_util_error("INVALID_PARAMETER(0x%08x)", IMAGE_UTIL_ERROR_INVALID_PARAMETER);
235                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
236         case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
237                 image_util_error("NOT_SUPPORTED_FORMAT(0x%08x)", IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT);
238                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
239         case MM_UTIL_ERROR_OUT_OF_MEMORY:
240                 image_util_error("OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
241                 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
242                 break;
243         case MM_UTIL_ERROR_INVALID_OPERATION:
244         default:
245                 image_util_error("INVALID_OPERATION(0x%08x)", error);
246                 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
247         }
248 }
249
250 bool _image_util_check_resolution(int width, int height)
251 {
252         if (width <= 0) {
253                 image_util_error("invalid width [%d]", width);
254                 return false;
255         }
256
257         if (height <= 0) {
258                 image_util_error("invalid height [%d]", height);
259                 return false;
260         }
261
262         return true;
263 }