Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxht.c
Line
Count
Source
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
/* Halftone rendering for imaging library */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gsstruct.h"
22
#include "gsbitops.h"
23
#include "gsutil.h"   /* for gs_next_ids */
24
#include "gxdcolor.h"
25
#include "gxfixed.h"
26
#include "gxdevice.h"   /* for gzht.h */
27
#include "gxgstate.h"
28
#include "gzht.h"
29
#include "gsserial.h"
30
31
/* Define the binary halftone device color type. */
32
/* The type descriptor must be public for Pattern types. */
33
gs_public_st_composite(st_dc_ht_binary, gx_device_color, "dc_ht_binary",
34
                       dc_ht_binary_enum_ptrs, dc_ht_binary_reloc_ptrs);
35
static dev_color_proc_save_dc(gx_dc_ht_binary_save_dc);
36
static dev_color_proc_get_dev_halftone(gx_dc_ht_binary_get_dev_halftone);
37
static dev_color_proc_load(gx_dc_ht_binary_load);
38
static dev_color_proc_fill_rectangle(gx_dc_ht_binary_fill_rectangle);
39
static dev_color_proc_fill_masked(gx_dc_ht_binary_fill_masked);
40
static dev_color_proc_equal(gx_dc_ht_binary_equal);
41
static dev_color_proc_write(gx_dc_ht_binary_write);
42
static dev_color_proc_read(gx_dc_ht_binary_read);
43
const gx_device_color_type_t
44
      gx_dc_type_data_ht_binary =
45
{&st_dc_ht_binary,
46
 gx_dc_ht_binary_save_dc, gx_dc_ht_binary_get_dev_halftone,
47
 gx_dc_ht_get_phase,
48
 gx_dc_ht_binary_load, gx_dc_ht_binary_fill_rectangle,
49
 gx_dc_ht_binary_fill_masked, gx_dc_ht_binary_equal,
50
 gx_dc_ht_binary_write, gx_dc_ht_binary_read,
51
 gx_dc_ht_binary_get_nonzero_comps
52
};
53
54
#undef gx_dc_type_ht_binary
55
const gx_device_color_type_t *const gx_dc_type_ht_binary =
56
&gx_dc_type_data_ht_binary;
57
58
5.22M
#define gx_dc_type_ht_binary (&gx_dc_type_data_ht_binary)
59
/* GC procedures */
60
static
61
240k
ENUM_PTRS_WITH(dc_ht_binary_enum_ptrs, gx_device_color *cptr) return 0;
62
80.3k
ENUM_PTR(0, gx_device_color, colors.binary.b_ht);
63
80.3k
case 1:
64
80.3k
{
65
80.3k
    gx_ht_tile *tile = cptr->colors.binary.b_tile;
66
67
80.3k
    ENUM_RETURN(tile ? tile - tile->index : 0);
68
0
}
69
240k
ENUM_PTRS_END
70
80.3k
static RELOC_PTRS_WITH(dc_ht_binary_reloc_ptrs, gx_device_color *cptr)
71
80.3k
{
72
80.3k
    gx_ht_tile *tile = cptr->colors.binary.b_tile;
73
80.3k
    uint index = tile ? tile->index : 0;
74
75
80.3k
    RELOC_PTR(gx_device_color, colors.binary.b_ht);
76
80.3k
    RELOC_TYPED_OFFSET_PTR(gx_device_color, colors.binary.b_tile, index);
77
80.3k
}
78
80.3k
RELOC_PTRS_END
79
#undef cptr
80
81
/* Other GC procedures */
82
private_st_ht_tiles();
83
static
84
ENUM_PTRS_BEGIN_PROC(ht_tiles_enum_ptrs)
85
2.13M
{
86
2.13M
    return 0;
87
2.13M
}
88
ENUM_PTRS_END_PROC
89
2.13M
static RELOC_PTRS_BEGIN(ht_tiles_reloc_ptrs)
90
2.13M
{
91
    /* Reset the bitmap pointers in the tiles. */
92
    /* We know the first tile points to the base of the bits. */
93
2.13M
    gx_ht_tile *ht_tiles = vptr;
94
2.13M
    byte *bits = ht_tiles->tiles.data;
95
2.13M
    uint diff;
96
97
2.13M
    if (bits == 0)
98
0
        return;
99
2.13M
    RELOC_VAR(bits);
100
2.13M
    if (size == size_of(gx_ht_tile)) { /* only 1 tile */
101
0
        ht_tiles->tiles.data = bits;
102
0
        return;
103
0
    }
104
2.13M
    diff = ht_tiles[1].tiles.data - ht_tiles[0].tiles.data;
105
1.85G
    for (; size; ht_tiles++, size -= size_of(gx_ht_tile), bits += diff) {
106
1.85G
        ht_tiles->tiles.data = bits;
107
1.85G
    }
108
2.13M
}
109
2.13M
RELOC_PTRS_END
110
private_st_ht_cache();
111
112
/* Return the default sizes of the halftone cache. */
113
uint
114
gx_ht_cache_default_tiles(void)
115
0
{
116
#ifdef DEBUG
117
    return (gs_debug_c('.') ? max_ht_cached_tiles_SMALL :
118
            max_ht_cached_tiles);
119
#else
120
0
    return max_ht_cached_tiles;
121
0
#endif
122
0
}
123
uint
124
gx_ht_cache_default_bits_size(void)
125
5.55M
{
126
#ifdef DEBUG
127
    return (gs_debug_c('.') ? max_ht_cache_bits_size_SMALL :
128
            max_ht_cache_bits_size);
129
#else
130
5.55M
    return max_ht_cache_bits_size;
131
5.55M
#endif
132
5.55M
}
133
134
/* Allocate a halftone cache. max_bits_size is number of bytes */
135
gx_ht_cache *
136
gx_ht_alloc_cache(gs_memory_t * mem, uint max_tiles, uint max_bits_size)
137
3.97M
{
138
3.97M
    gx_ht_cache *pcache =
139
3.97M
    gs_alloc_struct(mem, gx_ht_cache, &st_ht_cache,
140
3.97M
                    "alloc_ht_cache(struct)");
141
3.97M
    byte *tbits =
142
3.97M
        gs_alloc_bytes(mem, max_bits_size, "alloc_ht_cache(bits)");
143
3.97M
    gx_ht_tile *ht_tiles =
144
3.97M
        gs_alloc_struct_array(mem, max_tiles, gx_ht_tile, &st_ht_tiles,
145
3.97M
                              "alloc_ht_cache(ht_tiles)");
146
147
3.97M
    if (pcache == 0 || tbits == 0 || ht_tiles == 0) {
148
9
        gs_free_object(mem, ht_tiles, "alloc_ht_cache(ht_tiles)");
149
9
        gs_free_object(mem, tbits, "alloc_ht_cache(bits)");
150
9
        gs_free_object(mem, pcache, "alloc_ht_cache(struct)");
151
9
        return 0;
152
9
    }
153
3.97M
    pcache->bits = tbits;
154
3.97M
    pcache->bits_size = max_bits_size;
155
3.97M
    pcache->ht_tiles = ht_tiles;
156
3.97M
    pcache->num_tiles = max_tiles;
157
3.97M
    pcache->order.cache = pcache;
158
3.97M
    pcache->order.transfer = 0;
159
3.97M
    gx_ht_clear_cache(pcache);
160
3.97M
    return pcache;
161
3.97M
}
162
163
/* Free a halftone cache. */
164
void
165
gx_ht_free_cache(gs_memory_t * mem, gx_ht_cache * pcache)
166
3.97M
{
167
3.97M
    gs_free_object(mem, pcache->ht_tiles, "free_ht_cache(ht_tiles)");
168
3.97M
    gs_free_object(mem, pcache->bits, "free_ht_cache(bits)");
169
3.97M
    gs_free_object(mem, pcache, "free_ht_cache(struct)");
170
3.97M
}
171
172
/* Render a given level into a halftone cache. */
173
static int render_ht(gx_ht_tile *, int, const gx_ht_order *,
174
                      gx_bitmap_id);
