Coverage Report

Created: 2022-04-16 11:23

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