Debian Codegraph @

pdf/pdf_colour.c

raw
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 /* colour operations for the PDF interpreter */
17
18 #include "pdf_int.h"
19 #include "pdf_doc.h"
20 #include "pdf_colour.h"
21 #include "pdf_pattern.h"
22 #include "pdf_stack.h"
23 #include "pdf_array.h"
24 #include "pdf_misc.h"
25 #include "gsicc_manage.h"
26 #include "gsicc_profilecache.h"
27 #include "gsicc_cache.h"
28
29 #include "gsicc_create.h"
30 #include "gsptype2.h"
31
32
33 #include "pdf_file.h"
34 #include "pdf_dict.h"
35 #include "pdf_loop_detect.h"
36 #include "pdf_func.h"
37 #include "pdf_shading.h"
38 #include "gscsepr.h"
39 #include "stream.h"
40 #include "strmio.h"
41 #include "gscdevn.h"
42 #include "gxcdevn.h"
43 #include "gscolor.h" /* For gs_setgray() and friends */
44 #include "gsicc.h" /* For gs_cspace_build_ICC() */
45 #include "gsstate.h" /* For gs_gdsvae() and gs_grestore() */
46 #include "gxdevsop.h"
47
48 /* Forward definitions for a routine we need */
49 static int pdfi_create_colorspace_by_array(pdf_context *ctx, pdf_array *color_array, int index,
50 pdf_dict *stream_dict, pdf_dict *page_dict,
51 gs_color_space **ppcs, bool inline_image);
52 static int pdfi_create_colorspace_by_name(pdf_context *ctx, pdf_name *name,
53 pdf_dict *stream_dict, pdf_dict *page_dict,
54 gs_color_space **ppcs, bool inline_image);
55
56 /* This is used only from the page level interpreter code, we need to know the number
57 * of spot colours in a PDF file, which we have to pass to the device for spot colour
58 * rendering. We deal with it here because its colour related.
59 * The PDF context has a page-level object which maintains a list of the spot colour
60 * names seen so far, so we can ensure we don't end up with duplictaes.
61 */
62 static int pdfi_check_for_spots_by_name(pdf_context *ctx, pdf_name *name,
63 pdf_dict *parent_dict, pdf_dict *page_dict, pdf_dict *spot_dict)
64 {
65 pdf_obj *ref_space;
66 int code;
67
68 if (pdfi_name_is(name, "G")) {
69 return 0;
70 } else if (pdfi_name_is(name, "RGB")) {
71 return 0;
72 } else if (pdfi_name_is(name, "CMYK")) {
73 return 0;
74 } else if (pdfi_name_is(name, "DeviceRGB")) {
75 return 0;
76 } else if (pdfi_name_is(name, "DeviceGray")) {
77 return 0;
78 } else if (pdfi_name_is(name, "DeviceCMYK")) {
79 return 0;
80 } else if (pdfi_name_is(name, "Pattern")) {
81 /* TODO: I think this is fine... */
82 return 0;
83 } else {
84 code = pdfi_find_resource(ctx, (unsigned char *)"ColorSpace", name, parent_dict, page_dict, &ref_space);
85 if (code < 0)
86 return code;
87
88 if (pdfi_type_of(ref_space) == PDF_NAME) {
89 if (pdfi_name_cmp(name, (pdf_name *)ref_space) == 0) {
90 pdfi_set_error(ctx, gs_error_circular_reference, NULL, E_PDF_CIRCULARNAME, NULL, NULL);
91 pdfi_countdown(ref_space);
92 return_error(gs_error_circular_reference);
93 }
94 }
95 /* recursion */
96 return pdfi_check_ColorSpace_for_spots(ctx, ref_space, parent_dict, page_dict, spot_dict);
97 }
98 return 0;
99 }
100
101 static int pdfi_check_for_spots_by_array(pdf_context *ctx, pdf_array *color_array,
102 pdf_dict *parent_dict, pdf_dict *page_dict, pdf_dict *spot_dict)
103 {
104 pdf_name *space = NULL;
105 pdf_array *a = NULL;
106 int code = 0;
107
108 if (!spot_dict)
109 return 0;
110
111 code = pdfi_array_get_type(ctx, color_array, 0, PDF_NAME, (pdf_obj **)&space);
112 if (code != 0)
113 goto exit;
114
115 code = 0;
116 if (pdfi_name_is(space, "G")) {
117 goto exit;
118 } else if (pdfi_name_is(space, "I") || pdfi_name_is(space, "Indexed")) {
119 pdf_obj *base_space;
120
121 code = pdfi_array_get(ctx, color_array, 1, &base_space);
122 if (code == 0) {
123 code = pdfi_check_ColorSpace_for_spots(ctx, base_space, parent_dict, page_dict, spot_dict);
124 (void)pdfi_countdown(base_space);
125 }
126 goto exit;
127 } else if (pdfi_name_is(space, "Pattern")) {
128 pdf_obj *base_space = NULL;
129 uint64_t size = pdfi_array_size(color_array);
130
131 /* Array of size 1 "[ /Pattern ]" is okay, just do nothing. */
132 if (size == 1)
133 goto exit;
134 /* Array of size > 2 we don't handle (shouldn't happen?) */
135 if (size != 2) {
136 dbgmprintf1(ctx->memory,
137 "WARNING: checking Pattern for spots, expected array size 2, got %lu\n",
138 size);
139 goto exit;
140 }
141 /* "[/Pattern base_space]" */
142 code = pdfi_array_get(ctx, color_array, 1, &base_space);
143 if (code == 0) {
144 code = pdfi_check_ColorSpace_for_spots(ctx, base_space, parent_dict, page_dict, spot_dict);
145 (void)pdfi_countdown(base_space);
146 }
147 goto exit;
148 } else if (pdfi_name_is(space, "Lab")) {
149 goto exit;
150 } else if (pdfi_name_is(space, "RGB")) {
151 goto exit;
152 } else if (pdfi_name_is(space, "CMYK")) {
153 goto exit;
154 } else if (pdfi_name_is(space, "CalRGB")) {
155 goto exit;
156 } else if (pdfi_name_is(space, "CalGray")) {
157 goto exit;
158 } else if (pdfi_name_is(space, "CalCMYK")) {
159 goto exit;
160 } else if (pdfi_name_is(space, "ICCBased")) {
161 goto exit;
162 } else if (pdfi_name_is(space, "DeviceRGB")) {
163 goto exit;
164 } else if (pdfi_name_is(space, "DeviceGray")) {
165 goto exit;
166 } else if (pdfi_name_is(space, "DeviceCMYK")) {
167 goto exit;
168 } else if (pdfi_name_is(space, "DeviceN")) {
169 bool known = false;
170 pdf_obj *dummy, *name;
171 int i;
172
173 pdfi_countdown(space);
174 code = pdfi_array_get_type(ctx, color_array, 1, PDF_ARRAY, (pdf_obj **)&space);
175 if (code != 0)
176 goto exit;
177
178 for (i=0;i < pdfi_array_size((pdf_array *)space); i++) {
179 code = pdfi_array_get_type(ctx, (pdf_array *)space, (uint64_t)i, PDF_NAME, &name);
180 if (code < 0)
181 goto exit;
182
183 if (pdfi_name_is((const pdf_name *)name, "Cyan") || pdfi_name_is((const pdf_name *)name, "Magenta") ||
184 pdfi_name_is((const pdf_name *)name, "Yellow") || pdfi_name_is((const pdf_name *)name, "Black") ||
185 pdfi_name_is((const pdf_name *)name, "None") || pdfi_name_is((const pdf_name *)name, "All")) {
186
187 pdfi_countdown(name);
188 continue;
189 }
190
191 code = pdfi_dict_known_by_key(ctx, spot_dict, (pdf_name *)name, &known);
192 if (code < 0) {
193 pdfi_countdown(name);
194 goto exit;
195 }
196 if (known) {
197 pdfi_countdown(name);
198 continue;
199 }
200
201 code = pdfi_object_alloc(ctx, PDF_INT, 0, &dummy);
202 if (code < 0)
203 goto exit;
204
205 code = pdfi_dict_put_obj(ctx, spot_dict, name, dummy, true);
206 pdfi_countdown(name);
207 if (code < 0)
208 break;
209 }
210 goto exit;
211 } else if (pdfi_name_is(space, "Separation")) {
212 bool known = false;
213 pdf_obj *dummy;
214
215 pdfi_countdown(space);
216 code = pdfi_array_get_type(ctx, color_array, 1, PDF_NAME, (pdf_obj **)&space);
217 if (code != 0)
218 goto exit;
219
220 if (pdfi_name_is((const pdf_name *)space, "Cyan") || pdfi_name_is((const pdf_name *)space, "Magenta") ||
221 pdfi_name_is((const pdf_name *)space, "Yellow") || pdfi_name_is((const pdf_name *)space, "Black") ||
222 pdfi_name_is((const pdf_name *)space, "None") || pdfi_name_is((const pdf_name *)space, "All"))
223 goto exit;
224 code = pdfi_dict_known_by_key(ctx, spot_dict, space, &known);
225 if (code < 0 || known)
226 goto exit;
227
228 code = pdfi_object_alloc(ctx, PDF_INT, 0, &dummy);
229 if (code < 0)
230 goto exit;
231
232 code = pdfi_dict_put_obj(ctx, spot_dict, (pdf_obj *)space, dummy, true);
233 goto exit;
234 } else {
235 code = pdfi_find_resource(ctx, (unsigned char *)"ColorSpace",
236 space, parent_dict, page_dict, (pdf_obj **)&a);
237 if (code < 0)
238 goto exit;
239
240 if (pdfi_type_of(a) != PDF_ARRAY) {
241 code = gs_note_error(gs_error_typecheck);
242 goto exit;
243 }
244 if (a == color_array) {
245 code = gs_note_error(gs_error_circular_reference);
246 goto exit;
247 }
248
249 /* recursion */
250 code = pdfi_check_for_spots_by_array(ctx, a, parent_dict, page_dict, spot_dict);
251 }
252
253 exit:
254 if (space)
255 pdfi_countdown(space);
256 if (a)
257 pdfi_countdown(a);
258 return code;
259 }
260
261 int pdfi_check_ColorSpace_for_spots(pdf_context *ctx, pdf_obj *space, pdf_dict *parent_dict,
262 pdf_dict *page_dict, pdf_dict *spot_dict)
263 {
264 int code;
265
266 if (!spot_dict)
267 return 0;
268
269 code = pdfi_loop_detector_mark(ctx);
270 if (code < 0)
271 return code;
272
273 switch(pdfi_type_of(space)) {
274 case PDF_NAME:
275 code = pdfi_check_for_spots_by_name(ctx, (pdf_name *)space, parent_dict, page_dict, spot_dict);
276 break;
277 case PDF_ARRAY:
278 code = pdfi_check_for_spots_by_array(ctx, (pdf_array *)space, parent_dict, page_dict, spot_dict);
279 break;
280 default:
281 pdfi_loop_detector_cleartomark(ctx);
282 return 0;
283 }
284
285 (void)pdfi_loop_detector_cleartomark(ctx);
286 return code;
287 }
288
289 /* Rendering intent is a bit of an oddity, but its clearly colour related, so we
290 * deal with it here. Cover it first to get it out of the way.
291 */
292 int pdfi_ri(pdf_context *ctx)
293 {
294 pdf_name *n;
295 int code;
296
297 if (pdfi_count_stack(ctx) < 1)
298 return_error(gs_error_stackunderflow);
299
300 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_NAME) {
301 pdfi_pop(ctx, 1);
302 return_error(gs_error_typecheck);
303 }
304 n = (pdf_name *)ctx->stack_top[-1];
305 pdfi_countup(n);
306 pdfi_pop(ctx, 1);
307 code = pdfi_setrenderingintent(ctx, n);
308 pdfi_countdown(n);
309 return code;
310 }
311
312 /*
313 * Pattern lifetime management turns out to be more complex than we would ideally like. Although
314 * Patterns are reference counted, and contain a client_data pointer, they don't have a gs_notify
315 * setup. This means that there's no simlpe way for us to be informed when a Pattern is released
316 * We could patch up the Pattern finalize() method, replacing it with one of our own which calls
317 * the original finalize() but that seems like a really nasty hack.
318 * For the time being we put code in pdfi_grestore() to check for Pattern colour spaces being
319 * restored away, but we also need to check for Pattern spaces being replaced in the current
320 * graphics state. We define 'pdfi' variants of several graphics library colour management
321 * functions to 'wrap' these with code to check for replacement of Patterns.
322 * This comment is duplicated in pdf_pattern.c
323 */
324
325 static void pdfi_cspace_free_callback(gs_memory_t * mem, void *cs)
326 {
327 gs_color_space *pcs = (gs_color_space *)cs;
328 pdf_obj *o = (pdf_obj *)pcs->interpreter_data;
329 pdf_context *ctx = NULL;
330 gs_function_t *pfn;
331
332 if (o == NULL)
333 return;
334
335 ctx = o->ctx;
336
337 if (gs_color_space_get_index(pcs) == gs_color_space_index_Separation) {
338 /* Handle cleanup of Separation functions if applicable */
339 pfn = gs_cspace_get_sepr_function(pcs);
340 if (pfn)
341 pdfi_free_function(ctx, pfn);
342 }
343
344 if (gs_color_space_get_index(pcs) == gs_color_space_index_DeviceN) {
345 /* Handle cleanup of DeviceN functions if applicable */
346 pfn = gs_cspace_get_devn_function(pcs);
347 if (pfn)
348 pdfi_free_function(ctx, pfn);
349 }
350 if (pdfi_type_of(o) != PDF_CTX) {
351 pdfi_countdown(o);
352 pcs->interpreter_data = NULL;
353 }
354 }
355
356 int pdfi_gs_setgray(pdf_context *ctx, double d)
357 {
358 int code = 0;
359 gs_color_space *pcs = ctx->pgs->color[0].color_space;
360
361 /* PDF Reference 1.7 p423, any colour operators in a CharProc, following a d1, should be ignored */
362 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
363 pdfi_log_info(ctx, "pdfi_gs_setgray", "colour operator in a CharProc, following a d1 ignored\n");
364 return 0;
365 }
366
367 if (ctx->page.DefaultGray_cs != NULL) {
368 gs_client_color cc;
369
370 code = gs_setcolorspace(ctx->pgs, ctx->page.DefaultGray_cs);
371 if (code < 0)
372 return code;
373 /* If we didn't change the colour space in the graphics state, do not attempt to
374 * set the callbacks, the current space might be inherited from PostScript.
375 */
376 if (pcs != ctx->pgs->color[0].color_space)
377 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
378 cc.paint.values[0] = d;
379 cc.pattern = 0;
380 return gs_setcolor(ctx->pgs, &cc);
381 } else {
382 code = gs_setgray(ctx->pgs, d);
383 if (code < 0)
384 return code;
385 }
386 /* If we didn't change the colour space in the graphics state, do not attempt to
387 * set the callbacks, the current space might be inherited from PostScript.
388 */
389 if (pcs != ctx->pgs->color[0].color_space)
390 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
391 return 0;
392 }
393
394 int pdfi_gs_setrgbcolor(pdf_context *ctx, double r, double g, double b)
395 {
396 int code = 0;
397 gs_color_space *pcs = ctx->pgs->color[0].color_space;
398
399 /* PDF Reference 1.7 p423, any colour operators in a CharProc, following a d1, should be ignored */
400 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
401 pdfi_log_info(ctx, "pdfi_gs_setrgbcolor", "colour operator in a CharProc, following a d1 ignored\n");
402 return 0;
403 }
404
405 if (ctx->page.DefaultRGB_cs != NULL) {
406 gs_client_color cc;
407
408 code = gs_setcolorspace(ctx->pgs, ctx->page.DefaultRGB_cs);
409 if (code < 0)
410 return code;
411 /* If we didn't change the colour space in the graphics state, do not attempt to
412 * set the callbacks, the current space might be inherited from PostScript.
413 */
414 if (pcs != ctx->pgs->color[0].color_space)
415 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
416 cc.paint.values[0] = r;
417 cc.paint.values[1] = g;
418 cc.paint.values[2] = b;
419 cc.pattern = 0;
420 return gs_setcolor(ctx->pgs, &cc);
421 } else {
422 code = gs_setrgbcolor(ctx->pgs, r, g, b);
423 if (code < 0)
424 return code;
425 /* If we didn't change the colour space in the graphics state, do not attempt to
426 * set the callbacks, the current space might be inherited from PostScript.
427 */
428 if (pcs != ctx->pgs->color[0].color_space)
429 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
430 }
431 return 0;
432 }
433
434 static int pdfi_gs_setcmykcolor(pdf_context *ctx, double c, double m, double y, double k)
435 {
436 int code = 0;
437 gs_color_space *pcs = ctx->pgs->color[0].color_space;
438
439 /* PDF Reference 1.7 p423, any colour operators in a CharProc, following a d1, should be ignored */
440 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
441 pdfi_log_info(ctx, "pdfi_gs_setcmykcolor", "colour operator in a CharProc, following a d1 ignored\n");
442 return 0;
443 }
444
445 if (ctx->page.DefaultCMYK_cs != NULL) {
446 gs_client_color cc;
447
448 code = gs_setcolorspace(ctx->pgs, ctx->page.DefaultCMYK_cs);
449 if (code < 0)
450 return code;
451 /* If we didn't change the colour space in the graphics state, do not attempt to
452 * set the callbacks, the current space might be inherited from PostScript.
453 */
454 if (pcs != ctx->pgs->color[0].color_space)
455 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
456 cc.paint.values[0] = c;
457 cc.paint.values[1] = m;
458 cc.paint.values[2] = y;
459 cc.paint.values[3] = k;
460 cc.pattern = 0;
461 return gs_setcolor(ctx->pgs, &cc);
462 } else {
463 code = gs_setcmykcolor(ctx->pgs, c, m, y, k);
464 if (code < 0)
465 return code;
466 /* If we didn't change the colour space in the graphics state, do not attempt to
467 * set the callbacks, the current space might be inherited from PostScript.
468 */
469 if (pcs != ctx->pgs->color[0].color_space)
470 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
471 }
472 return 0;
473 }
474
475 int pdfi_gs_setcolorspace(pdf_context *ctx, gs_color_space *pcs)
476 {
477 gs_color_space *old_pcs = ctx->pgs->color[0].color_space;
478 int code = 0;
479 /* If the target colour space is already the current colour space, don't
480 * bother to do anything.
481 */
482 if (ctx->pgs->color[0].color_space->id != pcs->id) {
483 /* PDF Reference 1.7 p423, any colour operators in a CharProc, following a d1, should be ignored */
484 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
485 pdfi_log_info(ctx, "pdfi_gs_setcolorspace", "colour operator in a CharProc, following a d1 ignored\n");
486 return 0;
487 }
488
489 code = gs_setcolorspace(ctx->pgs, pcs);
490 if (code < 0)
491 return code;
492 /* If we didn't change the colour space in the graphics state, do not attempt to
493 * set the callbacks, the current space might be inherited from PostScript.
494 */
495 if (old_pcs != ctx->pgs->color[0].color_space)
496 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
497 }
498 return 0;
499 }
500
501 /* Start with the simple cases, where we set the colour space and colour in a single operation */
502 int pdfi_setgraystroke(pdf_context *ctx)
503 {
504 int code;
505 double d1;
506
507 code = pdfi_destack_real(ctx, &d1);
508 if (code < 0)
509 return code;
510
511 gs_swapcolors_quick(ctx->pgs);
512 code = pdfi_gs_setgray(ctx, d1);
513 gs_swapcolors_quick(ctx->pgs);
514
515 return code;
516 }
517
518 int pdfi_setgrayfill(pdf_context *ctx)
519 {
520 int code;
521 double d1;
522
523 code = pdfi_destack_real(ctx, &d1);
524 if (code < 0)
525 return code;
526
527 return pdfi_gs_setgray(ctx, d1);
528 }
529
530 int pdfi_setrgbstroke(pdf_context *ctx)
531 {
532 double Values[3];
533 int code;
534
535 code = pdfi_destack_reals(ctx, Values, 3);
536 if (code < 0)
537 return code;
538
539 gs_swapcolors_quick(ctx->pgs);
540 code = pdfi_gs_setrgbcolor(ctx, Values[0], Values[1], Values[2]);
541 gs_swapcolors_quick(ctx->pgs);
542
543 return code;
544 }
545
546 /* Non-standard operator that is used in some annotation /DA
547 * Expects stack to be [r g b]
548 */
549 int pdfi_setrgbfill_array(pdf_context *ctx)
550 {
551 int code;
552 pdf_array *array = NULL;
553
554 pdfi_set_warning(ctx, 0, NULL, W_PDF_NONSTANDARD_OP, "pdfi_setrgbfill_array", (char *)"WARNING: Non-standard 'r' operator");
555
556 if (pdfi_count_stack(ctx) < 1)
557 return_error(gs_error_stackunderflow);
558
559 array = (pdf_array *)ctx->stack_top[-1];
560 pdfi_countup(array);
561 pdfi_pop(ctx, 1);
562 if (pdfi_type_of(array) != PDF_ARRAY) {
563 code = gs_note_error(gs_error_typecheck);
564 goto exit;
565 }
566
567 code = pdfi_setcolor_from_array(ctx, array);
568 exit:
569 pdfi_countdown(array);
570 return code;
571 }
572
573 int pdfi_setrgbfill(pdf_context *ctx)
574 {
575 double Values[3];
576 int code;
577
578 code = pdfi_destack_reals(ctx, Values, 3);
579 if (code < 0)
580 return code;
581
582 return pdfi_gs_setrgbcolor(ctx, Values[0], Values[1], Values[2]);
583 }
584
585 int pdfi_setcmykstroke(pdf_context *ctx)
586 {
587 double Values[4];
588 int code;
589
590 code = pdfi_destack_reals(ctx, Values, 4);
591 if (code < 0)
592 return code;
593
594 gs_swapcolors_quick(ctx->pgs);
595 code = pdfi_gs_setcmykcolor(ctx, Values[0], Values[1], Values[2], Values[3]);
596 gs_swapcolors_quick(ctx->pgs);
597
598 return code;
599 }
600
601 int pdfi_setcmykfill(pdf_context *ctx)
602 {
603 double Values[4];
604 int code;
605
606 code = pdfi_destack_reals(ctx, Values, 4);
607 if (code < 0)
608 return code;
609
610 return pdfi_gs_setcmykcolor(ctx, Values[0], Values[1], Values[2], Values[3]);
611 }
612
613 /* Do a setcolor using values in an array
614 * Will do gray, rgb, cmyk for sizes 1,3,4
615 * Anything else is an error
616 */
617 int pdfi_setcolor_from_array(pdf_context *ctx, pdf_array *array)
618 {
619 int code = 0;
620 uint64_t size;
621 double values[4];
622
623 size = pdfi_array_size(array);
624 if (size != 1 && size != 3 && size != 4) {
625 code = gs_note_error(gs_error_rangecheck);
626 goto exit;
627 }
628
629 code = pdfi_array_to_num_array(ctx, array, values, 0, size);
630 if (code < 0) goto exit;
631
632 switch (size) {
633 case 1:
634 code = pdfi_gs_setgray(ctx, values[0]);
635 break;
636 case 3:
637 code = pdfi_gs_setrgbcolor(ctx, values[0], values[1], values[2]);
638 break;
639 case 4:
640 code = pdfi_gs_setcmykcolor(ctx, values[0], values[1], values[2], values[3]);
641 break;
642 default:
643 break;
644 }
645
646 exit:
647 return code;
648 }
649
650 /* Get colors from top of stack into a client color */
651 static int
652 pdfi_get_color_from_stack(pdf_context *ctx, gs_client_color *cc, int ncomps)
653 {
654 int i, code;
655
656 if (pdfi_count_stack(ctx) < ncomps) {
657 pdfi_clearstack(ctx);
658 return_error(gs_error_stackunderflow);
659 }
660
661 for (i = 0; i < ncomps; i++) {
662 code = pdfi_obj_to_float(ctx, ctx->stack_top[i - ncomps], &cc->paint.values[i]);
663 if (code < 0) {
664 pdfi_clearstack(ctx);
665 return code;
666 }
667 }
668 pdfi_pop(ctx, ncomps);
669
670 return 0;
671 }
672
673 /* Now deal with the case where we have to set the colour space separately from the
674 * colour values. We'll start with the routines to set the colour, because setting
675 * colour components is relatively easy.
676 */
677
678 /* First up, the SC and sc operators. These set the colour for all spaces *except*
679 * ICCBased, Pattern, Separation and DeviceN
680 */
681 int pdfi_setstrokecolor(pdf_context *ctx)
682 {
683 const gs_color_space * pcs;
684 int ncomps, code;
685 gs_client_color cc;
686
687 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
688 /* There will have been a preceding operator to set the colour space, which we
689 * will have ignored, so now we don't know how many components to expect!
690 * Just clear the stack and hope for the best.
691 */
692 pdfi_clearstack(ctx);
693 pdfi_log_info(ctx, "pdfi_setstrokecolor", "colour operator in a CharProc, following a d1 ignored\n");
694 return 0;
695 }
696
697 cc.pattern = 0;
698 gs_swapcolors_quick(ctx->pgs);
699 pcs = gs_currentcolorspace(ctx->pgs);
700 ncomps = cs_num_components(pcs);
701 if (ncomps < 1) {
702 gs_swapcolors_quick(ctx->pgs);
703 return_error(gs_error_syntaxerror);
704 }
705 code = pdfi_get_color_from_stack(ctx, &cc, ncomps);
706 if (code == 0) {
707 if (pcs->type == &gs_color_space_type_Indexed) {
708 /* Special handling for floating point colour values
709 * PostScript doesn't specify what should happen with a float
710 * lookup value. PDF says 'nearest integer' and round 0.5 up.
711 * Doing this in the graphics library caused some (tiny) differences
712 * in output files, so instead perform that check here.
713 * The out of range clamping is already correct in the graphics library.
714 */
715 int index = (int)floor(cc.paint.values[0]);
716
717 if(cc.paint.values[0] - index > 0.49999)
718 index++;
719 cc.paint.values[0] = (float)index;
720 }
721 code = gs_setcolor(ctx->pgs, &cc);
722 }
723 gs_swapcolors_quick(ctx->pgs);
724 return code;
725 }
726
727 int pdfi_setfillcolor(pdf_context *ctx)
728 {
729 const gs_color_space * pcs = gs_currentcolorspace(ctx->pgs);
730 int ncomps, code;
731 gs_client_color cc;
732
733 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
734 /* There will have been a preceding operator to set the colour space, which we
735 * will have ignored, so now we don't know how many components to expect!
736 * Just clear the stack and hope for the best.
737 */
738 pdfi_clearstack(ctx);
739 pdfi_log_info(ctx, "pdfi_setfillcolor", "colour operator in a CharProc, following a d1 ignored\n");
740 return 0;
741 }
742
743 cc.pattern = 0;
744 ncomps = cs_num_components(pcs);
745 if (ncomps < 1)
746 return_error(gs_error_syntaxerror);
747 code = pdfi_get_color_from_stack(ctx, &cc, ncomps);
748 if (code == 0) {
749 if (pcs->type == &gs_color_space_type_Indexed) {
750 /* Special handling for floating point colour values
751 * PostScript doesn't specify what should happen with a float
752 * lookup value. PDF says 'nearest integer' and round 0.5 up.
753 * Doing this in the graphics library caused some (tiny) differences
754 * in output files, so instead perform that check here.
755 * The out of range clamping is already correct in the graphics library.
756 */
757 int index = (int)floor(cc.paint.values[0]);
758
759 if(cc.paint.values[0] - index > 0.49999)
760 index++;
761 cc.paint.values[0] = (float)index;
762 }
763 code = gs_setcolor(ctx->pgs, &cc);
764 }
765 return code;
766 }
767
768 static inline bool
769 pattern_instance_uses_base_space(const gs_pattern_instance_t * pinst)
770 {
771 return pinst->type->procs.uses_base_space(
772 pinst->type->procs.get_pattern(pinst) );
773 }
774
775 /* Now the SCN and scn operators. These set the colour for special spaces;
776 * ICCBased, Pattern, Separation and DeviceN
777 */
778 int
779 pdfi_setcolorN(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict, bool is_fill)
780 {
781 gs_color_space *pcs;
782 gs_color_space *base_space = NULL;
783 int ncomps=0, code = 0;
784 gs_client_color cc;
785 bool is_pattern = false;
786
787 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
788 /* There will have been a preceding operator to set the colour space, which we
789 * will have ignored, so now we don't know how many components to expect!
790 * Just clear the stack and hope for the best.
791 */
792 pdfi_clearstack(ctx);
793 pdfi_log_info(ctx, "pdfi_setcolorN", "colour operator in a CharProc, following a d1 ignored\n");
794 return 0;
795 }
796
797 if (!is_fill) {
798 gs_swapcolors_quick(ctx->pgs);
799 }
800 pcs = gs_currentcolorspace(ctx->pgs);
801
802 if (pdfi_count_stack(ctx) < 1) {
803 code = gs_note_error(gs_error_stackunderflow);
804 goto cleanupExit1;
805 }
806
807 memset(&cc, 0x00, sizeof(gs_client_color));
808
809 if (pcs->type == &gs_color_space_type_Pattern)
810 is_pattern = true;
811 if (is_pattern) {
812 pdf_name *n = NULL;
813
814 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_NAME) {
815 pdfi_clearstack(ctx);
816 code = gs_note_error(gs_error_typecheck);
817 goto cleanupExit0;
818 }
819 n = (pdf_name *)ctx->stack_top[-1];
820 pdfi_countup(n);
821 pdfi_pop(ctx, 1);
822
823 base_space = pcs->base_space;
824 code = pdfi_pattern_set(ctx, stream_dict, page_dict, n, &cc);
825 pdfi_countdown(n);
826 if (code < 0) {
827 /* Ignore the pattern if we failed to set it */
828 code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BADPATTERN, "pdfi_setcolorN", (char *)"PATTERN: Error setting pattern");
829 goto cleanupExit1;
830 }
831 if (base_space && pattern_instance_uses_base_space(cc.pattern))
832 ncomps = cs_num_components(base_space);
833 else
834 ncomps = 0;
835 } else
836 ncomps = cs_num_components(pcs);
837
838 if (ncomps > 0) {
839 code = pdfi_get_color_from_stack(ctx, &cc, ncomps);
840 if (code < 0)
841 goto cleanupExit1;
842 }
843
844 if (pcs->type == &gs_color_space_type_Indexed) {
845 if (ncomps <= 0)
846 {
847 code = gs_note_error(gs_error_rangecheck);
848 goto cleanupExit1;
849 }
850 if (cc.paint.values[0] < 0)
851 cc.paint.values[0] = 0.0;
852 else
853 {
854 if (cc.paint.values[0] > pcs->params.indexed.hival)
855 cc.paint.values[0] = (float)pcs->params.indexed.hival;
856 else
857 {
858 if (cc.paint.values[0] != floor(cc.paint.values[0]))
859 {
860 if (cc.paint.values[0] - floor(cc.paint.values[0]) < 0.5)
861 cc.paint.values[0] = floor(cc.paint.values[0]);
862 else
863 cc.paint.values[0] = ceil(cc.paint.values[0]);
864 }
865 }
866 }
867 }
868
869 code = gs_setcolor(ctx->pgs, &cc);
870
871 cleanupExit1:
872 if (is_pattern)
873 /* cc is a local scope variable, holding a reference to a pattern.
874 * We need to count the refrence down before the variable goes out of scope
875 * in order to prevent the pattern leaking.
876 */
877 rc_decrement(cc.pattern, "pdfi_setcolorN");
878
879 cleanupExit0:
880 if (!is_fill)
881 gs_swapcolors_quick(ctx->pgs);
882 return code;
883 }
884
885 /* And now, the routines to set the colour space on its own. */
886
887 /* Starting with the ICCBased colour space */
888
889 /* This routine is mostly a copy of seticc() in zicc.c */
890 static int pdfi_create_icc(pdf_context *ctx, char *Name, stream *s, int ncomps, int *icc_N, float *range_buff, ulong dictkey, gs_color_space **ppcs)
891 {
892 int code, k;
893 gs_color_space * pcs;
894 cmm_profile_t *picc_profile = NULL;
895 int i, expected = 0;
896
897 static const char *const icc_std_profile_names[] = {
898 GSICC_STANDARD_PROFILES
899 };
900 static const char *const icc_std_profile_keys[] = {
901 GSICC_STANDARD_PROFILES_KEYS
902 };
903
904 if (ppcs!= NULL)
905 *ppcs = NULL;
906
907 /* We need to use the graphics state memory, and beyond that we need to use stable memory, because the
908 * ICC cache can persist until the end of job, and so the profile (which is stored in teh cache)
909 * must likewise persist.
910 */
911 code = gs_cspace_build_ICC(&pcs, NULL, (gs_gstate_memory(ctx->pgs))->stable_memory);
912 if (code < 0)
913 return code;
914
915 if (Name != NULL){
916 /* Compare this to the standard profile names */
917 for (k = 0; k < GSICC_NUMBER_STANDARD_PROFILES; k++) {
918 if ( strcmp( Name, icc_std_profile_keys[k] ) == 0 ) {
919 picc_profile = gsicc_get_profile_handle_file(icc_std_profile_names[k],
920 strlen(icc_std_profile_names[k]), gs_gstate_memory(ctx->pgs));
921 break;
922 }
923 }
924 } else {
925 if (s == NULL)
926 return_error(gs_error_undefined);
927
928 picc_profile = gsicc_profile_new(s, gs_gstate_memory(ctx->pgs), NULL, 0);
929 if (picc_profile == NULL) {
930 rc_decrement(pcs,"pdfi_create_icc");
931 return gs_throw(gs_error_VMerror, "pdfi_create_icc Creation of ICC profile failed");
932 }
933 /* We have to get the profile handle due to the fact that we need to know
934 if it has a data space that is CIELAB */
935 picc_profile->profile_handle =
936 gsicc_get_profile_handle_buffer(picc_profile->buffer,
937 picc_profile->buffer_size,
938 gs_gstate_memory(ctx->pgs));
939 }
940
941 if (picc_profile == NULL || picc_profile->profile_handle == NULL) {
942 /* Free up everything, the profile is not valid. We will end up going
943 ahead and using a default based upon the number of components */
944 rc_decrement(picc_profile,"pdfi_create_icc");
945 rc_decrement(pcs,"pdfi_create_icc");
946 return -1;
947 }
948 code = gsicc_set_gscs_profile(pcs, picc_profile, gs_gstate_memory(ctx->pgs));
949 if (code < 0) {
950 rc_decrement(picc_profile,"pdfi_create_icc");
951 rc_decrement(pcs,"pdfi_create_icc");
952 return code;
953 }
954
955 picc_profile->data_cs =
956 gscms_get_profile_data_space(picc_profile->profile_handle,
957 picc_profile->memory);
958 switch (picc_profile->data_cs) {
959 case gsCIEXYZ:
960 case gsCIELAB:
961 case gsRGB:
962 expected = 3;
963 break;
964 case gsGRAY:
965 expected = 1;
966 break;
967 case gsCMYK:
968 expected = 4;
969 break;
970 case gsNCHANNEL:
971 case gsNAMED: /* Silence warnings */
972 case gsUNDEFINED: /* Silence warnings */
973 break;
974 }
975 if (expected == 0) {
976 rc_decrement(picc_profile,"pdfi_create_icc");
977 rc_decrement(pcs,"pdfi_create_icc");
978 return_error(gs_error_rangecheck);
979 }
980 /* Return the number of components the ICC profile has */
981 *icc_N = expected;
982 if (expected != ncomps) {
983 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_ICC_BAD_N, "pdfi_create_icc", "")) < 0)
984 goto exit;
985 ncomps = expected;
986 }
987
988 picc_profile->num_comps = ncomps;
989 /* Lets go ahead and get the hash code and check if we match one of the default spaces */
990 /* Later we may want to delay this, but for now lets go ahead and do it */
991 gsicc_init_hash_cs(picc_profile, ctx->pgs);
992
993 /* Set the range according to the data type that is associated with the
994 ICC input color type. Occasionally, we will run into CIELAB to CIELAB
995 profiles for spot colors in PDF documents. These spot colors are typically described
996 as separation colors with tint transforms that go from a tint value
997 to a linear mapping between the CIELAB white point and the CIELAB tint
998 color. This results in a CIELAB value that we need to use to fill. We
999 need to detect this to make sure we do the proper scaling of the data. For
1000 CIELAB images in PDF, the source is always normal 8 or 16 bit encoded data
1001 in the range from 0 to 255 or 0 to 65535. In that case, there should not
1002 be any encoding and decoding to CIELAB. The PDF content will not include
1003 an ICC profile but will set the color space to \Lab. In this case, we use
1004 our seticc_lab operation to install the LAB to LAB profile, but we detect
1005 that we did that through the use of the is_lab flag in the profile descriptor.
1006 When then avoid the CIELAB encode and decode */
1007 if (picc_profile->data_cs == gsCIELAB) {
1008 /* If the input space to this profile is CIELAB, then we need to adjust the limits */
1009 /* See ICC spec ICC.1:2004-10 Section 6.3.4.2 and 6.4. I don't believe we need to
1010 worry about CIEXYZ profiles or any of the other odds ones. Need to check that though
1011 at some point. */
1012 picc_profile->Range.ranges[0].rmin = 0.0;
1013 picc_profile->Range.ranges[0].rmax = 100.0;
1014 picc_profile->Range.ranges[1].rmin = -128.0;
1015 picc_profile->Range.ranges[1].rmax = 127.0;
1016 picc_profile->Range.ranges[2].rmin = -128.0;
1017 picc_profile->Range.ranges[2].rmax = 127.0;
1018 picc_profile->islab = true;
1019 } else {
1020 for (i = 0; i < ncomps; i++) {
1021 picc_profile->Range.ranges[i].rmin = range_buff[2 * i];
1022 picc_profile->Range.ranges[i].rmax = range_buff[2 * i + 1];
1023 }
1024 }
1025 /* Now see if we are in an overide situation. We have to wait until now
1026 in case this is an LAB profile which we will not overide */
1027 if (gs_currentoverrideicc(ctx->pgs) && picc_profile->data_cs != gsCIELAB) {
1028 /* Free up the profile structure */
1029 switch( picc_profile->data_cs ) {
1030 case gsRGB:
1031 pcs->cmm_icc_profile_data = ctx->pgs->icc_manager->default_rgb;
1032 break;
1033 case gsGRAY:
1034 pcs->cmm_icc_profile_data = ctx->pgs->icc_manager->default_gray;
1035 break;
1036 case gsCMYK:
1037 pcs->cmm_icc_profile_data = ctx->pgs->icc_manager->default_cmyk;
1038 break;
1039 default:
1040 break;
1041 }
1042 /* Have one increment from the color space. Having these tied
1043 together is not really correct. Need to fix that. ToDo. MJV */
1044 rc_adjust(picc_profile, -2, "pdfi_create_icc");
1045 rc_increment(pcs->cmm_icc_profile_data);
1046 }
1047 /* Add the color space to the profile cache */
1048 if (dictkey != 0) {
1049 code = gsicc_add_cs(ctx->pgs, pcs, dictkey);
1050 if (code < 0)
1051 goto exit;
1052 }
1053
1054 if (ppcs!= NULL){
1055 *ppcs = pcs;
1056 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
1057 } else {
1058 code = pdfi_gs_setcolorspace(ctx, pcs);
1059 rc_decrement_only_cs(pcs, "pdfi_create_icc");
1060 }
1061
1062 exit:
1063 /* The context has taken a reference to the colorspace. We no longer need
1064 * ours, so drop it. */
1065 rc_decrement(picc_profile, "pdfi_create_icc");
1066 return code;
1067 }
1068
1069 static int pdfi_create_iccprofile(pdf_context *ctx, pdf_stream *ICC_obj, char *cname,
1070 int64_t Length, int N, int *icc_N, float *range, gs_color_space **ppcs)
1071 {
1072 pdf_c_stream *profile_stream = NULL;
1073 byte *profile_buffer;
1074 int code, code1;
1075 ulong dictkey = 0;
1076
1077 /* See if the color space is in the profile cache */
1078 /* NOTE! 0 indicates a named colour space for JPX images, do not attempt to
1079 * find a cached space for this. Conveniently should we somehow manage to get
1080 * here from an array or other object which is not an indirect reference then we will
1081 * again not attempt to cache the space or lookup the cache.
1082 */
1083 if (!gs_currentoverrideicc(ctx->pgs)) {
1084 if (ICC_obj->object_num != 0) {
1085 gs_color_space *pcs = NULL;
1086
1087 pcs = gsicc_find_cs(ICC_obj->object_num, ctx->pgs);
1088 if (pcs != NULL) {
1089 if (ppcs!= NULL){
1090 *ppcs = pcs;
1091 } else {
1092 code = pdfi_gs_setcolorspace(ctx, pcs);
1093 rc_decrement_only_cs(pcs, "pdfi_create_iccprofile");
1094 }
1095 *icc_N = gs_color_space_num_components(pcs);
1096 /* We're passing back a new reference, increment the count */
1097 rc_adjust_only(pcs, 1, "pdfi_create_iccprofile, return cached ICC profile");
1098 return 0;
1099 }
1100 dictkey = ICC_obj->object_num;
1101 }
1102 }
1103
1104 /* The ICC profile reading code (irritatingly) requires a seekable stream, because it
1105 * rewinds it to the start, then seeks to the end to find the size, then rewinds the
1106 * stream again.
1107 * Ideally we would use a ReusableStreamDecode filter here, but that is largely
1108 * implemented in PostScript (!) so we can't use it. What we can do is create a
1109 * string sourced stream in memory, which is at least seekable.
1110 */
1111 code = pdfi_open_memory_stream_from_filtered_stream(ctx, ICC_obj, &profile_buffer, &profile_stream, true);
1112 if (code < 0) {
1113 return code;
1114 }
1115
1116 /* Now, finally, we can call the code to create and set the profile */
1117 code = pdfi_create_icc(ctx, cname, profile_stream->s, (int)N, icc_N, range, dictkey, ppcs);
1118
1119 code1 = pdfi_close_memory_stream(ctx, profile_buffer, profile_stream);
1120
1121 if (code == 0)
1122 code = code1;
1123
1124 return code;
1125 }
1126
1127 static int pdfi_set_CalGray_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict)
1128 {
1129 int code = 0, i;
1130 double f;
1131 /* The default values here are as per the PDF 1.7 specification, there is
1132 * no default for the WhitePoint as it is a required entry.
1133 */
1134 float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma = 1.0f;
1135 pdf_array *PDFArray = NULL;
1136
1137 code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray);
1138 if (code < 0) {
1139 pdfi_countdown(PDFArray);
1140 goto exit;
1141 }
1142 if (pdfi_array_size(PDFArray) != 3){
1143 code = gs_note_error(gs_error_rangecheck);
1144 goto exit;
1145 }
1146
1147 for (i=0; i < 3; i++) {
1148 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1149 if (code < 0)
1150 goto exit;
1151 WhitePoint[i] = (float)f;
1152 }
1153 pdfi_countdown(PDFArray);
1154 PDFArray = NULL;
1155
1156 /* Check the WhitePoint values, the PDF 1.7 reference states that
1157 * Xw ad Zw must be positive and Yw must be 1.0
1158 */
1159 if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) {
1160 code = gs_note_error(gs_error_rangecheck);
1161 goto exit;
1162 }
1163
1164 if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1165 if (pdfi_array_size(PDFArray) != 3){
1166 code = gs_note_error(gs_error_rangecheck);
1167 goto exit;
1168 }
1169 for (i=0; i < 3; i++) {
1170 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1171 if (code < 0)
1172 goto exit;
1173 /* The PDF 1.7 reference states that all three components of the BlackPoint
1174 * (if present) must be positive.
1175 */
1176 if (f < 0) {
1177 code = gs_note_error(gs_error_rangecheck);
1178 goto exit;
1179 }
1180 BlackPoint[i] = (float)f;
1181 }
1182 pdfi_countdown(PDFArray);
1183 PDFArray = NULL;
1184 }
1185
1186 if (pdfi_dict_knownget_number(ctx, ParamsDict, "Gamma", &f) > 0)
1187 Gamma = (float)f;
1188 /* The PDF 1.7 reference states that Gamma
1189 * (if present) must be positive.
1190 */
1191 if (Gamma < 0) {
1192 code = gs_note_error(gs_error_rangecheck);
1193 goto exit;
1194 }
1195 code = 0;
1196
1197 for (i = 0;i < 3; i++) {
1198 pcs->params.calgray.WhitePoint[i] = WhitePoint[i];
1199 pcs->params.calgray.BlackPoint[i] = BlackPoint[i];
1200 }
1201 pcs->params.calgray.Gamma = Gamma;
1202
1203 exit:
1204 pdfi_countdown(PDFArray);
1205 return code;
1206 }
1207
1208 static int pdfi_set_CalRGB_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict)
1209 {
1210 int code = 0, i;
1211 pdf_array *PDFArray = NULL;
1212 /* The default values here are as per the PDF 1.7 specification, there is
1213 * no default for the WhitePoint as it is a required entry
1214 */
1215 float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma[3] = {1.0f, 1.0f, 1.0f};
1216 float Matrix[9] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
1217 double f;
1218
1219 code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray);
1220 if (code < 0) {
1221 pdfi_countdown(PDFArray);
1222 goto exit;
1223 }
1224 if (pdfi_array_size(PDFArray) != 3){
1225 code = gs_note_error(gs_error_rangecheck);
1226 goto exit;
1227 }
1228
1229 for (i=0; i < 3; i++) {
1230 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1231 if (code < 0)
1232 goto exit;
1233 WhitePoint[i] = (float)f;
1234 }
1235 pdfi_countdown(PDFArray);
1236 PDFArray = NULL;
1237
1238 /* Check the WhitePoint values, the PDF 1.7 reference states that
1239 * Xw ad Zw must be positive and Yw must be 1.0
1240 */
1241 if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) {
1242 code = gs_note_error(gs_error_rangecheck);
1243 goto exit;
1244 }
1245
1246 if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1247 if (pdfi_array_size(PDFArray) != 3){
1248 code = gs_note_error(gs_error_rangecheck);
1249 goto exit;
1250 }
1251 for (i=0; i < 3; i++) {
1252 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1253 if (code < 0)
1254 goto exit;
1255 /* The PDF 1.7 reference states that all three components of the BlackPoint
1256 * (if present) must be positive.
1257 */
1258 if (f < 0) {
1259 code = gs_note_error(gs_error_rangecheck);
1260 goto exit;
1261 }
1262 BlackPoint[i] = (float)f;
1263 }
1264 pdfi_countdown(PDFArray);
1265 PDFArray = NULL;
1266 }
1267
1268 if (pdfi_dict_knownget_type(ctx, ParamsDict, "Gamma", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1269 if (pdfi_array_size(PDFArray) != 3){
1270 code = gs_note_error(gs_error_rangecheck);
1271 goto exit;
1272 }
1273 for (i=0; i < 3; i++) {
1274 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1275 if (code < 0)
1276 goto exit;
1277 Gamma[i] = (float)f;
1278 }
1279 pdfi_countdown(PDFArray);
1280 PDFArray = NULL;
1281 }
1282
1283 if (pdfi_dict_knownget_type(ctx, ParamsDict, "Matrix", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1284 if (pdfi_array_size(PDFArray) != 9){
1285 code = gs_note_error(gs_error_rangecheck);
1286 goto exit;
1287 }
1288 for (i=0; i < 9; i++) {
1289 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1290 if (code < 0)
1291 goto exit;
1292 Matrix[i] = (float)f;
1293 }
1294 pdfi_countdown(PDFArray);
1295 PDFArray = NULL;
1296 }
1297 code = 0;
1298
1299 for (i = 0;i < 3; i++) {
1300 pcs->params.calrgb.WhitePoint[i] = WhitePoint[i];
1301 pcs->params.calrgb.BlackPoint[i] = BlackPoint[i];
1302 pcs->params.calrgb.Gamma[i] = Gamma[i];
1303 }
1304 for (i = 0;i < 9; i++)
1305 pcs->params.calrgb.Matrix[i] = Matrix[i];
1306
1307 exit:
1308 pdfi_countdown(PDFArray);
1309 return code;
1310 }
1311
1312 static int pdfi_set_Lab_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict)
1313 {
1314 int code = 0, i;
1315 pdf_array *PDFArray = NULL;
1316 /* The default values here are as per the PDF 1.7 specification, there is
1317 * no default for the WhitePoint as it is a required entry
1318 */
1319 float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Range[4] = {-100.0, 100.0, -100.0, 100.0};
1320 double f;
1321
1322 code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray);
1323 if (code < 0) {
1324 pdfi_countdown(PDFArray);
1325 goto exit;
1326 }
1327 if (pdfi_array_size(PDFArray) != 3){
1328 code = gs_note_error(gs_error_rangecheck);
1329 goto exit;
1330 }
1331
1332 for (i=0; i < 3; i++) {
1333 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1334 if (code < 0)
1335 goto exit;
1336 WhitePoint[i] = (float)f;
1337 }
1338 pdfi_countdown(PDFArray);
1339 PDFArray = NULL;
1340
1341 /* Check the WhitePoint values, the PDF 1.7 reference states that
1342 * Xw ad Zw must be positive and Yw must be 1.0
1343 */
1344 if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) {
1345 code = gs_note_error(gs_error_rangecheck);
1346 goto exit;
1347 }
1348
1349 if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1350 if (pdfi_array_size(PDFArray) != 3){
1351 code = gs_note_error(gs_error_rangecheck);
1352 goto exit;
1353 }
1354 for (i=0; i < 3; i++) {
1355 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1356 if (code < 0)
1357 goto exit;
1358 /* The PDF 1.7 reference states that all three components of the BlackPoint
1359 * (if present) must be positive.
1360 */
1361 if (f < 0) {
1362 code = gs_note_error(gs_error_rangecheck);
1363 goto exit;
1364 }
1365 BlackPoint[i] = (float)f;
1366 }
1367 pdfi_countdown(PDFArray);
1368 PDFArray = NULL;
1369 }
1370
1371 if (pdfi_dict_knownget_type(ctx, ParamsDict, "Range", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1372 if (pdfi_array_size(PDFArray) != 4){
1373 code = gs_note_error(gs_error_rangecheck);
1374 goto exit;
1375 }
1376
1377 for (i=0; i < 4; i++) {
1378 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1379 if (code < 0)
1380 goto exit;
1381 Range[i] = f;
1382 }
1383 pdfi_countdown(PDFArray);
1384 PDFArray = NULL;
1385 }
1386 code = 0;
1387
1388 for (i = 0;i < 3; i++) {
1389 pcs->params.lab.WhitePoint[i] = WhitePoint[i];
1390 pcs->params.lab.BlackPoint[i] = BlackPoint[i];
1391 }
1392 for (i = 0;i < 4; i++)
1393 pcs->params.lab.Range[i] = Range[i];
1394
1395 exit:
1396 pdfi_countdown(PDFArray);
1397 return code;
1398 }
1399
1400 static int pdfi_create_iccbased(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image)
1401 {
1402 pdf_stream *ICC_obj = NULL;
1403 pdf_dict *dict; /* Alias to avoid tons of casting */
1404 pdf_array *a;
1405 int64_t Length, N;
1406 pdf_obj *Name = NULL, *Alt = NULL;
1407 char *cname = NULL;
1408 int code;
1409 bool known = true;
1410 float range[8];
1411 int icc_N;
1412 gs_color_space *pcs = NULL;
1413
1414 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_STREAM, (pdf_obj **)&ICC_obj);
1415 if (code < 0)
1416 return code;
1417 code = pdfi_dict_from_obj(ctx, (pdf_obj *)ICC_obj, &dict);
1418 if (code < 0)
1419 return code;
1420
1421 Length = pdfi_stream_length(ctx, ICC_obj);
1422 code = pdfi_dict_get_int(ctx, dict, "N", &N);
1423 if (code < 0)
1424 goto done;
1425 if (N != 1 && N != 3 && N != 4) {
1426 code = gs_note_error(gs_error_rangecheck);
1427 goto done;
1428 }
1429 code = pdfi_dict_knownget(ctx, dict, "Name", &Name);
1430 if (code > 0) {
1431 switch (pdfi_type_of(Name)) {
1432 case PDF_STRING:
1433 case PDF_NAME:
1434 cname = (char *)gs_alloc_bytes(ctx->memory, ((pdf_name *)Name)->length + (size_t)1, "pdfi_create_iccbased (profile name)");
1435 if (cname == NULL) {
1436 code = gs_note_error(gs_error_VMerror);
1437 goto done;
1438 }
1439 memset(cname, 0x00, ((pdf_name *)Name)->length + 1);
1440 memcpy(cname, ((pdf_name *)Name)->data, ((pdf_name *)Name)->length);
1441 break;
1442 default:
1443 break;
1444 }
1445 }
1446 if (code < 0)
1447 goto done;
1448
1449 code = pdfi_dict_knownget_type(ctx, dict, "Range", PDF_ARRAY, (pdf_obj **)&a);
1450 if (code < 0)
1451 goto done;
1452 if (code > 0) {
1453 double dbl;
1454 int i;
1455
1456 if (pdfi_array_size(a) >= N * 2) {
1457 for (i = 0; i < N * 2;i++) {
1458 code = pdfi_array_get_number(ctx, a, i, &dbl);
1459 if (code < 0) {
1460 known = false;
1461 break;
1462 }
1463 range[i] = (float)dbl;
1464 }
1465 } else {
1466 known = false;
1467 }
1468 pdfi_countdown(a);
1469 } else
1470 known = false;
1471
1472 /* We don't just use the final else clause above for setting the defaults
1473 * because we also want to use these if there's a problem with the
1474 * supplied data. In this case we also want to overwrite any partial
1475 * data we might have read
1476 */
1477 if (!known) {
1478 int i;
1479 for (i = 0;i < N; i++) {
1480 range[i * 2] = 0;
1481 range[(i * 2) + 1] = 1;
1482 }
1483 }
1484
1485 code = pdfi_create_iccprofile(ctx, ICC_obj, cname, Length, N, &icc_N, range, &pcs);
1486
1487 /* This is just plain hackery for the benefit of Bug696690.pdf. The old PostScript PDF interpreter says:
1488 * %% This section is to deal with the horrible pair of files in Bug #696690 and Bug #696120
1489 * %% These files have ICCBased spaces where the value of /N and the number of components
1490 * %% in the profile differ. In addition the profile in Bug #696690 is invalid. In the
1491 * %% case of Bug #696690 the /N value is correct, and the profile is wrong, in the case
1492 * %% of Bug #696120 the /N value is incorrect and the profile is correct.
1493 * %% We 'suspect' that Acrobat uses the fact that Bug #696120 is a pure image to detect
1494 * %% that the /N is incorrect, we can't be sure whether it uses the profile or just uses
1495 * %% the /N to decide on a device space.
1496 * We can't precisely duplicate the PostScript approach, but we now set the actual ICC profile
1497 * and therefore use the number of components in the profile. However, we pass back the number
1498 * of components in icc_N. We then check to see if N and icc_N are the same, if they are not we
1499 * try to set a devcie colour using the profile. If that fails (bad profile) then we enter the fallback
1500 * just as if we had failed to set the profile.
1501 */
1502 if (code >= 0 && N != icc_N) {
1503 gs_client_color cc;
1504 int i;
1505
1506 gs_gsave(ctx->pgs);
1507 code = gs_setcolorspace(ctx->pgs, pcs);
1508 if (code == 0) {
1509 cc.pattern = 0;
1510 for (i = 0;i < icc_N; i++)
1511 cc.paint.values[i] = 0;
1512 code = gs_setcolor(ctx->pgs, &cc);
1513 if (code == 0)
1514 code = gx_set_dev_color(ctx->pgs);
1515 }
1516 gs_grestore(ctx->pgs);
1517 }
1518
1519 if (code < 0) {
1520 pdf_obj *Alternate = NULL;
1521
1522 if (pcs != NULL)
1523 rc_decrement(pcs,"pdfi_create_iccbased");
1524
1525 /* Failed to set the ICCBased space, attempt to use the Alternate */
1526 code = pdfi_dict_knownget(ctx, dict, "Alternate", &Alternate);
1527 if (code > 0) {
1528 /* The Alternate should be one of the device spaces, therefore a Name object. If its not, fallback to using /N */
1529 if (pdfi_type_of(Alternate) == PDF_NAME)
1530 code = pdfi_create_colorspace_by_name(ctx, (pdf_name *)Alternate, stream_dict,
1531 page_dict, ppcs, inline_image);
1532 pdfi_countdown(Alternate);
1533 if (code == 0) {
1534 pdfi_set_warning(ctx, 0, NULL, W_PDF_BADICC_USE_ALT, "pdfi_create_iccbased", NULL);
1535 goto done;
1536 }
1537 }
1538 /* Use the number of components *from the profile* to set a space.... */
1539 pdfi_set_warning(ctx, 0, NULL, W_PDF_BADICC_USECOMPS, "pdfi_create_iccbased", NULL);
1540 switch(N) {
1541 case 1:
1542 pcs = gs_cspace_new_DeviceGray(ctx->memory);
1543 if (pcs == NULL)
1544 code = gs_note_error(gs_error_VMerror);
1545 break;
1546 case 3:
1547 pcs = gs_cspace_new_DeviceRGB(ctx->memory);
1548 if (pcs == NULL)
1549 code = gs_note_error(gs_error_VMerror);
1550 break;
1551 case 4:
1552 pcs = gs_cspace_new_DeviceCMYK(ctx->memory);
1553 if (pcs == NULL)
1554 code = gs_note_error(gs_error_VMerror);
1555 break;
1556 default:
1557 code = gs_note_error(gs_error_undefined);
1558 break;
1559 }
1560 } else {
1561 if (pcs->ICC_Alternate_space == gs_ICC_Alternate_None) {
1562 code = pdfi_dict_knownget(ctx, dict, "Alternate", (pdf_obj **)&Alt);
1563 if (code >= 0) {
1564 switch(pdfi_type_of(Alt)) {
1565 case PDF_NAME:
1566 /* Simple named spaces must be Gray, RGB or CMYK, we ignore /Indexed */
1567 if (pdfi_name_is((const pdf_name *)Alt, "DeviceGray"))
1568 pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceGray;
1569 else if (pdfi_name_is((const pdf_name *)Alt, "DeviceRGB"))
1570 pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceRGB;
1571 else if (pdfi_name_is((const pdf_name *)Alt, "DeviceCMYK"))
1572 pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceCMYK;
1573 break;
1574 case PDF_ARRAY:
1575 {
1576 pdf_obj *AltName = NULL, *ParamsDict = NULL;
1577
1578 code = pdfi_array_get_type(ctx, (pdf_array *)Alt, 0, PDF_NAME, &AltName);
1579 if (code >= 0) {
1580 code = pdfi_array_get_type(ctx, (pdf_array *)Alt, 1, PDF_DICT, &ParamsDict);
1581 if (code >= 0) {
1582 if (pdfi_name_is((const pdf_name *)AltName, "CalGray")) {
1583 code = pdfi_set_CalGray_params(ctx, pcs, (pdf_dict *)ParamsDict);
1584 if (code >= 0)
1585 pcs->ICC_Alternate_space = gs_ICC_Alternate_CalGray;
1586 } else {
1587 if (pdfi_name_is((const pdf_name *)AltName, "CalRGB")) {
1588 code = pdfi_set_CalRGB_params(ctx, pcs, (pdf_dict *)ParamsDict);
1589 if (code >= 0)
1590 pcs->ICC_Alternate_space = gs_ICC_Alternate_CalRGB;
1591 } else {
1592 if (pdfi_name_is((const pdf_name *)AltName, "CalCMYK")) {
1593 pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceCMYK;
1594 } else {
1595 if (pdfi_name_is((const pdf_name *)AltName, "Lab")) {
1596 code = pdfi_set_Lab_params(ctx, pcs, (pdf_dict *)ParamsDict);
1597 if (code >= 0)
1598 pcs->ICC_Alternate_space = gs_ICC_Alternate_Lab;
1599 }
1600 }
1601 }
1602 }
1603 }
1604 }
1605 code = 0;
1606 pdfi_countdown(ParamsDict);
1607 pdfi_countdown(AltName);
1608 }
1609 break;
1610 default:
1611 /* Probably an error, but just ignore it */
1612 break;
1613 }
1614 pdfi_countdown(Alt);
1615 Alt = NULL;
1616 }
1617 }
1618 }
1619
1620 if (ppcs!= NULL) {
1621 *ppcs = pcs;
1622 if (pcs != NULL)
1623 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
1624 }
1625 else {
1626 if (pcs != NULL) {
1627 code = pdfi_gs_setcolorspace(ctx, pcs);
1628 /* release reference from construction */
1629 rc_decrement_only_cs(pcs, "setseparationspace");
1630 }
1631 }
1632
1633
1634 done:
1635 if (cname)
1636 gs_free_object(ctx->memory, cname, "pdfi_create_iccbased (profile name)");
1637 pdfi_countdown(Alt);
1638 pdfi_countdown(Name);
1639 pdfi_countdown(ICC_obj);
1640 return code;
1641 }
1642
1643 /*
1644 * This, and pdfi_set_cal() below are copied from the similarly named routines
1645 * in zicc.c
1646 */
1647 /* Install a ICC type color space and use the ICC LABLUT profile. */
1648 static int
1649 pdfi_seticc_lab(pdf_context *ctx, float *range_buff, gs_color_space **ppcs)
1650 {
1651 int code;
1652 gs_color_space * pcs;
1653 int i;
1654
1655 /* build the color space object */
1656 /* We need to use the graphics state memory, and beyond that we need to use stable memory, because the
1657 * ICC cache can persist until the end of job, and so the profile (which is stored in teh cache)
1658 * must likewise persist.
1659 */
1660 code = gs_cspace_build_ICC(&pcs, NULL, (gs_gstate_memory(ctx->pgs))->stable_memory);
1661 if (code < 0)
1662 return code;
1663
1664 /* record the current space as the alternative color space */
1665 /* Get the lab profile. It may already be set in the icc manager.
1666 If not then lets populate it. */
1667 if (ctx->pgs->icc_manager->lab_profile == NULL ) {
1668 /* This can't happen as the profile
1669 should be initialized during the
1670 setting of the user params */
1671 return_error(gs_error_unknownerror);
1672 }
1673 /* Assign the LAB to LAB profile to this color space */
1674 code = gsicc_set_gscs_profile(pcs, ctx->pgs->icc_manager->lab_profile, gs_gstate_memory(ctx->pgs));
1675 if (code < 0)
1676 return code;
1677
1678 pcs->cmm_icc_profile_data->Range.ranges[0].rmin = 0.0;
1679 pcs->cmm_icc_profile_data->Range.ranges[0].rmax = 100.0;
1680 for (i = 1; i < 3; i++) {
1681 pcs->cmm_icc_profile_data->Range.ranges[i].rmin =
1682 range_buff[2 * (i-1)];
1683 pcs->cmm_icc_profile_data->Range.ranges[i].rmax =
1684 range_buff[2 * (i-1) + 1];
1685 }
1686 if (ppcs!= NULL){
1687 *ppcs = pcs;
1688 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
1689 } else {
1690 code = pdfi_gs_setcolorspace(ctx, pcs);
1691 rc_decrement_only_cs(pcs, "pdfi_seticc_lab");
1692 }
1693
1694 return code;
1695 }
1696
1697 static int pdfi_create_Lab(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs)
1698 {
1699 int code = 0, i;
1700 pdf_dict *Lab_dict = NULL;
1701 pdf_array *Range = NULL;
1702 float RangeBuf[4];
1703 double f;
1704 bool known = false;
1705
1706 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_DICT, (pdf_obj **)&Lab_dict);
1707 if (code < 0)
1708 return code;
1709
1710 code = pdfi_dict_known(ctx, Lab_dict, "Range", &known);
1711 if (code < 0)
1712 goto exit;
1713 if (known) {
1714 code = pdfi_dict_get_type(ctx, Lab_dict, "Range", PDF_ARRAY, (pdf_obj **)&Range);
1715 if (code < 0) {
1716 goto exit;
1717 }
1718 if (pdfi_array_size(Range) != 4){
1719 code = gs_note_error(gs_error_rangecheck);
1720 goto exit;
1721 }
1722
1723 for (i=0; i < 4; i++) {
1724 code = pdfi_array_get_number(ctx, Range, (uint64_t)i, &f);
1725 if (code < 0)
1726 goto exit;
1727 RangeBuf[i] = (float)f;
1728 }
1729 } else {
1730 RangeBuf[0] = RangeBuf[2] = -100.0;
1731 RangeBuf[1] = RangeBuf[3] = 100.0;
1732 }
1733
1734 code = pdfi_seticc_lab(ctx, RangeBuf, ppcs);
1735
1736 exit:
1737 pdfi_countdown(Lab_dict);
1738 pdfi_countdown(Range);
1739 return code;
1740 }
1741
1742 /* Install an ICC space from the PDF CalRGB or CalGray types */
1743 static int
1744 pdfi_seticc_cal(pdf_context *ctx, float *white, float *black, float *gamma,
1745 float *matrix, int num_colorants, ulong dictkey, gs_color_space **ppcs)
1746 {
1747 int code = 0;
1748 gs_color_space * pcs;
1749 int i;
1750 cmm_profile_t *cal_profile;
1751
1752 /* See if the color space is in the profile cache */
1753 pcs = gsicc_find_cs(dictkey, ctx->pgs);
1754 if (pcs == NULL ) {
1755 /* build the color space object. Since this is cached
1756 in the profile cache which is a member variable
1757 of the graphic state, we will want to use stable
1758 memory here */
1759 code = gs_cspace_build_ICC(&pcs, NULL, ctx->pgs->memory->stable_memory);
1760 if (code < 0)
1761 return code;
1762 /* There is no alternate for this. Perhaps we should set DeviceRGB? */
1763 pcs->base_space = NULL;
1764 /* Create the ICC profile from the CalRGB or CalGray parameters */
1765 /* We need to use the graphics state memory, in case we are running under Ghostscript. */
1766 cal_profile = gsicc_create_from_cal(white, black, gamma, matrix,
1767 ctx->pgs->memory->stable_memory, num_colorants);
1768 if (cal_profile == NULL) {
1769 rc_decrement(pcs, "seticc_cal");
1770 return_error(gs_error_VMerror);
1771 }
1772 /* Assign the profile to this color space */
1773 /* Apparently the memory pointer passed here here is not actually used, but we will supply
1774 * the graphics state memory allocator, because that's what the colour space should be using.
1775 */
1776 code = gsicc_set_gscs_profile(pcs, cal_profile, ctx->pgs->memory);
1777 /* profile is created with ref count of 1, gsicc_set_gscs_profile()
1778 * increments the ref count, so we need to decrement it here.
1779 */
1780 rc_decrement(cal_profile, "seticc_cal");
1781 if (code < 0) {
1782 rc_decrement(pcs, "seticc_cal");
1783 return code;
1784 }
1785 for (i = 0; i < num_colorants; i++) {
1786 pcs->cmm_icc_profile_data->Range.ranges[i].rmin = 0;
1787 pcs->cmm_icc_profile_data->Range.ranges[i].rmax = 1;
1788 }
1789 /* Add the color space to the profile cache */
1790 gsicc_add_cs(ctx->pgs, pcs, dictkey);
1791 } else {
1792 /* We're passing back a new reference, increment the count */
1793 rc_adjust_only(pcs, 1, "pdfi_seticc_cal, return cached ICC profile");
1794 }
1795
1796 if (ppcs!= NULL){
1797 *ppcs = pcs;
1798 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
1799 } else {
1800 code = pdfi_gs_setcolorspace(ctx, pcs);
1801 rc_decrement_only_cs(pcs, "pdfi_seticc_cal");
1802 }
1803
1804 return code;
1805 }
1806
1807 static int pdfi_create_CalGray(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs)
1808 {
1809 int code = 0, i;
1810 pdf_dict *CalGray_dict = NULL;
1811 pdf_array *PDFArray = NULL;
1812 /* The default values here are as per the PDF 1.7 specification, there is
1813 * no default for the WhitePoint as it is a required entry. The Matrix is
1814 * not specified for CalGray, but we need it for the general 'pdfi_set_icc'
1815 * routine, so we use the same default as CalRGB.
1816 */
1817 float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma = 1.0f;
1818 float Matrix[9] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
1819 double f;
1820
1821 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_DICT, (pdf_obj **)&CalGray_dict);
1822 if (code < 0)
1823 return code;
1824
1825 code = pdfi_dict_get_type(ctx, CalGray_dict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray);
1826 if (code < 0) {
1827 pdfi_countdown(PDFArray);
1828 goto exit;
1829 }
1830 if (pdfi_array_size(PDFArray) != 3){
1831 code = gs_note_error(gs_error_rangecheck);
1832 goto exit;
1833 }
1834
1835 for (i=0; i < 3; i++) {
1836 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1837 if (code < 0)
1838 goto exit;
1839 WhitePoint[i] = (float)f;
1840 }
1841 pdfi_countdown(PDFArray);
1842 PDFArray = NULL;
1843
1844 /* Check the WhitePoint values, the PDF 1.7 reference states that
1845 * Xw ad Zw must be positive and Yw must be 1.0
1846 */
1847 if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) {
1848 code = gs_note_error(gs_error_rangecheck);
1849 goto exit;
1850 }
1851
1852 if (pdfi_dict_knownget_type(ctx, CalGray_dict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1853 if (pdfi_array_size(PDFArray) != 3){
1854 code = gs_note_error(gs_error_rangecheck);
1855 goto exit;
1856 }
1857 for (i=0; i < 3; i++) {
1858 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1859 if (code < 0)
1860 goto exit;
1861 /* The PDF 1.7 reference states that all three components of the BlackPoint
1862 * (if present) must be positive.
1863 */
1864 if (f < 0) {
1865 code = gs_note_error(gs_error_rangecheck);
1866 goto exit;
1867 }
1868 BlackPoint[i] = (float)f;
1869 }
1870 pdfi_countdown(PDFArray);
1871 PDFArray = NULL;
1872 }
1873
1874 if (pdfi_dict_knownget_number(ctx, CalGray_dict, "Gamma", &f) > 0)
1875 Gamma = (float)f;
1876 /* The PDF 1.7 reference states that Gamma
1877 * (if present) must be positive.
1878 */
1879 if (Gamma < 0) {
1880 code = gs_note_error(gs_error_rangecheck);
1881 goto exit;
1882 }
1883
1884 code = pdfi_seticc_cal(ctx, WhitePoint, BlackPoint, &Gamma, Matrix, 1, color_array->object_num, ppcs);
1885
1886 exit:
1887 pdfi_countdown(PDFArray);
1888 pdfi_countdown(CalGray_dict);
1889 return code;
1890 }
1891
1892 static int pdfi_create_CalRGB(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs)
1893 {
1894 int code = 0, i;
1895 pdf_dict *CalRGB_dict = NULL;
1896 pdf_array *PDFArray = NULL;
1897 /* The default values here are as per the PDF 1.7 specification, there is
1898 * no default for the WhitePoint as it is a required entry
1899 */
1900 float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma[3] = {1.0f, 1.0f, 1.0f};
1901 float Matrix[9] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
1902 double f;
1903
1904 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_DICT, (pdf_obj **)&CalRGB_dict);
1905 if (code < 0)
1906 return code;
1907
1908 code = pdfi_dict_get_type(ctx, CalRGB_dict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray);
1909 if (code < 0) {
1910 pdfi_countdown(PDFArray);
1911 goto exit;
1912 }
1913 if (pdfi_array_size(PDFArray) != 3){
1914 code = gs_note_error(gs_error_rangecheck);
1915 goto exit;
1916 }
1917
1918 for (i=0; i < 3; i++) {
1919 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1920 if (code < 0)
1921 goto exit;
1922 WhitePoint[i] = (float)f;
1923 }
1924 pdfi_countdown(PDFArray);
1925 PDFArray = NULL;
1926
1927 /* Check the WhitePoint values, the PDF 1.7 reference states that
1928 * Xw ad Zw must be positive and Yw must be 1.0
1929 */
1930 if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) {
1931 code = gs_note_error(gs_error_rangecheck);
1932 goto exit;
1933 }
1934
1935 if (pdfi_dict_knownget_type(ctx, CalRGB_dict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1936 if (pdfi_array_size(PDFArray) != 3){
1937 code = gs_note_error(gs_error_rangecheck);
1938 goto exit;
1939 }
1940 for (i=0; i < 3; i++) {
1941 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1942 if (code < 0)
1943 goto exit;
1944 /* The PDF 1.7 reference states that all three components of the BlackPoint
1945 * (if present) must be positive.
1946 */
1947 if (f < 0) {
1948 code = gs_note_error(gs_error_rangecheck);
1949 goto exit;
1950 }
1951 BlackPoint[i] = (float)f;
1952 }
1953 pdfi_countdown(PDFArray);
1954 PDFArray = NULL;
1955 }
1956
1957 if (pdfi_dict_knownget_type(ctx, CalRGB_dict, "Gamma", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1958 if (pdfi_array_size(PDFArray) != 3){
1959 code = gs_note_error(gs_error_rangecheck);
1960 goto exit;
1961 }
1962 for (i=0; i < 3; i++) {
1963 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1964 if (code < 0)
1965 goto exit;
1966 Gamma[i] = (float)f;
1967 }
1968 pdfi_countdown(PDFArray);
1969 PDFArray = NULL;
1970 }
1971
1972 if (pdfi_dict_knownget_type(ctx, CalRGB_dict, "Matrix", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) {
1973 if (pdfi_array_size(PDFArray) != 9){
1974 code = gs_note_error(gs_error_rangecheck);
1975 goto exit;
1976 }
1977 for (i=0; i < 9; i++) {
1978 code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f);
1979 if (code < 0)
1980 goto exit;
1981 Matrix[i] = (float)f;
1982 }
1983 pdfi_countdown(PDFArray);
1984 PDFArray = NULL;
1985 }
1986 code = pdfi_seticc_cal(ctx, WhitePoint, BlackPoint, Gamma, Matrix, 3, color_array->object_num, ppcs);
1987
1988 exit:
1989 pdfi_countdown(PDFArray);
1990 pdfi_countdown(CalRGB_dict);
1991 return code;
1992 }
1993
1994 static int pdfi_create_Separation(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image)
1995 {
1996 pdf_obj *o = NULL;
1997 pdf_name *name = NULL, *NamedAlternate = NULL;
1998 pdf_array *ArrayAlternate = NULL;
1999 pdf_obj *transform = NULL;
2000 int code;
2001 gs_color_space *pcs = NULL, *pcs_alt = NULL;
2002 gs_function_t * pfn = NULL;
2003 separation_type sep_type;
2004
2005 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_NAME, (pdf_obj **)&name);
2006 if (code < 0)
2007 goto pdfi_separation_error;
2008
2009 sep_type = SEP_OTHER;
2010 if (name->length == 4 && memcmp(name->data, "None", 4) == 0)
2011 sep_type = SEP_NONE;
2012 if (name->length == 3 && memcmp(name->data, "All", 3) == 0)
2013 sep_type = SEP_ALL;
2014
2015 code = pdfi_array_get(ctx, color_array, index + 2, &o);
2016 if (code < 0)
2017 goto pdfi_separation_error;
2018
2019 switch (pdfi_type_of(o)) {
2020 case PDF_NAME:
2021 NamedAlternate = (pdf_name *)o;
2022 code = pdfi_create_colorspace_by_name(ctx, NamedAlternate, stream_dict, page_dict, &pcs_alt, inline_image);
2023 if (code < 0)
2024 goto pdfi_separation_error;
2025 break;
2026 case PDF_ARRAY:
2027 ArrayAlternate = (pdf_array *)o;
2028 code = pdfi_create_colorspace_by_array(ctx, ArrayAlternate, 0, stream_dict, page_dict, &pcs_alt, inline_image);
2029 if (code < 0)
2030 goto pdfi_separation_error;
2031 break;
2032 default:
2033 pdfi_countdown(o);
2034 code = gs_error_typecheck;
2035 goto pdfi_separation_error;
2036 }
2037
2038 code = pdfi_array_get(ctx, color_array, index + 3, &transform);
2039 if (code < 0)
2040 goto pdfi_separation_error;
2041
2042 code = pdfi_build_function(ctx, &pfn, NULL, 1, transform, page_dict);
2043 if (code < 0)
2044 goto pdfi_separation_error;
2045
2046 if (pfn->params.m != 1 || pfn->params.n != cs_num_components(pcs_alt)) {
2047 code = gs_note_error(gs_error_rangecheck);
2048 goto pdfi_separation_error;
2049 }
2050
2051 code = gs_cspace_new_Separation(&pcs, pcs_alt, ctx->memory);
2052 if (code < 0)
2053 goto pdfi_separation_error;
2054
2055 rc_decrement(pcs_alt, "pdfi_create_Separation");
2056 pcs_alt = NULL;
2057 pcs->params.separation.mem = ctx->memory;
2058 pcs->params.separation.sep_type = sep_type;
2059 pcs->params.separation.sep_name = (char *)gs_alloc_bytes(ctx->memory->non_gc_memory, (size_t)name->length + 1, "pdfi_setseparationspace(ink)");
2060 if (pcs->params.separation.sep_name == NULL) {
2061 code = gs_note_error(gs_error_VMerror);
2062 goto pdfi_separation_error;
2063 }
2064 memcpy(pcs->params.separation.sep_name, name->data, name->length);
2065 pcs->params.separation.sep_name[name->length] = 0x00;
2066
2067 code = gs_cspace_set_sepr_function(pcs, pfn);
2068 if (code < 0)
2069 goto pdfi_separation_error;
2070
2071 if (ppcs!= NULL){
2072 /* FIXME
2073 * I can see no justification for this whatever, but if I don't do this then some
2074 * files with images in a /Separation colour space come out incorrectly. Even surrounding
2075 * this with a gsave/grestore pair causes differences.
2076 */
2077 code = pdfi_gs_setcolorspace(ctx, pcs);
2078 *ppcs = pcs;
2079 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
2080 } else {
2081 code = pdfi_gs_setcolorspace(ctx, pcs);
2082 /* release reference from construction */
2083 rc_decrement_only_cs(pcs, "setseparationspace");
2084 }
2085
2086 pdfi_countdown(name);
2087 pdfi_countdown(NamedAlternate);
2088 pdfi_countdown(ArrayAlternate);
2089 pdfi_countdown(transform);
2090 return_error(0);
2091
2092 pdfi_separation_error:
2093 pdfi_free_function(ctx, pfn);
2094 if (pcs_alt != NULL)
2095 rc_decrement_only_cs(pcs_alt, "setseparationspace");
2096 if(pcs != NULL)
2097 rc_decrement_only_cs(pcs, "setseparationspace");
2098 pdfi_countdown(name);
2099 pdfi_countdown(NamedAlternate);
2100 pdfi_countdown(ArrayAlternate);
2101 pdfi_countdown(transform);
2102 return code;
2103 }
2104
2105 static int pdfi_create_DeviceN(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image)
2106 {
2107 pdf_obj *o = NULL;
2108 pdf_name *NamedAlternate = NULL;
2109 pdf_array *ArrayAlternate = NULL, *inks = NULL;
2110 pdf_obj *transform = NULL;
2111 pdf_dict *attributes = NULL;
2112 pdf_dict *Colorants = NULL, *Process = NULL;
2113 gs_color_space *process_space = NULL;
2114 int code;
2115 uint64_t ix;
2116 gs_color_space *pcs = NULL, *pcs_alt = NULL;
2117 gs_function_t * pfn = NULL;
2118
2119 /* Start with the array of inks */
2120 code = pdfi_array_get_type(ctx, color_array, index + 1, PDF_ARRAY, (pdf_obj **)&inks);
2121 if (code < 0)
2122 goto pdfi_devicen_error;
2123
2124 if (pdfi_array_size(inks) > GS_CLIENT_COLOR_MAX_COMPONENTS)
2125 return_error(gs_error_limitcheck);
2126
2127 for (ix = 0;ix < pdfi_array_size(inks);ix++) {
2128 pdf_name *ink_name = NULL;
2129
2130 code = pdfi_array_get_type(ctx, inks, ix, PDF_NAME, (pdf_obj **)&ink_name);
2131 if (code < 0)
2132 return code;
2133
2134 if (ink_name->length == 3 && memcmp(ink_name->data, "All", 3) == 0) {
2135 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_DEVICEN_USES_ALL, "pdfi_create_DeviceN", (char *)"WARNING: DeviceN space using /All ink name")) < 0)
2136 return code;
2137 }
2138 pdfi_countdown(ink_name);
2139 ink_name = NULL;
2140 }
2141
2142 /* Sigh, Acrobat allows this, even though its contra the spec. Convert to
2143 * a /Separation space, and then return. Actually Acrobat does not always permit this, see
2144 * tests_private/comparefiles/Testform.v1.0.2.pdf.
2145 */
2146 if (pdfi_array_size(inks) == 1) {
2147 pdf_name *ink_name = NULL;
2148 pdf_array *sep_color_array = NULL;
2149 pdf_obj *obj = NULL;
2150
2151 code = pdfi_array_get_type(ctx, inks, 0, PDF_NAME, (pdf_obj **)&ink_name);
2152 if (code < 0)
2153 goto pdfi_devicen_error;
2154
2155 if (ink_name->length == 3 && memcmp(ink_name->data, "All", 3) == 0) {
2156 code = pdfi_array_alloc(ctx, 4, &sep_color_array);
2157 if (code < 0)
2158 goto all_error;
2159 pdfi_countup(sep_color_array);
2160 code = pdfi_name_alloc(ctx, (byte *)"Separation", 10, &obj);
2161 if (code < 0)
2162 goto all_error;
2163 pdfi_countup(obj);
2164 code = pdfi_array_put(ctx, sep_color_array, 0, obj);
2165 if (code < 0)
2166 goto all_error;
2167 pdfi_countdown(obj);
2168 obj = NULL;
2169 code = pdfi_array_put(ctx, sep_color_array, 1, (pdf_obj *)ink_name);
2170 if (code < 0)
2171 goto all_error;
2172 code = pdfi_array_get(ctx, color_array, index + 2, &obj);
2173 if (code < 0)
2174 goto all_error;
2175 code = pdfi_array_put(ctx, sep_color_array, 2, obj);
2176 if (code < 0)
2177 goto all_error;
2178 pdfi_countdown(obj);
2179 obj = NULL;
2180 code = pdfi_array_get(ctx, color_array, index + 3, &obj);
2181 if (code < 0)
2182 goto all_error;
2183 code = pdfi_array_put(ctx, sep_color_array, 3, obj);
2184 if (code < 0)
2185 goto all_error;
2186 pdfi_countdown(obj);
2187 obj = NULL;
2188
2189 code = pdfi_create_Separation(ctx, sep_color_array, 0, stream_dict, page_dict, ppcs, inline_image);
2190 if (code < 0)
2191 goto all_error;
2192 all_error:
2193 pdfi_countdown(ink_name);
2194 pdfi_countdown(sep_color_array);
2195 pdfi_countdown(obj);
2196 pdfi_countdown(inks);
2197 return code;
2198 } else
2199 pdfi_countdown(ink_name);
2200 }
2201
2202 /* Deal with alternate space */
2203 code = pdfi_array_get(ctx, color_array, index + 2, &o);
2204 if (code < 0)
2205 goto pdfi_devicen_error;
2206
2207 switch (pdfi_type_of(o)) {
2208 case PDF_NAME:
2209 NamedAlternate = (pdf_name *)o;
2210 code = pdfi_create_colorspace_by_name(ctx, NamedAlternate, stream_dict, page_dict, &pcs_alt, inline_image);
2211 if (code < 0)
2212 goto pdfi_devicen_error;
2213 break;
2214 case PDF_ARRAY:
2215 ArrayAlternate = (pdf_array *)o;
2216 code = pdfi_create_colorspace_by_array(ctx, ArrayAlternate, 0, stream_dict, page_dict, &pcs_alt, inline_image);
2217 if (code < 0)
2218 /* OSS-fuzz error 42973; we don't need to count down 'o' here because
2219 * we have assigned it to ArrayAlternate and both the success and error
2220 * paths count down ArrayAlternate.
2221 */
2222 goto pdfi_devicen_error;
2223 break;
2224 default:
2225 code = gs_error_typecheck;
2226 pdfi_countdown(o);
2227 goto pdfi_devicen_error;
2228 }
2229
2230 /* Now the tint transform */
2231 code = pdfi_array_get(ctx, color_array, index + 3, &transform);
2232 if (code < 0)
2233 goto pdfi_devicen_error;
2234
2235 code = pdfi_build_function(ctx, &pfn, NULL, 1, transform, page_dict);
2236 if (code < 0)
2237 goto pdfi_devicen_error;
2238
2239 if (pfn->params.m != pdfi_array_size(inks) || pfn->params.n != cs_num_components(pcs_alt)) {
2240 code = gs_note_error(gs_error_rangecheck);
2241 goto pdfi_devicen_error;
2242 }
2243
2244 code = gs_cspace_new_DeviceN(&pcs, pdfi_array_size(inks), pcs_alt, ctx->memory);
2245 if (code < 0)
2246 return code;
2247
2248 rc_decrement(pcs_alt, "pdfi_create_DeviceN");
2249 pcs_alt = NULL;
2250 pcs->params.device_n.mem = ctx->memory;
2251
2252 for (ix = 0;ix < pdfi_array_size(inks);ix++) {
2253 pdf_name *ink_name;
2254
2255 ink_name = NULL;
2256 code = pdfi_array_get_type(ctx, inks, ix, PDF_NAME, (pdf_obj **)&ink_name);
2257 if (code < 0)
2258 goto pdfi_devicen_error;
2259
2260 pcs->params.device_n.names[ix] = (char *)gs_alloc_bytes(ctx->memory->non_gc_memory, (size_t)ink_name->length + 1, "pdfi_setdevicenspace(ink)");
2261 if (pcs->params.device_n.names[ix] == NULL) {
2262 code = gs_note_error(gs_error_VMerror);
2263 goto pdfi_devicen_error;
2264 }
2265 memcpy(pcs->params.device_n.names[ix], ink_name->data, ink_name->length);
2266 pcs->params.device_n.names[ix][ink_name->length] = 0x00;
2267 pdfi_countdown(ink_name);
2268 }
2269
2270 code = gs_cspace_set_devn_function(pcs, pfn);
2271 if (code < 0)
2272 goto pdfi_devicen_error;
2273
2274 if (pdfi_array_size(color_array) >= index + 5) {
2275 pdf_obj *ColorSpace = NULL;
2276 pdf_array *Components = NULL;
2277 pdf_obj *subtype = NULL;
2278
2279 code = pdfi_array_get_type(ctx, color_array, index + 4, PDF_DICT, (pdf_obj **)&attributes);
2280 if (code < 0)
2281 goto pdfi_devicen_error;
2282
2283 code = pdfi_dict_knownget(ctx, attributes, "Subtype", (pdf_obj **)&subtype);
2284 if (code < 0)
2285 goto pdfi_devicen_error;
2286
2287 if (code == 0) {
2288 pcs->params.device_n.subtype = gs_devicen_DeviceN;
2289 } else {
2290 switch (pdfi_type_of(subtype)) {
2291 case PDF_NAME:
2292 case PDF_STRING:
2293 if (memcmp(((pdf_name *)subtype)->data, "DeviceN", 7) == 0) {
2294 pcs->params.device_n.subtype = gs_devicen_DeviceN;
2295 } else {
2296 if (memcmp(((pdf_name *)subtype)->data, "NChannel", 8) == 0) {
2297 pcs->params.device_n.subtype = gs_devicen_NChannel;
2298 } else {
2299 pdfi_countdown(subtype);
2300 goto pdfi_devicen_error;
2301 }
2302 }
2303 pdfi_countdown(subtype);
2304 break;
2305 default:
2306 pdfi_countdown(subtype);
2307 goto pdfi_devicen_error;
2308 }
2309 }
2310
2311 code = pdfi_dict_knownget_type(ctx, attributes, "Process", PDF_DICT, (pdf_obj **)&Process);
2312 if (code < 0)
2313 goto pdfi_devicen_error;
2314
2315 if (Process != NULL && pdfi_dict_entries(Process) != 0) {
2316 int ix = 0;
2317 pdf_obj *name;
2318
2319 code = pdfi_dict_get(ctx, Process, "ColorSpace", (pdf_obj **)&ColorSpace);
2320 if (code < 0)
2321 goto pdfi_devicen_error;
2322
2323 code = pdfi_create_colorspace(ctx, ColorSpace, stream_dict, page_dict, &process_space, inline_image);
2324 pdfi_countdown(ColorSpace);
2325 if (code < 0)
2326 goto pdfi_devicen_error;
2327
2328 pcs->params.device_n.devn_process_space = process_space;
2329
2330 code = pdfi_dict_get_type(ctx, Process, "Components", PDF_ARRAY, (pdf_obj **)&Components);
2331 if (code < 0)
2332 goto pdfi_devicen_error;
2333
2334 pcs->params.device_n.num_process_names = pdfi_array_size(Components);
2335 pcs->params.device_n.process_names = (char **)gs_alloc_bytes(pcs->params.device_n.mem->non_gc_memory, (size_t)pdfi_array_size(Components) * sizeof(char *), "pdfi_devicen(Processnames)");
2336 if (pcs->params.device_n.process_names == NULL) {
2337 code = gs_error_VMerror;
2338 goto pdfi_devicen_error;
2339 }
2340 memset(pcs->params.device_n.process_names, 0x00, pdfi_array_size(Components) * sizeof(char *));
2341
2342 for (ix = 0; ix < pcs->params.device_n.num_process_names; ix++) {
2343 code = pdfi_array_get(ctx, Components, ix, &name);
2344 if (code < 0) {
2345 pdfi_countdown(Components);
2346 goto pdfi_devicen_error;
2347 }
2348
2349 switch (pdfi_type_of(name)) {
2350 case PDF_NAME:
2351 case PDF_STRING:
2352 pcs->params.device_n.process_names[ix] = (char *)gs_alloc_bytes(pcs->params.device_n.mem->non_gc_memory, ((pdf_name *)name)->length + (size_t)1, "pdfi_devicen(Processnames)");
2353 if (pcs->params.device_n.process_names[ix] == NULL) {
2354 pdfi_countdown(Components);
2355 pdfi_countdown(name);
2356 code = gs_error_VMerror;
2357 goto pdfi_devicen_error;
2358 }
2359 memcpy(pcs->params.device_n.process_names[ix], ((pdf_name *)name)->data, ((pdf_name *)name)->length);
2360 pcs->params.device_n.process_names[ix][((pdf_name *)name)->length] = 0x00;
2361 pdfi_countdown(name);
2362 break;
2363 default:
2364 pdfi_countdown(Components);
2365 pdfi_countdown(name);
2366 goto pdfi_devicen_error;
2367 }
2368 }
2369 pdfi_countdown(Components);
2370 }
2371
2372 code = pdfi_dict_knownget_type(ctx, attributes, "Colorants", PDF_DICT, (pdf_obj **)&Colorants);
2373 if (code < 0)
2374 goto pdfi_devicen_error;
2375
2376 if (Colorants != NULL && pdfi_dict_entries(Colorants) != 0) {
2377 uint64_t ix = 0;
2378 pdf_obj *Colorant = NULL, *Space = NULL;
2379 char *colorant_name;
2380 gs_color_space *colorant_space = NULL;
2381
2382 code = pdfi_dict_first(ctx, Colorants, &Colorant, &Space, &ix);
2383 if (code < 0)
2384 goto pdfi_devicen_error;
2385
2386 do {
2387 switch (pdfi_type_of(Space)) {
2388 case PDF_STRING:
2389 case PDF_NAME:
2390 case PDF_ARRAY:
2391 break;
2392 default:
2393 pdfi_countdown(Space);
2394 pdfi_countdown(Colorant);
2395 code = gs_note_error(gs_error_typecheck);
2396 goto pdfi_devicen_error;
2397 }
2398 switch (pdfi_type_of(Colorant)) {
2399 case PDF_STRING:
2400 case PDF_NAME:
2401 break;
2402 default:
2403 pdfi_countdown(Space);
2404 pdfi_countdown(Colorant);
2405 code = gs_note_error(gs_error_typecheck);
2406 goto pdfi_devicen_error;
2407 }
2408
2409 code = pdfi_create_colorspace(ctx, Space, stream_dict, page_dict, &colorant_space, inline_image);
2410 if (code < 0) {
2411 pdfi_countdown(Space);
2412 pdfi_countdown(Colorant);
2413 goto pdfi_devicen_error;
2414 }
2415
2416 colorant_name = (char *)gs_alloc_bytes(pcs->params.device_n.mem->non_gc_memory, ((pdf_name *)Colorant)->length + (size_t)1, "pdfi_devicen(colorant)");
2417 if (colorant_name == NULL) {
2418 rc_decrement_cs(colorant_space, "pdfi_devicen(colorant)");
2419 pdfi_countdown(Space);
2420 pdfi_countdown(Colorant);
2421 code = gs_note_error(gs_error_VMerror);
2422 goto pdfi_devicen_error;
2423 }
2424 memcpy(colorant_name, ((pdf_name *)Colorant)->data, ((pdf_name *)Colorant)->length);
2425 colorant_name[((pdf_name *)Colorant)->length] = 0x00;
2426
2427 code = gs_attach_colorant_to_space(colorant_name, pcs, colorant_space, pcs->params.device_n.mem->non_gc_memory);
2428 if (code < 0) {
2429 gs_free_object(pcs->params.device_n.mem->non_gc_memory, colorant_name, "pdfi_devicen(colorant)");
2430 rc_decrement_cs(colorant_space, "pdfi_devicen(colorant)");
2431 pdfi_countdown(Space);
2432 pdfi_countdown(Colorant);
2433 code = gs_note_error(gs_error_VMerror);
2434 goto pdfi_devicen_error;
2435 }
2436
2437 /* We've attached the colorant colour space to the DeviceN space, we no longer need this
2438 * reference to it, so discard it.
2439 */
2440 rc_decrement_cs(colorant_space, "pdfi_devicen(colorant)");
2441 pdfi_countdown(Space);
2442 pdfi_countdown(Colorant);
2443 Space = Colorant = NULL;
2444
2445 code = pdfi_dict_next(ctx, Colorants, &Colorant, &Space, &ix);
2446 if (code == gs_error_undefined)
2447 break;
2448
2449 if (code < 0) {
2450 pdfi_countdown(Space);
2451 pdfi_countdown(Colorant);
2452 goto pdfi_devicen_error;
2453 }
2454 }while (1);
2455 }
2456 }
2457
2458 if (ppcs!= NULL){
2459 *ppcs = pcs;
2460 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
2461 } else {
2462 code = pdfi_gs_setcolorspace(ctx, pcs);
2463 /* release reference from construction */
2464 rc_decrement_only_cs(pcs, "setdevicenspace");
2465 }
2466 pdfi_countdown(Process);
2467 pdfi_countdown(Colorants);
2468 pdfi_countdown(attributes);
2469 pdfi_countdown(inks);
2470 pdfi_countdown(NamedAlternate);
2471 pdfi_countdown(ArrayAlternate);
2472 pdfi_countdown(transform);
2473 return_error(0);
2474
2475 pdfi_devicen_error:
2476 pdfi_free_function(ctx, pfn);
2477 if (pcs_alt != NULL)
2478 rc_decrement_only_cs(pcs_alt, "setseparationspace");
2479 if(pcs != NULL)
2480 rc_decrement_only_cs(pcs, "setseparationspace");
2481 pdfi_countdown(Process);
2482 pdfi_countdown(Colorants);
2483 pdfi_countdown(attributes);
2484 pdfi_countdown(inks);
2485 pdfi_countdown(NamedAlternate);
2486 pdfi_countdown(ArrayAlternate);
2487 pdfi_countdown(transform);
2488 return code;
2489 }
2490
2491 /* Now /Indexed spaces, essentially we just need to set the underlying space(s) and then set
2492 * /Indexed.
2493 */
2494 static int
2495 pdfi_create_indexed(pdf_context *ctx, pdf_array *color_array, int index,
2496 pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image)
2497 {
2498 pdf_obj *space=NULL, *lookup=NULL;
2499 int code;
2500 int64_t hival, lookup_length = 0;
2501 int num_values;
2502 gs_color_space *pcs=NULL, *pcs_base=NULL;
2503 gs_color_space_index base_type;
2504 byte *Buffer = NULL;
2505
2506 if (index != 0)
2507 return_error(gs_error_syntaxerror);
2508
2509 code = pdfi_array_get_int(ctx, color_array, index + 2, &hival);
2510 if (code < 0)
2511 return code;
2512
2513 if (hival > 255 || hival < 0)
2514 return_error(gs_error_syntaxerror);
2515
2516 code = pdfi_array_get(ctx, color_array, index + 1, &space);
2517 if (code < 0)
2518 goto exit;
2519
2520 code = pdfi_create_colorspace(ctx, space, stream_dict, page_dict, &pcs_base, inline_image);
2521 if (code < 0)
2522 goto exit;
2523
2524 base_type = gs_color_space_get_index(pcs_base);
2525 if (base_type == gs_color_space_index_Pattern || base_type == gs_color_space_index_Indexed) {
2526 code = gs_note_error(gs_error_typecheck);
2527 goto exit;
2528 }
2529
2530 (void)pcs_base->type->install_cspace(pcs_base, ctx->pgs);
2531
2532 code = pdfi_array_get(ctx, color_array, index + 3, &lookup);
2533 if (code < 0)
2534 goto exit;
2535
2536 num_values = (hival+1) * cs_num_components(pcs_base);
2537 lookup_length = num_values;
2538
2539 switch (pdfi_type_of(lookup)) {
2540 case PDF_STREAM:
2541 code = pdfi_stream_to_buffer(ctx, (pdf_stream *)lookup, &Buffer, &lookup_length);
2542 if (code < 0)
2543 goto exit;
2544 break;
2545 case PDF_STRING:
2546 {
2547 pdf_string *lookup_string = (pdf_string *)lookup; /* alias */
2548
2549 Buffer = gs_alloc_bytes(ctx->memory, lookup_string->length, "pdfi_create_indexed (lookup buffer)");
2550 if (Buffer == NULL) {
2551 code = gs_note_error(gs_error_VMerror);
2552 goto exit;
2553 }
2554
2555 memcpy(Buffer, lookup_string->data, lookup_string->length);
2556 lookup_length = lookup_string->length;
2557 break;
2558 }
2559 default:
2560 code = gs_note_error(gs_error_typecheck);
2561 goto exit;
2562 }
2563
2564 if (num_values > lookup_length) {
2565 /* This is not legal, but Acrobat seems to accept it */
2566 byte *SBuffer = NULL;
2567
2568 code = pdfi_set_error_stop(ctx, gs_error_rangecheck, NULL, E_PDF_BAD_INDEXED_STRING, "pdfi_create_indexed", NULL);
2569 if (code < 0)
2570 goto exit;
2571 SBuffer = gs_alloc_bytes(ctx->memory, num_values, "pdfi_create_indexed (lookup buffer)");
2572 if (SBuffer == NULL) {
2573 code = gs_note_error(gs_error_VMerror);
2574 goto exit;
2575 }
2576 memcpy(SBuffer, Buffer, lookup_length);
2577 memset(&SBuffer[lookup_length], 0x00, num_values - lookup_length);
2578 gs_free_object(ctx->memory, Buffer, "pdfi_create_indexed (lookup buffer)");
2579 Buffer = SBuffer;
2580 }
2581
2582 /* If we have a named color profile and the base space is DeviceN or
2583 Separation use a different set of procedures to ensure the named
2584 color remapping code is used */
2585 if (ctx->pgs->icc_manager->device_named != NULL &&
2586 (base_type == gs_color_space_index_Separation ||
2587 base_type == gs_color_space_index_DeviceN))
2588 pcs = gs_cspace_alloc(ctx->memory, &gs_color_space_type_Indexed_Named);
2589 else
2590 pcs = gs_cspace_alloc(ctx->memory, &gs_color_space_type_Indexed);
2591
2592 if (pcs == NULL) {
2593 code = gs_note_error(gs_error_VMerror);
2594 goto exit;
2595 }
2596 /* NOTE: we don't need to increment the reference to pcs_base, since it is already 1 */
2597 pcs->base_space = pcs_base;
2598
2599 pcs->params.indexed.lookup.table.size = num_values;
2600 pcs->params.indexed.use_proc = 0;
2601 pcs->params.indexed.hival = hival;
2602 pcs->params.indexed.n_comps = cs_num_components(pcs_base);
2603 pcs->params.indexed.lookup.table.data = Buffer;
2604 Buffer = NULL;
2605
2606 if (ppcs != NULL) {
2607 *ppcs = pcs;
2608 pdfi_set_colour_callback(pcs, ctx, pdfi_cspace_free_callback);
2609 }
2610 else {
2611 code = pdfi_gs_setcolorspace(ctx, pcs);
2612 /* release reference from construction */
2613 rc_decrement_only_cs(pcs, "setindexedspace");
2614 }
2615
2616 exit:
2617 if (code != 0)
2618 rc_decrement(pcs_base, "pdfi_create_indexed(pcs_base) error");
2619 if (Buffer)
2620 gs_free_object(ctx->memory, Buffer, "pdfi_create_indexed (decompression buffer)");
2621 pdfi_countdown(space);
2622 pdfi_countdown(lookup);
2623 return code;
2624 }
2625
2626 static int pdfi_create_DeviceGray(pdf_context *ctx, gs_color_space **ppcs)
2627 {
2628 int code = 0;
2629
2630 if (ppcs != NULL) {
2631 /* Bug 709017: Don't return DefaultGray_cs if we are within a softmask */
2632 if (ctx->page.DefaultGray_cs != NULL &&
2633 (dev_proc(ctx->pgs->device, dev_spec_op)(ctx->pgs->device, gxdso_in_smask_construction, NULL, 0) <= 0)) {
2634 *ppcs = ctx->page.DefaultGray_cs;
2635 rc_increment(*ppcs);
2636 } else {
2637 *ppcs = gs_cspace_new_DeviceGray(ctx->memory);
2638 if (*ppcs == NULL)
2639 code = gs_note_error(gs_error_VMerror);
2640 else {
2641 code = ((gs_color_space *)*ppcs)->type->install_cspace(*ppcs, ctx->pgs);
2642 if (code < 0) {
2643 rc_decrement_only_cs(*ppcs, "pdfi_create_DeviceGray");
2644 *ppcs = NULL;
2645 }
2646 }
2647 if (*ppcs != NULL)
2648 pdfi_set_colour_callback(*ppcs, ctx, pdfi_cspace_free_callback);
2649 }
2650 } else {
2651 code = pdfi_gs_setgray(ctx, 0);
2652 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
2653 }
2654 return code;
2655 }
2656
2657 static int pdfi_create_DeviceRGB(pdf_context *ctx, gs_color_space **ppcs)
2658 {
2659 int code = 0;
2660
2661 if (ppcs != NULL) {
2662 /* Bug 709017: Don't return DefaultRGB_cs if we are within a softmask */
2663 if (ctx->page.DefaultRGB_cs != NULL &&
2664 (dev_proc(ctx->pgs->device, dev_spec_op)(ctx->pgs->device, gxdso_in_smask_construction, NULL, 0) <= 0)) {
2665 *ppcs = ctx->page.DefaultRGB_cs;
2666 rc_increment(*ppcs);
2667 } else {
2668 *ppcs = gs_cspace_new_DeviceRGB(ctx->memory);
2669 if (*ppcs == NULL)
2670 code = gs_note_error(gs_error_VMerror);
2671 else {
2672 code = ((gs_color_space *)*ppcs)->type->install_cspace(*ppcs, ctx->pgs);
2673 if (code < 0) {
2674 rc_decrement_only_cs(*ppcs, "pdfi_create_DeviceRGB");
2675 *ppcs = NULL;
2676 }
2677 }
2678 if (*ppcs != NULL)
2679 pdfi_set_colour_callback(*ppcs, ctx, pdfi_cspace_free_callback);
2680 }
2681 } else {
2682 code = pdfi_gs_setrgbcolor(ctx, 0, 0, 0);
2683 }
2684 return code;
2685 }
2686
2687 static int pdfi_create_DeviceCMYK(pdf_context *ctx, gs_color_space **ppcs)
2688 {
2689 int code = 0;
2690
2691 if (ppcs != NULL) {
2692 /* Bug 709017: Don't return DefaultCMYK_cs if we are within a softmask */
2693 if (ctx->page.DefaultCMYK_cs != NULL &&
2694 (dev_proc(ctx->pgs->device, dev_spec_op)(ctx->pgs->device, gxdso_in_smask_construction, NULL, 0) <= 0)) {
2695 *ppcs = ctx->page.DefaultCMYK_cs;
2696 rc_increment(*ppcs);
2697 } else {
2698 *ppcs = gs_cspace_new_DeviceCMYK(ctx->memory);
2699 if (*ppcs == NULL)
2700 code = gs_note_error(gs_error_VMerror);
2701 else {
2702 code = ((gs_color_space *)*ppcs)->type->install_cspace(*ppcs, ctx->pgs);
2703 if (code < 0) {
2704 rc_decrement_only_cs(*ppcs, "pdfi_create_DeviceCMYK");
2705 *ppcs = NULL;
2706 }
2707 }
2708 if (*ppcs != NULL)
2709 pdfi_set_colour_callback(*ppcs, ctx, pdfi_cspace_free_callback);
2710 }
2711 } else {
2712 code = pdfi_gs_setcmykcolor(ctx, 0, 0, 0, 1);
2713 pdfi_set_colour_callback(ctx->pgs->color[0].color_space, ctx, pdfi_cspace_free_callback);
2714 }
2715 return code;
2716 }
2717
2718 static int pdfi_create_JPX_space(pdf_context *ctx, const char *name, int num_components, gs_color_space **ppcs)
2719 {
2720 int code, icc_N;
2721 float range_buff[6] = {0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f};
2722
2723 code = pdfi_create_icc(ctx, (char *)name, NULL, num_components, &icc_N, range_buff, 0, ppcs);
2724 return code;
2725 }
2726
2727 /* These next routines allow us to use recursion to set up colour spaces. We can set
2728 * colour space starting from a name (which can be a named resource) or an array.
2729 * If we get a name, and its a named resource we dereference it and go round again.
2730 * If its an array we select the correct handler (above) for that space. The space
2731 * handler will call pdfi_create_colorspace() to set the underlying space(s) which
2732 * may mean calling pdfi_create_colorspace again....
2733 */
2734 static int
2735 pdfi_create_colorspace_by_array(pdf_context *ctx, pdf_array *color_array, int index,
2736 pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs,
2737 bool inline_image)
2738 {
2739 int code;
2740 pdf_name *space = NULL;
2741 pdf_array *a = NULL;
2742
2743 code = pdfi_array_get_type(ctx, color_array, index, PDF_NAME, (pdf_obj **)&space);
2744 if (code != 0)
2745 goto exit;
2746
2747 code = 0;
2748 if (pdfi_name_is(space, "G") || pdfi_name_is(space, "DeviceGray")) {
2749 if (pdfi_name_is(space, "G") && !inline_image) {
2750 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_array", NULL)) < 0)
2751 return code;
2752 }
2753 code = pdfi_create_DeviceGray(ctx, ppcs);
2754 } else if (pdfi_name_is(space, "I") || pdfi_name_is(space, "Indexed")) {
2755 if (pdfi_name_is(space, "I") && !inline_image) {
2756 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_array", NULL);
2757 if (ctx->args.pdfstoponwarning)
2758 return_error(gs_error_syntaxerror);
2759 }
2760 code = pdfi_create_indexed(ctx, color_array, index, stream_dict, page_dict, ppcs, inline_image);
2761 } else if (pdfi_name_is(space, "Lab")) {
2762 code = pdfi_create_Lab(ctx, color_array, index, stream_dict, page_dict, ppcs);
2763 } else if (pdfi_name_is(space, "RGB") || pdfi_name_is(space, "DeviceRGB")) {
2764 if (pdfi_name_is(space, "RGB") && !inline_image) {
2765 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_array", NULL);
2766 if (ctx->args.pdfstoponwarning)
2767 return_error(gs_error_syntaxerror);
2768 }
2769 code = pdfi_create_DeviceRGB(ctx, ppcs);
2770 } else if (pdfi_name_is(space, "CMYK") || pdfi_name_is(space, "DeviceCMYK") || pdfi_name_is(space, "CalCMYK")) {
2771 if (pdfi_name_is(space, "CMYK") && !inline_image) {
2772 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_array", NULL);
2773 if (ctx->args.pdfstoponwarning)
2774 return_error(gs_error_syntaxerror);
2775 }
2776 code = pdfi_create_DeviceCMYK(ctx, ppcs);
2777 } else if (pdfi_name_is(space, "CalRGB")) {
2778 code = pdfi_create_CalRGB(ctx, color_array, index, stream_dict, page_dict, ppcs);
2779 } else if (pdfi_name_is(space, "CalGray")) {
2780 code = pdfi_create_CalGray(ctx, color_array, index, stream_dict, page_dict, ppcs);
2781 } else if (pdfi_name_is(space, "Pattern")) {
2782 if (index != 0)
2783 code = gs_note_error(gs_error_syntaxerror);
2784 else
2785 code = pdfi_pattern_create(ctx, color_array, stream_dict, page_dict, ppcs);
2786 } else if (pdfi_name_is(space, "DeviceN")) {
2787 code = pdfi_create_DeviceN(ctx, color_array, index, stream_dict, page_dict, ppcs, inline_image);
2788 } else if (pdfi_name_is(space, "ICCBased")) {
2789 code = pdfi_create_iccbased(ctx, color_array, index, stream_dict, page_dict, ppcs, inline_image);
2790 } else if (pdfi_name_is(space, "Separation")) {
2791 code = pdfi_create_Separation(ctx, color_array, index, stream_dict, page_dict, ppcs, inline_image);
2792 } else {
2793 if (stream_dict == NULL)
2794 code = gs_note_error(gs_error_syntaxerror);
2795 else
2796 code = pdfi_find_resource(ctx, (unsigned char *)"ColorSpace",
2797 space, (pdf_dict *)stream_dict, page_dict, (pdf_obj **)&a);
2798 if (code < 0)
2799 goto exit;
2800
2801 if (pdfi_type_of(a) != PDF_ARRAY) {
2802 code = gs_note_error(gs_error_typecheck);
2803 goto exit;
2804 }
2805
2806 /* recursion */
2807 code = pdfi_create_colorspace_by_array(ctx, a, 0, stream_dict, page_dict, ppcs, inline_image);
2808 }
2809
2810 exit:
2811 pdfi_countdown(space);
2812 pdfi_countdown(a);
2813 return code;
2814 }
2815
2816 static int
2817 pdfi_create_colorspace_by_name(pdf_context *ctx, pdf_name *name,
2818 pdf_dict *stream_dict, pdf_dict *page_dict,
2819 gs_color_space **ppcs, bool inline_image)
2820 {
2821 int code = 0;
2822 gs_id oldid = 0;
2823
2824 if (pdfi_name_is(name, "G") || pdfi_name_is(name, "DeviceGray")) {
2825 if (pdfi_name_is(name, "G") && !inline_image) {
2826 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_name", NULL)) < 0)
2827 return code;
2828 }
2829 code = pdfi_create_DeviceGray(ctx, ppcs);
2830 } else if (pdfi_name_is(name, "RGB") || pdfi_name_is(name, "DeviceRGB")) {
2831 if (pdfi_name_is(name, "RGB") && !inline_image) {
2832 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_name", NULL)) < 0)
2833 return code;
2834 }
2835 code = pdfi_create_DeviceRGB(ctx, ppcs);
2836 } else if (pdfi_name_is(name, "CMYK") || pdfi_name_is(name, "DeviceCMYK") || pdfi_name_is(name, "CalCMYK")) {
2837 if (pdfi_name_is(name, "CMYK") && !inline_image) {
2838 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_name", NULL)) < 0)
2839 return code;
2840 }
2841 code = pdfi_create_DeviceCMYK(ctx, ppcs);
2842 } else if (pdfi_name_is(name, "Pattern")) {
2843 code = pdfi_pattern_create(ctx, NULL, stream_dict, page_dict, ppcs);
2844 } else if (pdfi_name_is(name, "esRGBICC")) { /* These 4 spaces are 'special' for JPX images */
2845 code = pdfi_create_JPX_space(ctx, "esrgb", 3, ppcs); /* the names are non-standad and must match those in */
2846 } else if (pdfi_name_is(name, "rommRGBICC")) { /* pdfi_image_get_color() in pdf_image.c */
2847 code = pdfi_create_JPX_space(ctx, "rommrgb", 3, ppcs); /* Note that the Lab space for JPX images does not use */
2848 } else if (pdfi_name_is(name, "sRGBICC")) { /* a special space but simply constructs an appropriate */
2849 code = pdfi_create_JPX_space(ctx, "srgb", 3, ppcs); /* pdf_array object with the corerct contents for an */
2850 } else if (pdfi_name_is(name, "sGrayICC")) { /* Lab space with a D65 white point. */
2851 code = pdfi_create_JPX_space(ctx, "sgray", 1, ppcs);
2852 } else {
2853 pdf_obj *ref_space = NULL;
2854
2855 if (ppcs == NULL && check_same_current_space(ctx, name) == 1)
2856 return 0;
2857
2858 code = pdfi_find_resource(ctx, (unsigned char *)"ColorSpace", name, (pdf_dict *)stream_dict,
2859 page_dict, &ref_space);
2860 if (code < 0)
2861 return code;
2862
2863 if (pdfi_type_of(ref_space) == PDF_NAME) {
2864 if (ref_space->object_num != 0 && ref_space->object_num == name->object_num) {
2865 pdfi_countdown(ref_space);
2866 return_error(gs_error_circular_reference);
2867 }
2868 if (((pdf_name *)ref_space)->length <= 0) {
2869 pdfi_countdown(ref_space);
2870 return_error(gs_error_syntaxerror);
2871 }
2872 if (((pdf_name *)ref_space)->length == name->length && memcmp(((pdf_name *)ref_space)->data, name->data, name->length) == 0) {
2873 pdfi_countdown(ref_space);
2874 return_error(gs_error_circular_reference);
2875 }
2876 }
2877
2878 oldid = ctx->pgs->color[0].color_space->id;
2879 /* recursion */
2880 code = pdfi_create_colorspace(ctx, ref_space, stream_dict, page_dict, ppcs, inline_image);
2881
2882 if (code >= 0) {
2883 if (oldid != ctx->pgs->color[0].color_space->id) {
2884 if (ppcs != NULL)
2885 pdfi_set_colourspace_name(ctx, *ppcs, name);
2886 else
2887 pdfi_set_colourspace_name(ctx, ctx->pgs->color[0].color_space, name);
2888 }
2889 }
2890
2891 pdfi_countdown(ref_space);
2892 return code;
2893 }
2894
2895 /* If we got here, it's a recursion base case, and ppcs should have been set if requested */
2896 if (ppcs != NULL && *ppcs == NULL)
2897 code = gs_note_error(gs_error_VMerror);
2898 return code;
2899 }
2900
2901 /*
2902 * Gets icc profile data from the provided stream.
2903 * Position in the stream is NOT preserved.
2904 * This is raw data, not filtered, so no need to worry about compression.
2905 * (Used for JPXDecode images)
2906 */
2907 int
2908 pdfi_create_icc_colorspace_from_stream(pdf_context *ctx, pdf_c_stream *stream, gs_offset_t offset,
2909 unsigned int length, int comps, int *icc_N, ulong dictkey, gs_color_space **ppcs)
2910 {
2911 pdf_c_stream *profile_stream = NULL;
2912 byte *profile_buffer;
2913 int code, code1;
2914 float range[8] = {0,1,0,1,0,1,0,1};
2915
2916 /* Move to the start of the profile data */
2917 pdfi_seek(ctx, stream, offset, SEEK_SET);
2918
2919 /* The ICC profile reading code (irritatingly) requires a seekable stream, because it
2920 * rewinds it to the start, then seeks to the end to find the size, then rewinds the
2921 * stream again.
2922 * Ideally we would use a ReusableStreamDecode filter here, but that is largely
2923 * implemented in PostScript (!) so we can't use it. What we can do is create a
2924 * string sourced stream in memory, which is at least seekable.
2925 */
2926 code = pdfi_open_memory_stream_from_stream(ctx, length, &profile_buffer, stream, &profile_stream, true);
2927 if (code < 0) {
2928 return code;
2929 }
2930
2931 /* Now, finally, we can call the code to create and set the profile */
2932 code = pdfi_create_icc(ctx, NULL, profile_stream->s, comps, icc_N, range, dictkey, ppcs);
2933
2934 code1 = pdfi_close_memory_stream(ctx, profile_buffer, profile_stream);
2935
2936 if (code == 0)
2937 code = code1;
2938
2939 return code;
2940 }
2941
2942 int pdfi_create_colorspace(pdf_context *ctx, pdf_obj *space, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image)
2943 {
2944 int code;
2945
2946 code = pdfi_loop_detector_mark(ctx);
2947 if (code < 0)
2948 return code;
2949
2950 switch (pdfi_type_of(space)) {
2951 case PDF_NAME:
2952 code = pdfi_create_colorspace_by_name(ctx, (pdf_name *)space, stream_dict, page_dict, ppcs, inline_image);
2953 break;
2954 case PDF_ARRAY:
2955 code = pdfi_create_colorspace_by_array(ctx, (pdf_array *)space, 0, stream_dict, page_dict, ppcs, inline_image);
2956 break;
2957 default:
2958 pdfi_loop_detector_cleartomark(ctx);
2959 return_error(gs_error_typecheck);
2960 }
2961 if (code >= 0 && ppcs && *ppcs)
2962 (void)(*ppcs)->type->install_cspace(*ppcs, ctx->pgs);
2963
2964 (void)pdfi_loop_detector_cleartomark(ctx);
2965 return code;
2966 }
2967
2968 int pdfi_setcolorspace(pdf_context *ctx, pdf_obj *space, pdf_dict *stream_dict, pdf_dict *page_dict)
2969 {
2970 return pdfi_create_colorspace(ctx, space, stream_dict, page_dict, NULL, false);
2971 }
2972
2973 /* And finally, the implementation of the actual PDF operators CS and cs */
2974 int pdfi_setstrokecolor_space(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
2975 {
2976 int code;
2977 pdf_obj *n = NULL;
2978
2979 if (pdfi_count_stack(ctx) < 1)
2980 return_error(gs_error_stackunderflow);
2981
2982 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
2983 pdfi_pop(ctx, 1);
2984 pdfi_log_info(ctx, "pdfi_setstrokecolor_space", "colour operator in a CharProc, following a d1 ignored\n");
2985 return 0;
2986 }
2987
2988 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_NAME) {
2989 pdfi_pop(ctx, 1);
2990 return_error(gs_error_typecheck);
2991 }
2992 n = ctx->stack_top[-1];
2993 pdfi_countup(n);
2994 pdfi_pop(ctx, 1);
2995
2996 gs_swapcolors_quick(ctx->pgs);
2997 code = pdfi_setcolorspace(ctx, n, stream_dict, page_dict);
2998 gs_swapcolors_quick(ctx->pgs);
2999
3000 pdfi_countdown(n);
3001 return code;
3002 }
3003
3004 int pdfi_setfillcolor_space(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
3005 {
3006 int code;
3007 pdf_obj *n = NULL;
3008
3009 if (pdfi_count_stack(ctx) < 1)
3010 return_error(gs_error_stackunderflow);
3011
3012 if (ctx->text.inside_CharProc && ctx->text.CharProc_d_type != pdf_type3_d0) {
3013 pdfi_pop(ctx, 1);
3014 pdfi_log_info(ctx, "pdfi_setfillcolor_space", "colour operator in a CharProc, following a d1 ignored\n");
3015 return 0;
3016 }
3017
3018 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_NAME) {
3019 pdfi_pop(ctx, 1);
3020 return_error(gs_error_typecheck);
3021 }
3022 n = ctx->stack_top[-1];
3023 pdfi_countup(n);
3024 pdfi_pop(ctx, 1);
3025
3026 code = pdfi_setcolorspace(ctx, n, stream_dict, page_dict);
3027
3028 pdfi_countdown(n);
3029 return code;
3030 }
3031
3032
3033 /*
3034 * Set device outputintent from stream
3035 * see zicc.c/zset_outputintent()
3036 */
3037 static int pdfi_device_setoutputintent(pdf_context *ctx, pdf_dict *profile_dict, stream *stream)
3038 {
3039 int code = 0;
3040 gs_gstate *pgs = ctx->pgs;
3041 gx_device *dev = gs_currentdevice(pgs);
3042 cmm_dev_profile_t *dev_profile;
3043 int64_t N;
3044 int ncomps, dev_comps;
3045 int expected = 0;
3046 cmm_profile_t *picc_profile = NULL;
3047 cmm_profile_t *source_profile = NULL;
3048 gsicc_manager_t *icc_manager = pgs->icc_manager;
3049 gs_color_space_index index;
3050
3051 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] Using OutputIntent\n");
3052
3053 /* get dev_profile and try initing it if fail first time */
3054 code = dev_proc(dev, get_profile)(dev, &dev_profile);
3055 if (code < 0)
3056 return code;
3057
3058 if (dev_profile == NULL) {
3059 code = gsicc_init_device_profile_struct(dev, NULL, 0);
3060 if (code < 0)
3061 return code;
3062 code = dev_proc(dev, get_profile)(dev, &dev_profile);
3063 if (code < 0)
3064 return code;
3065 }
3066 if (dev_profile->oi_profile != NULL) {
3067 return 0; /* Allow only one setting of this object */
3068 }
3069
3070 code = pdfi_dict_get_int(ctx, profile_dict, "N", &N);
3071 if (code < 0)
3072 goto exit;
3073 ncomps = (int)N;
3074 if (ncomps > ICC_MAX_CHANNELS)
3075 return_error(gs_error_rangecheck);
3076
3077 picc_profile = gsicc_profile_new(stream, gs_gstate_memory(pgs), NULL, 0);
3078 if (picc_profile == NULL) {
3079 code = gs_note_error(gs_error_VMerror);
3080 goto exit;
3081 }
3082 picc_profile->num_comps = ncomps;
3083 picc_profile->profile_handle =
3084 gsicc_get_profile_handle_buffer(picc_profile->buffer,
3085 picc_profile->buffer_size,
3086 gs_gstate_memory(pgs));
3087 if (picc_profile->profile_handle == NULL) {
3088 code = gs_note_error(gs_error_VMerror);
3089 goto exit;
3090 }
3091
3092 picc_profile->data_cs =
3093 gscms_get_profile_data_space(picc_profile->profile_handle,
3094 picc_profile->memory);
3095 switch (picc_profile->data_cs) {
3096 case gsCIEXYZ:
3097 case gsCIELAB:
3098 case gsRGB:
3099 expected = 3;
3100 source_profile = icc_manager->default_rgb;
3101 break;
3102 case gsGRAY:
3103 expected = 1;
3104 source_profile = icc_manager->default_gray;
3105 break;
3106 case gsCMYK:
3107 expected = 4;
3108 source_profile = icc_manager->default_cmyk;
3109 break;
3110 case gsNCHANNEL:
3111 expected = 0;
3112 break;
3113 case gsNAMED:
3114 case gsUNDEFINED:
3115 break;
3116 }
3117 if (expected && ncomps != expected) {
3118 code = gs_note_error(gs_error_rangecheck);
3119 goto exit;
3120 }
3121 gsicc_init_hash_cs(picc_profile, pgs);
3122
3123 /* All is well with the profile. Lets set the stuff that needs to be set */
3124 dev_profile->oi_profile = picc_profile;
3125 rc_increment(picc_profile);
3126 picc_profile->name = (char *) gs_alloc_bytes(picc_profile->memory,
3127 MAX_DEFAULT_ICC_LENGTH,
3128 "pdfi_color_setoutputintent");
3129 if (picc_profile->name == NULL) {
3130 code = gs_note_error(gs_error_VMerror);
3131 goto exit;
3132 }
3133 strncpy(picc_profile->name, OI_PROFILE, strlen(OI_PROFILE));
3134 picc_profile->name[strlen(OI_PROFILE)] = 0;
3135 picc_profile->name_length = strlen(OI_PROFILE);
3136 /* Set the range of the profile */
3137 gsicc_set_icc_range(&picc_profile);
3138
3139 /* If the output device has a different number of components, then we are
3140 going to set the output intent as the proofing profile, unless the
3141 proofing profile has already been set.
3142
3143 If the device has the same number of components (and color model) then as
3144 the profile we will use this as the output profile, unless someone has
3145 explicitly set the output profile.
3146
3147 Finally, we will use the output intent profile for the default profile
3148 of the proper Device profile in the icc manager, again, unless someone
3149 has explicitly set this default profile.
3150
3151 All of this is skipped if we are forcing oveprint simulation with
3152 the output intent set, in which case we will push the pdf14 device
3153 to render directly to the the output intent color space and then
3154 do a final transform to the target color space.
3155 */
3156 dev_comps = dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps;
3157 index = gsicc_get_default_type(dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]);
3158
3159 /* If we are doing simulate overprint and the output intent is different than
3160 what the device profile is the we will end up pushing the pdf14 device
3161 and doing a rendering to the output intent color space. Keep the device
3162 profile as is, and do not do a proofing profile */
3163
3164 if (!(ctx->pgs->device->icc_struct->overprint_control == gs_overprint_control_simulate &&
3165 !gsicc_profiles_equal(dev_profile->oi_profile, dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]))) {
3166 if (ncomps == dev_comps && index < gs_color_space_index_DevicePixel) {
3167 /* The OI profile is the same type as the profile for the device and a
3168 "default" profile for the device was not externally set. So we go
3169 ahead and use the OI profile as the device profile. Care needs to be
3170 taken here to keep from screwing up any device parameters. We will
3171 use a keyword of OIProfile for the user/device parameter to indicate
3172 its usage. Also, note conflicts if one is setting object dependent
3173 color management */
3174 dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE] = picc_profile;
3175 rc_increment(picc_profile);
3176 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] OutputIntent used for device profile\n");
3177 } else {
3178 if (dev_profile->proof_profile == NULL) {
3179 /* This means that we should use the OI profile as the proofing
3180 profile. Note that if someone already has specified a
3181 proofing profile it is unclear what they are trying to do
3182 with the output intent. In this case, we will use it
3183 just for the source data below */
3184 dev_profile->proof_profile = picc_profile;
3185 rc_increment(picc_profile);
3186 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] OutputIntent used for proof profile\n");
3187 }
3188 }
3189 }
3190
3191 /* Now the source colors. See which source color space needs to use the
3192 output intent ICC profile */
3193 index = gsicc_get_default_type(source_profile);
3194 if (index < gs_color_space_index_DevicePixel) {
3195 /* source_profile is currently the default. Set it to the OI profile */
3196 switch (picc_profile->data_cs) {
3197 case gsGRAY:
3198 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] OutputIntent used source Gray\n");
3199 icc_manager->default_gray = picc_profile;
3200 rc_increment(picc_profile);
3201 break;
3202 case gsRGB:
3203 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] OutputIntent used source RGB\n");
3204 icc_manager->default_rgb = picc_profile;
3205 rc_increment(picc_profile);
3206 break;
3207 case gsCMYK:
3208 if_debug0m(gs_debug_flag_icc, ctx->memory, "[icc] OutputIntent used source CMYK\n");
3209 icc_manager->default_cmyk = picc_profile;
3210 rc_increment(picc_profile);
3211 break;
3212 default:
3213 break;
3214 }
3215 }
3216
3217 exit:
3218 if (picc_profile != NULL)
3219 rc_decrement(picc_profile, "pdfi_color_setoutputintent");
3220 return code;
3221 }
3222
3223 /*
3224 * intent_dict -- the outputintent dictionary
3225 * profile -- the color profile (a stream)
3226 *
3227 */
3228 int pdfi_color_setoutputintent(pdf_context *ctx, pdf_dict *intent_dict, pdf_stream *profile)
3229 {
3230 pdf_c_stream *profile_stream = NULL;
3231 byte *profile_buffer;
3232 int code, code1;
3233 pdf_dict *profile_dict;
3234
3235 code = pdfi_dict_from_obj(ctx, (pdf_obj *)profile, &profile_dict);
3236 if (code < 0)
3237 return code;
3238
3239 /* The ICC profile reading code (irritatingly) requires a seekable stream, because it
3240 * rewinds it to the start, then seeks to the end to find the size, then rewinds the
3241 * stream again.
3242 * Ideally we would use a ReusableStreamDecode filter here, but that is largely
3243 * implemented in PostScript (!) so we can't use it. What we can do is create a
3244 * string sourced stream in memory, which is at least seekable.
3245 */
3246 code = pdfi_open_memory_stream_from_filtered_stream(ctx, profile, &profile_buffer, &profile_stream, true);
3247 if (code < 0)
3248 goto exit;
3249
3250 /* Create and set the device profile */
3251 code = pdfi_device_setoutputintent(ctx, profile_dict, profile_stream->s);
3252
3253 code1 = pdfi_close_memory_stream(ctx, profile_buffer, profile_stream);
3254
3255 if (code == 0)
3256 code = code1;
3257
3258 exit:
3259 return code;
3260 }
3261
3262 static int Check_Default_Space(pdf_context *ctx, pdf_obj *space, pdf_dict *source_dict, int num_components)
3263 {
3264 pdf_obj *primary = NULL;
3265 pdf_obj *ref_space = NULL;
3266 int code = 0;
3267
3268 if (pdfi_type_of(space) == PDF_NAME)
3269 {
3270 if (pdfi_name_is((const pdf_name *)space, "DeviceGray"))
3271 return (num_components == 1 ? 0 : gs_error_rangecheck);
3272 if (pdfi_name_is((const pdf_name *)space, "DeviceCMYK"))
3273 return (num_components == 4 ? 0 : gs_error_rangecheck);
3274 if (pdfi_name_is((const pdf_name *)space, "DeviceRGB"))
3275 return (num_components == 3 ? 0 : gs_error_rangecheck);
3276
3277 code = pdfi_find_resource(ctx, (unsigned char *)"ColorSpace", (pdf_name *)space, (pdf_dict *)source_dict,
3278 NULL, &ref_space);
3279 if (code < 0)
3280 return code;
3281
3282 if (pdfi_type_of(ref_space) == PDF_NAME) {
3283 if (ref_space->object_num != 0 && ref_space->object_num == space->object_num) {
3284 pdfi_countdown(ref_space);
3285 return_error(gs_error_circular_reference);
3286 }
3287 if (pdfi_name_is((const pdf_name *)ref_space, "DeviceGray")) {
3288 pdfi_countdown(ref_space);
3289 return (num_components == 1 ? 0 : gs_error_rangecheck);
3290 }
3291 if (pdfi_name_is((const pdf_name *)ref_space, "DeviceCMYK")) {
3292 pdfi_countdown(ref_space);
3293 return (num_components == 4 ? 0 : gs_error_rangecheck);
3294 }
3295 if (pdfi_name_is((const pdf_name *)ref_space, "DeviceRGB")) {
3296 pdfi_countdown(ref_space);
3297 return (num_components == 3 ? 0 : gs_error_rangecheck);
3298 }
3299 pdfi_countdown(ref_space);
3300 return_error(gs_error_typecheck);
3301 }
3302 space = ref_space;
3303 }
3304
3305 if (pdfi_type_of(space) == PDF_ARRAY) {
3306 code = pdfi_array_get(ctx, (pdf_array *)space, 0, &primary);
3307 if (code < 0)
3308 goto exit;
3309
3310 if (pdfi_type_of(primary) == PDF_NAME) {
3311 if (pdfi_name_is((pdf_name *)primary, "Lab")) {
3312 code = gs_note_error(gs_error_typecheck);
3313 goto exit;
3314 }
3315 if (pdfi_name_is((pdf_name *)primary, "Pattern")) {
3316 code = gs_note_error(gs_error_typecheck);
3317 goto exit;
3318 }
3319 if (pdfi_name_is((pdf_name *)primary, "Indexed")) {
3320 code = gs_note_error(gs_error_typecheck);
3321 goto exit;
3322 }
3323 }
3324 } else
3325 code = gs_note_error(gs_error_typecheck);
3326
3327 exit:
3328 pdfi_countdown(primary);
3329 pdfi_countdown(ref_space);
3330 return code;
3331 }
3332
3333 int pdfi_setup_DefaultSpaces(pdf_context *ctx, pdf_dict *source_dict)
3334 {
3335 int code = 0;
3336 pdf_dict *resources_dict = NULL, *colorspaces_dict = NULL;
3337 pdf_obj *DefaultSpace = NULL;
3338
3339 if (ctx->args.NOSUBSTDEVICECOLORS)
3340 return 0;
3341
3342 /* Create any required DefaultGray, DefaultRGB or DefaultCMYK
3343 * spaces.
3344 */
3345 code = pdfi_dict_knownget(ctx, source_dict, "Resources", (pdf_obj **)&resources_dict);
3346 if (code > 0) {
3347 code = pdfi_dict_knownget(ctx, resources_dict, "ColorSpace", (pdf_obj **)&colorspaces_dict);
3348 if (code > 0) {
3349 code = pdfi_dict_knownget(ctx, colorspaces_dict, "DefaultGray", &DefaultSpace);
3350 if (code > 0) {
3351 gs_color_space *pcs;
3352
3353 code = Check_Default_Space(ctx, DefaultSpace, source_dict, 1);
3354 if (code >= 0) {
3355 code = pdfi_create_colorspace(ctx, DefaultSpace, NULL, source_dict, &pcs, false);
3356 /* If any given Default* space fails simply ignore it, we wil then use the Device
3357 * space instead, this is as per the spec.
3358 */
3359 if (code >= 0) {
3360 if (gs_color_space_num_components(pcs) == 1) {
3361 ctx->page.DefaultGray_cs = pcs;
3362 pdfi_set_colour_callback(pcs, ctx, NULL);
3363 } else {
3364 rc_decrement(pcs, "setup_DefautSpaces");
3365 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3366 return code;
3367 }
3368 }
3369 } else {
3370 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3371 return code;
3372 }
3373 }
3374 pdfi_countdown(DefaultSpace);
3375 DefaultSpace = NULL;
3376 code = pdfi_dict_knownget(ctx, colorspaces_dict, "DefaultRGB", &DefaultSpace);
3377 if (code > 0) {
3378 gs_color_space *pcs;
3379
3380 code = Check_Default_Space(ctx, DefaultSpace, source_dict, 1);
3381 if (code >= 0) {
3382 code = pdfi_create_colorspace(ctx, DefaultSpace, NULL, source_dict, &pcs, false);
3383 /* If any given Default* space fails simply ignore it, we wil then use the Device
3384 * space instead, this is as per the spec.
3385 */
3386 if (code >= 0) {
3387 if (gs_color_space_num_components(pcs) == 3) {
3388 ctx->page.DefaultRGB_cs = pcs;
3389 pdfi_set_colour_callback(pcs, ctx, NULL);
3390 } else {
3391 rc_decrement(pcs, "setup_DefautSpaces");
3392 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3393 return code;
3394 }
3395 }
3396 } else {
3397 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3398 return code;
3399 }
3400 }
3401 pdfi_countdown(DefaultSpace);
3402 DefaultSpace = NULL;
3403 code = pdfi_dict_knownget(ctx, colorspaces_dict, "DefaultCMYK", &DefaultSpace);
3404 if (code > 0) {
3405 gs_color_space *pcs;
3406
3407 code = Check_Default_Space(ctx, DefaultSpace, source_dict, 1);
3408 if (code >= 0) {
3409 code = pdfi_create_colorspace(ctx, DefaultSpace, NULL, source_dict, &pcs, false);
3410 /* If any given Default* space fails simply ignore it, we wil then use the Device
3411 * space instead, this is as per the spec.
3412 */
3413 if (code >= 0) {
3414 if (gs_color_space_num_components(pcs) == 4) {
3415 ctx->page.DefaultCMYK_cs = pcs;
3416 pdfi_set_colour_callback(pcs, ctx, NULL);
3417 } else {
3418 rc_decrement(pcs, "setup_DefautSpaces");
3419 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3420 return code;
3421 }
3422 }
3423 } else {
3424 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_INVALID_DEFAULTSPACE, "pdfi_setup_DefaultSpaces", NULL)) < 0)
3425 return code;
3426 }
3427 }
3428 pdfi_countdown(DefaultSpace);
3429 DefaultSpace = NULL;
3430 }
3431 }
3432
3433 pdfi_countdown(DefaultSpace);
3434 pdfi_countdown(resources_dict);
3435 pdfi_countdown(colorspaces_dict);
3436 return 0;
3437 }