1 /*
2 Copyright 1996-2014 Han The Thanh, <thanh@pdftex.org>
3 
4 This file is part of pdfTeX.
5 
6 pdfTeX is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 pdfTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "ptexlib.h"
21 #include "image.h"
22 
23 #define JPG_GRAY  1             /* Gray color space, use /DeviceGray  */
24 #define JPG_RGB   3             /* RGB color space, use /DeviceRGB    */
25 #define JPG_CMYK  4             /* CMYK color space, use /DeviceCMYK  */
26 
27 typedef enum {                  /* JPEG marker codes                    */
28     M_SOF0 = 0xc0,              /* baseline DCT                         */
29     M_SOF1 = 0xc1,              /* extended sequential DCT              */
30     M_SOF2 = 0xc2,              /* progressive DCT                      */
31     M_SOF3 = 0xc3,              /* lossless (sequential)                */
32 
33     M_SOF5 = 0xc5,              /* differential sequential DCT          */
34     M_SOF6 = 0xc6,              /* differential progressive DCT         */
35     M_SOF7 = 0xc7,              /* differential lossless                */
36 
37     M_JPG = 0xc8,               /* JPEG extensions                      */
38     M_SOF9 = 0xc9,              /* extended sequential DCT              */
39     M_SOF10 = 0xca,             /* progressive DCT                      */
40     M_SOF11 = 0xcb,             /* lossless (sequential)                */
41 
42     M_SOF13 = 0xcd,             /* differential sequential DCT          */
43     M_SOF14 = 0xce,             /* differential progressive DCT         */
44     M_SOF15 = 0xcf,             /* differential lossless                */
45 
46     M_DHT = 0xc4,               /* define Huffman tables                */
47 
48     M_DAC = 0xcc,               /* define arithmetic conditioning table */
49 
50     M_RST0 = 0xd0,              /* restart                              */
51     M_RST1 = 0xd1,              /* restart                              */
52     M_RST2 = 0xd2,              /* restart                              */
53     M_RST3 = 0xd3,              /* restart                              */
54     M_RST4 = 0xd4,              /* restart                              */
55     M_RST5 = 0xd5,              /* restart                              */
56     M_RST6 = 0xd6,              /* restart                              */
57     M_RST7 = 0xd7,              /* restart                              */
58 
59     M_SOI = 0xd8,               /* start of image                       */
60     M_EOI = 0xd9,               /* end of image                         */
61     M_SOS = 0xda,               /* start of scan                        */
62     M_DQT = 0xdb,               /* define quantization tables           */
63     M_DNL = 0xdc,               /* define number of lines               */
64     M_DRI = 0xdd,               /* define restart interval              */
65     M_DHP = 0xde,               /* define hierarchical progression      */
66     M_EXP = 0xdf,               /* expand reference image(s)            */
67 
68     M_APP0 = 0xe0,              /* application marker, used for JFIF    */
69     M_APP1 = 0xe1,              /* application marker                   */
70     M_APP2 = 0xe2,              /* application marker                   */
71     M_APP3 = 0xe3,              /* application marker                   */
72     M_APP4 = 0xe4,              /* application marker                   */
73     M_APP5 = 0xe5,              /* application marker                   */
74     M_APP6 = 0xe6,              /* application marker                   */
75     M_APP7 = 0xe7,              /* application marker                   */
76     M_APP8 = 0xe8,              /* application marker                   */
77     M_APP9 = 0xe9,              /* application marker                   */
78     M_APP10 = 0xea,             /* application marker                   */
79     M_APP11 = 0xeb,             /* application marker                   */
80     M_APP12 = 0xec,             /* application marker                   */
81     M_APP13 = 0xed,             /* application marker                   */
82     M_APP14 = 0xee,             /* application marker, used by Adobe    */
83     M_APP15 = 0xef,             /* application marker                   */
84 
85     M_JPG0 = 0xf0,              /* reserved for JPEG extensions         */
86     M_JPG13 = 0xfd,             /* reserved for JPEG extensions         */
87     M_COM = 0xfe,               /* comment                              */
88 
89     M_TEM = 0x01,               /* temporary use                        */
90 
91     M_ERROR = 0x100             /* dummy marker, internal use only      */
92 } JPEG_MARKER;
93 
read2bytes(FILE * f)94 static JPG_UINT16 read2bytes(FILE * f)
95 {
96     int c = xgetc(f);
97     return (c << 8) + xgetc(f);
98 }
99 
100 static unsigned char
get_unsigned_byte(FILE * file)101 get_unsigned_byte (FILE *file)
102 {
103     int ch;
104     ch = fgetc (file);
105     return (unsigned char) ch;
106 }
107 
108 static unsigned short
get_unsigned_pair(FILE * file)109 get_unsigned_pair (FILE *file)
110 {
111     unsigned short pair = get_unsigned_byte(file);
112     pair = (pair << 8) | get_unsigned_byte(file);
113     return pair;
114 }
115 
116 static unsigned int
read_exif_bytes(unsigned char ** p,int n,int b)117 read_exif_bytes(unsigned char **p, int n, int b)
118 {
119     unsigned int rval = 0;
120     unsigned char *pp = *p;
121     if (b) {
122         switch (n) {
123         case 4:
124             rval += *pp++; rval <<= 8;
125             rval += *pp++; rval <<= 8;
126         case 2:
127             rval += *pp++; rval <<= 8;
128             rval += *pp;
129             break;
130         }
131     } else {
132         pp += n;
133         switch (n) {
134         case 4:
135             rval += *--pp; rval <<= 8;
136             rval += *--pp; rval <<= 8;
137         case 2:
138             rval += *--pp; rval <<= 8;
139             rval += *--pp;
140             break;
141         }
142     }
143     *p += n;
144     return rval;
145 }
146 
147 #define NEW(n, t)  (t*)xmalloc(n * sizeof(t))
148 
149 static void
read_APP1_Exif(FILE * fp,unsigned short length,int * xx,int * yy)150 read_APP1_Exif (FILE *fp, unsigned short length, int *xx, int *yy)
151 {
152     /* this doesn't save the data, just reads the tags we need */
153     /* based on info from http://www.exif.org/Exif2-2.PDF */
154     unsigned char *buffer = NEW(length, unsigned char);
155     unsigned char *p, *rp;
156     unsigned char *tiff_header;
157     char bigendian;
158     int i;
159     int num_fields, tag, type;
160     int value = 0, num = 0, den = 0;	/* silence uninitialized warnings */
161     double xres = 72.0;
162     double yres = 72.0;
163     double res_unit = 1.0;
164     fread(buffer, length, 1, fp);
165     p = buffer;
166     while ((p < buffer + length) && (*p == 0))
167         ++p;
168     tiff_header = p;
169     if ((*p == 'M') && (*(p+1) == 'M'))
170         bigendian = 1;
171     else if ((*p == 'I') && (*(p+1) == 'I'))
172         bigendian = 0;
173     else
174         goto err;
175     p += 2;
176     i = read_exif_bytes(&p, 2, bigendian);
177     if (i != 42)
178         goto err;
179     i = read_exif_bytes(&p, 4, bigendian);
180     p = tiff_header + i;
181     num_fields = read_exif_bytes(&p, 2, bigendian);
182     while (num_fields-- > 0) {
183         tag = read_exif_bytes(&p, 2, bigendian);
184         type = read_exif_bytes(&p, 2, bigendian);
185         read_exif_bytes(&p, 4, bigendian);
186         switch (type) {
187         case 1: /* byte */
188             value = *p++;
189             p += 3;
190             break;
191         case 3: /* short */
192             value = read_exif_bytes(&p, 2, bigendian);
193             p += 2;
194             break;
195         case 4: /* long */
196         case 9: /* slong */
197             value = read_exif_bytes(&p, 4, bigendian);
198             break;
199         case 5: /* rational */
200         case 10: /* srational */
201             value = read_exif_bytes(&p, 4, bigendian);
202             rp = tiff_header + value;
203             num = read_exif_bytes(&rp, 4, bigendian);
204             den = read_exif_bytes(&rp, 4, bigendian);
205             break;
206         case 7: /* undefined */
207             value = *p++;
208             p += 3;
209             break;
210         case 2: /* ascii */
211         default:
212             p += 4;
213             break;
214 	}
215         switch (tag) {
216         case 282: /* x res */
217             if (den != 0)
218                 xres = num / den;
219             break;
220         case 283: /* y res */
221             if (den != 0)
222                 yres = num / den;
223             break;
224         case 296: /* res unit */
225             switch (value) {
226             case 2:
227                 res_unit = 1.0;
228                 break;
229             case 3:
230                 res_unit = 2.54;
231                 break;
232             }
233         }
234     }
235 
236     *xx = (int)(xres * res_unit);
237     *yy = (int)(yres * res_unit);
238 
239 err:
240     free(buffer);
241     return;
242 }
243 
read_jpg_info(integer img)244 void read_jpg_info(integer img)
245 {
246     int i, units = 0;
247     unsigned short appmk, length;
248     unsigned char jpg_id[] = "JFIF";
249     img_xres(img) = img_yres(img) = 0;
250     jpg_ptr(img)->file = xfopen(img_name(img), FOPEN_RBIN_MODE);
251     /* no LFS needed, as JPEG is limited to <4GiB */
252     xfseek(jpg_ptr(img)->file, 0, SEEK_END, cur_file_name);
253     jpg_ptr(img)->length = xftell(jpg_ptr(img)->file, cur_file_name);
254     xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
255     if (read2bytes(jpg_ptr(img)->file) != 0xFFD8)
256         pdftex_fail("reading JPEG image failed (no JPEG header found)");
257     /* currently JFIF and Exif files allow extracting img_xres and img_yres */
258     appmk = read2bytes(jpg_ptr(img)->file);
259     if (appmk == 0xFFE0) {     /* check for JFIF */
260         (void) read2bytes(jpg_ptr(img)->file);
261         for (i = 0; i < 5; i++) {
262             if (xgetc(jpg_ptr(img)->file) != jpg_id[i])
263                 break;
264         }
265         if (i == 5) {           /* it's JFIF */
266             read2bytes(jpg_ptr(img)->file);
267             units = xgetc(jpg_ptr(img)->file);
268             img_xres(img) = read2bytes(jpg_ptr(img)->file);
269             img_yres(img) = read2bytes(jpg_ptr(img)->file);
270             switch (units) {
271             case 1:
272                 break;          /* pixels per inch */
273             case 2:
274                 img_xres(img) *= 2.54;
275                 img_yres(img) *= 2.54;
276                 break;          /* pixels per cm */
277             default:
278                 img_xres(img) = img_yres(img) = 0;
279                 break;
280             }
281         }
282         /* if either xres or yres is 0 but the other isn't,
283            set it to the value of the other */
284         if ((img_xres(img) == 0) && (img_yres(img) != 0)) {
285             img_xres(img) = img_yres(img);
286         }
287         if ((img_yres(img) == 0) && (img_xres(img) != 0)) {
288             img_yres(img) = img_xres(img);
289         }
290     } else if (appmk == 0xFFE1) {  /* check for Exif */
291         FILE *fp = jpg_ptr(img)->file;
292         int xxres = 0;
293         int yyres = 0;
294         char app_sig[32];
295         length = get_unsigned_pair(fp) - 2;
296         if (length > 5) {
297             if (fread(app_sig, sizeof(char), 5, fp) != 5)
298                 return;
299             length -= 5;
300             if (!memcmp(app_sig, "Exif\000", 5)) {
301                 read_APP1_Exif(fp, length, &xxres, &yyres);
302             }
303         }
304         img_xres(img) = xxres;
305         img_yres(img) = yyres;
306     }
307     xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
308     while (1) {
309         if (feof(jpg_ptr(img)->file))
310             pdftex_fail("reading JPEG image failed (premature file end)");
311         if (fgetc(jpg_ptr(img)->file) != 0xFF)
312             pdftex_fail("reading JPEG image failed (no marker found)");
313         switch (xgetc(jpg_ptr(img)->file)) {
314         case M_SOF5:
315         case M_SOF6:
316         case M_SOF7:
317         case M_SOF9:
318         case M_SOF10:
319         case M_SOF11:
320         case M_SOF13:
321         case M_SOF14:
322         case M_SOF15:
323             pdftex_fail("unsupported type of compression");
324         case M_SOF2:
325             if (fixedpdfminorversion <= 2)
326                 pdftex_fail("cannot use progressive DCT with PDF-1.2");
327         case M_SOF0:
328         case M_SOF1:
329         case M_SOF3:
330             (void) read2bytes(jpg_ptr(img)->file);      /* read segment length  */
331             jpg_ptr(img)->bits_per_component = xgetc(jpg_ptr(img)->file);
332             img_height(img) = read2bytes(jpg_ptr(img)->file);
333             img_width(img) = read2bytes(jpg_ptr(img)->file);
334             jpg_ptr(img)->color_space = xgetc(jpg_ptr(img)->file);
335             xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
336             switch (jpg_ptr(img)->color_space) {
337             case JPG_GRAY:
338                 img_color(img) = IMAGE_COLOR_B;
339                 break;
340             case JPG_RGB:
341                 img_color(img) = IMAGE_COLOR_C;
342                 break;
343             case JPG_CMYK:
344                 img_color(img) = IMAGE_COLOR_C;
345                 break;
346             default:
347                 pdftex_fail("Unsupported color space %i",
348                             (int) jpg_ptr(img)->color_space);
349             }
350             return;
351         case M_SOI:            /* ignore markers without parameters */
352         case M_EOI:
353         case M_TEM:
354         case M_RST0:
355         case M_RST1:
356         case M_RST2:
357         case M_RST3:
358         case M_RST4:
359         case M_RST5:
360         case M_RST6:
361         case M_RST7:
362             break;
363         default:               /* skip variable length markers */
364             xfseek(jpg_ptr(img)->file, read2bytes(jpg_ptr(img)->file) - 2,
365                    SEEK_CUR, cur_file_name);
366             break;
367         }
368     }
369 }
370 
write_jpg(integer img)371 void write_jpg(integer img)
372 {
373     long unsigned l;
374     FILE *f;
375     pdf_puts("/Type /XObject\n/Subtype /Image\n");
376     pdf_printf("/Width %i\n/Height %i\n/BitsPerComponent %i\n/Length %i\n",
377                (int) img_width(img),
378                (int) img_height(img),
379                (int) jpg_ptr(img)->bits_per_component,
380                (int) jpg_ptr(img)->length);
381     pdf_puts("/ColorSpace ");
382     if (img_colorspace_ref(img) != 0) {
383         pdf_printf("%i 0 R\n", (int) img_colorspace_ref(img));
384     } else {
385         switch (jpg_ptr(img)->color_space) {
386         case JPG_GRAY:
387             pdf_puts("/DeviceGray\n");
388             break;
389         case JPG_RGB:
390             pdf_puts("/DeviceRGB\n");
391             break;
392         case JPG_CMYK:
393             pdf_puts("/DeviceCMYK\n/Decode [1 0 1 0 1 0 1 0]\n");
394             break;
395         default:
396             pdftex_fail("Unsupported color space %i",
397                         (int) jpg_ptr(img)->color_space);
398         }
399     }
400     pdf_puts("/Filter /DCTDecode\n>>\nstream\n");
401     for (l = jpg_ptr(img)->length, f = jpg_ptr(img)->file; l > 0; l--)
402         pdfout(xgetc(f));
403     pdfendstream();
404 }
405