Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/gxpcmap.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-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
17
/* Pattern color mapping for Ghostscript library */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gp.h"
22
#include "gserrors.h"
23
#include "gsstruct.h"
24
#include "gsutil.h"             /* for gs_next_ids */
25
#include "gxfixed.h"
26
#include "gxmatrix.h"
27
#include "gspath2.h"
28
#include "gxcspace.h"           /* for gscolor2.h */
29
#include "gxcolor2.h"
30
#include "gxdcolor.h"
31
#include "gxdevice.h"
32
#include "gxdevmem.h"
33
#include "gxpcolor.h"
34
#include "gxp1impl.h"
35
#include "gxclist.h"
36
#include "gxcldev.h"
37
#include "gzstate.h"
38
#include "gxdevsop.h"
39
#include "gdevmpla.h"
40
#include "gdevp14.h"
41
#include "gxgetbit.h"
42
#include "gscoord.h"
43
#include "gsicc_blacktext.h"
44
#include "gscspace.h"
45
46
#if RAW_PATTERN_DUMP
47
unsigned int global_pat_index = 0;
48
#endif
49
50
/* Define the default size of the Pattern cache. */
51
234k
#define max_cached_patterns_LARGE 50
52
234k
#define max_pattern_bits_LARGE 100000
53
#define max_cached_patterns_SMALL 5
54
#define max_pattern_bits_SMALL 1000
55
uint
56
gx_pat_cache_default_tiles(void)
57
234k
{
58
#if ARCH_SMALL_MEMORY
59
    return max_cached_patterns_SMALL;
60
#else
61
#ifdef DEBUG
62
    return (gs_debug_c('.') ? max_cached_patterns_SMALL :
63
            max_cached_patterns_LARGE);
64
#else
65
234k
    return max_cached_patterns_LARGE;
66
234k
#endif
67
234k
#endif
68
234k
}
69
ulong
70
gx_pat_cache_default_bits(void)
71
234k
{
72
#if ARCH_SMALL_MEMORY
73
    return max_pattern_bits_SMALL;
74
#else
75
#ifdef DEBUG
76
    return (gs_debug_c('.') ? max_pattern_bits_SMALL :
77
            max_pattern_bits_LARGE);
78
#else
79
234k
    return max_pattern_bits_LARGE;
80
234k
#endif
81
234k
#endif
82
234k
}
83
84
/* Define the structures for Pattern rendering and caching. */
85
private_st_color_tile();
86
private_st_color_tile_element();
87
private_st_pattern_cache();
88
private_st_device_pattern_accum();
89
private_st_pattern_trans();
90
91
/* ------ Pattern rendering ------ */
92
93
/* Device procedures */
94
static dev_proc_open_device(pattern_accum_open);
95
static dev_proc_close_device(pattern_accum_close);
96
static dev_proc_fill_rectangle(pattern_accum_fill_rectangle);
97
static dev_proc_copy_mono(pattern_accum_copy_mono);
98
static dev_proc_copy_color(pattern_accum_copy_color);
99
static dev_proc_copy_planes(pattern_accum_copy_planes);
100
static dev_proc_get_bits_rectangle(pattern_accum_get_bits_rectangle);
101
static dev_proc_fill_rectangle_hl_color(pattern_accum_fill_rectangle_hl_color);
102
/* not static for use by clist_dev_spec_op with pattern-clist */
103
dev_proc_dev_spec_op(pattern_accum_dev_spec_op);
104
105
/* The device descriptor */
106
static void
107
pattern_accum_initialize_device_procs(gx_device *dev)
108
23.2k
{
109
23.2k
    set_dev_proc(dev, open_device, pattern_accum_open);
110
23.2k
    set_dev_proc(dev, close_device, pattern_accum_close);
111
23.2k
    set_dev_proc(dev, fill_rectangle, pattern_accum_fill_rectangle);
112
23.2k
    set_dev_proc(dev, copy_mono, pattern_accum_copy_mono);
113
23.2k
    set_dev_proc(dev, copy_color, pattern_accum_copy_color);
114
23.2k
    set_dev_proc(dev, get_clipping_box, gx_get_largest_clipping_box);
115
23.2k
    set_dev_proc(dev, get_bits_rectangle, pattern_accum_get_bits_rectangle);
116
23.2k
    set_dev_proc(dev, fill_rectangle_hl_color, pattern_accum_fill_rectangle_hl_color);
117
23.2k
    set_dev_proc(dev, dev_spec_op, pattern_accum_dev_spec_op);
118
23.2k
    set_dev_proc(dev, copy_planes, pattern_accum_copy_planes);
119
120
    /* It would be much nicer if gx_device_init set the following
121
     * defaults for us, but that doesn't work for some reason. */
122
23.2k
    set_dev_proc(dev, copy_alpha, gx_default_copy_alpha);
123
23.2k
    set_dev_proc(dev, fill_path, gx_default_fill_path);
124
23.2k
    set_dev_proc(dev, stroke_path, gx_default_stroke_path);
125
23.2k
    set_dev_proc(dev, fill_mask, gx_default_fill_mask);
126
23.2k
    set_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
127
23.2k
    set_dev_proc(dev, fill_parallelogram, gx_default_fill_parallelogram);
128
23.2k
    set_dev_proc(dev, fill_triangle, gx_default_fill_triangle);
129
23.2k
    set_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
130
23.2k
    set_dev_proc(dev, strip_tile_rectangle, gx_default_strip_tile_rectangle);
131
23.2k
    set_dev_proc(dev, begin_typed_image, gx_default_begin_typed_image);
132
23.2k
    set_dev_proc(dev, composite, gx_default_composite);
133
23.2k
    set_dev_proc(dev, text_begin, gx_default_text_begin);
134
23.2k
    set_dev_proc(dev, strip_copy_rop2, gx_default_strip_copy_rop2);
135
23.2k
    set_dev_proc(dev, strip_tile_rect_devn, gx_default_strip_tile_rect_devn);
136
23.2k
    set_dev_proc(dev, transform_pixel_region, gx_default_transform_pixel_region);
137
23.2k
    set_dev_proc(dev, fill_stroke_path, gx_default_fill_stroke_path);
138
23.2k
    set_dev_proc(dev, lock_pattern, gx_default_lock_pattern);
139
23.2k
    set_dev_proc(dev, copy_alpha_hl_color, gx_default_copy_alpha_hl_color);
140
23.2k
}
141
142
static const gx_device_pattern_accum gs_pattern_accum_device =
143
{std_device_std_body_type_open(gx_device_pattern_accum,
144
                               pattern_accum_initialize_device_procs,
145
                               "pattern accumulator",
146
                               &st_device_pattern_accum,
147
                               0, 0, 72, 72)
148
};
149
150
extern dev_proc_open_device(clist_open);
151
152
int
153
pattern_clist_open_device(gx_device *dev)
154
128k
{
155
    /* This function is defiled only for clist_init_bands. */
156
128k
    return clist_open(dev);
157
128k
}
158
159
static dev_proc_create_buf_device(dummy_create_buf_device)
160
257k
{
161
257k
    gx_device_memory *mdev = (gx_device_memory *)*pbdev;
162
163
257k
    gs_make_mem_device(mdev, gdev_mem_device_for_bits(target->color_info.depth),
164
257k
                mem, 0, target);
165
257k
    return 0;
166
257k
}
167
static dev_proc_size_buf_device(dummy_size_buf_device)
168
0
{
169
0
    return 0;
170
0
}
171
static dev_proc_setup_buf_device(dummy_setup_buf_device)
172
0
{
173
0
    return 0;
174
0
}
175
static dev_proc_destroy_buf_device(dummy_destroy_buf_device)
176
0
{
177
0
}
178
/* Attempt to determine the size of a pattern (the approximate amount that will */
179
/* be needed in the pattern cache). If we end up using the clist, this is only  */
180
/* a guess -- we use the tile size which will _probably_ be too large.          */
181
static size_t
182
gx_pattern_size_estimate(gs_pattern1_instance_t *pinst, bool has_tags)
183
180k
{
184
180k
    gx_device *tdev = pinst->saved->device;
185
180k
    int depth = (pinst->templat.PaintType == 2 ? 1 : tdev->color_info.depth);
186
180k
    size_t raster;
187
180k
    size_t size;
188
189
180k
    if (pinst->size.x == 0 || pinst->size.y == 0)
190
74
        return 0;
191
192
180k
    if (pinst->templat.uses_transparency) {
193
        /* if the device has tags, add in an extra tag byte for the pdf14 compositor */
194
38.8k
        raster = ((size_t)pinst->size.x * ((depth/8) + 1 + (has_tags ? 1 : 0)));
195
141k
    } else {
196
141k
        raster = ((size_t)pinst->size.x * depth + 7) / 8;
197
141k
    }
198
180k
    size = raster > max_size_t / pinst->size.y ? (max_size_t - 0xFFFF) : raster * pinst->size.y;
199
180k
    return size;
200
180k
}
201
202
static void gx_pattern_accum_finalize_cw(gx_device * dev)
203
128k
{
204
128k
    gx_device_clist_writer *cwdev = (gx_device_clist_writer *)dev;
205
128k
    rc_decrement_only(cwdev->target, "gx_pattern_accum_finalize_cw");
206
128k
}
207
208
bool gx_device_is_pattern_accum(gx_device *dev)
209
6.03M
{
210
6.03M
    return dev_proc(dev, open_device) == pattern_accum_open;
211
6.03M
}
212
213
bool gx_device_is_pattern_clist(gx_device *dev)
214
6.73M
{
215
6.73M
    return dev_proc(dev, open_device) == pattern_clist_open_device;
216
6.73M
}
217
218
/* Allocate a pattern accumulator, with an initial refct of 0. */
219
gx_device_forward *
220
gx_pattern_accum_alloc(gs_memory_t * mem, gs_memory_t * storage_memory,
221
                       gs_pattern1_instance_t *pinst, client_name_t cname)