175
static gx_ht_tile *
176
gx_render_ht_default(gx_ht_cache * pcache, int b_level)
177
307M
{
178
307M
    const gx_ht_order *porder = &pcache->order;
179
307M
    int level = porder->levels[b_level];
180
307M
    gx_ht_tile *bt;
181
182
307M
    if (pcache->num_cached < porder->num_levels )
183
3.34k
        bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
184
307M
    else
185
307M
        bt =  &pcache->ht_tiles[b_level]; /* one tile per b_level */
186
187
307M
    if (bt->level != level) {
188
1.28M
        int code = render_ht(bt, level, porder, pcache->base_id + b_level);
189
190
1.28M
        if (code < 0)
191
0
            return 0;
192
1.28M
    }
193
307M
    return bt;
194
307M
}
195
196
/* save information about the operand binary halftone color */
197
static void
198
gx_dc_ht_binary_save_dc(const gx_device_color * pdevc,
199
                        gx_device_color_saved * psdc)
200
2.74M
{
201
2.74M
    psdc->type = pdevc->type;
202
2.74M
    psdc->colors.binary.b_color[0] = pdevc->colors.binary.color[0];
203
2.74M
    psdc->colors.binary.b_color[1] = pdevc->colors.binary.color[1];
204
2.74M
    psdc->colors.binary.b_level = pdevc->colors.binary.b_level;
205
2.74M
    psdc->colors.binary.b_index = pdevc->colors.binary.b_index;
206
2.74M
    psdc->phase = pdevc->phase;
207
2.74M
}
208
209
/* get the halftone used for a binary halftone color */
210
static const gx_device_halftone *
211
gx_dc_ht_binary_get_dev_halftone(const gx_device_color * pdevc)
212
63.0M
{
213
63.0M
    return pdevc->colors.binary.b_ht;
214
63.0M
}
215
216
/* Load the device color into the halftone cache if needed. */
217
static int
218
gx_dc_ht_binary_load(gx_device_color * pdevc, const gs_gstate * pgs,
219
                     gx_device * dev, gs_color_select_t select)
