| 1 |
/* |
| 2 |
|
| 3 |
writeimg.c |
| 4 |
|
| 5 |
Copyright 1996-2006 Han The Thanh <[email protected]> |
| 6 |
Copyright 2006-2012 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 <kpathsea/absolute.h> |
| 25 |
#include <kpathsea/c-auto.h> |
| 26 |
#include <kpathsea/c-memstr.h> |
| 27 |
|
| 28 |
#include "image/image.h" |
| 29 |
#include "image/writejpg.h" |
| 30 |
#include "image/writejp2.h" |
| 31 |
#include "image/writepng.h" |
| 32 |
#include "image/writejbig2.h" |
| 33 |
|
| 34 |
#include "lua.h" |
| 35 |
#include "lauxlib.h" |
| 36 |
|
| 37 |
/*tex |
| 38 |
|
| 39 |
The function |readimage| performs some basic initializations. Then it looks at |
| 40 |
the file extension to determine the image type and calls specific code/functions. |
| 41 |
The main disadvantage is that standard file extensions have to be used, otherwise |
| 42 |
pdfTeX is not able to detect the correct image type. The patch now looks at the |
| 43 |
file header first regardless of the file extension. This is implemented in |
| 44 |
function |check_type_by_header|. If this check fails, the traditional test of |
| 45 |
standard file extension is tried, done in function |check_type_by_extension|. |
| 46 |
|
| 47 |
The magic headers are as follows: |
| 48 |
|
| 49 |
\startitemize |
| 50 |
\startitem |
| 51 |
\type {png}: 89 50 4E 47 0D 0A 1A 0A or |"\137PNG\013\010\026\010"| |
| 52 |
\stopitem |
| 53 |
\startitem |
| 54 |
\type {jpg}: FF D8 FF or |"\255\216\255"|. |
| 55 |
\stopitem |
| 56 |
\startitem |
| 57 |
\type {jp2}: 00 00 00 0C 6A 50 20 20 0D 0A or |"\000\000\000\012\106\080\032\032\013\010"| |
| 58 |
\stopitem |
| 59 |
\startitem |
| 60 |
\type {pdf}: |"%PDF"| somewhere in the beginning |
| 61 |
\stopitem |
| 62 |
\stopitemize |
| 63 |
|
| 64 |
Functions |check_type_by_header| and |check_type_by_extension|: |img_type(img)| |
| 65 |
is set to |IMG_TYPE_NONE| by |new_image_dict()|. Both functions try to detect a |
| 66 |
type and set |img_type(img)|. Thus a value other than |IMG_TYPE_NONE| indicates |
| 67 |
that a type has been found. |
| 68 |
|
| 69 |
*/ |
| 70 |
|
| 71 |
#define HEADER_JPG "\xFF\xD8" |
| 72 |
#define HEADER_PNG "\x89PNG\r\n\x1A\n" |
| 73 |
#define HEADER_JBIG2 "\x97\x4A\x42\x32\x0D\x0A\x1A\x0A" |
| 74 |
#define HEADER_JP2 "\x6A\x50\x20\x20" |
| 75 |
#define HEADER_PDF "%PDF-" |
| 76 |
|
| 77 |
#define MAX_HEADER (sizeof(HEADER_PNG)-1) |
| 78 |
|
| 79 |
#define HEADER_PDF_MEMSTREAM "data:application/pdf," |
| 80 |
#define LEN_PDF_MEMSTREAM 21 |
| 81 |
|
| 82 |
static void check_type_by_header(image_dict * idict) |
| 83 |
{ |
| 84 |
int i; |
| 85 |
FILE *file = NULL; |
| 86 |
char header[MAX_HEADER]; |
| 87 |
char prefix[LEN_PDF_MEMSTREAM+1]; |
| 88 |
if (idict == NULL) |
| 89 |
return; |
| 90 |
if (img_type(idict) != IMG_TYPE_NONE) |
| 91 |
return; |
| 92 |
/*tex Here we read the and also check for a memstream object. */ |
| 93 |
if (!img_filepath(idict) || !FOPEN_RBIN_MODE) { |
| 94 |
normal_error("pdf backend","reading image file failed"); |
| 95 |
} |
| 96 |
file = fopen(img_filepath(idict), FOPEN_RBIN_MODE); |
| 97 |
if (file == NULL) { |
| 98 |
/*tex We check the prefix of img_filepath(idict). */ |
| 99 |
for (i = 0; (unsigned) i < LEN_PDF_MEMSTREAM; i++) { |
| 100 |
prefix[i] = (char) (img_filepath(idict)[i]); |
| 101 |
} |
| 102 |
prefix[LEN_PDF_MEMSTREAM]='\0'; |
| 103 |
if (strncmp(prefix, HEADER_PDF_MEMSTREAM, LEN_PDF_MEMSTREAM) == 0) { |
| 104 |
img_type(idict) = IMG_TYPE_PDFMEMSTREAM; |
| 105 |
return; |
| 106 |
} else { |
| 107 |
formatted_error("pdf backend","reading image file '%s' failed",img_filepath(idict)); |
| 108 |
} |
| 109 |
} |
| 110 |
/*tex Do we have a valid file but perhaps unsupported? */ |
| 111 |
for (i = 0; (unsigned) i < MAX_HEADER; i++) { |
| 112 |
header[i] = (char) xgetc(file); |
| 113 |
if (feof(file)) { |
| 114 |
normal_error("pdf backend","reading image file failed"); |
| 115 |
} |
| 116 |
} |
| 117 |
xfclose(file, img_filepath(idict)); |
| 118 |
/*tex Further tests: */ |
| 119 |
if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0) |
| 120 |
img_type(idict) = IMG_TYPE_JPG; |
| 121 |
else if (strncmp(header + 4, HEADER_JP2, sizeof(HEADER_JP2) - 1) == 0) |
| 122 |
img_type(idict) = IMG_TYPE_JP2; |
| 123 |
else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0) |
| 124 |
img_type(idict) = IMG_TYPE_PNG; |
| 125 |
else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0) |
| 126 |
img_type(idict) = IMG_TYPE_JBIG2; |
| 127 |
else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0) |
| 128 |
img_type(idict) = IMG_TYPE_PDF; |
| 129 |
} |
| 130 |
|
| 131 |
static void check_type_by_extension(image_dict * idict) |
| 132 |
{ |
| 133 |
char *image_suffix; |
| 134 |
if (idict == NULL) |
| 135 |
return; |
| 136 |
if (img_type(idict) != IMG_TYPE_NONE) |
| 137 |
return; |
| 138 |
if ((image_suffix = strrchr(img_filename(idict), '.')) == 0) |
| 139 |
img_type(idict) = IMG_TYPE_NONE; |
| 140 |
else if (strcasecmp(image_suffix, ".png") == 0) |
| 141 |
img_type(idict) = IMG_TYPE_PNG; |
| 142 |
else if (strcasecmp(image_suffix, ".jpg") == 0 || |
| 143 |
strcasecmp(image_suffix, ".jpeg") == 0) |
| 144 |
img_type(idict) = IMG_TYPE_JPG; |
| 145 |
else if (strcasecmp(image_suffix, ".jp2") == 0) |
| 146 |
img_type(idict) = IMG_TYPE_JP2; |
| 147 |
else if (strcasecmp(image_suffix, ".jbig2") == 0 || |
| 148 |
strcasecmp(image_suffix, ".jb2") == 0) |
| 149 |
img_type(idict) = IMG_TYPE_JBIG2; |
| 150 |
else if (strcasecmp(image_suffix, ".pdf") == 0) |
| 151 |
img_type(idict) = IMG_TYPE_PDF; |
| 152 |
} |
| 153 |
|
| 154 |
void new_img_pdfstream_struct(image_dict * p) |
| 155 |
{ |
| 156 |
img_pdfstream_ptr(p) = xtalloc(1, pdf_stream_struct); |
| 157 |
img_pdfstream_stream(p) = NULL; |
| 158 |
img_pdfstream_size(p) = 0; |
| 159 |
} |
| 160 |
|
| 161 |
image *new_image(void) |
| 162 |
{ |
| 163 |
image *p = xtalloc(1, image); |
| 164 |
set_wd_running(p); |
| 165 |
set_ht_running(p); |
| 166 |
set_dp_running(p); |
| 167 |
img_transform(p) = 0; |
| 168 |
img_dict(p) = NULL; |
| 169 |
img_dictref(p) = LUA_NOREF; |
| 170 |
return p; |
| 171 |
} |
| 172 |
|
| 173 |
image_dict *new_image_dict(void) |
| 174 |
{ |
| 175 |
image_dict *p = xtalloc(1, image_dict); |
| 176 |
memset(p, 0, sizeof(image_dict)); |
| 177 |
set_wd_running(p); |
| 178 |
set_ht_running(p); |
| 179 |
set_dp_running(p); |
| 180 |
img_transform(p) = 0; |
| 181 |
img_pagenum(p) = 1; |
| 182 |
img_type(p) = IMG_TYPE_NONE; |
| 183 |
img_pagebox(p) = PDF_BOX_SPEC_MEDIA; |
| 184 |
img_unset_bbox(p); |
| 185 |
img_unset_group(p); |
| 186 |
img_state(p) = DICT_NEW; |
| 187 |
/*tex A value of -1 means unused while the used counts from 0 */ |
| 188 |
img_index(p) = -1; |
| 189 |
img_luaref(p) = 0; |
| 190 |
img_errorlevel(p) = pdf_inclusion_errorlevel; |
| 191 |
fix_pdf_version(static_pdf); |
| 192 |
img_pdfmajorversion(p) = pdf_major_version; |
| 193 |
img_pdfminorversion(p) = pdf_minor_version; |
| 194 |
return p; |
| 195 |
} |
| 196 |
|
| 197 |
static void free_dict_strings(image_dict * p) |
| 198 |
{ |
| 199 |
xfree(img_filename(p)); |
| 200 |
xfree(img_filepath(p)); |
| 201 |
xfree(img_attr(p)); |
| 202 |
xfree(img_pagename(p)); |
| 203 |
} |
| 204 |
|
| 205 |
void free_image_dict(image_dict * p) |
| 206 |
{ |
| 207 |
if (ini_version) { |
| 208 |
/*tex The image may be \.{\\dump}ed to a format. */ |
| 209 |
return; |
| 210 |
} |
| 211 |
/*tex Called from limglib.c. */ |
| 212 |
switch (img_type(p)) { |
| 213 |
case IMG_TYPE_PDFMEMSTREAM: |
| 214 |
case IMG_TYPE_PDF: |
| 215 |
flush_pdf_info(p); |
| 216 |
break; |
| 217 |
case IMG_TYPE_PNG: |
| 218 |
flush_png_info(p); |
| 219 |
break; |
| 220 |
case IMG_TYPE_JPG: |
| 221 |
flush_jpg_info(p); |
| 222 |
break; |
| 223 |
case IMG_TYPE_JP2: |
| 224 |
flush_jp2_info(p); |
| 225 |
break; |
| 226 |
case IMG_TYPE_JBIG2: |
| 227 |
flush_jbig2_info(p); |
| 228 |
break; |
| 229 |
case IMG_TYPE_PDFSTREAM: |
| 230 |
if (img_pdfstream_ptr(p) != NULL) { |
| 231 |
xfree(img_pdfstream_stream(p)); |
| 232 |
xfree(img_pdfstream_ptr(p)); |
| 233 |
} |
| 234 |
break; |
| 235 |
case IMG_TYPE_NONE: |
| 236 |
break; |
| 237 |
default: |
| 238 |
normal_error("pdf backend","unknown image type"); |
| 239 |
} |
| 240 |
free_dict_strings(p); |
| 241 |
xfree(p); |
| 242 |
} |
| 243 |
|
| 244 |
void read_img(image_dict * idict) |
| 245 |
{ |
| 246 |
char *filepath = NULL; |
| 247 |
int callback_id; |
| 248 |
if (img_filename(idict) == NULL) { |
| 249 |
normal_error("pdf backend","image file name missing"); |
| 250 |
} |
| 251 |
callback_id = callback_defined(find_image_file_callback); |
| 252 |
if (img_filepath(idict) == NULL) { |
| 253 |
if (callback_id > 0) { |
| 254 |
/*tex We always callback, also for a mem stream. */ |
| 255 |
if (run_callback(callback_id, "S->S", img_filename(idict),&filepath)) { |
| 256 |
if (filepath && (strlen(filepath) > 0)) { |
| 257 |
img_filepath(idict) = strdup(filepath); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
if (img_filepath(idict) == NULL && (strstr(img_filename(idict),"data:application/pdf,") != NULL)) { |
| 262 |
/*tex We need to check here for a pdf memstream. */ |
| 263 |
img_filepath(idict) = strdup(img_filename(idict)); |
| 264 |
} else if (callback_id == 0) { |
| 265 |
/*tex Otherwise we use kpse but only when we don't callback. */ |
| 266 |
char *st = img_filename(idict); |
| 267 |
char *res = NULL; |
| 268 |
if (output_directory && !kpse_absolute_p(st, false)) { |
| 269 |
char *ftemp = concat3(output_directory, DIR_SEP_STRING, st); |
| 270 |
res = kpse_find_file(ftemp, kpse_tex_format, true); |
| 271 |
xfree(ftemp); |
| 272 |
} |
| 273 |
if(res) { |
| 274 |
img_filepath(idict) = res; |
| 275 |
} else { |
| 276 |
img_filepath(idict) = kpse_find_file(st, kpse_tex_format, true); |
| 277 |
} |
| 278 |
} |
| 279 |
if (img_filepath(idict) == NULL) { |
| 280 |
/*tex In any case we need a name. */ |
| 281 |
formatted_error("pdf backend","cannot find image file '%s'", img_filename(idict)); |
| 282 |
} |
| 283 |
} |
| 284 |
recorder_record_input(img_filepath(idict)); |
| 285 |
/*tex A few type checks. */ |
| 286 |
check_type_by_header(idict); |
| 287 |
check_type_by_extension(idict); |
| 288 |
/*tex Now we're ready to read the image. */ |
| 289 |
switch (img_type(idict)) { |
| 290 |
case IMG_TYPE_PDFMEMSTREAM: |
| 291 |
case IMG_TYPE_PDF: |
| 292 |
read_pdf_info(idict); |
| 293 |
break; |
| 294 |
case IMG_TYPE_PNG: |
| 295 |
read_png_info(idict); |
| 296 |
break; |
| 297 |
case IMG_TYPE_JPG: |
| 298 |
read_jpg_info(idict); |
| 299 |
break; |
| 300 |
case IMG_TYPE_JP2: |
| 301 |
read_jp2_info(idict); |
| 302 |
break; |
| 303 |
case IMG_TYPE_JBIG2: |
| 304 |
read_jbig2_info(idict); |
| 305 |
break; |
| 306 |
default: |
| 307 |
img_type(idict) = IMG_TYPE_NONE; |
| 308 |
if (pdf_ignore_unknown_images) { |
| 309 |
normal_warning("pdf backend","internal error: ignoring unknown image type"); |
| 310 |
} else { |
| 311 |
normal_error("pdf backend","internal error: unknown image type"); |
| 312 |
} |
| 313 |
break; |
| 314 |
} |
| 315 |
cur_file_name = NULL; |
| 316 |
if (img_type(idict) == IMG_TYPE_NONE) { |
| 317 |
img_state(idict) = DICT_NEW; |
| 318 |
} else if (img_state(idict) < DICT_FILESCANNED) { |
| 319 |
img_state(idict) = DICT_FILESCANNED; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
static image_dict *read_image(char *file_name, int page_num, char *page_name, int colorspace, int page_box, char *user_password, char *owner_password, char *visible_filename) |
| 324 |
{ |
| 325 |
image *a = new_image(); |
| 326 |
image_dict *idict = img_dict(a) = new_image_dict(); |
| 327 |
static_pdf->ximage_count++; |
| 328 |
img_objnum(idict) = pdf_create_obj(static_pdf, obj_type_ximage, static_pdf->ximage_count); |
| 329 |
img_index(idict) = static_pdf->ximage_count; |
| 330 |
set_obj_data_ptr(static_pdf, img_objnum(idict), img_index(idict)); |
| 331 |
idict_to_array(idict); |
| 332 |
img_colorspace(idict) = colorspace; |
| 333 |
img_pagenum(idict) = page_num; |
| 334 |
img_pagename(idict) = page_name; |
| 335 |
img_userpassword(idict) = user_password; |
| 336 |
img_ownerpassword(idict) = owner_password; |
| 337 |
img_visiblefilename(idict) = visible_filename; |
| 338 |
if (file_name == NULL) { |
| 339 |
normal_error("pdf backend","no image filename given"); |
| 340 |
} |
| 341 |
cur_file_name = file_name; |
| 342 |
img_filename(idict) = file_name; |
| 343 |
img_pagebox(idict) = page_box; |
| 344 |
read_img(idict); |
| 345 |
return idict; |
| 346 |
} |
| 347 |
|
| 348 |
/*tex |
| 349 |
|
| 350 |
There can be several page boxes. Normally the cropbox is used. |
| 351 |
|
| 352 |
*/ |
| 353 |
|
| 354 |
static pdfboxspec_e scan_pdf_box_spec(void) |
| 355 |
{ |
| 356 |
if (scan_keyword("mediabox")) |
| 357 |
return PDF_BOX_SPEC_MEDIA; |
| 358 |
else if (scan_keyword("cropbox")) |
| 359 |
return PDF_BOX_SPEC_CROP; |
| 360 |
else if (scan_keyword("bleedbox")) |
| 361 |
return PDF_BOX_SPEC_BLEED; |
| 362 |
else if (scan_keyword("trimbox")) |
| 363 |
return PDF_BOX_SPEC_TRIM; |
| 364 |
else if (scan_keyword("artbox")) |
| 365 |
return PDF_BOX_SPEC_ART; |
| 366 |
else |
| 367 |
return PDF_BOX_SPEC_NONE; |
| 368 |
} |
| 369 |
|
| 370 |
void scan_pdfximage(PDF pdf) |
| 371 |
{ |
| 372 |
scaled_whd alt_rule; |
| 373 |
image_dict *idict; |
| 374 |
int transform = 0, page = 1, pagebox, colorspace = 0; |
| 375 |
char *named = NULL, *attr = NULL, *file_name = NULL, *user = NULL, *owner = NULL, *visible = NULL; |
| 376 |
alt_rule = scan_alt_rule(); |
| 377 |
if (scan_keyword("attr")) { |
| 378 |
scan_toks(false, true); |
| 379 |
attr = tokenlist_to_cstring(def_ref, true, NULL); |
| 380 |
delete_token_ref(def_ref); |
| 381 |
} |
| 382 |
if (scan_keyword("named")) { |
| 383 |
scan_toks(false, true); |
| 384 |
if (0) { |
| 385 |
named = tokenlist_to_cstring(def_ref, true, NULL); |
| 386 |
page = 0; |
| 387 |
} else { |
| 388 |
normal_warning("pdf backend","named pages are not supported, using page 1"); |
| 389 |
page = 1; |
| 390 |
} |
| 391 |
delete_token_ref(def_ref); |
| 392 |
} else if (scan_keyword("page")) { |
| 393 |
scan_int(); |
| 394 |
page = cur_val; |
| 395 |
} |
| 396 |
if (scan_keyword("userpassword")) { |
| 397 |
scan_toks(false, true); |
| 398 |
user = tokenlist_to_cstring(def_ref, true, NULL); |
| 399 |
delete_token_ref(def_ref); |
| 400 |
} |
| 401 |
if (scan_keyword("ownerpassword")) { |
| 402 |
scan_toks(false, true); |
| 403 |
owner = tokenlist_to_cstring(def_ref, true, NULL); |
| 404 |
delete_token_ref(def_ref); |
| 405 |
} |
| 406 |
if (scan_keyword("visiblefilename")) { |
| 407 |
scan_toks(false, true); |
| 408 |
visible = tokenlist_to_cstring(def_ref, true, NULL); |
| 409 |
delete_token_ref(def_ref); |
| 410 |
} |
| 411 |
if (scan_keyword("colorspace")) { |
| 412 |
scan_int(); |
| 413 |
colorspace = cur_val; |
| 414 |
} |
| 415 |
pagebox = scan_pdf_box_spec(); |
| 416 |
if (pagebox == PDF_BOX_SPEC_NONE) { |
| 417 |
pagebox = pdf_pagebox; |
| 418 |
if (pagebox == PDF_BOX_SPEC_NONE) |
| 419 |
pagebox = PDF_BOX_SPEC_CROP; |
| 420 |
} |
| 421 |
scan_toks(false, true); |
| 422 |
file_name = tokenlist_to_cstring(def_ref, true, NULL); |
| 423 |
if (file_name == NULL) { |
| 424 |
normal_error("pdf backend","no image filename given"); |
| 425 |
} |
| 426 |
delete_token_ref(def_ref); |
| 427 |
idict = read_image(file_name, page, named, colorspace, pagebox, user, owner, visible); |
| 428 |
img_attr(idict) = attr; |
| 429 |
img_dimen(idict) = alt_rule; |
| 430 |
img_transform(idict) = transform; |
| 431 |
last_saved_image_index = img_objnum(idict); |
| 432 |
last_saved_image_pages = img_totalpages(idict); |
| 433 |
} |
| 434 |
|
| 435 |
void scan_pdfrefximage(PDF pdf) |
| 436 |
{ |
| 437 |
/*tex One could scan transform as well. */ |
| 438 |
int transform = 0; |
| 439 |
/*tex Begin of experiment. */ |
| 440 |
int open = 0; |
| 441 |
/*tex End of experiment. */ |
| 442 |
image_dict *idict; |
| 443 |
/*tex This scans |<rule spec>| to |alt_rule|. */ |
| 444 |
scaled_whd alt_rule, dim; |
| 445 |
alt_rule = scan_alt_rule(); |
| 446 |
/*tex Begin of experiment. */ |
| 447 |
if (scan_keyword("keepopen")) { |
| 448 |
open = 1; |
| 449 |
} |
| 450 |
/*tex End of experiment. */ |
| 451 |
scan_int(); |
| 452 |
check_obj_type(pdf, obj_type_ximage, cur_val); |
| 453 |
tail_append(new_rule(image_rule)); |
| 454 |
idict = idict_array[obj_data_ptr(pdf, cur_val)]; |
| 455 |
/*tex Begin of experiment, */ |
| 456 |
if (open) { |
| 457 |
/*tex So we keep the original value when no close is given. */ |
| 458 |
idict->keepopen = 1; |
| 459 |
} |
| 460 |
/*tex End of experiment. */ |
| 461 |
if (img_state(idict) == DICT_NEW) { |
| 462 |
normal_warning("image","don't rely on the image data to be okay"); |
| 463 |
width(tail_par) = 0; |
| 464 |
height(tail_par) = 0; |
| 465 |
depth(tail_par) = 0; |
| 466 |
} else { |
| 467 |
if (alt_rule.wd != null_flag || alt_rule.ht != null_flag || alt_rule.dp != null_flag) { |
| 468 |
dim = scale_img(idict, alt_rule, transform); |
| 469 |
} else { |
| 470 |
dim = scale_img(idict, img_dimen(idict), img_transform(idict)); |
| 471 |
} |
| 472 |
width(tail_par) = dim.wd; |
| 473 |
height(tail_par) = dim.ht; |
| 474 |
depth(tail_par) = dim.dp; |
| 475 |
rule_transform(tail_par) = transform; |
| 476 |
rule_index(tail_par) = img_index(idict); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
The |tex_scale| function follows a sequence of decisions: |
| 482 |
|
| 483 |
\starttyping |
| 484 |
wd ht dp : res = tex; |
| 485 |
wd ht -- |
| 486 |
wd -- dp |
| 487 |
wd -- -- |
| 488 |
-- ht dp |
| 489 |
-- ht -- |
| 490 |
-- -- dp |
| 491 |
-- -- -- : res = nat; |
| 492 |
\stoptyping |
| 493 |
|
| 494 |
*/ |
| 495 |
|
| 496 |
scaled_whd tex_scale(scaled_whd nat, scaled_whd tex) |
| 497 |
{ |
| 498 |
scaled_whd res; |
| 499 |
if (!is_running(tex.wd) && !is_running(tex.ht) && !is_running(tex.dp)) { |
| 500 |
/*tex width, height, and depth specified */ |
| 501 |
res = tex; |
| 502 |
} else if (!is_running(tex.wd)) { |
| 503 |
/*tex max. 2 dimensions are specified */ |
| 504 |
res.wd = tex.wd; |
| 505 |
if (!is_running(tex.ht)) { |
| 506 |
res.ht = tex.ht; |
| 507 |
/*tex width and height specified */ |
| 508 |
res.dp = ext_xn_over_d(tex.ht, nat.dp, nat.ht); |
| 509 |
} else if (!is_running(tex.dp)) { |
| 510 |
res.dp = tex.dp; |
| 511 |
/*tex width and depth specified */ |
| 512 |
res.ht = ext_xn_over_d(tex.wd, nat.ht + nat.dp, nat.wd) - tex.dp; |
| 513 |
} else { |
| 514 |
/*tex only width specified */ |
| 515 |
res.ht = ext_xn_over_d(tex.wd, nat.ht, nat.wd); |
| 516 |
res.dp = ext_xn_over_d(tex.wd, nat.dp, nat.wd); |
| 517 |
} |
| 518 |
} else if (!is_running(tex.ht)) { |
| 519 |
res.ht = tex.ht; |
| 520 |
if (!is_running(tex.dp)) { |
| 521 |
res.dp = tex.dp; |
| 522 |
/*tex height and depth specified */ |
| 523 |
res.wd = ext_xn_over_d(tex.ht + tex.dp, nat.wd, nat.ht + nat.dp); |
| 524 |
} else { |
| 525 |
/*tex only height specified */ |
| 526 |
res.wd = ext_xn_over_d(tex.ht, nat.wd, nat.ht); |
| 527 |
res.dp = ext_xn_over_d(tex.ht, nat.dp, nat.ht); |
| 528 |
} |
| 529 |
} else if (!is_running(tex.dp)) { |
| 530 |
res.dp = tex.dp; |
| 531 |
/*tex only depth specified */ |
| 532 |
res.ht = nat.ht - (tex.dp - nat.dp); |
| 533 |
res.wd = nat.wd; |
| 534 |
} else { |
| 535 |
/*tex nothing specified */ |
| 536 |
res = nat; |
| 537 |
} |
| 538 |
return res; |
| 539 |
} |
| 540 |
|
| 541 |
/*tex |
| 542 |
|
| 543 |
Within |scale_img| only image width and height matter; the offsets and |
| 544 |
positioning are not interesting here. But one needs rotation info to swap width |
| 545 |
and height. |img_rotation| comes from the optional |/Rotate| key in the PDF file. |
| 546 |
|
| 547 |
*/ |
| 548 |
|
| 549 |
scaled_whd scale_img(image_dict * idict, scaled_whd alt_rule, int transform) |
| 550 |
{ |
| 551 |
/*tex size and resolution of image */ |
| 552 |
int x, y, xr, yr, tmp; |
| 553 |
/*tex natural size corresponding to image resolution */ |
| 554 |
scaled_whd nat; |
| 555 |
int default_res; |
| 556 |
nat.dp = 0; |
| 557 |
nat.wd = 0; |
| 558 |
nat.ht = 0; |
| 559 |
if (img_nobbox(idict)) { |
| 560 |
if (img_is_bbox(idict)) { |
| 561 |
x = img_xsize(idict) = img_bbox(idict)[2] - img_bbox(idict)[0]; |
| 562 |
y = img_ysize(idict) = img_bbox(idict)[3] - img_bbox(idict)[1]; |
| 563 |
img_xorig(idict) = img_bbox(idict)[0]; |
| 564 |
img_yorig(idict) = img_bbox(idict)[1]; |
| 565 |
nat.wd = x; |
| 566 |
nat.ht = y; |
| 567 |
} else { |
| 568 |
normal_error("pdf backend","use boundingbox to pass dimensions"); |
| 569 |
} |
| 570 |
} else { |
| 571 |
if ((img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM |
| 572 |
|| img_type(idict) == IMG_TYPE_PDFSTREAM) && img_is_bbox(idict)) { |
| 573 |
/*tex dimensions from image.bbox */ |
| 574 |
x = img_xsize(idict) = img_bbox(idict)[2] - img_bbox(idict)[0]; |
| 575 |
y = img_ysize(idict) = img_bbox(idict)[3] - img_bbox(idict)[1]; |
| 576 |
img_xorig(idict) = img_bbox(idict)[0]; |
| 577 |
img_yorig(idict) = img_bbox(idict)[1]; |
| 578 |
} else { |
| 579 |
/*tex dimensions, resolutions from image file */ |
| 580 |
x = img_xsize(idict); |
| 581 |
y = img_ysize(idict); |
| 582 |
} |
| 583 |
xr = img_xres(idict); |
| 584 |
yr = img_yres(idict); |
| 585 |
if (x <= 0 || y <= 0 || xr < 0 || yr < 0) |
| 586 |
normal_error("pdf backend","invalid image dimensions"); |
| 587 |
if (xr > 65535 || yr > 65535) { |
| 588 |
xr = 0; |
| 589 |
yr = 0; |
| 590 |
normal_warning("pdf backend","too large image resolution ignored"); |
| 591 |
} |
| 592 |
if (((transform - img_rotation(idict)) & 1) == 1) { |
| 593 |
tmp = x; |
| 594 |
x = y; |
| 595 |
y = tmp; |
| 596 |
tmp = xr; |
| 597 |
xr = yr; |
| 598 |
yr = tmp; |
| 599 |
} |
| 600 |
/*tex always for images */ |
| 601 |
if (img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM |
| 602 |
|| img_type(idict) == IMG_TYPE_PDFSTREAM) { |
| 603 |
nat.wd = x; |
| 604 |
nat.ht = y; |
| 605 |
} else { |
| 606 |
default_res = fix_int(pdf_image_resolution, 0, 65535); |
| 607 |
if (default_res > 0 && (xr == 0 || yr == 0)) { |
| 608 |
xr = default_res; |
| 609 |
yr = default_res; |
| 610 |
} |
| 611 |
if (xr > 0 && yr > 0) { |
| 612 |
nat.wd = ext_xn_over_d(one_hundred_inch, x, 100 * xr); |
| 613 |
nat.ht = ext_xn_over_d(one_hundred_inch, y, 100 * yr); |
| 614 |
} else { |
| 615 |
nat.wd = ext_xn_over_d(one_hundred_inch, x, 7200); |
| 616 |
nat.ht = ext_xn_over_d(one_hundred_inch, y, 7200); |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
return tex_scale(nat, alt_rule); |
| 621 |
} |
| 622 |
|
| 623 |
void write_img(PDF pdf, image_dict * idict) |
| 624 |
{ |
| 625 |
if (img_state(idict) < DICT_WRITTEN) { |
| 626 |
report_start_file(filetype_image, img_filepath(idict)); |
| 627 |
switch (img_type(idict)) { |
| 628 |
case IMG_TYPE_PNG: |
| 629 |
write_png(pdf, idict); |
| 630 |
break; |
| 631 |
case IMG_TYPE_JPG: |
| 632 |
write_jpg(pdf, idict); |
| 633 |
break; |
| 634 |
case IMG_TYPE_JP2: |
| 635 |
write_jp2(pdf, idict); |
| 636 |
break; |
| 637 |
case IMG_TYPE_JBIG2: |
| 638 |
write_jbig2(pdf, idict); |
| 639 |
break; |
| 640 |
case IMG_TYPE_PDFMEMSTREAM: |
| 641 |
case IMG_TYPE_PDF: |
| 642 |
write_epdf(pdf, idict,(int) pdf_suppress_optional_info); |
| 643 |
break; |
| 644 |
case IMG_TYPE_PDFSTREAM: |
| 645 |
write_pdfstream(pdf, idict); |
| 646 |
break; |
| 647 |
default: |
| 648 |
normal_error("pdf backend","internal error: writing unknown image type"); |
| 649 |
} |
| 650 |
report_stop_file(filetype_image); |
| 651 |
if (img_type(idict) == IMG_TYPE_PNG) { |
| 652 |
write_additional_png_objects(pdf); |
| 653 |
} |
| 654 |
} |
| 655 |
if (img_state(idict) < DICT_WRITTEN) |
| 656 |
img_state(idict) = DICT_WRITTEN; |
| 657 |
} |
| 658 |
|
| 659 |
int write_img_object(PDF pdf, image_dict * idict, int n) |
| 660 |
{ |
| 661 |
return write_epdf_object(pdf, idict, n); |
| 662 |
} |
| 663 |
|
| 664 |
void pdf_write_image(PDF pdf, int n) |
| 665 |
{ |
| 666 |
if (pdf->draftmode == 0) |
| 667 |
write_img(pdf, idict_array[obj_data_ptr(pdf, n)]); |
| 668 |
} |
| 669 |
|
| 670 |
void check_pdfstream_dict(image_dict * idict) |
| 671 |
{ |
| 672 |
if (!img_is_bbox(idict) && !img_nobbox(idict)) { |
| 673 |
normal_error("pdf backend","image.stream: no bbox given"); |
| 674 |
} |
| 675 |
if (img_state(idict) < DICT_FILESCANNED) { |
| 676 |
img_state(idict) = DICT_FILESCANNED; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
void write_pdfstream(PDF pdf, image_dict * idict) |
| 681 |
{ |
| 682 |
pdf_begin_obj(pdf, img_objnum(idict), OBJSTM_NEVER); |
| 683 |
pdf_begin_dict(pdf); |
| 684 |
if (!img_notype(idict)) { |
| 685 |
pdf_dict_add_name(pdf, "Type", "XObject"); |
| 686 |
pdf_dict_add_name(pdf, "Subtype", "Form"); |
| 687 |
pdf_dict_add_int(pdf, "FormType", 1); |
| 688 |
} |
| 689 |
if (!img_nobbox(idict)) { |
| 690 |
pdf_add_name(pdf, "BBox"); |
| 691 |
pdf_begin_array(pdf); |
| 692 |
pdf_add_real(pdf, sp2bp(img_bbox(idict)[0])); |
| 693 |
pdf_add_real(pdf, sp2bp(img_bbox(idict)[1])); |
| 694 |
pdf_add_real(pdf, sp2bp(img_bbox(idict)[2])); |
| 695 |
pdf_add_real(pdf, sp2bp(img_bbox(idict)[3])); |
| 696 |
pdf_end_array(pdf); |
| 697 |
} |
| 698 |
if (img_attr(idict) != NULL && strlen(img_attr(idict)) > 0) { |
| 699 |
pdf_printf(pdf, "\n%s\n", img_attr(idict)); |
| 700 |
} |
| 701 |
if (!img_nolength(idict)) { |
| 702 |
pdf_dict_add_streaminfo(pdf); |
| 703 |
} |
| 704 |
pdf_end_dict(pdf); |
| 705 |
pdf_begin_stream(pdf); |
| 706 |
if (img_pdfstream_stream(idict) != NULL) { |
| 707 |
pdf_out_block(pdf, (const char *) img_pdfstream_stream(idict), img_pdfstream_size(idict)); |
| 708 |
} |
| 709 |
pdf_end_stream(pdf); |
| 710 |
pdf_end_obj(pdf); |
| 711 |
} |
| 712 |
|
| 713 |
idict_entry *idict_ptr, *idict_array = NULL; |
| 714 |
size_t idict_limit; |
| 715 |
|
| 716 |
void idict_to_array(image_dict * idict) |
| 717 |
{ |
| 718 |
if (idict_ptr - idict_array == 0) { |
| 719 |
/*tex align to count from 1 */ |
| 720 |
alloc_array(idict, 1, SMALL_BUF_SIZE); |
| 721 |
idict_ptr++; |
| 722 |
} |
| 723 |
alloc_array(idict, 1, SMALL_BUF_SIZE); |
| 724 |
*idict_ptr = idict; |
| 725 |
idict_ptr++; |
| 726 |
} |
| 727 |
|
| 728 |
void pdf_dict_add_img_filename(PDF pdf, image_dict * idict) |
| 729 |
{ |
| 730 |
char *p; |
| 731 |
if ((pdf_image_addfilename > 0) && ((pdf_suppress_optional_info & 2) == 0)) { |
| 732 |
/*tex |
| 733 |
For now |PTEX.FileName| is only used for \PDF, but we're prepared |
| 734 |
for \JPG, \PNG, ... |
| 735 |
*/ |
| 736 |
if (! ( (img_type(idict) == IMG_TYPE_PDF) || (img_type(idict) == IMG_TYPE_PDFMEMSTREAM) )) |
| 737 |
return; |
| 738 |
if (img_visiblefilename(idict) != NULL) { |
| 739 |
if (strlen(img_visiblefilename(idict)) == 0) { |
| 740 |
/*tex empty string blocks PTEX.FileName output */ |
| 741 |
return; |
| 742 |
} else { |
| 743 |
p = img_visiblefilename(idict); |
| 744 |
} |
| 745 |
} else { |
| 746 |
/*tex unset so let's use the default */ |
| 747 |
p = img_filepath(idict); |
| 748 |
} |
| 749 |
/*tex write additional information */ |
| 750 |
pdf_add_name(pdf, pdf_pdf_prefix_str("PTEX_FileName", "PTEX.InfoDict")); |
| 751 |
pdf_printf(pdf, " (%s)", convertStringToPDFString(p, strlen(p))); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
/*tex |
| 756 |
|
| 757 |
To allow the use of box resources inside saved boxes in -ini mode, the |
| 758 |
information in the array has to be (un)dumped with the format. The next two |
| 759 |
routines take care of that. |
| 760 |
|
| 761 |
Most of the work involved in setting up the images is simply executed again. This |
| 762 |
solves the many possible errors resulting from the split in two separate runs. |
| 763 |
|
| 764 |
There was only one problem remaining: The |pdfversion| and |
| 765 |
|pdfinclusionerrorlevel| can have changed inbetween the call to |readimage| and |
| 766 |
dump time. |
| 767 |
|
| 768 |
Some of the dumped values are really type int, not integer,but since the macro |
| 769 |
falls back to |generic_dump| anyway, that does not matter. |
| 770 |
|
| 771 |
We might drop this feature as it makes no sense to store images in the format. |
| 772 |
|
| 773 |
*/ |
| 774 |
|
| 775 |
#define dumpinteger generic_dump |
| 776 |
#define undumpinteger generic_undump |
| 777 |
|
| 778 |
/*tex |
| 779 |
|
| 780 |
(Un)dumping a string means dumping the allocation size, followed by the bytes. |
| 781 |
The trailing \.{\\0} is dumped as well, because that makes the code simpler. The |
| 782 |
rule specification ends up in |alt_rule|. |
| 783 |
|
| 784 |
*/ |
| 785 |
|
| 786 |
scaled_whd scan_alt_rule(void) |
| 787 |
{ |
| 788 |
boolean loop; |
| 789 |
scaled_whd alt_rule; |
| 790 |
alt_rule.wd = null_flag; |
| 791 |
alt_rule.ht = null_flag; |
| 792 |
alt_rule.dp = null_flag; |
| 793 |
do { |
| 794 |
loop = false; |
| 795 |
if (scan_keyword("width")) { |
| 796 |
scan_normal_dimen(); |
| 797 |
alt_rule.wd = cur_val; |
| 798 |
loop = true; |
| 799 |
} else if (scan_keyword("height")) { |
| 800 |
scan_normal_dimen(); |
| 801 |
alt_rule.ht = cur_val; |
| 802 |
loop = true; |
| 803 |
} else if (scan_keyword("depth")) { |
| 804 |
scan_normal_dimen(); |
| 805 |
alt_rule.dp = cur_val; |
| 806 |
loop = true; |
| 807 |
} |
| 808 |
} while (loop); |
| 809 |
return alt_rule; |
| 810 |
} |
| 811 |
|
| 812 |
/*tex |
| 813 |
|
| 814 |
This copy a file of arbitrary size to the buffer and flushed as needed. |
| 815 |
|
| 816 |
*/ |
| 817 |
|
| 818 |
size_t read_file_to_buf(PDF pdf, FILE * f, size_t len) |
| 819 |
{ |
| 820 |
size_t i, j, k = 0; |
| 821 |
while (len > 0) { |
| 822 |
i = (size_t) (len > pdf->buf->size) ? (size_t) pdf->buf->size : len; |
| 823 |
pdf_room(pdf, (int) i); |
| 824 |
j = fread(pdf->buf->p, 1, i, f); |
| 825 |
pdf->buf->p += j; |
| 826 |
k += j; |
| 827 |
len -= j; |
| 828 |
if (i != j) |
| 829 |
break; |
| 830 |
} |
| 831 |
return k; |
| 832 |
} |