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
#define TEM       0x01
50
#define RST_begin 0xd0
51
#define RST_end   0xd7
52
#define SOI       0xd8
53
#define EOI       0xd9
54

            
55
/* Start of frame markers. */
56
#define SOF0  0xc0
57
#define SOF1  0xc1
58
#define SOF2  0xc2
59
#define SOF3  0xc3
60
#define SOF5  0xc5
61
#define SOF6  0xc6
62
#define SOF7  0xc7
63
#define SOF9  0xc9
64
#define SOF10 0xca
65
#define SOF11 0xcb
66
#define SOF13 0xcd
67
#define SOF14 0xce
68
#define SOF15 0xcf
69

            
70
static const unsigned char *
71
_jpeg_skip_segment (const unsigned char *p)
72
{
73
    int len;
74

            
75
    p++;
76
    len = (p[0] << 8) | p[1];
77

            
78
    return p + len;
79
}
80

            
81
static void
82
_jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p)
83
{
84
    info->width = (p[6] << 8) + p[7];
85
    info->height = (p[4] << 8) + p[5];
86
    info->num_components = p[8];
87
    info->bits_per_component = p[3];
88
}
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
{
95
    const unsigned char *p = data;
96

            
97
    while (p + 1 < data + length) {
98
	if (*p != 0xff)
99
	    return CAIRO_INT_STATUS_UNSUPPORTED;
100
	p++;
101

            
102
	switch (*p) {
103
	    /* skip fill bytes */
104
	case 0xff:
105
	    p++;
106
	    break;
107

            
108
	case TEM:
109
	case SOI:
110
	case EOI:
111
	    p++;
112
	    break;
113

            
114
	case SOF0:
115
	case SOF1:
116
	case SOF2:
117
	case SOF3:
118
	case SOF5:
119
	case SOF6:
120
	case SOF7:
121
	case SOF9:
122
	case SOF10:
123
	case SOF11:
124
	case SOF13:
125
	case SOF14:
126
	case SOF15:
127
	    /* Start of frame found. Extract the image parameters. */
128
	    if (p + 8 > data + length)
129
		return CAIRO_INT_STATUS_UNSUPPORTED;
130

            
131
	    _jpeg_extract_info (info, p);
132
	    return CAIRO_STATUS_SUCCESS;
133

            
134
	default:
135
	    if (*p >= RST_begin && *p <= RST_end) {
136
		p++;
137
		break;
138
	    }
139

            
140
	    if (p + 3 > data + length)
141
		return CAIRO_INT_STATUS_UNSUPPORTED;
142

            
143
	    p = _jpeg_skip_segment (p);
144
	    break;
145
	}
146
    }
147

            
148
    return CAIRO_STATUS_SUCCESS;
149
}
150

            
151
/* JPEG 2000 (image/jp2)
152
 *
153
 * http://www.jpeg.org/public/15444-1annexi.pdf
154
 */
155

            
156
#define JPX_FILETYPE 0x66747970
157
#define JPX_JP2_HEADER 0x6A703268
158
#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
{
167
    if (p + 4 < end) {
168
	uint32_t length = get_unaligned_be32 (p);
169
	if (p + length < end)
170
	    return p + length;
171
    }
172

            
173
    return end;
174
}
175

            
176
static const unsigned char *
177
_jpx_get_box_contents (const unsigned char *p)
178
{
179
    return p + 8;
180
}
181

            
182
static cairo_bool_t
183
_jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type)
184
{
185
    uint32_t length;
186

            
187
    if (p + 8 < end) {
188
	length = get_unaligned_be32 (p);
189
	if (get_unaligned_be32 (p + 4) == type &&  p + length < end)
190
	    return TRUE;
191
    }
192

            
193
    return FALSE;
194
}
195

            
196
static const unsigned char *
197
_jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type)
198
{
199
    while (p < end) {
200
	if (_jpx_match_box (p, end, type))
201
	    return p;
202
	p = _jpx_next_box (p, end);
203
    }
204

            
205
    return NULL;
206
}
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
{
211
    if (p + 11 >= end) {
212
	return CAIRO_INT_STATUS_UNSUPPORTED;
213
    }
214

            
215
    info->height = get_unaligned_be32 (p);
216
    info->width = get_unaligned_be32 (p + 4);
217
    info->num_components = (p[8] << 8) + p[9];
218
    info->bits_per_component = p[10];
219

            
220
    return CAIRO_STATUS_SUCCESS;
221
}
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
{
228
    const unsigned char *p = data;
229
    const unsigned char *end = data + length;
230

            
231
    /* First 12 bytes must be the JPEG 2000 signature box. */
232
    if (length < ARRAY_LENGTH(_jpx_signature) ||
233
	memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0)
234
	return CAIRO_INT_STATUS_UNSUPPORTED;
235

            
236
    p += ARRAY_LENGTH(_jpx_signature);
237

            
238
    /* Next box must be a File Type Box */
239
    if (! _jpx_match_box (p, end, JPX_FILETYPE))
240
	return CAIRO_INT_STATUS_UNSUPPORTED;
241

            
242
    p = _jpx_next_box (p, end);
243

            
244
    /* Locate the JP2 header box. */
245
    p = _jpx_find_box (p, end, JPX_JP2_HEADER);
246
    if (!p)
247
	return CAIRO_INT_STATUS_UNSUPPORTED;
248

            
249
    /* Step into the JP2 header box. First box must be the Image
250
     * Header */
251
    p = _jpx_get_box_contents (p);
252
    if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER))