220
56.7M
{
221
56.7M
    int component_index = pdevc->colors.binary.b_index;
222
56.7M
    const gx_ht_order *porder;
223
56.7M
    gx_ht_cache *pcache;
224
225
56.7M
    if (component_index < 0) {
226
0
        porder = &pdevc->colors.binary.b_ht->order;
227
56.7M
    } else {
228
56.7M
        int i = 0;
229
230
        /* We can get here with pgs being NULL from the clist. In that case we can't check the saved halftone
231
         * against the graphics state, because there is no graphics state. But I believe it should not be
232
         * possible for this to cause a problem with the clist.
233
         */
234
56.7M
        if (pgs != NULL) {
235
            /* Ensure the halftone saved in the device colour matches one of the
236
             * object-type device halftones. It should not be possible for this not
237
             * to be the case, but an image with a procedural data source which executes
238
             * more grestores than gsaves can restore away the halftone that was in
239
             * force at the start of the image, while we're trying to still use it.
240
             * If that happens we cna't do anything but throw an error.
241
             */
242
56.7M
            for (i=0;i < HT_OBJTYPE_COUNT;i++) {
243
56.7M
                if (pdevc->colors.binary.b_ht == pgs->dev_ht[i])
244
56.7M
                    break;
245
56.7M
            }
246
56.7M
            if (i == HT_OBJTYPE_COUNT)
247
0
                return_error(gs_error_unknownerror);
248
56.7M
        }
249
56.7M
        porder = &pdevc->colors.binary.b_ht->components[component_index].corder;
250
251
56.7M
    }
252
56.7M
    pcache = porder->cache;
253
56.7M
    if (pcache->order.bit_data != porder->bit_data) {
254
        /* I don't think this should be possible, but just in case */
255
0
        if (pgs == NULL)
256
0
            return_error(gs_error_unknownerror);
257
0
        gx_ht_init_cache(pgs->memory, pcache, porder);
258
0
    }
259
    /*
260
     * We do not load the cache now.  Instead we wait until we are ready
261
     * to actually render the color.  This allows multiple colors to be
262
     * loaded without cache conflicts.  (Cache conflicts can occur when
263
     * if two device colors use the same cache elements.  This can occur
264
     * when the tile size is large enough that we do not have a separate
265
     * tile for each half tone level.)  See gx_dc_ht_binary_load_cache.
266
     */
267
56.7M
    pdevc->colors.binary.b_tile = NULL;
268
56.7M
    return 0;
269
56.7M
}
270
271
/*
272
 * Load the half tone tile in the halftone cache.
273
 */
274
static int
275
gx_dc_ht_binary_load_cache(const gx_device_color * pdevc)
276
154M
{
277
154M
    int component_index = pdevc->colors.binary.b_index;
278
154M
    const gx_ht_order *porder =
279
154M
         &pdevc->colors.binary.b_ht->components[component_index].corder;
280
154M
    gx_ht_cache *pcache = porder->cache;
281
154M
    int b_level = pdevc->colors.binary.b_level;
282
154M
    int level = porder->levels[b_level];
283
154M
    gx_ht_tile *bt;
284
285
154M
    if (pcache->num_cached < porder->num_levels )
286
0
        bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
287
154M
    else
288
154M
        bt =  &pcache->ht_tiles[b_level]; /* one tile per b_level */
289
290
154M
    if (bt->level != level) {
291
110k
        int code = render_ht(bt, level, porder, pcache->base_id + b_level);
292
293
110k
        if (code < 0)
294
0
            return_error(gs_error_Fatal);
295
110k
    }
296
154M
    ((gx_device_color *)pdevc)->colors.binary.b_tile = bt;
297
154M
    return 0;
298
154M
}
299
300
/* Fill a rectangle with a binary halftone. */
301
/* Note that we treat this as "texture" for RasterOp. */
302
static int
303
gx_dc_ht_binary_fill_rectangle(const gx_device_color * pdevc, int x, int y,
304
                  int w, int h, gx_device * dev, gs_logical_operation_t lop,
305
                               const gx_rop_source_t * source)
