Debian Codegraph @

texk/web2c/luatexdir/image/writejpg.c

raw
1 /*
2
3 writejpg.w
4
5 Copyright 1996-2006 Han The Thanh <[email protected]>
6 Copyright 2006-2011 Taco Hoekwater <[email protected]>
7
8 This file is part of LuaTeX.
9
10 LuaTeX is free software; you can redistribute it and/or modify it under the terms
11 of the GNU General Public License as published by the Free Software Foundation;
12 either version 2 of the License, or (at your option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include "ptexlib.h"
24 #include <assert.h>
25 #include "image/image.h"
26 #include "image/writejpg.h"
27
28 #define JPG_GRAY 1 /* Gray color space, use /DeviceGray */
29 #define JPG_RGB 3 /* RGB color space, use /DeviceRGB */
30 #define JPG_CMYK 4 /* CMYK color space, use /DeviceCMYK */
31
32 typedef enum {
33 M_SOF0 = 0xc0, /* baseline DCT */
34 M_SOF1 = 0xc1, /* extended sequential DCT */
35 M_SOF2 = 0xc2, /* progressive DCT */
36 M_SOF3 = 0xc3, /* lossless (sequential) */
37
38 M_SOF5 = 0xc5, /* differential sequential DCT */
39 M_SOF6 = 0xc6, /* differential progressive DCT */
40 M_SOF7 = 0xc7, /* differential lossless (sequential) */
41
42 M_JPG = 0xc8, /* reserved for JPEG extensions */
43 M_SOF9 = 0xc9, /* extended sequential DCT */
44 M_SOF10 = 0xca, /* progressive DCT */
45 M_SOF11 = 0xcb, /* lossless (sequential) */
46
47 M_SOF13 = 0xcd, /* differential sequential DCT */
48 M_SOF14 = 0xce, /* differential progressive DCT */
49 M_SOF15 = 0xcf, /* differential lossless (sequential) */
50
51 M_DHT = 0xc4, /* define Huffman table(s) */
52
53 M_DAC = 0xcc, /* define arithmetic conditioning table */
54
55 M_RST0 = 0xd0, /* restart */
56 M_RST1 = 0xd1, /* restart */
57 M_RST2 = 0xd2, /* restart */
58 M_RST3 = 0xd3, /* restart */
59 M_RST4 = 0xd4, /* restart */
60 M_RST5 = 0xd5, /* restart */
61 M_RST6 = 0xd6, /* restart */
62 M_RST7 = 0xd7, /* restart */
63
64 M_SOI = 0xd8, /* start of image */
65 M_EOI = 0xd9, /* end of image */
66 M_SOS = 0xda, /* start of scan */
67 M_DQT = 0xdb, /* define quantization tables */
68 M_DNL = 0xdc, /* define number of lines */
69 M_DRI = 0xdd, /* define restart interval */
70 M_DHP = 0xde, /* define hierarchical progression */
71 M_EXP = 0xdf, /* expand reference image(s) */
72
73 M_APP0 = 0xe0, /* application marker, used for JFIF */
74 M_APP1 = 0xe1, /* application marker */
75 M_APP2 = 0xe2, /* application marker */
76 M_APP3 = 0xe3, /* application marker */
77 M_APP4 = 0xe4, /* application marker */
78 M_APP5 = 0xe5, /* application marker */
79 M_APP6 = 0xe6, /* application marker */
80 M_APP7 = 0xe7, /* application marker */
81 M_APP8 = 0xe8, /* application marker */
82 M_APP9 = 0xe9, /* application marker */
83 M_APP10 = 0xea, /* application marker */
84 M_APP11 = 0xeb, /* application marker */
85 M_APP12 = 0xec, /* application marker */
86 M_APP13 = 0xed, /* application marker */
87 M_APP14 = 0xee, /* application marker, used by Adobe */
88 M_APP15 = 0xef, /* application marker */
89
90 M_JPG0 = 0xf0, /* reserved for JPEG extensions */
91 M_JPG13 = 0xfd, /* reserved for JPEG extensions */
92 M_COM = 0xfe, /* comment */
93
94 M_TEM = 0x01, /* temporary use */
95
96 M_ERROR = 0x100 /* dummy marker, internal use only */
97 } JPEG_MARKER;
98
99 static unsigned int read_exif_bytes(unsigned char **p, int n, int b)
100 {
101 unsigned int rval = 0;
102 unsigned char *pp = *p;
103 if (b) {
104 switch (n) {
105 case 4:
106 rval += *pp++; rval <<= 8;
107 rval += *pp++; rval <<= 8;
108 case 2:
109 rval += *pp++; rval <<= 8;
110 rval += *pp;
111 break;
112 }
113 } else {
114 pp += n;
115 switch (n) {
116 case 4:
117 rval += *--pp; rval <<= 8;
118 rval += *--pp; rval <<= 8;
119 case 2:
120 rval += *--pp; rval <<= 8;
121 rval += *--pp;
122 break;
123 }
124 }
125 *p += n;
126 return rval;
127 }
128
129 /*tex
130
131 The Exif block can contain the data on the resolution in two forms:
132 XResolution, YResolution and ResolutionUnit (tag 282, 283 and 296) as well as
133 PixelPerUnitX, PixelPerUnitY and PixelUnit (tag 0x5111, 0x5112 and 0x5110).
134 Tags 282, 293 and 296 have the priority, with ResolutionUnit set to inch by
135 default, then tag 0x5110, 0x5111 and 0x5112, where the only valid value for
136 PixelUnit is 0.0254, and finally the given value xx and yy, choosen if the
137 Exif x and y resolution are not strictly positive.
138
139 The next one doesn't save the data, just reads the tags we need based on info
140 from \typ {http://www.exif.org/Exif2-2.PDF}.
141
142 */
143
144 static void read_APP1_Exif (FILE *fp, unsigned short length, int *xx, int *yy, int *or)
145 {
146 unsigned char *buffer = (unsigned char *)xmalloc(length);
147 unsigned char *p, *rp;
148 unsigned char *tiff_header;
149 char bigendian;
150 int i;
151 int num_fields, tag, type;
152 /*tex silence uninitialized warnings */
153 int value = 0;
154 unsigned int num = 0;
155 unsigned int den = 0;
156 boolean found_x = false;
157 boolean found_y = false;
158 int tempx = 0;
159 int tempy = 0;
160 double xres = 0;
161 double yres = 0;
162 double res_unit = 1.0;
163 unsigned int xres_ms = 0;
164 unsigned int yres_ms = 0;
165 double res_unit_ms = 0;
166 boolean found_x_ms = false;
167 boolean found_y_ms = false;
168 boolean found_res= false;
169 int orientation = 1;
170 size_t ret_len;
171 ret_len = fread(buffer, length, 1, fp);
172 if (ret_len != 1)
173 goto err ;
174 p = buffer;
175 while ((p < buffer + length) && (*p == 0))
176 ++p;
177 tiff_header = p;
178 if ((*p == 'M') && (*(p+1) == 'M'))
179 bigendian = 1;
180 else if ((*p == 'I') && (*(p+1) == 'I'))
181 bigendian = 0;
182 else
183 goto err;
184 p += 2;
185 i = read_exif_bytes(&p, 2, bigendian);
186 if (i != 42)
187 goto err;
188 i = read_exif_bytes(&p, 4, bigendian);
189 p = tiff_header + i;
190 num_fields = read_exif_bytes(&p, 2, bigendian);
191 while (num_fields-- > 0) {
192 tag = read_exif_bytes(&p, 2, bigendian);
193 type = read_exif_bytes(&p, 2, bigendian);
194 read_exif_bytes(&p, 4, bigendian);
195 switch (type) {
196 case 1:
197 /*tex byte */
198 value = *p++;
199 p += 3;
200 break;
201 case 3:
202 /*tex unsigned short */
203 case 8:
204 /*tex signed short */
205 value = read_exif_bytes(&p, 2, bigendian);
206 p += 2;
207 break;
208 case 4:
209 /*tex unsigned long */
210 case 9:
211 /*tex signed long */
212 value = read_exif_bytes(&p, 4, bigendian);
213 break;
214 case 5:
215 /*tex rational */
216 case 10:
217 /*tex srational */
218 value = read_exif_bytes(&p, 4, bigendian);
219 rp = tiff_header + value;
220 num = read_exif_bytes(&rp, 4, bigendian);
221 den = read_exif_bytes(&rp, 4, bigendian);
222 break;
223 case 7:
224 /*tex undefined */
225 value = *p++;
226 p += 3;
227 break;
228 case 2:
229 /*tex ascii */
230 default:
231 p += 4;
232 break;
233 }
234 switch (tag) {
235 case 274:
236 /*tex orientation */
237 orientation = value;
238 break;
239 case 282:
240 /*tex x res */
241 if (den != 0) {
242 xres = num / den;
243 found_x = true;
244 }
245 break;
246 case 283:
247 /*tex y res */
248 if (den != 0) {
249 yres = num / den;
250 found_y = true ;
251 }
252 break;
253 case 296:
254 /*tex res unit */
255 switch (value) {
256 case 2:
257 res_unit = 1.0;
258 break;
259 case 3:
260 res_unit = 2.54;
261 break;
262 default:
263 res_unit = 0;
264 break;
265 }
266 break;
267 case 0x5110:
268 /*tex PixelUnit */
269 switch (value) {
270 case 1:
271 res_unit_ms = 0.0254; /* Unit is meter */
272 break;
273 default:
274 res_unit_ms = 0;
275 }
276 break;
277 case 0x5111:
278 /*tex PixelPerUnitX */
279 found_x_ms = true ;
280 xres_ms = value;
281 break;
282 case 0x5112:
283 /*tex PixelPerUnitY */
284 found_y_ms = true ;
285 yres_ms = value ;
286 break;
287 }
288 }
289 if (found_x && found_y && res_unit>0) {
290 found_res = true;
291 tempx = (int)(xres * res_unit+0.5);
292 tempy = (int)(yres * res_unit+0.5);
293 } else if (found_x_ms && found_y_ms && res_unit_ms==0.0254) {
294 found_res = true;
295 tempx = (int)(xres_ms * res_unit_ms+0.5);
296 tempy = (int)(yres_ms * res_unit_ms+0.5);
297 }
298 if (found_res) {
299 if (tempx>0 && tempy>0) {
300 if ((tempx!=(*xx) || tempy!=(*yy)) && (*xx!=0 && (*yy!=0) ) ) {
301 formatted_warning("readjpg","Exif resolution %ddpi x %ddpi differs from the input resolution %ddpi x %ddpi",tempx,tempy,*xx,*yy);
302 }
303 if (tempx==1 || tempy==1) {
304 formatted_warning("readjpg","Exif resolution %ddpi x %ddpi looks weird", tempx, tempy);
305 }
306 *xx = tempx;
307 *yy = tempy;
308 } else {
309 formatted_warning("readjpg","Bad Exif resolution %ddpi x %ddpi (zero or negative value of a signed integer)",tempx,tempy);
310 }
311 }
312 *or = orientation;
313 err:
314 free(buffer);
315 return;
316 }
317
318 /*tex
319
320 Contrary to \PDF\ where several parallel usage can happen (\PDF, |TEX, \LUA)
321 with bitmaps we care less about keeping files open. So, we can keep files
322 open in the img lib but then they are closed after inclusion anyway.
323
324 */
325
326 static void close_and_cleanup_jpg(image_dict * idict)
327 {
328 /*tex if one of then is not NULL we already cleaned up */
329 if (img_file(idict) != NULL) {
330 xfclose(img_file(idict), img_filepath(idict));
331 img_file(idict) = NULL;
332 }
333 if (img_jpg_ptr(idict) != NULL) {
334 xfree(img_jpg_ptr(idict));
335 }
336 }
337
338 void flush_jpg_info(image_dict * idict)
339 {
340 close_and_cleanup_jpg(idict);
341 }
342
343 /*tex
344
345 The jpeg images are scanned for resolution, colorspace, depth, dimensions and
346 orientation. We need to look at the exif blob for that. The original version
347 did a quick test for jfif and exif but there can be more blobs later on. The
348 current approach is to run over the linked list of blobs which is somewhat
349 less efficient but not noticeable.
350
351 */
352
353 void read_jpg_info(image_dict * idict)
354 {
355 int i, position, units = 0;
356 unsigned short length;
357 int okay = 0 ;
358 FILE *fp = img_file(idict);
359 if (img_type(idict) != IMG_TYPE_JPG) {
360 normal_error("readjpg","conflicting image dictionary");
361 }
362 if (fp != NULL) {
363 normal_error("readjpg","image data already read");
364 }
365 fp = xfopen(img_filepath(idict), FOPEN_RBIN_MODE);
366 img_totalpages(idict) = 1;
367 img_pagenum(idict) = 1;
368 img_xres(idict) = img_yres(idict) = 0;
369 img_file(idict) = fp;
370 if (fp == NULL) {
371 normal_error("readjpg","unable to read image file");
372 }
373 img_jpg_ptr(idict) = xtalloc(1, jpg_img_struct);
374 xfseek(fp, 0, SEEK_END, img_filepath(idict));
375 img_jpg_ptr(idict)->length = xftell(fp, img_filepath(idict));
376 xfseek(fp, 0, SEEK_SET, img_filepath(idict));
377 if ((unsigned int) read2bytes(fp) != 0xFFD8) {
378 normal_error("readjpg","no header found");
379 }
380 xfseek(fp, 0, SEEK_SET, img_filepath(idict));
381 while (1) {
382 if (feof(fp)) {
383 if (okay) {
384 break ;
385 } else {
386 normal_error("readjpg","premature file end");
387 }
388 } else if (fgetc(fp) != 0xFF) {
389 if (okay) {
390 break ;
391 } else {
392 normal_error("readjpg","no marker found");
393 }
394 }
395 i = xgetc(fp);
396 position = ftell(fp);
397 length = 0 ;
398 switch (i) {
399 case M_SOF3:
400 /*tex lossless */
401 case M_SOF5:
402 case M_SOF6:
403 case M_SOF7:
404 /*tex lossless */
405 case M_SOF9:
406 case M_SOF10:
407 case M_SOF11:
408 /*tex lossless */
409 case M_SOF13:
410 case M_SOF14:
411 case M_SOF15:
412 /*tex lossless */
413 formatted_error("readjpg","unsupported compression SOF_%d", i - M_SOF0);
414 break;
415 case M_SOF2:
416 if (img_pdfmajorversion(idict) < 2 && img_pdfminorversion(idict) <= 2) {
417 normal_error("readjpg","progressive DCT with PDF-1.2 is not permitted");
418 }
419 case M_SOF0:
420 case M_SOF1:
421 /*tex read segment length */
422 length = (int) read2bytes(fp);
423 img_colordepth(idict) = xgetc(fp);
424 img_ysize(idict) = (int) read2bytes(fp);
425 img_xsize(idict) = (int) read2bytes(fp);
426 img_jpg_color(idict) = xgetc(fp);
427 switch (img_jpg_color(idict)) {
428 case JPG_GRAY:
429 img_procset(idict) |= PROCSET_IMAGE_B;
430 break;
431 case JPG_RGB:
432 img_procset(idict) |= PROCSET_IMAGE_C;
433 break;
434 case JPG_CMYK:
435 img_procset(idict) |= PROCSET_IMAGE_C;
436 break;
437 default:
438 formatted_error("readjpg","unsupported color space %i", (int) img_jpg_color(idict));
439 }
440 okay = 1 ;
441 break ;
442 case M_APP0:
443 {
444 char app_sig[32];
445 length = (int) read2bytes(fp);
446 if (length > 6) {
447 if (fread(app_sig, sizeof(char), 5, fp) != 5)
448 return;
449 if (!memcmp(app_sig, "JFIF\000", 5)) {
450 /*tex skip two bytes, compiler is also happy*/
451 units = (int) read2bytes(fp);
452 units = xgetc(fp);
453 img_xres(idict) = (int) read2bytes(fp);
454 img_yres(idict) = (int) read2bytes(fp);
455 switch (units) {
456 case 1:
457 /*tex pixels per inch */
458 if ((img_xres(idict) == 1) || (img_yres(idict) == 1)) {
459 formatted_warning("readjpg","unusual resolution of %ddpi by %ddpi", img_xres(idict), img_yres(idict));
460 }
461 break;
462 case 2:
463 /*tex pixels per cm */
464 img_xres(idict) = (int) ((double) img_xres(idict) * 2.54);
465 img_yres(idict) = (int) ((double) img_yres(idict) * 2.54);
466 break;
467 default:
468 img_xres(idict) = img_yres(idict) = 0;
469 break;
470 }
471 }
472 /*tex
473 If either xres or yres is 0 but the other isn't, set
474 it to the value of the other.
475 */
476 }
477 }
478 break;
479 case M_APP1:
480 {
481 char app_sig[32];
482 length = (int) read2bytes(fp);
483 if (length > 7) {
484 if (fread(app_sig, sizeof(char), 5, fp) != 5)
485 return;
486 if (!memcmp(app_sig, "Exif\000", 5)) {
487 int xxres = img_xres(idict);
488 int yyres = img_yres(idict);
489 int orientation = img_orientation(idict);
490 read_APP1_Exif(fp, length - 7, &xxres, &yyres, &orientation);
491 img_xres(idict) = xxres;
492 img_yres(idict) = yyres;
493 img_orientation(idict) = orientation;
494 }
495 }
496 }
497 break;
498 /*tex ignore markers without parameters */
499 case M_SOI:
500 case M_EOI:
501 case M_TEM:
502 case M_RST0:
503 case M_RST1:
504 case M_RST2:
505 case M_RST3:
506 case M_RST4:
507 case M_RST5:
508 case M_RST6:
509 case M_RST7:
510 break;
511 default:
512 /*tex skip variable length markers */
513 length = (int) read2bytes(fp);
514 break;
515 }
516 if (length > 0) {
517 xfseek(fp, position + length, SEEK_SET, img_filepath(idict));
518 }
519 }
520 xfseek(fp, 0, SEEK_SET, img_filepath(idict));
521 if (! img_keepopen(idict)) {
522 close_and_cleanup_jpg(idict);
523 }
524 if (okay){
525 if ((img_xres(idict) == 0) && (img_yres(idict) != 0)) {
526 img_xres(idict) = img_yres(idict);
527 }
528 if ((img_yres(idict) == 0) && (img_xres(idict) != 0)) {
529 img_yres(idict) = img_xres(idict);
530 }
531 } else {
532 normal_error("readjpg","unknown fatal error");
533 }
534 }
535
536 static void reopen_jpg(image_dict * idict)
537 {
538 int width = img_xsize(idict);
539 int height = img_ysize(idict);
540 int xres = img_xres(idict);
541 int yres = img_yres(idict);
542 /*tex
543 we need to make sure that the file kept open
544 */
545 img_keepopen(idict) = 1;
546 read_jpg_info(idict);
547 if (width != img_xsize(idict) || height != img_ysize(idict) || xres != img_xres(idict) || yres != img_yres(idict)) {
548 normal_error("writejpg","image dimensions have changed");
549 }
550 }
551
552 void write_jpg(PDF pdf, image_dict * idict)
553 {
554 size_t l;
555 if (img_file(idict) == NULL) {
556 reopen_jpg(idict);
557 }
558 pdf_begin_obj(pdf, img_objnum(idict), OBJSTM_NEVER);
559 pdf_begin_dict(pdf);
560 pdf_dict_add_name(pdf, "Type", "XObject");
561 pdf_dict_add_name(pdf, "Subtype", "Image");
562 pdf_dict_add_img_filename(pdf, idict);
563 if (img_attr(idict) != NULL && strlen(img_attr(idict)) > 0) {
564 pdf_printf(pdf, "\n%s\n", img_attr(idict));
565 }
566 pdf_dict_add_int(pdf, "Width", (int) img_xsize(idict));
567 pdf_dict_add_int(pdf, "Height", (int) img_ysize(idict));
568 pdf_dict_add_int(pdf, "BitsPerComponent", (int) img_colordepth(idict));
569 pdf_dict_add_int(pdf, "Length", (int) img_jpg_ptr(idict)->length);
570 if (img_colorspace(idict) != 0) {
571 pdf_dict_add_ref(pdf, "ColorSpace", (int) img_colorspace(idict));
572 } else {
573 switch (img_jpg_color(idict)) {
574 case JPG_GRAY:
575 pdf_dict_add_name(pdf, "ColorSpace", "DeviceGray");
576 break;
577 case JPG_RGB:
578 pdf_dict_add_name(pdf, "ColorSpace", "DeviceRGB");
579 break;
580 case JPG_CMYK:
581 pdf_dict_add_name(pdf, "ColorSpace", "DeviceCMYK");
582 pdf_add_name(pdf, "Decode");
583 pdf_begin_array(pdf);
584 pdf_add_int(pdf, 1);
585 pdf_add_int(pdf, 0);
586 pdf_add_int(pdf, 1);
587 pdf_add_int(pdf, 0);
588 pdf_add_int(pdf, 1);
589 pdf_add_int(pdf, 0);
590 pdf_add_int(pdf, 1);
591 pdf_add_int(pdf, 0);
592 pdf_end_array(pdf);
593 break;
594 default:
595 formatted_error("writejpg","unsupported JPEG color space %i", (int) img_jpg_color(idict));
596 }
597 }
598 pdf_dict_add_name(pdf, "Filter", "DCTDecode");
599 pdf_end_dict(pdf);
600 pdf_begin_stream(pdf);
601 l = (size_t) img_jpg_ptr(idict)->length;
602 xfseek(img_file(idict), 0, SEEK_SET, img_filepath(idict));
603 if (read_file_to_buf(pdf, img_file(idict), l) != l) {
604 normal_error("writejpg","fread failed");
605 }
606 pdf_end_stream(pdf);
607 pdf_end_obj(pdf);
608 close_and_cleanup_jpg(idict);
609 }