/src/cairo/src/cairo-image-info.c
Line | Count | Source |
1 | | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */ |
2 | | /* cairo - a vector graphics library with display and print output |
3 | | * |
4 | | * Copyright © 2008 Adrian Johnson |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it either under the terms of the GNU Lesser General Public |
8 | | * License version 2.1 as published by the Free Software Foundation |
9 | | * (the "LGPL") or, at your option, under the terms of the Mozilla |
10 | | * Public License Version 1.1 (the "MPL"). If you do not alter this |
11 | | * notice, a recipient may use your version of this file under either |
12 | | * the MPL or the LGPL. |
13 | | * |
14 | | * You should have received a copy of the LGPL along with this library |
15 | | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
17 | | * You should have received a copy of the MPL along with this library |
18 | | * in the file COPYING-MPL-1.1 |
19 | | * |
20 | | * The contents of this file are subject to the Mozilla Public License |
21 | | * Version 1.1 (the "License"); you may not use this file except in |
22 | | * compliance with the License. You may obtain a copy of the License at |
23 | | * http://www.mozilla.org/MPL/ |
24 | | * |
25 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
26 | | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
27 | | * the specific language governing rights and limitations. |
28 | | * |
29 | | * The Original Code is the cairo graphics library. |
30 | | * |
31 | | * The Initial Developer of the Original Code is Adrian Johnson. |
32 | | * |
33 | | * Contributor(s): |
34 | | * Adrian Johnson <ajohnson@redneon.com> |
35 | | */ |
36 | | |
37 | | #include "cairoint.h" |
38 | | |
39 | | #include "cairo-error-private.h" |
40 | | #include "cairo-image-info-private.h" |
41 | | |
42 | | /* JPEG (image/jpeg) |
43 | | * |
44 | | * http://www.w3.org/Graphics/JPEG/itu-t81.pdf |
45 | | */ |
46 | | |
47 | | /* Markers with no parameters. All other markers are followed by a two |
48 | | * byte length of the parameters. */ |
49 | 0 | #define TEM 0x01 |
50 | 0 | #define RST_begin 0xd0 |
51 | 0 | #define RST_end 0xd7 |
52 | 0 | #define SOI 0xd8 |
53 | 0 | #define EOI 0xd9 |
54 | | |
55 | | /* Start of frame markers. */ |
56 | 0 | #define SOF0 0xc0 |
57 | 0 | #define SOF1 0xc1 |
58 | 0 | #define SOF2 0xc2 |
59 | 0 | #define SOF3 0xc3 |
60 | 0 | #define SOF5 0xc5 |
61 | 0 | #define SOF6 0xc6 |
62 | 0 | #define SOF7 0xc7 |
63 | 0 | #define SOF9 0xc9 |
64 | 0 | #define SOF10 0xca |
65 | 0 | #define SOF11 0xcb |
66 | 0 | #define SOF13 0xcd |
67 | 0 | #define SOF14 0xce |
68 | 0 | #define SOF15 0xcf |
69 | | |
70 | | static const unsigned char * |
71 | | _jpeg_skip_segment (const unsigned char *p) |
72 | 0 | { |
73 | 0 | int len; |
74 | |
|
75 | 0 | p++; |
76 | 0 | len = (p[0] << 8) | p[1]; |
77 | |
|
78 | 0 | return p + len; |
79 | 0 | } |
80 | | |
81 | | static void |
82 | | _jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p) |
83 | 0 | { |
84 | 0 | info->width = (p[6] << 8) + p[7]; |
85 | 0 | info->height = (p[4] << 8) + p[5]; |
86 | 0 | info->num_components = p[8]; |
87 | 0 | info->bits_per_component = p[3]; |
88 | 0 | } |
89 | | |
90 | | cairo_int_status_t |
91 | | _cairo_image_info_get_jpeg_info (cairo_image_info_t *info, |
92 | | const unsigned char *data, |
93 | | unsigned long length) |
94 | 0 | { |
95 | 0 | const unsigned char *p = data; |
96 | |
|
97 | 0 | while (p + 1 < data + length) { |
98 | 0 | if (*p != 0xff) |
99 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
100 | 0 | p++; |
101 | |
|
102 | 0 | switch (*p) { |
103 | | /* skip fill bytes */ |
104 | 0 | case 0xff: |
105 | 0 | p++; |
106 | 0 | break; |
107 | | |
108 | 0 | case TEM: |
109 | 0 | case SOI: |
110 | 0 | case EOI: |
111 | 0 | p++; |
112 | 0 | break; |
113 | | |
114 | 0 | case SOF0: |
115 | 0 | case SOF1: |
116 | 0 | case SOF2: |
117 | 0 | case SOF3: |
118 | 0 | case SOF5: |
119 | 0 | case SOF6: |
120 | 0 | case SOF7: |
121 | 0 | case SOF9: |
122 | 0 | case SOF10: |
123 | 0 | case SOF11: |
124 | 0 | case SOF13: |
125 | 0 | case SOF14: |
126 | 0 | case SOF15: |
127 | | /* Start of frame found. Extract the image parameters. */ |
128 | 0 | if (p + 8 > data + length) |
129 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
130 | | |
131 | 0 | _jpeg_extract_info (info, p); |
132 | 0 | return CAIRO_STATUS_SUCCESS; |
133 | | |
134 | 0 | default: |
135 | 0 | if (*p >= RST_begin && *p <= RST_end) { |
136 | 0 | p++; |
137 | 0 | break; |
138 | 0 | } |
139 | | |
140 | 0 | if (p + 3 > data + length) |
141 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
142 | | |
143 | 0 | p = _jpeg_skip_segment (p); |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | 0 | return CAIRO_STATUS_SUCCESS; |
149 | 0 | } |
150 | | |
151 | | /* JPEG 2000 (image/jp2) |
152 | | * |
153 | | * http://www.jpeg.org/public/15444-1annexi.pdf |
154 | | */ |
155 | | |
156 | 3 | #define JPX_FILETYPE 0x66747970 |
157 | 3 | #define JPX_JP2_HEADER 0x6A703268 |
158 | 3 | #define JPX_IMAGE_HEADER 0x69686472 |
159 | | |
160 | | static const unsigned char _jpx_signature[] = { |
161 | | 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a |
162 | | }; |
163 | | |
164 | | static const unsigned char * |
165 | | _jpx_next_box (const unsigned char *p, const unsigned char *end) |
166 | 3 | { |
167 | 3 | if (p + 4 < end) { |
168 | 3 | uint32_t length = get_unaligned_be32 (p); |
169 | 3 | if (p + length < end) |
170 | 3 | return p + length; |
171 | 3 | } |
172 | | |
173 | 0 | return end; |
174 | 3 | } |
175 | | |
176 | | static const unsigned char * |
177 | | _jpx_get_box_contents (const unsigned char *p) |
178 | 6 | { |
179 | 6 | return p + 8; |
180 | 6 | } |
181 | | |
182 | | static cairo_bool_t |
183 | | _jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type) |
184 | 9 | { |
185 | 9 | uint32_t length; |
186 | | |
187 | 9 | if (p + 8 < end) { |
188 | 9 | length = get_unaligned_be32 (p); |
189 | 9 | if (get_unaligned_be32 (p + 4) == type && p + length < end) |
190 | 9 | return TRUE; |
191 | 9 | } |
192 | | |
193 | 0 | return FALSE; |
194 | 9 | } |
195 | | |
196 | | static const unsigned char * |
197 | | _jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type) |
198 | 3 | { |
199 | 3 | while (p < end) { |
200 | 3 | if (_jpx_match_box (p, end, type)) |
201 | 3 | return p; |
202 | 0 | p = _jpx_next_box (p, end); |
203 | 0 | } |
204 | | |
205 | 0 | return NULL; |
206 | 3 | } |
207 | | |
208 | | static cairo_int_status_t |
209 | | _jpx_extract_info (const unsigned char *p, cairo_image_info_t *info, const unsigned char *end) |
210 | 3 | { |
211 | 3 | if (p + 11 >= end) { |
212 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
213 | 0 | } |
214 | | |
215 | 3 | info->height = get_unaligned_be32 (p); |
216 | 3 | info->width = get_unaligned_be32 (p + 4); |
217 | 3 | info->num_components = (p[8] << 8) + p[9]; |
218 | 3 | info->bits_per_component = p[10]; |
219 | | |
220 | 3 | return CAIRO_STATUS_SUCCESS; |
221 | 3 | } |
222 | | |
223 | | cairo_int_status_t |
224 | | _cairo_image_info_get_jpx_info (cairo_image_info_t *info, |
225 | | const unsigned char *data, |
226 | | unsigned long length) |
227 | 3 | { |
228 | 3 | const unsigned char *p = data; |
229 | 3 | const unsigned char *end = data + length; |
230 | | |
231 | | /* First 12 bytes must be the JPEG 2000 signature box. */ |
232 | 3 | if (length < ARRAY_LENGTH(_jpx_signature) || |
233 | 3 | memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0) |
234 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
235 | | |
236 | 3 | p += ARRAY_LENGTH(_jpx_signature); |
237 | | |
238 | | /* Next box must be a File Type Box */ |
239 | 3 | if (! _jpx_match_box (p, end, JPX_FILETYPE)) |
240 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
241 | | |
242 | 3 | p = _jpx_next_box (p, end); |
243 | | |
244 | | /* Locate the JP2 header box. */ |
245 | 3 | p = _jpx_find_box (p, end, JPX_JP2_HEADER); |
246 | 3 | if (!p) |
247 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
248 | | |
249 | | /* Step into the JP2 header box. First box must be the Image |
250 | | * Header */ |
251 | 3 | p = _jpx_get_box_contents (p); |
252 | 3 | if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER)) |
253 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
254 | | |
255 | | /* Get the image info */ |
256 | 3 | p = _jpx_get_box_contents (p); |
257 | 3 | return _jpx_extract_info (p, info, end); |
258 | 3 | } |
259 | | |
260 | | /* PNG (image/png) |
261 | | * |
262 | | * http://www.w3.org/TR/2003/REC-PNG-20031110/ |
263 | | */ |
264 | | |
265 | 0 | #define PNG_IHDR 0x49484452 |
266 | | |
267 | | static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; |
268 | | |
269 | | cairo_int_status_t |
270 | | _cairo_image_info_get_png_info (cairo_image_info_t *info, |
271 | | const unsigned char *data, |
272 | | unsigned long length) |
273 | 0 | { |
274 | 0 | const unsigned char *p = data; |
275 | 0 | const unsigned char *end = data + length; |
276 | |
|
277 | 0 | if (length < 8 || memcmp (data, _png_magic, 8) != 0) |
278 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
279 | | |
280 | 0 | p += 8; |
281 | | |
282 | | /* The first chunk must be IDHR. IDHR has 13 bytes of data plus |
283 | | * the 12 bytes of overhead for the chunk. */ |
284 | 0 | if (p + 13 + 12 > end) |
285 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
286 | | |
287 | 0 | p += 4; |
288 | 0 | if (get_unaligned_be32 (p) != PNG_IHDR) |
289 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
290 | | |
291 | 0 | p += 4; |
292 | 0 | info->width = get_unaligned_be32 (p); |
293 | 0 | p += 4; |
294 | 0 | info->height = get_unaligned_be32 (p); |
295 | |
|
296 | 0 | return CAIRO_STATUS_SUCCESS; |
297 | 0 | } |
298 | | |
299 | | static const unsigned char * |
300 | | _jbig2_find_data_end (const unsigned char *p, |
301 | | const unsigned char *end, |
302 | | int type) |
303 | 0 | { |
304 | 0 | unsigned char end_seq[2]; |
305 | 0 | int mmr; |
306 | | |
307 | | /* Segments of type "Immediate generic region" may have an |
308 | | * unspecified data length. The JBIG2 specification specifies the |
309 | | * method to find the end of the data for these segments. */ |
310 | 0 | if (type == 36 || type == 38 || type == 39) { |
311 | 0 | if (p + 18 < end) { |
312 | 0 | mmr = p[17] & 0x01; |
313 | 0 | if (mmr) { |
314 | | /* MMR encoding ends with 0x00, 0x00 */ |
315 | 0 | end_seq[0] = 0x00; |
316 | 0 | end_seq[1] = 0x00; |
317 | 0 | } else { |
318 | | /* Template encoding ends with 0xff, 0xac */ |
319 | 0 | end_seq[0] = 0xff; |
320 | 0 | end_seq[1] = 0xac; |
321 | 0 | } |
322 | 0 | p += 18; |
323 | 0 | while (p < end) { |
324 | 0 | if (p[0] == end_seq[0] && p[1] == end_seq[1]) { |
325 | | /* Skip the 2 terminating bytes and the 4 byte row count that follows. */ |
326 | 0 | p += 6; |
327 | 0 | if (p < end) |
328 | 0 | return p; |
329 | 0 | } |
330 | 0 | p++; |
331 | 0 | } |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | 0 | return NULL; |
336 | 0 | } |
337 | | |
338 | | static const unsigned char * |
339 | | _jbig2_get_next_segment (const unsigned char *p, |
340 | | const unsigned char *end, |
341 | | int *type, |
342 | | const unsigned char **data, |
343 | | unsigned long *data_len) |
344 | 7 | { |
345 | 7 | unsigned long seg_num; |
346 | 7 | cairo_bool_t big_page_size; |
347 | 7 | int num_segs; |
348 | 7 | int ref_seg_bytes; |
349 | 7 | int referred_size; |
350 | | |
351 | 7 | if (p + 6 >= end) |
352 | 0 | return NULL; |
353 | | |
354 | 7 | seg_num = get_unaligned_be32 (p); |
355 | 7 | *type = p[4] & 0x3f; |
356 | 7 | big_page_size = (p[4] & 0x40) != 0; |
357 | 7 | p += 5; |
358 | | |
359 | 7 | num_segs = p[0] >> 5; |
360 | 7 | if (num_segs == 7) { |
361 | 0 | if (p + 4 >= end) |
362 | 0 | return NULL; |
363 | 0 | num_segs = get_unaligned_be32 (p) & 0x1fffffff; |
364 | 0 | ref_seg_bytes = 4 + ((num_segs + 1)/8); |
365 | 7 | } else { |
366 | 7 | ref_seg_bytes = 1; |
367 | 7 | } |
368 | 7 | p += ref_seg_bytes; |
369 | | |
370 | 7 | if (seg_num <= 256) |
371 | 7 | referred_size = 1; |
372 | 0 | else if (seg_num <= 65536) |
373 | 0 | referred_size = 2; |
374 | 0 | else |
375 | 0 | referred_size = 4; |
376 | | |
377 | 7 | p += num_segs * referred_size; |
378 | 7 | p += big_page_size ? 4 : 1; |
379 | 7 | if (p + 4 >= end) |
380 | 0 | return NULL; |
381 | | |
382 | 7 | *data_len = get_unaligned_be32 (p); |
383 | 7 | p += 4; |
384 | 7 | *data = p; |
385 | | |
386 | 7 | if (*data_len == 0xffffffff) { |
387 | | /* if data length is -1 we have to scan through the data to find the end */ |
388 | 0 | p = _jbig2_find_data_end (*data, end, *type); |
389 | 0 | if (!p || p >= end) |
390 | 0 | return NULL; |
391 | | |
392 | 0 | *data_len = p - *data; |
393 | 7 | } else { |
394 | 7 | p += *data_len; |
395 | 7 | } |
396 | | |
397 | 7 | if (p < end) |
398 | 7 | return p; |
399 | 0 | else |
400 | 0 | return NULL; |
401 | 7 | } |
402 | | |
403 | | static void |
404 | | _jbig2_extract_info (cairo_image_info_t *info, const unsigned char *p) |
405 | 7 | { |
406 | 7 | info->width = get_unaligned_be32 (p); |
407 | 7 | info->height = get_unaligned_be32 (p + 4); |
408 | 7 | info->num_components = 1; |
409 | 7 | info->bits_per_component = 1; |
410 | 7 | } |
411 | | |
412 | | cairo_int_status_t |
413 | | _cairo_image_info_get_jbig2_info (cairo_image_info_t *info, |
414 | | const unsigned char *data, |
415 | | unsigned long length) |
416 | 7 | { |
417 | 7 | const unsigned char *p = data; |
418 | 7 | const unsigned char *end = data + length; |
419 | 7 | int seg_type; |
420 | 7 | const unsigned char *seg_data; |
421 | 7 | unsigned long seg_data_len; |
422 | | |
423 | 7 | while (p && p < end) { |
424 | 7 | p = _jbig2_get_next_segment (p, end, &seg_type, &seg_data, &seg_data_len); |
425 | 7 | if (p && seg_type == 48 && seg_data_len > 8) { |
426 | | /* page information segment */ |
427 | 7 | _jbig2_extract_info (info, seg_data); |
428 | 7 | return CAIRO_STATUS_SUCCESS; |
429 | 7 | } |
430 | 7 | } |
431 | | |
432 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
433 | 7 | } |