222
152k
{
223
152k
    gx_device *tdev = pinst->saved->device;
224
152k
    bool has_tags = device_encodes_tags(tdev);
225
152k
    size_t size = gx_pattern_size_estimate(pinst, has_tags);
226
152k
    gx_device_forward *fdev;
227
152k
    int force_no_clist = 0;
228
152k
    size_t max_pattern_bitmap = tdev->MaxPatternBitmap == 0 ? MaxPatternBitmap_DEFAULT :
229
152k
                                tdev->MaxPatternBitmap;
230
231
152k
    pinst->num_planar_planes = tdev->num_planar_planes;
232
    /*
233
     * If the target device can accumulate a pattern stream and the language
234
     * client supports high level patterns (ps and pdf only) we don't need a
235
     * raster or clist representation for the pattern, but the code goes
236
     * through the motions of creating the device anyway.  Later when the
237
     * pattern paint procedure is called  an error is returned and whatever
238
     * has been set up here is destroyed.  We try to make sure the same path
239
     * is taken in the code even though the device is never used because
240
     * there are pathological problems (see Bug689851.pdf) where the pattern
241
     * is so large we can't even allocate the memory for the device and the
242
     * dummy clist path must be used.  None of this discussion is relevant if
243
     * the client language does not support high level patterns or the device
244
     * cannot accumulate the pattern stream.
245
     */
246
152k
    if (pinst->saved->have_pattern_streams == 0 && (*dev_proc(pinst->saved->device,
247
123k
        dev_spec_op))((gx_device *)pinst->saved->device,
248
123k
        gxdso_pattern_can_accum, pinst, 0) == 1)
249
0
        force_no_clist = 1; /* Set only for first time through */
250
    /* If the blend mode in use is not Normal, then we CANNOT use a tile. What
251
     * if the blend mode changes half way through the tile? We simply must use
252
     * a clist. */
253
152k
    if (force_no_clist ||
254
152k
        (((size < max_pattern_bitmap && !pinst->is_clist)
255
152k
           || pinst->templat.PaintType != 1) && !pinst->templat.BM_Not_Normal)) {
256
23.2k
        gx_device_pattern_accum *adev = gs_alloc_struct_immovable(mem, gx_device_pattern_accum,
257
23.2k
                        &st_device_pattern_accum, cname);
258
23.2k
        if (adev == 0)
259
0
            return 0;
260
#ifdef DEBUG
261
        if (pinst->is_clist)
262
            emprintf(mem, "not using clist even though clist is requested\n");
263
#endif
264
23.2k
        pinst->is_clist = false;
265
23.2k
        (void)gx_device_init((gx_device *)adev,
266
23.2k
                             (const gx_device *)&gs_pattern_accum_device,
267
23.2k
                             mem, true);
268
23.2k
        adev->instance = pinst;
269
23.2k
        adev->bitmap_memory = storage_memory;
270
23.2k
        fdev = (gx_device_forward *)adev;
271
128k
    } else {
272
128k
        gx_device_buf_procs_t buf_procs = {dummy_create_buf_device,
273
128k
        dummy_size_buf_device, dummy_setup_buf_device, dummy_destroy_buf_device};
274
128k
        gx_device_clist *cdev;
275
128k
        gx_device_clist_writer *cwdev;
276
128k
        const int data_size = 1024*128;
277
128k
        gx_band_params_t band_params = { 0 };
278
128k
        byte *data  = gs_alloc_bytes(mem->non_gc_memory, data_size, cname);
279
280
128k
        if (data == NULL)
281
0
            return 0;
282
128k
        pinst->is_clist = true;
283
        /* NB: band_params.page_uses_transparency is set in clist_make_accum_device */
284
128k
        band_params.BandWidth = pinst->size.x;
285
128k
        band_params.BandHeight = pinst->size.y;
286
128k
        band_params.BandBufferSpace = 0;
287
288
128k
        cdev = clist_make_accum_device(mem, tdev, "pattern-clist", data, data_size,
289
128k
                                       &buf_procs, &band_params, true, /* use_memory_clist */
290
128k
                                       pinst->templat.uses_transparency, pinst);
291
128k
        if (cdev == 0) {
292
0
            gs_free_object(tdev->memory->non_gc_memory, data, cname);
293
0
            return 0;
294
0
        }
295
128k
        cwdev = (gx_device_clist_writer *)cdev;
296
128k
        cwdev->finalize = gx_pattern_accum_finalize_cw;
297
128k
        set_dev_proc(cwdev, open_device, pattern_clist_open_device);
298
128k
        fdev = (gx_device_forward *)cdev;
299
128k
    }
300
152k
    fdev->log2_align_mod = tdev->log2_align_mod;
301
152k
    fdev->pad = tdev->pad;
302
152k
    fdev->num_planar_planes = tdev->num_planar_planes;
303
152k
    fdev->graphics_type_tag = tdev->graphics_type_tag;
304
152k
    fdev->interpolate_control = tdev->interpolate_control;
305
152k
    fdev->non_strict_bounds = tdev->non_strict_bounds;
306
152k
    gx_device_forward_fill_in_procs(fdev);
307
152k
    return fdev;
308
152k
}
309
310
gx_pattern_trans_t*
311
new_pattern_trans_buff(gs_memory_t *mem)
312
139k
{
313
139k
    gx_pattern_trans_t *result;
314
315
    /* Allocate structure that we will use for the trans pattern */
316
139k
    result = gs_alloc_struct(mem, gx_pattern_trans_t, &st_pattern_trans, "new_pattern_trans_buff");
317
318
139k
    if (result != NULL) {
319
139k
        result->transbytes = NULL;
320
139k
        result->pdev14 = NULL;
321
139k
        result->mem = NULL;
322
139k
        result->fill_trans_buffer = NULL;
323
139k
        result->buf = NULL;
324
139k
        result->n_chan = 0;
325
139k
    }
326
327
139k
    return(result);
328
139k
}
329
330
/*
331
 * Initialize a pattern accumulator.
332
 * Client must already have set instance and bitmap_memory.
333
 *
334
 * Note that mask and bits accumulators are only created if necessary.
335
 */
336
static int
337
pattern_accum_open(gx_device * dev)
338
23.2k
{
339
23.2k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
340
23.2k
    const gs_pattern1_instance_t *pinst = padev->instance;
341
23.2k
    gs_memory_t *mem = padev->bitmap_memory;
342
23.2k
    gx_device_memory *mask = 0;
343
23.2k
    gx_device_memory *bits = 0;
344
    /*
345
     * The client should preset the target, because the device for which the
346
     * pattern is being rendered may not (in general, will not) be the same
347
     * as the one that was current when the pattern was instantiated.
348
     */
349
23.2k
    gx_device *target =
350
23.2k
        (padev->target == 0 ? gs_currentdevice(pinst->saved) :
351
23.2k
         padev->target);
352
23.2k
    int width = pinst->size.x;
353
23.2k
    int height = pinst->size.y;
354
23.2k
    int code = 0;
355
23.2k
    bool mask_open = false;
356
357
    /*
358
     * C's bizarre coercion rules force us to copy HWResolution in pieces
359
     * rather than using a single assignment.
360
     */
361
23.2k
#define PDSET(dev)\
362
50.9k
  ((dev)->width = width, (dev)->height = height,\
363
   /*(dev)->HWResolution = target->HWResolution*/\
364
50.9k
   (dev)->HWResolution[0] = target->HWResolution[0],\
365
50.9k
   (dev)->HWResolution[1] = target->HWResolution[1])
366
367
23.2k
    PDSET(padev);
368
23.2k
    padev->color_info = target->color_info;
369
    /* Bug 689737: If PaintType == 2 (Uncolored tiling pattern), pattern is
370
     * 1bpp bitmap. No antialiasing in this case! */
371
23.2k
    if (pinst->templat.PaintType == 2) {
372
212
        padev->color_info.anti_alias.text_bits = 1;
373
212
        padev->color_info.anti_alias.graphics_bits = 1;
374
212
    }
375
    /* If we have transparency, then fix the color info
376
       now so that the mem device allocates the proper
377
       buffer space for the pattern template.  We can
378
       do this since the transparency code all */
379
23.2k
    if (pinst->templat.uses_transparency) {
380
        /* Allocate structure that we will use for the trans pattern */
381
18.6k
        padev->transbuff = new_pattern_trans_buff(mem);
382
18.6k
        if (padev->transbuff == NULL)
383
0
            return_error(gs_error_VMerror);
384
18.6k
    } else {
385
4.59k
        padev->transbuff = NULL;
386
4.59k
    }
387
23.2k
    if (pinst->uses_mask) {
388
23.2k
        mask = gs_alloc_struct( mem,
389
23.2k
                                gx_device_memory,
390
23.2k
                                &st_device_memory,
391
23.2k
                                "pattern_accum_open(mask)"
392
23.2k
                                );
393
23.2k
        if (mask == 0)
394
0
            return_error(gs_error_VMerror);
395
23.2k
        gs_make_mem_mono_device(mask, mem, 0);
396
23.2k
        PDSET(mask);
397
23.2k
        mask->bitmap_memory = mem;
398
23.2k
        mask->base = 0;
399
23.2k
        code = (*dev_proc(mask, open_device)) ((gx_device *) mask);
400
23.2k
        if (code >= 0) {
401
23.2k
            mask_open = true;
402
23.2k
            memset(mask->base, 0, (size_t)mask->raster * mask->height);
403
23.2k
        }
404
23.2k
    }
405
406
23.2k
    if (code >= 0) {
407
23.2k
        if (pinst->templat.uses_transparency) {
408
            /* In this case, we will grab the buffer created
409
               by the graphic state's device (which is pdf14) and
410
               we will be tiling that into a transparency group buffer
411
               to blend with the pattern accumulator's target.  Since
412
               all the transparency stuff is planar format, it is
413
               best just to keep the data in that form */
414
18.6k
            gx_device_set_target((gx_device_forward *)padev, target);
415
18.6k
        } else {
416
4.59k
            switch (pinst->templat.PaintType) {
417
212
            case 2:             /* uncolored */
418
212
                gx_device_set_target((gx_device_forward *)padev, target);
419
212
                break;
420
4.38k
            case 1:             /* colored */
421
4.38k
                bits = gs_alloc_struct(mem, gx_device_memory,
422
4.38k
                                       &st_device_memory,
423
4.38k
                                       "pattern_accum_open(bits)");
424
4.38k
                if (bits == 0)
425
0
                    code = gs_note_error(gs_error_VMerror);
426
4.38k
                else {
427
4.38k
                    gs_make_mem_device(bits,
428
4.38k
                            gdev_mem_device_for_bits(padev->color_info.depth),
429
4.38k
                                       mem, -1, target);
430
4.38k
                    PDSET(bits);
431
4.38k
#undef PDSET
432
4.38k
                    bits->color_info = padev->color_info;
433
4.38k
                    bits->bitmap_memory = mem;
434
435
4.38k
                    if (target->num_planar_planes > 0)
436
482
                    {
437
482
                        gx_render_plane_t planes[GX_DEVICE_COLOR_MAX_COMPONENTS];
438
482
                        uchar num_comp = padev->num_planar_planes;
439
482
                        uchar i;
440
482
                        int depth = target->color_info.depth / num_comp;
441
2.21k
                        for (i = 0; i < num_comp; i++)
442
1.72k
                        {
443
1.72k
                            planes[i].shift = depth * (num_comp - 1 - i);
444
1.72k
                            planes[i].depth = depth;
445
1.72k
                            planes[i].index = i;
446
1.72k
                        }
447
482
                        code = gdev_mem_set_planar(bits, num_comp, planes);
448
482
                    }
449
4.38k
                    if (code >= 0) {
450
4.38k
                        code = (*dev_proc(bits, open_device)) ((gx_device *) bits);
451
4.38k
                        gx_device_set_target((gx_device_forward *)padev,
452
4.38k
                                             (gx_device *)bits);
453
                        /* The update_spot_equivalent_color proc for the bits device
454
                           should forward to the real target device.  This will ensure
455
                           that the target device can get equivalent CMYK values for
456
                           spot colors if we are using a separation device and the spot
457
                           color occurs only in patterns on the page. */
458
4.38k
                        bits->procs.update_spot_equivalent_colors = gx_forward_update_spot_equivalent_colors;
459
4.38k
                    }
460
4.38k
                }
461
4.59k
            }
462
4.59k
        }
463
23.2k
    }
464
23.2k
    if (code < 0) {
465
0
        if (bits != 0)
466
0
            gs_free_object(mem, bits, "pattern_accum_open(bits)");
467
0
        if (mask != 0) {
468
0
            if (mask_open)
469
0
                (*dev_proc(mask, close_device)) ((gx_device *) mask);
470
0
            gs_free_object(mem, mask, "pattern_accum_open(mask)");
471
0
        }
472
0
        return code;
473
0
    }
474
23.2k
    padev->mask = mask;
475
23.2k
    padev->bits = bits;
476
    /* Retain the device, so it will survive anomalous grestores. */
477
23.2k
    gx_device_retain(dev, true);
478
23.2k
    return code;
479
23.2k
}
480
481
/* Close an accumulator and free the bits. */
482
static int
483
pattern_accum_close(gx_device * dev)
484
46.5k
{
485
46.5k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
486
46.5k
    gs_memory_t *mem = padev->bitmap_memory;
487
488
    /*
489
     * If bits != 0, it is the target of the device; reference counting
490
     * will close and free it.
491
     */
492
46.5k
    gx_device_set_target((gx_device_forward *)padev, NULL);
493
46.5k
    padev->bits = 0;
494
46.5k
    if (padev->mask != 0) {
495
23.2k
        (*dev_proc(padev->mask, close_device)) ((gx_device *) padev->mask);
496
23.2k
        gs_free_object(mem, padev->mask, "pattern_accum_close(mask)");
497
23.2k
        padev->mask = 0;
498
23.2k
    }
499
500
46.5k
    if (padev->transbuff != 0) {
501
15.5k
        gs_free_object(mem,padev->target,"pattern_accum_close(transbuff)");
502
15.5k
        padev->transbuff = NULL;
503
15.5k
    }
504
505
    /* Un-retain the device now, so reference counting will free it. */
506
46.5k
    gx_device_retain(dev, false);
507
46.5k
    return 0;
508
46.5k
}
509
510
/* _hl_color */
511
static int
512
pattern_accum_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect,
513
                                      const gs_gstate *pgs,
