Modify converting to capi error and simplify error type use
[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 _image_error_capi(image_util_error_type_e error_type, int error_code)
191 {
192         if (error_code != MM_UTIL_ERROR_NONE)
193                 image_util_error("[type : %d] error : %d ", error_type, error_code);
194
195         /*Error None*/
196         if (error_code == MM_UTIL_ERROR_NONE)
197                 return IMAGE_UTIL_ERROR_NONE;
198
199         /* Invalid parameter error*/
200         else if (error_code == MM_UTIL_ERROR_INVALID_PARAMETER)
201                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
202
203         else if (error_code == MM_UTIL_ERROR_OUT_OF_MEMORY)
204                 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
205
206         /* No such file  error*/
207         else if (error_code == MM_UTIL_ERROR_NO_SUCH_FILE)
208                 return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
209
210         /* Internal operation error*/
211         else if (error_code == MM_UTIL_ERROR_INVALID_OPERATION)
212                 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
213
214         /* Not supported format error*/
215         else if (error_code == MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT)
216                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
217
218         /*ETC*/
219         return IMAGE_UTIL_ERROR_INVALID_OPERATION;
220
221 }
222
223 bool _image_util_check_resolution(int width, int height)
224 {
225         if (width <= 0) {
226                 image_util_error("invalid width [%d]", width);
227                 return false;
228         }
229
230         if (height <= 0) {
231                 image_util_error("invalid height [%d]", height);
232                 return false;
233         }
234
235         return true;
236 }