Coverage Report

Created: 2025-06-10 07:27

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