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