514
                                      const gx_drawing_color *pdcolor,
515
                                      const gx_clip_path *pcpath)
516
143k
{
517
143k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
518
143k
    int code;
519
520
143k
    if (padev->bits) {
521
143k
        code = (*dev_proc(padev->target, fill_rectangle_hl_color))
522
143k
            (padev->target, rect, pgs, pdcolor, pcpath);
523
143k
        if (code < 0)
524
0
            return code;
525
143k
    }
526
143k
    if (padev->mask) {
527
142k
        int x, y, w, h;
528
529
142k
        x = fixed2int(rect->p.x);
530
142k
        y = fixed2int(rect->p.y);
531
142k
        w = fixed2int(rect->q.x) - x;
532
142k
        h = fixed2int(rect->q.y) - y;
533
534
142k
        return (*dev_proc(padev->mask, fill_rectangle))
535
142k
            ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
536
142k
    }
537
633
    return 0;
538
143k
}
539
540
/* Fill a rectangle */
541
static int
542
pattern_accum_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
543
                             gx_color_index color)
544
435k
{
545
435k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
546
547
435k
    if (padev->bits)
548
433k
        (*dev_proc(padev->target, fill_rectangle))
549
433k
            (padev->target, x, y, w, h, color);
550
435k
    if (padev->mask)
551
397k
        return (*dev_proc(padev->mask, fill_rectangle))
552
397k
            ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
553
38.0k
     else
554
38.0k
        return 0;
555
435k
}
556
557
/* Copy a monochrome bitmap. */
558
static int
559
pattern_accum_copy_mono(gx_device * dev, const byte * data, int data_x,
560
                    int raster, gx_bitmap_id id, int x, int y, int w, int h,
561
                        gx_color_index color0, gx_color_index color1)
562
168k
{
563
168k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
564
565
    /* opt out early if nothing to render (some may think this a bug) */
566
168k
    if (color0 == gx_no_color_index && color1 == gx_no_color_index)
567
0
        return 0;
568
168k
    if (padev->bits)
569
168k
        (*dev_proc(padev->target, copy_mono))
570
168k
            (padev->target, data, data_x, raster, id, x, y, w, h,
571
168k
             color0, color1);
572
168k
    if (padev->mask) {
573
168k
        if (color0 != gx_no_color_index)
574
167k
            color0 = 1;
575
168k
        if (color1 != gx_no_color_index)
576
168k
            color1 = 1;
577
168k
        if (color0 == 1 && color1 == 1)
578
167k
            return (*dev_proc(padev->mask, fill_rectangle))
579
167k
                ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
580
51
        else
581
51
            return (*dev_proc(padev->mask, copy_mono))
582
51
                ((gx_device *) padev->mask, data, data_x, raster, id, x, y, w, h,
583
51
                 color0, color1);
584
168k
    } else
585
0
        return 0;
586
168k
}
587
588
/* Copy a color bitmap. */
589
static int
590
pattern_accum_copy_color(gx_device * dev, const byte * data, int data_x,
591
                    int raster, gx_bitmap_id id, int x, int y, int w, int h)
592
104k
{
593
104k
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
594
595
104k
    if (padev->bits)
596
104k
        (*dev_proc(padev->target, copy_color))
597
104k
            (padev->target, data, data_x, raster, id, x, y, w, h);
598
104k
    if (padev->mask)
599
104k
        return (*dev_proc(padev->mask, fill_rectangle))
600
104k
            ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
601
0
    else
602
0
        return 0;
603
104k
}
604
605
/* Copy a color plane. */
606
static int
607
pattern_accum_copy_planes(gx_device * dev, const byte * data, int data_x,
608
                          int raster, gx_bitmap_id id,
609
                          int x, int y, int w, int h, int plane_height)
610
320
{
611
320
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
612
613
320
    if (padev->bits)
614
320
        (*dev_proc(padev->target, copy_planes))
615
320
            (padev->target, data, data_x, raster, id, x, y, w, h, plane_height);
616
320
    if (padev->mask)
617
320
        return (*dev_proc(padev->mask, fill_rectangle))
618
320
            ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
619
0
    else
620
0
        return 0;
621
320
}
622
623
static int
624
blank_unmasked_bits(gx_device * mask,
625
                    int polarity,
626
                    int num_comps,
627
                    int depth,
628
                    const gs_int_rect *prect,
629
                    gs_get_bits_params_t *p)
630
0
{
631
0
    static const int required_options = GB_COLORS_NATIVE
632
0
                       | GB_ALPHA_NONE
633
0
                       | GB_RETURN_COPY
634
0
                       | GB_ALIGN_STANDARD
635
0
                       | GB_OFFSET_0
636
0
                       | GB_RASTER_STANDARD;
637
0
    int raster = p->raster;
638
0
    byte *min;
639
0
    int x0 = prect->p.x;
640
0
    int y0 = prect->p.y;
641
0
    int x, y;
642
0
    int w = prect->q.x - x0;
643
0
    int h = prect->q.y - y0;
644
0
    int code = 0;
645
0
    byte *ptr;
646
0
    int blank = (polarity == GX_CINFO_POLARITY_ADDITIVE ? 255 : 0);
647
0
    gs_int_rect rect;
648
0
    gs_get_bits_params_t params;
649
650
0
    if ((p->options & required_options) != required_options)
651
0
        return_error(gs_error_rangecheck);
652
653
0
    min = gs_alloc_bytes(mask->memory, (w+7)>>3, "blank_unmasked_bits");
654
0
    if (min == NULL)
655
0
        return_error(gs_error_VMerror);
656
657
0
    rect.p.x = 0;
658
0
    rect.q.x = mask->width;
659
0
    params.x_offset = 0;
660
0
    params.raster = bitmap_raster(mask->width * mask->color_info.depth);
661
662
0
    if (p->options & GB_PACKING_CHUNKY)
663
0
    {
664
0
        if ((depth & 7) != 0 || depth > 64) {
665
0
            code = gs_note_error(gs_error_rangecheck);
666
0
            goto fail;
667
0
        }
668
0
        ptr = p->data[0];
669
0
        depth >>= 3;
670
0
        raster -= w*depth;
671
0
        for (y = 0; y < h; y++)
672
0
        {
673
0
            byte *mine;
674
675
0
            rect.p.y = y+y0;
676
0
            rect.q.y = y+y0+1;
677
0
            params.options = (GB_ALIGN_ANY |
678
0
                              (GB_RETURN_COPY | GB_RETURN_POINTER) |
679
0
                              GB_OFFSET_0 |
680
0
                              GB_RASTER_STANDARD | GB_PACKING_CHUNKY |
681
0
                              GB_COLORS_NATIVE | GB_ALPHA_NONE);
682
0
            params.data[0] = min;
683
0
            code = (*dev_proc(mask, get_bits_rectangle))(mask, &rect,
684
0
                                                         &params);
685
0
            if (code < 0)
686
0
                goto fail;
687
0
            mine = params.data[0];
688
0
            for (x = 0; x < w; x++)
689
0
            {
690
0
                int xx = x+x0;
691
0
                if (((mine[xx>>3]<<(x&7)) & 128) == 0) {
692
0
                    switch (depth)
693
0
                    {
694
0
                    case 8:
695
0
                        *ptr++ = blank;
696
0
                    case 7:
697
0
                        *ptr++ = blank;
698
0
                    case 6:
699
0
                        *ptr++ = blank;
700
0
                    case 5:
701
0
                        *ptr++ = blank;
702
0
                    case 4:
703
0
                        *ptr++ = blank;
704
0
                    case 3:
705
0
                        *ptr++ = blank;
706
0
                    case 2:
707
0
                        *ptr++ = blank;
708
0
                    case 1:
709
0
                        *ptr++ = blank;
710
0
                        break;
711
0
                    }
712
0
                } else {
713
0
                    ptr += depth;
714
0
                }
715
0
            }
716
0
            ptr += raster;
717
0
        }
718
0
    } else {
719
        /* Planar, only handle 8 or 16 bits */
720
0
        int bytes_per_component = (depth/num_comps) >> 3;
721
722
0
        if (depth/num_comps != 8 && depth/num_comps != 16) {
723
0
            code = gs_note_error(gs_error_rangecheck);
724
0
            goto fail;
725
0
        }
726
0
        for (y = 0; y < h; y++)
727
0
        {
728
0
            int c;
729
0
            byte *mine;
730
731
0
            rect.p.y = y+y0;
732
0
            rect.q.y = y+y0+1;
733
0
            params.options = (GB_ALIGN_ANY |
734
0
                              (GB_RETURN_COPY | GB_RETURN_POINTER) |
735
0
                              GB_OFFSET_0 |
736
0
                              GB_RASTER_STANDARD | GB_PACKING_CHUNKY |
737
0
                              GB_COLORS_NATIVE | GB_ALPHA_NONE);
738
0
            params.data[0] = min;
739
0
            code = (*dev_proc(mask, get_bits_rectangle))(mask, &rect,
740
0
                                                         &params);
741
0
            if (code < 0)
742
0
                goto fail;
743
0
            mine = params.data[0];
744
745
0
            for (c = 0; c < num_comps; c++)
746
0
            {
747
0
                if (p->data[c] == NULL)
748
0
                    continue;
749
0
                ptr = p->data[c] + raster * y;
750
0
                for (x = 0; x < w; x++)
751
0
                {
752
0
                    int xx = x+x0;
753
0
                    if (((mine[xx>>3]>>(x&7)) & 1) == 0) {
754
0
                        *ptr++ = blank;
755
0
                        if (bytes_per_component > 1)
756
0
                            *ptr++ = blank;
757
0
                    } else {
758
0
                        ptr += bytes_per_component;
759
0
                    }
760
0
                }
761
0
            }
762
0
        }
763
0
    }
764
765
0
fail:
766
0
    gs_free_object(mask->memory, min, "blank_unmasked_bits");
767
768
0
    return code;
769
0
}
770
771
/* Read back a rectangle of bits. */
772
/****** SHOULD USE MASK TO DEFINE UNREAD AREA *****/
773
static int
774
pattern_accum_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
775
                       gs_get_bits_params_t * params)
