215
115
#define HEADER_JBIG2 "\x97\x4A\x42\x32\x0D\x0A\x1A\x0A"
216
116
#define HEADER_PDF "%PDF-1."
217
117
#define MAX_HEADER (sizeof(HEADER_PNG)-1)
218
static void checktypebyheader (integer img)
119
static void check_type_by_header(image_dict * idict)
221
122
FILE *file = NULL;
222
123
char header[MAX_HEADER];
224
if (img_type (img) != IMAGE_TYPE_NONE) /* nothing to do */
125
assert(idict != NULL);
126
if (img_type(idict) != IMAGE_TYPE_NONE) /* nothing to do */
227
128
/* read the header */
228
file = xfopen (img_name (img), FOPEN_RBIN_MODE);
129
file = xfopen(img_filepath(idict), FOPEN_RBIN_MODE);
229
130
for (i = 0; (unsigned) i < MAX_HEADER; i++) {
230
header[i] = xgetc (file);
232
pdftex_fail ("reading image file failed");
131
header[i] = xgetc(file);
133
pdftex_fail("reading image file failed");
234
xfclose (file, img_name (img));
135
xfclose(file, img_filepath(idict));
237
if (strncmp (header, HEADER_JPG, sizeof (HEADER_JPG) - 1) == 0)
238
img_type (img) = IMAGE_TYPE_JPG;
239
else if (strncmp (header, HEADER_PNG, sizeof (HEADER_PNG) - 1) == 0)
240
img_type (img) = IMAGE_TYPE_PNG;
241
else if (strncmp (header, HEADER_JBIG2, sizeof (HEADER_JBIG2) - 1) == 0)
242
img_type (img) = IMAGE_TYPE_JBIG2;
243
else if (strncmp (header, HEADER_PDF, sizeof (HEADER_PDF) - 1) == 0)
244
img_type (img) = IMAGE_TYPE_PDF;
137
if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0)
138
img_type(idict) = IMAGE_TYPE_JPG;
139
else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0)
140
img_type(idict) = IMAGE_TYPE_PNG;
141
else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0)
142
img_type(idict) = IMAGE_TYPE_JBIG2;
143
else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0)
144
img_type(idict) = IMAGE_TYPE_PDF;
247
static void checktypebyextension (integer img)
147
static void check_type_by_extension(image_dict * idict)
249
149
char *image_suffix;
251
if (img_type (img) != IMAGE_TYPE_NONE) /* nothing to do */
151
assert(idict != NULL);
152
if (img_type(idict) != IMAGE_TYPE_NONE) /* nothing to do */
254
if ((image_suffix = strrchr (cur_file_name, '.')) == 0)
255
img_type (img) = IMAGE_TYPE_NONE;
256
else if (strcasecmp (image_suffix, ".png") == 0)
257
img_type (img) = IMAGE_TYPE_PNG;
258
else if (strcasecmp (image_suffix, ".jpg") == 0 ||
259
strcasecmp (image_suffix, ".jpeg") == 0)
260
img_type (img) = IMAGE_TYPE_JPG;
261
else if (strcasecmp (image_suffix, ".jbig2") == 0 ||
262
strcasecmp (image_suffix, ".jb2") == 0)
263
img_type (img) = IMAGE_TYPE_JBIG2;
264
else if (strcasecmp (image_suffix, ".pdf") == 0)
265
img_type (img) = IMAGE_TYPE_PDF;
268
integer read_image (strnumber s, integer page_num, strnumber page_name,
269
integer colorspace, integer pagebox,
270
integer pdfversion, integer pdfinclusionerrorlevel)
273
integer img = new_image_entry ();
155
if ((image_suffix = strrchr(img_filename(idict), '.')) == 0)
156
img_type(idict) = IMAGE_TYPE_NONE;
157
else if (strcasecmp(image_suffix, ".png") == 0)
158
img_type(idict) = IMAGE_TYPE_PNG;
159
else if (strcasecmp(image_suffix, ".jpg") == 0 ||
160
strcasecmp(image_suffix, ".jpeg") == 0)
161
img_type(idict) = IMAGE_TYPE_JPG;
162
else if (strcasecmp(image_suffix, ".jbig2") == 0 ||
163
strcasecmp(image_suffix, ".jb2") == 0)
164
img_type(idict) = IMAGE_TYPE_JBIG2;
165
else if (strcasecmp(image_suffix, ".pdf") == 0)
166
img_type(idict) = IMAGE_TYPE_PDF;
169
/**********************************************************************/
171
void init_image(image * p)
177
img_transform(p) = 0;
179
img_unset_refered(p); /* wd/ht/dp may be modified */
182
img_dictref(p) = LUA_NOREF;
187
image *p = xtalloc(1, image);
192
void init_image_dict(image_dict * p)
203
img_colorspace(p) = 0;
204
img_totalpages(p) = 0;
206
img_pagename(p) = NULL;
207
img_filename(p) = NULL;
208
img_filepath(p) = NULL;
211
img_type(p) = IMAGE_TYPE_NONE;
213
img_colordepth(p) = 0;
214
img_pagebox(p) = PDF_BOX_SPEC_MEDIA;
215
img_state(p) = DICT_NEW;
216
img_png_ptr(p) = NULL; /* union */
219
image_dict *new_image_dict()
221
image_dict *p = xtalloc(1, image_dict);
226
void free_dict_strings(image_dict * p)
228
if (img_filename(p) != NULL)
229
xfree(img_filename(p));
230
img_filename(p) = NULL;
231
if (img_filepath(p) != NULL)
232
xfree(img_filepath(p));
233
img_filepath(p) = NULL;
234
if (img_attr(p) != NULL)
237
if (img_pagename(p) != NULL)
238
xfree(img_pagename(p));
239
img_pagename(p) = NULL;
242
void free_image_dict(image_dict * p)
243
{ /* called from limglib.c */
244
assert(img_state(p) < DICT_REFERED);
245
switch (img_type(p)) {
247
unrefPdfDocument(img_filepath(p));
249
case IMAGE_TYPE_PNG: /* assuming IMG_CLOSEINBETWEEN */
250
assert(img_png_ptr(p) == NULL);
252
case IMAGE_TYPE_JPG: /* assuming IMG_CLOSEINBETWEEN */
253
assert(img_jpg_ptr(p) == NULL);
255
case IMAGE_TYPE_JBIG2: /* todo: writejbig2.c cleanup */
257
case IMAGE_TYPE_NONE:
262
free_dict_strings(p);
263
assert(img_file(p) == NULL);
267
/**********************************************************************/
269
void pdf_print_resname_prefix()
271
if (pdf_resname_prefix != 0)
272
pdf_printf(makecstring(pdf_resname_prefix));
275
void read_img(image_dict * idict, integer pdf_minor_version,
276
integer pdf_inclusion_errorlevel)
276
img_colorspace_ref (img) = colorspace;
278
/* need to allocate new string as makecstring's buffer is
279
already used by cur_file_name */
281
dest = xstrdup (makecstring (page_name));
282
cur_file_name = makecfilename (s);
284
callback_id=callback_defined(find_image_file_callback);
285
if (callback_id>0 && run_callback(callback_id,"S->S",cur_file_name,&imagename)) {
286
if (imagename && !strlen(imagename))
288
img_name (img) = imagename;
290
img_name (img) = kpse_find_file (cur_file_name, kpse_tex_format, true);
280
assert(idict != NULL);
281
if (img_filename(idict) == NULL)
282
pdftex_fail("image file name missing");
283
callback_id = callback_defined(find_image_file_callback);
284
if (img_filepath(idict) == NULL) {
286
&& run_callback(callback_id, "S->S", img_filename(idict),
288
if (filepath && (strlen(filepath) > 0))
289
img_filepath(idict) = strdup(filepath);
291
img_filepath(idict) =
292
kpse_find_file(img_filename(idict), kpse_tex_format, true);
292
if (img_name (img) == NULL)
293
pdftex_fail ("cannot find image file");
294
/* kpse_find_file perhaps changed the file name */
295
cur_file_name = img_name (img);
294
if (img_filepath(idict) == NULL)
295
pdftex_fail("cannot find image file");
297
296
/* type checks */
298
checktypebyheader (img);
299
checktypebyextension (img);
297
check_type_by_header(idict);
298
check_type_by_extension(idict);
301
switch (img_type (img)) {
303
pdf_ptr (img) = xtalloc (1, pdf_image_struct);
304
pdf_ptr (img)->page_box = pagebox;
305
page_num = read_pdf_info (img_name (img), dest, page_num, pagebox,
306
pdfversion, pdfinclusionerrorlevel);
307
img_width (img) = bp2int (epdf_width);
308
img_height (img) = bp2int (epdf_height);
309
img_pages (img) = epdf_num_pages;
310
pdf_ptr (img)->orig_x = bp2int (epdf_orig_x_i);
311
pdf_ptr (img)->orig_y = bp2int (epdf_orig_y_i);
312
pdf_ptr (img)->selected_page = page_num;
313
pdf_ptr (img)->doc = epdf_doc;
320
jpg_ptr (img) = xtalloc (1, JPG_IMAGE_INFO);
324
case IMAGE_TYPE_JBIG2:
325
if (pdfversion < 4) {
327
("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
330
jbig2_ptr (img) = xtalloc (1, JBIG2_IMAGE_INFO);
331
img_type (img) = IMAGE_TYPE_JBIG2;
332
jbig2_ptr (img)->selected_page = page_num;
333
read_jbig2_info (img);
336
pdftex_fail ("unknown type of image");
339
cur_file_name = NULL;
343
void write_image (integer img)
345
cur_file_name = img_name (img);
347
tex_printf (" <%s", img_name (img));
348
switch (img_type (img)) {
355
case IMAGE_TYPE_JBIG2:
359
epdf_doc = pdf_ptr (img)->doc;
360
epdf_selected_page = pdf_ptr (img)->selected_page;
361
epdf_page_box = pdf_ptr (img)->page_box;
365
pdftex_fail ("unknown type of image");
369
cur_file_name = NULL;
372
void delete_image (integer img)
374
switch (img_type (img)) {
376
epdf_doc = pdf_ptr (img)->doc;
380
xfclose ((FILE *) png_ptr (img)->io_ptr, cur_file_name);
381
png_destroy_read_struct (&(png_ptr (img)), &(png_info (img)), NULL);
384
xfclose (jpg_ptr (img)->file, cur_file_name);
386
case IMAGE_TYPE_JBIG2:
389
pdftex_fail ("unknown type of image");
391
xfree (img_name (img));
300
switch (img_type(idict)) {
302
read_pdf_info(idict, pdf_minor_version, pdf_inclusion_errorlevel);
305
read_png_info(idict, IMG_CLOSEINBETWEEN);
308
read_jpg_info(idict, IMG_CLOSEINBETWEEN);
310
case IMAGE_TYPE_JBIG2:
311
if (pdf_minor_version < 4) {
313
("JBIG2 images only possible with at least PDF 1.4; you are generating PDF 1.%i",
314
(int) pdf_minor_version);
316
read_jbig2_info(idict);
319
pdftex_fail("internal error: unknown image type");
321
cur_file_name = NULL;
322
if (img_state(idict) < DICT_FILESCANNED)
323
img_state(idict) = DICT_FILESCANNED;
326
void scale_img(image * img)
328
integer x, y, xr, yr; /* size and resolution of image */
329
scaled w, h; /* indeed size corresponds to image resolution */
332
image_dict *idict = img_dict(img);
333
assert(idict != NULL);
334
x = img_xsize(idict); /* dimensions, resolutions from image file */
335
y = img_ysize(idict);
336
xr = img_xres(idict);
337
yr = img_yres(idict);
338
if ((img_transform(img) & 1) == 1) {
346
if (xr > 65535 || yr > 65535) {
349
pdftex_warn("ext1: too large image resolution ignored");
351
if (x <= 0 || y <= 0 || xr < 0 || yr < 0)
352
pdftex_fail("ext1: invalid image dimensions");
353
if (img_type(idict) == IMAGE_TYPE_PDF) {
357
default_res = fix_int(get_pdf_image_resolution(), 0, 65535);
358
if (default_res > 0 && (xr == 0 || yr == 0)) {
362
if (is_wd_running(img) && is_ht_running(img)) {
363
if (xr > 0 && yr > 0) {
364
w = ext_xn_over_d(one_hundred_inch, x, 100 * xr);
365
h = ext_xn_over_d(one_hundred_inch, y, 100 * yr);
367
w = ext_xn_over_d(one_hundred_inch, x, 7200);
368
h = ext_xn_over_d(one_hundred_inch, y, 7200);
372
if (is_wd_running(img) && is_ht_running(img) && is_dp_running(img)) {
376
} else if (is_wd_running(img)) {
377
/* image depth or height is explicitly specified */
378
if (is_ht_running(img)) {
379
/* image depth is explicitly specified */
380
img_width(img) = ext_xn_over_d(h, x, y);
381
img_height(img) = h - img_depth(img);
382
} else if (is_dp_running(img)) {
383
/* image height is explicitly specified */
384
img_width(img) = ext_xn_over_d(img_height(img), x, y);
387
/* both image depth and height are explicitly specified */
389
ext_xn_over_d(img_height(img) + img_depth(img), x, y);
392
/* image width is explicitly specified */
393
if (is_ht_running(img) && is_dp_running(img)) {
394
/* both image depth and height are not specified */
395
img_height(img) = ext_xn_over_d(img_width(img), y, x);
398
/* image depth is explicitly specified */
399
else if (is_ht_running(img)) {
401
ext_xn_over_d(img_width(img), y, x) - img_depth(img);
403
/* image height is explicitly specified */
404
else if (is_dp_running(img)) {
407
/* else both image depth and height are explicitly specified */
412
void out_img(image * img, scaled hpos, scaled vpos)
414
float a[6]; /* transformation matrix */
415
int r; /* number of digits after the decimal point */
417
image_dict *idict = img_dict(img);
419
scaled wd = img_width(img);
420
scaled ht = img_height(img);
421
scaled dp = img_depth(img);
422
if (img_type(idict) == IMAGE_TYPE_PDF) {
423
a[0] = wd * 1.0e6 / img_xsize(idict);
426
a[3] = (ht + dp) * 1.0e6 / img_ysize(idict);
427
a[4] = hpos - (float) wd *img_xorig(idict) / img_xsize(idict);
428
a[5] = vpos - (float) ht *img_yorig(idict) / img_ysize(idict);
431
a[0] = wd * 1.0e6 / one_hundred_bp;
434
a[3] = (ht + dp) * 1.0e6 / one_hundred_bp;
439
if ((img_transform(img) & 1) == 1) {
441
pdftex_fail("image transform: division by zero (height == -depth)");
443
pdftex_fail("image transform: division by zero (width == 0)");
445
switch (img_transform(img) & 7) {
446
case 0: /* no transform */
448
case 1: /* rot. 90 deg. (counterclockwise) */
449
a[1] = a[0] * (ht + dp) / wd;
451
a[2] = -a[3] * wd / (ht + dp);
455
case 2: /* rot. 180 deg. */
461
case 3: /* rot. 270 deg. */
462
a[1] = -a[0] * (ht + dp) / wd;
464
a[2] = a[3] * wd / (ht + dp);
468
case 4: /* mirrored, unrotated */
472
case 5: /* mirrored, then rot. 90 deg. */
473
a[1] = -a[0] * (ht + dp) / wd;
475
a[2] = -a[3] * wd / (ht + dp);
480
case 6: /* mirrored, then rot. 180 deg. */
484
case 7: /* mirrored, then rot. 270 deg. */
485
a[1] = a[0] * (ht + dp) / wd;
487
a[2] = a[3] * wd / (ht + dp);
495
pdf_print_real((integer) a[0], r);
497
pdf_print_real((integer) a[1], r);
499
pdf_print_real((integer) a[2], r);
501
pdf_print_real((integer) a[3], r);
503
pdf_print_bp((integer) a[4]);
505
pdf_print_bp((integer) a[5]);
506
pdf_printf(" cm\n/Im");
507
pdf_print_int(img_index(idict));
508
pdf_print_resname_prefix();
509
pdf_printf(" Do\nQ\n");
510
if (img_state(idict) < DICT_OUTIMG)
511
img_state(idict) = DICT_OUTIMG;
514
void write_img(image_dict * idict)
516
assert(idict != NULL);
517
if (img_state(idict) < DICT_WRITTEN) {
519
tex_printf(" <%s", img_filepath(idict));
520
switch (img_type(idict)) {
527
case IMAGE_TYPE_JBIG2:
534
pdftex_fail("internal error: unknown image type");
539
if (img_state(idict) < DICT_WRITTEN)
540
img_state(idict) = DICT_WRITTEN;
543
/**********************************************************************/
545
typedef image *img_entry;
546
/* define img_ptr, img_array, & img_limit */
547
define_array(img); /* array of pointers to image structures */
549
integer img_to_array(image * img)
552
alloc_array(img, 1, SMALL_BUF_SIZE);
554
return img_ptr++ - img_array; /* now img is read-only */
557
/**********************************************************************/
558
/* stuff to be accessible from TeX */
560
integer read_image(integer objnum, integer index, strnumber filename,
564
integer colorspace, integer page_box,
565
integer pdf_minor_version, integer pdf_inclusion_errorlevel)
568
image *a = new_image();
569
ref = img_to_array(a);
570
image_dict *idict = img_dict(a) = new_image_dict();
571
assert(idict != NULL);
572
img_objnum(idict) = objnum;
573
img_index(idict) = index;
574
/* img_xsize, img_ysize, img_xres, img_yres set by read_img() */
575
img_colorspace(idict) = colorspace;
576
img_pagenum(idict) = page_num;
577
/* img_totalpages set by read_img() */
579
img_pagename(idict) = xstrdup(makecstring(page_name));
580
cur_file_name = makecfilename(filename);
581
assert(cur_file_name != NULL);
582
img_filename(idict) = xstrdup(cur_file_name);
584
img_attr(idict) = xstrdup(makecstring(attr));
585
img_pagebox(idict) = page_box;
586
read_img(idict, pdf_minor_version, pdf_inclusion_errorlevel);
592
void set_image_dimensions(integer ref, integer wd, integer ht, integer dp)
594
image *a = img_array[ref];
600
void scale_image(integer ref)
602
scale_img(img_array[ref]);
605
void out_image(integer ref, scaled hpos, scaled vpos)
607
image *a = img_array[ref];
608
out_img(a, hpos, vpos);
611
void write_image(integer ref)
613
write_img(img_dict(img_array[ref]));
616
integer image_pages(integer ref)
618
return img_totalpages(img_dict(img_array[ref]));
621
integer image_colordepth(integer ref)
623
return img_colordepth(img_dict(img_array[ref]));
626
integer epdf_orig_x(integer ref)
628
return img_xorig(img_dict(img_array[ref]));
631
integer epdf_orig_y(integer ref)
633
return img_yorig(img_dict(img_array[ref]));
636
integer image_objnum(integer ref)
638
return img_objnum(img_dict(img_array[ref]));
641
integer image_index(integer ref)
643
return img_index(img_dict(img_array[ref]));
646
integer image_width(integer ref)
648
return img_width(img_array[ref]);
651
integer image_height(integer ref)
653
return img_height(img_array[ref]);
656
integer image_depth(integer ref)
658
return img_depth(img_array[ref]);
661
void update_image_procset(integer ref)
663
pdf_image_procset |= img_color(img_dict(img_array[ref]));
666
boolean check_image_b(integer procset)
668
return procset & IMAGE_COLOR_B;
671
boolean check_image_c(integer procset)
673
return procset & IMAGE_COLOR_C;
676
boolean check_image_i(integer procset)
678
return procset & IMAGE_COLOR_I;