Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-image-info.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2008 Adrian Johnson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Adrian Johnson.
31  *
32  * Contributor(s):
33  *      Adrian Johnson <ajohnson@redneon.com>
34  */
35
36 #include "cairoint.h"
37
38 #include "cairo-error-private.h"
39 #include "cairo-image-info-private.h"
40
41 static uint32_t
42 _get_be32 (const unsigned char *p)
43 {
44     return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
45 }
46
47 /* JPEG (image/jpeg)
48  *
49  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
50  */
51
52 /* Markers with no parameters. All other markers are followed by a two
53  * byte length of the parameters. */
54 #define TEM       0x01
55 #define RST_begin 0xd0
56 #define RST_end   0xd7
57 #define SOI       0xd8
58 #define EOI       0xd9
59
60 /* Start of frame markers. */
61 #define SOF0  0xc0
62 #define SOF1  0xc1
63 #define SOF2  0xc2
64 #define SOF3  0xc3
65 #define SOF5  0xc5
66 #define SOF6  0xc6
67 #define SOF7  0xc7
68 #define SOF9  0xc9
69 #define SOF10 0xca
70 #define SOF11 0xcb
71 #define SOF13 0xcd
72 #define SOF14 0xce
73 #define SOF15 0xcf
74
75 static const unsigned char *
76 _jpeg_skip_segment (const unsigned char *p)
77 {
78     int len;
79
80     p++;
81     len = (p[0] << 8) | p[1];
82
83     return p + len;
84 }
85
86 static void
87 _jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p)
88 {
89     info->width = (p[6] << 8) + p[7];
90     info->height = (p[4] << 8) + p[5];
91     info->num_components = p[8];
92     info->bits_per_component = p[3];
93 }
94
95 cairo_int_status_t
96 _cairo_image_info_get_jpeg_info (cairo_image_info_t     *info,
97                                  const unsigned char    *data,
98                                  long                    length)
99 {
100     const unsigned char *p = data;
101
102     while (p + 1 < data + length) {
103         if (*p != 0xff)
104             return CAIRO_INT_STATUS_UNSUPPORTED;
105         p++;
106
107         switch (*p) {
108             /* skip fill bytes */
109         case 0xff:
110             p++;
111             break;
112
113         case TEM:
114         case SOI:
115         case EOI:
116             p++;
117             break;
118
119         case SOF0:
120         case SOF1:
121         case SOF2:
122         case SOF3:
123         case SOF5:
124         case SOF6:
125         case SOF7:
126         case SOF9:
127         case SOF10:
128         case SOF11:
129         case SOF13:
130         case SOF14:
131         case SOF15:
132             /* Start of frame found. Extract the image parameters. */
133             if (p + 8 > data + length)
134                 return CAIRO_INT_STATUS_UNSUPPORTED;
135
136             _jpeg_extract_info (info, p);
137             return CAIRO_STATUS_SUCCESS;
138
139         default:
140             if (*p >= RST_begin && *p <= RST_end) {
141                 p++;
142                 break;
143             }
144
145             if (p + 2 > data + length)
146                 return CAIRO_INT_STATUS_UNSUPPORTED;
147
148             p = _jpeg_skip_segment (p);
149             break;
150         }
151     }
152
153     return CAIRO_STATUS_SUCCESS;
154 }
155
156 /* JPEG 2000 (image/jp2)
157  *
158  * http://www.jpeg.org/public/15444-1annexi.pdf
159  */
160
161 #define JPX_FILETYPE 0x66747970
162 #define JPX_JP2_HEADER 0x6A703268
163 #define JPX_IMAGE_HEADER 0x69686472
164
165 static const unsigned char _jpx_signature[] = {
166     0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a
167 };
168
169 static const unsigned char *
170 _jpx_next_box (const unsigned char *p)
171 {
172     return p + _get_be32 (p);
173 }
174
175 static const unsigned char *
176 _jpx_get_box_contents (const unsigned char *p)
177 {
178     return p + 8;
179 }
180
181 static cairo_bool_t
182 _jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type)
183 {
184     uint32_t length;
185
186     if (p + 8 < end) {
187         length = _get_be32 (p);
188         if (_get_be32 (p + 4) == type &&  p + length < end)
189             return TRUE;
190     }
191
192     return FALSE;
193 }
194
195 static const unsigned char *
196 _jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type)
197 {
198     while (p < end) {
199         if (_jpx_match_box (p, end, type))
200             return p;
201         p = _jpx_next_box (p);
202     }
203
204     return NULL;
205 }
206
207 static void
208 _jpx_extract_info (const unsigned char *p, cairo_image_info_t *info)
209 {
210     info->height = _get_be32 (p);
211     info->width = _get_be32 (p + 4);
212     info->num_components = (p[8] << 8) + p[9];
213     info->bits_per_component = p[10];
214 }
215
216 cairo_int_status_t
217 _cairo_image_info_get_jpx_info (cairo_image_info_t      *info,
218                                 const unsigned char     *data,
219                                 unsigned long            length)
220 {
221     const unsigned char *p = data;
222     const unsigned char *end = data + length;
223
224     /* First 12 bytes must be the JPEG 2000 signature box. */
225     if (length < ARRAY_LENGTH(_jpx_signature) ||
226         memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0)
227         return CAIRO_INT_STATUS_UNSUPPORTED;
228
229     p += ARRAY_LENGTH(_jpx_signature);
230
231     /* Next box must be a File Type Box */
232     if (! _jpx_match_box (p, end, JPX_FILETYPE))
233         return CAIRO_INT_STATUS_UNSUPPORTED;
234
235     p = _jpx_next_box (p);
236
237     /* Locate the JP2 header box. */
238     p = _jpx_find_box (p, end, JPX_JP2_HEADER);
239     if (!p)
240         return CAIRO_INT_STATUS_UNSUPPORTED;
241
242     /* Step into the JP2 header box. First box must be the Image
243      * Header */
244     p = _jpx_get_box_contents (p);
245     if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER))
246         return CAIRO_INT_STATUS_UNSUPPORTED;
247
248     /* Get the image info */
249     p = _jpx_get_box_contents (p);
250     _jpx_extract_info (p, info);
251
252     return CAIRO_STATUS_SUCCESS;
253 }
254
255 /* PNG (image/png)
256  *
257  * http://www.w3.org/TR/2003/REC-PNG-20031110/
258  */
259
260 #define PNG_IHDR 0x49484452
261
262 static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
263
264 cairo_int_status_t
265 _cairo_image_info_get_png_info (cairo_image_info_t     *info,
266                                const unsigned char     *data,
267                                unsigned long            length)
268 {
269     const unsigned char *p = data;
270     const unsigned char *end = data + length;
271
272     if (length < 8 || memcmp (data, _png_magic, 8) != 0)
273        return CAIRO_INT_STATUS_UNSUPPORTED;
274
275     p += 8;
276
277     /* The first chunk must be IDHR. IDHR has 13 bytes of data plus
278      * the 12 bytes of overhead for the chunk. */
279     if (p + 13 + 12 > end)
280        return CAIRO_INT_STATUS_UNSUPPORTED;
281
282     p += 4;
283     if (_get_be32 (p) != PNG_IHDR)
284        return CAIRO_INT_STATUS_UNSUPPORTED;
285
286     p += 4;
287     info->width = _get_be32 (p);
288     p += 4;
289     info->height = _get_be32 (p);
290
291     return CAIRO_STATUS_SUCCESS;
292 }