776
0
{
777
0
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
778
0
    int code;
779
0
    gs_get_bits_params_t params2 = *params;
780
781
0
    if (padev->bits) {
782
0
        if (padev->mask)
783
0
            params2.options &= ~GB_RETURN_POINTER;
784
0
        code = (*dev_proc(padev->target, get_bits_rectangle))
785
0
            (padev->target, prect, &params2);
786
        /* If we have a mask, then unmarked pixels of the bits
787
         * will be undefined. Strictly speaking it makes no
788
         * sense for us to return any value here, but the only
789
         * caller of this currently is the overprint code, which
790
         * uses the the values to parrot back to us. Let's
791
         * make sure they are set to the default 'empty' values.
792
         */
793
0
        if (code >= 0 && padev->mask)
794
0
            code = blank_unmasked_bits((gx_device *)padev->mask,
795
0
                                       padev->target->color_info.polarity,
796
0
                                       padev->target->color_info.num_components,
797
0
                                       padev->target->color_info.depth,
798
0
                                       prect, &params2);
799
0
        return code;
800
0
    }
801
802
0
    return_error(gs_error_Fatal); /* shouldn't happen */
803
0
}
804
805
/* ------ Color space implementation ------ */
806
807
/* Free all entries in a pattern cache. */
808
static bool
809
pattern_cache_choose_all(gx_color_tile * ctile, void *proc_data)
810
182k
{
811
182k
    return true;
812
182k
}
813
static void
814
pattern_cache_free_all(gx_pattern_cache * pcache)
815
1.25M
{
816
1.25M
    gx_pattern_cache_winnow(pcache, pattern_cache_choose_all, NULL);
817
1.25M
}
818
819
/* Allocate a Pattern cache. */
820
gx_pattern_cache *
821
gx_pattern_alloc_cache(gs_memory_t * mem, uint num_tiles, ulong max_bits)
822
234k
{
823
234k
    gx_pattern_cache *pcache =
824
234k
    gs_alloc_struct(mem, gx_pattern_cache, &st_pattern_cache,
825
234k
                    "gx_pattern_alloc_cache(struct)");
826
234k
    gx_color_tile *tiles =
827
234k
    gs_alloc_struct_array(mem, num_tiles, gx_color_tile,
828
234k
                          &st_color_tile_element,
829
234k
                          "gx_pattern_alloc_cache(tiles)");
830
234k
    uint i;
831
832
234k
    if (pcache == 0 || tiles == 0) {
833
0
        gs_free_object(mem, tiles, "gx_pattern_alloc_cache(tiles)");
834
0
        gs_free_object(mem, pcache, "gx_pattern_alloc_cache(struct)");
835
0
        return 0;
836
0
    }
837
234k
    pcache->memory = mem;
838
234k
    pcache->tiles = tiles;
839
234k
    pcache->num_tiles = num_tiles;
840
234k
    pcache->tiles_used = 0;
841
234k
    pcache->next = 0;
842
234k
    pcache->bits_used = 0;
843
234k
    pcache->max_bits = max_bits;
844
234k
    pcache->free_all = pattern_cache_free_all;
845
11.9M
    for (i = 0; i < num_tiles; tiles++, i++) {
846
11.7M
        tiles->id = gx_no_bitmap_id;
847
        /* Clear the pointers to pacify the GC. */
848
11.7M
        uid_set_invalid(&tiles->uid);
849
11.7M
        tiles->bits_used = 0;
850
11.7M
#ifdef PACIFY_VALGRIND
851
        /* The following memsets are required to avoid a valgrind warning
852
         * in:
853
         *   gs -I./gs/lib -sOutputFile=out.pgm -dMaxBitmap=10000
854
         *      -sDEVICE=pgmraw -r300 -Z: -sDEFAULTPAPERSIZE=letter
855
         *      -dNOPAUSE -dBATCH -K2000000 -dClusterJob -dJOBSERVER
856
         *      tests_private/ps/ps3cet/11-14.PS
857
         * Setting the individual elements of the structures directly is
858
         * not enough, which leads me to believe that we are writing the
859
         * entire structs out, padding and all.
860
         */
861
11.7M
        memset(&tiles->tbits, 0, sizeof(tiles->tbits));
862
11.7M
        memset(&tiles->tmask, 0, sizeof(tiles->tmask));
863
#else
864
        tiles->tbits.data = 0;
865
        tiles->tmask.data = 0;
866
#endif
867
11.7M
        tiles->index = i;
868
11.7M
        tiles->cdev = NULL;
869
11.7M
        tiles->ttrans = NULL;
870
11.7M
        tiles->num_planar_planes = 0;
871
11.7M
    }
872
234k
    return pcache;
873
234k
}
874
/* Ensure that an imager has a Pattern cache. */
875
static int
876
ensure_pattern_cache(gs_gstate * pgs)
877
548k
{
878
548k
    if (pgs->pattern_cache == 0) {
879
74.3k
        gx_pattern_cache *pcache =
880
74.3k
        gx_pattern_alloc_cache(pgs->memory,
881
74.3k
                               gx_pat_cache_default_tiles(),
882
74.3k
                               gx_pat_cache_default_bits());
883
884
74.3k
        if (pcache == 0)
885
0
            return_error(gs_error_VMerror);
886
74.3k
        pgs->pattern_cache = pcache;
887
74.3k
    }
888
548k
    return 0;
889
548k
}
890
891
/* Free pattern cache and its components. */
892
void
893
gx_pattern_cache_free(gx_pattern_cache *pcache)
894
166k
{
895
166k
    if (pcache == NULL)
896
92.4k
        return;
897
74.3k
    pattern_cache_free_all(pcache);
898
74.3k
    gs_free_object(pcache->memory, pcache->tiles, "gx_pattern_cache_free");
899
74.3k
    pcache->tiles = NULL;
900
74.3k
    gs_free_object(pcache->memory, pcache, "gx_pattern_cache_free");
901
74.3k
}
902
903
/* Get and set the Pattern cache in a gstate. */
904
gx_pattern_cache *
905
gstate_pattern_cache(gs_gstate * pgs)
906
103k
{
907
103k
    return pgs->pattern_cache;
908
103k
}
909
void
910
gstate_set_pattern_cache(gs_gstate * pgs, gx_pattern_cache * pcache)
911
159k
{
912
159k
    pgs->pattern_cache = pcache;
913
159k
}
914
915
/* Free a Pattern cache entry. */
916
/* This will not free a pattern if it is 'locked' which should only be for */
917
/* a stroke pattern during fill_stroke_path.                               */
918
static void
919
gx_pattern_cache_free_entry(gx_pattern_cache * pcache, gx_color_tile * ctile, bool free_dummy)
920
1.51M
{
921
1.51M
    gx_device *temp_device;
922
923
1.51M
    if ((ctile->id != gx_no_bitmap_id) && (!ctile->is_dummy || free_dummy) && !ctile->is_locked) {
924
272k
        gs_memory_t *mem = pcache->memory;
925
926
        /*
927
         * We must initialize the memory device properly, even though
928
         * we aren't using it for drawing.
929
         */
930
272k
        if (ctile->tmask.data != 0) {
931
60.8k
            gs_free_object(mem, ctile->tmask.data,
932
60.8k
                           "free_pattern_cache_entry(mask data)");
933
60.8k
            ctile->tmask.data = 0;      /* for GC */
934
60.8k
        }
935
272k
        if (ctile->tbits.data != 0) {
936
45.8k
            gs_free_object(mem, ctile->tbits.data,
937
45.8k
                           "free_pattern_cache_entry(bits data)");
938
45.8k
            ctile->tbits.data = 0;      /* for GC */
939
45.8k
        }
940
272k
        if (ctile->cdev != NULL) {
941
126k
            ctile->cdev->common.do_not_open_or_close_bandfiles = false;  /* make sure memfile gets freed/closed */
942
126k
            dev_proc(&ctile->cdev->common, close_device)((gx_device *)&ctile->cdev->common);
943
            /* Free up the icc based stuff in the clist device.  I am puzzled
944
               why the other objects are not released */
945
126k
            clist_free_icc_table(ctile->cdev->common.icc_table,
946
126k
                            ctile->cdev->common.memory);
947
126k
            ctile->cdev->common.icc_table = NULL;
948
126k
            rc_decrement(ctile->cdev->common.icc_cache_cl,
949
126k
                            "gx_pattern_cache_free_entry");
950
126k
            ctile->cdev->common.icc_cache_cl = NULL;
951
126k
            ctile->cdev->writer.pinst = NULL;
952
126k
            gs_free_object(ctile->cdev->common.memory->non_gc_memory, ctile->cdev->common.cache_chunk, "free tile cache for clist");
953
126k
            ctile->cdev->common.cache_chunk = 0;
954
126k
            temp_device = (gx_device *)ctile->cdev;
955
126k
            gx_device_retain(temp_device, false);
956
126k
            ctile->cdev = NULL;
957
126k
        }
958
959
272k
        if (ctile->ttrans != NULL) {
960
94.5k
            if_debug2m('v', mem,
961
94.5k
                       "[v*] Freeing trans pattern from cache, uid = %ld id = %ld\n",
962
94.5k
                       ctile->uid.id, ctile->id);
963
94.5k
            if ( ctile->ttrans->pdev14 == NULL) {
964
                /* This can happen if we came from the clist */
965
94.5k
                if (ctile->ttrans->mem != NULL)
966
94.5k
                    gs_free_object(ctile->ttrans->mem ,ctile->ttrans->transbytes,
967
94.5k
                                   "free_pattern_cache_entry(transbytes)");
968
94.5k
                gs_free_object(mem,ctile->ttrans->fill_trans_buffer,
969
94.5k
                                "free_pattern_cache_entry(fill_trans_buffer)");
970
94.5k
                ctile->ttrans->transbytes = NULL;
971
94.5k
                ctile->ttrans->fill_trans_buffer = NULL;
972
94.5k
            } else {
973
0
                dev_proc(ctile->ttrans->pdev14, close_device)((gx_device *)ctile->ttrans->pdev14);
974
0
                temp_device = (gx_device *)(ctile->ttrans->pdev14);
975
0
                gx_device_retain(temp_device, false);
976
0
                rc_decrement(temp_device,"gx_pattern_cache_free_entry");
977
0
                ctile->ttrans->pdev14 = NULL;
978
0
                ctile->ttrans->transbytes = NULL;  /* should be ok due to pdf14_close */
979
0
                ctile->ttrans->fill_trans_buffer = NULL; /* This is always freed */
980
0
            }
981
982
94.5k
            gs_free_object(mem, ctile->ttrans,
983
94.5k
                           "free_pattern_cache_entry(ttrans)");
984
94.5k
            ctile->ttrans = NULL;
985
986
94.5k
        }
987
988
272k
        pcache->tiles_used--;
989
272k
        pcache->bits_used -= ctile->bits_used;
990
272k
        ctile->id = gx_no_bitmap_id;
991
272k
    }
992
1.51M
}
993
994
/*
995
    Historically, the pattern cache has used a very simple hashing
996
    scheme whereby pattern A goes into slot idx = (A.id % num_tiles).
997
    Unfortunately, now we allow tiles to be 'locked' into the
998
    pattern cache, we might run into the case where we want both
999
    tiles A and B to be in the cache at once where:
1000
      (A.id % num_tiles) == (B.id % num_tiles).
1001
1002
    We have a maximum of 2 locked tiles, and one of those can be
1003
    placed while the other one is locked. So we only need to cope
1004
    with a single 'collision'.
1005
1006
    We therefore allow tiles to either go in at idx or at
1007
    (idx + 1) % num_tiles. This means we need to be prepared to
1008
    search a bit further for them, hence we now have 2 helper
1009
    functions to do this.
1010
*/
1011
1012
/* We can have at most 1 locked tile while looking for a place to
1013
 * put another tile. */
