1 /* Copyright (C) 1993, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16
17 /*$Id: gxcht.c,v 1.17 2005/08/31 03:41:24 dan Exp $ */
18 /* Color halftone rendering for Ghostscript imaging library */
19 #include <assert.h>
20 #include "memory_.h"
21 #include "gx.h"
22 #include "gserrors.h"
23 #include "gsutil.h" /* for id generation */
24 #include "gxarith.h"
25 #include "gxfixed.h"
26 #include "gxmatrix.h"
27 #include "gxdevice.h"
28 #include "gxcmap.h"
29 #include "gxdcolor.h"
30 #include "gxistate.h"
31 #include "gzht.h"
32 #include "gsserial.h"
33
34 /* Define whether to force use of the slow code, for testing. */
35 #define USE_SLOW_CODE 0
36
37 /* Define the size of the tile buffer allocated on the stack. */
38 #define tile_longs_LARGE 256
39 #define tile_longs_SMALL 64
40 #if arch_small_memory
41 # define tile_longs_allocated tile_longs_SMALL
42 # define tile_longs tile_longs_SMALL
43 #else
44 # define tile_longs_allocated tile_longs_LARGE
45 # ifdef DEBUG
46 # define tile_longs\
47 (gs_debug_c('.') ? tile_longs_SMALL : tile_longs_LARGE)
48 # else
49 # define tile_longs tile_longs_LARGE
50 # endif
51 #endif
52
53 /* Define the colored halftone device color type. */
54 gs_private_st_ptrs1(st_dc_ht_colored, gx_device_color, "dc_ht_colored",
55 dc_ht_colored_enum_ptrs, dc_ht_colored_reloc_ptrs, colors.colored.c_ht);
56 private dev_color_proc_save_dc(gx_dc_ht_colored_save_dc);
57 private dev_color_proc_get_dev_halftone(gx_dc_ht_colored_get_dev_halftone);
58 private dev_color_proc_load(gx_dc_ht_colored_load);
59 private dev_color_proc_fill_rectangle(gx_dc_ht_colored_fill_rectangle);
60 private dev_color_proc_equal(gx_dc_ht_colored_equal);
61 private dev_color_proc_write(gx_dc_ht_colored_write);
62 private dev_color_proc_read(gx_dc_ht_colored_read);
63 const gx_device_color_type_t gx_dc_type_data_ht_colored = {
64 &st_dc_ht_colored,
65 gx_dc_ht_colored_save_dc, gx_dc_ht_colored_get_dev_halftone,
66 gx_dc_ht_get_phase,
67 gx_dc_ht_colored_load, gx_dc_ht_colored_fill_rectangle,
68 gx_dc_default_fill_masked, gx_dc_ht_colored_equal,
69 gx_dc_ht_colored_write, gx_dc_ht_colored_read,
70 gx_dc_ht_colored_get_nonzero_comps
71 };
72 #undef gx_dc_type_ht_colored
73 const gx_device_color_type_t *const gx_dc_type_ht_colored =
74 &gx_dc_type_data_ht_colored;
75 #define gx_dc_type_ht_colored (&gx_dc_type_data_ht_colored)
76
77 /* save information about the operand device color */
78 private void
gx_dc_ht_colored_save_dc(const gx_device_color * pdevc,gx_device_color_saved * psdc)79 gx_dc_ht_colored_save_dc(const gx_device_color * pdevc,
80 gx_device_color_saved * psdc)
81 {
82 psdc->type = pdevc->type;
83 memcpy( psdc->colors.colored.c_base,
84 pdevc->colors.colored.c_base,
85 sizeof(psdc->colors.colored.c_base) );
86 memcpy( psdc->colors.colored.c_level,
87 pdevc->colors.colored.c_level,
88 sizeof(psdc->colors.colored.c_base) );
89 psdc->colors.colored.alpha = pdevc->colors.colored.alpha;
90 psdc->phase = pdevc->phase;
91 }
92
93 /* get the halftone used for the operand device color */
94 private const gx_device_halftone *
gx_dc_ht_colored_get_dev_halftone(const gx_device_color * pdevc)95 gx_dc_ht_colored_get_dev_halftone(const gx_device_color * pdevc)
96 {
97 return pdevc->colors.colored.c_ht;
98 }
99
100 /* Compare two colored halftones for equality. */
101 private bool
gx_dc_ht_colored_equal(const gx_device_color * pdevc1,const gx_device_color * pdevc2)102 gx_dc_ht_colored_equal(const gx_device_color * pdevc1,
103 const gx_device_color * pdevc2)
104 {
105 uint num_comp = pdevc1->colors.colored.num_components;
106
107 if (pdevc2->type != pdevc1->type ||
108 pdevc1->colors.colored.c_ht != pdevc2->colors.colored.c_ht ||
109 pdevc1->colors.colored.alpha != pdevc2->colors.colored.alpha ||
110 pdevc1->phase.x != pdevc2->phase.x ||
111 pdevc1->phase.y != pdevc2->phase.y ||
112 num_comp != pdevc2->colors.colored.num_components
113 )
114 return false;
115 return
116 !memcmp(pdevc1->colors.colored.c_base,
117 pdevc2->colors.colored.c_base,
118 num_comp * sizeof(pdevc1->colors.colored.c_base[0])) &&
119 !memcmp(pdevc1->colors.colored.c_level,
120 pdevc2->colors.colored.c_level,
121 num_comp * sizeof(pdevc1->colors.colored.c_level[0]));
122 }
123
124
125 /*
126 * Flags to indicate the pieces of a colored halftone that are included
127 * in its string representation. The first byte of the string holds this
128 * set of flags.
129 *
130 * The case alpha = gx_max_color_value is by far the most common, so
131 * special treatment is provided for this case.
132 *
133 * The halftone is never transmitted as part of a device color, so there
134 * is no flag for it.
135 */
136 private const int dc_ht_colored_has_base = 0x01;
137 private const int dc_ht_colored_has_level = 0x02;
138 private const int dc_ht_colored_has_alpha = 0x04;
139 private const int dc_ht_colored_alpha_is_max = 0x08;
140
141 /*
142 * Serialize a device color that uses a traditional colored halftone.
143 *
144 * The number of components of a device color must match that of the
145 * process color model, so it is not transmitted.
146 *
147 * The most common situation in which this routine is used is for 1-bit
148 * per component devices. In that case, base[i] will always be 0 or 1,
149 * and thus may be fit in a single bit.
150 *
151 * In many situations, one or more of the color component intensity
152 * levels will be 0. The plane_mask field identifies those components
153 * where this is not the case. By tansmitting the plane_mask, only those
154 * components with non-zero levels need be transmitted.
155 *
156 * The case alpha = gx_max_color_value is by far the most common, so
157 * special treatment is provided for this case.
158 *
159 *
160 * Operands:
161 *
162 * pdevc pointer to device color to be serialized
163 *
164 * psdc pointer ot saved version of last serialized color (for
165 * this band)
166 *
167 * dev pointer to the current device, used to retrieve process
168 * color model information
169 *
170 * pdata pointer to buffer in which to write the data
171 *
172 * psize pointer to a location that, on entry, contains the size of
173 * the buffer pointed to by pdata; on return, the size of
174 * the data required or actually used will be written here.
175 *
176 * Returns:
177 * 1, with *psize set to 0, if *psdc and *pdevc represent the same color
178 *
179 * 0, with *psize set to the amount of data written, if everything OK
180 *
181 * gs_error_rangecheck, with *psize set to the size of buffer required,
182 * if *psize was not large enough
183 *
184 * < 0, != gs_error_rangecheck, in the event of some other error
185 * (currently none); in this case *psize is not changed.
186 */
187 private int
gx_dc_ht_colored_write(const gx_device_color * pdevc,const gx_device_color_saved * psdc0,const gx_device * dev,byte * pdata,uint * psize)188 gx_dc_ht_colored_write(
189 const gx_device_color * pdevc,
190 const gx_device_color_saved * psdc0,
191 const gx_device * dev,
192 byte * pdata,
193 uint * psize )
194 {
195 int req_size = 1;
196 int flag_bits = 0;
197 int num_comps = dev->color_info.num_components;
198 int depth = dev->color_info.depth;
199 gx_color_index plane_mask = pdevc->colors.colored.plane_mask;
200 gx_color_value alpha = pdevc->colors.colored.alpha;
201 const gx_device_color_saved * psdc = psdc0;
202 byte * pdata0 = pdata;
203
204 /* sanity check */
205 assert(pdevc->colors.colored.num_components == num_comps);
206
207 /* check if saved color is of the same type */
208 if (psdc != 0 && psdc->type != pdevc->type)
209 psdc = 0;
210
211 /* calculate the size required */
212 if ( psdc == 0 ||
213 memcmp( pdevc->colors.colored.c_base,
214 psdc->colors.colored.c_base,
215 num_comps * sizeof(pdevc->colors.colored.c_base[0]) ) != 0 ) {
216 flag_bits |= dc_ht_colored_has_base;
217 if (num_comps == depth) /* 1 bit / component */
218 req_size += (num_comps + 7) >> 3;
219 else
220 req_size += num_comps * sizeof(pdevc->colors.colored.c_base[0]);
221 }
222
223 plane_mask = pdevc->colors.colored.plane_mask;
224 if ( psdc == 0 ||
225 memcmp( pdevc->colors.colored.c_level,
226 psdc->colors.colored.c_level,
227 num_comps * sizeof(pdevc->colors.colored.c_level[0]) ) != 0 ) {
228 gx_color_index comp_bit;
229 int i;
230 uint tmp_mask;
231
232 flag_bits |= dc_ht_colored_has_level;
233 if (num_comps > 8 * sizeof(uint)) {
234 tmp_mask = (uint)plane_mask;
235 req_size += enc_u_sizew(tmp_mask);
236 tmp_mask = (uint)(plane_mask >> (8 * sizeof(uint)));
237 req_size += enc_u_sizew(tmp_mask);
238 } else {
239 tmp_mask = (uint)plane_mask;
240 req_size += enc_u_sizew(tmp_mask);
241 }
242 for (i = 0, comp_bit = 0x1; i < num_comps; i++, comp_bit <<= 1) {
243 if ((plane_mask & comp_bit) != 0)
244 req_size += enc_u_sizew(pdevc->colors.colored.c_level[i]);
245 }
246 }
247
248 if (psdc == 0 || alpha != psdc->colors.colored.alpha) {
249 if (alpha == gx_max_color_value)
250 flag_bits |= dc_ht_colored_alpha_is_max;
251 else {
252 flag_bits |= dc_ht_colored_has_alpha;
253 req_size += enc_u_sizew(alpha);
254 }
255 }
256
257 /* see if there is anything to do */
258 if (flag_bits == 0) {
259 *psize = 0;
260 return 1;
261 }
262
263 /* see if enough space is available */
264 if (req_size > *psize) {
265 *psize = req_size;
266 return gs_error_rangecheck;
267 }
268
269 /* write out the flag byte */
270 *pdata++ = (byte)flag_bits;
271
272 /* write out such other parts of the device color as required */
273 if ((flag_bits & dc_ht_colored_has_base) != 0) {
274 if (num_comps == depth) {
275 gx_color_index base_mask = 0;
276 int num_bytes = (num_comps + 7) >> 3;
277 int i;
278
279 for (i = 0; i < num_comps; i++) {
280 if (pdevc->colors.colored.c_base[i] != 0)
281 base_mask |= (gx_color_index)1 << i;
282 }
283 for (i = 0; i < num_bytes; i++, base_mask >>= 8)
284 *pdata++ = (byte)base_mask;
285 } else {
286 memcpy( pdata,
287 pdevc->colors.colored.c_base,
288 num_comps * sizeof(pdevc->colors.colored.c_base[0]) );
289 pdata += num_comps * sizeof(pdevc->colors.colored.c_base[0]);
290 }
291 }
292
293 if ((flag_bits & dc_ht_colored_has_level) != 0) {
294 gx_color_index code_bit;
295 int i;
296 uint tmp_mask;
297
298 if (num_comps > 8 * sizeof(uint)) {
299 tmp_mask = (uint)plane_mask;
300 enc_u_putw(tmp_mask, pdata);
301 tmp_mask = (uint)(plane_mask >> (8 * sizeof(uint)));
302 enc_u_putw(tmp_mask, pdata);
303 } else {
304 tmp_mask = (uint)plane_mask;
305 enc_u_putw(tmp_mask, pdata);
306 }
307 for (i = 0, code_bit = 0x1; i < num_comps; i++, code_bit <<= 1) {
308 if ((plane_mask & code_bit) != 0)
309 enc_u_putw(pdevc->colors.colored.c_level[i], pdata);
310 }
311 }
312
313 if ((flag_bits & dc_ht_colored_has_alpha) != 0)
314 enc_u_putw(alpha, pdata);
315
316 *psize = pdata - pdata0;
317 return 0;
318 }
319
320 /*
321 * Reconstruct a device color from its serial representation.
322 *
323 * Operands:
324 *
325 * pdevc pointer to the location in which to write the
326 * reconstructed device color
327 *
328 * pis pointer to the current imager state (to access the
329 * current halftone)
330 *
331 * prior_devc pointer to the current device color (this is provided
332 * separately because the device color is not part of the
333 * imager state)
334 *
335 * dev pointer to the current device, used to retrieve process
336 * color model information
337 *
338 * pdata pointer to the buffer to be read
339 *
340 * size size of the buffer to be read; this should be large
341 * enough to hold the entire color description
342 *
343 * mem pointer to the memory to be used for allocations
344 * (ignored here)
345 *
346 * Returns:
347 *
348 * # of bytes read if everthing OK, < 0 in the event of an error
349 */
350 private int
gx_dc_ht_colored_read(gx_device_color * pdevc,const gs_imager_state * pis,const gx_device_color * prior_devc,const gx_device * dev,const byte * pdata,uint size,gs_memory_t * mem)351 gx_dc_ht_colored_read(
352 gx_device_color * pdevc,
353 const gs_imager_state * pis,
354 const gx_device_color * prior_devc,
355 const gx_device * dev,
356 const byte * pdata,
357 uint size,
358 gs_memory_t * mem ) /* ignored */
359 {
360 gx_device_color devc;
361 int num_comps = dev->color_info.num_components;
362 int depth = dev->color_info.depth;
363 const byte * pdata0 = pdata;
364 int flag_bits;
365
366 /* if prior information is available, use it */
367 if (prior_devc != 0 && prior_devc->type == gx_dc_type_ht_colored)
368 devc = *prior_devc;
369 else
370 memset(&devc, 0, sizeof(devc)); /* clear pointers */
371 devc.type = gx_dc_type_ht_colored;
372
373 /* the number of components is determined by the color model */
374 devc.colors.colored.num_components = num_comps;
375 devc.colors.colored.c_ht = pis->dev_ht;
376
377 /*
378 * Verify that we have at least the flag bits. For performance
379 * reasons, the routines that convert serialized representations
380 * of integers do not check buffer size. Hence, in many cases below,
381 * only a very rough check is made to verify that we have not
382 * exhausted the buffer. This should not cause a problem in
383 * practice.
384 */
385 if (size == 0)
386 return_error(gs_error_rangecheck);
387 size--;
388 flag_bits = *pdata++;
389
390 /* read the other components provided */
391 if ((flag_bits & dc_ht_colored_has_base) != 0) {
392 if (depth == num_comps) {
393 gx_color_index base_mask = 0;
394 int num_bytes = (num_comps + 7) >> 3;
395 int i, shift = 0;
396
397 if (size < num_bytes)
398 return_error(gs_error_rangecheck);
399 size -= num_bytes;
400 for (i = 0; i < num_bytes; i++, shift += 8)
401 base_mask |= (gx_color_index)(*pdata++) << shift;
402 for (i = 0; i < num_comps; i++, base_mask >>= 1)
403 devc.colors.colored.c_base[i] = base_mask & 0x1;
404 } else {
405 if (size < num_comps)
406 return_error(gs_error_rangecheck);
407 size -= num_comps;
408 memcpy(devc.colors.colored.c_base, pdata, num_comps);
409 pdata += num_comps;
410 }
411 }
412
413 if ((flag_bits & dc_ht_colored_has_level) != 0) {
414 const byte * pdata_start = pdata;
415 gx_color_index plane_mask;
416 uint tmp_mask;
417 int i;
418
419 if (size < 1)
420 return_error(gs_error_rangecheck);
421
422 if (num_comps > 8 * sizeof(uint)) {
423 enc_u_getw(tmp_mask, pdata);
424 plane_mask = (gx_color_index)tmp_mask;
425 enc_u_getw(tmp_mask, pdata);
426 plane_mask = (gx_color_index)tmp_mask << (8 * sizeof(uint));
427 } else {
428 enc_u_getw(tmp_mask, pdata);
429 plane_mask = (gx_color_index)tmp_mask;
430 }
431 devc.colors.colored.plane_mask = plane_mask;
432 for (i = 0; i < num_comps; i++, plane_mask >>= 1) {
433 if ((plane_mask & 0x1) != 0) {
434 if (size - (pdata - pdata_start) < 1)
435 return_error(gs_error_rangecheck);
436 enc_u_getw(devc.colors.colored.c_level[i], pdata);
437 } else
438 devc.colors.colored.c_level[i] = 0;
439 }
440 size -= pdata - pdata_start;
441 }
442
443 if ((flag_bits & dc_ht_colored_alpha_is_max) != 0)
444 devc.colors.colored.alpha = gx_max_color_value;
445 else if ((flag_bits & dc_ht_colored_has_alpha) != 0) {
446 const byte * pdata_start = pdata;
447
448 if (size < 1)
449 return_error(gs_error_rangecheck);
450 enc_u_getw(devc.colors.colored.alpha, pdata);
451 size -= pdata - pdata_start;
452 }
453
454 /* set the phase as required (select value is arbitrary) */
455 color_set_phase_mod( &devc,
456 pis->screen_phase[0].x,
457 pis->screen_phase[0].y,
458 pis->dev_ht->lcm_width,
459 pis->dev_ht->lcm_height );
460
461 /* everything looks OK */
462 *pdevc = devc;
463 return pdata - pdata0;
464 }
465
466
467 /*
468 * Get the nonzero components of a coloredhalftone. This is used to
469 * distinguish components that are given zero intensity due to halftoning
470 * from those for which the original color intensity was in fact zero.
471 *
472 * An original component intensity of zero will yield a c_base value of
473 * 0 and a c_level of 0. The plane_mask field already contains the latter
474 * information, so we need only add those components for which c_base is
475 * non-zero.
476 */
477 int
gx_dc_ht_colored_get_nonzero_comps(const gx_device_color * pdevc,const gx_device * dev_ignored,gx_color_index * pcomp_bits)478 gx_dc_ht_colored_get_nonzero_comps(
479 const gx_device_color * pdevc,
480 const gx_device * dev_ignored,
481 gx_color_index * pcomp_bits )
482 {
483 int i, ncomps = pdevc->colors.colored.num_components;
484 gx_color_index comp_bits = pdevc->colors.colored.plane_mask;
485
486 for (i = 0; i < ncomps; i++) {
487 if (pdevc->colors.colored.c_base[i] != 0)
488 comp_bits |= ((gx_color_index)1) << i;
489 }
490 *pcomp_bits = comp_bits;
491
492 return 0;
493 }
494
495 /*
496 * Define an abbreviation for a heavily used value: the maximum number of
497 * of device colorants (device colors).
498 */
499 #define MAX_DCC GX_DEVICE_COLOR_MAX_COMPONENTS
500 /*
501 * Define a size for the "colors" array. For less than 5 colors, there are
502 * 2**n values stored (for a maximum of 16 values). For 5 or more colors, we
503 * only store 2 values per color so the array size can be 2 * MAX_DCC. Use which
504 * ever is larger for the array size.
505 */
506 #define MAX_DCC_16 (2 * MAX_DCC < 16 ? 16 : 2 * MAX_DCC)
507
508 /* Forward references. */
509 /* Use a typedef to attempt to work around overly picky compilers. */
510 typedef gx_color_value gx_color_value_array[MAX_DCC];
511 typedef struct color_values_pair_s {
512 gx_color_value_array values[2];
513 } color_values_pair_t;
514 #define SET_HT_COLORS_PROC(proc)\
515 int proc(\
516 color_values_pair_t *pvp,\
517 gx_color_index colors[MAX_DCC_16],\
518 const gx_const_strip_bitmap *sbits[MAX_DCC],\
519 const gx_device_color *pdevc,\
520 gx_device *dev,\
521 gx_ht_cache *caches[MAX_DCC],\
522 int nplanes\
523 )
524
525 private SET_HT_COLORS_PROC(set_ht_colors_le_4);
526 private SET_HT_COLORS_PROC(set_cmyk_1bit_colors);
527 private SET_HT_COLORS_PROC(set_ht_colors_gt_4);
528
529 #define SET_COLOR_HT_PROC(proc)\
530 void proc(\
531 byte *dest_data, /* the output tile */\
532 uint dest_raster, /* ibid. */\
533 int px, /* the initial phase of the output tile */\
534 int py,\
535 int w, /* how much of the tile to set */\
536 int h,\
537 int depth, /* depth of tile (4, 8, 16, 24, 32) */\
538 int special, /* >0 means special 1-bit CMYK */\
539 int nplanes,\
540 gx_color_index plane_mask, /* which planes are halftoned */\
541 gx_device *dev, /* in case we are mapping lazily */\
542 const color_values_pair_t *pvp, /* color values ditto */\
543 gx_color_index colors[MAX_DCC], /* the actual colors for the tile, */\
544 /* actually [nplanes] */\
545 const gx_const_strip_bitmap * sbits[MAX_DCC] /* the bitmaps for the planes, */\
546 /* actually [nplanes] */\
547 )
548
549 private SET_COLOR_HT_PROC(set_color_ht_le_4);
550 private SET_COLOR_HT_PROC(set_color_ht_gt_4);
551
552 /* Prepare to use a colored halftone, by loading the default cache. */
553 private int
gx_dc_ht_colored_load(gx_device_color * pdevc,const gs_imager_state * pis,gx_device * ignore_dev,gs_color_select_t select)554 gx_dc_ht_colored_load(gx_device_color * pdevc, const gs_imager_state * pis,
555 gx_device * ignore_dev, gs_color_select_t select)
556 {
557 /* TO_DO_DEVICEN */
558
559 return 0;
560 }
561
562 /* Fill a rectangle with a colored halftone. */
563 /* Note that we treat this as "texture" for RasterOp. */
564 private int
gx_dc_ht_colored_fill_rectangle(const gx_device_color * pdevc,int x,int y,int w,int h,gx_device * dev,gs_logical_operation_t lop,const gx_rop_source_t * source)565 gx_dc_ht_colored_fill_rectangle(const gx_device_color * pdevc,
566 int x, int y, int w, int h,
567 gx_device * dev, gs_logical_operation_t lop,
568 const gx_rop_source_t * source)
569 {
570 ulong tbits[tile_longs_allocated];
571 const uint tile_bytes = tile_longs * size_of(long);
572 gx_strip_bitmap tiles;
573 gx_rop_source_t no_source;
574 const gx_device_halftone *pdht = pdevc->colors.colored.c_ht;
575 int depth = dev->color_info.depth;
576 int nplanes = dev->color_info.num_components;
577
578 SET_HT_COLORS_PROC((*set_ht_colors)) =
579 (
580 #if USE_SLOW_CODE
581 set_ht_colors_gt_4
582 #else
583 (dev_proc(dev, map_cmyk_color) == gx_default_encode_color &&
584 dev->color_info.depth == 4) ?
585 set_cmyk_1bit_colors :
586 nplanes <= 4 ? set_ht_colors_le_4 :
587 set_ht_colors_gt_4
588 #endif
589 );
590 SET_COLOR_HT_PROC((*set_color_ht)) =
591 (
592 #if !USE_SLOW_CODE
593 !(pdevc->colors.colored.plane_mask & ~(gx_color_index)15) &&
594 set_ht_colors != set_ht_colors_gt_4 ?
595 set_color_ht_le_4 :
596 #endif
597 set_color_ht_gt_4);
598 color_values_pair_t vp;
599 gx_color_index colors[MAX_DCC_16];
600 const gx_const_strip_bitmap *sbits[MAX_DCC];
601 gx_ht_cache *caches[MAX_DCC];
602 int special;
603 int code = 0;
604 int raster;
605 uint size_x;
606 int dw, dh;
607 int lw = pdht->lcm_width, lh = pdht->lcm_height;
608 bool no_rop;
609 int i;
610
611 if (w <= 0 || h <= 0)
612 return 0;
613 if ((w | h) >= 16) {
614 /* It's worth taking the trouble to check the clipping box. */
615 gs_fixed_rect cbox;
616 int t;
617
618 dev_proc(dev, get_clipping_box)(dev, &cbox);
619 if ((t = fixed2int(cbox.p.x)) > x) {
620 if ((w += x - t) <= 0)
621 return 0;
622 x = t;
623 }
624 if ((t = fixed2int(cbox.p.y)) > y) {
625 if ((h += y - t) <= 0)
626 return 0;
627 y = t;
628 }
629 if ((t = fixed2int(cbox.q.x)) < x + w)
630 if ((w = t - x) <= 0)
631 return 0;
632 if ((t = fixed2int(cbox.q.y)) < y + h)
633 if ((h = t - y) <= 0)
634 return 0;
635 }
636 /* Colored halftone patterns are unconditionally opaque. */
637 lop &= ~lop_T_transparent;
638 if (pdht->components == 0) {
639 caches[0] = caches[1] = caches[2] = caches[3] = pdht->order.cache;
640 for (i = 4; i < nplanes; ++i)
641 caches[i] = pdht->order.cache;
642 } else {
643 gx_ht_order_component *pocs = pdht->components;
644
645
646 for (i = 0; i < nplanes; ++i)
647 caches[i] = pocs[i].corder.cache;
648 }
649 special = set_ht_colors(&vp, colors, sbits, pdevc, dev, caches, nplanes);
650 no_rop = source == NULL && lop_no_S_is_T(lop);
651 /*
652 * If the LCM of the plane cell sizes is smaller than the rectangle
653 * being filled, compute a single tile and let tile_rectangle do the
654 * replication.
655 */
656 if ((w > lw || h > lh) &&
657 (raster = bitmap_raster(lw * depth)) <= tile_bytes / lh
658 ) {
659 /*
660 * The only reason we need to do fit_fill here is that if the
661 * device is a clipper, the caller might be counting on it to do
662 * all necessary clipping. Actually, we should clip against the
663 * device's clipping box, not the default....
664 */
665 fit_fill(dev, x, y, w, h);
666 /* Check to make sure we still have a big rectangle. */
667 if (w > lw || h > lh) {
668 tiles.data = (byte *)tbits;
669 tiles.raster = raster;
670 tiles.rep_width = tiles.size.x = lw;
671 tiles.rep_height = tiles.size.y = lh;
672 tiles.id = gs_next_ids(dev->memory, 1);
673 tiles.rep_shift = tiles.shift = 0;
674 set_color_ht((byte *)tbits, raster, 0, 0, lw, lh, depth,
675 special, nplanes, pdevc->colors.colored.plane_mask,
676 dev, &vp, colors, sbits);
677 if (no_rop)
678 return (*dev_proc(dev, strip_tile_rectangle)) (dev, &tiles,
679 x, y, w, h,
680 gx_no_color_index, gx_no_color_index,
681 pdevc->phase.x, pdevc->phase.y);
682 if (source == NULL)
683 set_rop_no_source(source, no_source, dev);
684 return (*dev_proc(dev, strip_copy_rop)) (dev, source->sdata,
685 source->sourcex, source->sraster, source->id,
686 (source->use_scolors ? source->scolors : NULL),
687 &tiles, NULL,
688 x, y, w, h,
689 pdevc->phase.x, pdevc->phase.y,
690 lop);
691 }
692 }
693 size_x = w * depth;
694 raster = bitmap_raster(size_x);
695 if (raster > tile_bytes) {
696 /*
697 * We can't even do an entire line at once. See above for
698 * why we do the X equivalent of fit_fill here.
699 */
700 if (x < 0)
701 w += x, x = 0;
702 if (x > dev->width - w)
703 w = dev->width - x;
704 if (w <= 0)
705 return 0;
706 size_x = w * depth;
707 raster = bitmap_raster(size_x);
708 if (raster > tile_bytes) {
709 /* We'll have to do a partial line. */
710 dw = tile_bytes * 8 / depth;
711 size_x = dw * depth;
712 raster = bitmap_raster(size_x);
713 dh = 1;
714 goto fit;
715 }
716 }
717 /* Do as many lines as will fit. */
718 dw = w;
719 dh = tile_bytes / raster;
720 if (dh > h)
721 dh = h;
722 fit: /* Now the tile will definitely fit. */
723 if (!no_rop) {
724 tiles.data = (byte *)tbits;
725 tiles.id = gx_no_bitmap_id;
726 tiles.raster = raster;
727 tiles.rep_width = tiles.size.x = size_x / depth;
728 tiles.rep_shift = tiles.shift = 0;
729 }
730 while (w) {
731 int cy = y, ch = dh, left = h;
732
733 for (;;) {
734 set_color_ht((byte *)tbits, raster,
735 x + pdevc->phase.x, cy + pdevc->phase.y,
736 dw, ch, depth, special, nplanes,
737 pdevc->colors.colored.plane_mask,
738 dev, &vp, colors, sbits);
739 if (no_rop) {
740 code = (*dev_proc(dev, copy_color))
741 (dev, (byte *)tbits, 0, raster, gx_no_bitmap_id,
742 x, cy, dw, ch);
743 } else {
744 tiles.rep_height = tiles.size.y = ch;
745 if (source == NULL)
746 set_rop_no_source(source, no_source, dev);
747 /****** WRONG - MUST ADJUST source VALUES ******/
748 code = (*dev_proc(dev, strip_copy_rop))
749 (dev, source->sdata, source->sourcex, source->sraster,
750 source->id,
751 (source->use_scolors ? source->scolors : NULL),
752 &tiles, NULL, x, cy, dw, ch, 0, 0, lop);
753 }
754 if (code < 0)
755 return code;
756 if (!(left -= ch))
757 break;
758 cy += ch;
759 if (ch > left)
760 ch = left;
761 }
762 if (!(w -= dw))
763 break;
764 x += dw;
765 if (dw > w)
766 dw = w;
767 }
768 return code;
769 }
770
771 /* ---------------- Color table setup ---------------- */
772
773 /*
774 * We could cache this if we had a place to store it. Even a 1-element
775 * cache would help performance substantially.
776 * Key: device + c_base/c_level of device color
777 * Value: colors table
778 */
779
780 /*
781 * We construct color halftone tiles out of multiple "planes".
782 * Each plane specifies halftoning for one component (R/G/B, C/M/Y/K,
783 * or DeviceN components).
784 */
785
786 private const struct {
787 ulong pad; /* to get bytes aligned properly */
788 byte bytes[sizeof(ulong) * 8]; /* 8 is arbitrary */
789 } ht_no_bitmap_data = { 0 };
790 private const gx_const_strip_bitmap ht_no_bitmap = {
791 &ht_no_bitmap_data.bytes[0], sizeof(ulong),
792 {sizeof(ulong) * 8, sizeof(ht_no_bitmap_data.bytes) / sizeof(ulong)},
793 gx_no_bitmap_id, 1, 1, 0, 0
794 };
795
796 /* Set the color value(s) and halftone mask for one plane. */
797
798 /* Free variables: pvp, pdc, sbits, max_color */
799 #define SET_PLANE_COLOR_CONSTANT(i)\
800 BEGIN\
801 pvp->values[1][i] = pvp->values[0][i] = \
802 fractional_color(pdc->colors.colored.c_base[i], max_color);\
803 sbits[i] = &ht_no_bitmap;\
804 END
805
806 /* Free variables: pvp, pdc, sbits, caches, invert, max_color */
807 #define SET_PLANE_COLOR(i)\
808 BEGIN\
809 uint q = pdc->colors.colored.c_base[i];\
810 uint r = pdc->colors.colored.c_level[i];\
811 \
812 pvp->values[0][i] = fractional_color(q, max_color);\
813 if (r == 0)\
814 pvp->values[1][i] = pvp->values[0][i], sbits[i] = &ht_no_bitmap;\
815 else if (!invert) {\
816 pvp->values[1][i] = fractional_color(q + 1, max_color);\
817 sbits[i] = (const gx_const_strip_bitmap *)\
818 &gx_render_ht(caches[i], r)->tiles;\
819 } else { \
820 const gx_device_halftone *pdht = pdc->colors.colored.c_ht; \
821 int nlevels =\
822 (pdht->components ?\
823 pdht->components[i].corder.num_levels :\
824 pdht->order.num_levels);\
825 \
826 pvp->values[1][i] = pvp->values[0][i]; \
827 pvp->values[0][i] = fractional_color(q + 1, max_color); \
828 sbits[i] = (const gx_const_strip_bitmap *)\
829 &gx_render_ht(caches[i], nlevels - r)->tiles; \
830 }\
831 END
832
833 /* Set up the colors and the individual plane halftone bitmaps. */
834 private int
set_ht_colors_le_4(color_values_pair_t * pvp,gx_color_index colors[MAX_DCC_16],const gx_const_strip_bitmap * sbits[MAX_DCC],const gx_device_color * pdc,gx_device * dev,gx_ht_cache * caches[MAX_DCC],int nplanes)835 set_ht_colors_le_4(color_values_pair_t *pvp /* only used internally */,
836 gx_color_index colors[MAX_DCC_16] /* 16 used */,
837 const gx_const_strip_bitmap * sbits[MAX_DCC],
838 const gx_device_color * pdc, gx_device * dev,
839 gx_ht_cache * caches[MAX_DCC], int nplanes)
840 {
841 gx_color_value max_color = dev->color_info.dither_colors - 1;
842 gx_color_value cvalues[4];
843 /*
844 * NB: the halftone orders are all set up for an additive color space.
845 * To make these work with a subtractive device space such as CMYK,
846 * it is necessary to invert both the color level and the color
847 * pair. Note that if the original color was provided an additive
848 * space, this will reverse (in an approximate sense) the color
849 * conversion performed to express the color in the device space.
850 */
851 bool invert = dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE;
852
853 SET_PLANE_COLOR(0);
854 if (nplanes >= 2) {
855 SET_PLANE_COLOR(1);
856 }
857 if (nplanes >= 3) {
858 SET_PLANE_COLOR(2);
859 }
860 if (nplanes == 3) {
861 gx_color_value alpha = pdc->colors.colored.alpha;
862
863 if (alpha == gx_max_color_value) {
864 #define M(i)\
865 cvalues[0] = pvp->values[(i) & 1][0];\
866 cvalues[1] = pvp->values[((i) & 2) >> 1][1];\
867 cvalues[2] = pvp->values[(i) >> 2][2];\
868 colors[i] = dev_proc(dev, encode_color)(dev, cvalues);
869
870 M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
871 #undef M
872 } else {
873 #define M(i)\
874 colors[i] = dev_proc(dev, map_rgb_alpha_color)(dev, pvp->values[(i) & 1][0],\
875 pvp->values[((i) & 2) >> 1][1],\
876 pvp->values[(i) >> 2][2], alpha)
877 M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
878 #undef M
879 }
880 } else if (nplanes > 3){
881 SET_PLANE_COLOR(3);
882 if (nplanes > 4) {
883 /*
884 * Set colors for any planes beyond the 4th. Since this code
885 * only handles the case of at most 4 active planes, we know
886 * that any further planes are constant.
887 */
888 /****** DOESN'T MAP COLORS RIGHT, DOESN'T HANDLE ALPHA ******/
889 int pi;
890
891 for (pi = 4; pi < nplanes; ++pi)
892 SET_PLANE_COLOR_CONSTANT(pi);
893 }
894 /*
895 * For CMYK output, especially if the input was RGB, it's
896 * common for one or more of the components to be zero.
897 * Each zero component can cut the cost of color mapping in
898 * half, so it's worth doing a little checking here.
899 */
900 #define M(i)\
901 cvalues[0] = pvp->values[(i) & 1][0];\
902 cvalues[1] = pvp->values[((i) & 2) >> 1][1];\
903 cvalues[2] = pvp->values[((i) & 4) >> 2][2];\
904 cvalues[3] = pvp->values[(i) >> 3][3];\
905 colors[i] = dev_proc(dev, encode_color)(dev, cvalues)
906
907 /* We know that plane_mask <= 15. */
908 switch ((int)pdc->colors.colored.plane_mask) {
909 case 15:
910 M(15); M(14); M(13); M(12);
911 M(11); M(10); M(9); M(8);
912 case 7:
913 M(7); M(6); M(5); M(4);
914 c3: case 3:
915 M(3); M(2);
916 c1: case 1:
917 M(1);
918 break;
919 case 14:
920 M(14); M(12); M(10); M(8);
921 case 6:
922 M(6); M(4);
923 c2: case 2:
924 M(2);
925 break;
926 case 13:
927 M(13); M(12); M(9); M(8);
928 case 5:
929 M(5); M(4);
930 goto c1;
931 case 12:
932 M(12); M(8);
933 case 4:
934 M(4);
935 break;
936 case 11:
937 M(11); M(10); M(9); M(8);
938 goto c3;
939 case 10:
940 M(10); M(8);
941 goto c2;
942 case 9:
943 M(9); M(8);
944 goto c1;
945 case 8:
946 M(8);
947 break;
948 case 0:;
949 }
950 M(0);
951
952 #undef M
953 }
954 return 0;
955 }
956
957 /* Set up colors using the standard 1-bit CMYK mapping. */
958 private int
set_cmyk_1bit_colors(color_values_pair_t * ignore_pvp,gx_color_index colors[MAX_DCC_16],const gx_const_strip_bitmap * sbits[MAX_DCC],const gx_device_color * pdc,gx_device * dev,gx_ht_cache * caches[MAX_DCC],int nplanes)959 set_cmyk_1bit_colors(color_values_pair_t *ignore_pvp,
960 gx_color_index colors[MAX_DCC_16] /*2 used*/,
961 const gx_const_strip_bitmap * sbits[MAX_DCC /*4 used*/],
962 const gx_device_color * pdc, gx_device * dev,
963 gx_ht_cache * caches[MAX_DCC /*4 used*/],
964 int nplanes /*4*/)
965 {
966 const gx_device_halftone *pdht = pdc->colors.colored.c_ht;
967 /*
968 * By reversing the order of the planes, we make the pixel values
969 * line up with the color indices. Then instead of a lookup, we
970 * can compute the pixels directly using a Boolean function.
971 *
972 * We compute each output bit
973 * out[i] = (in[i] & mask1) | (~in[i] & mask0)
974 * We store the two masks in colors[0] and colors[1], since the
975 * colors array is otherwise unused in this case. We duplicate
976 * the values in all the nibbles so we can do several pixels at a time.
977 */
978 bits32 mask0 = 0, mask1 = 0;
979 #define SET_PLANE_COLOR_CMYK(i, mask)\
980 BEGIN\
981 uint r = pdc->colors.colored.c_level[i];\
982 \
983 if (r == 0) {\
984 if (pdc->colors.colored.c_base[i])\
985 mask0 |= mask, mask1 |= mask;\
986 sbits[3 - i] = &ht_no_bitmap;\
987 } else {\
988 int nlevels =\
989 (pdht->components ?\
990 pdht->components[i].corder.num_levels :\
991 pdht->order.num_levels);\
992 \
993 mask0 |= mask;\
994 sbits[3 - i] = (const gx_const_strip_bitmap *)\
995 &gx_render_ht(caches[i], nlevels - r)->tiles;\
996 }\
997 END
998 /* Suppress a compiler warning about signed/unsigned constants. */
999 SET_PLANE_COLOR_CMYK(0, /*0x88888888*/ (bits32)~0x77777777);
1000 SET_PLANE_COLOR_CMYK(1, 0x44444444);
1001 SET_PLANE_COLOR_CMYK(2, 0x22222222);
1002 SET_PLANE_COLOR_CMYK(3, 0x11111111);
1003
1004 #undef SET_PLANE_COLOR_CMYK
1005 {
1006 gx_ht_cache *ctemp;
1007
1008 ctemp = caches[0], caches[0] = caches[3], caches[3] = ctemp;
1009 ctemp = caches[1], caches[1] = caches[2], caches[2] = ctemp;
1010 }
1011 colors[0] = mask0;
1012 colors[1] = mask1;
1013 return 1;
1014 }
1015
1016 /*
1017 * Set up colors for >4 planes. In this case, we assume that the color
1018 * component values are "separable". (That we can form a gx_color_index value
1019 * for a color by a bit wise or of the gx_color_index values of the individual
1020 * components.)
1021 */
1022 private int
set_ht_colors_gt_4(color_values_pair_t * pvp,gx_color_index colors[MAX_DCC_16],const gx_const_strip_bitmap * sbits[MAX_DCC],const gx_device_color * pdc,gx_device * dev,gx_ht_cache * caches[MAX_DCC],int nplanes)1023 set_ht_colors_gt_4(color_values_pair_t *pvp,
1024 gx_color_index colors[MAX_DCC_16 /* 2 * nplanes */],
1025 const gx_const_strip_bitmap * sbits[MAX_DCC],
1026 const gx_device_color * pdc, gx_device * dev,
1027 gx_ht_cache * caches[MAX_DCC], int nplanes)
1028 {
1029 gx_color_value max_color = dev->color_info.dither_colors - 1;
1030 bool invert = dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE;
1031 gx_color_index plane_mask = pdc->colors.colored.plane_mask;
1032 int i;
1033 gx_color_value cv[MAX_DCC] = {0};
1034
1035 /* Set the color values and halftone caches. */
1036 for (i = 0; i < nplanes; ++i)
1037 if ((plane_mask >> i) & 1)
1038 SET_PLANE_COLOR(i);
1039 else
1040 SET_PLANE_COLOR_CONSTANT(i);
1041
1042 /*
1043 * Determine a gs_color_index value for each pair of component values.
1044 * We assume that an overall index value can be formed from the
1045 * bitwise or of each component. We calculate a value for both
1046 * the high and low value of each component. These are stored
1047 * in adjacent locations in 'colors'.
1048 */
1049 for (i = 0; i < nplanes; i++ ) {
1050 cv[i] = pvp->values[0][i];
1051 colors[2 * i] = dev_proc(dev, encode_color)(dev, cv);
1052 /* We only need both values for components being halftoned */
1053 if ((plane_mask >> i) & 1) {
1054 cv[i] = pvp->values[1][i];
1055 colors[2 * i + 1] = dev_proc(dev, encode_color)(dev, cv);
1056 }
1057 cv[i] = 0;
1058 }
1059
1060 return 0;
1061 }
1062
1063 /* ---------------- Color rendering ---------------- */
1064
1065 /* Define the bookkeeping structure for each plane of halftone rendering. */
1066 typedef struct tile_cursor_s {
1067 int tile_shift; /* X shift per copy of tile */
1068 int xoffset;
1069 int xshift;
1070 uint xbytes;
1071 int xbits;
1072 const byte *row;
1073 const byte *tdata;
1074 uint raster;
1075 const byte *data;
1076 int bit_shift;
1077 } tile_cursor_t;
1078
1079 /*
1080 * Initialize one plane cursor, including setting up for the first row
1081 * (data and bit_shift).
1082 */
1083 private void
init_tile_cursor(int i,tile_cursor_t * ptc,const gx_const_strip_bitmap * btile,int endx,int lasty)1084 init_tile_cursor(int i, tile_cursor_t *ptc, const gx_const_strip_bitmap *btile,
1085 int endx, int lasty)
1086 {
1087 int tw = btile->size.x;
1088 int bx = ((ptc->tile_shift = btile->shift) == 0 ? endx :
1089 endx + lasty / btile->size.y * ptc->tile_shift) % tw;
1090 int by = lasty % btile->size.y;
1091
1092 ptc->xoffset = bx >> 3;
1093 ptc->xshift = 8 - (bx & 7);
1094 ptc->xbytes = (tw - 1) >> 3;
1095 ptc->xbits = ((tw - 1) & 7) + 1;
1096 ptc->tdata = btile->data;
1097 ptc->raster = btile->raster;
1098 ptc->row = ptc->tdata + by * (int)ptc->raster;
1099 ptc->data = ptc->row + ptc->xoffset;
1100 ptc->bit_shift = ptc->xshift;
1101 if_debug6('h', "[h]plane %d: size=%d,%d shift=%d bx=%d by=%d\n",
1102 i, tw, btile->size.y, btile->shift, bx, by);
1103 }
1104
1105 /* Step a cursor to the next row. */
1106 private void
wrap_shifted_cursor(tile_cursor_t * ptc,const gx_const_strip_bitmap * psbit)1107 wrap_shifted_cursor(tile_cursor_t *ptc, const gx_const_strip_bitmap *psbit)
1108 {
1109 ptc->row += ptc->raster * (psbit->size.y - 1);
1110 if (ptc->tile_shift) {
1111 if ((ptc->xshift += ptc->tile_shift) >= 8) {
1112 if ((ptc->xoffset -= ptc->xshift >> 3) < 0) {
1113 /* wrap around in X */
1114 int bx = (ptc->xoffset << 3) + 8 - (ptc->xshift & 7) +
1115 psbit->size.x;
1116
1117 ptc->xoffset = bx >> 3;
1118 ptc->xshift = 8 - (bx & 7);
1119 } else
1120 ptc->xshift &= 7;
1121 }
1122 }
1123 }
1124 #define STEP_ROW(c, i)\
1125 BEGIN\
1126 if (c.row > c.tdata)\
1127 c.row -= c.raster;\
1128 else { /* wrap around to end of tile */\
1129 wrap_shifted_cursor(&c, sbits[i]);\
1130 }\
1131 c.data = c.row + c.xoffset;\
1132 c.bit_shift = c.xshift;\
1133 END
1134
1135 /* Define a table for expanding 8x1 bits to 8x4. */
1136 private const bits32 expand_8x1_to_8x4[256] = {
1137 #define X16(c)\
1138 c+0, c+1, c+0x10, c+0x11, c+0x100, c+0x101, c+0x110, c+0x111,\
1139 c+0x1000, c+0x1001, c+0x1010, c+0x1011, c+0x1100, c+0x1101, c+0x1110, c+0x1111
1140 X16(0x00000000), X16(0x00010000), X16(0x00100000), X16(0x00110000),
1141 X16(0x01000000), X16(0x01010000), X16(0x01100000), X16(0x01110000),
1142 X16(0x10000000), X16(0x10010000), X16(0x10100000), X16(0x10110000),
1143 X16(0x11000000), X16(0x11010000), X16(0x11100000), X16(0x11110000)
1144 #undef X16
1145 };
1146
1147 /*
1148 * Render the combined halftone for nplanes <= 4.
1149 */
1150 private void
set_color_ht_le_4(byte * dest_data,uint dest_raster,int px,int py,int w,int h,int depth,int special,int nplanes,gx_color_index plane_mask,gx_device * ignore_dev,const color_values_pair_t * ignore_pvp,gx_color_index colors[MAX_DCC_16],const gx_const_strip_bitmap * sbits[MAX_DCC])1151 set_color_ht_le_4(byte *dest_data, uint dest_raster, int px, int py,
1152 int w, int h, int depth, int special, int nplanes,
1153 gx_color_index plane_mask, gx_device *ignore_dev,
1154 const color_values_pair_t *ignore_pvp,
1155 gx_color_index colors[MAX_DCC_16],
1156 const gx_const_strip_bitmap * sbits[MAX_DCC])
1157 {
1158 /*
1159 * Note that the planes are specified in the order RGB or CMYK, but
1160 * the indices used for the internal colors array are BGR or KYMC,
1161 * except for the special 1-bit CMYK case.
1162 */
1163 int x, y;
1164 tile_cursor_t cursor[MAX_DCC];
1165 int dbytes = depth >> 3;
1166 byte *dest_row =
1167 dest_data + dest_raster * (h - 1) + (w * depth) / 8;
1168
1169 if (special > 0) {
1170 /* Planes are in reverse order. */
1171 plane_mask =
1172 "\000\010\004\014\002\012\006\016\001\011\005\015\003\013\007\017"[plane_mask];
1173 }
1174 if_debug6('h',
1175 "[h]color_ht_le_4: x=%d y=%d w=%d h=%d plane_mask=0x%lu depth=%d\n",
1176 px, py, w, h, (ulong)plane_mask, depth);
1177
1178 /* Do one-time cursor initialization. */
1179 {
1180 int endx = w + px;
1181 int lasty = h - 1 + py;
1182
1183 if (plane_mask & 1)
1184 init_tile_cursor(0, &cursor[0], sbits[0], endx, lasty);
1185 if (plane_mask & 2)
1186 init_tile_cursor(1, &cursor[1], sbits[1], endx, lasty);
1187 if (plane_mask & 4)
1188 init_tile_cursor(2, &cursor[2], sbits[2], endx, lasty);
1189 if (plane_mask & 8)
1190 init_tile_cursor(3, &cursor[3], sbits[3], endx, lasty);
1191 }
1192
1193 /* Now compute the actual tile. */
1194 for (y = h; ; dest_row -= dest_raster) {
1195 byte *dest = dest_row;
1196
1197 --y;
1198 for (x = w; x > 0;) {
1199 bits32 indices;
1200 int nx, i;
1201 register uint bits;
1202
1203 /* Get the next byte's worth of bits. Note that there may be */
1204 /* excess bits set beyond the 8th. */
1205 #define NEXT_BITS(c)\
1206 BEGIN\
1207 if (c.data > c.row) {\
1208 bits = ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
1209 c.data--;\
1210 } else {\
1211 bits = *c.data >> c.bit_shift;\
1212 c.data += c.xbytes;\
1213 if ((c.bit_shift -= c.xbits) < 0) {\
1214 bits |= *c.data << -c.bit_shift;\
1215 c.bit_shift += 8;\
1216 } else {\
1217 bits |= ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
1218 c.data--;\
1219 }\
1220 }\
1221 END
1222 if (plane_mask & 1) {
1223 NEXT_BITS(cursor[0]);
1224 indices = expand_8x1_to_8x4[bits & 0xff];
1225 } else
1226 indices = 0;
1227 if (plane_mask & 2) {
1228 NEXT_BITS(cursor[1]);
1229 indices |= expand_8x1_to_8x4[bits & 0xff] << 1;
1230 }
1231 if (plane_mask & 4) {
1232 NEXT_BITS(cursor[2]);
1233 indices |= expand_8x1_to_8x4[bits & 0xff] << 2;
1234 }
1235 if (plane_mask & 8) {
1236 NEXT_BITS(cursor[3]);
1237 indices |= expand_8x1_to_8x4[bits & 0xff] << 3;
1238 }
1239 #undef NEXT_BITS
1240 nx = min(x, 8); /* 1 <= nx <= 8 */
1241 x -= nx;
1242 switch (dbytes) {
1243 case 0: /* 4 */
1244 if (special > 0) {
1245 /* Special 1-bit CMYK. */
1246 /* Compute all the pixels at once! */
1247 indices =
1248 (indices & colors[1]) | (~indices & colors[0]);
1249 i = nx;
1250 if ((x + nx) & 1) {
1251 /* First pixel is even nibble. */
1252 *dest = (*dest & 0xf) +
1253 ((indices & 0xf) << 4);
1254 indices >>= 4;
1255 --i;
1256 }
1257 /* Now 0 <= i <= 8. */
1258 for (; (i -= 2) >= 0; indices >>= 8)
1259 *--dest = (byte)indices;
1260 /* Check for final odd nibble. */
1261 if (i & 1)
1262 *--dest = indices & 0xf;
1263 } else {
1264 /* Other 4-bit pixel */
1265 i = nx;
1266 if ((x + nx) & 1) {
1267 /* First pixel is even nibble. */
1268 *dest = (*dest & 0xf) +
1269 ((byte)colors[indices & 0xf] << 4);
1270 indices >>= 4;
1271 --i;
1272 }
1273 /* Now 0 <= i <= 8. */
1274 for (; (i -= 2) >= 0; indices >>= 8)
1275 *--dest =
1276 (byte)colors[indices & 0xf] +
1277 ((byte)colors[(indices >> 4) & 0xf]
1278 << 4);
1279 /* Check for final odd nibble. */
1280 if (i & 1)
1281 *--dest = (byte)colors[indices & 0xf];
1282 }
1283 break;
1284 case 4: /* 32 */
1285 for (i = nx; --i >= 0; indices >>= 4) {
1286 bits32 tcolor = (bits32)colors[indices & 0xf];
1287
1288 dest -= 4;
1289 dest[3] = (byte)tcolor;
1290 dest[2] = (byte)(tcolor >> 8);
1291 tcolor >>= 16;
1292 dest[1] = (byte)tcolor;
1293 dest[0] = (byte)(tcolor >> 8);
1294 }
1295 break;
1296 case 3: /* 24 */
1297 for (i = nx; --i >= 0; indices >>= 4) {
1298 bits32 tcolor = (bits32)colors[indices & 0xf];
1299
1300 dest -= 3;
1301 dest[2] = (byte) tcolor;
1302 dest[1] = (byte)(tcolor >> 8);
1303 dest[0] = (byte)(tcolor >> 16);
1304 }
1305 break;
1306 case 2: /* 16 */
1307 for (i = nx; --i >= 0; indices >>= 4) {
1308 uint tcolor =
1309 (uint)colors[indices & 0xf];
1310
1311 dest -= 2;
1312 dest[1] = (byte)tcolor;
1313 dest[0] = (byte)(tcolor >> 8);
1314 }
1315 break;
1316 case 1: /* 8 */
1317 for (i = nx; --i >= 0; indices >>= 4)
1318 *--dest = (byte)colors[indices & 0xf];
1319 break;
1320 }
1321 }
1322 if (y == 0)
1323 break;
1324
1325 if (plane_mask & 1)
1326 STEP_ROW(cursor[0], 0);
1327 if (plane_mask & 2)
1328 STEP_ROW(cursor[1], 1);
1329 if (plane_mask & 4)
1330 STEP_ROW(cursor[2], 2);
1331 if (plane_mask & 8)
1332 STEP_ROW(cursor[3], 3);
1333 }
1334 }
1335
1336 /*
1337 * Render the combined halftone for nplanes > 4. This routine assumes
1338 * that we can form a gx_color_index value by the bitwise or index values
1339 * for each of the individual components.
1340 */
1341 private void
set_color_ht_gt_4(byte * dest_data,uint dest_raster,int px,int py,int w,int h,int depth,int special,int num_planes,gx_color_index plane_mask,gx_device * dev,const color_values_pair_t * pvp,gx_color_index colors[MAX_DCC_16],const gx_const_strip_bitmap * sbits[MAX_DCC])1342 set_color_ht_gt_4(byte *dest_data, uint dest_raster, int px, int py,
1343 int w, int h, int depth, int special, int num_planes,
1344 gx_color_index plane_mask, gx_device *dev,
1345 const color_values_pair_t *pvp,
1346 gx_color_index colors[MAX_DCC_16],
1347 const gx_const_strip_bitmap * sbits[MAX_DCC])
1348 {
1349 int x, y;
1350 tile_cursor_t cursor[MAX_DCC];
1351 int dbytes = depth >> 3;
1352 byte *dest_row =
1353 dest_data + dest_raster * (h - 1) + (w * depth) / 8;
1354 int pmin, pmax;
1355 gx_color_index base_color = 0;
1356
1357 /* Compute the range of active planes. */
1358 if (plane_mask == 0)
1359 pmin = 0, pmax = -1;
1360 else {
1361 for (pmin = 0; !((plane_mask >> pmin) & 1); )
1362 ++pmin;
1363 for (pmax = 0; (plane_mask >> pmax) > 1; )
1364 ++pmax;
1365 }
1366 if_debug6('h',
1367 "[h]color_ht_gt_4: x=%d y=%d w=%d h=%d plane_mask=0x%lu depth=%d\n",
1368 px, py, w, h, (ulong)plane_mask, depth);
1369
1370 /* Do one-time cursor initialization. */
1371 {
1372 int endx = w + px;
1373 int lasty = h - 1 + py;
1374 int i;
1375
1376 for (i = pmin; i <= pmax; ++i)
1377 if ((plane_mask >> i) & 1)
1378 init_tile_cursor(i, &cursor[i], sbits[i], endx, lasty);
1379 }
1380
1381 /* Pre-load the color value for the non halftoning planes. */
1382 {
1383 int i;
1384
1385 for (i = 0; i < num_planes; ++i)
1386 if ((~plane_mask >> i) & 1)
1387 base_color |= colors[2 * i];
1388 }
1389
1390 /* Now compute the actual tile. */
1391 for (y = h; ; dest_row -= dest_raster) {
1392 byte *dest = dest_row;
1393 int i;
1394
1395 --y;
1396 for (x = w; x > 0;) {
1397 gx_color_index tcolor = base_color;
1398
1399 for (i = pmin; i <= pmax; ++i)
1400 if ((plane_mask >> i) & 1) {
1401 /* Get the next bit from an individual mask. */
1402 tile_cursor_t *ptc = &cursor[i];
1403 byte tile_bit;
1404
1405 b: if (ptc->bit_shift < 8)
1406 tile_bit = *ptc->data >> ptc->bit_shift++;
1407 else if (ptc->data > ptc->row) {
1408 tile_bit = *--(ptc->data);
1409 ptc->bit_shift = 1;
1410 } else {
1411 /* Wrap around. */
1412 ptc->data += ptc->xbytes;
1413 ptc->bit_shift = 8 - ptc->xbits;
1414 goto b;
1415 }
1416 tcolor |= colors[2 * i + (tile_bit & 1)];
1417 }
1418 --x;
1419 switch (dbytes) {
1420 case 0: /* 4 -- might be 2, but we don't support this */
1421 if (x & 1) { /* odd nibble */
1422 *--dest = (byte)tcolor;
1423 } else { /* even nibble */
1424 *dest = (*dest & 0xf) + ((byte)tcolor << 4);
1425 }
1426 break;
1427 case 4: /* 32 */
1428 dest[-4] = (byte)(tcolor >> 24);
1429 case 3: /* 24 */
1430 dest[-3] = (byte)(tcolor >> 16);
1431 case 2: /* 16 */
1432 dest[-2] = (byte)(tcolor >> 8);
1433 case 1: /* 8 */
1434 dest[-1] = (byte)tcolor;
1435 dest -= dbytes;
1436 break;
1437 }
1438 }
1439 if (y == 0)
1440 break;
1441 for (i = pmin; i <= pmax; ++i)
1442 if ((plane_mask >> i) & 1)
1443 STEP_ROW(cursor[i], i);
1444 }
1445 }
1446