| 1 |
/* Copyright (C) 2018-2026 Artifex Software, Inc. |
| 2 |
All Rights Reserved. |
| 3 |
|
| 4 |
This software is provided AS-IS with no warranty, either express or |
| 5 |
implied. |
| 6 |
|
| 7 |
This software is distributed under license and may not be copied, |
| 8 |
modified or distributed except as expressly authorized under the terms |
| 9 |
of the license contained in the file LICENSE in this distribution. |
| 10 |
|
| 11 |
Refer to licensing information at http://www.artifex.com or contact |
| 12 |
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
CA 94129, USA, for further information. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* Image operations for the PDF interpreter */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_stack.h" |
| 20 |
#include "pdf_font_types.h" |
| 21 |
#include "pdf_gstate.h" |
| 22 |
#include "pdf_doc.h" |
| 23 |
#include "pdf_page.h" |
| 24 |
#include "pdf_image.h" |
| 25 |
#include "pdf_file.h" |
| 26 |
#include "pdf_dict.h" |
| 27 |
#include "pdf_array.h" |
| 28 |
#include "pdf_loop_detect.h" |
| 29 |
#include "pdf_colour.h" |
| 30 |
#include "pdf_trans.h" |
| 31 |
#include "pdf_misc.h" |
| 32 |
#include "pdf_optcontent.h" |
| 33 |
#include "pdf_mark.h" |
| 34 |
#include "stream.h" /* for stell() */ |
| 35 |
#include "gsicc_cache.h" |
| 36 |
|
| 37 |
#include "gspath2.h" |
| 38 |
#include "gsiparm4.h" |
| 39 |
#include "gsiparm3.h" |
| 40 |
#include "gsipar3x.h" |
| 41 |
#include "gsform1.h" |
| 42 |
#include "gstrans.h" |
| 43 |
#include "gxdevsop.h" /* For special ops */ |
| 44 |
#include "gspath.h" /* For gs_moveto() and friends */ |
| 45 |
#include "gsstate.h" /* For gs_setoverprintmode() */ |
| 46 |
#include "gscoord.h" /* for gs_concat() and others */ |
| 47 |
#include "gxgstate.h" |
| 48 |
|
| 49 |
int pdfi_BI(pdf_context *ctx) |
| 50 |
{ |
| 51 |
int code; |
| 52 |
|
| 53 |
if (ctx->text.BlockDepth != 0) { |
| 54 |
ctx->text.BlockDepth = 0; |
| 55 |
if (ctx->text.TextClip) { |
| 56 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 57 |
|
| 58 |
ctx->text.TextClip = false; |
| 59 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 60 |
} |
| 61 |
code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_BI", NULL); |
| 62 |
if (code < 0) |
| 63 |
return code; |
| 64 |
} |
| 65 |
|
| 66 |
return pdfi_mark_stack(ctx, PDF_DICT_MARK); |
| 67 |
} |
| 68 |
|
| 69 |
typedef struct { |
| 70 |
int comps; |
| 71 |
int bpc; |
| 72 |
uint32_t cs_enum; |
| 73 |
bool iccbased; |
| 74 |
bool no_data; |
| 75 |
bool is_valid; |
| 76 |
uint32_t icc_offset; |
| 77 |
uint32_t icc_length; |
| 78 |
} pdfi_jpx_info_t; |
| 79 |
|
| 80 |
typedef struct { |
| 81 |
/* Type and SubType were already checked by caller */ |
| 82 |
/* OPI, Metadata -- do we care? */ |
| 83 |
bool ImageMask; |
| 84 |
bool Interpolate; |
| 85 |
int64_t Length; |
| 86 |
int64_t Height; |
| 87 |
int64_t Width; |
| 88 |
int64_t BPC; |
| 89 |
int64_t StructParent; |
| 90 |
int64_t SMaskInData; |
| 91 |
pdf_obj *Mask; |
| 92 |
pdf_obj *SMask; |
| 93 |
pdf_obj *ColorSpace; |
| 94 |
pdf_name *Intent; |
| 95 |
pdf_obj *Alternates; |
| 96 |
pdf_obj *Name; /* obsolete, do we still support? */ |
| 97 |
pdf_obj *Decode; |
| 98 |
pdf_dict *OC; /* Optional Content */ |
| 99 |
/* Filter and DecodeParms handled by pdfi_filter() (can probably remove, but I like the info while debugging) */ |
| 100 |
bool is_JPXDecode; |
| 101 |
pdf_obj *Filter; |
| 102 |
pdf_obj *DecodeParms; |
| 103 |
|
| 104 |
/* Convenience variables (save these here instead of passing around as params) */ |
| 105 |
pdf_dict *page_dict; |
| 106 |
pdf_dict *stream_dict; |
| 107 |
bool inline_image; |
| 108 |
pdfi_jpx_info_t jpx_info; |
| 109 |
} pdfi_image_info_t; |
| 110 |
|
| 111 |
static void |
| 112 |
pdfi_free_image_info_components(pdfi_image_info_t *info) |
| 113 |
{ |
| 114 |
if (info->Mask) |
| 115 |
pdfi_countdown(info->Mask); |
| 116 |
if (info->SMask) |
| 117 |
pdfi_countdown(info->SMask); |
| 118 |
if (info->ColorSpace) |
| 119 |
pdfi_countdown(info->ColorSpace); |
| 120 |
if (info->Intent) |
| 121 |
pdfi_countdown(info->Intent); |
| 122 |
if (info->Alternates) |
| 123 |
pdfi_countdown(info->Alternates); |
| 124 |
if (info->Name) |
| 125 |
pdfi_countdown(info->Name); |
| 126 |
if (info->Decode) |
| 127 |
pdfi_countdown(info->Decode); |
| 128 |
if (info->OC) |
| 129 |
pdfi_countdown(info->OC); |
| 130 |
if (info->Filter) |
| 131 |
pdfi_countdown(info->Filter); |
| 132 |
if (info->DecodeParms) |
| 133 |
pdfi_countdown(info->DecodeParms); |
| 134 |
memset(info, 0, sizeof(*info)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static inline uint64_t |
| 139 |
pdfi_get_image_data_size(gs_data_image_t *pim, int comps) |
| 140 |
{ |
| 141 |
int64_t size, H, W, B; |
| 142 |
|
| 143 |
H = pim->Height; |
| 144 |
W = pim->Width; |
| 145 |
B = pim->BitsPerComponent; |
| 146 |
|
| 147 |
size = (((W * comps * B) + 7) / 8) * H; |
| 148 |
return size; |
| 149 |
} |
| 150 |
|
| 151 |
/* Find first dictionary in array that contains "/DefaultForPrinting true" */ |
| 152 |
static pdf_stream * |
| 153 |
pdfi_find_alternate(pdf_context *ctx, pdf_obj *alt) |
| 154 |
{ |
| 155 |
pdf_array *array = NULL; |
| 156 |
pdf_obj *item = NULL; |
| 157 |
pdf_stream *alt_stream = NULL; |
| 158 |
int i; |
| 159 |
int code; |
| 160 |
bool flag; |
| 161 |
|
| 162 |
if (pdfi_type_of(alt) != PDF_ARRAY) |
| 163 |
return NULL; |
| 164 |
|
| 165 |
array = (pdf_array *)alt; |
| 166 |
for (i=0; i<pdfi_array_size(array);i++) { |
| 167 |
code = pdfi_array_get_type(ctx, array, (uint64_t)i, PDF_DICT, &item); |
| 168 |
if (code != 0) |
| 169 |
continue; |
| 170 |
code = pdfi_dict_get_bool(ctx, (pdf_dict *)item, "DefaultForPrinting", &flag); |
| 171 |
if (code != 0 || !flag) { |
| 172 |
pdfi_countdown(item); |
| 173 |
item = NULL; |
| 174 |
continue; |
| 175 |
} |
| 176 |
code = pdfi_dict_get_type(ctx, (pdf_dict *)item, "Image", PDF_STREAM, (pdf_obj **)&alt_stream); |
| 177 |
pdfi_countdown(item); |
| 178 |
item = NULL; |
| 179 |
if (code != 0) |
| 180 |
continue; |
| 181 |
return alt_stream; |
| 182 |
} |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
|
| 186 |
#define READ32BE(i) (((i)[0] << 24) | ((i)[1] << 16) | ((i)[2] << 8) | (i)[3]) |
| 187 |
#define READ16BE(i) (((i)[0] << 8) | (i)[1]) |
| 188 |
#define K4(a, b, c, d) ((a << 24) + (b << 16) + (c << 8) + d) |
| 189 |
#define LEN_IHDR 14 |
| 190 |
#define LEN_DATA 2048 |
| 191 |
|
| 192 |
/* Returns either < 0, or exactly 8 */ |
| 193 |
static int |
| 194 |
get_box(pdf_context *ctx, pdf_c_stream *source, int length, uint32_t *box_len, uint32_t *box_val) |
| 195 |
{ |
| 196 |
int code; |
| 197 |
byte blob[4]; |
| 198 |
|
| 199 |
if (length < 8) |
| 200 |
return_error(gs_error_limitcheck); |
| 201 |
code = pdfi_read_bytes(ctx, blob, 1, 4, source); |
| 202 |
if (code < 0) |
| 203 |
return code; |
| 204 |
*box_len = READ32BE(blob); |
| 205 |
if (*box_len < 8) |
| 206 |
return_error(gs_error_limitcheck); |
| 207 |
code = pdfi_read_bytes(ctx, blob, 1, 4, source); |
| 208 |
if (code < 0) |
| 209 |
return code; |
| 210 |
*box_val = READ32BE(blob); |
| 211 |
|
| 212 |
if(ctx->args.pdfdebug) |
| 213 |
dbgmprintf3(ctx->memory, "JPXFilter: BOX: l:%d, v:%x (%4.4s)\n", *box_len, *box_val, blob); |
| 214 |
return 8; |
| 215 |
} |
| 216 |
|
| 217 |
/* Scan JPX image for header info */ |
| 218 |
static int |
| 219 |
pdfi_scan_jpxfilter(pdf_context *ctx, pdf_c_stream *source, int length, pdfi_jpx_info_t *info) |
| 220 |
{ |
| 221 |
uint32_t box_len = 0; |
| 222 |
uint32_t box_val = 0; |
| 223 |
int code; |
| 224 |
byte ihdr_data[LEN_IHDR]; |
| 225 |
byte *data = NULL; |
| 226 |
int data_buf_len = 0; |
| 227 |
int avail = length; |
| 228 |
int bpc = 0; |
| 229 |
int comps = 0; |
| 230 |
int cs_meth = 0; |
| 231 |
uint32_t cs_enum = 0; |
| 232 |
bool got_color = false; |
| 233 |
|
| 234 |
if (ctx->args.pdfdebug) |
| 235 |
dbgmprintf1(ctx->memory, "JPXFilter: Image length %d\n", length); |
| 236 |
|
| 237 |
/* Clear out the info param */ |
| 238 |
memset(info, 0, sizeof(pdfi_jpx_info_t)); |
| 239 |
|
| 240 |
info->no_data = false; |
| 241 |
|
| 242 |
/* Allocate a data buffer that hopefully is big enough */ |
| 243 |
data_buf_len = LEN_DATA; |
| 244 |
data = gs_alloc_bytes(ctx->memory, data_buf_len, "pdfi_scan_jpxfilter (data)"); |
| 245 |
if (!data) { |
| 246 |
code = gs_note_error(gs_error_VMerror); |
| 247 |
goto exit; |
| 248 |
} |
| 249 |
|
| 250 |
/* Find the 'jp2h' box, skipping over everything else */ |
| 251 |
while (avail > 0) { |
| 252 |
code = get_box(ctx, source, avail, &box_len, &box_val); |
| 253 |
if (code < 0) |
| 254 |
goto exit; |
| 255 |
avail -= 8; |
| 256 |
box_len -= 8; |
| 257 |
if (box_len <= 0 || box_len > avail) { |
| 258 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_INVALID_JPX_HDR, "pdfi_scan_jpxfilter", NULL); |
| 259 |
goto exit; |
| 260 |
} |
| 261 |
if (box_val == K4('j','p','2','h')) { |
| 262 |
break; |
| 263 |
} |
| 264 |
pdfi_seek(ctx, source, box_len, SEEK_CUR); |
| 265 |
avail -= box_len; |
| 266 |
} |
| 267 |
if (avail <= 0) { |
| 268 |
info->no_data = true; |
| 269 |
code = gs_note_error(gs_error_ioerror); |
| 270 |
goto exit; |
| 271 |
} |
| 272 |
|
| 273 |
/* Now we are only looking inside the jp2h box */ |
| 274 |
avail = box_len; |
| 275 |
|
| 276 |
/* The first thing in the 'jp2h' box is an 'ihdr', get that */ |
| 277 |
code = get_box(ctx, source, avail, &box_len, &box_val); |
| 278 |
if (code < 0) |
| 279 |
goto exit; |
| 280 |
avail -= 8; |
| 281 |
box_len -= 8; |
| 282 |
if (box_val != K4('i','h','d','r')) { |
| 283 |
code = gs_note_error(gs_error_syntaxerror); |
| 284 |
goto exit; |
| 285 |
} |
| 286 |
if (box_len != LEN_IHDR) { |
| 287 |
code = gs_note_error(gs_error_syntaxerror); |
| 288 |
goto exit; |
| 289 |
} |
| 290 |
|
| 291 |
/* Get things we care about from ihdr */ |
| 292 |
code = pdfi_read_bytes(ctx, ihdr_data, 1, LEN_IHDR, source); |
| 293 |
if (code < 0) |
| 294 |
goto exit; |
| 295 |
avail -= LEN_IHDR; |
| 296 |
comps = READ16BE(ihdr_data+8); |
| 297 |
if (ctx->args.pdfdebug) |
| 298 |
dbgmprintf1(ctx->memory, " COMPS: %d\n", comps); |
| 299 |
bpc = ihdr_data[10]; |
| 300 |
if (bpc != 255) |
| 301 |
bpc += 1; |
| 302 |
if (ctx->args.pdfdebug) |
| 303 |
dbgmprintf1(ctx->memory, " BPC: %d\n", bpc); |
| 304 |
|
| 305 |
/* Parse the rest of the things */ |
| 306 |
while (avail > 0) { |
| 307 |
code = get_box(ctx, source, avail, &box_len, &box_val); |
| 308 |
if (code < 0) |
| 309 |
goto exit; |
| 310 |
avail -= 8; |
| 311 |
box_len -= 8; |
| 312 |
if (box_len <= 0) { |
| 313 |
code = gs_note_error(gs_error_syntaxerror); |
| 314 |
goto exit; |
| 315 |
} |
| 316 |
/* Re-alloc buffer if it wasn't big enough (unlikely) */ |
| 317 |
if (box_len > data_buf_len) { |
| 318 |
if (ctx->args.pdfdebug) |
| 319 |
dbgmprintf2(ctx->memory, "data buffer (size %d) was too small, reallocing to size %d\n", |
| 320 |
data_buf_len, box_len); |
| 321 |
gs_free_object(ctx->memory, data, "pdfi_scan_jpxfilter (data)"); |
| 322 |
data_buf_len = box_len; |
| 323 |
data = gs_alloc_bytes(ctx->memory, data_buf_len, "pdfi_scan_jpxfilter (data)"); |
| 324 |
if (!data) { |
| 325 |
code = gs_note_error(gs_error_VMerror); |
| 326 |
goto exit; |
| 327 |
} |
| 328 |
} |
| 329 |
code = pdfi_read_bytes(ctx, data, 1, box_len, source); |
| 330 |
if (code < 0) |
| 331 |
goto exit; |
| 332 |
avail -= box_len; |
| 333 |
switch(box_val) { |
| 334 |
case K4('b','p','c','c'): |
| 335 |
{ |
| 336 |
int i; |
| 337 |
int bpc2; |
| 338 |
|
| 339 |
bpc2 = data[0]; |
| 340 |
for (i=1;i<comps;i++) { |
| 341 |
if (bpc2 != data[i]) { |
| 342 |
emprintf(ctx->memory, |
| 343 |
"*** Error: JPX image colour channels do not all have the same colour depth\n"); |
| 344 |
emprintf(ctx->memory, |
| 345 |
" Output may be incorrect.\n"); |
| 346 |
} |
| 347 |
} |
| 348 |
bpc = bpc2+1; |
| 349 |
if (ctx->args.pdfdebug) |
| 350 |
dbgmprintf1(ctx->memory, " BPCC: %d\n", bpc); |
| 351 |
} |
| 352 |
break; |
| 353 |
case K4('c','o','l','r'): |
| 354 |
if (got_color) { |
| 355 |
if (ctx->args.pdfdebug) |
| 356 |
dbgmprintf(ctx->memory, "JPXFilter: Ignore extra COLR specs\n"); |
| 357 |
break; |
| 358 |
} |
| 359 |
cs_meth = data[0]; |
| 360 |
if (cs_meth == 1) |
| 361 |
cs_enum = READ32BE(data+3); |
| 362 |
else if (cs_meth == 2 || cs_meth == 3) { |
| 363 |
/* This is an ICCBased color space just sitting there in the buffer. |
| 364 |
* TODO: I could create the colorspace now while I have the buffer, |
| 365 |
* but code flow is more consistent if I do it later. Could change this. |
| 366 |
* |
| 367 |
* NOTE: cs_meth == 3 is apparently treated the same as 2. |
| 368 |
* No idea why... it's really not documented anywhere. |
| 369 |
*/ |
| 370 |
info->iccbased = true; |
| 371 |
info->icc_offset = pdfi_tell(source) - (box_len-3); |
| 372 |
info->icc_length = box_len - 3; |
| 373 |
if (ctx->args.pdfdebug) |
| 374 |
dbgmprintf5(ctx->memory, "JPXDecode: COLR Meth %d at offset %d(0x%x), length %d(0x%x)\n", |
| 375 |
cs_meth, info->icc_offset, info->icc_offset, |
| 376 |
info->icc_length, info->icc_length); |
| 377 |
cs_enum = 0; |
| 378 |
} else { |
| 379 |
if (ctx->args.pdfdebug) |
| 380 |
dbgmprintf1(ctx->memory, "JPXDecode: COLR unexpected method %d\n", cs_meth); |
| 381 |
cs_enum = 0; |
| 382 |
} |
| 383 |
if (ctx->args.pdfdebug) |
| 384 |
dbgmprintf2(ctx->memory, " COLR: M:%d, ENUM:%d\n", cs_meth, cs_enum); |
| 385 |
got_color = true; |
| 386 |
break; |
| 387 |
case K4('p','c','l','r'): |
| 388 |
/* Apparently we just grab the BPC out of this */ |
| 389 |
if (ctx->args.pdfdebug) |
| 390 |
dbgmprintf7(ctx->memory, " PCLR Data: %x %x %x %x %x %x %x\n", |
| 391 |
data[0], data[1], data[2], data[3], data[4], data[5], data[6]); |
| 392 |
bpc = data[3]; |
| 393 |
bpc = (bpc & 0x7) + 1; |
| 394 |
if (ctx->args.pdfdebug) |
| 395 |
dbgmprintf1(ctx->memory, " PCLR BPC: %d\n", bpc); |
| 396 |
break; |
| 397 |
case K4('c','d','e','f'): |
| 398 |
dbgmprintf(ctx->memory, "JPXDecode: CDEF not supported yet\n"); |
| 399 |
break; |
| 400 |
default: |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
info->comps = comps; |
| 407 |
info->bpc = bpc; |
| 408 |
info->cs_enum = cs_enum; |
| 409 |
info->is_valid = true; |
| 410 |
|
| 411 |
exit: |
| 412 |
if (data) |
| 413 |
gs_free_object(ctx->memory, data, "pdfi_scan_jpxfilter (data)"); |
| 414 |
/* Always return 0 -- there are cases where this no image header at all, and just ignoring |
| 415 |
* the header seems to work. In this case is_valid will be false, so we know not to rely on |
| 416 |
* the data from it. |
| 417 |
* Sample: tests_private/comparefiles/Bug694873.pdf |
| 418 |
*/ |
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
/* Get image info out of dict into more convenient form, enforcing some requirements from the spec */ |
| 423 |
static int |
| 424 |
pdfi_get_image_info(pdf_context *ctx, pdf_stream *image_obj, |
| 425 |
pdf_dict *page_dict, pdf_dict *stream_dict, bool inline_image, |
| 426 |
pdfi_image_info_t *info) |
| 427 |
{ |
| 428 |
int code; |
| 429 |
double temp_f; |
| 430 |
pdf_dict *image_dict = NULL; |
| 431 |
|
| 432 |
memset(info, 0, sizeof(*info)); |
| 433 |
info->page_dict = page_dict; |
| 434 |
info->stream_dict = stream_dict; |
| 435 |
info->inline_image = inline_image; |
| 436 |
|
| 437 |
/* Not Handled: "ID", "OPI" */ |
| 438 |
|
| 439 |
/* Length if it's in a stream dict */ |
| 440 |
info->Length = pdfi_stream_length(ctx, image_obj); |
| 441 |
|
| 442 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)image_obj, &image_dict); |
| 443 |
if (code < 0) |
| 444 |
goto errorExit; |
| 445 |
|
| 446 |
/* Required */ |
| 447 |
code = pdfi_dict_get_number2(ctx, image_dict, "Height", "H", &temp_f); |
| 448 |
if (code < 0) |
| 449 |
goto errorExit; |
| 450 |
/* This is bonkers, but... Bug695872.pdf has /W and /H which are real numbers */ |
| 451 |
info->Height = (int)temp_f; |
| 452 |
if ((int)temp_f != (int)(temp_f+.5)) { |
| 453 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 454 |
goto errorExit; |
| 455 |
} |
| 456 |
if (info->Height < 0) { |
| 457 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 458 |
goto errorExit; |
| 459 |
info->Height = 0; |
| 460 |
} |
| 461 |
|
| 462 |
/* Required */ |
| 463 |
code = pdfi_dict_get_number2(ctx, image_dict, "Width", "W", &temp_f); |
| 464 |
if (code < 0) |
| 465 |
goto errorExit; |
| 466 |
info->Width = (int)temp_f; |
| 467 |
if ((int)temp_f != (int)(temp_f+.5)) { |
| 468 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 469 |
goto errorExit; |
| 470 |
} |
| 471 |
if (info->Width < 0) { |
| 472 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 473 |
goto errorExit; |
| 474 |
info->Width = 0; |
| 475 |
} |
| 476 |
|
| 477 |
/* Optional, default false */ |
| 478 |
code = pdfi_dict_get_bool2(ctx, image_dict, "ImageMask", "IM", &info->ImageMask); |
| 479 |
if (code != 0) { |
| 480 |
if (code != gs_error_undefined) |
| 481 |
goto errorExit; |
| 482 |
info->ImageMask = false; |
| 483 |
} |
| 484 |
|
| 485 |
/* Optional, default false */ |
| 486 |
code = pdfi_dict_get_bool2(ctx, image_dict, "Interpolate", "I", &info->Interpolate); |
| 487 |
if (code != 0) { |
| 488 |
if (code != gs_error_undefined) |
| 489 |
goto errorExit; |
| 490 |
info->Interpolate = false; |
| 491 |
} |
| 492 |
|
| 493 |
/* Optional (Required, unless ImageMask is true) |
| 494 |
* But apparently for JPXDecode filter, this can be omitted. |
| 495 |
* Let's try a default of 1 for now... |
| 496 |
*/ |
| 497 |
code = pdfi_dict_get_int2(ctx, image_dict, "BitsPerComponent", "BPC", &info->BPC); |
| 498 |
if (code < 0) { |
| 499 |
if (code != gs_error_undefined) { |
| 500 |
goto errorExit; |
| 501 |
} |
| 502 |
info->BPC = 1; |
| 503 |
} |
| 504 |
else if (info->BPC != 1 && info->BPC != 2 && info->BPC != 4 && info->BPC != 8 && info->BPC != 16) { |
| 505 |
code = gs_note_error(gs_error_rangecheck); |
| 506 |
goto errorExit; |
| 507 |
} |
| 508 |
/* TODO: spec says if ImageMask is specified, and BPC is specified, then BPC must be 1 |
| 509 |
Should we flag an error if this is violated? |
| 510 |
*/ |
| 511 |
|
| 512 |
/* Optional (apparently there is no "M" abbreviation for "Mask"? */ |
| 513 |
code = pdfi_dict_get(ctx, image_dict, "Mask", &info->Mask); |
| 514 |
if (code < 0) { |
| 515 |
/* A lack of a Mask is not an error. If there is a genuine error reading |
| 516 |
* the Mask, ignore it unless PDFSTOPONWARNING is set. We can still render |
| 517 |
* the image. Arguably we should not, and Acrobat doesn't, but the current |
| 518 |
* GS implementation does. |
| 519 |
*/ |
| 520 |
if (code != gs_error_undefined) { |
| 521 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 522 |
goto errorExit; |
| 523 |
} |
| 524 |
} |
| 525 |
if (info->Mask != NULL && (pdfi_type_of(info->Mask) != PDF_ARRAY && pdfi_type_of(info->Mask) != PDF_STREAM)) { |
| 526 |
pdfi_countdown(info->Mask); |
| 527 |
info->Mask = NULL; |
| 528 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 529 |
goto errorExit; |
| 530 |
} |
| 531 |
|
| 532 |
/* Optional (apparently there is no abbreviation for "SMask"? */ |
| 533 |
code = pdfi_dict_get(ctx, image_dict, "SMask", &info->SMask); |
| 534 |
if (code < 0) { |
| 535 |
if (code != gs_error_undefined) { |
| 536 |
/* Broken SMask, Warn, and ignore the SMask */ |
| 537 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", (char *)"*** Warning: Image has invalid SMask. Ignoring it")) < 0) |
| 538 |
goto errorExit; |
| 539 |
} |
| 540 |
} else { |
| 541 |
if (pdfi_type_of(info->SMask) == PDF_NAME) { |
| 542 |
pdf_obj *o = NULL; |
| 543 |
|
| 544 |
code = pdfi_find_resource(ctx, (unsigned char *)"ExtGState", (pdf_name *)info->SMask, image_dict, page_dict, &o); |
| 545 |
if (code >= 0) { |
| 546 |
pdfi_countdown(info->SMask); |
| 547 |
info->SMask = o; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (pdfi_type_of(info->SMask) != PDF_STREAM){ |
| 552 |
pdfi_countdown(info->SMask); |
| 553 |
info->SMask = NULL; |
| 554 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 555 |
goto errorExit; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
/* Optional, for JPXDecode filter images |
| 560 |
* (If non-zero, then SMask shouldn't be specified) |
| 561 |
* Default: 0 |
| 562 |
*/ |
| 563 |
code = pdfi_dict_get_int(ctx, image_dict, "SMaskInData", &info->SMaskInData); |
| 564 |
if (code < 0) { |
| 565 |
if (code != gs_error_undefined) |
| 566 |
goto errorExit; |
| 567 |
info->SMaskInData = 0; |
| 568 |
} |
| 569 |
|
| 570 |
/* Optional (Required except for ImageMask, not allowed for ImageMask)*/ |
| 571 |
/* TODO: Should we enforce this required/not allowed thing? */ |
| 572 |
code = pdfi_dict_get2(ctx, image_dict, "ColorSpace", "CS", &info->ColorSpace); |
| 573 |
if (code < 0) { |
| 574 |
if (code != gs_error_undefined) |
| 575 |
goto errorExit; |
| 576 |
} |
| 577 |
if (info->ColorSpace != NULL && (pdfi_type_of(info->ColorSpace) != PDF_NAME && pdfi_type_of(info->ColorSpace) != PDF_ARRAY)) { |
| 578 |
pdfi_countdown(info->ColorSpace); |
| 579 |
info->ColorSpace = NULL; |
| 580 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 581 |
goto errorExit; |
| 582 |
} |
| 583 |
|
| 584 |
/* Optional (default is to use from graphics state) */ |
| 585 |
/* (no abbreviation for inline) */ |
| 586 |
code = pdfi_dict_get_type(ctx, image_dict, "Intent", PDF_NAME, (pdf_obj **)&info->Intent); |
| 587 |
if (code < 0) { |
| 588 |
if (code != gs_error_undefined) |
| 589 |
goto errorExit; |
| 590 |
} |
| 591 |
|
| 592 |
/* Optional (array of alternate image dicts, can't be nested) */ |
| 593 |
code = pdfi_dict_get(ctx, image_dict, "Alternates", &info->Alternates); |
| 594 |
if (code < 0) { |
| 595 |
if (code != gs_error_undefined) |
| 596 |
goto errorExit; |
| 597 |
} |
| 598 |
if (info->Alternates != NULL && pdfi_type_of(info->Alternates) != PDF_ARRAY) { |
| 599 |
pdfi_countdown(info->Alternates); |
| 600 |
info->Alternates = NULL; |
| 601 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 602 |
goto errorExit; |
| 603 |
} |
| 604 |
|
| 605 |
/* Optional (required in PDF1.0, obsolete, do we support?) */ |
| 606 |
code = pdfi_dict_get(ctx, image_dict, "Name", &info->Name); |
| 607 |
if (code < 0) { |
| 608 |
if (code != gs_error_undefined) |
| 609 |
goto errorExit; |
| 610 |
} |
| 611 |
if (info->Name != NULL && pdfi_type_of(info->Name) != PDF_NAME) { |
| 612 |
pdfi_countdown(info->Name); |
| 613 |
info->Name = NULL; |
| 614 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGENAME, "pdfi_get_image_info", NULL)) < 0) |
| 615 |
goto errorExit; |
| 616 |
} |
| 617 |
|
| 618 |
/* Required "if image is structural content item" */ |
| 619 |
/* TODO: Figure out what to do here */ |
| 620 |
code = pdfi_dict_get_int(ctx, image_dict, "StructParent", &info->StructParent); |
| 621 |
if (code < 0) { |
| 622 |
if (code != gs_error_undefined) |
| 623 |
goto errorExit; |
| 624 |
} |
| 625 |
|
| 626 |
/* Optional (default is probably [0,1] per component) */ |
| 627 |
code = pdfi_dict_get2(ctx, image_dict, "Decode", "D", &info->Decode); |
| 628 |
if (code < 0) { |
| 629 |
if (code != gs_error_undefined) |
| 630 |
goto errorExit; |
| 631 |
} |
| 632 |
if (info->Decode != NULL && pdfi_type_of(info->Decode) != PDF_ARRAY) { |
| 633 |
pdfi_countdown(info->Decode); |
| 634 |
info->Decode = NULL; |
| 635 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 636 |
goto errorExit; |
| 637 |
} |
| 638 |
|
| 639 |
/* Optional "Optional Content" */ |
| 640 |
code = pdfi_dict_get_type(ctx, image_dict, "OC", PDF_DICT, (pdf_obj **)&info->OC); |
| 641 |
if (code < 0) { |
| 642 |
if (code != gs_error_undefined) |
| 643 |
goto errorExit; |
| 644 |
} |
| 645 |
|
| 646 |
/* Optional */ |
| 647 |
code = pdfi_dict_get2(ctx, image_dict, "Filter", "F", &info->Filter); |
| 648 |
if (code < 0) { |
| 649 |
if (code != gs_error_undefined) |
| 650 |
goto errorExit; |
| 651 |
} |
| 652 |
if (info->Filter != NULL && (pdfi_type_of(info->Filter) != PDF_NAME && pdfi_type_of(info->Filter) != PDF_ARRAY)) { |
| 653 |
pdfi_countdown(info->Filter); |
| 654 |
info->Filter = NULL; |
| 655 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 656 |
goto errorExit; |
| 657 |
} |
| 658 |
|
| 659 |
/* Check and set JPXDecode flag for later */ |
| 660 |
info->is_JPXDecode = false; |
| 661 |
if (info->Filter && pdfi_type_of(info->Filter) == PDF_NAME) { |
| 662 |
if (pdfi_name_is((pdf_name *)info->Filter, "JPXDecode")) |
| 663 |
info->is_JPXDecode = true; |
| 664 |
} |
| 665 |
|
| 666 |
/* Optional */ |
| 667 |
code = pdfi_dict_get2(ctx, image_dict, "DecodeParms", "DP", &info->DecodeParms); |
| 668 |
if (code < 0) { |
| 669 |
if (code != gs_error_undefined) |
| 670 |
goto errorExit; |
| 671 |
} |
| 672 |
if (info->DecodeParms != NULL && (pdfi_type_of(info->DecodeParms) != PDF_DICT && pdfi_type_of(info->DecodeParms) != PDF_ARRAY)) { |
| 673 |
/* null is legal. Pointless but legal. */ |
| 674 |
if (pdfi_type_of(info->DecodeParms) != PDF_NULL) { |
| 675 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_get_image_info", NULL)) < 0) |
| 676 |
goto errorExit; |
| 677 |
} |
| 678 |
pdfi_countdown(info->DecodeParms); |
| 679 |
info->DecodeParms = NULL; |
| 680 |
} |
| 681 |
|
| 682 |
return 0; |
| 683 |
|
| 684 |
errorExit: |
| 685 |
pdfi_free_image_info_components(info); |
| 686 |
return code; |
| 687 |
} |
| 688 |
|
| 689 |
static int pdfi_check_inline_image_keys(pdf_context *ctx, pdf_dict *image_dict) |
| 690 |
{ |
| 691 |
bool known = false; |
| 692 |
|
| 693 |
pdfi_dict_known(ctx, image_dict, "BPC", &known); |
| 694 |
if (known) |
| 695 |
goto error_inline_check; |
| 696 |
pdfi_dict_known(ctx, image_dict, "CS", &known); |
| 697 |
if (known) |
| 698 |
goto error_inline_check; |
| 699 |
pdfi_dict_known(ctx, image_dict, "D", &known); |
| 700 |
if (known) |
| 701 |
goto error_inline_check; |
| 702 |
pdfi_dict_known(ctx, image_dict, "DP", &known); |
| 703 |
if (known) |
| 704 |
goto error_inline_check; |
| 705 |
pdfi_dict_known(ctx, image_dict, "F", &known); |
| 706 |
if (known) |
| 707 |
goto error_inline_check; |
| 708 |
pdfi_dict_known(ctx, image_dict, "H", &known); |
| 709 |
if (known) |
| 710 |
goto error_inline_check; |
| 711 |
pdfi_dict_known(ctx, image_dict, "IM", &known); |
| 712 |
if (known) |
| 713 |
goto error_inline_check; |
| 714 |
pdfi_dict_known(ctx, image_dict, "I", &known); |
| 715 |
if (known) |
| 716 |
goto error_inline_check; |
| 717 |
pdfi_dict_known(ctx, image_dict, "W", &known); |
| 718 |
if (known) |
| 719 |
goto error_inline_check; |
| 720 |
|
| 721 |
return 0; |
| 722 |
|
| 723 |
error_inline_check: |
| 724 |
return pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEIMAGEKEY, "pdfi_check_inline_image_keys", NULL); |
| 725 |
} |
| 726 |
|
| 727 |
/* Render a PDF image |
| 728 |
* pim can be type1 (or imagemask), type3, type4 |
| 729 |
*/ |
| 730 |
static int |
| 731 |
pdfi_render_image(pdf_context *ctx, gs_pixel_image_t *pim, pdf_c_stream *image_stream, |
| 732 |
unsigned char *mask_buffer, uint64_t mask_size, |
| 733 |
int comps, bool ImageMask) |
| 734 |
{ |
| 735 |
int code; |
| 736 |
gs_image_enum *penum = NULL; |
| 737 |
uint64_t bytes_left; |
| 738 |
uint64_t bytes_used = 0; |
| 739 |
uint64_t bytes_avail = 0; |
| 740 |
gs_const_string plane_data[GS_IMAGE_MAX_COMPONENTS]; |
| 741 |
int main_plane=0, mask_plane=0; |
| 742 |
bool no_progress = false; |
| 743 |
int min_left; |
| 744 |
|
| 745 |
#if DEBUG_IMAGES |
| 746 |
dbgmprintf(ctx->memory, "pdfi_render_image BEGIN\n"); |
| 747 |
#endif |
| 748 |
|
| 749 |
code = pdfi_gsave(ctx); |
| 750 |
if (code < 0) |
| 751 |
return code; |
| 752 |
|
| 753 |
/* Disable overprint mode for images that are not a mask */ |
| 754 |
if (!ImageMask) |
| 755 |
gs_setoverprintmode(ctx->pgs, 0); |
| 756 |
|
| 757 |
penum = gs_image_enum_alloc(ctx->memory, "pdfi_render_image (gs_image_enum_alloc)"); |
| 758 |
if (!penum) { |
| 759 |
code = gs_note_error(gs_error_VMerror); |
| 760 |
goto cleanupExit; |
| 761 |
} |
| 762 |
|
| 763 |
/* Took this logic from gs_image_init() |
| 764 |
* (the other tests in there have already been handled elsewhere) |
| 765 |
*/ |
| 766 |
{ |
| 767 |
gx_image_enum_common_t *pie; |
| 768 |
|
| 769 |
if (!ImageMask) { |
| 770 |
/* TODO: Can in_cachedevice ever be set in PDF? */ |
| 771 |
if (ctx->pgs->in_cachedevice != CACHE_DEVICE_NONE) { |
| 772 |
code = gs_note_error(gs_error_undefined); |
| 773 |
goto cleanupExit; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
code = gs_image_begin_typed((const gs_image_common_t *)pim, ctx->pgs, ImageMask, false, &pie); |
| 778 |
if (code < 0) |
| 779 |
goto cleanupExit; |
| 780 |
|
| 781 |
code = gs_image_enum_init(penum, pie, (const gs_data_image_t *)pim, ctx->pgs); |
| 782 |
if (code < 0) |
| 783 |
goto cleanupExit; |
| 784 |
} |
| 785 |
|
| 786 |
/* NOTE: I used image_file_continue() as my template for this code. |
| 787 |
* But this case is (hopefully) much much simpler. |
| 788 |
* We only handle two situations -- if there is mask_data, then we assume there are two planes. |
| 789 |
* If no mask_data, then there is one plane. |
| 790 |
*/ |
| 791 |
if (mask_buffer) { |
| 792 |
main_plane = 1; |
| 793 |
mask_plane = 0; |
| 794 |
plane_data[mask_plane].data = mask_buffer; |
| 795 |
plane_data[mask_plane].size = mask_size; |
| 796 |
} else { |
| 797 |
main_plane = 0; |
| 798 |
} |
| 799 |
|
| 800 |
bytes_left = pdfi_get_image_data_size((gs_data_image_t *)pim, comps); |
| 801 |
while (bytes_left > 0) { |
| 802 |
uint used[GS_IMAGE_MAX_COMPONENTS]; |
| 803 |
|
| 804 |
while ((bytes_avail = sbufavailable(image_stream->s)) <= (min_left = sbuf_min_left(image_stream->s))) { |
| 805 |
switch (image_stream->s->end_status) { |
| 806 |
case 0: |
| 807 |
s_process_read_buf(image_stream->s); |
| 808 |
continue; |
| 809 |
case EOFC: |
| 810 |
case INTC: |
| 811 |
case CALLC: |
| 812 |
break; |
| 813 |
default: |
| 814 |
/* case ERRC: */ |
| 815 |
code = gs_note_error(gs_error_ioerror); |
| 816 |
goto cleanupExit; |
| 817 |
} |
| 818 |
break; /* for EOFC */ |
| 819 |
} |
| 820 |
|
| 821 |
/* |
| 822 |
* Note that in the EOF case, we can get here with no data |
| 823 |
* available. |
| 824 |
*/ |
| 825 |
if (bytes_avail >= min_left) |
| 826 |
bytes_avail = (bytes_avail - min_left); /* may be 0 */ |
| 827 |
|
| 828 |
plane_data[main_plane].data = sbufptr(image_stream->s); |
| 829 |
plane_data[main_plane].size = bytes_avail; |
| 830 |
|
| 831 |
code = gs_image_next_planes(penum, plane_data, used, false); |
| 832 |
if (code < 0) { |
| 833 |
goto cleanupExit; |
| 834 |
} |
| 835 |
/* If no data was consumed twice in a row, then there must be some kind of error, |
| 836 |
* even if the error code was not < 0. Saw this with pdfwrite device. |
| 837 |
*/ |
| 838 |
if (used[main_plane] == 0 && used[mask_plane] == 0) { |
| 839 |
if (no_progress) { |
| 840 |
code = gs_note_error(gs_error_unknownerror); |
| 841 |
goto cleanupExit; |
| 842 |
} |
| 843 |
no_progress = true; |
| 844 |
} else { |
| 845 |
no_progress = false; |
| 846 |
} |
| 847 |
|
| 848 |
(void)sbufskip(image_stream->s, used[main_plane]); |
| 849 |
|
| 850 |
/* It might not always consume all the data, but so far the only case |
| 851 |
* I have seen with that was one that had mask data. |
| 852 |
* In that case, it used all of plane 0, and none of plane 1 on the first pass. |
| 853 |
* (image_2bpp.pdf) |
| 854 |
* |
| 855 |
* Anyway, this math should handle that case (as well as a case where it consumed only |
| 856 |
* part of the data, if that can actually happen). |
| 857 |
*/ |
| 858 |
bytes_used = used[main_plane]; |
| 859 |
bytes_left -= bytes_used; |
| 860 |
bytes_avail -= bytes_used; |
| 861 |
} |
| 862 |
|
| 863 |
code = 0; |
| 864 |
|
| 865 |
cleanupExit: |
| 866 |
if (penum) |
| 867 |
gs_image_cleanup_and_free_enum(penum, ctx->pgs); |
| 868 |
pdfi_grestore(ctx); |
| 869 |
#if DEBUG_IMAGES |
| 870 |
dbgmprintf(ctx->memory, "pdfi_render_image END\n"); |
| 871 |
#endif |
| 872 |
return code; |
| 873 |
} |
| 874 |
|
| 875 |
/* Load up params common to the different image types */ |
| 876 |
static int |
| 877 |
pdfi_data_image_params(pdf_context *ctx, pdfi_image_info_t *info, |
| 878 |
gs_data_image_t *pim, int comps, gs_color_space *pcs) |
| 879 |
{ |
| 880 |
int code; |
| 881 |
|
| 882 |
pim->BitsPerComponent = info->BPC; |
| 883 |
pim->Width = info->Width; |
| 884 |
pim->Height = info->Height; |
| 885 |
pim->ImageMatrix.xx = (float)info->Width; |
| 886 |
pim->ImageMatrix.yy = (float)(info->Height * -1); |
| 887 |
pim->ImageMatrix.ty = (float)info->Height; |
| 888 |
|
| 889 |
pim->Interpolate = info->Interpolate; |
| 890 |
|
| 891 |
/* Get the decode array (required for ImageMask, probably for everything) */ |
| 892 |
if (info->Decode) { |
| 893 |
pdf_array *decode_array = (pdf_array *)info->Decode; |
| 894 |
int i; |
| 895 |
double num; |
| 896 |
|
| 897 |
if (pdfi_array_size(decode_array) > GS_IMAGE_MAX_COMPONENTS * 2) { |
| 898 |
code = gs_note_error(gs_error_limitcheck); |
| 899 |
goto cleanupExit; |
| 900 |
} |
| 901 |
|
| 902 |
for (i=0; i<pdfi_array_size(decode_array); i++) { |
| 903 |
code = pdfi_array_get_number(ctx, decode_array, i, &num); |
| 904 |
if (code < 0) |
| 905 |
goto cleanupExit; |
| 906 |
pim->Decode[i] = (float)num; |
| 907 |
} |
| 908 |
} else { |
| 909 |
/* Provide a default if not specified [0 1 ...] per component */ |
| 910 |
int i; |
| 911 |
float minval, maxval; |
| 912 |
|
| 913 |
/* TODO: Is there a less hacky way to identify Indexed case? */ |
| 914 |
if (pcs && (pcs->type == &gs_color_space_type_Indexed || |
| 915 |
pcs->type == &gs_color_space_type_Indexed_Named)) { |
| 916 |
/* Default value is [0,N], where N=2^n-1, our hival (which depends on BPC)*/ |
| 917 |
minval = 0.0; |
| 918 |
maxval = (float)((1 << info->BPC) - 1); |
| 919 |
} else { |
| 920 |
bool islab = false; |
| 921 |
|
| 922 |
if (pcs && pcs->cmm_icc_profile_data != NULL) |
| 923 |
islab = pcs->cmm_icc_profile_data->islab; |
| 924 |
|
| 925 |
if(islab) { |
| 926 |
pim->Decode[0] = 0.0; |
| 927 |
pim->Decode[1] = 100.0; |
| 928 |
pim->Decode[2] = pcs->cmm_icc_profile_data->Range.ranges[1].rmin; |
| 929 |
pim->Decode[3] = pcs->cmm_icc_profile_data->Range.ranges[1].rmax; |
| 930 |
pim->Decode[4] = pcs->cmm_icc_profile_data->Range.ranges[2].rmin; |
| 931 |
pim->Decode[5] = pcs->cmm_icc_profile_data->Range.ranges[2].rmax; |
| 932 |
return 0; |
| 933 |
} else { |
| 934 |
minval = 0.0; |
| 935 |
maxval = 1.0; |
| 936 |
} |
| 937 |
} |
| 938 |
for (i=0; i<comps*2; i+=2) { |
| 939 |
pim->Decode[i] = minval; |
| 940 |
pim->Decode[i+1] = maxval; |
| 941 |
} |
| 942 |
} |
| 943 |
code = 0; |
| 944 |
|
| 945 |
cleanupExit: |
| 946 |
return code; |
| 947 |
} |
| 948 |
|
| 949 |
/* Returns number of components in Matte array or 0 if not found, <0 if error */ |
| 950 |
static int |
| 951 |
pdfi_image_get_matte(pdf_context *ctx, pdf_obj *smask_obj, float *vals, int size, bool *has_Matte) |
| 952 |
{ |
| 953 |
int i; |
| 954 |
pdf_array *Matte = NULL; |
| 955 |
int code; |
| 956 |
double f; |
| 957 |
pdf_dict *smask_dict = NULL; |
| 958 |
|
| 959 |
*has_Matte = false; |
| 960 |
code = pdfi_dict_from_obj(ctx, smask_obj, &smask_dict); |
| 961 |
if (code < 0) |
| 962 |
goto exit; |
| 963 |
|
| 964 |
code = pdfi_dict_knownget_type(ctx, smask_dict, "Matte", |
| 965 |
PDF_ARRAY, (pdf_obj **)&Matte); |
| 966 |
if (code <= 0) |
| 967 |
goto exit; |
| 968 |
|
| 969 |
*has_Matte = true; |
| 970 |
if (pdfi_array_size(Matte) > size) { |
| 971 |
code = gs_note_error(gs_error_rangecheck); |
| 972 |
goto exit; |
| 973 |
} |
| 974 |
|
| 975 |
for (i = 0; i < pdfi_array_size(Matte); i++) { |
| 976 |
code = pdfi_array_get_number(ctx, Matte, (uint64_t)i, &f); |
| 977 |
if (code < 0) |
| 978 |
goto exit; |
| 979 |
vals[i] = (float)f; |
| 980 |
} |
| 981 |
if (i == pdfi_array_size(Matte)) |
| 982 |
code = i; |
| 983 |
|
| 984 |
exit: |
| 985 |
pdfi_countdown(Matte); |
| 986 |
return code; |
| 987 |
} |
| 988 |
|
| 989 |
/* See ztrans.c/zbegintransparencymaskimage() and pdf_draw.ps/doimagesmask */ |
| 990 |
static int |
| 991 |
pdfi_do_image_smask(pdf_context *ctx, pdf_c_stream *source, pdfi_image_info_t *image_info, bool *has_Matte) |
| 992 |
{ |
| 993 |
gs_rect bbox = { { 0, 0} , { 1, 1} }; |
| 994 |
gs_transparency_mask_params_t params; |
| 995 |
gs_offset_t savedoffset = 0; |
| 996 |
int code, code1; |
| 997 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)ctx->pgs->client_data; |
| 998 |
pdf_stream *stream_obj = NULL; |
| 999 |
gs_rect clip_box; |
| 1000 |
int empty = 0; |
| 1001 |
|
| 1002 |
#if DEBUG_IMAGES |
| 1003 |
dbgmprintf(ctx->memory, "pdfi_do_image_smask BEGIN\n"); |
| 1004 |
#endif |
| 1005 |
|
| 1006 |
code = pdfi_loop_detector_mark(ctx); |
| 1007 |
if (code < 0) |
| 1008 |
return code; |
| 1009 |
|
| 1010 |
if (pdfi_type_of(image_info->SMask) != PDF_STREAM) |
| 1011 |
return_error(gs_error_typecheck); |
| 1012 |
|
| 1013 |
if (image_info->SMask->object_num != 0) { |
| 1014 |
if (pdfi_loop_detector_check_object(ctx, image_info->SMask->object_num)) |
| 1015 |
return gs_note_error(gs_error_circular_reference); |
| 1016 |
code = pdfi_loop_detector_add_object(ctx, image_info->SMask->object_num); |
| 1017 |
if (code < 0) |
| 1018 |
goto exit; |
| 1019 |
} |
| 1020 |
|
| 1021 |
gs_trans_mask_params_init(¶ms, TRANSPARENCY_MASK_Luminosity); |
| 1022 |
|
| 1023 |
/* gs_begin_transparency_mask is going to crap all over the current |
| 1024 |
* graphics state. We need to be sure that everything goes back as |
| 1025 |
* it was. So, gsave here, and grestore on the 'end', right? Well |
| 1026 |
* that doesn't work, cos then we throw away the SMask we've just |
| 1027 |
* drawn! We'll do some magic to ensure that doesn't happen. */ |
| 1028 |
code = gs_gsave(ctx->pgs); |
| 1029 |
if (code < 0) |
| 1030 |
goto exit; |
| 1031 |
|
| 1032 |
if (gs_clip_bounds_in_user_space(ctx->pgs, &clip_box) >= 0) |
| 1033 |
{ |
| 1034 |
rect_intersect(bbox, clip_box); |
| 1035 |
if (bbox.p.x >= bbox.q.x || bbox.p.y >= bbox.q.y) |
| 1036 |
{ |
| 1037 |
/* If the bbox is illegal, we still need to set up an empty mask. |
| 1038 |
* We can't just skip forwards as everything will now be unmasked! |
| 1039 |
* We can skip doing the actual work though. */ |
| 1040 |
empty = 1; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
code = pdfi_image_get_matte(ctx, image_info->SMask, params.Matte, GS_CLIENT_COLOR_MAX_COMPONENTS, has_Matte); |
| 1045 |
|
| 1046 |
if (code < 0) { |
| 1047 |
code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_ERROR_IN_MATTE, "", NULL); |
| 1048 |
if (code < 0) |
| 1049 |
goto exitSaved; |
| 1050 |
} |
| 1051 |
if (code >= 0) |
| 1052 |
params.Matte_components = code; |
| 1053 |
|
| 1054 |
code = gs_begin_transparency_mask(ctx->pgs, ¶ms, &bbox, true); |
| 1055 |
if (code < 0) |
| 1056 |
goto exitSaved; |
| 1057 |
if (empty) |
| 1058 |
goto exitMasked; |
| 1059 |
savedoffset = pdfi_tell(ctx->main_stream); |
| 1060 |
code = pdfi_gsave(ctx); |
| 1061 |
if (code < 0) |
| 1062 |
goto exitMasked; |
| 1063 |
|
| 1064 |
/* Disable SMask for inner image */ |
| 1065 |
pdfi_gstate_smask_free(igs); |
| 1066 |
|
| 1067 |
gs_setstrokeconstantalpha(ctx->pgs, 1.0); |
| 1068 |
gs_setfillconstantalpha(ctx->pgs, 1.0); |
| 1069 |
gs_setblendmode(ctx->pgs, BLEND_MODE_Compatible); |
| 1070 |
|
| 1071 |
pdfi_seek(ctx, ctx->main_stream, |
| 1072 |
pdfi_stream_offset(ctx, (pdf_stream *)image_info->SMask), SEEK_SET); |
| 1073 |
|
| 1074 |
switch (pdfi_type_of(image_info->SMask)) { |
| 1075 |
case PDF_DICT: |
| 1076 |
code = pdfi_obj_dict_to_stream(ctx, (pdf_dict *)image_info->SMask, &stream_obj, false); |
| 1077 |
if (code == 0) { |
| 1078 |
code = pdfi_do_image_or_form(ctx, image_info->stream_dict, |
| 1079 |
image_info->page_dict, (pdf_obj *)stream_obj); |
| 1080 |
|
| 1081 |
pdfi_countdown(stream_obj); |
| 1082 |
} |
| 1083 |
break; |
| 1084 |
case PDF_STREAM: |
| 1085 |
code = pdfi_do_image_or_form(ctx, image_info->stream_dict, |
| 1086 |
image_info->page_dict, image_info->SMask); |
| 1087 |
break; |
| 1088 |
default: |
| 1089 |
code = gs_note_error(gs_error_typecheck); |
| 1090 |
goto exitSavedTwice; |
| 1091 |
} |
| 1092 |
|
| 1093 |
exitSavedTwice: |
| 1094 |
pdfi_seek(ctx, ctx->main_stream, savedoffset, SEEK_SET); |
| 1095 |
|
| 1096 |
code1 = pdfi_grestore(ctx); |
| 1097 |
if (code < 0) |
| 1098 |
code = code1; |
| 1099 |
exitMasked: |
| 1100 |
code1 = gs_end_transparency_mask(ctx->pgs, TRANSPARENCY_CHANNEL_Opacity); |
| 1101 |
if (code < 0) |
| 1102 |
code = code1; |
| 1103 |
exitSaved: |
| 1104 |
/* This is where we have to do some magic. If we just grestore here, then |
| 1105 |
* any SMask we've rendered will be popped away. */ |
| 1106 |
{ |
| 1107 |
/* Stash the flag that means 'Pop the SMask'. */ |
| 1108 |
int flag = ctx->pgs->trans_flags.xstate_change; |
| 1109 |
/* Clear the flag so the SMask will live. */ |
| 1110 |
ctx->pgs->trans_flags.xstate_change = 0; |
| 1111 |
code1 = gs_grestore(ctx->pgs); |
| 1112 |
if (code < 0) |
| 1113 |
code = code1; |
| 1114 |
/* Put the flag back 1 level higher. This relies on the fact that |
| 1115 |
* the graphics state we return to does not already have this set. |
| 1116 |
* We ensure that by ensuring that callers have gsaved/grestored |
| 1117 |
* around this. */ |
| 1118 |
ctx->pgs->trans_flags.xstate_change = flag; |
| 1119 |
} |
| 1120 |
|
| 1121 |
exit: |
| 1122 |
#if DEBUG_IMAGES |
| 1123 |
dbgmprintf(ctx->memory, "pdfi_do_image_smask END\n"); |
| 1124 |
#endif |
| 1125 |
pdfi_loop_detector_cleartomark(ctx); |
| 1126 |
return code; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
/* Setup for transparency (see pdf_draw.ps/doimage) */ |
| 1131 |
static int |
| 1132 |
pdfi_image_setup_trans(pdf_context *ctx, pdfi_trans_state_t *state) |
| 1133 |
{ |
| 1134 |
int code; |
| 1135 |
gs_rect bbox; |
| 1136 |
|
| 1137 |
/* We need to create a bbox in order to pass it to the transparency setup, |
| 1138 |
* which (potentially, at least, uses it to set up a transparency group. |
| 1139 |
* Setting up a 1x1 path, and establishing it's BBox will work, because |
| 1140 |
* the image scaling is already in place. We don't want to disturb the |
| 1141 |
* graphics state, so do this inside a gsave/grestore pair. |
| 1142 |
*/ |
| 1143 |
code = pdfi_gsave(ctx); |
| 1144 |
if (code < 0) |
| 1145 |
return code; |
| 1146 |
|
| 1147 |
code = gs_newpath(ctx->pgs); |
| 1148 |
if (code < 0) |
| 1149 |
goto exit; |
| 1150 |
code = gs_moveto(ctx->pgs, 1.0, 1.0); |
| 1151 |
if (code < 0) |
| 1152 |
goto exit; |
| 1153 |
code = gs_lineto(ctx->pgs, 0., 0.); |
| 1154 |
if (code < 0) |
| 1155 |
goto exit; |
| 1156 |
|
| 1157 |
code = pdfi_get_current_bbox(ctx, &bbox, false); |
| 1158 |
if (code < 0) |
| 1159 |
goto exit; |
| 1160 |
|
| 1161 |
pdfi_grestore(ctx); |
| 1162 |
|
| 1163 |
code = pdfi_trans_setup(ctx, state, &bbox, TRANSPARENCY_Caller_Image); |
| 1164 |
exit: |
| 1165 |
return code; |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
/* Setup a type 4 image, particularly the MaskColor array. |
| 1170 |
* Handles error situations like pdf_draw.ps/makemaskimage |
| 1171 |
*/ |
| 1172 |
static int |
| 1173 |
pdfi_image_setup_type4(pdf_context *ctx, pdfi_image_info_t *image_info, |
| 1174 |
gs_image4_t *t4image, |
| 1175 |
pdf_array *mask_array, gs_color_space *pcs) |
| 1176 |
{ |
| 1177 |
int i; |
| 1178 |
double num; |
| 1179 |
int code = 0; |
| 1180 |
int bpc = image_info->BPC; |
| 1181 |
uint mask = (1 << bpc) - 1; |
| 1182 |
uint maxval = mask; |
| 1183 |
int64_t intval; |
| 1184 |
bool had_range_error = false; |
| 1185 |
bool had_float_error = false; |
| 1186 |
/* Check for special case of Indexed and BPC=1 (to match AR) |
| 1187 |
* See bugs: 692852, 697919, 689717 |
| 1188 |
*/ |
| 1189 |
bool indexed_case = (pcs && pcs->type == &gs_color_space_type_Indexed && bpc == 1); |
| 1190 |
|
| 1191 |
memset(t4image, 0, sizeof(gs_image4_t)); |
| 1192 |
gs_image4_t_init(t4image, NULL); |
| 1193 |
|
| 1194 |
if (pdfi_array_size(mask_array) > GS_IMAGE_MAX_COMPONENTS * 2) { |
| 1195 |
code = gs_note_error(gs_error_rangecheck); |
| 1196 |
goto exit; |
| 1197 |
} |
| 1198 |
|
| 1199 |
for (i=0; i<pdfi_array_size(mask_array); i++) { |
| 1200 |
code = pdfi_array_get_int(ctx, mask_array, i, &intval); |
| 1201 |
if (code == gs_error_typecheck) { |
| 1202 |
code = pdfi_array_get_number(ctx, mask_array, i, &num); |
| 1203 |
if (code == 0) { |
| 1204 |
intval = (int64_t)(num + 0.5); |
| 1205 |
had_float_error = true; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
if (code < 0) |
| 1209 |
goto exit; |
| 1210 |
if (intval > maxval) { |
| 1211 |
had_range_error = true; |
| 1212 |
if (indexed_case) { |
| 1213 |
if (i == 0) { |
| 1214 |
/* If first component is invalid, AR9 ignores the mask. */ |
| 1215 |
code = gs_note_error(gs_error_rangecheck); |
| 1216 |
goto exit; |
| 1217 |
} else { |
| 1218 |
/* If second component is invalid, AR9 replace it with 1. */ |
| 1219 |
intval = 1; |
| 1220 |
} |
| 1221 |
} else { |
| 1222 |
if (bpc != 1) { |
| 1223 |
/* If not special handling, just mask it off to be in range */ |
| 1224 |
intval &= mask; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
t4image->MaskColor[i] = intval; |
| 1229 |
} |
| 1230 |
t4image->MaskColor_is_range = true; |
| 1231 |
|
| 1232 |
/* Another special handling (see Bug701468) |
| 1233 |
* If 1 BPC and the two entries are not the same, ignore the mask |
| 1234 |
*/ |
| 1235 |
if (!indexed_case && bpc == 1 && had_range_error) { |
| 1236 |
if (t4image->MaskColor[0] != t4image->MaskColor[1]) { |
| 1237 |
code = gs_note_error(gs_error_rangecheck); |
| 1238 |
goto exit; |
| 1239 |
} else { |
| 1240 |
t4image->MaskColor[0] &= mask; |
| 1241 |
t4image->MaskColor[1] &= mask; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
code = 0; |
| 1246 |
|
| 1247 |
exit: |
| 1248 |
if (had_float_error) { |
| 1249 |
pdfi_set_warning(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_IMAGE_ERROR, "pdfi_image_setup_type4", (char *)"*** Error: Some elements of Mask array are not integers"); |
| 1250 |
} |
| 1251 |
if (had_range_error) { |
| 1252 |
pdfi_set_warning(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_IMAGE_ERROR, "pdfi_image_setup_type4", (char *)"*** Error: Some elements of Mask array are out of range"); |
| 1253 |
} |
| 1254 |
return code; |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* Setup a type 3x image |
| 1258 |
*/ |
| 1259 |
static int |
| 1260 |
pdfi_image_setup_type3x(pdf_context *ctx, pdfi_image_info_t *image_info, |
| 1261 |
gs_image3x_t *t3ximage, pdfi_image_info_t *smask_info, int comps) |
| 1262 |
{ |
| 1263 |
int code = 0; |
| 1264 |
gs_image3x_mask_t *mask; |
| 1265 |
|
| 1266 |
memset(t3ximage, 0, sizeof(*t3ximage)); |
| 1267 |
gs_image3x_t_init(t3ximage, NULL); |
| 1268 |
if (gs_getalphaisshape(ctx->pgs)) |
| 1269 |
mask = &t3ximage->Shape; |
| 1270 |
else |
| 1271 |
mask = &t3ximage->Opacity; |
| 1272 |
mask->InterleaveType = 3; |
| 1273 |
|
| 1274 |
code = pdfi_image_get_matte(ctx, image_info->SMask, mask->Matte, GS_CLIENT_COLOR_MAX_COMPONENTS, &mask->has_Matte); |
| 1275 |
if (code < 0) |
| 1276 |
return code; |
| 1277 |
|
| 1278 |
code = pdfi_data_image_params(ctx, smask_info, &mask->MaskDict, comps, NULL); |
| 1279 |
return code; |
| 1280 |
} |
| 1281 |
|
| 1282 |
static int pdfi_create_JPX_Lab(pdf_context *ctx, pdf_obj **ColorSpace) |
| 1283 |
{ |
| 1284 |
int code, i; |
| 1285 |
pdf_name *SpaceName = NULL, *WhitePointName = NULL; |
| 1286 |
pdf_dict *Params = NULL; |
| 1287 |
pdf_array *WhitePoint = NULL; |
| 1288 |
double WP[3] = {0.9505, 1.0, 1.0890}; |
| 1289 |
pdf_num *num = NULL; |
| 1290 |
|
| 1291 |
*ColorSpace = NULL; |
| 1292 |
code = pdfi_name_alloc(ctx, (byte *)"Lab", 3, (pdf_obj **)&SpaceName); |
| 1293 |
if (code < 0) |
| 1294 |
goto cleanupExit; |
| 1295 |
pdfi_countup(SpaceName); |
| 1296 |
|
| 1297 |
code = pdfi_dict_alloc(ctx, 1, &Params); |
| 1298 |
if (code < 0) |
| 1299 |
goto cleanupExit; |
| 1300 |
pdfi_countup(Params); |
| 1301 |
|
| 1302 |
code = pdfi_name_alloc(ctx, (byte *)"WhitePoint", 3, (pdf_obj **)&WhitePointName); |
| 1303 |
if (code < 0) |
| 1304 |
goto cleanupExit; |
| 1305 |
pdfi_countup(WhitePointName); |
| 1306 |
|
| 1307 |
code = pdfi_array_alloc(ctx, 3, &WhitePoint); |
| 1308 |
if (code < 0) |
| 1309 |
goto cleanupExit; |
| 1310 |
pdfi_countup(WhitePoint); |
| 1311 |
|
| 1312 |
for (i = 0; i < 3; i++) { |
| 1313 |
code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&num); |
| 1314 |
if (code < 0) |
| 1315 |
goto cleanupExit; |
| 1316 |
num->value.d = WP[i]; |
| 1317 |
pdfi_countup(num); |
| 1318 |
code = pdfi_array_put(ctx, WhitePoint, i, (pdf_obj *)num); |
| 1319 |
if (code < 0) |
| 1320 |
goto cleanupExit; |
| 1321 |
pdfi_countdown(num); |
| 1322 |
} |
| 1323 |
num = NULL; |
| 1324 |
|
| 1325 |
code = pdfi_dict_put_obj(ctx, Params, (pdf_obj *)WhitePointName, (pdf_obj *)WhitePoint, true); |
| 1326 |
if (code < 0) |
| 1327 |
goto cleanupExit; |
| 1328 |
|
| 1329 |
pdfi_countdown(WhitePointName); |
| 1330 |
WhitePointName = NULL; |
| 1331 |
pdfi_countdown(WhitePoint); |
| 1332 |
WhitePoint = NULL; |
| 1333 |
|
| 1334 |
code = pdfi_array_alloc(ctx, 2, (pdf_array **)ColorSpace); |
| 1335 |
if (code < 0) |
| 1336 |
goto cleanupExit; |
| 1337 |
|
| 1338 |
code = pdfi_array_put(ctx, (pdf_array *)*ColorSpace, 0, (pdf_obj *)SpaceName); |
| 1339 |
if (code < 0) |
| 1340 |
goto cleanupExit; |
| 1341 |
pdfi_countdown(SpaceName); |
| 1342 |
SpaceName = NULL; |
| 1343 |
|
| 1344 |
code = pdfi_array_put(ctx, (pdf_array *)*ColorSpace, 1, (pdf_obj *)Params); |
| 1345 |
if (code < 0) |
| 1346 |
goto cleanupExit; |
| 1347 |
pdfi_countdown(Params); |
| 1348 |
|
| 1349 |
return 0; |
| 1350 |
|
| 1351 |
cleanupExit: |
| 1352 |
pdfi_countdown(*ColorSpace); |
| 1353 |
pdfi_countdown(SpaceName); |
| 1354 |
pdfi_countdown(Params); |
| 1355 |
pdfi_countdown(WhitePointName); |
| 1356 |
pdfi_countdown(WhitePoint); |
| 1357 |
pdfi_countdown(num); |
| 1358 |
return code; |
| 1359 |
} |
| 1360 |
|
| 1361 |
static int |
| 1362 |
pdfi_image_get_color(pdf_context *ctx, pdf_c_stream *source, pdfi_image_info_t *image_info, |
| 1363 |
int *comps, ulong dictkey, gs_color_space **pcs) |
| 1364 |
{ |
| 1365 |
int code = 0; |
| 1366 |
pdfi_jpx_info_t *jpx_info = &image_info->jpx_info; |
| 1367 |
pdf_obj *ColorSpace = NULL; |
| 1368 |
char *backup_color_name = NULL; |
| 1369 |
bool using_enum_cs = false; |
| 1370 |
|
| 1371 |
/* NOTE: Spec says ImageMask and ColorSpace mutually exclusive |
| 1372 |
* We need to make sure we do not have both set or we could end up treating |
| 1373 |
* an image as a mask or vice versa and trying to use a non-existent colour space. |
| 1374 |
*/ |
| 1375 |
if (image_info->ImageMask) { |
| 1376 |
if (image_info->ColorSpace == NULL) { |
| 1377 |
*comps = 1; |
| 1378 |
*pcs = NULL; |
| 1379 |
if (image_info->Mask) { |
| 1380 |
pdfi_countdown(image_info->Mask); |
| 1381 |
image_info->Mask = NULL; |
| 1382 |
} |
| 1383 |
return 0; |
| 1384 |
} else { |
| 1385 |
if (image_info->BPC != 1 || image_info->Mask != NULL) { |
| 1386 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_IMAGE_MASKWITHCOLOR, "pdfi_image_get_color", "BitsPerComonent is not 1, so treating as an image")) < 0) { |
| 1387 |
return code; |
| 1388 |
} |
| 1389 |
image_info->ImageMask = 0; |
| 1390 |
} |
| 1391 |
else { |
| 1392 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_IMAGE_MASKWITHCOLOR, "pdfi_image_get_color", "BitsPerComonent is 1, so treating as a mask")) < 0) { |
| 1393 |
return code; |
| 1394 |
} |
| 1395 |
pdfi_countdown(image_info->ColorSpace); |
| 1396 |
image_info->ColorSpace = NULL; |
| 1397 |
*comps = 1; |
| 1398 |
*pcs = NULL; |
| 1399 |
return 0; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
ColorSpace = image_info->ColorSpace; |
| 1405 |
if (ColorSpace) |
| 1406 |
pdfi_countup(ColorSpace); |
| 1407 |
if (ColorSpace == NULL) { |
| 1408 |
if (image_info->is_JPXDecode) { |
| 1409 |
/* The graphics library doesn't support 12-bit images, so the openjpeg layer |
| 1410 |
* (see sjpx_openjpeg.c/decode_image()) is going to translate the 12-bits up to 16-bits. |
| 1411 |
* That means we just treat it as 16-bit when rendering, so force the value |
| 1412 |
* to 16 here. |
| 1413 |
*/ |
| 1414 |
if (jpx_info->bpc == 12) { |
| 1415 |
jpx_info->bpc = 16; |
| 1416 |
} |
| 1417 |
image_info->BPC = jpx_info->bpc; |
| 1418 |
|
| 1419 |
if (jpx_info->iccbased) { |
| 1420 |
int dummy; /* Holds number of components read from the ICC profile, we ignore this here */ |
| 1421 |
|
| 1422 |
code = pdfi_create_icc_colorspace_from_stream(ctx, source, jpx_info->icc_offset, |
| 1423 |
jpx_info->icc_length, jpx_info->comps, &dummy, |
| 1424 |
dictkey, pcs); |
| 1425 |
if (code < 0) { |
| 1426 |
code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_JPX_CS_ERROR, "pdfi_image_get_color", NULL); |
| 1427 |
dbgprintf2("WARNING JPXDecode: Error setting icc colorspace (offset=%d,len=%d)\n", |
| 1428 |
jpx_info->icc_offset, jpx_info->icc_length); |
| 1429 |
goto cleanupExit; |
| 1430 |
} |
| 1431 |
*comps = gs_color_space_num_components(*pcs); |
| 1432 |
goto cleanupExit; |
| 1433 |
} else { |
| 1434 |
char *color_str = NULL; |
| 1435 |
|
| 1436 |
/* TODO: These colorspace names are pulled from the gs code (jp2_csp_dict), but need |
| 1437 |
* to be implemented to actually work. |
| 1438 |
*/ |
| 1439 |
backup_color_name = (char *)"DeviceRGB"; |
| 1440 |
switch(jpx_info->cs_enum) { |
| 1441 |
case 12: |
| 1442 |
color_str = (char *)"DeviceCMYK"; |
| 1443 |
break; |
| 1444 |
case 14: |
| 1445 |
/* All the other colour spaces are set by name, either a device name or a |
| 1446 |
* 'special' internal name (same as Ghostscript) which is picked up in |
| 1447 |
* pdfi_create_colorspace_by_name() in pdf_colour.c. These names must |
| 1448 |
* match! |
| 1449 |
* However for Lab we need to set a White Point and its simplest just |
| 1450 |
* to create an appropriate color space array here. |
| 1451 |
*/ |
| 1452 |
code = pdfi_create_JPX_Lab(ctx, &ColorSpace); |
| 1453 |
if (code < 0) |
| 1454 |
goto cleanupExit; |
| 1455 |
break; |
| 1456 |
case 16: |
| 1457 |
color_str = (char *)"sRGBICC"; |
| 1458 |
break; |
| 1459 |
case 17: |
| 1460 |
color_str = (char *)"sGrayICC"; |
| 1461 |
backup_color_name = (char *)"DeviceGray"; |
| 1462 |
break; |
| 1463 |
case 18: |
| 1464 |
color_str = (char *)"DeviceRGB"; |
| 1465 |
break; |
| 1466 |
case 20: |
| 1467 |
case 24: |
| 1468 |
color_str = (char *)"esRGBICC"; |
| 1469 |
break; |
| 1470 |
case 21: |
| 1471 |
color_str = (char *)"rommRGBICC"; |
| 1472 |
break; |
| 1473 |
default: |
| 1474 |
{ |
| 1475 |
char extra_info[gp_file_name_sizeof]; |
| 1476 |
/* TODO: Could try DeviceRGB instead of erroring out? */ |
| 1477 |
gs_snprintf(extra_info, sizeof(extra_info), "**** Error: JPXDecode: Unsupported EnumCS %d\n", jpx_info->cs_enum); |
| 1478 |
pdfi_set_error(ctx, 0, NULL, E_PDF_IMAGECOLOR_ERROR, "pdfi_image_get_color", extra_info); |
| 1479 |
code = gs_note_error(gs_error_rangecheck); |
| 1480 |
goto cleanupExit; |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
/* Make a ColorSpace for the name */ |
| 1485 |
if (color_str != NULL) { |
| 1486 |
code = pdfi_name_alloc(ctx, (byte *)color_str, strlen(color_str), &ColorSpace); |
| 1487 |
if (code < 0) |
| 1488 |
goto cleanupExit; |
| 1489 |
pdfi_countup(ColorSpace); |
| 1490 |
using_enum_cs = true; |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} else { |
| 1494 |
/* Assume DeviceRGB colorspace */ |
| 1495 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_undefined), NULL, W_PDF_BAD_IMAGEDICT, "pdfi_image_get_color", (char *)"**** Error: image has no /ColorSpace key; assuming /DeviceRGB")) < 0) |
| 1496 |
goto cleanupExit; |
| 1497 |
code = pdfi_name_alloc(ctx, (byte *)"DeviceRGB", strlen("DeviceRGB"), &ColorSpace); |
| 1498 |
if (code < 0) |
| 1499 |
goto cleanupExit; |
| 1500 |
pdfi_countup(ColorSpace); |
| 1501 |
} |
| 1502 |
} else { |
| 1503 |
/* Override BPC from JPXDecode if applicable |
| 1504 |
* Sample: tests_private/comparefiles/Bug695387.pdf |
| 1505 |
*/ |
| 1506 |
if (image_info->is_JPXDecode && jpx_info->is_valid) |
| 1507 |
image_info->BPC = jpx_info->bpc; |
| 1508 |
} |
| 1509 |
|
| 1510 |
/* At this point ColorSpace is either a string we just made, or the one from the Image */ |
| 1511 |
code = pdfi_create_colorspace(ctx, ColorSpace, |
| 1512 |
image_info->stream_dict, image_info->page_dict, |
| 1513 |
pcs, image_info->inline_image); |
| 1514 |
if (code < 0) { |
| 1515 |
dbgprintf("WARNING: Image has unsupported ColorSpace "); |
| 1516 |
if (pdfi_type_of(ColorSpace) == PDF_NAME) { |
| 1517 |
pdf_name *name = (pdf_name *)ColorSpace; |
| 1518 |
char str[100]; |
| 1519 |
int length = name->length; |
| 1520 |
|
| 1521 |
if (length > 0) { |
| 1522 |
if (length > 99) |
| 1523 |
length = 99; |
| 1524 |
|
| 1525 |
memcpy(str, (const char *)name->data, length); |
| 1526 |
str[length] = '\0'; |
| 1527 |
dbgprintf1("NAME:%s\n", str); |
| 1528 |
} |
| 1529 |
} else { |
| 1530 |
dbgmprintf(ctx->memory, "(not a name)\n"); |
| 1531 |
} |
| 1532 |
|
| 1533 |
/* If we were trying an enum_cs, attempt to use backup_color_name instead */ |
| 1534 |
if (using_enum_cs) { |
| 1535 |
pdfi_countdown(ColorSpace); |
| 1536 |
code = pdfi_name_alloc(ctx, (byte *)backup_color_name, strlen(backup_color_name), &ColorSpace); |
| 1537 |
if (code < 0) |
| 1538 |
goto cleanupExit; |
| 1539 |
pdfi_countup(ColorSpace); |
| 1540 |
/* Try to set the backup name */ |
| 1541 |
code = pdfi_create_colorspace(ctx, ColorSpace, |
| 1542 |
image_info->stream_dict, image_info->page_dict, |
| 1543 |
pcs, image_info->inline_image); |
| 1544 |
|
| 1545 |
if (code < 0) { |
| 1546 |
pdfi_set_error(ctx, 0, NULL, E_PDF_IMAGECOLOR_ERROR, "pdfi_image_get_color", NULL); |
| 1547 |
goto cleanupExit; |
| 1548 |
} |
| 1549 |
} else { |
| 1550 |
pdfi_set_error(ctx, 0, NULL, E_PDF_IMAGECOLOR_ERROR, "pdfi_image_get_color", NULL); |
| 1551 |
goto cleanupExit; |
| 1552 |
} |
| 1553 |
} |
| 1554 |
*comps = gs_color_space_num_components(*pcs); |
| 1555 |
|
| 1556 |
cleanupExit: |
| 1557 |
pdfi_countdown(ColorSpace); |
| 1558 |
return code; |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* Make a fake SMask dict from a JPX SMaskInData */ |
| 1562 |
static int |
| 1563 |
pdfi_make_smask_dict(pdf_context *ctx, pdf_stream *image_stream, pdfi_image_info_t *image_info, |
| 1564 |
int comps) |
| 1565 |
{ |
| 1566 |
int code = 0; |
| 1567 |
pdf_dict *smask_dict = NULL; |
| 1568 |
pdf_stream *fake_smask = NULL; |
| 1569 |
pdf_array *array = NULL; |
| 1570 |
pdf_array *matte = NULL; |
| 1571 |
pdf_dict *image_dict = NULL, *dict = NULL; /* alias */ |
| 1572 |
|
| 1573 |
if (image_info->SMask != NULL) { |
| 1574 |
code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_SMASK_IN_SMASK, "pdfi_make_smask_dict", NULL); |
| 1575 |
goto exit; |
| 1576 |
} |
| 1577 |
|
| 1578 |
if (pdfi_type_of(image_stream) != PDF_STREAM) { |
| 1579 |
code = gs_note_error(gs_error_typecheck); |
| 1580 |
goto exit; |
| 1581 |
} |
| 1582 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)image_stream, &image_dict); |
| 1583 |
if (code < 0) goto exit; |
| 1584 |
|
| 1585 |
/* Make a new stream object and a dict for it */ |
| 1586 |
code = pdfi_object_alloc(ctx, PDF_STREAM, 0, (pdf_obj **)&fake_smask); |
| 1587 |
if (code < 0) goto exit; |
| 1588 |
pdfi_countup(fake_smask); |
| 1589 |
|
| 1590 |
code = pdfi_dict_alloc(ctx, 32, &smask_dict); |
| 1591 |
if (code < 0) goto exit; |
| 1592 |
|
| 1593 |
fake_smask->stream_dict = smask_dict; |
| 1594 |
pdfi_countup(smask_dict); |
| 1595 |
smask_dict = NULL; |
| 1596 |
dict = fake_smask->stream_dict; /* alias */ |
| 1597 |
|
| 1598 |
/* Copy everything from the image_dict */ |
| 1599 |
code = pdfi_dict_copy(ctx, dict, image_dict); |
| 1600 |
fake_smask->stream_offset = image_stream->stream_offset; |
| 1601 |
|
| 1602 |
code = pdfi_dict_put_int(ctx, dict, "SMaskInData", 0); |
| 1603 |
if (code < 0) goto exit; |
| 1604 |
|
| 1605 |
code = pdfi_dict_put_name(ctx, dict, "ColorSpace", "DeviceGray"); |
| 1606 |
if (code < 0) goto exit; |
| 1607 |
|
| 1608 |
/* BPC needs to come from the jpxinfo */ |
| 1609 |
code = pdfi_dict_put_int(ctx, dict, "BitsPerComponent", image_info->jpx_info.bpc); |
| 1610 |
if (code < 0) goto exit; |
| 1611 |
|
| 1612 |
/* "Alpha" is a non-standard thing that gs code uses to tell |
| 1613 |
* the jpxfilter that it is doing an SMask. I guess we can do |
| 1614 |
* the same, since we're making this dictionary anyway. |
| 1615 |
*/ |
| 1616 |
code = pdfi_dict_put_bool(ctx, dict, "Alpha", true); |
| 1617 |
if (code < 0) goto exit; |
| 1618 |
|
| 1619 |
/* Make an array [0,1] */ |
| 1620 |
code = pdfi_array_alloc(ctx, 2, &array); |
| 1621 |
if (code < 0) goto exit; |
| 1622 |
pdfi_countup(array); |
| 1623 |
code = pdfi_array_put_int(ctx, array, 0, 0); |
| 1624 |
if (code < 0) goto exit; |
| 1625 |
code = pdfi_array_put_int(ctx, array, 1, 1); |
| 1626 |
if (code < 0) goto exit; |
| 1627 |
code = pdfi_dict_put(ctx, dict, "Decode", (pdf_obj *)array); |
| 1628 |
if (code < 0) goto exit; |
| 1629 |
|
| 1630 |
/* Make Matte array if needed */ |
| 1631 |
/* This just makes an array [0,0,0...] of size 'comps' |
| 1632 |
* See pdf_draw.ps/makeimagekeys |
| 1633 |
* TODO: The only sample in our test suite that triggers this path is fts_17_1718.pdf |
| 1634 |
* and this code being there or not makes no difference on that sample, so.. ??? |
| 1635 |
*/ |
| 1636 |
if (image_info->SMaskInData == 2) { |
| 1637 |
int i; |
| 1638 |
code = pdfi_array_alloc(ctx, comps, &matte); |
| 1639 |
if (code < 0) goto exit; |
| 1640 |
pdfi_countup(matte); |
| 1641 |
for (i=0; i<comps; i++) { |
| 1642 |
code = pdfi_array_put_int(ctx, matte, i, 0); |
| 1643 |
if (code < 0) goto exit; |
| 1644 |
} |
| 1645 |
code = pdfi_dict_put(ctx, dict, "Matte", (pdf_obj *)matte); |
| 1646 |
if (code < 0) goto exit; |
| 1647 |
} |
| 1648 |
|
| 1649 |
image_info->SMask = (pdf_obj *)fake_smask; |
| 1650 |
|
| 1651 |
exit: |
| 1652 |
if (code < 0) { |
| 1653 |
pdfi_countdown(fake_smask); |
| 1654 |
pdfi_countdown(smask_dict); |
| 1655 |
} |
| 1656 |
pdfi_countdown(array); |
| 1657 |
pdfi_countdown(matte); |
| 1658 |
return code; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/* NOTE: "source" is the current input stream. |
| 1662 |
* on exit: |
| 1663 |
* inline_image = TRUE, stream it will point to after the image data. |
| 1664 |
* inline_image = FALSE, stream position undefined. |
| 1665 |
*/ |
| 1666 |
static int |
| 1667 |
pdfi_do_image(pdf_context *ctx, pdf_dict *page_dict, pdf_dict *stream_dict, pdf_stream *image_stream, |
| 1668 |
pdf_c_stream *source, bool inline_image) |
| 1669 |
{ |
| 1670 |
pdf_c_stream *new_stream = NULL, *SFD_stream = NULL; |
| 1671 |
int code = 0, code1 = 0; |
| 1672 |
int comps = 0; |
| 1673 |
gs_color_space *pcs = NULL; |
| 1674 |
gs_image1_t t1image; |
| 1675 |
gs_image4_t t4image; |
| 1676 |
gs_image3_t t3image; |
| 1677 |
gs_image3x_t t3ximage; |
| 1678 |
gs_pixel_image_t *pim = NULL; |
| 1679 |
pdf_stream *alt_stream = NULL; |
| 1680 |
pdfi_image_info_t image_info, mask_info, smask_info; |
| 1681 |
pdf_stream *mask_stream = NULL; |
| 1682 |
pdf_stream *smask_stream = NULL; /* only non-null for imagetype 3x (PreserveSMask) */ |
| 1683 |
pdf_array *mask_array = NULL; |
| 1684 |
unsigned char *mask_buffer = NULL; |
| 1685 |
uint64_t mask_size = 0; |
| 1686 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)ctx->pgs->client_data; |
| 1687 |
bool transparency_group = false; |
| 1688 |
bool need_smask_cleanup = false; |
| 1689 |
bool maybe_jpxdecode = false; |
| 1690 |
pdfi_trans_state_t trans_state; |
| 1691 |
gs_offset_t stream_offset; |
| 1692 |
int trans_required; |
| 1693 |
|
| 1694 |
#if DEBUG_IMAGES |
| 1695 |
dbgmprintf(ctx->memory, "pdfi_do_image BEGIN\n"); |
| 1696 |
#endif |
| 1697 |
memset(&mask_info, 0, sizeof(mask_info)); |
| 1698 |
memset(&smask_info, 0, sizeof(mask_info)); |
| 1699 |
|
| 1700 |
/* Make sure the image is a stream (which we will assume in later code) */ |
| 1701 |
if (pdfi_type_of(image_stream) != PDF_STREAM) |
| 1702 |
return_error(gs_error_typecheck); |
| 1703 |
|
| 1704 |
if (!inline_image) { |
| 1705 |
pdf_dict *image_dict = NULL; |
| 1706 |
|
| 1707 |
/* If we are not processing an inline image, check to see if any of the abbreviated |
| 1708 |
* keys are present in the image dictionary. If they are, and we need to abort, we'll |
| 1709 |
* get an error return, otherwise we can continue. |
| 1710 |
*/ |
| 1711 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)image_stream, &image_dict); |
| 1712 |
if (code < 0) |
| 1713 |
return code; |
| 1714 |
|
| 1715 |
code = pdfi_check_inline_image_keys(ctx, image_dict); |
| 1716 |
if (code < 0) |
| 1717 |
return code; |
| 1718 |
} |
| 1719 |
|
| 1720 |
/* We want to gsave in here so that any changes made by the rendering |
| 1721 |
* don't affect the wider world. This is particularly important if we |
| 1722 |
* have an SMask here, because that RELIES on this gsave/grestore |
| 1723 |
* level being here. Because we want to handle SMask changes we need |
| 1724 |
* to push/pop the transparency state changes too. The easiest way to |
| 1725 |
* do this is to call 'q' and 'Q'. */ |
| 1726 |
code = pdfi_op_q(ctx); |
| 1727 |
if (code < 0) |
| 1728 |
return code; |
| 1729 |
|
| 1730 |
code = pdfi_get_image_info(ctx, image_stream, page_dict, stream_dict, inline_image, &image_info); |
| 1731 |
if (code < 0) |
| 1732 |
goto cleanupExit; |
| 1733 |
|
| 1734 |
/* Don't render this if turned off */ |
| 1735 |
if (pdfi_oc_is_off(ctx)) |
| 1736 |
goto cleanupExit; |
| 1737 |
/* If there is an OC dictionary, see if we even need to render this */ |
| 1738 |
if (image_info.OC) { |
| 1739 |
if (!(ctx->device_state.writepdfmarks && ctx->device_state.WantsOptionalContent)) { |
| 1740 |
if (!pdfi_oc_is_ocg_visible(ctx, image_info.OC)) |
| 1741 |
goto cleanupExit; |
| 1742 |
} |
| 1743 |
} |
| 1744 |
|
| 1745 |
/* If there is an alternate, swap it in */ |
| 1746 |
/* If image_info.Alternates, look in the array, see if any of them are flagged as "DefaultForPrinting" |
| 1747 |
* and if so, substitute that one for the image we are processing. |
| 1748 |
* (it can probably be either an array, or a reference to an array, need an example to test/implement) |
| 1749 |
* see p.274 of PDFReference.pdf |
| 1750 |
*/ |
| 1751 |
|
| 1752 |
if (image_info.Alternates != NULL) { |
| 1753 |
alt_stream = pdfi_find_alternate(ctx, image_info.Alternates); |
| 1754 |
if (alt_stream != NULL) { |
| 1755 |
image_stream = alt_stream; |
| 1756 |
pdfi_free_image_info_components(&image_info); |
| 1757 |
code = pdfi_get_image_info(ctx, image_stream, page_dict, stream_dict, inline_image, &image_info); |
| 1758 |
if (code < 0) |
| 1759 |
goto cleanupExit; |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* Grab stream_offset after alternate has (possibly) set */ |
| 1764 |
stream_offset = pdfi_stream_offset(ctx, image_stream); |
| 1765 |
|
| 1766 |
/* See if it might be a JPXDecode image even though not in the header */ |
| 1767 |
if (image_info.ColorSpace == NULL && !image_info.ImageMask) |
| 1768 |
maybe_jpxdecode = true; |
| 1769 |
|
| 1770 |
/* Handle JPXDecode filter pre-scan of header */ |
| 1771 |
if ((maybe_jpxdecode || image_info.is_JPXDecode) && !inline_image) { |
| 1772 |
pdfi_seek(ctx, source, stream_offset, SEEK_SET); |
| 1773 |
code = pdfi_scan_jpxfilter(ctx, source, image_info.Length, &image_info.jpx_info); |
| 1774 |
if (code < 0 && image_info.is_JPXDecode) |
| 1775 |
goto cleanupExit; |
| 1776 |
|
| 1777 |
/* I saw this JPXDecode images that have SMaskInData */ |
| 1778 |
if (image_info.jpx_info.no_data) |
| 1779 |
image_info.is_JPXDecode = false; |
| 1780 |
|
| 1781 |
if (code == 0 && maybe_jpxdecode) |
| 1782 |
image_info.is_JPXDecode = true; |
| 1783 |
} |
| 1784 |
|
| 1785 |
/* Set the rendering intent if applicable */ |
| 1786 |
if (image_info.Intent) { |
| 1787 |
code = pdfi_setrenderingintent(ctx, image_info.Intent); |
| 1788 |
if (code < 0) { |
| 1789 |
/* TODO: Flag a warning on this? Sample fts_17_1706.pdf has misspelled Intent |
| 1790 |
which gs renders without flagging an error */ |
| 1791 |
#if DEBUG_IMAGES |
| 1792 |
dbgmprintf(ctx->memory, "WARNING: Image with unexpected Intent\n"); |
| 1793 |
#endif |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
/* Get the color for this image */ |
| 1798 |
code = pdfi_image_get_color(ctx, source, &image_info, &comps, image_stream->object_num, &pcs); |
| 1799 |
if (code < 0) |
| 1800 |
goto cleanupExit; |
| 1801 |
|
| 1802 |
if (image_info.BPC != 1 && image_info.BPC != 2 && image_info.BPC != 4 && image_info.BPC != 8 && image_info.BPC != 16) { |
| 1803 |
code = gs_note_error(gs_error_rangecheck); |
| 1804 |
goto cleanupExit; |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* Set the colorspace */ |
| 1808 |
if (pcs) { |
| 1809 |
gs_color_space *pcs1 = pcs; |
| 1810 |
|
| 1811 |
code = pdfi_gs_setcolorspace(ctx, pcs); |
| 1812 |
if (code < 0) |
| 1813 |
goto cleanupExit; |
| 1814 |
|
| 1815 |
if (pcs->type->index == gs_color_space_index_Indexed) |
| 1816 |
pcs1 = pcs->base_space; |
| 1817 |
|
| 1818 |
/* It is possible that we get no error returned from setting an |
| 1819 |
* ICC space, but that we are not able when rendering to create a link |
| 1820 |
* between the ICC space and the output device profile. |
| 1821 |
* The PostScript PDF interpreter sets the colour after setting the space, which |
| 1822 |
* (eventually) causes us to set the device colour, and that actually creates the |
| 1823 |
* link. This is apparntly the only way we can detect this error. Otherwise we |
| 1824 |
* would carry on until we tried to render the image, and that would fail with |
| 1825 |
* a not terribly useful error of -1. So here we try to set the device colour, |
| 1826 |
* for images in an ICC profile space. If that fails then we try to manufacture |
| 1827 |
* a Device space from the number of components in the profile. |
| 1828 |
* I do feel this is something we should be able to handle better! |
| 1829 |
*/ |
| 1830 |
if (pcs1->type->index == gs_color_space_index_ICC) |
| 1831 |
{ |
| 1832 |
gs_client_color cc; |
| 1833 |
int comp = 0; |
| 1834 |
pdf_obj *ColorSpace = NULL; |
| 1835 |
|
| 1836 |
cc.pattern = 0; |
| 1837 |
for (comp = 0; comp < pcs1->cmm_icc_profile_data->num_comps;comp++) |
| 1838 |
cc.paint.values[comp] = 0; |
| 1839 |
|
| 1840 |
code = gs_setcolor(ctx->pgs, &cc); |
| 1841 |
if (code < 0) |
| 1842 |
goto cleanupExit; |
| 1843 |
|
| 1844 |
code = gx_set_dev_color(ctx->pgs); |
| 1845 |
if (code < 0) { |
| 1846 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_ICC_PROFILE_LINK, "pdfi_do_image", "Attempting to use profile /N to create a device colour space")) < 0) |
| 1847 |
goto cleanupExit; |
| 1848 |
|
| 1849 |
/* Possibly we couldn't create a link profile, soemthing wrong with the ICC profile, try to use a device space */ |
| 1850 |
switch(pcs1->cmm_icc_profile_data->num_comps) { |
| 1851 |
case 1: |
| 1852 |
code = pdfi_name_alloc(ctx, (byte *)"DeviceGray", 10, &ColorSpace); |
| 1853 |
if (code < 0) |
| 1854 |
goto cleanupExit; |
| 1855 |
pdfi_countup(ColorSpace); |
| 1856 |
break; |
| 1857 |
case 3: |
| 1858 |
code = pdfi_name_alloc(ctx, (byte *)"DeviceRGB", 9, &ColorSpace); |
| 1859 |
if (code < 0) |
| 1860 |
goto cleanupExit; |
| 1861 |
pdfi_countup(ColorSpace); |
| 1862 |
break; |
| 1863 |
case 4: |
| 1864 |
code = pdfi_name_alloc(ctx, (byte *)"DeviceCMYK", 10, &ColorSpace); |
| 1865 |
if (code < 0) |
| 1866 |
goto cleanupExit; |
| 1867 |
pdfi_countup(ColorSpace); |
| 1868 |
break; |
| 1869 |
default: |
| 1870 |
code = gs_error_unknownerror; |
| 1871 |
goto cleanupExit; |
| 1872 |
break; |
| 1873 |
} |
| 1874 |
if (pcs != NULL) |
| 1875 |
rc_decrement_only_cs(pcs, "pdfi_do_image"); |
| 1876 |
/* At this point ColorSpace is either a string we just made, or the one from the Image */ |
| 1877 |
code = pdfi_create_colorspace(ctx, ColorSpace, |
| 1878 |
image_info.stream_dict, image_info.page_dict, |
| 1879 |
&pcs, image_info.inline_image); |
| 1880 |
pdfi_countdown(ColorSpace); |
| 1881 |
if (code < 0) |
| 1882 |
goto cleanupExit; |
| 1883 |
|
| 1884 |
code = pdfi_gs_setcolorspace(ctx, pcs); |
| 1885 |
if (code < 0) |
| 1886 |
goto cleanupExit; |
| 1887 |
|
| 1888 |
/* Try to set the device color again as that failure |
| 1889 |
is why we are here. If it fails again, something |
| 1890 |
is very wrong */ |
| 1891 |
code = gx_set_dev_color(ctx->pgs); |
| 1892 |
if (code < 0) |
| 1893 |
goto cleanupExit; |
| 1894 |
} |
| 1895 |
} |
| 1896 |
} |
| 1897 |
else { |
| 1898 |
if (image_info.ImageMask == 0) { |
| 1899 |
code = gs_note_error(gs_error_undefined); |
| 1900 |
goto cleanupExit; |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* Make a fake SMask dict if needed for JPXDecode */ |
| 1905 |
if (ctx->page.has_transparency && image_info.is_JPXDecode && image_info.SMaskInData != 0) { |
| 1906 |
code = pdfi_make_smask_dict(ctx, image_stream, &image_info, comps); |
| 1907 |
if (code < 0) |
| 1908 |
goto cleanupExit; |
| 1909 |
} |
| 1910 |
|
| 1911 |
if (ctx->page.has_transparency == true && image_info.SMask != NULL) { |
| 1912 |
bool has_Matte = false; |
| 1913 |
|
| 1914 |
/* If this flag is set, then device will process the SMask and we need do nothing |
| 1915 |
* here (e.g. pdfwrite). |
| 1916 |
*/ |
| 1917 |
if (!ctx->device_state.preserve_smask) { |
| 1918 |
code = pdfi_do_image_smask(ctx, source, &image_info, &has_Matte); |
| 1919 |
if (code < 0) |
| 1920 |
goto cleanupExit; |
| 1921 |
need_smask_cleanup = true; |
| 1922 |
} |
| 1923 |
|
| 1924 |
/* If we are in an overprint situation this group will need |
| 1925 |
to have its blend mode set to compatible overprint. */ |
| 1926 |
if (ctx->page.needs_OP) { |
| 1927 |
if (pdfi_trans_okOPcs(ctx)) { |
| 1928 |
if (gs_currentfilloverprint(ctx->pgs)) { |
| 1929 |
code = gs_setblendmode(ctx->pgs, BLEND_MODE_CompatibleOverprint); |
| 1930 |
if (code < 0) |
| 1931 |
goto cleanupExit; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
} |
| 1935 |
|
| 1936 |
if (has_Matte) |
| 1937 |
code = pdfi_trans_begin_isolated_group(ctx, true, pcs); |
| 1938 |
else |
| 1939 |
code = pdfi_trans_begin_isolated_group(ctx, true, NULL); |
| 1940 |
if (code < 0) |
| 1941 |
goto cleanupExit; |
| 1942 |
transparency_group = true; |
| 1943 |
} else if (igs->SMask) { |
| 1944 |
code = pdfi_trans_begin_isolated_group(ctx, false, NULL); |
| 1945 |
if (code < 0) |
| 1946 |
goto cleanupExit; |
| 1947 |
transparency_group = true; |
| 1948 |
} |
| 1949 |
|
| 1950 |
if (transparency_group && !ctx->device_state.preserve_smask) { |
| 1951 |
gs_setstrokeconstantalpha(ctx->pgs, 1.0); |
| 1952 |
gs_setfillconstantalpha(ctx->pgs, 1.0); |
| 1953 |
} |
| 1954 |
|
| 1955 |
/* Get the Mask data either as an array or a dict, if present */ |
| 1956 |
if (image_info.Mask != NULL) { |
| 1957 |
switch (pdfi_type_of(image_info.Mask)) { |
| 1958 |
case PDF_ARRAY: |
| 1959 |
mask_array = (pdf_array *)image_info.Mask; |
| 1960 |
break; |
| 1961 |
case PDF_STREAM: |
| 1962 |
mask_stream = (pdf_stream *)image_info.Mask; |
| 1963 |
code = pdfi_get_image_info(ctx, mask_stream, page_dict, |
| 1964 |
stream_dict, inline_image, &mask_info); |
| 1965 |
if (code < 0) |
| 1966 |
goto cleanupExit; |
| 1967 |
break; |
| 1968 |
default: |
| 1969 |
pdfi_countdown(image_info.Mask); |
| 1970 |
image_info.Mask = NULL; |
| 1971 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_MASK_ERROR, "pdfi_do_image", NULL)) < 0) |
| 1972 |
goto cleanupExit; |
| 1973 |
} |
| 1974 |
} |
| 1975 |
|
| 1976 |
/* Get the SMask info if we will need it (Type 3x images) */ |
| 1977 |
if (image_info.SMask && pdfi_type_of(image_info.SMask) == PDF_STREAM && ctx->device_state.preserve_smask) { |
| 1978 |
/* smask_dict non-NULL is used to flag a Type 3x image below */ |
| 1979 |
smask_stream = (pdf_stream *)image_info.SMask; |
| 1980 |
code = pdfi_get_image_info(ctx, smask_stream, page_dict, stream_dict, |
| 1981 |
inline_image, &smask_info); |
| 1982 |
if (code < 0) |
| 1983 |
goto cleanupExit; |
| 1984 |
} |
| 1985 |
|
| 1986 |
/* Get the image into a supported gs type (type1, type3, type4, type3x) */ |
| 1987 |
if (!image_info.Mask && !smask_stream) { /* Type 1 and ImageMask */ |
| 1988 |
memset(&t1image, 0, sizeof(t1image)); |
| 1989 |
pim = (gs_pixel_image_t *)&t1image; |
| 1990 |
|
| 1991 |
if (image_info.ImageMask) { |
| 1992 |
/* Sets up timage.ImageMask, amongst other things */ |
| 1993 |
gs_image_t_init_adjust(&t1image, NULL, false); |
| 1994 |
} else { |
| 1995 |
gs_image_t_init_adjust(&t1image, pcs, true); |
| 1996 |
} |
| 1997 |
} else if (smask_stream) { /* Type 3x */ |
| 1998 |
/* In the event where we have both /SMask and /Mask, favour /SMask |
| 1999 |
See tests_private/pdf/sumatra/1901_-_tiling_inconsistencies.pdf |
| 2000 |
*/ |
| 2001 |
mask_stream = NULL; |
| 2002 |
code = pdfi_image_setup_type3x(ctx, &image_info, &t3ximage, &smask_info, comps); |
| 2003 |
if (code < 0) { |
| 2004 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(code), NULL, E_PDF_INVALID_MATTE, "pdfi_do_image", "")) < 0) |
| 2005 |
goto cleanupExitError; |
| 2006 |
/* If this got an error, setup as a Type 1 image */ |
| 2007 |
/* NOTE: I did this error-handling the same as for Type 4 image below. |
| 2008 |
* Dunno if it's better to do this or to just abort the whole image? |
| 2009 |
*/ |
| 2010 |
memset(&t1image, 0, sizeof(t1image)); |
| 2011 |
pim = (gs_pixel_image_t *)&t1image; |
| 2012 |
gs_image_t_init_adjust(&t1image, pcs, true); |
| 2013 |
smask_stream = mask_stream = NULL; |
| 2014 |
} else { |
| 2015 |
pim = (gs_pixel_image_t *)&t3ximage; |
| 2016 |
} |
| 2017 |
} else { |
| 2018 |
if (mask_array) { /* Type 4 */ |
| 2019 |
code = pdfi_image_setup_type4(ctx, &image_info, &t4image, mask_array, pcs); |
| 2020 |
if (code < 0) { |
| 2021 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_IMAGE_ERROR, "pdfi_do_image", "")) < 0) { |
| 2022 |
goto cleanupExit; |
| 2023 |
} |
| 2024 |
/* If this got an error, setup as a Type 1 image */ |
| 2025 |
memset(&t1image, 0, sizeof(t1image)); |
| 2026 |
pim = (gs_pixel_image_t *)&t1image; |
| 2027 |
gs_image_t_init_adjust(&t1image, pcs, true); |
| 2028 |
} else { |
| 2029 |
pim = (gs_pixel_image_t *)&t4image; |
| 2030 |
} |
| 2031 |
} else { /* Type 3 */ |
| 2032 |
memset(&t3image, 0, sizeof(t3image)); |
| 2033 |
pim = (gs_pixel_image_t *)&t3image; |
| 2034 |
gs_image3_t_init(&t3image, NULL, interleave_separate_source); |
| 2035 |
code = pdfi_data_image_params(ctx, &mask_info, &t3image.MaskDict, 1, NULL); |
| 2036 |
if (code < 0) |
| 2037 |
goto cleanupExit; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
|
| 2041 |
|
| 2042 |
/* At this point pim points to a structure containing the specific type |
| 2043 |
* of image, and then we can handle it generically from here. |
| 2044 |
* The underlying gs image functions will do different things for different |
| 2045 |
* types of images. |
| 2046 |
*/ |
| 2047 |
|
| 2048 |
/* Setup the common params */ |
| 2049 |
pim->ColorSpace = pcs; |
| 2050 |
code = pdfi_data_image_params(ctx, &image_info, (gs_data_image_t *)pim, comps, pcs); |
| 2051 |
if (code < 0) |
| 2052 |
goto cleanupExit; |
| 2053 |
|
| 2054 |
/* Grab the mask_image data buffer in advance. |
| 2055 |
* Doing it this way because I don't want to muck with reading from |
| 2056 |
* two streams simultaneously -- not even sure that is feasible? |
| 2057 |
*/ |
| 2058 |
if (smask_stream) { |
| 2059 |
mask_size = ((((smask_info.Width * smask_info.BPC) + 7) / 8) * smask_info.Height); |
| 2060 |
/* This will happen only in case of PreserveSMask (Type 3x) */ |
| 2061 |
code = pdfi_stream_to_buffer(ctx, smask_stream, &mask_buffer, (int64_t *)&mask_size); |
| 2062 |
if (code < 0) |
| 2063 |
goto cleanupExit; |
| 2064 |
} else if (mask_stream) { |
| 2065 |
/* Calculate expected mask size */ |
| 2066 |
mask_size = ((((t3image.MaskDict.BitsPerComponent * (int64_t)t3image.MaskDict.Width) + 7) / 8) * (int64_t)t3image.MaskDict.Height); |
| 2067 |
code = pdfi_stream_to_buffer(ctx, mask_stream, &mask_buffer, (int64_t *)&mask_size); |
| 2068 |
if (code < 0) |
| 2069 |
goto cleanupExit; |
| 2070 |
} |
| 2071 |
/* Setup the data stream for the image data */ |
| 2072 |
if (!inline_image) { |
| 2073 |
pdfi_seek(ctx, source, stream_offset, SEEK_SET); |
| 2074 |
|
| 2075 |
code = pdfi_apply_SubFileDecode_filter(ctx, 0, "endstream", source, &SFD_stream, false); |
| 2076 |
if (code < 0) |
| 2077 |
goto cleanupExit; |
| 2078 |
source = SFD_stream; |
| 2079 |
} |
| 2080 |
|
| 2081 |
code = pdfi_filter(ctx, image_stream, source, &new_stream, inline_image); |
| 2082 |
if (code < 0) |
| 2083 |
goto cleanupExit; |
| 2084 |
|
| 2085 |
/* This duplicates the code in gs_img.ps; if we have an imagemask, with 1 bit per component (is there any other kind ?) |
| 2086 |
* and the image is to be interpolated, and we are nto sending it to a high level device. Then check the scaling. |
| 2087 |
* If we are scaling up (in device space) by afactor of more than 2, then we install the ImScaleDecode filter, |
| 2088 |
* which interpolates the input data by a factor of 4. |
| 2089 |
* The scaling of 2 is arbitrary (says so in gs_img.ps) but we use it for consistency. The scaling of the input |
| 2090 |
* by 4 is just a magic number, the scaling is always by 4, and we need to know it so we can adjust the Image Matrix |
| 2091 |
* and Width and Height values. |
| 2092 |
*/ |
| 2093 |
if (image_info.ImageMask == 1 && image_info.BPC == 1 && image_info.Interpolate == 1 && !ctx->device_state.HighLevelDevice) |
| 2094 |
{ |
| 2095 |
pdf_c_stream *s = new_stream; |
| 2096 |
gs_matrix mat4 = {4, 0, 0, 4, 0, 0}, inverseIM; |
| 2097 |
gs_point pt, pt1; |
| 2098 |
float s1, s2; |
| 2099 |
|
| 2100 |
code = gs_matrix_invert(&pim->ImageMatrix, &inverseIM); |
| 2101 |
if (code < 0) |
| 2102 |
goto cleanupExit; |
| 2103 |
|
| 2104 |
code = gs_distance_transform(0, 1, &inverseIM, &pt); |
| 2105 |
if (code < 0) |
| 2106 |
goto cleanupExit; |
| 2107 |
|
| 2108 |
code = gs_distance_transform(pt.x, pt.y, &ctm_only(ctx->pgs), &pt1); |
| 2109 |
if (code < 0) |
| 2110 |
goto cleanupExit; |
| 2111 |
|
| 2112 |
s1 = sqrt(pt1.x * pt1.x + pt1.y * pt1.y); |
| 2113 |
|
| 2114 |
code = gs_distance_transform(1, 0, &inverseIM, &pt); |
| 2115 |
if (code < 0) |
| 2116 |
goto cleanupExit; |
| 2117 |
|
| 2118 |
code = gs_distance_transform(pt.x, pt.y, &ctm_only(ctx->pgs), &pt1); |
| 2119 |
if (code < 0) |
| 2120 |
goto cleanupExit; |
| 2121 |
|
| 2122 |
s2 = sqrt(pt1.x * pt1.x + pt1.y * pt1.y); |
| 2123 |
|
| 2124 |
if (s1 > 2.0 || s2 > 2.0) { |
| 2125 |
code = pdfi_apply_imscale_filter(ctx, 0, image_info.Width, image_info.Height, s, &new_stream); |
| 2126 |
if (code < 0) |
| 2127 |
goto cleanupExit; |
| 2128 |
/* This adds the filter to the 'chain' of filter we created. When we close this filter |
| 2129 |
* it closes all the filters in the chain, back to either the SubFileDecode filter or the |
| 2130 |
* original main stream. If we don't patch this up then we leak memory for any filters |
| 2131 |
* applied in pdfi_filter above. |
| 2132 |
*/ |
| 2133 |
new_stream->original = s->original; |
| 2134 |
|
| 2135 |
/* We'e created a new 'new_stream', which is a C stream, to hold the filter chain |
| 2136 |
* but we still need to free the original C stream 'wrapper' we created with pdfi_filter() |
| 2137 |
* Do that now. |
| 2138 |
*/ |
| 2139 |
gs_free_object(ctx->memory, s, "free stream replaced by adding image scaling filter"); |
| 2140 |
image_info.Width *= 4; |
| 2141 |
image_info.Height *= 4; |
| 2142 |
pim->Width *= 4; |
| 2143 |
pim->Height *= 4; |
| 2144 |
code = gs_matrix_multiply(&pim->ImageMatrix, &mat4, &pim->ImageMatrix); |
| 2145 |
if (code < 0) |
| 2146 |
goto cleanupExit; |
| 2147 |
} |
| 2148 |
} |
| 2149 |
|
| 2150 |
trans_required = pdfi_trans_required(ctx); |
| 2151 |
|
| 2152 |
if (trans_required) { |
| 2153 |
code = pdfi_image_setup_trans(ctx, &trans_state); |
| 2154 |
if (code < 0) |
| 2155 |
goto cleanupExit; |
| 2156 |
} |
| 2157 |
|
| 2158 |
/* Render the image */ |
| 2159 |
code = pdfi_render_image(ctx, pim, new_stream, |
| 2160 |
mask_buffer, mask_size, |
| 2161 |
comps, image_info.ImageMask); |
| 2162 |
if (code < 0) { |
| 2163 |
if (ctx->args.pdfdebug) |
| 2164 |
outprintf(ctx->memory, "WARNING: pdfi_do_image: error %d from pdfi_render_image\n", code); |
| 2165 |
} |
| 2166 |
|
| 2167 |
if (trans_required) { |
| 2168 |
code1 = pdfi_trans_teardown(ctx, &trans_state); |
| 2169 |
if (code == 0) |
| 2170 |
code = code1; |
| 2171 |
} |
| 2172 |
|
| 2173 |
cleanupExit: |
| 2174 |
if (code < 0) |
| 2175 |
code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_IMAGE_ERROR, "pdfi_do_image", NULL); |
| 2176 |
|
| 2177 |
cleanupExitError: |
| 2178 |
if (transparency_group) { |
| 2179 |
pdfi_trans_end_isolated_group(ctx); |
| 2180 |
if (need_smask_cleanup) |
| 2181 |
pdfi_trans_end_smask_notify(ctx); |
| 2182 |
} |
| 2183 |
|
| 2184 |
/* See note above on the pdfi_op_q call. This does a grestore, and |
| 2185 |
* pops the transparency state. */ |
| 2186 |
(void)pdfi_op_Q(ctx); |
| 2187 |
|
| 2188 |
if (new_stream) |
| 2189 |
pdfi_close_file(ctx, new_stream); |
| 2190 |
if (SFD_stream) |
| 2191 |
pdfi_close_file(ctx, SFD_stream); |
| 2192 |
if (mask_buffer) |
| 2193 |
gs_free_object(ctx->memory, mask_buffer, "pdfi_do_image (mask_buffer)"); |
| 2194 |
|
| 2195 |
pdfi_countdown(alt_stream); |
| 2196 |
|
| 2197 |
pdfi_free_image_info_components(&image_info); |
| 2198 |
pdfi_free_image_info_components(&mask_info); |
| 2199 |
pdfi_free_image_info_components(&smask_info); |
| 2200 |
|
| 2201 |
if (pcs != NULL) |
| 2202 |
rc_decrement_only_cs(pcs, "pdfi_do_image"); |
| 2203 |
|
| 2204 |
#if DEBUG_IMAGES |
| 2205 |
dbgmprintf(ctx->memory, "pdfi_do_image END\n"); |
| 2206 |
#endif |
| 2207 |
return code; |
| 2208 |
} |
| 2209 |
|
| 2210 |
int pdfi_ID(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_c_stream *source) |
| 2211 |
{ |
| 2212 |
pdf_dict *d = NULL; |
| 2213 |
int code; |
| 2214 |
pdf_stream *image_stream; |
| 2215 |
|
| 2216 |
if (ctx->text.BlockDepth != 0) { |
| 2217 |
ctx->text.BlockDepth = 0; |
| 2218 |
if (ctx->text.TextClip) { |
| 2219 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 2220 |
|
| 2221 |
ctx->text.TextClip = false; |
| 2222 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 2223 |
} |
| 2224 |
code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_ID", NULL); |
| 2225 |
if (code < 0) |
| 2226 |
return code; |
| 2227 |
} |
| 2228 |
|
| 2229 |
/* we want to have the indirect_num and indirect_gen of the created dictionary |
| 2230 |
* be 0, because we are reading from a stream, and the stream has already |
| 2231 |
* been decrypted, we don't need to decrypt any strings contained in the |
| 2232 |
* inline dictionary. |
| 2233 |
*/ |
| 2234 |
code = pdfi_dict_from_stack(ctx, 0, 0, false); |
| 2235 |
if (code < 0) |
| 2236 |
/* pdfi_dict_from_stack cleans up the stack so we don't need to in case of an error */ |
| 2237 |
return code; |
| 2238 |
|
| 2239 |
d = (pdf_dict *)ctx->stack_top[-1]; |
| 2240 |
pdfi_countup(d); |
| 2241 |
pdfi_pop(ctx, 1); |
| 2242 |
|
| 2243 |
code = pdfi_obj_dict_to_stream(ctx, d, &image_stream, true); |
| 2244 |
if (code < 0) |
| 2245 |
goto error; |
| 2246 |
|
| 2247 |
code = pdfi_do_image(ctx, page_dict, stream_dict, image_stream, source, true); |
| 2248 |
error: |
| 2249 |
pdfi_countdown(image_stream); |
| 2250 |
pdfi_countdown(d); |
| 2251 |
return code; |
| 2252 |
} |
| 2253 |
|
| 2254 |
int pdfi_EI(pdf_context *ctx) |
| 2255 |
{ |
| 2256 |
if (ctx->text.BlockDepth != 0) { |
| 2257 |
ctx->text.BlockDepth = 0; |
| 2258 |
if (ctx->text.TextClip) { |
| 2259 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 2260 |
|
| 2261 |
ctx->text.TextClip = false; |
| 2262 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 2263 |
} |
| 2264 |
return pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_EI", NULL); |
| 2265 |
} |
| 2266 |
|
| 2267 |
/* pdfi_clearstack(ctx);*/ |
| 2268 |
return 0; |
| 2269 |
} |
| 2270 |
|
| 2271 |
/* see .execgroup */ |
| 2272 |
int pdfi_form_execgroup(pdf_context *ctx, pdf_dict *page_dict, pdf_stream *xobject_obj, |
| 2273 |
gs_gstate *GroupGState, gs_color_space *pcs, gs_client_color *pcc, gs_matrix *matrix) |
| 2274 |
{ |
| 2275 |
int code; |
| 2276 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)ctx->pgs->client_data; |
| 2277 |
|
| 2278 |
code = pdfi_gsave(ctx); |
| 2279 |
if (code < 0) |
| 2280 |
goto exit; |
| 2281 |
|
| 2282 |
if (GroupGState) { |
| 2283 |
code = pdfi_gs_setgstate(ctx->pgs, GroupGState); |
| 2284 |
if (code < 0) |
| 2285 |
goto exit2; |
| 2286 |
} |
| 2287 |
|
| 2288 |
/* Override the colorspace if specified */ |
| 2289 |
if (pcs) { |
| 2290 |
code = pdfi_gs_setcolorspace(ctx, pcs); |
| 2291 |
if (code < 0) |
| 2292 |
goto exit2; |
| 2293 |
code = gs_setcolor(ctx->pgs, (const gs_client_color *)pcc); |
| 2294 |
if (code < 0) |
| 2295 |
goto exit2; |
| 2296 |
} |
| 2297 |
|
| 2298 |
/* Disable the SMask */ |
| 2299 |
pdfi_gstate_smask_free(igs); |
| 2300 |
|
| 2301 |
gs_setblendmode(ctx->pgs, BLEND_MODE_Compatible); |
| 2302 |
gs_setstrokeconstantalpha(ctx->pgs, 1.0); |
| 2303 |
gs_setfillconstantalpha(ctx->pgs, 1.0); |
| 2304 |
|
| 2305 |
if (matrix) |
| 2306 |
code = gs_concat(ctx->pgs, matrix); |
| 2307 |
if (code < 0) { |
| 2308 |
goto exit2; |
| 2309 |
} |
| 2310 |
code = pdfi_run_context(ctx, xobject_obj, page_dict, false, "FORM"); |
| 2311 |
|
| 2312 |
exit2: |
| 2313 |
if (code != 0) |
| 2314 |
(void)pdfi_grestore(ctx); |
| 2315 |
else |
| 2316 |
code = pdfi_grestore(ctx); |
| 2317 |
exit: |
| 2318 |
return code; |
| 2319 |
} |
| 2320 |
|
| 2321 |
/* See zbeginform() */ |
| 2322 |
static int pdfi_form_highlevel_begin(pdf_context *ctx, pdf_dict *form_dict, gs_matrix *CTM, |
| 2323 |
gs_rect *BBox, gs_matrix *form_matrix) |
| 2324 |
{ |
| 2325 |
int code = 0; |
| 2326 |
gx_device *cdev = gs_currentdevice_inline(ctx->pgs); |
| 2327 |
gs_form_template_t template; |
| 2328 |
gs_point ll, ur; |
| 2329 |
gs_fixed_rect box; |
| 2330 |
|
| 2331 |
memset(&template, 0, sizeof(template)); |
| 2332 |
|
| 2333 |
template.CTM = *CTM; /* (structure copy) */ |
| 2334 |
template.BBox = *BBox; /* (structure copy) */ |
| 2335 |
template.form_matrix = *form_matrix; /* (structure copy) */ |
| 2336 |
template.FormID = -1; |
| 2337 |
template.pcpath = ctx->pgs->clip_path; |
| 2338 |
template.pgs = ctx->pgs; |
| 2339 |
|
| 2340 |
code = dev_proc(cdev, dev_spec_op)(cdev, gxdso_form_begin, &template, 0); |
| 2341 |
/* return value > 0 means the device sent us back a matrix |
| 2342 |
* and wants the CTM set to that. |
| 2343 |
* TODO: No idea if we need this nonsense. See zbeginform() |
| 2344 |
*/ |
| 2345 |
if (code > 0) |
| 2346 |
{ |
| 2347 |
gs_setmatrix(ctx->pgs, &template.CTM); |
| 2348 |
gs_distance_transform(template.BBox.p.x, template.BBox.p.y, &template.CTM, &ll); |
| 2349 |
gs_distance_transform(template.BBox.q.x, template.BBox.q.y, &template.CTM, &ur); |
| 2350 |
|
| 2351 |
/* A form can legitimately have negative co-ordinates in paths |
| 2352 |
* because it can be translated. But we always clip paths to the |
| 2353 |
* page which (clearly) can't have negative co-ordinates. NB this |
| 2354 |
* wouldn't be a problem if we didn't reset the CTM, but that would |
| 2355 |
* break the form capture. |
| 2356 |
* So here we temporarily set the clip to permit negative values, |
| 2357 |
* fortunately this works..... |
| 2358 |
*/ |
| 2359 |
/* We choose to permit negative values of the same magnitude as the |
| 2360 |
* positive ones. |
| 2361 |
*/ |
| 2362 |
|
| 2363 |
box.p.x = float2fixed(ll.x); |
| 2364 |
box.p.y = float2fixed(ll.y); |
| 2365 |
box.q.x = float2fixed(ur.x); |
| 2366 |
box.q.y = float2fixed(ur.y); |
| 2367 |
|
| 2368 |
if (box.p.x < 0) { |
| 2369 |
if(box.p.x * -1 > box.q.x) |
| 2370 |
box.q.x = box.p.x * -1; |
| 2371 |
} else { |
| 2372 |
if (fabs(ur.x) > fabs(ll.x)) |
| 2373 |
box.p.x = box.q.x * -1; |
| 2374 |
else { |
| 2375 |
box.p.x = float2fixed(ll.x * -1); |
| 2376 |
box.q.x = float2fixed(ll.x); |
| 2377 |
} |
| 2378 |
} |
| 2379 |
if (box.p.y < 0) { |
| 2380 |
if(box.p.y * -1 > box.q.y) |
| 2381 |
box.q.y = box.p.y * -1; |
| 2382 |
} else { |
| 2383 |
if (fabs(ur.y) > fabs(ll.y)) |
| 2384 |
box.p.y = box.q.y * -1; |
| 2385 |
else { |
| 2386 |
box.p.y = float2fixed(ll.y * -1); |
| 2387 |
box.q.y = float2fixed(ll.y); |
| 2388 |
} |
| 2389 |
} |
| 2390 |
/* This gets undone when we grestore after the form is executed */ |
| 2391 |
code = gx_clip_to_rectangle(ctx->pgs, &box); |
| 2392 |
} |
| 2393 |
|
| 2394 |
return code; |
| 2395 |
} |
| 2396 |
|
| 2397 |
/* See zendform() */ |
| 2398 |
static int pdfi_form_highlevel_end(pdf_context *ctx) |
| 2399 |
{ |
| 2400 |
int code = 0; |
| 2401 |
gx_device *cdev = gs_currentdevice_inline(ctx->pgs); |
| 2402 |
|
| 2403 |
code = dev_proc(cdev, dev_spec_op)(cdev, gxdso_form_end, 0, 0); |
| 2404 |
return code; |
| 2405 |
} |
| 2406 |
|
| 2407 |
/* See bug #702560. The original file has a Form XObject which is not a stream. Instead |
| 2408 |
* the Form XObject has a /Contents key which points to a stream dictionary. This is plainly |
| 2409 |
* illegal but, as always, Acrobat can open it.... |
| 2410 |
* If PDFSTOPONERROR is true then we just exit. Otherwise we look for a /Contents key in the stream |
| 2411 |
* dictionary. If we find one we dereference the object to get a stream dictionary, then merge the |
| 2412 |
* two dictionaries, ensuring the stream offset is correct, and proceed as if that's what we'd |
| 2413 |
* always had. If we don't have a /Contents key then exit with a typecheck error. |
| 2414 |
* |
| 2415 |
* Sets *hacked_stream to the /Contents stream if it found one. |
| 2416 |
*/ |
| 2417 |
static int pdfi_form_stream_hack(pdf_context *ctx, pdf_dict *form_dict, pdf_stream **hacked_stream) |
| 2418 |
{ |
| 2419 |
int code = 0; |
| 2420 |
pdf_stream *stream_obj = NULL; |
| 2421 |
pdf_obj *Parent = NULL; |
| 2422 |
pdf_dict *d = NULL; |
| 2423 |
pdf_dict *stream_dict = NULL; |
| 2424 |
|
| 2425 |
*hacked_stream = NULL; |
| 2426 |
|
| 2427 |
if (pdfi_type_of(form_dict) == PDF_STREAM) |
| 2428 |
return 0; |
| 2429 |
|
| 2430 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BADSTREAMDICT, "pdfi_form_stream_hack", NULL)) < 0) { |
| 2431 |
goto exit; |
| 2432 |
} |
| 2433 |
|
| 2434 |
code = pdfi_dict_knownget_type(ctx, form_dict, "Contents", PDF_STREAM, |
| 2435 |
(pdf_obj **)&stream_obj); |
| 2436 |
if (code < 0 || stream_obj == NULL) { |
| 2437 |
pdfi_set_error(ctx, 0, NULL, E_PDF_BADSTREAMDICT, "pdfi_form_stream_hack", NULL); |
| 2438 |
code = gs_note_error(gs_error_typecheck); |
| 2439 |
goto exit; |
| 2440 |
} |
| 2441 |
|
| 2442 |
d = form_dict; |
| 2443 |
pdfi_countup(d); |
| 2444 |
do { |
| 2445 |
code = pdfi_dict_knownget(ctx, d, "Parent", (pdf_obj **)&Parent); |
| 2446 |
if (code > 0 && pdfi_type_of(Parent) == PDF_DICT) { |
| 2447 |
if (Parent->object_num == stream_obj->object_num) { |
| 2448 |
pdfi_countdown(d); |
| 2449 |
pdfi_countdown(Parent); |
| 2450 |
pdfi_set_error(ctx, 0, NULL, E_PDF_BADSTREAMDICT, "pdfi_form_stream_hack", NULL); |
| 2451 |
code = gs_note_error(gs_error_undefined); |
| 2452 |
goto exit; |
| 2453 |
} |
| 2454 |
pdfi_countdown(d); |
| 2455 |
d = (pdf_dict *)Parent; |
| 2456 |
} else { |
| 2457 |
pdfi_countdown(d); |
| 2458 |
break; |
| 2459 |
} |
| 2460 |
} while (1); |
| 2461 |
|
| 2462 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict); |
| 2463 |
if (code < 0) { |
| 2464 |
pdfi_set_error(ctx, 0, NULL, E_PDF_BADSTREAMDICT, "pdfi_form_stream_hack", NULL); |
| 2465 |
goto exit; |
| 2466 |
} |
| 2467 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_STREAM_HAS_CONTENTS, "pdfi_form_stream_hack", NULL); |
| 2468 |
code = pdfi_merge_dicts(ctx, stream_dict, form_dict); |
| 2469 |
/* Having merged the dictionaries, we don't want the Contents key in the stream dict. |
| 2470 |
* We do want to leave it in the form dictionary, in case we use this form again. |
| 2471 |
* Leaving the reference in the stream dicttionary leads to a reference counting problem |
| 2472 |
* because stream_dict is contained in stream_obj so stream_obj becomes self-referencing. |
| 2473 |
*/ |
| 2474 |
pdfi_dict_delete(ctx, stream_dict, "Contents"); |
| 2475 |
|
| 2476 |
if (code == 0) { |
| 2477 |
*hacked_stream = stream_obj; |
| 2478 |
pdfi_countup(stream_obj); |
| 2479 |
} |
| 2480 |
|
| 2481 |
exit: |
| 2482 |
pdfi_countdown(stream_obj); |
| 2483 |
return code; |
| 2484 |
} |
| 2485 |
|
| 2486 |
static int pdfi_do_form(pdf_context *ctx, pdf_dict *page_dict, pdf_stream *form_obj) |
| 2487 |
{ |
| 2488 |
int code, code1 = 0; |
| 2489 |
bool group_known = false, known = false; |
| 2490 |
bool do_group = false; |
| 2491 |
pdf_array *FormMatrix = NULL; |
| 2492 |
gs_matrix formmatrix, CTM; |
| 2493 |
gs_rect bbox; |
| 2494 |
pdf_array *BBox = NULL; |
| 2495 |
bool save_PreservePDFForm; |
| 2496 |
pdf_stream *form_stream = NULL; /* Alias */ |
| 2497 |
pdf_stream *hacked_stream = NULL; |
| 2498 |
pdf_dict *form_dict; |
| 2499 |
gs_color_space *pcs = NULL; |
| 2500 |
gs_client_color cc, *pcc; |
| 2501 |
|
| 2502 |
#if DEBUG_IMAGES |
| 2503 |
dbgmprintf(ctx->memory, "pdfi_do_form BEGIN\n"); |
| 2504 |
#endif |
| 2505 |
if (pdfi_type_of(form_obj) != PDF_STREAM) { |
| 2506 |
code = pdfi_form_stream_hack(ctx, (pdf_dict *)form_obj, &hacked_stream); |
| 2507 |
if (code < 0) |
| 2508 |
return code; |
| 2509 |
form_stream = hacked_stream; |
| 2510 |
} else |
| 2511 |
form_stream = (pdf_stream *)form_obj; |
| 2512 |
|
| 2513 |
code = pdfi_dict_from_obj(ctx, (pdf_obj *)form_stream, &form_dict); |
| 2514 |
if (code < 0) |
| 2515 |
goto exit; |
| 2516 |
|
| 2517 |
code = pdfi_dict_known(ctx, form_dict, "Group", &group_known); |
| 2518 |
if (code < 0) |
| 2519 |
goto exit; |
| 2520 |
if (group_known && ctx->page.has_transparency) |
| 2521 |
do_group = true; |
| 2522 |
|
| 2523 |
/* Grab the CTM before it gets modified */ |
| 2524 |
code = gs_currentmatrix(ctx->pgs, &CTM); |
| 2525 |
if (code < 0) goto exit1; |
| 2526 |
|
| 2527 |
code = pdfi_op_q(ctx); |
| 2528 |
if (code < 0) goto exit1; |
| 2529 |
|
| 2530 |
code = pdfi_dict_knownget_type(ctx, form_dict, "Matrix", PDF_ARRAY, (pdf_obj **)&FormMatrix); |
| 2531 |
if (code < 0) goto exit1; |
| 2532 |
|
| 2533 |
code = pdfi_array_to_gs_matrix(ctx, FormMatrix, &formmatrix); |
| 2534 |
if (code < 0) goto exit1; |
| 2535 |
|
| 2536 |
code = pdfi_dict_known(ctx, form_dict, "BBox", &known); |
| 2537 |
if (known) { |
| 2538 |
code = pdfi_dict_get_type(ctx, form_dict, "BBox", PDF_ARRAY, (pdf_obj **)&BBox); |
| 2539 |
if (code < 0) goto exit1; |
| 2540 |
} else { |
| 2541 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_MISSING_BBOX, "pdfi_do_form", NULL)) < 0) { |
| 2542 |
goto exit; |
| 2543 |
} |
| 2544 |
} |
| 2545 |
|
| 2546 |
code = pdfi_array_to_gs_rect(ctx, BBox, &bbox); |
| 2547 |
if (code < 0) goto exit1; |
| 2548 |
|
| 2549 |
if (bbox.q.x - bbox.p.x == 0.0 || bbox.q.y - bbox.p.y == 0.0) { |
| 2550 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_FORM_CLIPPEDOUT, "pdfi_do_form", "")) < 0) |
| 2551 |
goto exit1; |
| 2552 |
|
| 2553 |
if (ctx->PreservePDFForm) { |
| 2554 |
code = pdfi_form_highlevel_begin(ctx, form_dict, &CTM, &bbox, &formmatrix); |
| 2555 |
if (code >= 0) |
| 2556 |
code = pdfi_form_highlevel_end(ctx); |
| 2557 |
} |
| 2558 |
goto exit1; |
| 2559 |
} |
| 2560 |
|
| 2561 |
code = gs_concat(ctx->pgs, &formmatrix); |
| 2562 |
if (code < 0) goto exit1; |
| 2563 |
|
| 2564 |
code = gs_rectclip(ctx->pgs, &bbox, 1); |
| 2565 |
if (code < 0) goto exit1; |
| 2566 |
|
| 2567 |
if (ctx->PreservePDFForm) { |
| 2568 |
code = pdfi_form_highlevel_begin(ctx, form_dict, &CTM, &bbox, &formmatrix); |
| 2569 |
if (code < 0) goto exit1; |
| 2570 |
} |
| 2571 |
save_PreservePDFForm = ctx->PreservePDFForm; |
| 2572 |
ctx->PreservePDFForm = false; /* Turn off in case there are any sub-forms */ |
| 2573 |
|
| 2574 |
if (do_group) { |
| 2575 |
code = pdfi_loop_detector_mark(ctx); |
| 2576 |
if (code < 0) goto exit1; |
| 2577 |
|
| 2578 |
/* Save the current color space in case it gets changed */ |
| 2579 |
pcs = gs_currentcolorspace(ctx->pgs); |
| 2580 |
rc_increment(pcs); |
| 2581 |
pcc = (gs_client_color *)gs_currentcolor(ctx->pgs); |
| 2582 |
cc = *pcc; |
| 2583 |
|
| 2584 |
code = pdfi_trans_begin_form_group(ctx, page_dict, form_dict); |
| 2585 |
(void)pdfi_loop_detector_cleartomark(ctx); |
| 2586 |
if (code < 0) goto exit1; |
| 2587 |
|
| 2588 |
code = pdfi_form_execgroup(ctx, page_dict, form_stream, NULL, pcs, &cc, NULL); |
| 2589 |
code1 = pdfi_trans_end_group(ctx); |
| 2590 |
if (code == 0) code = code1; |
| 2591 |
} else { |
| 2592 |
bool saved_decrypt_strings = ctx->encryption.decrypt_strings; |
| 2593 |
|
| 2594 |
/* We can run a Form even when we aren't running a page content stresm, |
| 2595 |
* eg for an annotation, and we need to *not* decrypt strings in that |
| 2596 |
* case (the content stream will be decrypted and strings in content |
| 2597 |
* streams are not additionally encrypted). |
| 2598 |
*/ |
| 2599 |
ctx->encryption.decrypt_strings = false; |
| 2600 |
code = pdfi_run_context(ctx, form_stream, page_dict, false, "FORM"); |
| 2601 |
ctx->encryption.decrypt_strings = saved_decrypt_strings; |
| 2602 |
} |
| 2603 |
|
| 2604 |
ctx->PreservePDFForm = save_PreservePDFForm; |
| 2605 |
if (ctx->PreservePDFForm) { |
| 2606 |
code = pdfi_form_highlevel_end(ctx); |
| 2607 |
} |
| 2608 |
|
| 2609 |
exit1: |
| 2610 |
code1 = pdfi_op_Q(ctx); |
| 2611 |
if (code == 0) code = code1; |
| 2612 |
|
| 2613 |
exit: |
| 2614 |
pdfi_countdown(FormMatrix); |
| 2615 |
pdfi_countdown(BBox); |
| 2616 |
pdfi_countdown(hacked_stream); |
| 2617 |
if (pcs) |
| 2618 |
rc_decrement_only_cs(pcs, "pdfi_do_form(pcs)"); |
| 2619 |
#if DEBUG_IMAGES |
| 2620 |
dbgmprintf(ctx->memory, "pdfi_do_form END\n"); |
| 2621 |
#endif |
| 2622 |
if (code < 0) |
| 2623 |
return code; |
| 2624 |
return 0; |
| 2625 |
} |
| 2626 |
|
| 2627 |
int pdfi_do_highlevel_form(pdf_context *ctx, pdf_dict *page_dict, pdf_stream *form_stream) |
| 2628 |
{ |
| 2629 |
int code = 0; |
| 2630 |
|
| 2631 |
ctx->PreservePDFForm = true; |
| 2632 |
code = pdfi_do_form(ctx, page_dict, form_stream); |
| 2633 |
ctx->PreservePDFForm = false; |
| 2634 |
|
| 2635 |
return code; |
| 2636 |
} |
| 2637 |
|
| 2638 |
int pdfi_do_image_or_form(pdf_context *ctx, pdf_dict *stream_dict, |
| 2639 |
pdf_dict *page_dict, pdf_obj *xobject_obj) |
| 2640 |
{ |
| 2641 |
int code; |
| 2642 |
pdf_name *n = NULL; |
| 2643 |
pdf_dict *xobject_dict; |
| 2644 |
bool known = false; |
| 2645 |
gs_offset_t savedoffset; |
| 2646 |
|
| 2647 |
code = pdfi_dict_from_obj(ctx, xobject_obj, &xobject_dict); |
| 2648 |
if (code < 0) |
| 2649 |
return code; |
| 2650 |
|
| 2651 |
/* Check Optional Content status */ |
| 2652 |
code = pdfi_dict_known(ctx, xobject_dict, "OC", &known); |
| 2653 |
if (code < 0) |
| 2654 |
return code; |
| 2655 |
|
| 2656 |
if (known) { |
| 2657 |
pdf_dict *OCDict = NULL; |
| 2658 |
bool visible = false; |
| 2659 |
gx_device *cdev = gs_currentdevice_inline(ctx->pgs); |
| 2660 |
|
| 2661 |
code = pdfi_dict_get(ctx, xobject_dict, "OC", (pdf_obj **)&OCDict); |
| 2662 |
if (code < 0) |
| 2663 |
return code; |
| 2664 |
|
| 2665 |
if (pdfi_type_of(OCDict) == PDF_DICT) { |
| 2666 |
char *label = NULL; |
| 2667 |
|
| 2668 |
if (ctx->device_state.writepdfmarks && ctx->device_state.WantsOptionalContent) { |
| 2669 |
code = pdfi_pdfmark_dict(ctx, OCDict); |
| 2670 |
if (code < 0) { |
| 2671 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_DO_OC_FAILED, "pdfi_do_image_or_form", NULL)) < 0) { |
| 2672 |
pdfi_countdown(OCDict); |
| 2673 |
return code; |
| 2674 |
} |
| 2675 |
} |
| 2676 |
code = pdfi_obj_get_label(ctx, (pdf_obj *)OCDict, &label); |
| 2677 |
if (code >= 0) { |
| 2678 |
code = dev_proc(cdev, dev_spec_op)(cdev, gxdso_pending_optional_content, label, 0); |
| 2679 |
gs_free_object(ctx->memory, label, ""); |
| 2680 |
if (code < 0) { |
| 2681 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_DO_OC_FAILED, "pdfi_do_image_or_form", NULL)) < 0) { |
| 2682 |
pdfi_countdown(OCDict); |
| 2683 |
return code; |
| 2684 |
} |
| 2685 |
} |
| 2686 |
} else { |
| 2687 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_DO_OC_FAILED, "pdfi_do_image_or_form", NULL)) < 0) { |
| 2688 |
pdfi_countdown(OCDict); |
| 2689 |
return code; |
| 2690 |
} |
| 2691 |
} |
| 2692 |
} else { |
| 2693 |
visible = pdfi_oc_is_ocg_visible(ctx, OCDict); |
| 2694 |
if (!visible) { |
| 2695 |
pdfi_countdown(OCDict); |
| 2696 |
return 0; |
| 2697 |
} |
| 2698 |
} |
| 2699 |
} else { |
| 2700 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_BAD_OCDICT, "pdfi_do_image_or_form", NULL)) < 0) |
| 2701 |
return code; |
| 2702 |
} |
| 2703 |
pdfi_countdown(OCDict); |
| 2704 |
} |
| 2705 |
|
| 2706 |
#if DEBUG_IMAGES |
| 2707 |
dbgmprintf1(ctx->memory, "pdfi_do_image_or_form BEGIN (OBJ = %d)\n", xobject_obj->object_num); |
| 2708 |
#endif |
| 2709 |
code = pdfi_trans_set_params(ctx); |
| 2710 |
if (code < 0) |
| 2711 |
return code; |
| 2712 |
|
| 2713 |
code = pdfi_dict_get(ctx, xobject_dict, "Subtype", (pdf_obj **)&n); |
| 2714 |
if (code < 0) { |
| 2715 |
if (code == gs_error_undefined) { |
| 2716 |
int code1 = 0; |
| 2717 |
/* This is illegal, because we have no way to tell is an XObject is a Form |
| 2718 |
* or Image object. However it seems Acrobat just assumes that it's a Form! |
| 2719 |
* See test file /tests_private/pdf/PDFIA1.7_SUBSET/CATX2063.pdf |
| 2720 |
*/ |
| 2721 |
code1 = pdfi_name_alloc(ctx, (byte *)"Form", 4, (pdf_obj **)&n); |
| 2722 |
if (code1 == 0) { |
| 2723 |
pdfi_countup(n); |
| 2724 |
} |
| 2725 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_NO_SUBTYPE, "pdfi_do_image_or_form", NULL)) < 0) { |
| 2726 |
goto exit; |
| 2727 |
} |
| 2728 |
} |
| 2729 |
else |
| 2730 |
goto exit; |
| 2731 |
} |
| 2732 |
if (pdfi_type_of(n) != PDF_NAME) { |
| 2733 |
code = gs_note_error(gs_error_typecheck); |
| 2734 |
goto exit; |
| 2735 |
} |
| 2736 |
|
| 2737 |
if (pdfi_name_is(n, "Image")) { |
| 2738 |
try_as_image: |
| 2739 |
if (pdfi_type_of(xobject_obj) != PDF_STREAM) { |
| 2740 |
code = gs_note_error(gs_error_typecheck); |
| 2741 |
goto exit; |
| 2742 |
} |
| 2743 |
savedoffset = pdfi_tell(ctx->main_stream); |
| 2744 |
code = pdfi_do_image(ctx, page_dict, stream_dict, (pdf_stream *)xobject_obj, |
| 2745 |
ctx->main_stream, false); |
| 2746 |
pdfi_seek(ctx, ctx->main_stream, savedoffset, SEEK_SET); |
| 2747 |
} else if (pdfi_name_is(n, "Form")) { |
| 2748 |
/* In theory a Form must be a stream, but we don't check that here |
| 2749 |
* because there is a broken case where it can be a dict. |
| 2750 |
* So pdfi_do_form() will handle that crazy case if it's not actually a stream. |
| 2751 |
*/ |
| 2752 |
code = pdfi_do_form(ctx, page_dict, (pdf_stream *)xobject_obj); |
| 2753 |
} else if (pdfi_name_is(n, "PS")) { |
| 2754 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_PS_XOBJECT_IGNORED, "pdfi_form_stream_hack", NULL); |
| 2755 |
} else { |
| 2756 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BAD_SUBTYPE, "pdfi_do_image_or_form", NULL); |
| 2757 |
if (code >= 0) |
| 2758 |
goto try_as_image; |
| 2759 |
} |
| 2760 |
|
| 2761 |
exit: |
| 2762 |
pdfi_countdown(n); |
| 2763 |
#if DEBUG_IMAGES |
| 2764 |
dbgmprintf(ctx->memory, "pdfi_do_image_or_form END\n"); |
| 2765 |
#endif |
| 2766 |
return code; |
| 2767 |
} |
| 2768 |
|
| 2769 |
int pdfi_Do(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict) |
| 2770 |
{ |
| 2771 |
int code = 0; |
| 2772 |
pdf_name *n = NULL; |
| 2773 |
pdf_obj *o = NULL; |
| 2774 |
pdf_dict *sdict = NULL; |
| 2775 |
bool known = false, AddedParent = false; |
| 2776 |
|
| 2777 |
if (pdfi_count_stack(ctx) < 1) { |
| 2778 |
code = gs_note_error(gs_error_stackunderflow); |
| 2779 |
goto exit1; |
| 2780 |
} |
| 2781 |
n = (pdf_name *)ctx->stack_top[-1]; |
| 2782 |
pdfi_countup(n); |
| 2783 |
pdfi_pop(ctx, 1); |
| 2784 |
|
| 2785 |
if (pdfi_type_of(n) != PDF_NAME) { |
| 2786 |
code = gs_note_error(gs_error_typecheck); |
| 2787 |
goto exit1; |
| 2788 |
} |
| 2789 |
|
| 2790 |
if (ctx->text.BlockDepth != 0) { |
| 2791 |
ctx->text.BlockDepth = 0; |
| 2792 |
if (ctx->text.TextClip) { |
| 2793 |
gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
| 2794 |
|
| 2795 |
ctx->text.TextClip = false; |
| 2796 |
(void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1); |
| 2797 |
} |
| 2798 |
code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_Do", NULL); |
| 2799 |
if (code < 0) |
| 2800 |
goto exit; |
| 2801 |
} |
| 2802 |
|
| 2803 |
code = pdfi_loop_detector_mark(ctx); |
| 2804 |
if (code < 0) |
| 2805 |
goto exit1; |
| 2806 |
code = pdfi_find_resource(ctx, (unsigned char *)"XObject", n, (pdf_dict *)stream_dict, page_dict, &o); |
| 2807 |
if (code < 0) |
| 2808 |
goto exit; |
| 2809 |
|
| 2810 |
if (pdfi_type_of(o) != PDF_STREAM && pdfi_type_of(o) != PDF_DICT) { |
| 2811 |
code = gs_note_error(gs_error_typecheck); |
| 2812 |
goto exit; |
| 2813 |
} |
| 2814 |
|
| 2815 |
/* This doesn't count up the stream dictionary, so we don't need to count it down later */ |
| 2816 |
code = pdfi_dict_from_obj(ctx, o, &sdict); |
| 2817 |
if (code < 0) |
| 2818 |
goto exit; |
| 2819 |
|
| 2820 |
code = pdfi_dict_known(ctx, sdict, "Parent", &known); |
| 2821 |
if (code < 0) |
| 2822 |
goto exit; |
| 2823 |
/* Add a Parent ref, unless it happens to be a circular reference |
| 2824 |
* (sample Bug298226.pdf -- has a circular ref and adding the Parent caused memory leak) |
| 2825 |
*/ |
| 2826 |
if (!known && sdict->object_num != stream_dict->object_num) { |
| 2827 |
code = pdfi_dict_put(ctx, sdict, "Parent", (pdf_obj *)stream_dict); |
| 2828 |
if (code < 0) |
| 2829 |
goto exit; |
| 2830 |
pdfi_countup(sdict); |
| 2831 |
AddedParent = true; |
| 2832 |
} |
| 2833 |
|
| 2834 |
(void)pdfi_loop_detector_cleartomark(ctx); |
| 2835 |
/* We used to have a pdfi_gsave/pdfi_grestore around this, this is actually done |
| 2836 |
* for us within the functions that pdfi_do_image_or_form calls now. */ |
| 2837 |
code = pdfi_do_image_or_form(ctx, stream_dict, page_dict, o); |
| 2838 |
pdfi_countdown(n); |
| 2839 |
pdfi_countdown(o); |
| 2840 |
if (AddedParent == true) { |
| 2841 |
if (code >= 0) |
| 2842 |
code = pdfi_dict_delete(ctx, sdict, "Parent"); |
| 2843 |
else |
| 2844 |
(void)pdfi_dict_delete(ctx, sdict, "Parent"); |
| 2845 |
pdfi_countdown(sdict); |
| 2846 |
} |
| 2847 |
return code; |
| 2848 |
|
| 2849 |
exit: |
| 2850 |
(void)pdfi_loop_detector_cleartomark(ctx); |
| 2851 |
exit1: |
| 2852 |
pdfi_countdown(n); |
| 2853 |
pdfi_countdown(o); |
| 2854 |
return code; |
| 2855 |
} |