Debian Codegraph @

texk/dvipdfm-x/jpegimage.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 /*
28 * JPEG SUPPORT
29 *
30 * Accroding to Libjpeg document:
31 *
32 * CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK
33 * JPEG files: 0 represents 100% ink coverage, rather than 0% ink as you'd
34 * expect....
35 *
36 * To wrok with this problem, we must detect whether CMYK JPEG file is
37 * created by Photoshop. But there are no reliable way to determine this.
38 *
39 * According to Adobe Technical Note #5516,
40 * "Supporting the DCT Filters in PostScript Level 2", Section 18, p.27.
41 *
42 * DCTDecode ignores and skips any APPE marker segment does not begin with
43 * the `Adobe' 5-character string.
44 *
45 * PDF Reference Manual 4th ed., p.61-62.
46 *
47 * The JPEG filter implementation in Adobe Acrobat products does not
48 * support features of the JPEG standard that are irrelevant to images.
49 * In addition, certain choices have been made regarding reserved marker
50 * codes and other optional features of the standard. For details, see
51 * Adobe Technical Note #5116, Supporting the DCT Filters in PostScript
52 * Level 2.
53 */
54
55 #include "system.h"
56 #include "error.h"
57 #include "mem.h"
58
59 #include "mfileio.h"
60 #include "numbers.h"
61
62 #include "dpxconf.h"
63
64 #include "pdfobj.h"
65
66 #include "jpegimage.h"
67 #include "pdfcolor.h"
68
69 #include "pdfximage.h"
70
71 #define JPEG_DEBUG_STR "JPEG"
72 #define JPEG_DEBUG 3
73
74 #ifdef HAVE_LIBJPEG
75 #include <jpeglib.h>
76 #endif /* HAVE_LIBJPEG */
77
78 /* JPEG Markers */
79 typedef enum {
80 JM_SOF0 = 0xc0,
81 JM_SOF1 = 0xc1,
82 JM_SOF2 = 0xc2,
83 JM_SOF3 = 0xc3,
84 JM_SOF5 = 0xc5,
85 JM_DHT = 0xc4,
86 JM_SOF6 = 0xc6,
87 JM_SOF7 = 0xc7,
88 JM_SOF9 = 0xc9,
89 JM_SOF10 = 0xca,
90 JM_SOF11 = 0xcb,
91 JM_DAC = 0xcc,
92 JM_SOF13 = 0xcd,
93 JM_SOF14 = 0xce,
94 JM_SOF15 = 0xcf,
95
96 JM_RST0 = 0xd0,
97 JM_RST1 = 0xd1,
98 JM_RST2 = 0xd2,
99 JM_RST3 = 0xd3,
100 JM_RST4 = 0xd4,
101 JM_RST5 = 0xd5,
102 JM_RST6 = 0xd6,
103 JM_RST7 = 0xd7,
104
105 JM_SOI = 0xd8,
106 JM_EOI = 0xd9,
107 JM_SOS = 0xda,
108 JM_DQT = 0xdb,
109 JM_DNL = 0xdc,
110 JM_DRI = 0xdd,
111 JM_DHP = 0xde,
112 JM_EXP = 0xdf,
113
114 JM_APP0 = 0xe0,
115 JM_APP1 = 0xe1,
116 JM_APP2 = 0xe2,
117 JM_APP14 = 0xee,
118 JM_APP15 = 0xef,
119
120 JM_COM = 0xfe
121 } JPEG_marker;
122
123 typedef enum {
124 JS_APPn_JFIF,
125 JS_APPn_ADOBE,
126 JS_APPn_ICC,
127 JS_APPn_XMP
128 } JPEG_APPn_sig;
129
130 struct JPEG_APPn_JFIF /* APP0 */
131 {
132 uint16_t version;
133 uint8_t units; /* 0: only aspect ratio
134 * 1: dots per inch
135 * 2: dots per cm
136 */
137 uint16_t Xdensity;
138 uint16_t Ydensity;
139 uint8_t Xthumbnail;
140 uint8_t Ythumbnail;
141 unsigned char *thumbnail; /* Thumbnail data. */
142 };
143
144 struct JPEG_APPn_ICC /* APP2 */
145 {
146 uint8_t seq_id;
147 uint8_t num_chunks;
148 unsigned char *chunk;
149
150 /* Length of ICC profile data in this chunk. */
151 size_t length;
152 };
153
154 struct JPEG_APPn_Adobe /* APP14 */
155 {
156 uint16_t version;
157 uint16_t flag0;
158 uint16_t flag1;
159 uint8_t transform; /* color transform code */
160 };
161
162 struct JPEG_APPn_XMP /* APP1 */
163 {
164 unsigned char *packet; /* XMP packet */
165
166 /* Length of XMP packet data */
167 size_t length;
168 };
169
170 struct JPEG_ext
171 {
172 JPEG_marker marker;
173 JPEG_APPn_sig app_sig;
174 void *app_data;
175 };
176
177 #define MAX_COUNT 1024
178 struct JPEG_info
179 {
180 uint16_t height;
181 uint16_t width;
182
183 uint8_t bits_per_component;
184 uint8_t num_components;
185
186 double xdpi;
187 double ydpi;
188
189 /* Application specific extensions */
190 int flags;
191 int num_appn, max_appn;
192 struct JPEG_ext *appn;
193
194 /* Skip chunks not necessary. */
195 char skipbits[MAX_COUNT / 8 + 1];
196 };
197
198 #define HAVE_APPn_JFIF (1 << 0)
199 #define HAVE_APPn_ADOBE (1 << 1)
200 #define HAVE_APPn_ICC (1 << 2)
201 #define HAVE_APPn_Exif (1 << 3)
202 #define HAVE_APPn_XMP (1 << 4)
203
204 static int JPEG_scan_file (struct JPEG_info *j_info, FILE *fp);
205 static int JPEG_copy_stream (struct JPEG_info *j_info, pdf_obj *stream, FILE *fp);
206
207 static void JPEG_info_init (struct JPEG_info *j_info);
208 static void JPEG_info_clear (struct JPEG_info *j_info);
209 static pdf_obj *JPEG_get_XMP (struct JPEG_info *j_info);
210 static pdf_obj *JPEG_get_iccp (struct JPEG_info *j_info);
211 static void jpeg_get_density (struct JPEG_info *j_info, double *xdensity, double *ydensity);
212
213 int
214 check_for_jpeg (FILE *fp)
215 {
216 unsigned char jpeg_sig[2];
217
218 rewind(fp);
219 if (fread(jpeg_sig, sizeof(unsigned char), 2, fp) != 2)
220 return 0;
221 else if (jpeg_sig[0] != 0xff || jpeg_sig[1] != JM_SOI)
222 return 0;
223
224 return 1;
225 }
226
227 int
228 jpeg_include_image (pdf_ximage *ximage, FILE *fp)
229 {
230 pdf_obj *stream;
231 pdf_obj *stream_dict;
232 pdf_obj *colorspace;
233 int colortype;
234 ximage_info info;
235 struct JPEG_info j_info;
236
237 if (!check_for_jpeg(fp)) {
238 WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR);
239 rewind(fp);
240 return -1;
241 }
242 /* File position is 2 here... */
243
244 pdf_ximage_init_image_info(&info);
245
246 JPEG_info_init(&j_info);
247
248 if (JPEG_scan_file(&j_info, fp) < 0) {
249 WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR);
250 JPEG_info_clear(&j_info);
251 return -1;
252 }
253
254 switch (j_info.num_components) {
255 case 1:
256 colortype = PDF_COLORSPACE_TYPE_GRAY;
257 break;
258 case 3:
259 colortype = PDF_COLORSPACE_TYPE_RGB;
260 break;
261 case 4:
262 colortype = PDF_COLORSPACE_TYPE_CMYK;
263 break;
264 default:
265 WARN("%s: Unknown color space (num components: %d)", JPEG_DEBUG_STR, info.num_components);
266 JPEG_info_clear(&j_info);
267 return -1;
268 }
269
270 /* JPEG image use DCTDecode. */
271 stream = pdf_new_stream (0);
272 stream_dict = pdf_stream_dict(stream);
273 pdf_add_dict(stream_dict, pdf_new_name("Filter"), pdf_new_name("DCTDecode"));
274
275 /* XMP Metadata */
276 if (pdf_check_version(1, 4) >= 0) {
277 if (j_info.flags & HAVE_APPn_XMP) {
278 pdf_obj *XMP_stream;
279
280 XMP_stream = JPEG_get_XMP(&j_info);
281 pdf_add_dict(stream_dict,
282 pdf_new_name("Metadata"), pdf_ref_obj(XMP_stream));
283 pdf_release_obj(XMP_stream);
284 }
285 }
286
287 /* Check embedded ICC Profile */
288 colorspace = NULL;
289 if (j_info.flags & HAVE_APPn_ICC) {
290 pdf_obj *icc_stream, *intent;
291 int cspc_id;
292
293 icc_stream = JPEG_get_iccp(&j_info);
294 if (!icc_stream)
295 colorspace = NULL;
296 else {
297 if (iccp_check_colorspace(colortype,
298 pdf_stream_dataptr(icc_stream), pdf_stream_length (icc_stream)) < 0)
299 colorspace = NULL;
300 else {
301 cspc_id = iccp_load_profile(NULL, /* noname */
302 pdf_stream_dataptr(icc_stream),
303 pdf_stream_length (icc_stream));
304 if (cspc_id < 0)
305 colorspace = NULL;
306 else {
307 colorspace = pdf_get_colorspace_reference(cspc_id);
308 intent = iccp_get_rendering_intent(pdf_stream_dataptr(icc_stream),
309 pdf_stream_length (icc_stream));
310 if (intent)
311 pdf_add_dict(stream_dict, pdf_new_name("Intent"), intent);
312 }
313 }
314 pdf_release_obj(icc_stream);
315 }
316 }
317 /* No ICC or invalid ICC profile. */
318 if (!colorspace) {
319 switch (colortype) {
320 case PDF_COLORSPACE_TYPE_GRAY:
321 colorspace = pdf_new_name("DeviceGray");
322 break;
323 case PDF_COLORSPACE_TYPE_RGB:
324 colorspace = pdf_new_name("DeviceRGB");
325 break;
326 case PDF_COLORSPACE_TYPE_CMYK:
327 colorspace = pdf_new_name("DeviceCMYK");
328 break;
329 }
330 }
331 pdf_add_dict(stream_dict, pdf_new_name("ColorSpace"), colorspace);
332
333 #define IS_ADOBE_CMYK(j) (((j).flags & HAVE_APPn_ADOBE) && (j).num_components == 4)
334 if (IS_ADOBE_CMYK(j_info)) {
335 pdf_obj *decode;
336 int i;
337
338 WARN("Adobe CMYK JPEG: Inverted color assumed.");
339 decode = pdf_new_array();
340 for (i = 0; i < j_info.num_components; i++) {
341 pdf_add_array(decode, pdf_new_number(1.0));
342 pdf_add_array(decode, pdf_new_number(0.0));
343 }
344 pdf_add_dict(stream_dict, pdf_new_name("Decode"), decode);
345 }
346
347 /* Copy file */
348 JPEG_copy_stream(&j_info, stream, fp);
349
350 info.width = j_info.width;
351 info.height = j_info.height;
352 info.bits_per_component = j_info.bits_per_component;
353 info.num_components = j_info.num_components;
354
355 jpeg_get_density(&j_info, &info.xdensity, &info.ydensity);
356
357 pdf_ximage_set_image(ximage, &info, stream);
358 JPEG_info_clear(&j_info);
359
360 return 0;
361 }
362
363 static void
364 jpeg_get_density (struct JPEG_info *j_info, double *xdensity, double *ydensity)
365 {
366 if (dpx_conf.compat_mode == dpx_mode_compat_mode) {
367 *xdensity = *ydensity = 72.0 / 100.0;
368 return;
369 }
370
371 /*
372 * j_info->xdpi and j_info->ydpi are determined in most cases
373 * in JPEG_scan_file(). FIXME: However, in some kinds of JPEG files,
374 * j_info->xdpi, and j_info->ydpi are not determined in
375 * JPEG_scan_file(). In this case we assume
376 * that j_info->xdpi = j_info->ydpi = 72.0.
377 */
378 if (j_info->xdpi < 0.1 && j_info->ydpi < 0.1) {
379 j_info->xdpi = j_info->ydpi = 72.0;
380 }
381 *xdensity = 72.0 / j_info->xdpi;
382 *ydensity = 72.0 / j_info->ydpi;
383
384 return;
385 }
386
387 static void
388 JPEG_info_init (struct JPEG_info *j_info)
389 {
390 j_info->width = 0;
391 j_info->height = 0;
392 j_info->bits_per_component = 0;
393 j_info->num_components = 0;
394
395 j_info->xdpi = 0.0;
396 j_info->ydpi = 0.0;
397
398 j_info->flags = 0;
399 j_info->num_appn = 0;
400 j_info->max_appn = 0;
401 j_info->appn = NULL;
402
403 memset(j_info->skipbits, 0, MAX_COUNT / 8 + 1);
404 }
405
406 static void
407 JPEG_release_APPn_data (JPEG_marker marker, JPEG_APPn_sig app_sig, void *app_data)
408 {
409 if (marker == JM_APP0 &&
410 app_sig == JS_APPn_JFIF) {
411 struct JPEG_APPn_JFIF *data;
412
413 data = (struct JPEG_APPn_JFIF *) app_data;
414 if (data->thumbnail)
415 RELEASE(data->thumbnail);
416 data->thumbnail = NULL;
417
418 RELEASE(data);
419 } else if (marker == JM_APP2 && app_sig == JS_APPn_ICC) {
420 struct JPEG_APPn_ICC *data;
421
422 data = (struct JPEG_APPn_ICC *) app_data;
423 if (data->chunk)
424 RELEASE(data->chunk);
425 data->chunk = NULL;
426
427 RELEASE(data);
428 } else if (marker == JM_APP14 && app_sig == JS_APPn_ADOBE) {
429 struct JPEG_APPn_Adobe *data;
430
431 data = (struct JPEG_APPn_Adobe *) app_data;
432
433 RELEASE(data);
434 } else if (marker == JM_APP1 && app_sig == JS_APPn_XMP) {
435 struct JPEG_APPn_XMP *data;
436
437 data = (struct JPEG_APPn_XMP *) app_data;
438 if (data->packet)
439 RELEASE(data->packet);
440
441 RELEASE(data);
442 }
443 }
444
445 static void
446 JPEG_info_clear (struct JPEG_info *j_info)
447 {
448 if (j_info->num_appn > 0 &&
449 j_info->appn != NULL) {
450 int i;
451
452 for (i = 0; i < j_info->num_appn; i++)
453 JPEG_release_APPn_data(j_info->appn[i].marker,
454 j_info->appn[i].app_sig, j_info->appn[i].app_data);
455 RELEASE(j_info->appn);
456 }
457 j_info->appn = NULL;
458 j_info->num_appn = 0;
459 j_info->max_appn = 0;
460 j_info->flags = 0;
461 }
462
463 static pdf_obj *
464 JPEG_get_iccp (struct JPEG_info *j_info)
465 {
466 pdf_obj *icc_stream;
467 struct JPEG_APPn_ICC *icc;
468 int i, prev_id = 0, num_icc_seg = -1;
469
470 icc_stream = pdf_new_stream(STREAM_COMPRESS);
471 for (i = 0; i < j_info->num_appn; i++) {
472 if (j_info->appn[i].marker != JM_APP2 ||
473 j_info->appn[i].app_sig != JS_APPn_ICC)
474 continue;
475 icc = (struct JPEG_APPn_ICC *) j_info->appn[i].app_data;
476 if (num_icc_seg < 0 && prev_id == 0) {
477 num_icc_seg = icc->num_chunks;
478 /* ICC chunks are sorted? */
479 } else if (icc->seq_id != prev_id + 1 ||
480 num_icc_seg != icc->num_chunks || icc->seq_id > icc->num_chunks) {
481 WARN("Invalid JPEG ICC chunk: %d (p:%d, n:%d)", icc->seq_id, prev_id, icc->num_chunks);
482 pdf_release_obj(icc_stream);
483 icc_stream = NULL;
484 break;
485 }
486 pdf_add_stream(icc_stream, icc->chunk, icc->length);
487 prev_id = icc->seq_id;
488 num_icc_seg = icc->num_chunks;
489 }
490
491 return icc_stream;
492 }
493
494 static pdf_obj *
495 JPEG_get_XMP (struct JPEG_info *j_info)
496 {
497 pdf_obj *XMP_stream, *stream_dict;
498 struct JPEG_APPn_XMP *XMP;
499 int i, count = 0;
500
501 /* I don't know if XMP Metadata should be compressed here.*/
502 XMP_stream = pdf_new_stream(STREAM_COMPRESS);
503 stream_dict = pdf_stream_dict(XMP_stream);
504 pdf_add_dict(stream_dict,
505 pdf_new_name("Type"), pdf_new_name("Metadata"));
506 pdf_add_dict(stream_dict,
507 pdf_new_name("Subtype"), pdf_new_name("XML"));
508 for (i = 0; i < j_info->num_appn; i++) {
509 /* Not sure for the case of multiple segments */
510 if (j_info->appn[i].marker != JM_APP1 ||
511 j_info->appn[i].app_sig != JS_APPn_XMP)
512 continue;
513 XMP = (struct JPEG_APPn_XMP *) j_info->appn[i].app_data;
514 pdf_add_stream(XMP_stream, XMP->packet, XMP->length);
515 count++;
516 }
517 if (count > 1)
518 WARN("%s: Multiple XMP segments found in JPEG file. (untested)", JPEG_DEBUG_STR);
519
520 return XMP_stream;
521 }
522
523 static JPEG_marker
524 JPEG_get_marker (FILE *fp)
525 {
526 int c;
527
528 c = fgetc(fp);
529 if (c != 255)
530 return -1;
531
532 for (;;) {
533 c = fgetc(fp);
534 if (c < 0)
535 return -1;
536 else if (c > 0 && c < 255) {
537 return c;
538 }
539 }
540 }
541
542 static int
543 add_APPn_marker (struct JPEG_info *j_info, JPEG_marker marker, int app_sig, void *app_data)
544 {
545 int n;
546
547 if (j_info->num_appn >= j_info->max_appn) {
548 j_info->max_appn += 16;
549 j_info->appn = RENEW(j_info->appn, j_info->max_appn, struct JPEG_ext);
550 }
551 n = j_info->num_appn;
552
553 j_info->appn[n].marker = marker;
554 j_info->appn[n].app_sig = app_sig;
555 j_info->appn[n].app_data = app_data;
556
557 j_info->num_appn += 1;
558
559 return n;
560 }
561
562 static unsigned short
563 read_APP14_Adobe (struct JPEG_info *j_info, FILE *fp)
564 {
565 struct JPEG_APPn_Adobe *app_data;
566
567 app_data = NEW(1, struct JPEG_APPn_Adobe);
568 app_data->version = get_unsigned_pair(fp);
569 app_data->flag0 = get_unsigned_pair(fp);
570 app_data->flag1 = get_unsigned_pair(fp);
571 app_data->transform = get_unsigned_byte(fp);
572
573 add_APPn_marker(j_info, JM_APP14, JS_APPn_ADOBE, app_data);
574
575 return 7;
576 }
577
578 #define JPEG_EXIF_BIGENDIAN 0
579 #define JPEG_EXIF_LITTLEENDIAN 1
580 static int
581 read_exif_bytes (unsigned char **pp, int n, int endian)
582 {
583 int rval = 0;
584 unsigned char *p = *pp;
585 int i;
586
587 switch (endian) {
588 case JPEG_EXIF_BIGENDIAN:
589 for (i = 0; i < n; i++) {
590 rval = (rval << 8) + p[i];
591 }
592 break;
593 case JPEG_EXIF_LITTLEENDIAN:
594 for (i = n - 1; i >= 0; i--) {
595 rval = (rval << 8) + p[i];
596 }
597 break;
598 }
599
600 *pp += n;
601 return rval;
602 }
603
604 #define JPEG_EXIF_TYPE_BYTE 1
605 #define JPEG_EXIF_TYPE_ASCII 2
606 #define JPEG_EXIF_TYPE_SHORT 3
607 #define JPEG_EXIF_TYPE_LONG 4
608 #define JPEG_EXIF_TYPE_RATIONAL 5
609 #define JPEG_EXIF_TYPE_UNDEFINED 7
610 #define JPEG_EXIF_TYPE_SLONG 9
611 #define JPEG_EXIF_TYPE_SRATIONAL 10
612
613 #define JPEG_EXIF_TAG_XRESOLUTION 282
614 #define JPEG_EXIF_TAG_YRESOLUTION 283
615 #define JPEG_EXIF_TAG_RESOLUTIONUNIT 296
616 #define JPEG_EXIF_TAG_RESUNIT_MS 0x5110
617 #define JPEG_EXIF_TAG_XRES_MS 0x5111
618 #define JPEG_EXIF_TAG_YRES_MS 0x5112
619 static size_t
620 read_APP1_Exif (struct JPEG_info *j_info, FILE *fp, size_t length)
621 {
622 /* this doesn't save the data, just reads the tags we need */
623 /* based on info from http://www.exif.org/Exif2-2.PDF */
624 unsigned char *buffer = NEW(length, unsigned char);
625 unsigned char *p, *endptr;
626 unsigned char *tiff_header;
627 int endian;
628 int num_fields;
629 int value = 0, offset;
630 double xres = 0.0, yres = 0.0;
631 double res_unit = 1.0;
632 unsigned int xres_ms = 0;
633 unsigned int yres_ms = 0;
634 double res_unit_ms = 0.0;
635 double exifxdpi = 0.0, exifydpi = 0.0;
636
637 fread(buffer, length, 1, fp);
638 if (length < 10)
639 goto err;
640
641 p = buffer; endptr = buffer + length;
642 while ((p < endptr) && (*p == 0))
643 p++;
644
645 if (p + 8 >= endptr)
646 goto err;
647 /* TIFF header */
648 tiff_header = p;
649 if ((p[0] == 'M') && (p[1] == 'M'))
650 endian = JPEG_EXIF_BIGENDIAN;
651 else if ((p[0] == 'I') && (p[1] == 'I'))
652 endian = JPEG_EXIF_LITTLEENDIAN;
653 else {
654 WARN("%s: Invalid value in Exif TIFF header.", JPEG_DEBUG_STR);
655 goto err;
656 }
657 p += 2;
658 value = read_exif_bytes(&p, 2, endian);
659 if (value != 42) {
660 WARN("%s: Invalid value in Exif TIFF header.", JPEG_DEBUG_STR);
661 goto err;
662 }
663 /* Offset to 0th IFD */
664 offset = read_exif_bytes(&p, 4, endian);
665
666 p = tiff_header + offset;
667 if (p + 2 >= endptr)
668 goto err;
669 num_fields = read_exif_bytes(&p, 2, endian);
670 while (num_fields-- > 0 && p < endptr) {
671 int tag, type;
672 int count;
673 unsigned int den, num;
674
675 if (p + 12 > endptr) {
676 WARN("%s: Truncated Exif data...", JPEG_DEBUG_STR);
677 goto err;
678 }
679 tag = (int) read_exif_bytes(&p, 2, endian);
680 type = (int) read_exif_bytes(&p, 2, endian);
681 count = read_exif_bytes(&p, 4, endian);
682 /* Exif data is redundant... */
683 switch (tag) {
684 case JPEG_EXIF_TAG_XRESOLUTION:
685 if (type != JPEG_EXIF_TYPE_RATIONAL || count != 1) {
686 WARN("%s: Invalid data for XResolution in Exif chunk.", JPEG_DEBUG_STR);
687 goto err;
688 }
689 offset = read_exif_bytes(&p, 4, endian);
690 if (tiff_header + offset + 8 > buffer + length) {
691 WARN("%s: Invalid offset value in Exif data.", JPEG_DEBUG_STR);
692 goto err;
693 } else {
694 unsigned char *vp = tiff_header + offset;
695 num = (unsigned int) read_exif_bytes(&vp, 4, endian);
696 den = (unsigned int) read_exif_bytes(&vp, 4, endian);
697 }
698 if (den > 0)
699 xres = (double) num / den;
700 break;
701 case JPEG_EXIF_TAG_YRESOLUTION:
702 if (type != JPEG_EXIF_TYPE_RATIONAL || count != 1) {
703 WARN("%s: Invalid data for XResolution in Exif chunk.", JPEG_DEBUG_STR);
704 goto err;
705 }
706 offset = read_exif_bytes(&p, 4, endian);
707 if (tiff_header + offset + 8 > buffer + length) {
708 WARN("%s: Invalid offset value in Exif data.", JPEG_DEBUG_STR);
709 goto err;
710 } else {
711 unsigned char *vp = tiff_header + offset;
712 num = (unsigned int) read_exif_bytes(&vp, 4, endian);
713 den = (unsigned int) read_exif_bytes(&vp, 4, endian);
714 }
715 if (den > 0)
716 yres = (double) num / den;
717 break;
718 case JPEG_EXIF_TAG_RESOLUTIONUNIT:
719 if (type != JPEG_EXIF_TYPE_SHORT || count != 1) {
720 WARN("%s: Invalid data for ResolutionUnit in Exif chunk.", JPEG_DEBUG_STR);
721 goto err;
722 }
723 value = read_exif_bytes(&p, 2, endian);
724 p += 2;
725 if (value == 2)
726 res_unit = 1.0 ; /* inch */
727 else if (value == 3)
728 res_unit = 2.54; /* cm */
729 break;
730 case JPEG_EXIF_TAG_RESUNIT_MS: /* PixelUnit */
731 if (type != JPEG_EXIF_TYPE_BYTE || count != 1) {
732 WARN("%s: Invalid data for ResolutionUnit in Exif chunk.", JPEG_DEBUG_STR);
733 goto err;
734 }
735 value = read_exif_bytes(&p, 1, endian);
736 p += 3;
737 if (value == 1)
738 res_unit_ms = 0.0254; /* Unit is meter */
739 else
740 res_unit_ms = 0.0;
741 break;
742 case JPEG_EXIF_TAG_XRES_MS: /* PixelPerUnitX */
743 if (type != JPEG_EXIF_TYPE_LONG || count != 1) {
744 WARN("%s: Invalid data for PixelPerUnitX in Exif chunk.", JPEG_DEBUG_STR);
745 goto err;
746 }
747 value = read_exif_bytes(&p, 4, endian);
748 xres_ms = value;
749 break;
750 case JPEG_EXIF_TAG_YRES_MS: /* PixelPerUnitY */
751 if (type != JPEG_EXIF_TYPE_LONG || count != 1) {
752 WARN("%s: Invalid data for PixelPerUnitY in Exif chunk.", JPEG_DEBUG_STR);
753 goto err;
754 }
755 value = read_exif_bytes(&p, 4, endian);
756 yres_ms = value;
757 break;
758 default:
759 /* 40901 ColorSpace and 42240 Gamma unsupported... */
760 p += 4;
761 break;
762 }
763 }
764 if (num_fields > 0) {
765 WARN("%s: Truncated Exif data...", JPEG_DEBUG_STR);
766 goto err;
767 }
768
769 /* Calculate Exif resolution, if given.
770 */
771 if (xres > 0.0 && yres > 0.0) {
772 exifxdpi = xres * res_unit;
773 exifydpi = yres * res_unit;
774 } else if (xres_ms > 0 && yres_ms > 0 && res_unit_ms > 0.0) {
775 exifxdpi = xres_ms * res_unit_ms;
776 exifydpi = yres_ms * res_unit_ms;
777 } else {
778 exifxdpi = 72.0 * res_unit;
779 exifydpi = 72.0 * res_unit;
780 }
781
782 /* Do not overwrite j_info->xdpi and j_info->ydpi if they are
783 * already determined in JFIF.
784 */
785 if (j_info->xdpi < 0.1 && j_info->ydpi < 0.1) {
786 j_info->xdpi = exifxdpi;
787 j_info->ydpi = exifydpi;
788 } else {
789 double xxx1 = floor(exifxdpi+0.5);
790 double xxx2 = floor(j_info->xdpi+0.5);
791 double yyy1 = floor(exifydpi+0.5);
792 double yyy2 = floor(j_info->ydpi+0.5);
793
794 if (xxx1 != xxx2 || yyy1 != yyy2) {
795 WARN("%s: Inconsistent resolution may have " \
796 "specified in Exif and JFIF: %gx%g - %gx%g", JPEG_DEBUG_STR,
797 xxx1, yyy1, xxx2, yyy2);
798 }
799 }
800
801 err:
802 RELEASE(buffer);
803 return length;
804 }
805
806 static size_t
807 read_APP0_JFIF (struct JPEG_info *j_info, FILE *fp)
808 {
809 struct JPEG_APPn_JFIF *app_data;
810 size_t thumb_data_len;
811
812 app_data = NEW(1, struct JPEG_APPn_JFIF);
813 app_data->version = get_unsigned_pair(fp);
814 app_data->units = get_unsigned_byte(fp);
815 app_data->Xdensity = get_unsigned_pair(fp);
816 app_data->Ydensity = get_unsigned_pair(fp);
817 app_data->Xthumbnail = get_unsigned_byte(fp);
818 app_data->Ythumbnail = get_unsigned_byte(fp);
819 thumb_data_len = 3 * app_data->Xthumbnail * app_data->Ythumbnail;
820 if (thumb_data_len > 0) {
821 app_data->thumbnail = NEW(thumb_data_len, unsigned char);
822 fread(app_data->thumbnail, 1, thumb_data_len, fp);
823 } else {
824 app_data->thumbnail = NULL;
825 }
826
827 add_APPn_marker(j_info, JM_APP0, JS_APPn_JFIF, app_data);
828
829 switch (app_data->units) {
830 case 1:
831 j_info->xdpi = app_data->Xdensity;
832 j_info->ydpi = app_data->Ydensity;
833 break;
834 case 2: /* density is in pixels per cm */
835 j_info->xdpi = app_data->Xdensity * 2.54;
836 j_info->ydpi = app_data->Ydensity * 2.54;
837 break;
838 default: /* FIXME: not sure what to do with this.... */
839 j_info->xdpi = 72.0;
840 j_info->ydpi = 72.0;
841 break;
842 }
843
844 return (9 + thumb_data_len);
845 }
846
847 static size_t
848 read_APP0_JFXX (FILE *fp, size_t length)
849 {
850 get_unsigned_byte(fp);
851 /* Extension Code:
852 *
853 * 0x10: Thumbnail coded using JPEG
854 * 0x11: Thumbnail stored using 1 byte/pixel
855 * 0x13: Thumbnail stored using 3 bytes/pixel
856 */
857 seek_relative(fp, length - 1); /* Thunbnail image */
858
859 /* Ignore */
860
861 return length;
862 }
863
864 static size_t
865 read_APP1_XMP (struct JPEG_info *j_info, FILE *fp, size_t length)
866 {
867 struct JPEG_APPn_XMP *app_data;
868
869 app_data = NEW(1, struct JPEG_APPn_XMP);
870 app_data->length = length;
871 app_data->packet = NEW(app_data->length, unsigned char);
872 fread(app_data->packet, 1, app_data->length, fp);
873
874 add_APPn_marker(j_info, JM_APP1, JS_APPn_XMP, app_data);
875
876 return length;
877 }
878
879 static size_t
880 read_APP2_ICC (struct JPEG_info *j_info, FILE *fp, size_t length)
881 {
882 struct JPEG_APPn_ICC *app_data;
883
884 app_data = NEW(1, struct JPEG_APPn_ICC);
885 app_data->seq_id = get_unsigned_byte(fp); /* Starting at 1 */
886 app_data->num_chunks = get_unsigned_byte(fp);
887 app_data->length = length - 2;
888 app_data->chunk = NEW(app_data->length, unsigned char);
889 fread(app_data->chunk, 1, app_data->length, fp);
890
891 add_APPn_marker(j_info, JM_APP2, JS_APPn_ICC, app_data);
892
893 return length;
894 }
895
896 static int
897 JPEG_copy_stream (struct JPEG_info *j_info, pdf_obj *stream, FILE *fp)
898 {
899 JPEG_marker marker;
900 int length;
901 int found_SOFn, count;
902
903 #define SKIP_CHUNK(j,c) ((j)->skipbits[(c) / 8] & (1 << (7 - (c) % 8)))
904 #define COPY_CHUNK(f,s,l) while ((l) > 0) { \
905 int nb_read = fread(work_buffer, sizeof(char), MIN((l), WORK_BUFFER_SIZE), (f)); \
906 if (nb_read > 0) \
907 pdf_add_stream((s), work_buffer, nb_read); \
908 (l) -= nb_read; \
909 }
910 rewind(fp);
911 count = 0;
912 found_SOFn = 0;
913 while (!found_SOFn && count < MAX_COUNT &&
914 (marker = JPEG_get_marker(fp)) != (JPEG_marker) - 1) {
915 if ( marker == JM_SOI ||
916 (marker >= JM_RST0 && marker <= JM_RST7)) {
917 work_buffer[0] = (char) 0xff;
918 work_buffer[1] = (char) marker;
919 pdf_add_stream(stream, work_buffer, 2);
920 } else {
921 length = get_unsigned_pair(fp) - 2;
922 switch (marker) {
923 case JM_SOF0: case JM_SOF1: case JM_SOF2: case JM_SOF3:
924 case JM_SOF5: case JM_SOF6: case JM_SOF7: case JM_SOF9:
925 case JM_SOF10: case JM_SOF11: case JM_SOF13: case JM_SOF14:
926 case JM_SOF15:
927 work_buffer[0] = (char) 0xff;
928 work_buffer[1] = (char) marker;
929 work_buffer[2] = ((length + 2) >> 8) & 0xff;
930 work_buffer[3] = (length + 2) & 0xff;
931 pdf_add_stream(stream, work_buffer, 4);
932 COPY_CHUNK(fp, stream, length);
933 found_SOFn = 1;
934 break;
935 default:
936 if (SKIP_CHUNK(j_info, count)) {
937 seek_relative(fp, length);
938 } else {
939 work_buffer[0] = (char) 0xff;
940 work_buffer[1] = (char) marker;
941 work_buffer[2] = ((length + 2) >> 8) & 0xff;
942 work_buffer[3] = (length + 2) & 0xff;
943 pdf_add_stream(stream, work_buffer, 4);
944 COPY_CHUNK(fp, stream, length);
945 }
946 }
947 }
948 count++;
949 }
950 while ((length = fread(work_buffer,
951 sizeof(char), WORK_BUFFER_SIZE, fp)) > 0) {
952 pdf_add_stream(stream, work_buffer, length);
953 }
954
955 return (found_SOFn ? 0 : -1);
956 }
957
958 /* Adobe Technical Note #5116 "Supporting the DCT Filters in PostScript Level 2"
959 * 6. "DCTDecode Filter Summary"
960 * ... APPn (application-specific) markers are skipped over harmlessly except
961 * for the Adobe reserved marker described later.
962 *
963 */
964 #define SET_SKIP(j,c) if ((c) < MAX_COUNT) { \
965 (j)->skipbits[(c) / 8] |= (1 << (7 - ((c) % 8))); \
966 }
967 static int
968 JPEG_scan_file (struct JPEG_info *j_info, FILE *fp)
969 {
970 JPEG_marker marker;
971 int found_SOFn, count;
972 char app_sig[128];
973
974 rewind(fp);
975 count = 0;
976 found_SOFn = 0;
977 while (!found_SOFn &&
978 (marker = JPEG_get_marker(fp)) != (JPEG_marker) -1) {
979 if ( marker != JM_SOI &&
980 (marker < JM_RST0 || marker > JM_RST7)) {
981 int length = get_unsigned_pair(fp) - 2;
982 switch (marker) {
983 case JM_SOF0: case JM_SOF1: case JM_SOF2: case JM_SOF3:
984 case JM_SOF5: case JM_SOF6: case JM_SOF7: case JM_SOF9:
985 case JM_SOF10: case JM_SOF11: case JM_SOF13: case JM_SOF14:
986 case JM_SOF15:
987 j_info->bits_per_component = get_unsigned_byte(fp);
988 j_info->height = get_unsigned_pair(fp);
989 j_info->width = get_unsigned_pair(fp);
990 j_info->num_components = get_unsigned_byte(fp);
991 found_SOFn = 1;
992 break;
993 case JM_APP0:
994 if (length > 5) {
995 if (fread(app_sig, sizeof(char), 5, fp) != 5)
996 return -1;
997 length -= 5;
998 if (!memcmp(app_sig, "JFIF\000", 5)) {
999 /* APP0 JFIF marker preserved */
1000 j_info->flags |= HAVE_APPn_JFIF;
1001 length -= read_APP0_JFIF(j_info, fp);
1002 } else if (!memcmp(app_sig, "JFXX", 5)) {
1003 length -= read_APP0_JFXX(fp, length);
1004 SET_SKIP(j_info, count);
1005 } else {
1006 SET_SKIP(j_info, count);
1007 }
1008 } else {
1009 SET_SKIP(j_info, count);
1010 }
1011 seek_relative(fp, length);
1012 break;
1013 case JM_APP1:
1014 if (length > 5) {
1015 if (fread(app_sig, sizeof(char), 5, fp) != 5)
1016 return -1;
1017 length -= 5;
1018 if (!memcmp(app_sig, "Exif\000", 5)) {
1019 /* APP1 Exif marker preserved */
1020 j_info->flags |= HAVE_APPn_Exif;
1021 length -= read_APP1_Exif(j_info, fp, length);
1022 } else if (!memcmp(app_sig, "http:", 5) && length > 24) {
1023 if (fread(app_sig, sizeof(char), 24, fp) != 24)
1024 return -1;
1025 length -= 24;
1026 if (!memcmp(app_sig, "//ns.adobe.com/xap/1.0/\000", 24)) {
1027 j_info->flags |= HAVE_APPn_XMP;
1028 length -= read_APP1_XMP(j_info, fp, length);
1029 }
1030 SET_SKIP(j_info, count);
1031 }
1032 } else {
1033 SET_SKIP(j_info, count);
1034 }
1035 seek_relative(fp, length);
1036 break;
1037 case JM_APP2:
1038 if (length >= 14) {
1039 if (fread(app_sig, sizeof(char), 12, fp) != 12)
1040 return -1;
1041 length -= 12;
1042 if (!memcmp(app_sig, "ICC_PROFILE\000", 12)) {
1043 j_info->flags |= HAVE_APPn_ICC;
1044 length -= read_APP2_ICC(j_info, fp, length);
1045 }
1046 }
1047 seek_relative(fp, length);
1048 SET_SKIP(j_info, count);
1049 break;
1050 case JM_APP14:
1051 if (length > 5) {
1052 if (fread(app_sig, sizeof(char), 5, fp) != 5)
1053 return -1;
1054 length -= 5;
1055 if (!memcmp(app_sig, "Adobe", 5)) {
1056 /* APP14 Adobe marker preserved */
1057 j_info->flags |= HAVE_APPn_ADOBE;
1058 length -= read_APP14_Adobe(j_info, fp);
1059 } else {
1060 SET_SKIP(j_info, count);
1061 }
1062 } else {
1063 SET_SKIP(j_info, count);
1064 }
1065 seek_relative(fp, length);
1066 break;
1067 default:
1068 seek_relative(fp, length);
1069 if (marker >= JM_APP0 && marker <= JM_APP15) {
1070 SET_SKIP(j_info, count);
1071 }
1072 break;
1073 }
1074 }
1075 count++;
1076 }
1077
1078 return (found_SOFn ? 0 : -1);
1079 }
1080
1081 int
1082 jpeg_get_bbox (FILE *fp, int *width, int *height, double *xdensity, double *ydensity)
1083 {
1084 struct JPEG_info j_info;
1085
1086 JPEG_info_init(&j_info);
1087
1088 if (JPEG_scan_file(&j_info, fp) < 0) {
1089 WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR);
1090 JPEG_info_clear(&j_info);
1091 return -1;
1092 }
1093
1094 *width = j_info.width;
1095 *height = j_info.height;
1096
1097 jpeg_get_density(&j_info, xdensity, ydensity);
1098
1099 JPEG_info_clear(&j_info);
1100
1101 return 0;
1102 }