Debian Codegraph @

texk/dvipdfm-x/pdfximage.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 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 #include "system.h"
28 #include "error.h"
29 #include "mem.h"
30
31 #include "dpxconf.h"
32 #include "dpxfile.h"
33 #include "dpxutil.h"
34
35 #include "pdfobj.h"
36
37 #include "pdfdoc.h"
38 #include "pdfdev.h"
39 #include "pdfdraw.h"
40 #include "pdfnames.h"
41
42 #include "epdf.h"
43 #include "mpost.h"
44 #include "pngimage.h"
45 #include "jpegimage.h"
46 #include "jp2image.h"
47 #include "bmpimage.h"
48
49 #include "pdfximage.h"
50
51 static int check_for_ps (FILE *image_file);
52 static int check_for_mp (FILE *image_file);
53 static int ps_include_page (pdf_ximage *ximage,
54 const char *ident, load_options options);
55
56
57 #define IMAGE_TYPE_UNKNOWN -1
58 #define IMAGE_TYPE_PDF 0
59 #define IMAGE_TYPE_JPEG 1
60 #define IMAGE_TYPE_PNG 2
61 #define IMAGE_TYPE_MPS 4
62 #define IMAGE_TYPE_EPS 5
63 #define IMAGE_TYPE_BMP 6
64 #define IMAGE_TYPE_JP2 7
65
66 #if defined(_WIN32)
67 extern int fsyscp_stat(const char *path, struct stat *buffer);
68 #endif /* _WIN32 */
69
70 struct attr_
71 {
72 int width, height;
73 double xdensity, ydensity;
74 pdf_rect bbox;
75
76 /* Not appropriate place but... someone need them. */
77 int page_no;
78 int page_count;
79 int bbox_type; /* Ugh */
80 pdf_obj *dict;
81 char tempfile;
82 char *page_name;
83 };
84
85 struct pdf_ximage_
86 {
87 char *ident;
88 char res_name[16];
89
90 int subtype;
91
92 struct attr_ attr;
93
94 char *filename;
95 char *fullname;
96 pdf_obj *reference;
97 pdf_obj *resource;
98
99 int reserved;
100 };
101
102
103 /* verbose, verbose, verbose... */
104 struct opt_
105 {
106 char *cmdtmpl;
107 };
108
109 static struct opt_ _opts = {
110 NULL
111 };
112
113 struct ic_
114 {
115 int count, capacity;
116 pdf_ximage *ximages;
117 };
118
119 static struct ic_ _ic = {
120 0, 0, NULL
121 };
122
123 static void
124 pdf_init_ximage_struct (pdf_ximage *I)
125 {
126 I->ident = NULL;
127 I->filename = NULL;
128 I->fullname = NULL;
129
130 I->subtype = -1;
131 memset(I->res_name, 0, 16);
132 I->reference = NULL;
133 I->resource = NULL;
134 I->reserved = 0;
135
136 I->attr.width = I->attr.height = 0;
137 I->attr.xdensity = I->attr.ydensity = 1.0;
138 I->attr.bbox.llx = I->attr.bbox.lly = 0;
139 I->attr.bbox.urx = I->attr.bbox.ury = 0;
140
141 I->attr.page_no = 1;
142 I->attr.page_count = 1;
143 I->attr.bbox_type = 0;
144
145 I->attr.dict = NULL;
146 I->attr.tempfile = 0;
147
148 I->attr.page_name = NULL;
149 }
150
151 static void
152 pdf_set_ximage_tempfile (pdf_ximage *I, const char *fullname)
153 {
154 if (I->fullname)
155 RELEASE(I->fullname);
156 I->fullname = NEW(strlen(fullname)+1, char);
157 strcpy(I->fullname, fullname);
158 I->attr.tempfile = 1;
159 }
160
161 static void
162 pdf_clean_ximage_struct (pdf_ximage *I)
163 {
164 if (I->ident)
165 RELEASE(I->ident);
166 if (I->filename)
167 RELEASE(I->filename);
168 if (I->fullname)
169 RELEASE(I->fullname);
170 if (I->reference)
171 pdf_release_obj(I->reference);
172 if (I->resource)
173 pdf_release_obj(I->resource);
174 if (I->attr.dict) /* unsafe? */
175 pdf_release_obj(I->attr.dict);
176 pdf_init_ximage_struct(I);
177 }
178
179
180 void
181 pdf_init_images (void)
182 {
183 struct ic_ *ic = &_ic;
184 ic->count = 0;
185 ic->capacity = 0;
186 ic->ximages = NULL;
187 }
188
189 void
190 pdf_close_images (void)
191 {
192 struct ic_ *ic = &_ic;
193 if (ic->ximages) {
194 int i;
195 for (i = 0; i < ic->count; i++) {
196 pdf_ximage *I = ic->ximages+i;
197 if (I->attr.tempfile) {
198 /*
199 * It is important to remove temporary files at the end because
200 * we cache file names. Since we use mkstemp to create them, we
201 * might get the same file name again if we delete the first file.
202 * (This happens on NetBSD, reported by Jukka Salmi.)
203 * We also use this to convert a PS file only once if multiple
204 * pages are imported from that file.
205 */
206 if (dpx_conf.verbose_level > 1 &&
207 dpx_conf.file.keep_cache != 1)
208 MESG("pdf_image>> deleting temporary file \"%s\"\n", I->filename);
209 dpx_delete_temp_file(I->fullname, false); /* temporary filename freed here */
210 I->fullname = NULL;
211 }
212 pdf_clean_ximage_struct(I);
213 }
214 RELEASE(ic->ximages);
215 ic->ximages = NULL;
216 ic->count = ic->capacity = 0;
217 }
218
219 if (_opts.cmdtmpl)
220 RELEASE(_opts.cmdtmpl);
221 _opts.cmdtmpl = NULL;
222 }
223
224 static int
225 source_image_type (FILE *fp)
226 {
227 int format = IMAGE_TYPE_UNKNOWN;
228
229 rewind(fp);
230 /*
231 * Make sure we check for PS *after* checking for MP since
232 * MP is a special case of PS.
233 */
234 if (check_for_jpeg(fp))
235 {
236 format = IMAGE_TYPE_JPEG;
237 }
238 else if (check_for_jp2(fp))
239 {
240 format = IMAGE_TYPE_JP2;
241 }
242 #ifdef HAVE_LIBPNG
243 else if (check_for_png(fp))
244 {
245 format = IMAGE_TYPE_PNG;
246 }
247 #endif
248 else if (check_for_bmp(fp))
249 {
250 format = IMAGE_TYPE_BMP;
251 } else if (check_for_pdf(fp)) {
252 format = IMAGE_TYPE_PDF;
253 } else if (check_for_mp(fp)) {
254 format = IMAGE_TYPE_MPS;
255 } else if (check_for_ps(fp)) {
256 format = IMAGE_TYPE_EPS;
257 } else {
258 format = IMAGE_TYPE_UNKNOWN;
259 }
260 rewind(fp);
261
262 return format;
263 }
264
265 static int
266 load_image (const char *ident, const char *filename, const char *fullname, int format, FILE *fp,
267 load_options options)
268 {
269 struct ic_ *ic = &_ic;
270 int id = -1, reserved = 0;
271 pdf_ximage *I;
272
273 if (ident) {
274 for (id = 0; id < ic->count; id++) {
275 I = &ic->ximages[id];
276 if (I->ident && !strcmp(ident, I->ident)) {
277 if (I->reserved) {
278 reserved = 1;
279 break;
280 }
281 }
282 }
283 }
284 if (!reserved) {
285 id = ic->count;
286 if (ic->count >= ic->capacity) {
287 ic->capacity += 16;
288 ic->ximages = RENEW(ic->ximages, ic->capacity, pdf_ximage);
289 }
290 I = &ic->ximages[id];
291 pdf_init_ximage_struct(I);
292 if (ident) {
293 I->ident = NEW(strlen(ident)+1, char);
294 strcpy(I->ident, ident);
295 }
296 ic->count++;
297 }
298 if (filename) {
299 I->filename = NEW(strlen(filename)+1, char);
300 strcpy(I->filename, filename);
301 }
302 if (fullname) {
303 I->fullname = NEW(strlen(fullname)+1, char);
304 strcpy(I->fullname, fullname);
305 }
306
307 I->attr.page_no = options.page_no;
308 I->attr.page_name = options.page_name;
309 I->attr.bbox_type = options.bbox_type;
310 I->attr.dict = options.dict; /* unsafe? */
311
312 switch (format) {
313 case IMAGE_TYPE_JPEG:
314 if (dpx_conf.verbose_level > 0)
315 MESG("[JPEG]");
316 if (jpeg_include_image(I, fp) < 0)
317 goto error;
318 I->subtype = PDF_XOBJECT_TYPE_IMAGE;
319 break;
320 case IMAGE_TYPE_JP2:
321 if (dpx_conf.verbose_level > 0)
322 MESG("[JP2]");
323 if (jp2_include_image(I, fp) < 0)
324 goto error;
325 I->subtype = PDF_XOBJECT_TYPE_IMAGE;
326 break;
327 #ifdef HAVE_LIBPNG
328 case IMAGE_TYPE_PNG:
329 if (dpx_conf.verbose_level > 0)
330 MESG("[PNG]");
331 if (png_include_image(I, fp) < 0)
332 goto error;
333 I->subtype = PDF_XOBJECT_TYPE_IMAGE;
334 break;
335 #endif
336 case IMAGE_TYPE_BMP:
337 if (dpx_conf.verbose_level > 0)
338 MESG("[BMP]");
339 if (bmp_include_image(I, fp) < 0)
340 goto error;
341 I->subtype = PDF_XOBJECT_TYPE_IMAGE;
342 break;
343 case IMAGE_TYPE_PDF:
344 if (dpx_conf.verbose_level > 0)
345 MESG("[PDF]");
346 {
347 int result = pdf_include_page(I, fp, fullname, options);
348 if (result > 0)
349 /* PDF version too recent */
350 result = ps_include_page(I, fullname, options);
351 if (result < 0)
352 goto error;
353 }
354 if (dpx_conf.verbose_level > 0)
355 MESG(",Page:%ld", I->attr.page_no);
356 I->subtype = PDF_XOBJECT_TYPE_FORM;
357 break;
358 /*
359 * case IMAGE_TYPE_EPS:
360 */
361 default:
362 if (dpx_conf.verbose_level > 0)
363 MESG(format == IMAGE_TYPE_EPS ? "[PS]" : "[UNKNOWN]");
364 if (ps_include_page(I, fullname, options) < 0)
365 goto error;
366 if (dpx_conf.verbose_level > 0)
367 MESG(",Page:%ld", I->attr.page_no);
368 I->subtype = PDF_XOBJECT_TYPE_FORM;
369 }
370
371 switch (I->subtype) {
372 case PDF_XOBJECT_TYPE_IMAGE:
373 sprintf(I->res_name, "Im%d", id);
374 break;
375 case PDF_XOBJECT_TYPE_FORM:
376 sprintf(I->res_name, "Fm%d", id);
377 break;
378 default:
379 ERROR("Unknown XObject subtype: %d", I->subtype);
380 goto error;
381 }
382
383 return id;
384
385 error:
386 pdf_clean_ximage_struct(I);
387 return -1;
388 }
389
390
391 #define dpx_find_file(n,d,s) (kpse_find_pict((n)))
392 #define dpx_fopen(n,m) (MFOPEN((n),(m)))
393 #define dpx_fclose(f) (MFCLOSE((f)))
394
395 #if defined(WIN32)
396 int utf8name_failed = 0;
397 #endif /* WIN32 */
398
399 #define dpx_streq(a, b) ((a) == (b) || (a) && (b) && strcmp(a, b) == 0)
400
401 int
402 pdf_ximage_load_image (const char *ident, const char *filename, load_options options)
403 {
404 struct ic_ *ic = &_ic;
405 int i, id = -1;
406 pdf_ximage *I;
407 char *fullname, *f = NULL;
408 int format;
409 FILE *fp;
410
411 for (i = 0; i < ic->count; i++) {
412 I = &ic->ximages[i];
413 if (I->filename == NULL || strcmp(filename, I->filename) != 0)
414 continue;
415 id = i;
416 f = I->fullname;
417
418 if (I->attr.page_no != options.page_no)
419 continue;
420 if (!dpx_streq(I->attr.page_name, options.page_name))
421 continue;
422 if (pdf_compare_object(I->attr.dict, options.dict) != 0) /* ????? */
423 continue;
424 if (I->attr.bbox_type != options.bbox_type)
425 continue;
426
427 return id;
428 }
429 if (f) {
430 /* we already have converted this file; f is the temporary file name */
431 fullname = NEW(strlen(f)+1, char);
432 strcpy(fullname, f);
433 } else {
434 /* try loading image */
435 #if defined(WIN32)
436 utf8name_failed = 0;
437 #endif /* WIN32 */
438 fullname = dpx_find_file(filename, "_pic_", "");
439 #if defined(WIN32)
440 if (!fullname && file_system_codepage != win32_codepage) {
441 int tmpcp = file_system_codepage;
442 utf8name_failed = 1;
443 file_system_codepage = win32_codepage;
444 fullname = dpx_find_file(filename, "_pic_", "");
445 file_system_codepage = tmpcp;
446 }
447 #endif /* WIN32 */
448 if (!fullname) {
449 #if defined(WIN32)
450 utf8name_failed = 0;
451 #endif /* WIN32 */
452 if (dpx_conf.compat_mode == dpx_mode_compat_mode) {
453 WARN("Image inclusion failed. Could not find file: %s", filename);
454 } else {
455 ERROR("Image inclusion failed. Could not find file: %s", filename);
456 }
457 return -1;
458 }
459 }
460
461 fp = dpx_fopen(fullname, FOPEN_RBIN_MODE);
462 if (!fp) {
463 if (dpx_conf.compat_mode == dpx_mode_compat_mode) {
464 WARN("Error opening image file \"%s\"", fullname);
465 RELEASE(fullname);
466 } else {
467 RELEASE(fullname);
468 ERROR("Error opening image file \"%s\".", filename);
469 }
470 return -1;
471 }
472 if (dpx_conf.verbose_level > 0) {
473 MESG("(Image:%s", filename);
474 if (dpx_conf.verbose_level > 1)
475 MESG("[%s]", fullname);
476 }
477
478 format = source_image_type(fp);
479 switch (format) {
480 case IMAGE_TYPE_MPS:
481 if (dpx_conf.verbose_level > 0)
482 MESG("[MPS]");
483 id = mps_include_page(filename, fp);
484 if (id < 0) {
485 WARN("Try again with the distiller.");
486 format = IMAGE_TYPE_EPS;
487 rewind(fp);
488 } else {
489 /* Workaround for the problem reported.
490 * mps_include_page() above doesn't set I->filename...
491 */
492 I = &ic->ximages[id];
493 if (!I->filename) {
494 I->filename = NEW(strlen(filename)+1, char);
495 strcpy(I->filename, filename);
496 }
497 break;
498 }
499 default:
500 id = load_image(ident, filename, fullname, format, fp, options);
501 break;
502 }
503 dpx_fclose(fp);
504
505 RELEASE(fullname);
506
507 if (dpx_conf.verbose_level > 0)
508 MESG(")");
509
510 if (id < 0) {
511 if (dpx_conf.compat_mode == dpx_mode_compat_mode) {
512 if (format == IMAGE_TYPE_PDF || format == IMAGE_TYPE_EPS) {
513 WARN("Image inclusion failed for \"%s\" (page=%d).", filename, options.page_no);
514 } else {
515 WARN("Image inclusion failed for \"%s\"", filename);
516 }
517 } else {
518 if (format == IMAGE_TYPE_PDF || format == IMAGE_TYPE_EPS) {
519 ERROR("Image inclusion failed for \"%s\" (page=%d).", filename, options.page_no);
520 } else {
521 ERROR("Image inclusion failed for \"%s\"", filename);
522 }
523 }
524 }
525
526 return id;
527 }
528
529 int
530 pdf_ximage_findresource (const char *ident)
531 {
532 struct ic_ *ic = &_ic;
533 int id = -1;
534 pdf_ximage *I;
535
536 for (id = 0; id < ic->count; id++) {
537 I = &ic->ximages[id];
538 if (I->ident && !strcmp(ident, I->ident)) {
539 return id;
540 }
541 }
542
543 return -1;
544 }
545
546 /* Reference: PDF Reference 1.5 v6, pp.321--322
547 *
548 * TABLE 4.42 Additional entries specific to a type 1 form dictionary
549 *
550 * BBox rectangle (Required) An array of four numbers in the form coordinate
551 * system, giving the coordinates of the left, bottom, right,
552 * and top edges, respectively, of the form XObject's bounding
553 * box. These boundaries are used to clip the form XObject and
554 * to determine its size for caching.
555 *
556 * Matrix array (Optional) An array of six numbers specifying the form
557 * matrix, which maps form space into user space.
558 * Default value: the identity matrix [1 0 0 1 0 0].
559 */
560 void
561 pdf_ximage_init_form_info (xform_info *info)
562 {
563 info->flags = 0;
564 info->bbox.llx = 0;
565 info->bbox.lly = 0;
566 info->bbox.urx = 0;
567 info->bbox.ury = 0;
568 info->matrix.a = 1.0;
569 info->matrix.b = 0.0;
570 info->matrix.c = 0.0;
571 info->matrix.d = 1.0;
572 info->matrix.e = 0.0;
573 info->matrix.f = 0.0;
574 }
575
576 /* Reference: PDF Reference 1.5 v6, pp.303--306
577 *
578 * TABLE 4.42 Additional entries specific to an image dictionary
579 *
580 * Width integer (Required) The width of the image, in samples.
581 *
582 * Height integer (Required) The height of the image, in samples.
583 *
584 * ColorSpace name or array
585 * (Required for images, except those that use the JPXDecode
586 * filter; not allowed for image masks) The color space in
587 * which image samples are specified. This may be any type
588 * of color space except Patter.
589 *
590 * If the image uses the JPXDecode filter, this entry is
591 * optional.
592 *
593 * BitsPerComponent integer
594 * (Required except for image masks and images that use the
595 * JPXDecode filter) The number of bits used to represent
596 * each color component. Only a single value may be specified;
597 * the number of bits is the same for all color components.
598 * Valid values are 1,2,4,8, and (in PDF1.5) 16. If ImageMask
599 * is true, this entry is optional, and if speficified, its
600 * value must be 1.
601 *
602 * If the image stream uses the JPXDecode filter, this entry
603 * is optional and ignored if present. The bit depth is
604 * determined in the process of decoding the JPEG2000 image.
605 */
606 void
607 pdf_ximage_init_image_info (ximage_info *info)
608 {
609 info->flags = 0;
610 info->width = 0;
611 info->height = 0;
612 info->bits_per_component = 0;
613 info->num_components = 0;
614 info->min_dpi = 0;
615 info->xdensity = info->ydensity = 1.0;
616 }
617
618 void
619 pdf_ximage_set_image (pdf_ximage *I, void *image_info, pdf_obj *resource)
620 {
621 pdf_obj *dict;
622 ximage_info *info = image_info;
623
624 if (!PDF_OBJ_STREAMTYPE(resource))
625 ERROR("Image XObject must be of stream type.");
626
627 I->subtype = PDF_XOBJECT_TYPE_IMAGE;
628
629 I->attr.width = info->width; /* The width of the image, in samples */
630 I->attr.height = info->height; /* The height of the image, in samples */
631 I->attr.xdensity = info->xdensity;
632 I->attr.ydensity = info->ydensity;
633
634 dict = pdf_stream_dict(resource);
635 pdf_add_dict(dict, pdf_new_name("Type"), pdf_new_name("XObject"));
636 pdf_add_dict(dict, pdf_new_name("Subtype"), pdf_new_name("Image"));
637 pdf_add_dict(dict, pdf_new_name("Width"), pdf_new_number(info->width));
638 pdf_add_dict(dict, pdf_new_name("Height"), pdf_new_number(info->height));
639 if (info->bits_per_component > 0) /* Ignored for JPXDecode filter. FIXME */
640 pdf_add_dict(dict, pdf_new_name("BitsPerComponent"),
641 pdf_new_number(info->bits_per_component));
642 if (I->attr.dict)
643 pdf_merge_dict(dict, I->attr.dict);
644
645 if (I->ident) {
646 int error;
647
648 error = pdf_names_add_object(global_names, I->ident, strlen(I->ident), pdf_link_obj(resource));
649 if (I->reference)
650 pdf_release_obj(I->reference);
651 if (error) {
652 I->reference = pdf_ref_obj(resource);
653 } else {
654 /* Need to create object reference before closing it */
655 I->reference = pdf_names_lookup_reference(global_names, I->ident, strlen(I->ident));
656 pdf_names_close_object(global_names, I->ident, strlen(I->ident));
657 }
658 I->reserved = 0;
659 } else {
660 I->reference = pdf_ref_obj(resource);
661 }
662 pdf_release_obj(resource); /* Caller don't know we are using reference. */
663 I->resource = NULL;
664 }
665
666 void
667 pdf_ximage_set_form (pdf_ximage *I, void *form_info, pdf_obj *resource)
668 {
669 xform_info *info = form_info;
670 pdf_coord p1, p2, p3, p4;
671
672 I->subtype = PDF_XOBJECT_TYPE_FORM;
673
674 /* Image's attribute "bbox" here is affected by /Rotate entry of included
675 * PDF page.
676 */
677 p1.x = info->bbox.llx; p1.y = info->bbox.lly;
678 pdf_dev_transform(&p1, &info->matrix);
679 p2.x = info->bbox.urx; p1.y = info->bbox.lly;
680 pdf_dev_transform(&p2, &info->matrix);
681 p3.x = info->bbox.urx; p3.y = info->bbox.ury;
682 pdf_dev_transform(&p3, &info->matrix);
683 p4.x = info->bbox.llx; p4.y = info->bbox.ury;
684 pdf_dev_transform(&p4, &info->matrix);
685
686 I->attr.bbox.llx = min4(p1.x, p2.x, p3.x, p4.x);
687 I->attr.bbox.lly = min4(p1.y, p2.y, p3.y, p4.y);
688 I->attr.bbox.urx = max4(p1.x, p2.x, p3.x, p4.x);
689 I->attr.bbox.ury = max4(p1.y, p2.y, p3.y, p4.y);
690
691 if (I->ident) {
692 int error;
693
694 error = pdf_names_add_object(global_names, I->ident, strlen(I->ident), pdf_link_obj(resource));
695 if (I->reference)
696 pdf_release_obj(I->reference);
697 if (error) {
698 I->reference = pdf_ref_obj(resource);
699 } else {
700 /* Need to create object reference before closing it */
701 I->reference = pdf_names_lookup_reference(global_names, I->ident, strlen(I->ident));
702 pdf_names_close_object(global_names, I->ident, strlen(I->ident));
703 }
704 I->reserved = 0;
705 } else {
706 I->reference = pdf_ref_obj(resource);
707 }
708 pdf_release_obj(resource); /* Caller don't know we are using reference. */
709 I->resource = NULL;
710 }
711
712 int
713 pdf_ximage_get_page (pdf_ximage *I)
714 {
715 return I->attr.page_no;
716 }
717
718 #define CHECK_ID(c,n) do {\
719 if ((n) < 0 || (n) >= (c)->count) {\
720 ERROR("Invalid XObject ID: %d", (n));\
721 }\
722 } while (0)
723 #define GET_IMAGE(c,n) (&((c)->ximages[(n)]))
724
725 pdf_obj *
726 pdf_ximage_get_reference (int id)
727 {
728 struct ic_ *ic = &_ic;
729 pdf_ximage *I;
730
731 CHECK_ID(ic, id);
732
733 I = GET_IMAGE(ic, id);
734 if (!I->reference && I->resource)
735 I->reference = pdf_ref_obj(I->resource);
736
737 return pdf_link_obj(I->reference);
738 }
739
740 /* called from pdfdoc.c only for late binding */
741 int
742 pdf_ximage_defineresource (const char *ident,
743 int subtype, void *info, pdf_obj *resource)
744 {
745 struct ic_ *ic = &_ic;
746 int id, reserved = 0;
747 pdf_ximage *I;
748
749 if (ident) {
750 for (id = 0; id < ic->count; id++) {
751 I = &ic->ximages[id];
752 if (I->ident && !strcmp(ident, I->ident) && I->reserved) {
753 reserved = 1;
754 break;
755 }
756 }
757 }
758
759 if (!reserved) {
760 id = ic->count;
761 if (ic->count >= ic->capacity) {
762 ic->capacity += 16;
763 ic->ximages = RENEW(ic->ximages, ic->capacity, pdf_ximage);
764 }
765 I = &ic->ximages[id];
766 pdf_init_ximage_struct(I);
767
768 if (ident) {
769 I->ident = NEW(strlen(ident)+1, char);
770 strcpy(I->ident, ident);
771 }
772 ic->count++;
773 }
774
775 switch (subtype) {
776 case PDF_XOBJECT_TYPE_IMAGE:
777 pdf_ximage_set_image(I, info, resource);
778 sprintf(I->res_name, "Im%d", id);
779 break;
780 case PDF_XOBJECT_TYPE_FORM:
781 pdf_ximage_set_form (I, info, resource);
782 sprintf(I->res_name, "Fm%d", id);
783 break;
784 default:
785 ERROR("Unknown XObject subtype: %d", subtype);
786 }
787
788 return id;
789 }
790
791 int
792 pdf_ximage_reserve (const char *ident)
793 {
794 struct ic_ *ic = &_ic;
795 int id;
796 pdf_ximage *I;
797
798 for (id = 0; id < ic->count; id++) {
799 I = &ic->ximages[id];
800 if (I->ident && !strcmp(ident, I->ident)) {
801 WARN("XObject ID \"%s\" already used!", ident);
802 return -1;
803 }
804 }
805
806 id = ic->count;
807 if (ic->count >= ic->capacity) {
808 ic->capacity += 16;
809 ic->ximages = RENEW(ic->ximages, ic->capacity, pdf_ximage);
810 }
811
812 I = &ic->ximages[id];
813
814 pdf_init_ximage_struct(I);
815
816 if (ident) {
817 I->ident = NEW(strlen(ident)+1, char);
818 strcpy(I->ident, ident);
819 }
820 I->reference = pdf_names_reserve(global_names, ident, strlen(ident));
821 sprintf(I->res_name, "Fm%d", id);
822 I->reserved = 1;
823 ic->count++;
824
825 return id;
826 }
827
828 char *
829 pdf_ximage_get_resname (int id)
830 {
831 struct ic_ *ic = &_ic;
832 pdf_ximage *I;
833
834 CHECK_ID(ic, id);
835
836 I = GET_IMAGE(ic, id);
837
838 return I->res_name;
839 }
840
841 int
842 pdf_ximage_get_subtype (int id)
843 {
844 struct ic_ *ic = &_ic;
845 pdf_ximage *I;
846
847 CHECK_ID(ic, id);
848
849 I = GET_IMAGE(ic, id);
850
851 return I->subtype;
852 }
853
854 void
855 pdf_ximage_set_attr (int id, int width, int height, double xdensity, double ydensity, double llx, double lly, double urx, double ury)
856 {
857 struct ic_ *ic = &_ic;
858 pdf_ximage *I;
859
860 CHECK_ID(ic, id);
861
862 I = GET_IMAGE(ic, id);
863 I->attr.width = width;
864 I->attr.height = height;
865 I->attr.xdensity = xdensity;
866 I->attr.ydensity = ydensity;
867 I->attr.bbox.llx = llx;
868 I->attr.bbox.lly = lly;
869 I->attr.bbox.urx = urx;
870 I->attr.bbox.ury = ury;
871 }
872
873 /* depth...
874 * Dvipdfm treat "depth" as "yoffset" for pdf:image and pdf:uxobj
875 * not as vertical dimension of scaled image. (And there are bugs.)
876 * This part contains incompatibile behaviour than dvipdfm!
877 */
878 #define EBB_DPI 72
879
880 static void
881 scale_to_fit_I (pdf_tmatrix *T,
882 transform_info *p,
883 pdf_ximage *I)
884 {
885 double s_x, s_y, d_x, d_y;
886 double wd0, ht0, dp, xscale, yscale;
887
888 if (p->flags & INFO_HAS_USER_BBOX) {
889 wd0 = p->bbox.urx - p->bbox.llx;
890 ht0 = p->bbox.ury - p->bbox.lly;
891 xscale = I->attr.width * I->attr.xdensity / wd0;
892 yscale = I->attr.height * I->attr.ydensity / ht0;
893 d_x = -p->bbox.llx / wd0;
894 d_y = -p->bbox.lly / ht0;
895 } else {
896 wd0 = I->attr.width * I->attr.xdensity;
897 ht0 = I->attr.height * I->attr.ydensity;
898 xscale = yscale = 1.0;
899 d_x = 0.0;
900 d_y = 0.0;
901 }
902
903 if (wd0 == 0.0) {
904 WARN("Image width=0.0!");
905 wd0 = 1.0;
906 }
907 if (ht0 == 0.0) {
908 WARN("Image height=0.0!");
909 ht0 = 1.0;
910 }
911
912 if ( (p->flags & INFO_HAS_WIDTH ) &&
913 (p->flags & INFO_HAS_HEIGHT) ) {
914 s_x = p->width * xscale;
915 s_y = (p->height + p->depth) * yscale;
916 dp = p->depth * yscale;
917 } else if ( p->flags & INFO_HAS_WIDTH ) {
918 s_x = p->width * xscale;
919 s_y = s_x * ((double)I->attr.height / I->attr.width);
920 dp = 0.0;
921 } else if ( p->flags & INFO_HAS_HEIGHT) {
922 s_y = (p->height + p->depth) * yscale;
923 s_x = s_y * ((double)I->attr.width / I->attr.height);
924 dp = p->depth * yscale;
925 } else {
926 s_x = wd0;
927 s_y = ht0;
928 dp = 0.0;
929 }
930 T->a = s_x; T->c = 0.0;
931 T->b = 0.0; T->d = s_y;
932 T->e = d_x * s_x / xscale; T->f = d_y * s_y / yscale - dp;
933
934 return;
935 }
936
937
938 static void
939 scale_to_fit_F (pdf_tmatrix *T,
940 transform_info *p,
941 pdf_ximage *I)
942 {
943 double s_x, s_y, d_x, d_y;
944 double wd0, ht0, dp;
945
946 if (p->flags & INFO_HAS_USER_BBOX) {
947 wd0 = p->bbox.urx - p->bbox.llx;
948 ht0 = p->bbox.ury - p->bbox.lly;
949 d_x = -p->bbox.llx;
950 d_y = -p->bbox.lly;
951 } else {
952 wd0 = I->attr.bbox.urx - I->attr.bbox.llx;
953 ht0 = I->attr.bbox.ury - I->attr.bbox.lly;
954 d_x = 0.0;
955 d_y = 0.0;
956 }
957
958 if (wd0 == 0.0) {
959 WARN("Image width=0.0!");
960 wd0 = 1.0;
961 }
962 if (ht0 == 0.0) {
963 WARN("Image height=0.0!");
964 ht0 = 1.0;
965 }
966
967 if ( (p->flags & INFO_HAS_WIDTH ) &&
968 (p->flags & INFO_HAS_HEIGHT) ) {
969 s_x = p->width / wd0;
970 s_y = (p->height + p->depth) / ht0;
971 dp = p->depth;
972 } else if ( p->flags & INFO_HAS_WIDTH ) {
973 s_x = p->width / wd0;
974 s_y = s_x;
975 dp = 0.0;
976 } else if ( p->flags & INFO_HAS_HEIGHT) {
977 s_y = (p->height + p->depth) / ht0;
978 s_x = s_y;
979 dp = p->depth;
980 } else {
981 s_x = s_y = 1.0;
982 dp = 0.0;
983 }
984
985 T->a = s_x; T->c = 0.0;
986 T->b = 0.0; T->d = s_y;
987 T->e = s_x * d_x; T->f = s_y * d_y - dp;
988
989 return;
990 }
991
992
993 /* called from pdfdev.c and spc_html.c */
994 int
995 pdf_ximage_scale_image (int id,
996 pdf_tmatrix *M, /* return value for trans matrix */
997 pdf_rect *r, /* return value for clipping */
998 transform_info *p /* argument from specials */
999 )
1000 {
1001 struct ic_ *ic = &_ic;
1002 pdf_ximage *I;
1003
1004 CHECK_ID(ic, id);
1005
1006 I = GET_IMAGE(ic, id);
1007
1008 pdf_setmatrix(M, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1009
1010 switch (I->subtype) {
1011 /* Reference: PDF Reference 1.5 v6, p.302
1012 *
1013 * An image can be placed on the output page in any desired position,
1014 * orientation, and size by using the cm operator to modify the current
1015 * transformation matrix (CTM) so as to map the unit square of user space
1016 * to the rectangle or parallelogram in which the image is to be painted.
1017 *
1018 * There is neither BBox nor Matrix key in the image XObject.
1019 * Everything must be controlled by the cm operator.
1020 *
1021 * The argument [p] contains the user-defined bounding box, the scailing
1022 * factor of which is bp as EPS and PDF. On the other hand, I->attr
1023 * contains the (sampling) width and the (sampling) height of the image.
1024 *
1025 * There is no problem if a bitmap image has density information.
1026 * Otherwise, DVIPDFM's ebb generates bounding box as 100px = 72bp = 1in.
1027 * In this case, screen captured images look bad. Moreover, DVIPDFM's ebb
1028 * ignores all density information and use just 100px = 72bp = 1in.
1029 *
1030 * On the other hand, pdfTeX uses 100px = 100bp to get a better quality
1031 * for screen captured images.
1032 *
1033 * DVIPDFMx's xbb generates bounding box as 100px = 100bp in the same
1034 * way as pdfTeX. Furthermore, it takes care of density information too.
1035 */
1036 case PDF_XOBJECT_TYPE_IMAGE:
1037 scale_to_fit_I(M, p, I);
1038 if (p->flags & INFO_HAS_USER_BBOX) {
1039 r->llx = p->bbox.llx / (I->attr.width * I->attr.xdensity);
1040 r->lly = p->bbox.lly / (I->attr.height * I->attr.ydensity);
1041 r->urx = p->bbox.urx / (I->attr.width * I->attr.xdensity);
1042 r->ury = p->bbox.ury / (I->attr.height * I->attr.ydensity);
1043 } else {
1044 r->llx = 0.0;
1045 r->lly = 0.0;
1046 r->urx = 1.0;
1047 r->ury = 1.0;
1048 }
1049 break;
1050 /* User-defined transformation and clipping are controlled by
1051 * the cm operator and W operator, explicitly */
1052 case PDF_XOBJECT_TYPE_FORM:
1053 scale_to_fit_F(M, p, I);
1054 if (p->flags & INFO_HAS_USER_BBOX) {
1055 r->llx = p->bbox.llx;
1056 r->lly = p->bbox.lly;
1057 r->urx = p->bbox.urx;
1058 r->ury = p->bbox.ury;
1059 } else { /* I->attr.bbox from the image bounding box */
1060 r->llx = I->attr.bbox.llx;
1061 r->lly = I->attr.bbox.lly;
1062 r->urx = I->attr.bbox.urx;
1063 r->ury = I->attr.bbox.ury;
1064 }
1065 break;
1066 default: /* maybe reserved */
1067 if (p->flags & INFO_HAS_USER_BBOX) {
1068 r->llx = p->bbox.llx;
1069 r->lly = p->bbox.lly;
1070 r->urx = p->bbox.urx;
1071 r->ury = p->bbox.ury;
1072 } else { /* I->attr.bbox from the image bounding box */
1073 r->llx = 0.0;
1074 r->lly = 0.0;
1075 r->urx = 1.0;
1076 r->ury = 1.0;
1077 }
1078 }
1079
1080 return 0;
1081 }
1082
1083
1084 /* Migrated from psimage.c */
1085
1086 void set_distiller_template (char *s)
1087 {
1088 if (_opts.cmdtmpl)
1089 RELEASE(_opts.cmdtmpl);
1090 if (!s || *s == '\0')
1091 _opts.cmdtmpl = NULL;
1092 else {
1093 _opts.cmdtmpl = NEW(strlen(s) + 1, char);
1094 strcpy(_opts.cmdtmpl, s);
1095 }
1096 return;
1097 }
1098
1099 char *get_distiller_template (void)
1100 {
1101 return _opts.cmdtmpl;
1102 }
1103
1104 static int
1105 ps_include_page (pdf_ximage *ximage, const char *filename, load_options options)
1106 {
1107 char *distiller_template = _opts.cmdtmpl;
1108 char *temp;
1109 FILE *fp;
1110 int error = 0;
1111 struct stat stat_o, stat_t;
1112 #if defined(_WIN32)
1113 char *utf8temp;
1114 wchar_t *wtemp;
1115 char *ftest = NULL;
1116 #endif
1117 if (!distiller_template) {
1118 WARN("No image converter available for converting file \"%s\" to PDF format.", filename);
1119 WARN(">> Please check if you have 'D' option in config file.");
1120 return -1;
1121 }
1122
1123 temp = dpx_create_fix_temp_file(filename);
1124 if (!temp) {
1125 WARN("Failed to create temporary file for image conversion: %s", filename);
1126 return -1;
1127 }
1128
1129 #ifdef MIKTEX
1130 {
1131 char *p;
1132 for (p = (char *)filename; *p; p++) {
1133 if (*p == '\\') *p = '/';
1134 }
1135 for (p = (char *)temp; *p; p++) {
1136 if (*p == '\\') *p = '/';
1137 }
1138 }
1139 #endif
1140
1141 #if defined(_WIN32)
1142 /* temp is always win32_codepage only. So fsyscp_stat() is not
1143 * necessary for temp. However, filename can be non-ASCII UTF-8.
1144 */
1145 if (dpx_conf.file.keep_cache != -1 &&
1146 stat(temp, &stat_t)==0 &&
1147 (fsyscp_stat(filename, &stat_o)==0 ||
1148 stat(filename, &stat_o)==0) &&
1149 stat_t.st_mtime > stat_o.st_mtime) {
1150 #else
1151 if (dpx_conf.file.keep_cache != -1 &&
1152 stat(temp, &stat_t)==0 && stat(filename, &stat_o)==0 &&
1153 stat_t.st_mtime > stat_o.st_mtime) {
1154 #endif /* _WIN32 */
1155 /* cache exist */
1156 /*printf("\nLast file modification: %s", ctime(&stat_o.st_mtime));
1157 printf("Last file modification: %s", ctime(&stat_t.st_mtime));*/
1158 ;
1159 } else {
1160 if (dpx_conf.verbose_level > 1) {
1161 MESG("\n");
1162 MESG("pdf_image>> Converting file \"%s\" --> \"%s\" via:\n", filename, temp);
1163 MESG("pdf_image>> %s\n", distiller_template);
1164 MESG("pdf_image>> ...");
1165 }
1166
1167 /* Support non-ascii TEMP and TMP on Windows to call Ghostscript */
1168 #if defined(_WIN32)
1169 ftest = dpx_find_file(filename, "_pic_", "");
1170 if (ftest) {
1171 wtemp = get_wstring_from_mbstring(win32_codepage,
1172 temp, wtemp = NULL);
1173 utf8temp = get_mbstring_from_wstring(file_system_codepage,
1174 wtemp, utf8temp = NULL);
1175 error = dpx_file_apply_filter(distiller_template, filename,
1176 utf8temp, pdf_get_version());
1177 } else {
1178 utf8name_failed = 1;
1179 error = dpx_file_apply_filter(distiller_template, filename,
1180 temp, pdf_get_version());
1181 utf8name_failed = 0;
1182 }
1183 #else
1184 error = dpx_file_apply_filter(distiller_template, filename, temp,
1185 pdf_get_version());
1186 #endif
1187 if (error) {
1188 WARN("Image format conversion for \"%s\" failed...", filename);
1189 dpx_delete_temp_file(temp, true);
1190 return error;
1191 }
1192 }
1193 #if defined(_WIN32)
1194 /* Use a simple fopen() since temp is in win32_codepage */
1195 fp = fopen(temp, FOPEN_RBIN_MODE);
1196 #else
1197 fp = MFOPEN(temp, FOPEN_RBIN_MODE);
1198 #endif
1199 if (!fp) {
1200 WARN("Could not open conversion result \"%s\" for image \"%s\". Why?", temp, filename);
1201 dpx_delete_temp_file(temp, true);
1202 return -1;
1203 }
1204 pdf_set_ximage_tempfile(ximage, temp);
1205 error = pdf_include_page(ximage, fp, temp, options);
1206 MFCLOSE(fp);
1207
1208 /* See pdf_close_images for why we cannot delete temporary files here. */
1209 #if defined(_WIN32)
1210 if (ftest) {
1211 RELEASE(utf8temp);
1212 RELEASE(wtemp);
1213 RELEASE(ftest);
1214 }
1215 #endif
1216 RELEASE(temp);
1217
1218 if (error) {
1219 WARN("Failed to include image file \"%s\"", filename);
1220 WARN(">> Please check if");
1221 WARN(">> %s", distiller_template);
1222 WARN(">> %%o = output filename, %%i = input filename, %%b = input filename without suffix");
1223 WARN(">> can really convert \"%s\" to PDF format image.", filename);
1224 }
1225
1226 return error;
1227 }
1228
1229 static int check_for_ps (FILE *image_file)
1230 {
1231 rewind (image_file);
1232 mfgets (work_buffer, WORK_BUFFER_SIZE, image_file);
1233 if (!strncmp (work_buffer, "%!", 2))
1234 return 1;
1235 return 0;
1236 }
1237
1238 static int check_for_mp (FILE *image_file)
1239 {
1240 int try_count = 10;
1241
1242 rewind (image_file);
1243 mfgets(work_buffer, WORK_BUFFER_SIZE, image_file);
1244 if (strncmp(work_buffer, "%!PS", 4))
1245 return 0;
1246
1247 while (try_count > 0) {
1248 mfgets(work_buffer, WORK_BUFFER_SIZE, image_file);
1249 if (!strncmp(work_buffer, "%%Creator:", 10)) {
1250 if (strlen(work_buffer+10) >= 8 &&
1251 strstr(work_buffer+10, "MetaPost"))
1252 break;
1253 }
1254 try_count--;
1255 }
1256
1257 return ((try_count > 0) ? 1 : 0);
1258 }
1259
1260 /* ERROR() can't be used here otherwise the cleanup routine is recursively called. */
1261 #undef ERROR
1262 void
1263 pdf_error_cleanup_cache (void)
1264 {
1265 struct ic_ *ic = &_ic;
1266 int i;
1267 pdf_ximage *I;
1268
1269 for (i = 0; i < ic->count; i++) {
1270 I = &ic->ximages[i];
1271 if (I->attr.tempfile) {
1272 dpx_delete_temp_file(I->fullname, false); /* temporary filename freed here */
1273 }
1274 }
1275 }