253
	return CAIRO_INT_STATUS_UNSUPPORTED;
254

            
255
    /* Get the image info */
256
    p = _jpx_get_box_contents (p);
257
    return _jpx_extract_info (p, info, end);
258
}
259

            
260
/* PNG (image/png)
261
 *
262
 * http://www.w3.org/TR/2003/REC-PNG-20031110/
263
 */
264

            
265
#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
{
274
    const unsigned char *p = data;
275
    const unsigned char *end = data + length;
276

            
277
    if (length < 8 || memcmp (data, _png_magic, 8) != 0)
278
       return CAIRO_INT_STATUS_UNSUPPORTED;
279

            
280
    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
    if (p + 13 + 12 > end)
285
       return CAIRO_INT_STATUS_UNSUPPORTED;
286

            
287
    p += 4;
288
    if (get_unaligned_be32 (p) != PNG_IHDR)
289
       return CAIRO_INT_STATUS_UNSUPPORTED;
290

            
291
    p += 4;
292
    info->width = get_unaligned_be32 (p);
293
    p += 4;
294
    info->height = get_unaligned_be32 (p);
295

            
296
    return CAIRO_STATUS_SUCCESS;
297
}
298

            
299
static const unsigned char *
300
_jbig2_find_data_end (const unsigned char *p,
301
		      const unsigned char *end,
302
		      int                  type)
303
{
304
    unsigned char end_seq[2];
305
    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
    if (type == 36 || type == 38 || type == 39) {
311
	if (p + 18 < end) {
312
	    mmr = p[17] & 0x01;
313
	    if (mmr) {
314
		/* MMR encoding ends with 0x00, 0x00 */
315
		end_seq[0] = 0x00;
316
		end_seq[1] = 0x00;
317
	    } else {
318
		/* Template encoding ends with 0xff, 0xac */
319
		end_seq[0] = 0xff;
320
		end_seq[1] = 0xac;
321
	    }
322
	    p += 18;
323
	    while (p < end) {
324
		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
		    p += 6;
327
		    if (p < end)
328
			return p;
329
		}
330
		p++;
331
	    }
332
	}
333
    }
334

            
335
    return NULL;
336
}
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
{
345
    unsigned long seg_num;
346
    cairo_bool_t big_page_size;
347
    int num_segs;
348
    int ref_seg_bytes;
349
    int referred_size;
350

            
351
    if (p + 6 >= end)
352
	return NULL;
353

            
354
    seg_num = get_unaligned_be32 (p);
355
    *type = p[4] & 0x3f;
356
    big_page_size = (p[4] & 0x40) != 0;
357
    p += 5;
358

            
359
    num_segs = p[0] >> 5;
360
    if (num_segs == 7) {
361
	if (p + 4 >= end)
362
	    return NULL;
363
	num_segs = get_unaligned_be32 (p) & 0x1fffffff;
364
	ref_seg_bytes = 4 + ((num_segs + 1)/8);
365
    } else {
366
	ref_seg_bytes = 1;
367
    }
368
    p += ref_seg_bytes;
369

            
370
    if (seg_num <= 256)
371
	referred_size = 1;
372
    else if (seg_num <= 65536)
373
	referred_size = 2;
374
    else
375
	referred_size = 4;
376

            
377
    p += num_segs * referred_size;
378
    p += big_page_size ? 4 : 1;
379
    if (p + 4 >= end)
380
	return NULL;
381

            
382
    *data_len = get_unaligned_be32 (p);
383
    p += 4;
384
    *data = p;
385

            
386
    if (*data_len == 0xffffffff) {
387
	/* if data length is -1 we have to scan through the data to find the end */
388
	p = _jbig2_find_data_end (*data, end, *type);
389
	if (!p || p >= end)
390
	    return NULL;
391

            
392
	*data_len = p - *data;
393
    } else {
394
	p += *data_len;
395
    }
396

            
397
    if (p < end)
398
	return p;
399
    else
400
	return NULL;
401
}
402

            
403
static void
404
_jbig2_extract_info (cairo_image_info_t *info, const unsigned char *p)
405
{
406
    info->width = get_unaligned_be32 (p);
407
    info->height = get_unaligned_be32 (p + 4);
408
    info->num_components = 1;
409
    info->bits_per_component = 1;
410
}
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
{
417
    const unsigned char *p = data;
418
    const unsigned char *end = data + length;
419
    int seg_type;
420
    const unsigned char *seg_data;
421
    unsigned long seg_data_len;
422

            
423
    while (p && p < end) {
424
	p = _jbig2_get_next_segment (p, end, &seg_type, &seg_data, &seg_data_len);
425
	if (p && seg_type == 48 && seg_data_len > 8) {
426
	    /* page information segment */
427
	    _jbig2_extract_info (info, seg_data);
428
	    return CAIRO_STATUS_SUCCESS;
429
	}
430
    }
431

            
432
    return CAIRO_INT_STATUS_UNSUPPORTED;
433
}