306
917M
{
307
917M
    gx_rop_source_t no_source;
308
309
917M
    fit_fill(dev, x, y, w, h);
310
    /* Load the halftone cache for the color */
311
151M
    gx_dc_ht_binary_load_cache(pdevc);
312
    /*
313
     * Observation of H-P devices and documentation yields confusing
314
     * evidence about whether white pixels in halftones are always
315
     * opaque.  It appears that for black-and-white devices, these
316
     * pixels are *not* opaque.
317
     */
318
151M
    if (dev->color_info.depth > 1)
319
45.6M
        lop &= ~lop_T_transparent;
320
151M
    if (source == NULL && lop_no_S_is_T(lop))
321
151M
        return (*dev_proc(dev, strip_tile_rectangle)) (dev,
322
151M
                                        &pdevc->colors.binary.b_tile->tiles,
323
151M
                                  x, y, w, h, pdevc->colors.binary.color[0],
324
151M
                                              pdevc->colors.binary.color[1],
325
151M
                                            pdevc->phase.x, pdevc->phase.y);
326
    /* Adjust the logical operation per transparent colors. */
327
0
    if (pdevc->colors.binary.color[0] == gx_no_color_index)
328
0
        lop = rop3_use_D_when_T_0(lop);
329
0
    if (pdevc->colors.binary.color[1] == gx_no_color_index)
330
0
        lop = rop3_use_D_when_T_1(lop);
331
0
    if (source == NULL)
332
0
        set_rop_no_source(source, no_source, dev);
333
0
    return (*dev_proc(dev, strip_copy_rop2))
334
0
                             (dev, source->sdata,
335
0
                              source->sourcex, source->sraster, source->id,
336
0
                              (source->use_scolors ? source->scolors : NULL),
337
0
                              &pdevc->colors.binary.b_tile->tiles,
338
0
                              pdevc->colors.binary.color,
339
0
                              x, y, w, h, pdevc->phase.x, pdevc->phase.y,
340
0
                              lop, source->planar_height);
341
151M
}
342
343
static int
344
gx_dc_ht_binary_fill_masked(const gx_device_color * pdevc, const byte * data,
345
        int data_x, int raster, gx_bitmap_id id, int x, int y, int w, int h,
346
                   gx_device * dev, gs_logical_operation_t lop, bool invert)
347
3.19M
{
348
    /*
349
     * Load the halftone cache for the color.  We do not do it earlier
350
     * because for small halftone caches, each cache tile may be used for
351
     * for more than one halftone level.  This can cause conflicts if more
352
     * than one device color has been set and they use the same cache
353
     * entry.
354
     */
355
3.19M
    int code = gx_dc_ht_binary_load_cache(pdevc);
356
357
3.19M
    if (code < 0)
358
0
        return code;
359
3.19M
    return gx_dc_default_fill_masked(pdevc, data, data_x, raster, id,
360
3.19M
                                        x, y, w, h, dev, lop, invert);
361
3.19M
}
362
363
/* Compare two binary halftones for equality. */
364
static bool
365
gx_dc_ht_binary_equal(const gx_device_color * pdevc1,
366
                      const gx_device_color * pdevc2)
367
7.72M
{
368
7.72M
    return pdevc2->type == pdevc1->type &&
369
5.49M
        pdevc1->phase.x == pdevc2->phase.x &&
370
5.49M
        pdevc1->phase.y == pdevc2->phase.y &&
371
5.49M
        gx_dc_binary_color0(pdevc1) == gx_dc_binary_color0(pdevc2) &&
372
5.22M
        gx_dc_binary_color1(pdevc1) == gx_dc_binary_color1(pdevc2) &&
373
5.18M
        pdevc1->colors.binary.b_level == pdevc2->colors.binary.b_level;
374
7.72M
}
375
376
/*
377
 * Flags to indicate the pieces of a binary halftone that are included
378
 * in its string representation. The first byte of the string holds this
379
 * set of flags.
380
 *
381
 * The binary halftone tile is never transmitted as part of the string
382
 * representation, so there is also no flag bit for it.
383
 */
384
enum {
385
    dc_ht_binary_has_color0 = 0x01,
386
    dc_ht_binary_has_color1 = 0x02,
387
    dc_ht_binary_has_level = 0x04,
388
    dc_ht_binary_has_index = 0x08,
389
    dc_ht_binary_has_phase_x = 0x10,
390
    dc_ht_binary_has_phase_y = 0x20,
391
};
392
393
/*
394
 * Serialize a binany halftone device color.
395
 *
396
 * Operands:
397
 *
398
 *  pdevc       pointer to device color to be serialized
399
 *
400
 *  psdc        pointer ot saved version of last serialized color (for
401
 *              this band)
402
 *
403
 *  dev         pointer to the current device, used to retrieve process
404
 *              color model information
405
 *
406
 *  pdata       pointer to buffer in which to write the data
407
 *
408
 *  psize       pointer to a location that, on entry, contains the size of
409
 *              the buffer pointed to by pdata; on return, the size of
410
 *              the data required or actually used will be written here.
411
 *
412
 * Returns:
413
 *  1, with *psize set to 0, if *psdc and *pdevc represent the same color
414
 *
415
 *  0, with *psize set to the amount of data written, if everything OK
416
 *
417
 *  gs_error_rangecheck, with *psize set to the size of buffer required,
418
 *  if *psize was not large enough
419
 *
420
 *  < 0, != gs_error_rangecheck, in the event of some other error; in this
421
 *  case *psize is not changed.
422
 */
423
static int
424
gx_dc_ht_binary_write(
425
    const gx_device_color *         pdevc,
426
    const gx_device_color_saved *   psdc0,
427
    const gx_device *               dev,
428
    int64_t         offset,
429
    byte *                          pdata,
430
    uint *                          psize )
