Coverage Report

Created: 2022-10-31 07:00

/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
1.92M
#define gx_dc_type_ht_binary (&gx_dc_type_data_ht_binary)
59
/* GC procedures */
60
static
61
1.11k
ENUM_PTRS_WITH(dc_ht_binary_enum_ptrs, gx_device_color *cptr) return 0;
62
371
ENUM_PTR(0, gx_device_color, colors.binary.b_ht);
63
371
case 1:
64
371
{
65
371
    gx_ht_tile *tile = cptr->colors.binary.b_tile;
66
67
371
    ENUM_RETURN(tile ? tile - tile->index : 0);
68
0
}
69
1.11k
ENUM_PTRS_END
70
371
static RELOC_PTRS_WITH(dc_ht_binary_reloc_ptrs, gx_device_color *cptr)
71
371
{
72
371
    gx_ht_tile *tile = cptr->colors.binary.b_tile;
73
371
    uint index = tile ? tile->index : 0;
74
75
371
    RELOC_PTR(gx_device_color, colors.binary.b_ht);
76
371
    RELOC_TYPED_OFFSET_PTR(gx_device_color, colors.binary.b_tile, index);
77
371
}
78
371
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
813k
{
86
813k
    return 0;
87
813k
}
88
ENUM_PTRS_END_PROC
89
813k
static RELOC_PTRS_BEGIN(ht_tiles_reloc_ptrs)
90
813k
{
91
    /* Reset the bitmap pointers in the tiles. */
92
    /* We know the first tile points to the base of the bits. */
93
813k
    gx_ht_tile *ht_tiles = vptr;
94
813k
    byte *bits = ht_tiles->tiles.data;
95
813k
    uint diff;
96
97
813k
    if (bits == 0)
98
0
        return;
99
813k
    RELOC_VAR(bits);
100
813k
    if (size == size_of(gx_ht_tile)) { /* only 1 tile */
101
0
        ht_tiles->tiles.data = bits;
102
0
        return;
103
0
    }
104
813k
    diff = ht_tiles[1].tiles.data - ht_tiles[0].tiles.data;
105
582M
    for (; size; ht_tiles++, size -= size_of(gx_ht_tile), bits += diff) {
106
581M
        ht_tiles->tiles.data = bits;
107
581M
    }
108
813k
}
109
813k
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
2.89M
{
126
#ifdef DEBUG
127
    return (gs_debug_c('.') ? max_ht_cache_bits_size_SMALL :
128
            max_ht_cache_bits_size);
129
#else
130
2.89M
    return max_ht_cache_bits_size;
131
2.89M
#endif
132
2.89M
}
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
1.92M
{
138
1.92M
    gx_ht_cache *pcache =
139
1.92M
    gs_alloc_struct(mem, gx_ht_cache, &st_ht_cache,
140
1.92M
                    "alloc_ht_cache(struct)");
141
1.92M
    byte *tbits =
142
1.92M
        gs_alloc_bytes(mem, max_bits_size, "alloc_ht_cache(bits)");
143
1.92M
    gx_ht_tile *ht_tiles =
144
1.92M
        gs_alloc_struct_array(mem, max_tiles, gx_ht_tile, &st_ht_tiles,
145
1.92M
                              "alloc_ht_cache(ht_tiles)");
146
147
1.92M
    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
1.92M
    pcache->bits = tbits;
154
1.92M
    pcache->bits_size = max_bits_size;
155
1.92M
    pcache->ht_tiles = ht_tiles;
156
1.92M
    pcache->num_tiles = max_tiles;
157
1.92M
    pcache->order.cache = pcache;
158
1.92M
    pcache->order.transfer = 0;
159
1.92M
    gx_ht_clear_cache(pcache);
160
1.92M
    return pcache;
161
1.92M
}
162
163
/* Free a halftone cache. */
164
void
165
gx_ht_free_cache(gs_memory_t * mem, gx_ht_cache * pcache)
166
1.92M
{
167
1.92M
    gs_free_object(mem, pcache->ht_tiles, "free_ht_cache(ht_tiles)");
168
1.92M
    gs_free_object(mem, pcache->bits, "free_ht_cache(bits)");
169
1.92M
    gs_free_object(mem, pcache, "free_ht_cache(struct)");
170
1.92M
}
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
200M
{
178
200M
    const gx_ht_order *porder = &pcache->order;
179
200M
    int level = porder->levels[b_level];
180
200M
    gx_ht_tile *bt;
181
182
200M
    if (pcache->num_cached < porder->num_levels )
183
1.74k
        bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
184
200M
    else
185
200M
        bt =  &pcache->ht_tiles[b_level]; /* one tile per b_level */
186
187
200M
    if (bt->level != level) {
188
909k
        int code = render_ht(bt, level, porder, pcache->base_id + b_level);
189
190
909k
        if (code < 0)
191
0
            return 0;
192
909k
    }
193
200M
    return bt;
194
200M
}
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
981k
{
201
981k
    psdc->type = pdevc->type;
202
981k
    psdc->colors.binary.b_color[0] = pdevc->colors.binary.color[0];
203
981k
    psdc->colors.binary.b_color[1] = pdevc->colors.binary.color[1];
204
981k
    psdc->colors.binary.b_level = pdevc->colors.binary.b_level;
205
981k
    psdc->colors.binary.b_index = pdevc->colors.binary.b_index;
206
981k
    psdc->phase = pdevc->phase;
207
981k
}
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
8.80M
{
213
8.80M
    return pdevc->colors.binary.b_ht;
214
8.80M
}
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
11.7M
{
221
11.7M
    int component_index = pdevc->colors.binary.b_index;
222
11.7M
    const gx_ht_order *porder =
223
11.7M
        (component_index < 0 ?
224
0
         &pdevc->colors.binary.b_ht->order :
225
11.7M
         &pdevc->colors.binary.b_ht->components[component_index].corder);
226
11.7M
    gx_ht_cache *pcache = porder->cache;
227
228
11.7M
    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
11.7M
    pdevc->colors.binary.b_tile = NULL;
239
11.7M
    return 0;
240
11.7M
}
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
60.5M
{
248
60.5M
    int component_index = pdevc->colors.binary.b_index;
249
60.5M
    const gx_ht_order *porder =
250
60.5M
         &pdevc->colors.binary.b_ht->components[component_index].corder;
251
60.5M
    gx_ht_cache *pcache = porder->cache;
252
60.5M
    int b_level = pdevc->colors.binary.b_level;
253
60.5M
    int level = porder->levels[b_level];
254
60.5M
    gx_ht_tile *bt;
255
256
60.5M
    if (pcache->num_cached < porder->num_levels )
257
0
        bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
258
60.5M
    else
259
60.5M
        bt =  &pcache->ht_tiles[b_level]; /* one tile per b_level */
260
261
60.5M
    if (bt->level != level) {
262
58.8k
        int code = render_ht(bt, level, porder, pcache->base_id + b_level);
263
264
58.8k
        if (code < 0)
265
0
            return_error(gs_error_Fatal);
266
58.8k
    }
267
60.5M
    ((gx_device_color *)pdevc)->colors.binary.b_tile = bt;
268
60.5M
    return 0;
269
60.5M
}
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
607M
{
278
607M
    gx_rop_source_t no_source;
279
280
607M
    fit_fill(dev, x, y, w, h);
281
    /* Load the halftone cache for the color */
282
60.4M
    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
60.4M
    if (dev->color_info.depth > 1)
290
32.0M
        lop &= ~lop_T_transparent;
291
60.4M
    if (source == NULL && lop_no_S_is_T(lop))
292
60.4M
        return (*dev_proc(dev, strip_tile_rectangle)) (dev,
293
60.4M
                                        &pdevc->colors.binary.b_tile->tiles,
294
60.4M
                                  x, y, w, h, pdevc->colors.binary.color[0],
295
60.4M
                                              pdevc->colors.binary.color[1],
296
60.4M
                                            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
60.4M
}
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
129k
{
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
129k
    int code = gx_dc_ht_binary_load_cache(pdevc);
327
328
129k
    if (code < 0)
329
0
        return code;
330
129k
    return gx_dc_default_fill_masked(pdevc, data, data_x, raster, id,
331
129k
                                        x, y, w, h, dev, lop, invert);
332
129k
}
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
4.20M
{
339
4.20M
    return pdevc2->type == pdevc1->type &&
340
4.20M
        pdevc1->phase.x == pdevc2->phase.x &&
341
4.20M
        pdevc1->phase.y == pdevc2->phase.y &&
342
4.20M
        gx_dc_binary_color0(pdevc1) == gx_dc_binary_color0(pdevc2) &&
343
4.20M
        gx_dc_binary_color1(pdevc1) == gx_dc_binary_color1(pdevc2) &&
344
4.20M
        pdevc1->colors.binary.b_level == pdevc2->colors.binary.b_level;
345
4.20M
}
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
9.78M
{
403
9.78M
    int                             req_size = 1;   /* flag bits */
404
9.78M
    int                             flag_bits = 0;
405
9.78M
    uint                            tmp_size;
406
9.78M
    byte *                          pdata0 = pdata;
407
9.78M
    const gx_device_color_saved *   psdc = psdc0;
408
9.78M
    int                             code;
409
410
9.78M
    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
9.78M
    if (psdc != 0 && psdc->type != pdevc->type)
415
334k
        psdc = 0;
416
417
    /* check for the information that must be transmitted */
418
9.78M
    if ( psdc == 0                                                      ||
419
9.78M
         pdevc->colors.binary.color[0] != psdc->colors.binary.b_color[0]  ) {
420
338k
        flag_bits |= dc_ht_binary_has_color0;
421
338k
        tmp_size = 0;
422
338k
        (void)gx_dc_write_color( pdevc->colors.binary.color[0],
423
338k
                                 dev,
424
338k
                                 pdata,
425
338k
                                 &tmp_size );
426
338k
        req_size += tmp_size;
427
338k
    }
428
9.78M
    if ( psdc == NULL ||
429
9.78M
         pdevc->colors.binary.color[1] != psdc->colors.binary.b_color[1]  ) {
430
338k
        flag_bits |= dc_ht_binary_has_color1;
431
338k
        tmp_size = 0;
432
338k
        (void)gx_dc_write_color( pdevc->colors.binary.color[1],
433
338k
                                 dev,
434
338k
                                 pdata,
435
338k
                                 &tmp_size );
436
338k
        req_size += tmp_size;
437
338k
    }
438
439
9.78M
    if ( psdc == NULL ||
440
9.78M
         pdevc->colors.binary.b_level != psdc->colors.binary.b_level  ) {
441
1.96M
        flag_bits |= dc_ht_binary_has_level;
442
1.96M
        req_size += enc_u_sizew(pdevc->colors.binary.b_level);
443
1.96M
    }
444
445
9.78M
    if ( psdc == NULL ||
446
9.78M
         pdevc->colors.binary.b_index != psdc->colors.binary.b_index  ) {
447
337k
        flag_bits |= dc_ht_binary_has_index;
448
337k
        req_size += 1;
449
337k
    }
450
451
9.78M
    if ( psdc == NULL ||
452
9.78M
         pdevc->phase.x != psdc->phase.x ) {
453
334k
        flag_bits |= dc_ht_binary_has_phase_x;
454
334k
        req_size += enc_u_sizew(pdevc->phase.x);
455
334k
    }
456
457
9.78M
    if ( psdc == NULL ||
458
9.78M
         pdevc->phase.y != psdc->phase.y ) {
459
334k
        flag_bits |= dc_ht_binary_has_phase_y;
460
334k
        req_size += enc_u_sizew(pdevc->phase.y);
461
334k
    }
462
463
    /* check if there is anything to be done */
464
9.78M
    if (flag_bits == 0) {
465
7.82M
        *psize = 0;
466
7.82M
        return 1;
467
7.82M
    }
468
469
    /* check if sufficient space has been provided */
470
1.96M
    if (req_size > *psize) {
471
981k
        *psize = req_size;
472
981k
        return_error(gs_error_rangecheck);
473
981k
    }
474
475
    /* write out the flag byte */
476
981k
    *pdata++ = (byte)flag_bits;
477
478
    /* write out such other parts of the device color as are required */
479
981k
    if ((flag_bits & dc_ht_binary_has_color0) != 0) {
480
169k
        tmp_size = req_size - (pdata - pdata0);
481
169k
        code = gx_dc_write_color( pdevc->colors.binary.color[0],
482
169k
                                  dev,
483
169k
                                  pdata,
484
169k
                                  &tmp_size );
485
169k
        if (code < 0)
486
0
            return code;
487
169k
        pdata += tmp_size;
488
169k
    }
489
981k
    if ((flag_bits & dc_ht_binary_has_color1) != 0) {
490
169k
        tmp_size = req_size - (pdata - pdata0);
491
169k
        code = gx_dc_write_color( pdevc->colors.binary.color[1],
492
169k
                                  dev,
493
169k
                                  pdata,
494
169k
                                  &tmp_size );
495
169k
        if (code < 0)
496
0
            return code;
497
169k
        pdata += tmp_size;
498
169k
    }
499
981k
    if ((flag_bits & dc_ht_binary_has_level) != 0)
500
981k
        enc_u_putw(pdevc->colors.binary.b_level, pdata);
501
981k
    if ((flag_bits & dc_ht_binary_has_index) != 0)
502
168k
        *pdata++ = pdevc->colors.binary.b_index;
503
981k
    if ((flag_bits & dc_ht_binary_has_phase_x) != 0)
504
981k
        enc_u_putw(pdevc->phase.x, pdata);
505
981k
    if ((flag_bits & dc_ht_binary_has_phase_y) != 0)
506
981k
        enc_u_putw(pdevc->phase.y, pdata);
507
508
981k
    *psize = pdata - pdata0;
509
981k
    return 0;
510
981k
}
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
962k
{
555
962k
    gx_device_color         devc;
556
962k
    const byte *            pdata0 = pdata;
557
962k
    int                     code, flag_bits;
558
559
962k
    if (offset != 0)
560
0
        return_error(gs_error_unregistered); /* Not implemented yet. */
561
562
    /* if prior information is available, use it */
563
962k
    if (prior_devc != 0 && prior_devc->type == gx_dc_type_ht_binary)
564
817k
        devc = *prior_devc;
565
144k
    else
566
144k
        memset(&devc, 0, sizeof(devc));   /* clear pointers */
567
962k
    devc.type = gx_dc_type_ht_binary;
568
569
    /* the halftone is always taken from the gs_gstate */
570
962k
    devc.colors.binary.b_ht = pgs->dev_ht[HT_OBJTYPE_DEFAULT];
571
572
    /* cache is not provided until the device color is used */
573
962k
    devc.colors.binary.b_tile = 0;
574
575
    /* verify the minimum amount of information */
576
962k
    if (size == 0)
577
0
        return_error(gs_error_rangecheck);
578
962k
    size --;
579
962k
    flag_bits = *pdata++;
580
581
    /* read the other information provided */
582
962k
    if ((flag_bits & dc_ht_binary_has_color0) != 0) {
583
164k
        code = gx_dc_read_color( &devc.colors.binary.color[0],
584
164k
                                 dev,
585
164k
                                 pdata,
586
164k
                                 size );
587
164k
        if (code < 0)
588
0
            return code;
589
164k
        size -= code;
590
164k
        pdata += code;
591
164k
    }
592
962k
    if ((flag_bits & dc_ht_binary_has_color1) != 0) {
593
164k
        code = gx_dc_read_color( &devc.colors.binary.color[1],
594
164k
                                 dev,
595
164k
                                 pdata,
596
164k
                                 size );
597
164k
        if (code < 0)
598
0
            return code;
599
164k
        size -= code;
600
164k
        pdata += code;
601
164k
    }
602
962k
    if ((flag_bits & dc_ht_binary_has_level) != 0) {
603
962k
        const byte *pdata_start = pdata;
604
605
962k
        if (size < 1)
606
0
            return_error(gs_error_rangecheck);
607
962k
        enc_u_getw(devc.colors.binary.b_level, pdata);
608
962k
        size -= pdata - pdata_start;
609
962k
    }
610
962k
    if ((flag_bits & dc_ht_binary_has_index) != 0) {
611
163k
        if (size == 0)
612
0
            return_error(gs_error_rangecheck);
613
163k
        --size;
614
163k
        devc.colors.binary.b_index = *pdata++;
615
163k
    }
616
962k
    if ((flag_bits & dc_ht_binary_has_phase_x) != 0) {
617
162k
        const byte *pdata_start = pdata;
618
619
162k
        if (size < 1)
620
0
            return_error(gs_error_rangecheck);
621
162k
        enc_u_getw(devc.phase.x, pdata);
622
162k
        devc.phase.x += x0;
623
162k
        size -= pdata - pdata_start;
624
162k
    }
625
962k
    if ((flag_bits & dc_ht_binary_has_phase_y) != 0) {
626
162k
        const byte *pdata_start = pdata;
627
628
162k
        if (size < 1)
629
0
            return_error(gs_error_rangecheck);
630
162k
        enc_u_getw(devc.phase.y, pdata);
631
162k
        devc.phase.y += y0;
632
162k
        size -= pdata - pdata_start;
633
162k
    }
634
635
    /* everything looks good */
636
962k
    *pdevc = devc;
637
962k
    return pdata - pdata0;
638
962k
}
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
1.92M
{
684
1.92M
    uint width = porder->width;
685
1.92M
    uint height = porder->height;
686
1.92M
    uint size = width * height + 1;
687
1.92M
    int width_unit =
688
1.92M
    (width <= ht_mask_bits / 2 ? ht_mask_bits / width * width :
689
1.92M
     width);
690
1.92M
    int height_unit = height;
691
1.92M
    uint raster = porder->raster;
692
1.92M
    uint tile_bytes = raster * height;
693
1.92M
    uint shift = porder->shift;
694
1.92M
    int num_cached;
695
1.92M
    int i;
696
1.92M
    byte *tbits = pcache->bits;
697
698
    /* Non-monotonic halftones may have more bits than size. */
699
1.92M
    if (porder->num_bits >= size)
700
0
        size = porder->num_bits + 1;
701
    /* Make sure num_cached is within bounds */
702
1.92M
    num_cached = pcache->bits_size / tile_bytes;
703
1.92M
    if (num_cached > size)
704
1.92M
        num_cached = size;
705
1.92M
    if (num_cached > pcache->num_tiles)
706
0
        num_cached = pcache->num_tiles;
707
1.92M
    if (num_cached == size &&
708
1.92M
        tile_bytes * num_cached <= pcache->bits_size / 2
709
1.92M
        ) {
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
1.92M
        uint rep_raster =
720
1.92M
        ((pcache->bits_size / num_cached) / height) &
721
1.92M
        ~(align_bitmap_mod - 1);
722
1.92M
        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
1.92M
        if (rep_count > sizeof(ulong) * 8)
730
0
            rep_count = sizeof(ulong) * 8;
731
1.92M
        width_unit = width * rep_count;
732
1.92M
        raster = bitmap_raster(width_unit);
733
1.92M
        tile_bytes = raster * height;
734
1.92M
    }
735
1.92M
    pcache->base_id = gs_next_ids(mem, porder->num_levels + 1);
736
1.92M
    pcache->order = *porder;
737
    /* The transfer function is irrelevant, and might become dangling. */
738
1.92M
    pcache->order.transfer = 0;
739
1.92M
    pcache->num_cached = num_cached;
740
1.92M
    pcache->levels_per_tile = (size + num_cached - 1) / num_cached;
741
1.92M
    pcache->tiles_fit = -1;
742
1.92M
    memset(tbits, 0, pcache->bits_size);
743
224M
    for (i = 0; i < num_cached; i++, tbits += tile_bytes) {
744
222M
        register gx_ht_tile *bt = &pcache->ht_tiles[i];
745
746
222M
        bt->level = 0;
747
222M
        bt->index = i;
748
222M
        bt->tiles.data = tbits;
749
222M
        bt->tiles.raster = raster;
750
222M
        bt->tiles.size.x = width_unit;
751
222M
        bt->tiles.size.y = height_unit;
752
222M
        bt->tiles.rep_width = width;
753
222M
        bt->tiles.rep_height = height;
754
222M
        bt->tiles.shift = bt->tiles.rep_shift = shift;
755
222M
        bt->tiles.num_planes = 1;
756
222M
    }
757
1.92M
    pcache->render_ht = gx_render_ht_default;
758
1.92M
}
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
968k
{
773
968k
    byte *data = pbt->tiles.data;
774
968k
    int code;
775
776
968k
    if_debug7('H', "[H]Halftone cache slot "PRI_INTPTR": old=%d, new=%d, w=%d(%d), h=%d(%d):\n",
777
968k
              (intptr_t)data, pbt->level, level,
778
968k
              pbt->tiles.size.x, porder->width,
779
968k
              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
968k
    code = porder->procs->render(pbt, level, porder);
788
968k
    if (code < 0)
789
0
        return code;
790
968k
    pbt->level = level;
791
968k
    pbt->tiles.id = new_id;
792
968k
    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
968k
    if (pbt->tiles.raster > porder->raster)
803
966k
        bits_replicate_horizontally(data, pbt->tiles.rep_width,
804
966k
                                    pbt->tiles.rep_height, porder->raster,
805
966k
                                    pbt->tiles.size.x, pbt->tiles.raster);
806
968k
    if (pbt->tiles.size.y > pbt->tiles.rep_height &&
807
968k
        pbt->tiles.shift == 0
808
968k
        )
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
968k
    return 0;
828
968k
}