Debian Codegraph @

texk/web2c/pdftexdir/writeimg.c

raw
1 /*
2 Copyright 1996-2021 Han The Thanh, <[email protected]>
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 #include <kpathsea/c-auto.h>
23 #include <kpathsea/c-memstr.h>
24
25 #define bp2int(p) round(p*(onehundredbp/100.0))
26
27 /* define image_ptr, image_array & image_limit */
28 /* define_array(image); */
29
30 /* avoid use of size_t */
31 image_entry *image_ptr, *image_array = NULL;
32 integer image_limit;
33
34 float epdf_width;
35 float epdf_height;
36 float epdf_orig_x;
37 float epdf_orig_y;
38 float epdf_rotate;
39 int epdf_selected_page;
40 int epdf_num_pages;
41 int epdf_page_box;
42 void *epdf_doc;
43 int epdf_has_page_group;
44
45 static integer new_image_entry(void)
46 {
47 alloc_array(image, 1, SMALL_BUF_SIZE);
48 image_ptr->image_type = IMAGE_TYPE_NONE;
49 image_ptr->color_type = 0;
50 image_ptr->num_pages = 0;
51 image_ptr->x_res = 0;
52 image_ptr->y_res = 0;
53 image_ptr->width = 0;
54 image_ptr->height = 0;
55 image_ptr->rotate = 0;
56 image_ptr->colorspace_ref = 0;
57 image_ptr->group_ref = 0;
58 return image_ptr++ - image_array;
59 }
60
61 integer imagecolor(integer img)
62 {
63 return img_color(img);
64 }
65
66 integer imagewidth(integer img)
67 {
68 return img_width(img);
69 }
70
71 integer imageheight(integer img)
72 {
73 return img_height(img);
74 }
75
76 integer imagerotate(integer img)
77 {
78 return img_rotate(img);
79 }
80
81 integer imagexres(integer img)
82 {
83 return img_xres(img);
84 }
85
86 integer imageyres(integer img)
87 {
88 return img_yres(img);
89 }
90
91 boolean ispdfimage(integer img)
92 {
93 return img_type(img) == IMAGE_TYPE_PDF;
94 }
95
96 boolean ispngimage(integer img)
97 {
98 return img_type(img) == IMAGE_TYPE_PNG;
99 }
100
101 boolean checkimageb(integer procset)
102 {
103 return procset & IMAGE_COLOR_B;
104 }
105
106 boolean checkimagec(integer procset)
107 {
108 return procset & IMAGE_COLOR_C;
109 }
110
111 boolean checkimagei(integer procset)
112 {
113 return procset & IMAGE_COLOR_I;
114 }
115
116 void updateimageprocset(integer img)
117 {
118 pdfimageprocset |= img_color(img);
119 }
120
121 integer epdforigx(integer img)
122 {
123 return pdf_ptr(img)->orig_x;
124 }
125
126 integer epdforigy(integer img)
127 {
128 return pdf_ptr(img)->orig_y;
129 }
130
131 integer imagepages(integer img)
132 {
133 return img_pages(img);
134 }
135
136 integer imagecolordepth(integer img)
137 {
138 switch (img_type(img)) {
139 case IMAGE_TYPE_PNG:
140 return png_get_bit_depth(png_ptr(img), png_info(img));
141 case IMAGE_TYPE_JPG:
142 return jpg_ptr(img)->bits_per_component;
143 case IMAGE_TYPE_JBIG2:
144 return 0;
145 case IMAGE_TYPE_PDF:
146 return 0;
147 default:
148 pdftex_fail("unknown type of image");
149 return -1; /* to make the compiler happy */
150 }
151 }
152
153 integer getimagegroupref(integer img)
154 {
155 return img_group_ref(img);
156 }
157
158 void setimagegroupref(integer img, integer value)
159 {
160 img_group_ref(img) = value;
161 }
162
163 /*
164 Patch ImageTypeDetection 2003/02/08 by Heiko Oberdiek.
165
166 Function "readimage" performs some basic initializations.
167 Then it looks at the file extension to determine the
168 image type and calls specific code/functions.
169 The main disadvantage is that standard file extensions
170 have to be used, otherwise pdfTeX is not able to detect
171 the correct image type.
172
173 The patch now looks at the file header first regardless of
174 the file extension. This is implemented in function
175 "checktypebyheader". If this check fails, the traditional
176 test of standard file extension is tried, done in function
177 "checktypebyextension".
178
179 Magic headers:
180
181 * "PNG (Portable Network Graphics) Specification", Version 1.2
182 (http://www.libpng.org/pub/png):
183
184 | 3.1. PNG file signature
185 |
186 | The first eight bytes of a PNG file always contain the following
187 | (decimal) values:
188 |
189 | 137 80 78 71 13 10 26 10
190
191 Translation to C: "\x89PNG\r\n\x1A\n"
192
193 * "JPEG File Interchange Format", Version 1.02:
194
195 | o you can identify a JFIF file by looking for the following
196 | sequence: X'FF', SOI X'FF', APP0, <2 bytes to be skipped>,
197 | "JFIF", X'00'.
198
199 Function "checktypebyheader" only looks at the first two bytes:
200 "\xFF\xD8"
201
202 * ISO/IEC JTC 1/SC 29/WG 1
203 (ITU-T SG8)
204 Coding of Still Pictures
205 Title: 14492 FCD
206 Source: JBIG Committee
207 Project: JTC 1.29.10
208 Status: Final Committee Draft
209
210 | D.4.1, ID string
211 |
212 | This is an 8-byte sequence containing 0x97 0x4A 0x42 0x32 0x0D 0x0A
213 | 0x1A 0x0A.
214
215 * "PDF Reference", third edition:
216 * The first line should contain "%PDF-1.0" until "%PDF-1.4"
217 (section 3.4.1 "File Header").
218 * The "implementation notes" say:
219
220 | 3.4.1, File Header
221 | 12. Acrobat viewers require only that the header appear
222 | somewhere within the first 1024 bytes of the file.
223 | 13. Acrobat viewers will also accept a header of the form
224 | %!PS-Adobe-N.n PDF-M.m
225
226 The check in function "checktypebyheader" only implements
227 the first issue. The implementation notes are not considered.
228 Therefore files with garbage at start of file must have the
229 standard extension.
230
231 Functions "checktypebyheader" and "checktypebyextension":
232 img_type(img) is set to IMAGE_TYPE_NONE by new_image_entry().
233 Both functions tries to detect a type and set img_type(img).
234 Thus a value other than IMAGE_TYPE_NONE indicates that a
235 type is found.
236 */
237
238 #define HEADER_JPG "\xFF\xD8"
239 #define HEADER_PNG "\x89PNG\r\n\x1A\n"
240 #define HEADER_JBIG2 "\x97\x4A\x42\x32\x0D\x0A\x1A\x0A"
241 #define HEADER_PDF "%PDF-1."
242 #define MAX_HEADER (sizeof(HEADER_PNG)-1)
243 static void checktypebyheader(integer img)
244 {
245 int i;
246 FILE *file = NULL;
247 char header[MAX_HEADER];
248
249 if (img_type(img) != IMAGE_TYPE_NONE) /* nothing to do */
250 return;
251
252 /* read the header */
253 file = xfopen(img_name(img), FOPEN_RBIN_MODE);
254 for (i = 0; (unsigned) i < MAX_HEADER; i++) {
255 header[i] = xgetc(file);
256 if (feof(file))
257 pdftex_fail("reading image file failed");
258 }
259 xfclose(file, img_name(img));
260
261 /* tests */
262 if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0)
263 img_type(img) = IMAGE_TYPE_JPG;
264 else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0)
265 img_type(img) = IMAGE_TYPE_PNG;
266 else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0)
267 img_type(img) = IMAGE_TYPE_JBIG2;
268 else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0)
269 img_type(img) = IMAGE_TYPE_PDF;
270 }
271
272 static void checktypebyextension(integer img)
273 {
274 char *image_suffix;
275
276 if (img_type(img) != IMAGE_TYPE_NONE) /* nothing to do */
277 return;
278 /* tests */
279 if ((image_suffix = strrchr(cur_file_name, '.')) == 0)
280 img_type(img) = IMAGE_TYPE_NONE;
281 else if (strcasecmp(image_suffix, ".png") == 0)
282 img_type(img) = IMAGE_TYPE_PNG;
283 else if (strcasecmp(image_suffix, ".jpg") == 0 ||
284 strcasecmp(image_suffix, ".jpeg") == 0)
285 img_type(img) = IMAGE_TYPE_JPG;
286 else if (strcasecmp(image_suffix, ".jbig2") == 0 ||
287 strcasecmp(image_suffix, ".jb2") == 0)
288 img_type(img) = IMAGE_TYPE_JBIG2;
289 else if (strcasecmp(image_suffix, ".pdf") == 0)
290 img_type(img) = IMAGE_TYPE_PDF;
291 }
292
293 integer readimage(strnumber s, integer page_num, strnumber page_name,
294 integer colorspace, integer pagebox,
295 integer pdfmajorversion, integer pdfminorversion, integer pdfinclusionerrorlevel)
296 {
297 char *dest = NULL;
298 integer img = new_image_entry();
299 img_colorspace_ref(img) = colorspace;
300
301 /* need to allocate new string as makecstring's buffer is
302 already used by cur_file_name */
303 if (page_name != 0)
304 dest = xstrdup(makecstring(page_name));
305 cur_file_name = find_input_file(s);
306 img_name(img) = cur_file_name;
307 if (img_name(img) == NULL)
308 pdftex_fail("cannot find image file %s", makecstring(s));
309 recorder_record_input(cur_file_name);
310 /* type checks */
311 checktypebyheader(img);
312 checktypebyextension(img);
313 /* read image */
314 switch (img_type(img)) {
315 case IMAGE_TYPE_PDF:
316 pdf_ptr(img) = xtalloc(1, pdf_image_struct);
317 pdf_ptr(img)->page_box = pagebox;
318 page_num = read_pdf_info(img_name(img), dest, page_num, pagebox,
319 pdfmajorversion, pdfminorversion, pdfinclusionerrorlevel);
320 img_width(img) = bp2int(epdf_width);
321 img_height(img) = bp2int(epdf_height);
322 img_rotate(img) = epdf_rotate;
323 img_pages(img) = epdf_num_pages;
324 pdf_ptr(img)->orig_x = bp2int(epdf_orig_x);
325 pdf_ptr(img)->orig_y = bp2int(epdf_orig_y);
326 pdf_ptr(img)->selected_page = page_num;
327 pdf_ptr(img)->doc = epdf_doc;
328 if (epdf_has_page_group == 1)
329 img_group_ref(img) = -1; // page group present, but new object
330 // number not set yet
331 else
332 img_group_ref(img) = 0; // no page group present
333 break;
334 case IMAGE_TYPE_PNG:
335 img_pages(img) = 1;
336 read_png_info(img);
337 break;
338 case IMAGE_TYPE_JPG:
339 jpg_ptr(img) = xtalloc(1, JPG_IMAGE_INFO);
340 img_pages(img) = 1;
341 read_jpg_info(img);
342 break;
343 case IMAGE_TYPE_JBIG2:
344 if (pdfmajorversion == 1 && pdfminorversion < 4) {
345 pdftex_fail
346 ("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
347 (int) pdfminorversion);
348 }
349 jbig2_ptr(img) = xtalloc(1, JBIG2_IMAGE_INFO);
350 img_type(img) = IMAGE_TYPE_JBIG2;
351 jbig2_ptr(img)->selected_page = page_num;
352 read_jbig2_info(img);
353 break;
354 default:
355 pdftex_fail("unknown type of image");
356 }
357 xfree(dest);
358 cur_file_name = NULL;
359 return img;
360 }
361
362 void writeimage(integer img)
363 {
364 cur_file_name = img_name(img);
365 tex_printf(" <%s", img_name(img));
366 switch (img_type(img)) {
367 case IMAGE_TYPE_PNG:
368 write_png(img);
369 break;
370 case IMAGE_TYPE_JPG:
371 write_jpg(img);
372 break;
373 case IMAGE_TYPE_JBIG2:
374 write_jbig2(img);
375 break;
376 case IMAGE_TYPE_PDF:
377 epdf_doc = pdf_ptr(img)->doc;
378 epdf_selected_page = pdf_ptr(img)->selected_page;
379 epdf_page_box = pdf_ptr(img)->page_box;
380 write_epdf();
381 break;
382 default:
383 pdftex_fail("unknown type of image");
384 }
385
386 tex_printf(">");
387 cur_file_name = NULL;
388 }
389
390 void deleteimage(integer img)
391 {
392 if (iniversion)
393 return; // The image may be \dump{}ed to a format
394 switch (img_type(img)) {
395 case IMAGE_TYPE_PDF:
396 epdf_doc = pdf_ptr(img)->doc;
397 epdf_delete();
398 break;
399 case IMAGE_TYPE_PNG:
400 xfclose((FILE *) png_get_io_ptr(png_ptr(img)), cur_file_name);
401 png_destroy_read_struct(&(png_ptr(img)), &(png_info(img)), NULL);
402 break;
403 case IMAGE_TYPE_JPG:
404 xfclose(jpg_ptr(img)->file, cur_file_name);
405 break;
406 case IMAGE_TYPE_JBIG2:
407 break;
408 default:
409 pdftex_fail("unknown type of image");
410 }
411 xfree(img_name(img));
412 return;
413 }
414
415 void img_free(void)
416 {
417 xfree(image_array);
418 }
419
420
421 /* To allow the use of \pdfrefximage inside saved boxes in -ini mode,
422 * the information in the array has to be (un)dumped with the format.
423 * The next two routines take care of that.
424 *
425 * Most of the work involved in setting up the images is simply
426 * executed again. This solves the many possible errors resulting from
427 * the split in two separate runs.
428
429 * There was only one problem remaining: The pdfversion and
430 * pdfinclusionerrorlevel can have changed inbetween the call to
431 * readimage() and dump time. That is why they are passed as arguments
432 * to undumpimagemeta once more.
433 */
434
435 /* some of the dumped values are really type int, not integer,
436 * but since the macro falls back to generic_dump anyway, that
437 * does not matter.
438 */
439
440 /* #define dumpsizet generic_dump */
441 #define dumpinteger generic_dump
442
443 /* #define undumpsizet generic_undump */
444 #define undumpinteger generic_undump
445
446 void dumpimagemeta(void)
447 {
448 int cur_image, img;
449
450 dumpinteger(image_limit);
451 cur_image = (image_ptr - image_array);
452 dumpinteger(cur_image);
453
454 for (img = 0; img < cur_image; img++) {
455
456 dumpcharptr(img_name(img));
457 dumpinteger(img_type(img));
458 dumpinteger(img_color(img));
459 dumpinteger(img_width(img));
460 dumpinteger(img_height(img));
461 dumpinteger(img_xres(img));
462 dumpinteger(img_yres(img));
463 dumpinteger(img_pages(img));
464 dumpinteger(img_colorspace_ref(img));
465 dumpinteger(img_group_ref(img));
466
467 /* the image_struct is not dumped at all, except for a few
468 variables that are needed to restore the contents */
469
470 if (img_type(img) == IMAGE_TYPE_PDF) {
471 dumpinteger(pdf_ptr(img)->page_box);
472 dumpinteger(pdf_ptr(img)->selected_page);
473 } else if (img_type(img) == IMAGE_TYPE_JBIG2) {
474 dumpinteger(jbig2_ptr(img)->selected_page);
475 }
476
477 }
478 }
479
480 void undumpimagemeta(integer pdfmajorversion, integer pdfminorversion, integer pdfinclusionerrorlevel)
481 {
482 int cur_image, img;
483
484 undumpinteger(image_limit);
485
486 image_array = xtalloc(image_limit, image_entry);
487 undumpinteger(cur_image);
488 image_ptr = image_array + cur_image;
489
490 for (img = 0; img < cur_image; img++) {
491 undumpcharptr(img_name(img));
492 undumpinteger(img_type(img));
493 undumpinteger(img_color(img));
494 undumpinteger(img_width(img));
495 undumpinteger(img_height(img));
496 undumpinteger(img_xres(img));
497 undumpinteger(img_yres(img));
498 undumpinteger(img_pages(img));
499 undumpinteger(img_colorspace_ref(img));
500 undumpinteger(img_group_ref(img));
501
502 /* if img_name(img)==NULL -- which it shouldn't be -- the next line
503 will trigger an assertion failure. */
504 if (kpse_find_file(img_name(img), kpse_tex_format, true) == NULL)
505 pdftex_fail("cannot find image file %s", img_name(img));
506
507 switch (img_type(img)) {
508 case IMAGE_TYPE_PDF:
509 pdf_ptr(img) = xtalloc(1, pdf_image_struct);
510
511 undumpinteger(pdf_ptr(img)->page_box);
512 undumpinteger(pdf_ptr(img)->selected_page);
513
514 read_pdf_info(img_name(img), NULL, pdf_ptr(img)->selected_page,
515 pdf_ptr(img)->page_box, pdfmajorversion, pdfminorversion,
516 pdfinclusionerrorlevel);
517
518 img_width(img) = bp2int(epdf_width);
519 img_height(img) = bp2int(epdf_height);
520 img_pages(img) = epdf_num_pages;
521 pdf_ptr(img)->orig_x = bp2int(epdf_orig_x);
522 pdf_ptr(img)->orig_y = bp2int(epdf_orig_y);
523 pdf_ptr(img)->doc = epdf_doc;
524 break;
525 case IMAGE_TYPE_PNG:
526 img_pages(img) = 1;
527 read_png_info(img);
528 break;
529 case IMAGE_TYPE_JPG:
530 jpg_ptr(img) = xtalloc(1, JPG_IMAGE_INFO);
531 img_pages(img) = 1;
532 read_jpg_info(img);
533 break;
534 case IMAGE_TYPE_JBIG2:
535 if (pdfmajorversion == 1 && pdfminorversion < 4) {
536 pdftex_fail
537 ("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
538 (int) pdfminorversion);
539 }
540 jbig2_ptr(img) = xtalloc(1, JBIG2_IMAGE_INFO);
541 img_type(img) = IMAGE_TYPE_JBIG2;
542 undumpinteger(jbig2_ptr(img)->selected_page);
543 read_jbig2_info(img);
544 break;
545 default:
546 pdftex_fail("unknown type of image");
547 }
548 }
549 }