431
65.7M
{
432
65.7M
    int                             req_size = 1;   /* flag bits */
433
65.7M
    int                             flag_bits = 0;
434
65.7M
    uint                            tmp_size;
435
65.7M
    byte *                          pdata0 = pdata;
436
65.7M
    const gx_device_color_saved *   psdc = psdc0;
437
65.7M
    int                             code;
438
439
65.7M
    if (offset != 0)
440
0
        return_error(gs_error_unregistered); /* Not implemented yet. */
441
442
    /* check if operand and saved colors are the same type */
443
65.7M
    if (psdc != 0 && psdc->type != pdevc->type)
444
621k
        psdc = 0;
445
446
    /* check for the information that must be transmitted */
447
65.7M
    if ( psdc == 0                                                      ||
448
65.1M
         pdevc->colors.binary.color[0] != psdc->colors.binary.b_color[0]  ) {
449
651k
        flag_bits |= dc_ht_binary_has_color0;
450
651k
        tmp_size = 0;
451
651k
        (void)gx_dc_write_color( pdevc->colors.binary.color[0],
452
651k
                                 dev,
453
651k
                                 pdata,
454
651k
                                 &tmp_size );
455
651k
        req_size += tmp_size;
456
651k
    }
457
65.7M
    if ( psdc == NULL ||
458
65.1M
         pdevc->colors.binary.color[1] != psdc->colors.binary.b_color[1]  ) {
459
646k
        flag_bits |= dc_ht_binary_has_color1;
460
646k
        tmp_size = 0;
461
646k
        (void)gx_dc_write_color( pdevc->colors.binary.color[1],
462
646k
                                 dev,
463
646k
                                 pdata,
464
646k
                                 &tmp_size );
465
646k
        req_size += tmp_size;
466
646k
    }
467
468
65.7M
    if ( psdc == NULL ||
469
65.1M
         pdevc->colors.binary.b_level != psdc->colors.binary.b_level  ) {
470
5.49M
        flag_bits |= dc_ht_binary_has_level;
471
5.49M
        req_size += enc_u_sizew(pdevc->colors.binary.b_level);
472
5.49M
    }
473
474
65.7M
    if ( psdc == NULL ||
475
65.1M
         pdevc->colors.binary.b_index != psdc->colors.binary.b_index  ) {
476
649k
        flag_bits |= dc_ht_binary_has_index;
477
649k
        req_size += 1;
478
649k
    }
479
480
65.7M
    if ( psdc == NULL ||
481
65.1M
         pdevc->phase.x != psdc->phase.x ) {
482
621k
        flag_bits |= dc_ht_binary_has_phase_x;
483
621k
        req_size += enc_u_sizew(pdevc->phase.x);
484
621k
    }
485
486
65.7M
    if ( psdc == NULL ||
487
65.1M
         pdevc->phase.y != psdc->phase.y ) {
488
621k
        flag_bits |= dc_ht_binary_has_phase_y;
489
621k
        req_size += enc_u_sizew(pdevc->phase.y);
490
621k
    }
491
492
    /* check if there is anything to be done */
493
65.7M
    if (flag_bits == 0) {
494
60.2M
        *psize = 0;
495
60.2M
        return 1;
496
60.2M
    }
497
498
    /* check if sufficient space has been provided */
499
5.49M
    if (req_size > *psize) {
500
2.74M
        *psize = req_size;
501
2.74M
        return_error(gs_error_rangecheck);
502
2.74M
    }
503
504
    /* write out the flag byte */
505
2.74M
    *pdata++ = (byte)flag_bits;
506
507
    /* write out such other parts of the device color as are required */
508
2.74M
    if ((flag_bits & dc_ht_binary_has_color0) != 0) {
509
325k
        tmp_size = req_size - (pdata - pdata0);
510
325k
        code = gx_dc_write_color( pdevc->colors.binary.color[0],
511
325k
                                  dev,
512
325k
                                  pdata,
513
325k
                                  &tmp_size );
514
325k
        if (code < 0)
515
0
            return code;
516
325k
        pdata += tmp_size;
517
325k
    }
518
2.74M
    if ((flag_bits & dc_ht_binary_has_color1) != 0) {
519
323k
        tmp_size = req_size - (pdata - pdata0);
520
323k
        code = gx_dc_write_color( pdevc->colors.binary.color[1],
521
323k
                                  dev,
522
323k
                                  pdata,
523
323k
                                  &tmp_size );
524
323k
        if (code < 0)
525
0
            return code;
526
323k
        pdata += tmp_size;
527
323k
    }
528
2.74M
    if ((flag_bits & dc_ht_binary_has_level) != 0)
529
2.74M
        enc_u_putw(pdevc->colors.binary.b_level, pdata);
530
2.74M
    if ((flag_bits & dc_ht_binary_has_index) != 0)
531
324k
        *pdata++ = pdevc->colors.binary.b_index;
532
2.74M
    if ((flag_bits & dc_ht_binary_has_phase_x) != 0)
533
2.74M
        enc_u_putw(pdevc->phase.x, pdata);
534
2.74M
    if ((flag_bits & dc_ht_binary_has_phase_y) != 0)
535
2.74M
        enc_u_putw(pdevc->phase.y, pdata);
536
537
2.74M
    *psize = pdata - pdata0;
538
2.74M
    return 0;
539
2.74M
}
540
541
/*
542
 * Reconstruct a binary halftone device color from its serial representation.
543
 *
544
 * Operands:
545
 *
546
 *  pdevc       pointer to the location in which to write the
547
 *              reconstructed device color
548
 *
549
 *  pgs         pointer to the current gs_gstate (to access the
550
 *              current halftone)
551
 *
552
 *  prior_devc  pointer to the current device color (this is provided
553
 *              separately because the device color is not part of the
554
 *              gs_gstate)
555
 *
556
 *  dev         pointer to the current device, used to retrieve process
557
 *              color model information
558
 *
559
 *  pdata       pointer to the buffer to be read
560
 *
561
 *  size        size of the buffer to be read; this should be large
562
 *              enough to hold the entire color description
563
 *
564
 *  mem         pointer to the memory to be used for allocations
565
 *              (ignored here)
566
 *
567
 * Returns:
568
 *
569
 *  # of bytes read if everthing OK, < 0 in the event of an error
570
 */