1014
gx_color_tile *
1015
gx_pattern_cache_find_tile_for_id(gx_pattern_cache *pcache, gs_id id)
1016
2.36M
{
1017
2.36M
    gx_color_tile *ctile  = &pcache->tiles[id % pcache->num_tiles];
1018
2.36M
    gx_color_tile *ctile2 = &pcache->tiles[(id+1) % pcache->num_tiles];
1019
2.36M
    if (ctile->id == id || ctile->id == gs_no_id)
1020
2.35M
        return ctile;
1021
16.3k
    if (ctile2->id == id || ctile2->id == gs_no_id)
1022
14.1k
        return ctile2;
1023
2.22k
    if (!ctile->is_locked)
1024
2.22k
        return ctile;
1025
0
    return ctile2;
1026
2.22k
}
1027
1028
1029
/* Given the size of a new pattern tile, free entries from the cache until  */
1030
/* enough space is available (or nothing left to free).                     */
1031
/* This will allow 1 oversized entry                                        */
1032
void
1033
gx_pattern_cache_ensure_space(gs_gstate * pgs, size_t needed)
1034
272k
{
1035
272k
    int code = ensure_pattern_cache(pgs);
1036
272k
    gx_pattern_cache *pcache;
1037
272k
    int start_free_id;
1038
1039
272k
    if (code < 0)
1040
0
        return;                 /* no cache -- just exit */
1041
1042
272k
    pcache = pgs->pattern_cache;
1043
272k
    start_free_id = pcache->next; /* for scan wrap check */
1044
    /* If too large then start freeing entries */
1045
    /* By starting just after 'next', we attempt to first free the oldest entries */
1046
1.31M
    while (pcache->bits_used + needed > pcache->max_bits &&
1047
1.31M
           pcache->bits_used != 0) {
1048
1.03M
        pcache->next = (pcache->next + 1) % pcache->num_tiles;
1049
1.03M
        gx_pattern_cache_free_entry(pcache, &pcache->tiles[pcache->next], false);
1050
        /* since a pattern may be temporarily locked (stroke pattern for fill_stroke_path) */
1051
        /* we may not have freed all entries even though we've scanned the entire cache.   */
1052
        /* The following check for wrapping prevents infinite loop if stroke pattern was   */
1053
        /* larger than pcache->max_bits,                                                   */
1054
1.03M
        if (pcache->next == start_free_id)
1055
965
            break;   /* we wrapped -- cache may not be empty */
1056
1.03M
    }
1057
272k
}
1058
1059
/* Export updating the pattern_cache bits_used and tiles_used for clist reading */
1060
void
1061
gx_pattern_cache_update_used(gs_gstate *pgs, size_t used)
1062
266k
{
1063
266k
    gx_pattern_cache *pcache = pgs->pattern_cache;
1064
1065
266k
    pcache->bits_used += used;
1066
266k
    pcache->tiles_used++;
1067
266k
}
1068
1069
/*
1070
 * Add a Pattern cache entry.  This is exported for the interpreter.
1071
 * Note that this does not free any of the data in the accumulator
1072
 * device, but it may zero out the bitmap_memory pointers to prevent
1073
 * the accumulated bitmaps from being freed when the device is closed.
1074
 */
1075
static void make_bitmap(gx_strip_bitmap *, const gx_device_memory *, gx_bitmap_id, const gs_memory_t *);
1076
int
1077
gx_pattern_cache_add_entry(gs_gstate * pgs,
1078
                   gx_device_forward * fdev, gx_color_tile ** pctile)
