1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2008 Adrian Johnson
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.
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
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/
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.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Adrian Johnson.
33 * Adrian Johnson <ajohnson@redneon.com>
38 #include "cairo-error-private.h"
39 #include "cairo-image-info-private.h"
42 _get_be32 (const unsigned char *p)
44 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
49 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
52 /* Markers with no parameters. All other markers are followed by a two
53 * byte length of the parameters. */
55 #define RST_begin 0xd0
60 /* Start of frame markers. */
75 static const unsigned char *
76 _jpeg_skip_segment (const unsigned char *p)
81 len = (p[0] << 8) | p[1];
87 _jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p)
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];
96 _cairo_image_info_get_jpeg_info (cairo_image_info_t *info,
97 const unsigned char *data,
100 const unsigned char *p = data;
102 while (p + 1 < data + length) {
104 return CAIRO_INT_STATUS_UNSUPPORTED;
108 /* skip fill bytes */
132 /* Start of frame found. Extract the image parameters. */
133 if (p + 8 > data + length)
134 return CAIRO_INT_STATUS_UNSUPPORTED;
136 _jpeg_extract_info (info, p);
137 return CAIRO_STATUS_SUCCESS;
140 if (*p >= RST_begin && *p <= RST_end) {
145 if (p + 2 > data + length)
146 return CAIRO_INT_STATUS_UNSUPPORTED;
148 p = _jpeg_skip_segment (p);
153 return CAIRO_STATUS_SUCCESS;
156 /* JPEG 2000 (image/jp2)
158 * http://www.jpeg.org/public/15444-1annexi.pdf
161 #define JPX_FILETYPE 0x66747970
162 #define JPX_JP2_HEADER 0x6A703268
163 #define JPX_IMAGE_HEADER 0x69686472
165 static const unsigned char _jpx_signature[] = {
166 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a
169 static const unsigned char *
170 _jpx_next_box (const unsigned char *p)
172 return p + _get_be32 (p);
175 static const unsigned char *
176 _jpx_get_box_contents (const unsigned char *p)
182 _jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type)
187 length = _get_be32 (p);
188 if (_get_be32 (p + 4) == type && p + length < end)
195 static const unsigned char *
196 _jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type)
199 if (_jpx_match_box (p, end, type))
201 p = _jpx_next_box (p);
208 _jpx_extract_info (const unsigned char *p, cairo_image_info_t *info)
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];
217 _cairo_image_info_get_jpx_info (cairo_image_info_t *info,
218 const unsigned char *data,
219 unsigned long length)
221 const unsigned char *p = data;
222 const unsigned char *end = data + length;
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;
229 p += ARRAY_LENGTH(_jpx_signature);
231 /* Next box must be a File Type Box */
232 if (! _jpx_match_box (p, end, JPX_FILETYPE))
233 return CAIRO_INT_STATUS_UNSUPPORTED;
235 p = _jpx_next_box (p);
237 /* Locate the JP2 header box. */
238 p = _jpx_find_box (p, end, JPX_JP2_HEADER);
240 return CAIRO_INT_STATUS_UNSUPPORTED;
242 /* Step into the JP2 header box. First box must be the Image
244 p = _jpx_get_box_contents (p);
245 if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER))
246 return CAIRO_INT_STATUS_UNSUPPORTED;
248 /* Get the image info */
249 p = _jpx_get_box_contents (p);
250 _jpx_extract_info (p, info);
252 return CAIRO_STATUS_SUCCESS;
257 * http://www.w3.org/TR/2003/REC-PNG-20031110/
260 #define PNG_IHDR 0x49484452
262 static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
265 _cairo_image_info_get_png_info (cairo_image_info_t *info,
266 const unsigned char *data,
267 unsigned long length)
269 const unsigned char *p = data;
270 const unsigned char *end = data + length;
272 if (length < 8 || memcmp (data, _png_magic, 8) != 0)
273 return CAIRO_INT_STATUS_UNSUPPORTED;
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;
283 if (_get_be32 (p) != PNG_IHDR)
284 return CAIRO_INT_STATUS_UNSUPPORTED;
287 info->width = _get_be32 (p);
289 info->height = _get_be32 (p);
291 return CAIRO_STATUS_SUCCESS;