571
static int
572
gx_dc_ht_binary_read(
573
    gx_device_color *       pdevc,
574
    const gs_gstate        * pgs,
575
    const gx_device_color * prior_devc,
576
    const gx_device *       dev,        /* ignored */
577
    int64_t       offset,
578
    const byte *            pdata,
579
    uint                    size,
580
    gs_memory_t *           mem,        /* ignored */
581
    int                     x0,
582
    int                     y0)
583
2.61M
{
584
2.61M
    gx_device_color         devc;
585
2.61M
    const byte *            pdata0 = pdata;
586
2.61M
    int                     code, flag_bits;
587
588
2.61M
    if (offset != 0)
589
0
        return_error(gs_error_unregistered); /* Not implemented yet. */
590
591
    /* if prior information is available, use it */
592
2.61M
    if (prior_devc != 0 && prior_devc->type == gx_dc_type_ht_binary)
593
2.35M
        devc = *prior_devc;
594
256k
    else
595
256k
        memset(&devc, 0, sizeof(devc));   /* clear pointers */
596
2.61M
    devc.type = gx_dc_type_ht_binary;
597
598
    /* the halftone is always taken from the gs_gstate */
599
2.61M
    devc.colors.binary.b_ht = pgs->dev_ht[HT_OBJTYPE_DEFAULT];
600
601
    /* cache is not provided until the device color is used */
602
2.61M
    devc.colors.binary.b_tile = 0;
603
604
    /* verify the minimum amount of information */
605
2.61M
    if (size == 0)
606
0
        return_error(gs_error_rangecheck);
607
2.61M
    size --;
608
2.61M
    flag_bits = *pdata++;
609
610
    /* read the other information provided */
611
2.61M
    if ((flag_bits & dc_ht_binary_has_color0) != 0) {
612
292k
        code = gx_dc_read_color( &devc.colors.binary.color[0],
613
292k
                                 dev,
614
292k
                                 pdata,
615
292k
                                 size );
616
292k
        if (code < 0)
617
0
            return code;
618
292k
        size -= code;
619
292k
        pdata += code;
620
292k
    }
621
2.61M
    if ((flag_bits & dc_ht_binary_has_color1) != 0) {
622
291k
        code = gx_dc_read_color( &devc.colors.binary.color[1],
623
291k
                                 dev,
624
291k
                                 pdata,
625
291k
                                 size );
626
291k
        if (code < 0)
627
0
            return code;
628
291k
        size -= code;
629
291k
        pdata += code;
630
291k
    }
631
2.61M
    if ((flag_bits & dc_ht_binary_has_level) != 0) {
632
2.60M
        const byte *pdata_start = pdata;
633
634
2.60M
        if (size < 1)
635
0
            return_error(gs_error_rangecheck);
636
2.60M
        enc_u_getw(devc.colors.binary.b_level, pdata);
637
2.60M
        size -= pdata - pdata_start;
638
2.60M
    }
639
2.61M
    if ((flag_bits & dc_ht_binary_has_index) != 0) {
640
291k
        if (size == 0)
641
0
            return_error(gs_error_rangecheck);
642
291k
        --size;
643
291k
        devc.colors.binary.b_index = *pdata++;
644
291k
    }
645
2.61M
    if ((flag_bits & dc_ht_binary_has_phase_x) != 0) {
646
283k
        const byte *pdata_start = pdata;
647
648
283k
        if (size < 1)
649
0
            return_error(gs_error_rangecheck);
650
283k
        enc_u_getw(devc.phase.x, pdata);
651
283k
        devc.phase.x += x0;
652
283k
        size -= pdata - pdata_start;
653
283k
    }
654
2.61M
    if ((flag_bits & dc_ht_binary_has_phase_y) != 0) {
655
283k
        const byte *pdata_start = pdata;
656
657
283k
        if (size < 1)
658
0
            return_error(gs_error_rangecheck);
659
283k
        enc_u_getw(devc.phase.y, pdata);
660
283k
        devc.phase.y += y0;
661
283k
        size -= pdata - pdata_start;
662
283k
    }
663
664
    /* everything looks good */
665
2.61M
    *pdevc = devc;
666
2.61M
    return pdata - pdata0;
667
2.61M
}
668
669
/*
670
 * Get the nonzero components of a binary halftone. This is used to
671
 * distinguish components that are given zero intensity due to halftoning
672
 * from those for which the original color intensity was in fact zero.
673
 *
674
 * Since this device color type involves only a single halftone component,
675
 * we can reasonably assume that b_level != 0. Hence, we need to check
676
 * for components with identical intensities in color[0] and color[1].
677
 */