1079
22.1k
{
1080
22.1k
    gx_pattern_cache *pcache;
1081
22.1k
    const gs_pattern1_instance_t *pinst;
1082
22.1k
    size_t used = 0, mask_used = 0, trans_used = 0;
1083
22.1k
    gx_bitmap_id id;
1084
22.1k
    gx_color_tile *ctile;
1085
22.1k
    int code = ensure_pattern_cache(pgs);
1086
22.1k
    gx_device_memory *mmask = NULL;
1087
22.1k
    gx_device_memory *mbits = NULL;
1088
22.1k
    gx_pattern_trans_t *trans = NULL;
1089
22.1k
    int size_b, size_c;
1090
1091
22.1k
    if (code < 0)
1092
0
        return code;
1093
22.1k
    pcache = pgs->pattern_cache;
1094
1095
22.1k
    if (dev_proc(fdev, open_device) != pattern_clist_open_device) {
1096
19.6k
        gx_device_pattern_accum *padev = (gx_device_pattern_accum *)fdev;
1097
1098
19.6k
        mbits = padev->bits;
1099
19.6k
        mmask = padev->mask;
1100
19.6k
        pinst = padev->instance;
1101
19.6k
        trans = padev->transbuff;
1102
1103
        /*
1104
         * Check whether the pattern completely fills its box.
1105
         * If so, we can avoid the expensive masking operations
1106
         * when using the pattern.
1107
         */
1108
        /* Bug 700624: In cases where the mask is completely full,
1109
         * but the pattern cells are separated from one another,
1110
         * we need to leave gaps between the cells when rendering
1111
         * them. Sadly, the graphics library can't cope with this
1112
         * in the no-mask case. Therefore, only do the optimisation
1113
         * of not sending the mask if the step matrix is suitable.
1114
         *
1115
         * To do this, we compare the step matrix to the size. My
1116
         * belief is that the mask will only ever be full if it's
1117
         * orthogonal, cos otherwise the edges will be clipped,
1118
         * hence we lose no generality by checking for .xy and .yx
1119
         * being 0.
1120
         */
1121
19.6k
        if (mmask != 0 &&
1122
19.6k
            fabsf(pinst->step_matrix.xx) <= pinst->size.x &&
1123
19.6k
            fabsf(pinst->step_matrix.yy) <= pinst->size.y &&
1124
19.6k
            pinst->step_matrix.xy == 0 &&
1125
19.6k
            pinst->step_matrix.yx == 0) {
1126
16.5k
            int y;
1127
16.5k
            int w_less_8 = mmask->width-8;
1128
1129
36.0k
            for (y = 0; y < mmask->height; y++) {
1130
35.9k
                const byte *row = scan_line_base(mmask, y);
1131
35.9k
                int w;
1132
1133
1.54M
                for (w = w_less_8; w > 0; w -= 8)
1134
1.52M
                    if (*row++ != 0xff)
1135
16.3k
                        goto keep;
1136
19.5k
                w += 8;
1137
19.5k
                if ((*row | (0xff >> w)) != 0xff)
1138
33
                    goto keep;
1139
19.5k
            }
1140
            /* We don't need a mask. */
1141
172
            mmask = 0;
1142
16.5k
          keep:;
1143
16.5k
        }
1144
        /* Need to get size of buffers that are being added to the cache */
1145
19.6k
        if (mbits != 0)
1146
3.98k
            gdev_mem_bitmap_size(mbits, &used);
1147
19.6k
        if (mmask != 0) {
1148
19.5k
            gdev_mem_bitmap_size(mmask, &mask_used);
1149
19.5k
            used += mask_used;
1150
19.5k
        }
1151
19.6k
        if (trans != 0) {
1152
15.5k
            trans_used = (size_t)trans->planestride*trans->n_chan;
1153
15.5k
            used += trans_used;
1154
15.5k
        }
1155
19.6k
    } else {
1156
2.46k
        gx_device_clist *cdev = (gx_device_clist *)fdev;
1157
2.46k
        gx_device_clist_writer * cldev = (gx_device_clist_writer *)cdev;
1158
1159
2.46k
        code = clist_end_page(cldev);
1160
2.46k
        if (code < 0)
1161
0
            return code;
1162
2.46k
        pinst = cdev->writer.pinst;
1163
2.46k
        size_b = clist_data_size(cdev, 0);
1164
2.46k
        if (size_b < 0)
1165
0
            return_error(gs_error_unregistered);
1166
2.46k
        size_c = clist_data_size(cdev, 1);
1167
2.46k
        if (size_c < 0)
1168
0
            return_error(gs_error_unregistered);
1169
        /* The memfile size is the size, not the size determined by the depth*width*height */
1170
2.46k
        used = size_b + size_c;
1171
2.46k
    }
1172
22.1k
    id = pinst->id;
1173
22.1k
    ctile = gx_pattern_cache_find_tile_for_id(pcache, id);
1174
22.1k
    gx_pattern_cache_free_entry(pcache, ctile, false);         /* ensure that this cache slot is empty */
1175
22.1k
    ctile->id = id;
1176
22.1k
    ctile->num_planar_planes = pinst->num_planar_planes;
1177
22.1k
    ctile->depth = fdev->color_info.depth;
1178
22.1k
    ctile->uid = pinst->templat.uid;
1179
22.1k
    ctile->tiling_type = pinst->templat.TilingType;
1180
22.1k
    ctile->step_matrix = pinst->step_matrix;
1181
22.1k
    ctile->bbox = pinst->bbox;
1182
22.1k
    ctile->is_simple = pinst->is_simple;
1183
22.1k
    ctile->has_overlap = pinst->has_overlap;
1184
22.1k
    ctile->is_dummy = false;
1185
22.1k
    ctile->is_locked = false;
1186
22.1k
    ctile->blending_mode = 0;
1187
22.1k
    ctile->trans_group_popped = false;
1188
22.1k
    if (dev_proc(fdev, open_device) != pattern_clist_open_device) {
1189
19.6k
        if (mbits != 0) {
1190
3.98k
            make_bitmap(&ctile->tbits, mbits, gs_next_ids(pgs->memory, 1), pgs->memory);
1191
3.98k
            mbits->bitmap_memory = 0;   /* don't free the bits */
1192
3.98k
        } else
1193
15.6k
            ctile->tbits.data = 0;
1194
19.6k
        if (mmask != 0) {
1195
19.5k
            make_bitmap(&ctile->tmask, mmask, id, pgs->memory);
1196
19.5k
            mmask->bitmap_memory = 0;   /* don't free the bits */
1197
19.5k
        } else
1198
172
            ctile->tmask.data = 0;
1199
19.6k
        if (trans != 0) {
1200
15.5k
            if_debug2m('v', pgs->memory,
1201
15.5k
                       "[v*] Adding trans pattern to cache, uid = %ld id = %ld\n",
1202
15.5k
                       ctile->uid.id, ctile->id);
1203
15.5k
            ctile->ttrans = trans;
1204
15.5k
        }
1205
1206
19.6k
        ctile->cdev = NULL;
1207
19.6k
    } else {
1208
2.46k
        gx_device_clist *cdev = (gx_device_clist *)fdev;
1209
2.46k
        gx_device_clist_writer *cwdev = (gx_device_clist_writer *)fdev;
1210
1211
2.46k
        ctile->tbits.data = 0;
1212
2.46k
        ctile->tbits.size.x = 0;
1213
2.46k
        ctile->tbits.size.y = 0;
1214
2.46k
        ctile->tmask.data = 0;
1215
2.46k
        ctile->tmask.size.x = 0;
1216
2.46k
        ctile->tmask.size.y = 0;
1217
2.46k
        ctile->cdev = cdev;
1218
        /* Prevent freeing files on pattern_paint_cleanup : */
1219
2.46k
        cwdev->do_not_open_or_close_bandfiles = true;
1220
2.46k
    }
1221
    /* In the clist case, used is accurate. In the non-clist case, it may
1222
     * not be. The important thing is that we account the same for tiles
1223
     * going in and coming out of the cache. Therefore we store the used
1224
     * figure in the tile so we always remove the same amount. */
1225
22.1k
    ctile->bits_used = used;
1226
22.1k
    gx_pattern_cache_update_used(pgs, used);
1227
1228
22.1k
    *pctile = ctile;
1229
22.1k
    return 0;
1230
22.1k
}
1231
1232
/* set or clear the 'is_locked' flag for a tile in the cache. Used by */
1233
/* fill_stroke_path to make sure a large stroke pattern stays in the  */
1234
/* cache even if the fill is also a pattern.        */
1235
int
1236
gx_pattern_cache_entry_set_lock(gs_gstate *pgs, gs_id id, bool new_lock_value)
1237
2.54k
{
1238
2.54k
    gx_color_tile *ctile;
1239
2.54k
    int code = ensure_pattern_cache(pgs);
1240
1241
2.54k
    if (code < 0)
1242
0
        return code;
1243
2.54k
    ctile = gx_pattern_cache_find_tile_for_id(pgs->pattern_cache, id);
1244
2.54k
    if (ctile == NULL)
1245
0
        return_error(gs_error_undefined);
1246
2.54k
    ctile->is_locked = new_lock_value;
1247
2.54k
    return 0;
1248
2.54k
}
1249
1250
/* Get entry for reading a pattern from clist. */
1251
int
1252
gx_pattern_cache_get_entry(gs_gstate * pgs, gs_id id, gx_color_tile ** pctile)
1253
244k
{
1254
244k
    gx_pattern_cache *pcache;
1255
244k
    gx_color_tile *ctile;
1256
244k
    int code = ensure_pattern_cache(pgs);
1257
1258
244k
    if (code < 0)
1259
0
        return code;
1260
244k
    pcache = pgs->pattern_cache;
1261
244k
    ctile = gx_pattern_cache_find_tile_for_id(pcache, id);
1262
244k
    gx_pattern_cache_free_entry(pgs->pattern_cache, ctile, false);
1263
244k
    ctile->id = id;
1264
244k
    *pctile = ctile;
1265
244k
    return 0;
1266
244k
}
1267
1268
bool
1269
gx_pattern_tile_is_clist(gx_color_tile *ptile)
1270
789k
{
1271
789k
    return ptile != NULL && ptile->cdev != NULL;
1272
789k
}
1273
1274
/* Add a dummy Pattern cache entry.  Stubs a pattern tile for interpreter when
1275
   device handles high level patterns. */
1276
int
1277
gx_pattern_cache_add_dummy_entry(gs_gstate *pgs,
1278
            gs_pattern1_instance_t *pinst, int depth)
1279
5.98k
{
1280
5.98k
    gx_color_tile *ctile;
1281
5.98k
    gx_pattern_cache *pcache;
1282
5.98k
    gx_bitmap_id id = pinst->id;
1283
5.98k
    int code = ensure_pattern_cache(pgs);
1284
1285
5.98k
    if (code < 0)
1286
0
        return code;
1287
5.98k
    pcache = pgs->pattern_cache;
1288
5.98k
    ctile = gx_pattern_cache_find_tile_for_id(pcache, id);
1289
5.98k
    gx_pattern_cache_free_entry(pcache, ctile, false);
1290
5.98k
    ctile->id = id;
1291
5.98k
    ctile->depth = depth;
1292
5.98k
    ctile->uid = pinst->templat.uid;
1293
5.98k
    ctile->tiling_type = pinst->templat.TilingType;
1294
5.98k
    ctile->step_matrix = pinst->step_matrix;
1295
5.98k
    ctile->bbox = pinst->bbox;
1296
5.98k
    ctile->is_simple = pinst->is_simple;
1297
5.98k
    ctile->has_overlap = pinst->has_overlap;
1298
5.98k
    ctile->is_dummy = true;
1299
5.98k
    ctile->is_locked = false;
1300
5.98k
    memset(&ctile->tbits, 0 , sizeof(ctile->tbits));
1301
5.98k
    ctile->tbits.size = pinst->size;
1302
5.98k
    ctile->tbits.id = gs_no_bitmap_id;
1303
5.98k
    memset(&ctile->tmask, 0 , sizeof(ctile->tmask));
1304
5.98k
    ctile->cdev = NULL;
1305
5.98k
    ctile->ttrans = NULL;
1306
5.98k
    ctile->bits_used = 0;
1307
5.98k
    pcache->tiles_used++;
1308
5.98k
    return 0;
1309
5.98k
}
1310
1311
#if RAW_PATTERN_DUMP
1312
/* Debug dump of pattern image data. Saved in
1313
   interleaved form with global indexing in
1314
   file name */
1315
static void
1316
dump_raw_pattern(int height, int width, int n_chan, int depth,
1317
                byte *Buffer, int raster, const gx_device_memory * mdev,
1318
                const gs_memory_t *memory)
1319
{
1320
    char full_file_name[50];
1321
    gp_file *fid;
1322
    int max_bands;
1323
    int j, k, m;
1324
    int byte_number, bit_position;
1325
    unsigned char current_byte;
1326
    unsigned char output_val;
1327
    bool is_planar;
1328
    byte *curr_ptr = Buffer;
1329
    int plane_offset;
1330
1331
    is_planar = mdev->num_planar_planes > 0;
1332
    max_bands = ( n_chan < 57 ? n_chan : 56);   /* Photoshop handles at most 56 bands */
1333
    if (is_planar) {
1334
        gs_snprintf(full_file_name, sizeof(full_file_name), "%d)PATTERN_PLANE_%dx%dx%d.raw", global_pat_index,
1335
                mdev->raster, height, max_bands);
1336
    } else {
1337
        gs_snprintf(full_file_name, sizeof(full_file_name), "%d)PATTERN_CHUNK_%dx%dx%d.raw", global_pat_index,
1338
                width, height, max_bands);
1339
    }
1340
    fid = gp_fopen(memory,full_file_name,"wb");
1341
    if (depth >= 8) {
1342
        /* Contone data. */
1343
        if (is_planar) {
1344
            for (m = 0; m < max_bands; m++) {
1345
                curr_ptr = mdev->line_ptrs[m*mdev->height];
1346
                gp_fwrite(curr_ptr, 1, mdev->height * mdev->raster, fid);
1347
            }
1348
        } else {
1349
            /* Just dump it like it is */
1350
            gp_fwrite(Buffer, 1, max_bands * height * width, fid);
1351
        }
1352
    } else {
1353
        /* Binary Data. Lets get to 8 bit for debugging.  We have to
1354
           worry about planar vs. chunky.  Note this assumes 1 bit data
1355
           only. */
1356
        if (is_planar) {
1357
            plane_offset = mdev->raster * mdev->height;
1358
            for (m = 0; m < max_bands; m++) {
1359
                curr_ptr = mdev->line_ptrs[m*mdev->height];
1360
                for (j = 0; j < height; j++) {
1361
                    for (k = 0; k < width; k++) {
1362
                        byte_number = (int) ceil((( (float) k + 1.0) / 8.0)) - 1;
1363
                        current_byte = curr_ptr[j*(mdev->raster) + byte_number];
1364
                        bit_position = 7 - (k -  byte_number*8);
1365
                        output_val = ((current_byte >> bit_position) & 0x1) * 255;
1366
                        gp_fwrite(&output_val,1,1,fid);
1367
                    }
1368
                }
1369
            }
1370
        } else {
1371
            for (j = 0; j < height; j++) {
1372
                for (k = 0; k < width; k++) {
1373
                    for (m = 0; m < max_bands; m++) {
1374
                        /* index current byte */
1375
                        byte_number =
1376
                            (int) ceil((( (float) k * (float) max_bands +
1377
                                          (float) m + 1.0) / 8.0)) - 1;
1378
                        /* get byte of interest */
1379
                        current_byte =
1380
                                curr_ptr[j*(mdev->raster) + byte_number];
1381
                        /* get bit position */
1382
                        bit_position =
1383
                                7 - (k * max_bands + m -  byte_number * 8);
1384
                        /* extract and create byte */
1385
                        output_val =
1386
                                ((current_byte >> bit_position) & 0x1) * 255;
1387
                        gp_fwrite(&output_val,1,1,fid);
1388
                    }
1389
                }
1390
            }
1391
        }
1392
    }
1393
    gp_fclose(fid);
1394
}
1395
#endif
1396
1397
static void
1398
make_bitmap(register gx_strip_bitmap * pbm, const gx_device_memory * mdev,
1399
            gx_bitmap_id id, const gs_memory_t *memory)
