Debian Codegraph @

pdf/pdf_shading.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 /* Shading operations for the PDF interpreter */
17
18 #include "pdf_int.h"
19 #include "pdf_stack.h"
20 #include "pdf_font_types.h"
21 #include "pdf_gstate.h"
22 #include "pdf_shading.h"
23 #include "pdf_dict.h"
24 #include "pdf_array.h"
25 #include "pdf_func.h"
26 #include "pdf_file.h"
27 #include "pdf_loop_detect.h"
28 #include "pdf_colour.h"
29 #include "pdf_trans.h"
30 #include "pdf_optcontent.h"
31 #include "pdf_doc.h"
32 #include "pdf_misc.h"
33
34 #include "gsfunc3.h" /* for gs_function_Ad0t_params_t */
35 #include "gxshade.h"
36 #include "gsptype2.h"
37 #include "gsfunc0.h" /* For gs_function */
38 #include "gscolor3.h" /* For gs_shfill() */
39 #include "gsstate.h" /* For gs_setoverprintmode */
40 #include "gxdevsop.h" /* For special ops */
41
42 static int pdfi_build_shading_function(pdf_context *ctx, gs_function_t **ppfn, const float *shading_domain, int num_inputs, pdf_dict *shading_dict, pdf_dict *page_dict)
43 {
44 int code;
45 pdf_obj *o = NULL;
46 pdf_obj * rsubfn = NULL;
47 gs_function_AdOt_params_t params;
48
49 memset(&params, 0x00, sizeof(params));
50
51 code = pdfi_loop_detector_mark(ctx);
52 if (code < 0)
53 return code;
54
55 code = pdfi_dict_get(ctx, shading_dict, "Function", &o);
56 if (code < 0)
57 goto build_shading_function_error;
58
59 if (pdfi_type_of(o) != PDF_DICT && pdfi_type_of(o) != PDF_STREAM) {
60 uint size;
61 pdf_obj *rsubfn;
62 gs_function_t **Functions;
63 int64_t i;
64
65 if (pdfi_type_of(o) != PDF_ARRAY) {
66 code = gs_error_typecheck;
67 goto build_shading_function_error;
68 }
69 size = pdfi_array_size(((pdf_array *)o));
70
71 if (size == 0) {
72 code = gs_error_rangecheck;
73 goto build_shading_function_error;
74 }
75 code = alloc_function_array(size, &Functions, ctx->memory);
76 if (code < 0)
77 goto build_shading_function_error;
78
79 for (i = 0; i < size; ++i) {
80 code = pdfi_array_get(ctx, (pdf_array *)o, i, &rsubfn);
81 if (code == 0) {
82 if (pdfi_type_of(rsubfn) != PDF_DICT && pdfi_type_of(rsubfn) != PDF_STREAM)
83 code = gs_note_error(gs_error_typecheck);
84 }
85 if (code < 0) {
86 int j;
87
88 for (j = 0;j < i; j++) {
89 pdfi_free_function(ctx, Functions[j]);
90 Functions[j] = NULL;
91 }
92 gs_free_object(ctx->memory, Functions, "function array error, freeing functions");
93 goto build_shading_function_error;
94 }
95 code = pdfi_build_function(ctx, &Functions[i], shading_domain, num_inputs, rsubfn, page_dict);
96 if (code < 0)
97 goto build_shading_function_error;
98 pdfi_countdown(rsubfn);
99 rsubfn = NULL;
100 }
101 params.m = num_inputs;
102 params.Domain = 0;
103 params.n = size;
104 params.Range = 0;
105 params.Functions = (const gs_function_t * const *)Functions;
106 code = gs_function_AdOt_init(ppfn, &params, ctx->memory);
107 if (code < 0)
108 goto build_shading_function_error;
109 } else {
110 code = pdfi_build_function(ctx, ppfn, shading_domain, num_inputs, o, page_dict);
111 if (code < 0)
112 goto build_shading_function_error;
113 }
114
115 (void)pdfi_loop_detector_cleartomark(ctx);
116 pdfi_countdown(o);
117 return code;
118
119 build_shading_function_error:
120 gs_function_AdOt_free_params(&params, ctx->memory);
121 pdfi_countdown(rsubfn);
122 pdfi_countdown(o);
123 (void)pdfi_loop_detector_cleartomark(ctx);
124 return code;
125 }
126
127 static int pdfi_shading1(pdf_context *ctx, gs_shading_params_t *pcommon,
128 gs_shading_t **ppsh,
129 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
130 {
131 pdf_obj *o = NULL;
132 int code, i;
133 gs_shading_Fb_params_t params;
134 static const float default_Domain[4] = {0, 1, 0, 1};
135 pdf_dict *shading_dict;
136
137 if (pdfi_type_of(Shading) != PDF_DICT)
138 return_error(gs_error_typecheck);
139 shading_dict = (pdf_dict *)Shading;
140
141 memset(&params, 0, sizeof(params));
142 *(gs_shading_params_t *)&params = *pcommon;
143 gs_make_identity(&params.Matrix);
144 params.Function = 0;
145
146 code = fill_domain_from_dict(ctx, (float *)&params.Domain, 4, shading_dict);
147 if (code < 0) {
148 if (code == gs_error_undefined) {
149 for (i = 0; i < 4; i++) {
150 params.Domain[i] = default_Domain[i];
151 }
152 } else
153 return code;
154 }
155
156 code = fill_matrix_from_dict(ctx, (float *)&params.Matrix, shading_dict);
157 if (code < 0 && code != gs_error_undefined)
158 return code;
159
160 code = pdfi_build_shading_function(ctx, &params.Function, (const float *)&params.Domain, 2, (pdf_dict *)shading_dict, page_dict);
161 if (code < 0){
162 pdfi_countdown(o);
163 return code;
164 }
165 code = gs_shading_Fb_init(ppsh, &params, ctx->memory);
166 if (code < 0) {
167 gs_function_free(params.Function, true, ctx->memory);
168 params.Function = NULL;
169 pdfi_countdown(o);
170 }
171 return code;
172 }
173
174 static int pdfi_shading2(pdf_context *ctx, gs_shading_params_t *pcommon,
175 gs_shading_t **ppsh,
176 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
177 {
178 pdf_obj *o = NULL;
179 gs_shading_A_params_t params;
180 static const float default_Domain[2] = {0, 1};
181 int code, i;
182 pdf_dict *shading_dict;
183
184 if (pdfi_type_of(Shading) != PDF_DICT)
185 return_error(gs_error_typecheck);
186 shading_dict = (pdf_dict *)Shading;
187
188 memset(&params, 0, sizeof(params));
189 *(gs_shading_params_t *)&params = *pcommon;
190
191 code = fill_float_array_from_dict(ctx, (float *)&params.Coords, 4, shading_dict, "Coords");
192 if (code < 0)
193 return code;
194 code = fill_domain_from_dict(ctx, (float *)&params.Domain, 2, shading_dict);
195 if (code < 0) {
196 if (code == gs_error_undefined) {
197 for (i = 0; i < 2; i++) {
198 params.Domain[i] = default_Domain[i];
199 }
200 } else
201 return code;
202 }
203
204 code = fill_bool_array_from_dict(ctx, (bool *)&params.Extend, 2, shading_dict, "Extend");
205 if (code < 0) {
206 if (code == gs_error_undefined) {
207 params.Extend[0] = params.Extend[1] = false;
208 } else
209 return code;
210 }
211
212 code = pdfi_build_shading_function(ctx, &params.Function, (const float *)&params.Domain, 1, (pdf_dict *)shading_dict, page_dict);
213 if (code < 0){
214 pdfi_countdown(o);
215 return code;
216 }
217 code = gs_shading_A_init(ppsh, &params, ctx->memory);
218 if (code < 0){
219 gs_function_free(params.Function, true, ctx->memory);
220 params.Function = NULL;
221 pdfi_countdown(o);
222 return code;
223 }
224
225 return 0;
226 }
227
228 static int pdfi_shading3(pdf_context *ctx, gs_shading_params_t *pcommon,
229 gs_shading_t **ppsh,
230 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
231 {
232 pdf_obj *o = NULL;
233 gs_shading_R_params_t params;
234 static const float default_Domain[2] = {0, 1};
235 int code, i;
236 pdf_dict *shading_dict;
237
238 if (pdfi_type_of(Shading) != PDF_DICT)
239 return_error(gs_error_typecheck);
240 shading_dict = (pdf_dict *)Shading;
241
242 memset(&params, 0, sizeof(params));
243 *(gs_shading_params_t *)&params = *pcommon;
244
245 code = fill_float_array_from_dict(ctx, (float *)&params.Coords, 6, shading_dict, "Coords");
246 if (code < 0)
247 return code;
248 code = fill_domain_from_dict(ctx, (float *)&params.Domain, 4, shading_dict);
249 if (code < 0) {
250 if (code == gs_error_undefined) {
251 for (i = 0; i < 2; i++) {
252 params.Domain[i] = default_Domain[i];
253 }
254 } else
255 return code;
256 }
257
258 code = fill_bool_array_from_dict(ctx, (bool *)&params.Extend, 2, shading_dict, "Extend");
259 if (code < 0) {
260 if (code == gs_error_undefined) {
261 params.Extend[0] = params.Extend[1] = false;
262 } else
263 return code;
264 }
265
266 code = pdfi_build_shading_function(ctx, &params.Function, (const float *)&params.Domain, 1, (pdf_dict *)shading_dict, page_dict);
267 if (code < 0){
268 pdfi_countdown(o);
269 return code;
270 }
271 code = gs_shading_R_init(ppsh, &params, ctx->memory);
272 if (code < 0){
273 gs_function_free(params.Function, true, ctx->memory);
274 params.Function = NULL;
275 pdfi_countdown(o);
276 return code;
277 }
278
279 return 0;
280 }
281
282 static int pdfi_build_mesh_shading(pdf_context *ctx, gs_shading_mesh_params_t *params,
283 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
284 {
285 int num_decode = 4, code;
286 byte *data_source_buffer = NULL;
287 pdf_c_stream *shading_stream = NULL;
288 int64_t i;
289 pdf_dict *shading_dict;
290
291 if (pdfi_type_of(Shading) != PDF_STREAM)
292 return_error(gs_error_typecheck);
293
294 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
295 if (code < 0)
296 return code;
297
298 params->Function = NULL;
299 params->Decode = NULL;
300
301 code = pdfi_open_memory_stream_from_filtered_stream(ctx, (pdf_stream *)Shading, &data_source_buffer, &shading_stream, false);
302 if (code < 0) {
303 return code;
304 }
305
306 data_source_init_stream(&params->DataSource, shading_stream->s);
307
308 /* We need to clear up the PDF stream, but leave the underlying stream alone, that's now
309 * pointed to by the params.DataSource member.
310 */
311 gs_free_object(ctx->memory, shading_stream, "discard memory stream(pdf_stream)");
312
313 code = pdfi_build_shading_function(ctx, &params->Function, (const float *)NULL, 1,
314 shading_dict, page_dict);
315 if (code < 0 && code != gs_error_undefined)
316 goto build_mesh_shading_error;
317
318 code = pdfi_dict_get_int(ctx, shading_dict, "BitsPerCoordinate", &i);
319 if (code < 0)
320 goto build_mesh_shading_error;
321
322 if (i != 1 && i != 2 && i != 4 && i != 8 && i != 12 && i != 16 && i != 24 && i != 32) {
323 code = gs_error_rangecheck;
324 goto build_mesh_shading_error;
325 }
326
327 params->BitsPerCoordinate = i;
328
329 code = pdfi_dict_get_int(ctx, shading_dict, "BitsPerComponent", &i);
330 if (code < 0)
331 goto build_mesh_shading_error;
332
333 if (i != 1 && i != 2 && i != 4 && i != 8 && i != 12 && i != 16) {
334 code = gs_error_rangecheck;
335 goto build_mesh_shading_error;
336 }
337
338 params->BitsPerComponent = i;
339
340 if (params->Function != NULL)
341 num_decode += 2;
342 else
343 num_decode += gs_color_space_num_components(params->ColorSpace) * 2;
344
345 params->Decode = (float *) gs_alloc_byte_array(ctx->memory, num_decode, sizeof(float),
346 "build_mesh_shading");
347 if (params->Decode == NULL) {
348 code = gs_error_VMerror;
349 goto build_mesh_shading_error;
350 }
351
352 code = fill_float_array_from_dict(ctx, (float *)params->Decode, num_decode, shading_dict, "Decode");
353 if (code < 0)
354 goto build_mesh_shading_error;
355
356 return 0;
357
358 build_mesh_shading_error:
359 if (params->Function) {
360 pdfi_free_function(ctx, params->Function);
361 params->Function = NULL;
362 }
363 if (params->DataSource.data.strm != NULL) {
364 s_close_filters(&params->DataSource.data.strm, params->DataSource.data.strm->strm);
365 /* s_close_filters() sets the pointer to NULL so we don't need to */
366 }
367 gs_free_object(ctx->memory, params->Decode, "Decode");
368 params->Decode = NULL;
369 return code;
370 }
371
372 static int pdfi_shading4(pdf_context *ctx, gs_shading_params_t *pcommon,
373 gs_shading_t **ppsh,
374 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
375 {
376 gs_shading_FfGt_params_t params;
377 int code;
378 int64_t i;
379 pdf_dict *shading_dict;
380
381 memset(&params, 0, sizeof(params));
382 *(gs_shading_params_t *)&params = *pcommon;
383
384 code = pdfi_build_mesh_shading(ctx, (gs_shading_mesh_params_t *)&params, Shading, stream_dict, page_dict);
385 if (code < 0)
386 goto error;
387
388 /* pdfi_build_mesh_shading checks the type of the Shading object, so we don't need to here */
389 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
390 if (code < 0)
391 goto error;
392
393 code = pdfi_dict_get_int(ctx, shading_dict, "BitsPerFlag", &i);
394 if (code < 0)
395 goto error;
396
397 if (i != 2 && i != 4 && i != 8) {
398 code = gs_note_error(gs_error_rangecheck);
399 goto error;
400 }
401
402 params.BitsPerFlag = i;
403
404 code = gs_shading_FfGt_init(ppsh, &params, ctx->memory);
405 if (code < 0)
406 goto error;
407 return 0;
408
409 error:
410 if (params.Function) {
411 pdfi_free_function(ctx, params.Function);
412 params.Function = NULL;
413 }
414 if (params.DataSource.data.strm != NULL) {
415 s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
416 /* s_close_filters() sets the pointer to NULL so we don't need to */
417 }
418 gs_free_object(ctx->memory, params.Decode, "Decode");
419 params.Decode = NULL;
420 return code;
421 }
422
423 static int pdfi_shading5(pdf_context *ctx, gs_shading_params_t *pcommon,
424 gs_shading_t **ppsh,
425 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
426 {
427 gs_shading_LfGt_params_t params;
428 int code;
429 int64_t i;
430 pdf_dict *shading_dict;
431
432 memset(&params, 0, sizeof(params));
433 *(gs_shading_params_t *)&params = *pcommon;
434
435 code = pdfi_build_mesh_shading(ctx, (gs_shading_mesh_params_t *)&params, Shading, stream_dict, page_dict);
436 if (code < 0)
437 goto error;
438
439 /* pdfi_build_mesh_shading checks the type of the Shading object, so we don't need to here */
440 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
441 if (code < 0)
442 goto error;
443
444 code = pdfi_dict_get_int(ctx, shading_dict, "VerticesPerRow", &i);
445 if (code < 0)
446 goto error;
447
448 if (i < 2) {
449 code = gs_note_error(gs_error_rangecheck);
450 goto error;
451 }
452
453 params.VerticesPerRow = i;
454
455 code = gs_shading_LfGt_init(ppsh, &params, ctx->memory);
456 if (code < 0)
457 goto error;
458
459 return 0;
460
461 error:
462 if (params.Function) {
463 pdfi_free_function(ctx, params.Function);
464 params.Function = NULL;
465 }
466 if (params.DataSource.data.strm != NULL) {
467 s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
468 /* s_close_filters() sets the pointer to NULL so we don't need to */
469 }
470 gs_free_object(ctx->memory, params.Decode, "Decode");
471 params.Decode = NULL;
472 return code;
473 }
474
475 static int pdfi_shading6(pdf_context *ctx, gs_shading_params_t *pcommon,
476 gs_shading_t **ppsh,
477 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
478 {
479 gs_shading_Cp_params_t params;
480 int code;
481 int64_t i;
482 pdf_dict *shading_dict;
483
484 memset(&params, 0, sizeof(params));
485 *(gs_shading_params_t *)&params = *pcommon;
486
487 code = pdfi_build_mesh_shading(ctx, (gs_shading_mesh_params_t *)&params, Shading, stream_dict, page_dict);
488 if (code < 0)
489 goto error;
490
491 /* pdfi_build_mesh_shading checks the type of the Shading object, so we don't need to here */
492 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
493 if (code < 0)
494 goto error;
495
496 code = pdfi_dict_get_int(ctx, shading_dict, "BitsPerFlag", &i);
497 if (code < 0)
498 goto error;
499
500 if (i != 2 && i != 4 && i != 8) {
501 code = gs_note_error(gs_error_rangecheck);
502 goto error;
503 }
504
505 params.BitsPerFlag = i;
506
507 code = gs_shading_Cp_init(ppsh, &params, ctx->memory);
508 if (code < 0)
509 goto error;
510 return 0;
511
512 error:
513 if (params.Function) {
514 pdfi_free_function(ctx, params.Function);
515 params.Function = NULL;
516 }
517 if (params.DataSource.data.strm != NULL) {
518 s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
519 /* s_close_filters() sets the pointer to NULL so we don't need to */
520 }
521 gs_free_object(ctx->memory, params.Decode, "Decode");
522 params.Decode = NULL;
523 return code;
524 }
525
526 static int pdfi_shading7(pdf_context *ctx, gs_shading_params_t *pcommon,
527 gs_shading_t **ppsh,
528 pdf_obj *Shading, pdf_dict *stream_dict, pdf_dict *page_dict)
529 {
530 gs_shading_Tpp_params_t params;
531 int code;
532 int64_t i;
533 pdf_dict *shading_dict;
534
535 memset(&params, 0, sizeof(params));
536 *(gs_shading_params_t *)&params = *pcommon;
537
538 code = pdfi_build_mesh_shading(ctx, (gs_shading_mesh_params_t *)&params, Shading, stream_dict, page_dict);
539 if (code < 0)
540 goto error;
541
542 /* pdfi_build_mesh_shading checks the type of the Shading object, so we don't need to here */
543 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
544 if (code < 0)
545 return code;
546
547 code = pdfi_dict_get_int(ctx, shading_dict, "BitsPerFlag", &i);
548 if (code < 0)
549 goto error;
550
551 if (i != 2 && i != 4 && i != 8) {
552 code = gs_note_error(gs_error_rangecheck);
553 goto error;
554 }
555
556 params.BitsPerFlag = i;
557
558 code = gs_shading_Tpp_init(ppsh, &params, ctx->memory);
559 if (code < 0)
560 goto error;
561 return 0;
562
563 error:
564 if (params.Function) {
565 pdfi_free_function(ctx, params.Function);
566 params.Function = NULL;
567 }
568 if (params.DataSource.data.strm != NULL) {
569 s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
570 /* s_close_filters() sets the pointer to NULL so we don't need to */
571 }
572 gs_free_object(ctx->memory, params.Decode, "Decode");
573 params.Decode = NULL;
574 return code;
575 }
576
577 static int get_shading_common(pdf_context *ctx, pdf_dict *shading_dict, gs_shading_params_t *params)
578 {
579 gs_color_space *pcs = gs_currentcolorspace(ctx->pgs);
580 int code, num_comp = gs_color_space_num_components(pcs);
581 pdf_array *a = NULL;
582 double *temp;
583
584 if (num_comp < 0) /* Pattern color space */
585 return_error(gs_error_typecheck);
586
587 params->ColorSpace = pcs;
588 params->Background = NULL;
589 rc_increment_cs(pcs);
590
591 code = pdfi_dict_get_type(ctx, shading_dict, "Background", PDF_ARRAY, (pdf_obj **)&a);
592 if (code < 0 && code != gs_error_undefined)
593 return code;
594
595 if (code >= 0) {
596 uint64_t i;
597 gs_client_color *pcc = NULL;
598
599 if (pdfi_array_size(a) < num_comp) {
600 code = gs_error_rangecheck;
601 goto get_shading_common_error;
602 }
603
604 pcc = gs_alloc_struct(ctx->memory, gs_client_color, &st_client_color, "get_shading_common");
605 if (pcc == 0) {
606 code = gs_error_VMerror;
607 goto get_shading_common_error;
608 }
609
610 pcc->pattern = 0;
611 params->Background = pcc;
612
613 temp = (double *)gs_alloc_bytes(ctx->memory, (size_t)num_comp * sizeof(double), "temporary array of doubles");
614 if (temp == NULL) {
615 code = gs_error_VMerror;
616 goto get_shading_common_error;
617 }
618 for(i=0;i<num_comp;i++) {
619 code = pdfi_array_get_number(ctx, a, i, &temp[i]);
620 if (code < 0) {
621 gs_free_object(ctx->memory, temp, "free workign array (error)");
622 goto get_shading_common_error;
623 }
624 pcc->paint.values[i] = temp[i];
625 }
626 pdfi_countdown((pdf_obj *)a);
627 a = NULL;
628 gs_free_object(ctx->memory, temp, "free workign array (done)");
629 }
630
631
632 code = pdfi_dict_get_type(ctx, shading_dict, "BBox", PDF_ARRAY, (pdf_obj **)&a);
633 if (code < 0 && code != gs_error_undefined)
634 goto get_shading_common_error;
635
636 if (code >= 0) {
637 double box[4];
638 uint64_t i;
639
640 if (pdfi_array_size(a) < 4) {
641 code = gs_error_rangecheck;
642 goto get_shading_common_error;
643 }
644
645 for(i=0;i<4;i++) {
646 code = pdfi_array_get_number(ctx, a, i, &box[i]);
647 if (code < 0)
648 goto get_shading_common_error;
649 }
650 /* Adobe Interpreters accept denormalised BBox - bug 688937 */
651 if (box[0] <= box[2]) {
652 params->BBox.p.x = box[0];
653 params->BBox.q.x = box[2];
654 } else {
655 params->BBox.p.x = box[2];
656 params->BBox.q.x = box[0];
657 }
658 if (box[1] <= box[3]) {
659 params->BBox.p.y = box[1];
660 params->BBox.q.y = box[3];
661 } else {
662 params->BBox.p.y = box[3];
663 params->BBox.q.y = box[1];
664 }
665 params->have_BBox = true;
666 } else {
667 params->have_BBox = false;
668 }
669 pdfi_countdown(a);
670 a = NULL;
671
672 code = pdfi_dict_get_bool(ctx, shading_dict, "AntiAlias", &params->AntiAlias);
673 if (code < 0 && code != gs_error_undefined)
674 goto get_shading_common_error;
675
676 return 0;
677 get_shading_common_error:
678 pdfi_countdown((pdf_obj *)a);
679 gs_free_object(ctx->memory, params->Background, "Background (common_shading_error)");
680 params->Background = NULL;
681 return code;
682 }
683
684 /* Build gs_shading_t object from a Shading Dict */
685 int
686 pdfi_shading_build(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict,
687 pdf_obj *Shading, gs_shading_t **ppsh)
688 {
689 gs_shading_params_t params;
690 gs_shading_t *psh = NULL;
691 pdf_obj *cspace = NULL;
692 int64_t type = 0;
693 int code = 0;
694 pdf_dict *sdict = NULL;
695
696 memset(&params, 0, sizeof(params));
697
698 params.ColorSpace = 0;
699 params.cie_joint_caches = 0;
700 params.Background = 0;
701 params.have_BBox = 0;
702 params.AntiAlias = 0;
703
704 code = pdfi_dict_from_obj(ctx, Shading, &sdict);
705 if (code < 0)
706 return code;
707
708 code = pdfi_dict_get(ctx, sdict, "ColorSpace", &cspace);
709 if (code < 0)
710 goto shading_error;
711
712 code = pdfi_setcolorspace(ctx, cspace, stream_dict, page_dict);
713 if (code < 0)
714 goto shading_error;
715
716 code = get_shading_common(ctx, sdict, &params);
717 if (code < 0)
718 goto shading_error;
719
720 code = pdfi_dict_get_int(ctx, sdict, "ShadingType", &type);
721 if (code < 0)
722 goto shading_error;
723
724 switch(type){
725 case 1:
726 code = pdfi_shading1(ctx, &params, &psh, Shading, stream_dict, page_dict);
727 break;
728 case 2:
729 code = pdfi_shading2(ctx, &params, &psh, Shading, stream_dict, page_dict);
730 break;
731 case 3:
732 code = pdfi_shading3(ctx, &params, &psh, Shading, stream_dict, page_dict);
733 break;
734 case 4:
735 code = pdfi_shading4(ctx, &params, &psh, Shading, stream_dict, page_dict);
736 break;
737 case 5:
738 code = pdfi_shading5(ctx, &params, &psh, Shading, stream_dict, page_dict);
739 break;
740 case 6:
741 code = pdfi_shading6(ctx, &params, &psh, Shading, stream_dict, page_dict);
742 break;
743 case 7:
744 code = pdfi_shading7(ctx, &params, &psh, Shading, stream_dict, page_dict);
745 break;
746 default:
747 code = gs_note_error(gs_error_rangecheck);
748 break;
749 }
750 if (code < 0)
751 goto shading_error;
752
753 pdfi_countdown(cspace);
754 *ppsh = psh;
755 return code;
756
757 shading_error:
758 if (cspace != NULL)
759 pdfi_countdown(cspace);
760 if (params.ColorSpace != NULL) {
761 rc_decrement_only(params.ColorSpace, "ColorSpace (shading_build_error)");
762 params.ColorSpace = NULL;
763 }
764 if (params.Background != NULL) {
765 gs_free_object(ctx->memory, params.Background, "Background (shading_build_error)");
766 params.Background = NULL;
767 }
768 return code;
769 }
770
771 /* Free stuff associated with a gs_shading_t.
772 */
773 void
774 pdfi_shading_free(pdf_context *ctx, gs_shading_t *psh)
775 {
776 gs_shading_params_t *params = &psh->params;
777
778 rc_decrement_cs(params->ColorSpace, "pdfi_shading_free(ColorSpace)");
779 params->ColorSpace = NULL;
780
781 if (params->Background != NULL) {
782 gs_free_object(ctx->memory, params->Background, "pdfi_shading_free(Background)");
783 params->Background = NULL;
784 }
785
786 if (psh->head.type > 3) {
787 gs_shading_mesh_params_t *mesh_params = (gs_shading_mesh_params_t *)params;
788
789 if (mesh_params->Decode != NULL) {
790 gs_free_object(ctx->memory, mesh_params->Decode, "release mesh shading Decode array");
791 mesh_params->Decode = NULL;
792 }
793 if (mesh_params->DataSource.data.strm != NULL) {
794 s_close_filters(&mesh_params->DataSource.data.strm, mesh_params->DataSource.data.strm->strm);
795 /* s_close_filters() sets the pointer to NULL so we don't need to */
796 }
797 }
798
799 switch(psh->head.type) {
800 case 1:
801 if (((gs_shading_Fb_params_t *)&psh->params)->Function != NULL)
802 pdfi_free_function(ctx, ((gs_shading_Fb_params_t *)&psh->params)->Function);
803 break;
804 case 2:
805 if (((gs_shading_A_params_t *)&psh->params)->Function != NULL)
806 pdfi_free_function(ctx, ((gs_shading_A_params_t *)&psh->params)->Function);
807 break;
808 case 3:
809 if (((gs_shading_R_params_t *)&psh->params)->Function != NULL)
810 pdfi_free_function(ctx, ((gs_shading_R_params_t *)&psh->params)->Function);
811 break;
812 case 4:
813 if (((gs_shading_FfGt_params_t *)&psh->params)->Function != NULL)
814 pdfi_free_function(ctx, ((gs_shading_FfGt_params_t *)&psh->params)->Function);
815 break;
816 case 5:
817 if (((gs_shading_LfGt_params_t *)&psh->params)->Function != NULL)
818 pdfi_free_function(ctx, ((gs_shading_LfGt_params_t *)&psh->params)->Function);
819 break;
820 case 6:
821 if (((gs_shading_Cp_params_t *)&psh->params)->Function != NULL)
822 pdfi_free_function(ctx, ((gs_shading_Cp_params_t *)&psh->params)->Function);
823 break;
824 case 7:
825 if (((gs_shading_Tpp_params_t *)&psh->params)->Function != NULL)
826 pdfi_free_function(ctx, ((gs_shading_Tpp_params_t *)&psh->params)->Function);
827 break;
828 default:
829 break;
830 }
831 gs_free_object(ctx->memory, psh, "Free shading, finished");
832 }
833
834 /* Setup for transparency (see pdf_draw.ps/sh) */
835 static int
836 pdfi_shading_setup_trans(pdf_context *ctx, pdfi_trans_state_t *state, pdf_obj *Shading)
837 {
838 int code;
839 gs_rect bbox, *box = NULL;
840 pdf_array *BBox = NULL;
841 pdf_dict *shading_dict;
842
843 code = pdfi_dict_from_obj(ctx, Shading, &shading_dict);
844 if (code < 0)
845 return code;
846
847 code = pdfi_dict_knownget_type(ctx, shading_dict, "BBox", PDF_ARRAY, (pdf_obj **)&BBox);
848 if (code < 0)
849 goto exit;
850
851 if (code > 0) {
852 code = pdfi_array_to_gs_rect(ctx, BBox, &bbox);
853 if (code >= 0)
854 box = &bbox;
855 }
856
857 /* If we didn't get a BBox for the shading, then we need to create one, in order to
858 * pass it to the transparency setup, which (potentially, at least, uses it to set
859 * up a transparency group.
860 * In the absence of anything better, we take the current clip, turn that into a path
861 * and then get the bounding box of that path. Obviously we don't want to disturb the
862 * current path in the graphics state, so we do a gsave/grestore round it.
863 */
864 if (box == NULL) {
865 code = pdfi_gsave(ctx);
866 if (code < 0)
867 goto exit;
868
869 code = gs_newpath(ctx->pgs);
870 if (code < 0)
871 goto bbox_error;
872
873 code = gs_clippath(ctx->pgs);
874 if (code < 0)
875 goto bbox_error;
876
877 code = pdfi_get_current_bbox(ctx, &bbox, false);
878
879 bbox_error:
880 pdfi_grestore(ctx);
881
882 if (code < 0)
883 goto exit;
884
885 box = &bbox;
886 }
887 code = pdfi_trans_setup(ctx, state, box, TRANSPARENCY_Caller_Other);
888
889 exit:
890 pdfi_countdown(BBox);
891 return code;
892 }
893
894 int pdfi_shading(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
895 {
896 int code, code1;
897 pdf_name *n = NULL;
898 pdf_obj *Shading = NULL;
899 gs_shading_t *psh = NULL;
900 gs_offset_t savedoffset;
901 pdfi_trans_state_t trans_state;
902 int trans_required;
903
904 if (pdfi_count_stack(ctx) < 1)
905 return_error(gs_error_stackunderflow);
906
907 if (ctx->text.BlockDepth != 0) {
908 ctx->text.BlockDepth = 0;
909 if (ctx->text.TextClip) {
910 gx_device *dev = gs_currentdevice_inline(ctx->pgs);
911
912 ctx->text.TextClip = false;
913 (void)dev_proc(dev, dev_spec_op)(dev, gxdso_hilevel_text_clip, (void *)0, 1);
914 }
915 code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_OPINVALIDINTEXT, "pdfi_shading", NULL);
916 if (code < 0)
917 return code;
918 }
919
920 if (pdfi_oc_is_off(ctx)) {
921 pdfi_pop(ctx, 1);
922 return 0;
923 }
924
925 savedoffset = pdfi_tell(ctx->main_stream);
926
927 n = (pdf_name *)ctx->stack_top[-1];
928 pdfi_countup(n);
929 pdfi_pop(ctx, 1);
930
931 if (pdfi_type_of(n) != PDF_NAME) {
932 pdfi_countdown(n);
933 return gs_note_error(gs_error_typecheck);
934 }
935
936 code = pdfi_loop_detector_mark(ctx);
937 if (code < 0) {
938 pdfi_countdown(n);
939 return code;
940 }
941
942 code = pdfi_op_q(ctx);
943 if (code < 0)
944 goto exit1;
945
946 code = pdfi_find_resource(ctx, (unsigned char *)"Shading", n, (pdf_dict *)stream_dict, page_dict,
947 &Shading);
948 if (code < 0)
949 goto exit2;
950
951 if (pdfi_type_of(Shading) != PDF_DICT && pdfi_type_of(Shading) != PDF_STREAM) {
952 code = gs_note_error(gs_error_typecheck);
953 goto exit2;
954 }
955
956 code = pdfi_trans_set_params(ctx);
957 if (code < 0)
958 goto exit2;
959
960 /* Shadings fills can't use overprint mode */
961 code = gs_setoverprintmode(ctx->pgs, 0);
962 if (code < 0)
963 goto exit2;
964
965 code = pdfi_shading_build(ctx, stream_dict, page_dict, Shading, &psh);
966 if (code < 0)
967 goto exit2;
968
969 trans_required = pdfi_trans_required(ctx);
970
971 if (trans_required) {
972 code = pdfi_shading_setup_trans(ctx, &trans_state, Shading);
973 if (code < 0)
974 goto exit2;
975 }
976
977 code = gs_shfill(ctx->pgs, psh);
978 if (code < 0) {
979 pdfi_set_warning(ctx, 0, NULL, W_PDF_BADSHADING, "pdfi_shading", (char *)"ERROR: ignoring invalid smooth shading object, output may be incorrect");
980 code = 0;
981 }
982
983 if (trans_required) {
984 code1 = pdfi_trans_teardown(ctx, &trans_state);
985 if (code == 0)
986 code = code1;
987 }
988
989 exit2:
990 if (psh)
991 pdfi_shading_free(ctx, psh);
992
993 pdfi_countdown(Shading);
994 code1 = pdfi_op_Q(ctx);
995 if (code == 0)
996 code = code1;
997 exit1:
998 pdfi_countdown(n);
999 (void)pdfi_loop_detector_cleartomark(ctx);
1000 pdfi_seek(ctx, ctx->main_stream, savedoffset, SEEK_SET);
1001 return code;
1002 }