678
int
679
gx_dc_ht_binary_get_nonzero_comps(
680
    const gx_device_color * pdevc,
681
    const gx_device *       dev,
682
    gx_color_index *        pcomp_bits )
683
0
{
684
0
    int                     code;
685
0
    gx_color_value          cvals_0[GX_DEVICE_COLOR_MAX_COMPONENTS],
686
0
                            cvals_1[GX_DEVICE_COLOR_MAX_COMPONENTS];
687
688
0
    if ( (code = dev_proc(dev, decode_color)( (gx_device *)dev,
689
0
                                              pdevc->colors.binary.color[0],
690
0
                                              cvals_0 )) >= 0 &&
691
0
         (code = dev_proc(dev, decode_color)( (gx_device *)dev,
692
0
                                              pdevc->colors.binary.color[1],
693
0
                                              cvals_1 )) >= 0   ) {
694
0
        int     i, ncomps = dev->color_info.num_components;
695
0
        int     mask = 0x1, comp_bits = 0;
696
697
0
        for (i = 0; i < ncomps; i++, mask <<= 1) {
698
0
            if (cvals_0[i] != 0 || cvals_1[i] != 0)
699
0
                comp_bits |= mask;
700
0
        }
701
0
        *pcomp_bits = comp_bits;
702
0
        code = 0;
703
0
    }
704
705
0
    return code;
706
0
}
707
708
/* Initialize the tile cache for a given screen. */
709
/* Cache as many different levels as will fit. */
710
void
711
gx_ht_init_cache(const gs_memory_t *mem, gx_ht_cache * pcache, const gx_ht_order * porder)
712
3.97M
{
713
3.97M
    uint width = porder->width;
714
3.97M
    uint height = porder->height;
715
3.97M
    uint size = width * height + 1;
716
3.97M
    int width_unit =
717
3.97M
    (width <= ht_mask_bits / 2 ? ht_mask_bits / width * width :
718
3.97M
     width);
719
3.97M
    int height_unit = height;
720
3.97M
    uint raster = porder->raster;
721
3.97M
    uint tile_bytes = raster * height;
722
3.97M
    uint shift = porder->shift;
723
3.97M
    int num_cached;
724
3.97M
    int i;
725
3.97M
    byte *tbits = pcache->bits;
726
727
    /* Non-monotonic halftones may have more bits than size. */
728
3.97M
    if (porder->num_bits >= size)
729
0
        size = porder->num_bits + 1;
730
    /* Make sure num_cached is within bounds */
731
3.97M
    num_cached = pcache->bits_size / tile_bytes;
732
3.97M
    if (num_cached > size)
733
3.97M
        num_cached = size;
734
3.97M
    if (num_cached > pcache->num_tiles)
735
0
        num_cached = pcache->num_tiles;
736
3.97M
    if (num_cached == size &&
737
3.97M
        tile_bytes * num_cached <= pcache->bits_size / 2
738
3.97M
        ) {
739
        /*
740
         * We can afford to replicate every tile in the cache,
741
         * which will reduce breakage when tiling.  Since
742
         * horizontal breakage is more expensive than vertical,
743
         * and since wide shallow fills are more common than
744
         * narrow deep fills, we replicate the tile horizontally.
745
         * We do have to be careful not to replicate the tile
746
         * to an absurdly large size, however.
747
         */
748
3.97M
        uint rep_raster =
749
3.97M
        ((pcache->bits_size / num_cached) / height) &
750
3.97M
        ~(align_bitmap_mod - 1);
751
3.97M
        uint rep_count = rep_raster * 8 / width;
752
753
        /*
754
         * There's no real value in replicating the tile
755
         * beyond the point where the byte width of the replicated
756
         * tile is a multiple of a long.
757
         */
758
3.97M
        if (rep_count > sizeof(ulong) * 8)
759
0
            rep_count = sizeof(ulong) * 8;
760
3.97M
        width_unit = width * rep_count;
761
3.97M
        raster = bitmap_raster(width_unit);
762
3.97M
        tile_bytes = raster * height;
763
3.97M
    }
764
3.97M
    pcache->base_id = gs_next_ids(mem, porder->num_levels + 1);
765
3.97M
    pcache->order = *porder;
766
    /* The transfer function is irrelevant, and might become dangling. */
767
3.97M
    pcache->order.transfer = 0;
768
3.97M
    pcache->num_cached = num_cached;
769
3.97M
    pcache->levels_per_tile = (size + num_cached - 1) / num_cached;
770
3.97M
    pcache->tiles_fit = -1;
771
3.97M
    memset(tbits, 0, pcache->bits_size);
772
459M
    for (i = 0; i < num_cached; i++, tbits += tile_bytes) {
773
456M
        register gx_ht_tile *bt = &pcache->ht_tiles[i];
774
775
456M
        bt->level = 0;
776
456M
        bt->index = i;
777
456M
        bt->tiles.data = tbits;
778
456M
        bt->tiles.raster = raster;
779
456M
        bt->tiles.size.x = width_unit;
780
456M
        bt->tiles.size.y = height_unit;
781
456M
        bt->tiles.rep_width = width;
782
456M
        bt->tiles.rep_height = height;
783
456M
        bt->tiles.shift = bt->tiles.rep_shift = shift;
784
456M
        bt->tiles.num_planes = 1;
785
456M
    }
786
3.97M
    pcache->render_ht = gx_render_ht_default;
787
3.97M
}
788
789
/*
790
 * Compute and save the rendering of a given gray level
791
 * with the current halftone.  The cache holds multiple tiles,
792
 * where each tile covers a range of possible levels.
793
 * We adjust the tile whose range includes the desired level incrementally;
794
 * this saves a lot of time for the average image, where gray levels
795
 * don't change abruptly.  Note that the "level" is the number of bits,
796
 * not the index in the levels vector.
797
 */