1400
23.4k
{
1401
23.4k
    pbm->data = mdev->base;
1402
23.4k
    pbm->raster = mdev->raster;
1403
23.4k
    pbm->rep_width = pbm->size.x = mdev->width;
1404
23.4k
    pbm->rep_height = pbm->size.y = mdev->height;
1405
23.4k
    pbm->id = id;
1406
23.4k
    pbm->rep_shift = pbm->shift = 0;
1407
23.4k
    pbm->num_planes = mdev->num_planar_planes ? mdev->num_planar_planes : 1;
1408
1409
        /* Lets dump this for debug purposes */
1410
1411
#if RAW_PATTERN_DUMP
1412
    dump_raw_pattern(pbm->rep_height, pbm->rep_width,
1413
                        mdev->color_info.num_components,
1414
                        mdev->color_info.depth,
1415
                        (unsigned char*) mdev->base,
1416
                        pbm->raster, mdev, memory);
1417
1418
        global_pat_index++;
1419
1420
#endif
1421
1422
23.4k
}
1423
1424
/* Purge selected entries from the pattern cache. */
1425
void
1426
gx_pattern_cache_winnow(gx_pattern_cache * pcache,
1427
  bool(*proc) (gx_color_tile * ctile, void *proc_data), void *proc_data)
1428
1.25M
{
1429
1.25M
    uint i;
1430
1431
1.25M
    if (pcache == 0)            /* no cache created yet */
1432
0
        return;
1433
63.9M
    for (i = 0; i < pcache->num_tiles; ++i) {
1434
62.7M
        gx_color_tile *ctile = &pcache->tiles[i];
1435
1436
62.7M
        ctile->is_locked = false;   /* force freeing */
1437
62.7M
        if (ctile->id != gx_no_bitmap_id && (*proc) (ctile, proc_data))
1438
182k
            gx_pattern_cache_free_entry(pcache, ctile, false);
1439
62.7M
    }
1440
1.25M
}
1441
1442
void
1443
gx_pattern_cache_flush(gx_pattern_cache * pcache)
1444
103k
{
1445
103k
    uint i;
1446
1447
103k
    if (pcache == 0)            /* no cache created yet */
1448
0
        return;
1449
5.27M
    for (i = 0; i < pcache->num_tiles; ++i) {
1450
5.16M
        gx_color_tile *ctile = &pcache->tiles[i];
1451
1452
5.16M
        ctile->is_locked = false;   /* force freeing */
1453
5.16M
        if (ctile->id != gx_no_bitmap_id)
1454
23.1k
            gx_pattern_cache_free_entry(pcache, ctile, true);
1455
5.16M
    }
1456
103k
}
1457
1458
/* blank the pattern accumulator device assumed to be in the graphics
1459
   state */
1460
int
1461
gx_erase_colored_pattern(gs_gstate *pgs)
1462
3.98k
{
1463
3.98k
    int code;
1464
3.98k
    gx_device_pattern_accum *pdev = (gx_device_pattern_accum *)gs_currentdevice(pgs);
1465
1466
3.98k
    if ((code = gs_gsave(pgs)) < 0)
1467
0
        return code;
1468
3.98k
    if ((code = gs_setgray(pgs, 1.0)) >= 0) {
1469
3.98k
        gs_rect rect;
1470
3.98k
        gx_device_memory *mask;
1471
3.98k
        static const gs_matrix identity = { 1, 0, 0, 1, 0, 0 };
1472
1473
3.98k
        pgs->log_op = lop_default;
1474
3.98k
        rect.p.x = 0.0;
1475
3.98k
        rect.p.y = 0.0;
1476
3.98k
        rect.q.x = (double)pdev->width;
1477
3.98k
        rect.q.y = (double)pdev->height;
1478
1479
3.98k
        code = gs_setmatrix(pgs, &identity);
1480
3.98k
        if (code < 0) {
1481
0
            gs_grestore_only(pgs);
1482
0
            return code;
1483
0
        }
1484
        /* we don't want the fill rectangle device call to use the
1485
           mask */
1486
3.98k
        mask = pdev->mask;
1487
3.98k
        pdev->mask = NULL;
1488
3.98k
        code = gs_rectfill(pgs, &rect, 1);
1489
        /* restore the mask */
1490
3.98k
        pdev->mask = mask;
1491
3.98k
        if (code < 0) {
1492
0
            gs_grestore_only(pgs);
1493
0
            return code;
1494
0
        }
1495
3.98k
    }
1496
    /* we don't need wraparound here */
1497
3.98k
    gs_grestore_only(pgs);
1498
3.98k
    return code;
1499
3.98k
}
1500
1501
/* Reload a (non-null) Pattern color into the cache. */
1502
/* *pdc is already set, except for colors.pattern.p_tile and mask.m_tile. */
1503
int
1504
gx_pattern_load(gx_device_color * pdc, const gs_gstate * pgs,
1505
                gx_device * dev, gs_color_select_t select)
1506
29.4k
{
1507
29.4k
    gx_device_forward *adev = NULL;
1508
29.4k
    gs_pattern1_instance_t *pinst =
1509
29.4k
        (gs_pattern1_instance_t *)pdc->ccolor.pattern;
1510
29.4k
    gs_gstate *saved;
1511
29.4k
    gx_color_tile *ctile;
1512
29.4k
    gs_memory_t *mem = pgs->memory;
1513
29.4k
    bool has_tags = device_encodes_tags(dev);
1514
29.4k
    int code;
1515
1516
29.4k
    if (pgs->pattern_cache == NULL)
1517
0
        if ((code = ensure_pattern_cache((gs_gstate *) pgs))< 0)      /* break const for call */
1518
0
            return code;
1519
1520
29.4k
    if (gx_pattern_cache_lookup(pdc, pgs, dev, select))
1521
1.25k
        return 0;
1522
1523
    /* Get enough space in the cache for this pattern (estimated if it is a clist) */
1524
28.1k
    gx_pattern_cache_ensure_space((gs_gstate *)pgs, gx_pattern_size_estimate(pinst, has_tags));
1525
    /*
1526
     * Note that adev is an internal device, so it will be freed when the
1527
     * last reference to it from a graphics state is deleted.
1528
     */
1529
28.1k
    adev = gx_pattern_accum_alloc(mem, pgs->pattern_cache->memory, pinst, "gx_pattern_load");
1530
28.1k
    if (adev == 0)
1531
0
        return_error(gs_error_VMerror);
1532
28.1k
    gx_device_set_target((gx_device_forward *)adev, dev);
1533
28.1k
    code = dev_proc(adev, open_device)((gx_device *)adev);
1534
28.1k
    if (code < 0) {
1535
0
        gs_free_object(mem, adev, "gx_pattern_load");
1536
0
        return code;
1537
0
    }
1538
28.1k
    saved = gs_gstate_copy(pinst->saved, pinst->saved->memory);
1539
28.1k
    if (saved == 0) {
1540
0
        code = gs_note_error(gs_error_VMerror);
1541
0
        goto fail;
1542
0
    }
1543
28.1k
    if (saved->pattern_cache == 0)
1544
0
        saved->pattern_cache = pgs->pattern_cache;
1545
28.1k
    code = gs_setdevice_no_init(saved, (gx_device *)adev);
1546
28.1k
    if (code < 0)
1547
0
        goto fail;
1548
28.1k
    if (pinst->templat.uses_transparency) {
1549
19.4k
        if_debug1m('v', mem, "gx_pattern_load: pushing the pdf14 compositor device into this graphics state pat_id = %ld\n", pinst->id);
1550
19.4k
        if ((code = gs_push_pdf14trans_device(saved, true, false, 0, 0)) < 0)   /* spot_color_count taken from pdf14 target values */
1551
50
            goto fail;
1552
19.4k
        saved->device->is_open = true;
1553
19.4k
    } else {
1554
        /* For colored patterns we clear the pattern device's
1555
           background.  This is necessary for the anti aliasing code
1556
           and (unfortunately) it masks a difficult to fix UMR
1557
           affecting pcl patterns, see bug #690487.  Note we have to
1558
           make a similar change in zpcolor.c where much of this
1559
           pattern code is duplicated to support high level stream
1560
           patterns. */
1561
8.71k
        if (pinst->templat.PaintType == 1 && !(pinst->is_clist)
1562
8.71k
            && dev_proc(pinst->saved->device, dev_spec_op)(pinst->saved->device, gxdso_pattern_can_accum, NULL, 0) == 0)
1563
3.98k
            if ((code = gx_erase_colored_pattern(saved)) < 0)
1564
0
                goto fail;
1565
8.71k
    }
1566
1567
28.1k
    code = (*pinst->templat.PaintProc)(&pdc->ccolor, saved);
1568
28.1k
    if (code < 0) {
1569
5.98k
        if (dev_proc(adev, open_device) == pattern_accum_open) {
1570
            /* free pattern cache data that never got added to the dictionary */
1571
3.53k
            gx_device_pattern_accum *padev = (gx_device_pattern_accum *) adev;
1572
3.53k
            if ((padev->bits != NULL) && (padev->bits->base != NULL)) {
1573
403
                gs_free_object(padev->bits->memory, padev->bits->base, "mem_open");
1574
403
            }
1575
3.53k
        }
1576
        /* RJW: At this point, in the non transparency case,
1577
         * saved->device == adev. So unretain it, close it, and the
1578
         * gs_gstate_free(saved) will remove it. In the transparency case,
1579
         * saved->device = the pdf14 device. So we need to unretain it,
1580
         * close adev, and finally close saved->device.
1581
         */
1582
5.98k
        gx_device_retain(saved->device, false);         /* device no longer retained */
1583
5.98k
        if (pinst->templat.uses_transparency) {
1584
3.55k
            if (pinst->is_clist == 0) {
1585
3.08k
                gs_free_object(((gx_device_pattern_accum *)adev)->bitmap_memory,
1586
3.08k
                               ((gx_device_pattern_accum *)adev)->transbuff,
1587
3.08k
                               "gx_pattern_load");
1588
3.08k
                ((gx_device_pattern_accum *)adev)->transbuff = NULL;
1589
3.08k
            }
1590
3.55k
            dev_proc(adev, close_device)((gx_device *)adev);
1591
            /* adev was the target of the pdf14 device, so also is no longer retained */
1592
3.55k
            gx_device_retain((gx_device *)adev, false);         /* device no longer retained */
1593
3.55k
        }
1594
5.98k
        dev_proc(saved->device, close_device)((gx_device *)saved->device);
1595
        /* Freeing the state should now free the device which may be the pdf14 compositor. */
1596
5.98k
        gs_gstate_free_chain(saved);
1597
5.98k
        if (code == gs_error_handled)
1598
5.98k
            code = 0;
1599
5.98k
        return code;
1600
5.98k
    }
1601
22.1k
    if (pinst->templat.uses_transparency) {
1602
        /* if_debug0m('v', saved->memory, "gx_pattern_load: popping the pdf14 compositor device from this graphics state\n");
1603
        if ((code = gs_pop_pdf14trans_device(saved, true)) < 0)
1604
            return code; */
1605
15.8k
            if (pinst->is_clist) {
1606
                /* Send the compositor command to close the PDF14 device */
1607
321
                code = gs_pop_pdf14trans_device(saved, true);
1608
321
                if (code < 0)
1609
0
                    goto fail;
1610
15.5k
            } else {
1611
                /* Not a clist, get PDF14 buffer information */
1612
15.5k
                code =
1613
15.5k
                    pdf14_get_buffer_information(saved->device,
1614
15.5k
                                                ((gx_device_pattern_accum*)adev)->transbuff,
1615
15.5k
                                                 saved->memory,
1616
15.5k
                                                 true);
1617
                /* PDF14 device (and buffer) is destroyed when pattern cache
1618
                   entry is removed */
1619
15.5k
                if (code < 0)
1620
0
                    goto fail;
1621
15.5k
            }
1622
15.8k
    }
1623
    /* We REALLY don't like the following cast.... */
1624
22.1k
    code = gx_pattern_cache_add_entry((gs_gstate *)pgs,
1625
22.1k
                adev, &ctile);
1626
22.1k
    if (code >= 0) {
1627
22.1k
        if (!gx_pattern_cache_lookup(pdc, pgs, dev, select)) {
1628
0
            mlprintf(mem, "Pattern cache lookup failed after insertion!\n");
1629
0
            code = gs_note_error(gs_error_Fatal);
1630
0
        }
1631
22.1k
    }
1632
#ifdef DEBUG
1633
    if (gs_debug_c('B') && dev_proc(adev, open_device) == pattern_accum_open) {
1634
        gx_device_pattern_accum *pdev = (gx_device_pattern_accum *)adev;
1635
1636
        if (pdev->mask)
1637
            debug_dump_bitmap(pdev->memory,
1638
                              pdev->mask->base, pdev->mask->raster,
1639
                              pdev->mask->height, "[B]Pattern mask");
1640
        if (pdev->bits)
1641
            debug_dump_bitmap(pdev->memory,
1642
                              ((gx_device_memory *) pdev->target)->base,
1643
                              ((gx_device_memory *) pdev->target)->raster,
1644
                              pdev->target->height, "[B]Pattern bits");
1645
    }
1646
#endif
1647
    /* Free the bookkeeping structures, except for the bits and mask */
1648
    /* data iff they are still needed. */
1649
22.1k
    dev_proc(adev, close_device)((gx_device *)adev);
1650
    /* Free the chain of gstates. Freeing the state will free the device. */
1651
22.1k
    gs_gstate_free_chain(saved);
1652
22.1k
    return code;
1653
1654
50
fail:
1655
50
    if (dev_proc(adev, open_device) == pattern_accum_open) {
1656
        /* free pattern cache data that never got added to the dictionary */
1657
50
        gx_device_pattern_accum *padev = (gx_device_pattern_accum *) adev;
1658
50
        if ((padev->bits != NULL) && (padev->bits->base != NULL)) {
1659
0
            gs_free_object(padev->bits->memory, padev->bits->base, "mem_open");
1660
0
        }
1661
50
    }
1662
50
    if (dev_proc(adev, open_device) == pattern_clist_open_device) {
1663
0
        gx_device_clist *cdev = (gx_device_clist *)adev;
1664
1665
0
        gs_free_object(cdev->writer.bandlist_memory, cdev->common.data, "gx_pattern_load");
1666
0
        cdev->common.data = 0;
1667
0
    }
1668
50
    dev_proc(adev, close_device)((gx_device *)adev);
1669
50
    gx_device_set_target(adev, NULL);
1670
50
    gx_device_retain((gx_device *)adev, false);
1671
50
    gs_gstate_free_chain(saved);
1672
50
    return code;
1673
22.1k
}
1674
1675
/* Remap a PatternType 1 color. */
1676
cs_proc_remap_color(gx_remap_Pattern);  /* check the prototype */
1677
int
1678
gs_pattern1_remap_color(const gs_client_color * pc, const gs_color_space * pcs,
1679
                        gx_device_color * pdc, const gs_gstate * pgs,
1680
                        gx_device * dev, gs_color_select_t select)
1681
29.6k
{
1682
29.6k
    gs_pattern1_instance_t *pinst = (gs_pattern1_instance_t *)pc->pattern;
1683
29.6k
    int code;
1684
1685
    /* Save original color space and color info into dev color */
1686
29.6k
    pdc->ccolor = *pc;
1687
29.6k
    pdc->ccolor_valid = true;
1688
29.6k
    if (pinst == 0) {
1689
        /* Null pattern */
1690
0
        color_set_null_pattern(pdc);
1691
0
        return 0;
1692
0
    }
1693
29.6k
    if (pinst->templat.PaintType == 2) {       /* uncolored */
1694
441
        if (pcs->base_space) {
1695
238
            if (dev->icc_struct != NULL && dev->icc_struct->blackvector) {
1696
0
                gs_client_color temppc;
1697
0
                gs_color_space *graycs = gs_cspace_new_DeviceGray(pgs->memory);
1698
1699
0
                if (graycs == NULL) {
1700
0
                    code = (pcs->base_space->type->remap_color)
1701
0
                        (pc, pcs->base_space, pdc, pgs, dev, select);
1702
0
                } else {
1703
0
                    if (gsicc_is_white_blacktextvec((gs_gstate*) pgs,
1704
0
                        dev, (gs_color_space*) pcs, (gs_client_color*) pc))
1705
0
                        temppc.paint.values[0] = 1.0;
1706
0
                    else
1707
0
                        temppc.paint.values[0] = 0.0;
1708
0
                    code = (graycs->type->remap_color)
1709
0
                        (&temppc, graycs, pdc, pgs, dev, select);
1710
0
                    rc_decrement_cs(graycs, "gs_pattern1_remap_color");
1711
0
                }
1712
238
            } else {
1713
238
                code = (pcs->base_space->type->remap_color)
1714
238
                    (pc, pcs->base_space, pdc, pgs, dev, select);
1715
238
            }
1716
238
        } else
1717
203
            code = gs_note_error(gs_error_unregistered);
1718
441
        if (code < 0)
1719
203
            return code;
1720
238
        if (pdc->type == gx_dc_type_pure)
1721
223
            pdc->type = &gx_dc_pure_masked;
1722
15
        else if (pdc->type == gx_dc_type_ht_binary)
1723
2
            pdc->type = &gx_dc_binary_masked;
1724
13
        else if (pdc->type == gx_dc_type_ht_colored)
1725
10
            pdc->type = &gx_dc_colored_masked;
1726
3
        else if (pdc->type == gx_dc_type_devn)
1727
3
            pdc->type = &gx_dc_devn_masked;
1728
0
        else
1729
0
            return_error(gs_error_unregistered);
1730
238
    } else
1731
29.1k
        color_set_null_pattern(pdc);
1732
29.4k
    pdc->mask.id = pinst->id;
1733
29.4k
    pdc->mask.m_tile = 0;
1734
29.4k
    return gx_pattern_load(pdc, pgs, dev, select);
1735
29.6k
}
1736
1737
int
1738
pattern_accum_dev_spec_op(gx_device *dev, int dso, void *data, int size)
1739
1.07M
{
1740
1.07M
    gx_device_pattern_accum *const padev = (gx_device_pattern_accum *)dev;
1741
1.07M
    const gs_pattern1_instance_t *pinst = padev->instance;
1742
1.07M
    gx_device *target =
1743
1.07M
        (padev->target == 0 ? gs_currentdevice(pinst->saved) :
1744
1.07M
         padev->target);
1745
1746
1.07M
    if (dso == gxdso_in_pattern_accumulator)
1747
471
        return (pinst->templat.PaintType == 2 ? 2 : 1);
1748
1.07M
    if (dso == gxdso_get_dev_param) {
1749
0
        dev_param_req_t *request = (dev_param_req_t *)data;
1750
0
        gs_param_list * plist = (gs_param_list *)request->list;
1751
0
        bool bool_true = 1;
1752
1753
0
        if (strcmp(request->Param, "NoInterpolateImagemasks") == 0) {
1754
0
            return param_write_bool(plist, "NoInterpolateImagemasks", &bool_true);
1755
0
        }
1756
0
    }
1757
    /* Bug 704670.  Pattern accumulator should not allow whatever targets
1758
       lie beneath it to do any bbox adjustments. If we are here, the
1759
       pattern accumulator is actually drawing into a buffer
1760
       and it is not accumulating into a clist device. In this case, if it
1761
       was a pattern clist, we would be going to the special op for the clist
1762
       device of the pattern, which will have the proper extent and adjust
1763
       the bbox.  Here we just need to clip to the buffer into which we are drawing */
1764
1.07M
    if (dso == gxdso_restrict_bbox) {
1765
0
        gs_int_rect* ibox = (gs_int_rect*)data;
1766
1767
0
        if (ibox->p.y < 0)
1768
0
            ibox->p.y = 0;
1769
0
        if (ibox->q.y > padev->height)
1770
0
            ibox->q.y = padev->height;
1771
0
        if (ibox->p.x < 0)
1772
0
            ibox->p.x = 0;
1773
0
        if (ibox->q.x > padev->width)
1774
0
            ibox->q.x = padev->width;
1775
0
        return 0;
1776
0
    }
1777
1778
1.07M
    return dev_proc(target, dev_spec_op)(target, dso, data, size);
1779
1.07M
}