798
static int
799
render_ht(gx_ht_tile * pbt, int level /* [1..num_bits-1] */ ,
800
          const gx_ht_order * porder, gx_bitmap_id new_id)
801
1.39M
{
802
1.39M
    byte *data = pbt->tiles.data;
803
1.39M
    int code;
804
805
1.39M
    if_debug7('H', "[H]Halftone cache slot "PRI_INTPTR": old=%d, new=%d, w=%d(%d), h=%d(%d):\n",
806
1.39M
              (intptr_t)data, pbt->level, level,
807
1.39M
              pbt->tiles.size.x, porder->width,
808
1.39M
              pbt->tiles.size.y, porder->num_bits / porder->width);
809
#ifdef DEBUG
810
    if (level < 0 || level > porder->num_bits) {
811
        lprintf3("Error in render_ht: level=%d, old level=%d, num_bits=%d\n",
812
                 level, pbt->level, porder->num_bits);
813
        return_error(gs_error_Fatal);
814
    }
815
#endif
816
1.39M
    code = porder->procs->render(pbt, level, porder);
817
1.39M
    if (code < 0)
818
0
        return code;
819
1.39M
    pbt->level = level;
820
1.39M
    pbt->tiles.id = new_id;
821
1.39M
    pbt->tiles.num_planes = 1;
822
    /*
823
     * Check whether we want to replicate the tile in the cache.
824
     * Since we only do this when all the renderings will fit
825
     * in the cache, we only do it once per level, and it doesn't
826
     * have to be very efficient.
827
     */
828
        /****** TEST IS WRONG if width > rep_width but tile.raster ==
829
         ****** order raster.
830
         ******/
831
1.39M
    if (pbt->tiles.raster > porder->raster)
832
1.38M
        bits_replicate_horizontally(data, pbt->tiles.rep_width,
833
1.38M
                                    pbt->tiles.rep_height, porder->raster,
834
1.38M
                                    pbt->tiles.size.x, pbt->tiles.raster);
835
1.39M
    if (pbt->tiles.size.y > pbt->tiles.rep_height &&
836
0
        pbt->tiles.shift == 0
837
1.39M
        )
838
0
        bits_replicate_vertically(data, pbt->tiles.rep_height,
839
0
                                  pbt->tiles.raster, pbt->tiles.size.y);
840
#ifdef DEBUG
841
    if (gs_debug_c('H')) {
842
        const byte *p = pbt->tiles.data;
843
        int wb = pbt->tiles.raster;
844
        const byte *ptr = p + wb * pbt->tiles.size.y;
845
846
        while (p < ptr) {
847
            dmprintf8(porder->data_memory, " %d%d%d%d%d%d%d%d",
848
                      *p >> 7, (*p >> 6) & 1, (*p >> 5) & 1,
849
                      (*p >> 4) & 1, (*p >> 3) & 1, (*p >> 2) & 1,
850
                      (*p >> 1) & 1, *p & 1);
851
            if ((++p - data) % wb == 0)
852
                dmputc(porder->data_memory, '\n');
853
        }
854
    }
855
#endif
856
1.39M
    return 0;
857
1.39M
}