changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > plan9front / sys/src/cmd/gs/src/gxcht.c

changeset 7191: 753613b3b8c4
parent: eaccc3e8d226
author: cinap_lenrek@felloff.net
date: Fri, 03 May 2019 21:10:01 +0200
permissions: -rw-r--r--
description: gs: avoid stupid shifts by casting to uint64_t
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
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 *
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
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
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)((uint64_t)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)((uint64_t)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
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  if (num_comps > 8 * sizeof(uint)) {
422  enc_u_getw(tmp_mask, pdata);
423  plane_mask = (gx_color_index)tmp_mask;
424  enc_u_getw(tmp_mask, pdata);
425  plane_mask = (gx_color_index)((uint64_t)tmp_mask << (8 * sizeof(uint)));
426  } else {
427  enc_u_getw(tmp_mask, pdata);
428  plane_mask = (gx_color_index)tmp_mask;
429  }
430  devc.colors.colored.plane_mask = plane_mask;
431  for (i = 0; i < num_comps; i++, plane_mask >>= 1) {
432  if ((plane_mask & 0x1) != 0) {
433  if (size - (pdata - pdata_start) < 1)
434  return_error(gs_error_rangecheck);
435  enc_u_getw(devc.colors.colored.c_level[i], pdata);
436  } else
437  devc.colors.colored.c_level[i] = 0;
438  }
439  size -= pdata - pdata_start;
440  }
441 
442  if ((flag_bits & dc_ht_colored_alpha_is_max) != 0)
443  devc.colors.colored.alpha = gx_max_color_value;
444  else if ((flag_bits & dc_ht_colored_has_alpha) != 0) {
445  const byte * pdata_start = pdata;
446 
447  if (size < 1)
448  return_error(gs_error_rangecheck);
449  enc_u_getw(devc.colors.colored.alpha, pdata);
450  size -= pdata - pdata_start;
451  }
452 
453  /* set the phase as required (select value is arbitrary) */
454  color_set_phase_mod( &devc,
455  pis->screen_phase[0].x,
456  pis->screen_phase[0].y,
457  pis->dev_ht->lcm_width,
458  pis->dev_ht->lcm_height );
459 
460  /* everything looks OK */
461  *pdevc = devc;
462  return pdata - pdata0;
463 }
464 
465 
466 /*
467  * Get the nonzero components of a coloredhalftone. This is used to
468  * distinguish components that are given zero intensity due to halftoning
469  * from those for which the original color intensity was in fact zero.
470  *
471  * An original component intensity of zero will yield a c_base value of
472  * 0 and a c_level of 0. The plane_mask field already contains the latter
473  * information, so we need only add those components for which c_base is
474  * non-zero.
475  */
476 int
477 gx_dc_ht_colored_get_nonzero_comps(
478  const gx_device_color * pdevc,
479  const gx_device * dev_ignored,
480  gx_color_index * pcomp_bits )
481 {
482  int i, ncomps = pdevc->colors.colored.num_components;
483  gx_color_index comp_bits = pdevc->colors.colored.plane_mask;
484 
485  for (i = 0; i < ncomps; i++) {
486  if (pdevc->colors.colored.c_base[i] != 0)
487  comp_bits |= ((gx_color_index)1) << i;
488  }
489  *pcomp_bits = comp_bits;
490 
491  return 0;
492 }
493 
494 /*
495  * Define an abbreviation for a heavily used value: the maximum number of
496  * of device colorants (device colors).
497  */
498 #define MAX_DCC GX_DEVICE_COLOR_MAX_COMPONENTS
499 /*
500  * Define a size for the "colors" array. For less than 5 colors, there are
501  * 2**n values stored (for a maximum of 16 values). For 5 or more colors, we
502  * only store 2 values per color so the array size can be 2 * MAX_DCC. Use which
503  * ever is larger for the array size.
504  */
505 #define MAX_DCC_16 (2 * MAX_DCC < 16 ? 16 : 2 * MAX_DCC)
506 
507 /* Forward references. */
508 /* Use a typedef to attempt to work around overly picky compilers. */
509 typedef gx_color_value gx_color_value_array[MAX_DCC];
510 typedef struct color_values_pair_s {
511  gx_color_value_array values[2];
512 } color_values_pair_t;
513 #define SET_HT_COLORS_PROC(proc)\
514  int proc(\
515  color_values_pair_t *pvp,\
516  gx_color_index colors[MAX_DCC_16],\
517  const gx_const_strip_bitmap *sbits[MAX_DCC],\
518  const gx_device_color *pdevc,\
519  gx_device *dev,\
520  gx_ht_cache *caches[MAX_DCC],\
521  int nplanes\
522  )
523 
524 private SET_HT_COLORS_PROC(set_ht_colors_le_4);
525 private SET_HT_COLORS_PROC(set_cmyk_1bit_colors);
526 private SET_HT_COLORS_PROC(set_ht_colors_gt_4);
527 
528 #define SET_COLOR_HT_PROC(proc)\
529  void proc(\
530  byte *dest_data, /* the output tile */\
531  uint dest_raster, /* ibid. */\
532  int px, /* the initial phase of the output tile */\
533  int py,\
534  int w, /* how much of the tile to set */\
535  int h,\
536  int depth, /* depth of tile (4, 8, 16, 24, 32) */\
537  int special, /* >0 means special 1-bit CMYK */\
538  int nplanes,\
539  gx_color_index plane_mask, /* which planes are halftoned */\
540  gx_device *dev, /* in case we are mapping lazily */\
541  const color_values_pair_t *pvp, /* color values ditto */\
542  gx_color_index colors[MAX_DCC], /* the actual colors for the tile, */\
543  /* actually [nplanes] */\
544  const gx_const_strip_bitmap * sbits[MAX_DCC] /* the bitmaps for the planes, */\
545  /* actually [nplanes] */\
546  )
547 
548 private SET_COLOR_HT_PROC(set_color_ht_le_4);
549 private SET_COLOR_HT_PROC(set_color_ht_gt_4);
550 
551 /* Prepare to use a colored halftone, by loading the default cache. */
552 private int
553 gx_dc_ht_colored_load(gx_device_color * pdevc, const gs_imager_state * pis,
554  gx_device * ignore_dev, gs_color_select_t select)
555 {
556  /* TO_DO_DEVICEN */
557 
558  return 0;
559 }
560 
561 /* Fill a rectangle with a colored halftone. */
562 /* Note that we treat this as "texture" for RasterOp. */
563 private int
564 gx_dc_ht_colored_fill_rectangle(const gx_device_color * pdevc,
565  int x, int y, int w, int h,
566  gx_device * dev, gs_logical_operation_t lop,
567  const gx_rop_source_t * source)
568 {
569  ulong tbits[tile_longs_allocated];
570  const uint tile_bytes = tile_longs * size_of(long);
571  gx_strip_bitmap tiles;
572  gx_rop_source_t no_source;
573  const gx_device_halftone *pdht = pdevc->colors.colored.c_ht;
574  int depth = dev->color_info.depth;
575  int nplanes = dev->color_info.num_components;
576 
577  SET_HT_COLORS_PROC((*set_ht_colors)) =
578  (
579 #if USE_SLOW_CODE
580  set_ht_colors_gt_4
581 #else
582  (dev_proc(dev, map_cmyk_color) == gx_default_encode_color &&
583  dev->color_info.depth == 4) ?
584  set_cmyk_1bit_colors :
585  nplanes <= 4 ? set_ht_colors_le_4 :
586  set_ht_colors_gt_4
587 #endif
588  );
589  SET_COLOR_HT_PROC((*set_color_ht)) =
590  (
591 #if !USE_SLOW_CODE
592  !(pdevc->colors.colored.plane_mask & ~(gx_color_index)15) &&
593  set_ht_colors != set_ht_colors_gt_4 ?
594  set_color_ht_le_4 :
595 #endif
596  set_color_ht_gt_4);
597  color_values_pair_t vp;
598  gx_color_index colors[MAX_DCC_16];
599  const gx_const_strip_bitmap *sbits[MAX_DCC];
600  gx_ht_cache *caches[MAX_DCC];
601  int special;
602  int code = 0;
603  int raster;
604  uint size_x;
605  int dw, dh;
606  int lw = pdht->lcm_width, lh = pdht->lcm_height;
607  bool no_rop;
608  int i;
609 
610  if (w <= 0 || h <= 0)
611  return 0;
612  if ((w | h) >= 16) {
613  /* It's worth taking the trouble to check the clipping box. */
614  gs_fixed_rect cbox;
615  int t;
616 
617  dev_proc(dev, get_clipping_box)(dev, &cbox);
618  if ((t = fixed2int(cbox.p.x)) > x) {
619  if ((w += x - t) <= 0)
620  return 0;
621  x = t;
622  }
623  if ((t = fixed2int(cbox.p.y)) > y) {
624  if ((h += y - t) <= 0)
625  return 0;
626  y = t;
627  }
628  if ((t = fixed2int(cbox.q.x)) < x + w)
629  if ((w = t - x) <= 0)
630  return 0;
631  if ((t = fixed2int(cbox.q.y)) < y + h)
632  if ((h = t - y) <= 0)
633  return 0;
634  }
635  /* Colored halftone patterns are unconditionally opaque. */
636  lop &= ~lop_T_transparent;
637  if (pdht->components == 0) {
638  caches[0] = caches[1] = caches[2] = caches[3] = pdht->order.cache;
639  for (i = 4; i < nplanes; ++i)
640  caches[i] = pdht->order.cache;
641  } else {
642  gx_ht_order_component *pocs = pdht->components;
643 
644 
645  for (i = 0; i < nplanes; ++i)
646  caches[i] = pocs[i].corder.cache;
647  }
648  special = set_ht_colors(&vp, colors, sbits, pdevc, dev, caches, nplanes);
649  no_rop = source == NULL && lop_no_S_is_T(lop);
650  /*
651  * If the LCM of the plane cell sizes is smaller than the rectangle
652  * being filled, compute a single tile and let tile_rectangle do the
653  * replication.
654  */
655  if ((w > lw || h > lh) &&
656  (raster = bitmap_raster(lw * depth)) <= tile_bytes / lh
657  ) {
658  /*
659  * The only reason we need to do fit_fill here is that if the
660  * device is a clipper, the caller might be counting on it to do
661  * all necessary clipping. Actually, we should clip against the
662  * device's clipping box, not the default....
663  */
664  fit_fill(dev, x, y, w, h);
665  /* Check to make sure we still have a big rectangle. */
666  if (w > lw || h > lh) {
667  tiles.data = (byte *)tbits;
668  tiles.raster = raster;
669  tiles.rep_width = tiles.size.x = lw;
670  tiles.rep_height = tiles.size.y = lh;
671  tiles.id = gs_next_ids(dev->memory, 1);
672  tiles.rep_shift = tiles.shift = 0;
673  set_color_ht((byte *)tbits, raster, 0, 0, lw, lh, depth,
674  special, nplanes, pdevc->colors.colored.plane_mask,
675  dev, &vp, colors, sbits);
676  if (no_rop)
677  return (*dev_proc(dev, strip_tile_rectangle)) (dev, &tiles,
678  x, y, w, h,
679  gx_no_color_index, gx_no_color_index,
680  pdevc->phase.x, pdevc->phase.y);
681  if (source == NULL)
682  set_rop_no_source(source, no_source, dev);
683  return (*dev_proc(dev, strip_copy_rop)) (dev, source->sdata,
684  source->sourcex, source->sraster, source->id,
685  (source->use_scolors ? source->scolors : NULL),
686  &tiles, NULL,
687  x, y, w, h,
688  pdevc->phase.x, pdevc->phase.y,
689  lop);
690  }
691  }
692  size_x = w * depth;
693  raster = bitmap_raster(size_x);
694  if (raster > tile_bytes) {
695  /*
696  * We can't even do an entire line at once. See above for
697  * why we do the X equivalent of fit_fill here.
698  */
699  if (x < 0)
700  w += x, x = 0;
701  if (x > dev->width - w)
702  w = dev->width - x;
703  if (w <= 0)
704  return 0;
705  size_x = w * depth;
706  raster = bitmap_raster(size_x);
707  if (raster > tile_bytes) {
708  /* We'll have to do a partial line. */
709  dw = tile_bytes * 8 / depth;
710  size_x = dw * depth;
711  raster = bitmap_raster(size_x);
712  dh = 1;
713  goto fit;
714  }
715  }
716  /* Do as many lines as will fit. */
717  dw = w;
718  dh = tile_bytes / raster;
719  if (dh > h)
720  dh = h;
721 fit: /* Now the tile will definitely fit. */
722  if (!no_rop) {
723  tiles.data = (byte *)tbits;
724  tiles.id = gx_no_bitmap_id;
725  tiles.raster = raster;
726  tiles.rep_width = tiles.size.x = size_x / depth;
727  tiles.rep_shift = tiles.shift = 0;
728  }
729  while (w) {
730  int cy = y, ch = dh, left = h;
731 
732  for (;;) {
733  set_color_ht((byte *)tbits, raster,
734  x + pdevc->phase.x, cy + pdevc->phase.y,
735  dw, ch, depth, special, nplanes,
736  pdevc->colors.colored.plane_mask,
737  dev, &vp, colors, sbits);
738  if (no_rop) {
739  code = (*dev_proc(dev, copy_color))
740  (dev, (byte *)tbits, 0, raster, gx_no_bitmap_id,
741  x, cy, dw, ch);
742  } else {
743  tiles.rep_height = tiles.size.y = ch;
744  if (source == NULL)
745  set_rop_no_source(source, no_source, dev);
746  /****** WRONG - MUST ADJUST source VALUES ******/
747  code = (*dev_proc(dev, strip_copy_rop))
748  (dev, source->sdata, source->sourcex, source->sraster,
749  source->id,
750  (source->use_scolors ? source->scolors : NULL),
751  &tiles, NULL, x, cy, dw, ch, 0, 0, lop);
752  }
753  if (code < 0)
754  return code;
755  if (!(left -= ch))
756  break;
757  cy += ch;
758  if (ch > left)
759  ch = left;
760  }
761  if (!(w -= dw))
762  break;
763  x += dw;
764  if (dw > w)
765  dw = w;
766  }
767  return code;
768 }
769 
770 /* ---------------- Color table setup ---------------- */
771 
772 /*
773  * We could cache this if we had a place to store it. Even a 1-element
774  * cache would help performance substantially.
775  * Key: device + c_base/c_level of device color
776  * Value: colors table
777  */
778 
779 /*
780  * We construct color halftone tiles out of multiple "planes".
781  * Each plane specifies halftoning for one component (R/G/B, C/M/Y/K,
782  * or DeviceN components).
783  */
784 
785 private const struct {
786  ulong pad; /* to get bytes aligned properly */
787  byte bytes[sizeof(ulong) * 8]; /* 8 is arbitrary */
788 } ht_no_bitmap_data = { 0 };
789 private const gx_const_strip_bitmap ht_no_bitmap = {
790  &ht_no_bitmap_data.bytes[0], sizeof(ulong),
791  {sizeof(ulong) * 8, sizeof(ht_no_bitmap_data.bytes) / sizeof(ulong)},
792  gx_no_bitmap_id, 1, 1, 0, 0
793 };
794 
795 /* Set the color value(s) and halftone mask for one plane. */
796 
797 /* Free variables: pvp, pdc, sbits, max_color */
798 #define SET_PLANE_COLOR_CONSTANT(i)\
799  BEGIN\
800  pvp->values[1][i] = pvp->values[0][i] = \
801  fractional_color(pdc->colors.colored.c_base[i], max_color);\
802  sbits[i] = &ht_no_bitmap;\
803  END
804 
805 /* Free variables: pvp, pdc, sbits, caches, invert, max_color */
806 #define SET_PLANE_COLOR(i)\
807  BEGIN\
808  uint q = pdc->colors.colored.c_base[i];\
809  uint r = pdc->colors.colored.c_level[i];\
810 \
811  pvp->values[0][i] = fractional_color(q, max_color);\
812  if (r == 0)\
813  pvp->values[1][i] = pvp->values[0][i], sbits[i] = &ht_no_bitmap;\
814  else if (!invert) {\
815  pvp->values[1][i] = fractional_color(q + 1, max_color);\
816  sbits[i] = (const gx_const_strip_bitmap *)\
817  &gx_render_ht(caches[i], r)->tiles;\
818  } else { \
819  const gx_device_halftone *pdht = pdc->colors.colored.c_ht; \
820  int nlevels =\
821  (pdht->components ?\
822  pdht->components[i].corder.num_levels :\
823  pdht->order.num_levels);\
824 \
825  pvp->values[1][i] = pvp->values[0][i]; \
826  pvp->values[0][i] = fractional_color(q + 1, max_color); \
827  sbits[i] = (const gx_const_strip_bitmap *)\
828  &gx_render_ht(caches[i], nlevels - r)->tiles; \
829  }\
830  END
831 
832 /* Set up the colors and the individual plane halftone bitmaps. */
833 private int
834 set_ht_colors_le_4(color_values_pair_t *pvp /* only used internally */,
835  gx_color_index colors[MAX_DCC_16] /* 16 used */,
836  const gx_const_strip_bitmap * sbits[MAX_DCC],
837  const gx_device_color * pdc, gx_device * dev,
838  gx_ht_cache * caches[MAX_DCC], int nplanes)
839 {
840  gx_color_value max_color = dev->color_info.dither_colors - 1;
841  gx_color_value cvalues[4];
842  /*
843  * NB: the halftone orders are all set up for an additive color space.
844  * To make these work with a subtractive device space such as CMYK,
845  * it is necessary to invert both the color level and the color
846  * pair. Note that if the original color was provided an additive
847  * space, this will reverse (in an approximate sense) the color
848  * conversion performed to express the color in the device space.
849  */
850  bool invert = dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE;
851 
852  SET_PLANE_COLOR(0);
853  if (nplanes >= 2) {
854  SET_PLANE_COLOR(1);
855  }
856  if (nplanes >= 3) {
857  SET_PLANE_COLOR(2);
858  }
859  if (nplanes == 3) {
860  gx_color_value alpha = pdc->colors.colored.alpha;
861 
862  if (alpha == gx_max_color_value) {
863 #define M(i)\
864  cvalues[0] = pvp->values[(i) & 1][0];\
865  cvalues[1] = pvp->values[((i) & 2) >> 1][1];\
866  cvalues[2] = pvp->values[(i) >> 2][2];\
867  colors[i] = dev_proc(dev, encode_color)(dev, cvalues);
868 
869  M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
870 #undef M
871  } else {
872 #define M(i)\
873  colors[i] = dev_proc(dev, map_rgb_alpha_color)(dev, pvp->values[(i) & 1][0],\
874  pvp->values[((i) & 2) >> 1][1],\
875  pvp->values[(i) >> 2][2], alpha)
876  M(0); M(1); M(2); M(3); M(4); M(5); M(6); M(7);
877 #undef M
878  }
879  } else if (nplanes > 3){
880  SET_PLANE_COLOR(3);
881  if (nplanes > 4) {
882  /*
883  * Set colors for any planes beyond the 4th. Since this code
884  * only handles the case of at most 4 active planes, we know
885  * that any further planes are constant.
886  */
887  /****** DOESN'T MAP COLORS RIGHT, DOESN'T HANDLE ALPHA ******/
888  int pi;
889 
890  for (pi = 4; pi < nplanes; ++pi)
891  SET_PLANE_COLOR_CONSTANT(pi);
892  }
893  /*
894  * For CMYK output, especially if the input was RGB, it's
895  * common for one or more of the components to be zero.
896  * Each zero component can cut the cost of color mapping in
897  * half, so it's worth doing a little checking here.
898  */
899 #define M(i)\
900  cvalues[0] = pvp->values[(i) & 1][0];\
901  cvalues[1] = pvp->values[((i) & 2) >> 1][1];\
902  cvalues[2] = pvp->values[((i) & 4) >> 2][2];\
903  cvalues[3] = pvp->values[(i) >> 3][3];\
904  colors[i] = dev_proc(dev, encode_color)(dev, cvalues)
905 
906  /* We know that plane_mask <= 15. */
907  switch ((int)pdc->colors.colored.plane_mask) {
908  case 15:
909  M(15); M(14); M(13); M(12);
910  M(11); M(10); M(9); M(8);
911  case 7:
912  M(7); M(6); M(5); M(4);
913 c3: case 3:
914  M(3); M(2);
915 c1: case 1:
916  M(1);
917  break;
918  case 14:
919  M(14); M(12); M(10); M(8);
920  case 6:
921  M(6); M(4);
922 c2: case 2:
923  M(2);
924  break;
925  case 13:
926  M(13); M(12); M(9); M(8);
927  case 5:
928  M(5); M(4);
929  goto c1;
930  case 12:
931  M(12); M(8);
932  case 4:
933  M(4);
934  break;
935  case 11:
936  M(11); M(10); M(9); M(8);
937  goto c3;
938  case 10:
939  M(10); M(8);
940  goto c2;
941  case 9:
942  M(9); M(8);
943  goto c1;
944  case 8:
945  M(8);
946  break;
947  case 0:;
948  }
949  M(0);
950 
951 #undef M
952  }
953  return 0;
954 }
955 
956 /* Set up colors using the standard 1-bit CMYK mapping. */
957 private int
958 set_cmyk_1bit_colors(color_values_pair_t *ignore_pvp,
959  gx_color_index colors[MAX_DCC_16] /*2 used*/,
960  const gx_const_strip_bitmap * sbits[MAX_DCC /*4 used*/],
961  const gx_device_color * pdc, gx_device * dev,
962  gx_ht_cache * caches[MAX_DCC /*4 used*/],
963  int nplanes /*4*/)
964 {
965  const gx_device_halftone *pdht = pdc->colors.colored.c_ht;
966  /*
967  * By reversing the order of the planes, we make the pixel values
968  * line up with the color indices. Then instead of a lookup, we
969  * can compute the pixels directly using a Boolean function.
970  *
971  * We compute each output bit
972  * out[i] = (in[i] & mask1) | (~in[i] & mask0)
973  * We store the two masks in colors[0] and colors[1], since the
974  * colors array is otherwise unused in this case. We duplicate
975  * the values in all the nibbles so we can do several pixels at a time.
976  */
977  bits32 mask0 = 0, mask1 = 0;
978 #define SET_PLANE_COLOR_CMYK(i, mask)\
979  BEGIN\
980  uint r = pdc->colors.colored.c_level[i];\
981 \
982  if (r == 0) {\
983  if (pdc->colors.colored.c_base[i])\
984  mask0 |= mask, mask1 |= mask;\
985  sbits[3 - i] = &ht_no_bitmap;\
986  } else {\
987  int nlevels =\
988  (pdht->components ?\
989  pdht->components[i].corder.num_levels :\
990  pdht->order.num_levels);\
991 \
992  mask0 |= mask;\
993  sbits[3 - i] = (const gx_const_strip_bitmap *)\
994  &gx_render_ht(caches[i], nlevels - r)->tiles;\
995  }\
996  END
997  /* Suppress a compiler warning about signed/unsigned constants. */
998  SET_PLANE_COLOR_CMYK(0, /*0x88888888*/ (bits32)~0x77777777);
999  SET_PLANE_COLOR_CMYK(1, 0x44444444);
1000  SET_PLANE_COLOR_CMYK(2, 0x22222222);
1001  SET_PLANE_COLOR_CMYK(3, 0x11111111);
1002 
1003 #undef SET_PLANE_COLOR_CMYK
1004  {
1005  gx_ht_cache *ctemp;
1006 
1007  ctemp = caches[0], caches[0] = caches[3], caches[3] = ctemp;
1008  ctemp = caches[1], caches[1] = caches[2], caches[2] = ctemp;
1009  }
1010  colors[0] = mask0;
1011  colors[1] = mask1;
1012  return 1;
1013 }
1014 
1015 /*
1016  * Set up colors for >4 planes. In this case, we assume that the color
1017  * component values are "separable". (That we can form a gx_color_index value
1018  * for a color by a bit wise or of the gx_color_index values of the individual
1019  * components.)
1020  */
1021 private int
1022 set_ht_colors_gt_4(color_values_pair_t *pvp,
1023  gx_color_index colors[MAX_DCC_16 /* 2 * nplanes */],
1024  const gx_const_strip_bitmap * sbits[MAX_DCC],
1025  const gx_device_color * pdc, gx_device * dev,
1026  gx_ht_cache * caches[MAX_DCC], int nplanes)
1027 {
1028  gx_color_value max_color = dev->color_info.dither_colors - 1;
1029  bool invert = dev->color_info.polarity == GX_CINFO_POLARITY_SUBTRACTIVE;
1030  gx_color_index plane_mask = pdc->colors.colored.plane_mask;
1031  int i;
1032  gx_color_value cv[MAX_DCC] = {0};
1033 
1034  /* Set the color values and halftone caches. */
1035  for (i = 0; i < nplanes; ++i)
1036  if ((plane_mask >> i) & 1)
1037  SET_PLANE_COLOR(i);
1038  else
1039  SET_PLANE_COLOR_CONSTANT(i);
1040 
1041  /*
1042  * Determine a gs_color_index value for each pair of component values.
1043  * We assume that an overall index value can be formed from the
1044  * bitwise or of each component. We calculate a value for both
1045  * the high and low value of each component. These are stored
1046  * in adjacent locations in 'colors'.
1047  */
1048  for (i = 0; i < nplanes; i++ ) {
1049  cv[i] = pvp->values[0][i];
1050  colors[2 * i] = dev_proc(dev, encode_color)(dev, cv);
1051  /* We only need both values for components being halftoned */
1052  if ((plane_mask >> i) & 1) {
1053  cv[i] = pvp->values[1][i];
1054  colors[2 * i + 1] = dev_proc(dev, encode_color)(dev, cv);
1055  }
1056  cv[i] = 0;
1057  }
1058 
1059  return 0;
1060 }
1061 
1062 /* ---------------- Color rendering ---------------- */
1063 
1064 /* Define the bookkeeping structure for each plane of halftone rendering. */
1065 typedef struct tile_cursor_s {
1066  int tile_shift; /* X shift per copy of tile */
1067  int xoffset;
1068  int xshift;
1069  uint xbytes;
1070  int xbits;
1071  const byte *row;
1072  const byte *tdata;
1073  uint raster;
1074  const byte *data;
1075  int bit_shift;
1076 } tile_cursor_t;
1077 
1078 /*
1079  * Initialize one plane cursor, including setting up for the first row
1080  * (data and bit_shift).
1081  */
1082 private void
1083 init_tile_cursor(int i, tile_cursor_t *ptc, const gx_const_strip_bitmap *btile,
1084  int endx, int lasty)
1085 {
1086  int tw = btile->size.x;
1087  int bx = ((ptc->tile_shift = btile->shift) == 0 ? endx :
1088  endx + lasty / btile->size.y * ptc->tile_shift) % tw;
1089  int by = lasty % btile->size.y;
1090 
1091  ptc->xoffset = bx >> 3;
1092  ptc->xshift = 8 - (bx & 7);
1093  ptc->xbytes = (tw - 1) >> 3;
1094  ptc->xbits = ((tw - 1) & 7) + 1;
1095  ptc->tdata = btile->data;
1096  ptc->raster = btile->raster;
1097  ptc->row = ptc->tdata + by * (int)ptc->raster;
1098  ptc->data = ptc->row + ptc->xoffset;
1099  ptc->bit_shift = ptc->xshift;
1100  if_debug6('h', "[h]plane %d: size=%d,%d shift=%d bx=%d by=%d\n",
1101  i, tw, btile->size.y, btile->shift, bx, by);
1102 }
1103 
1104 /* Step a cursor to the next row. */
1105 private void
1106 wrap_shifted_cursor(tile_cursor_t *ptc, const gx_const_strip_bitmap *psbit)
1107 {
1108  ptc->row += ptc->raster * (psbit->size.y - 1);
1109  if (ptc->tile_shift) {
1110  if ((ptc->xshift += ptc->tile_shift) >= 8) {
1111  if ((ptc->xoffset -= ptc->xshift >> 3) < 0) {
1112  /* wrap around in X */
1113  int bx = (ptc->xoffset << 3) + 8 - (ptc->xshift & 7) +
1114  psbit->size.x;
1115 
1116  ptc->xoffset = bx >> 3;
1117  ptc->xshift = 8 - (bx & 7);
1118  } else
1119  ptc->xshift &= 7;
1120  }
1121  }
1122 }
1123 #define STEP_ROW(c, i)\
1124  BEGIN\
1125  if (c.row > c.tdata)\
1126  c.row -= c.raster;\
1127  else { /* wrap around to end of tile */\
1128  wrap_shifted_cursor(&c, sbits[i]);\
1129  }\
1130  c.data = c.row + c.xoffset;\
1131  c.bit_shift = c.xshift;\
1132  END
1133 
1134 /* Define a table for expanding 8x1 bits to 8x4. */
1135 private const bits32 expand_8x1_to_8x4[256] = {
1136 #define X16(c)\
1137  c+0, c+1, c+0x10, c+0x11, c+0x100, c+0x101, c+0x110, c+0x111,\
1138  c+0x1000, c+0x1001, c+0x1010, c+0x1011, c+0x1100, c+0x1101, c+0x1110, c+0x1111
1139  X16(0x00000000), X16(0x00010000), X16(0x00100000), X16(0x00110000),
1140  X16(0x01000000), X16(0x01010000), X16(0x01100000), X16(0x01110000),
1141  X16(0x10000000), X16(0x10010000), X16(0x10100000), X16(0x10110000),
1142  X16(0x11000000), X16(0x11010000), X16(0x11100000), X16(0x11110000)
1143 #undef X16
1144 };
1145 
1146 /*
1147  * Render the combined halftone for nplanes <= 4.
1148  */
1149 private void
1150 set_color_ht_le_4(byte *dest_data, uint dest_raster, int px, int py,
1151  int w, int h, int depth, int special, int nplanes,
1152  gx_color_index plane_mask, gx_device *ignore_dev,
1153  const color_values_pair_t *ignore_pvp,
1154  gx_color_index colors[MAX_DCC_16],
1155  const gx_const_strip_bitmap * sbits[MAX_DCC])
1156 {
1157  /*
1158  * Note that the planes are specified in the order RGB or CMYK, but
1159  * the indices used for the internal colors array are BGR or KYMC,
1160  * except for the special 1-bit CMYK case.
1161  */
1162  int x, y;
1163  tile_cursor_t cursor[MAX_DCC];
1164  int dbytes = depth >> 3;
1165  byte *dest_row =
1166  dest_data + dest_raster * (h - 1) + (w * depth) / 8;
1167 
1168  if (special > 0) {
1169  /* Planes are in reverse order. */
1170  plane_mask =
1171  "\000\010\004\014\002\012\006\016\001\011\005\015\003\013\007\017"[plane_mask];
1172  }
1173  if_debug6('h',
1174  "[h]color_ht_le_4: x=%d y=%d w=%d h=%d plane_mask=0x%lu depth=%d\n",
1175  px, py, w, h, (ulong)plane_mask, depth);
1176 
1177  /* Do one-time cursor initialization. */
1178  {
1179  int endx = w + px;
1180  int lasty = h - 1 + py;
1181 
1182  if (plane_mask & 1)
1183  init_tile_cursor(0, &cursor[0], sbits[0], endx, lasty);
1184  if (plane_mask & 2)
1185  init_tile_cursor(1, &cursor[1], sbits[1], endx, lasty);
1186  if (plane_mask & 4)
1187  init_tile_cursor(2, &cursor[2], sbits[2], endx, lasty);
1188  if (plane_mask & 8)
1189  init_tile_cursor(3, &cursor[3], sbits[3], endx, lasty);
1190  }
1191 
1192  /* Now compute the actual tile. */
1193  for (y = h; ; dest_row -= dest_raster) {
1194  byte *dest = dest_row;
1195 
1196  --y;
1197  for (x = w; x > 0;) {
1198  bits32 indices;
1199  int nx, i;
1200  register uint bits;
1201 
1202 /* Get the next byte's worth of bits. Note that there may be */
1203 /* excess bits set beyond the 8th. */
1204 #define NEXT_BITS(c)\
1205  BEGIN\
1206  if (c.data > c.row) {\
1207  bits = ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
1208  c.data--;\
1209  } else {\
1210  bits = *c.data >> c.bit_shift;\
1211  c.data += c.xbytes;\
1212  if ((c.bit_shift -= c.xbits) < 0) {\
1213  bits |= *c.data << -c.bit_shift;\
1214  c.bit_shift += 8;\
1215  } else {\
1216  bits |= ((c.data[-1] << 8) | *c.data) >> c.bit_shift;\
1217  c.data--;\
1218  }\
1219  }\
1220  END
1221  if (plane_mask & 1) {
1222  NEXT_BITS(cursor[0]);
1223  indices = expand_8x1_to_8x4[bits & 0xff];
1224  } else
1225  indices = 0;
1226  if (plane_mask & 2) {
1227  NEXT_BITS(cursor[1]);
1228  indices |= expand_8x1_to_8x4[bits & 0xff] << 1;
1229  }
1230  if (plane_mask & 4) {
1231  NEXT_BITS(cursor[2]);
1232  indices |= expand_8x1_to_8x4[bits & 0xff] << 2;
1233  }
1234  if (plane_mask & 8) {
1235  NEXT_BITS(cursor[3]);
1236  indices |= expand_8x1_to_8x4[bits & 0xff] << 3;
1237  }
1238 #undef NEXT_BITS
1239  nx = min(x, 8); /* 1 <= nx <= 8 */
1240  x -= nx;
1241  switch (dbytes) {
1242  case 0: /* 4 */
1243  if (special > 0) {
1244  /* Special 1-bit CMYK. */
1245  /* Compute all the pixels at once! */
1246  indices =
1247  (indices & colors[1]) | (~indices & colors[0]);
1248  i = nx;
1249  if ((x + nx) & 1) {
1250  /* First pixel is even nibble. */
1251  *dest = (*dest & 0xf) +
1252  ((indices & 0xf) << 4);
1253  indices >>= 4;
1254  --i;
1255  }
1256  /* Now 0 <= i <= 8. */
1257  for (; (i -= 2) >= 0; indices >>= 8)
1258  *--dest = (byte)indices;
1259  /* Check for final odd nibble. */
1260  if (i & 1)
1261  *--dest = indices & 0xf;
1262  } else {
1263  /* Other 4-bit pixel */
1264  i = nx;
1265  if ((x + nx) & 1) {
1266  /* First pixel is even nibble. */
1267  *dest = (*dest & 0xf) +
1268  ((byte)colors[indices & 0xf] << 4);
1269  indices >>= 4;
1270  --i;
1271  }
1272  /* Now 0 <= i <= 8. */
1273  for (; (i -= 2) >= 0; indices >>= 8)
1274  *--dest =
1275  (byte)colors[indices & 0xf] +
1276  ((byte)colors[(indices >> 4) & 0xf]
1277  << 4);
1278  /* Check for final odd nibble. */
1279  if (i & 1)
1280  *--dest = (byte)colors[indices & 0xf];
1281  }
1282  break;
1283  case 4: /* 32 */
1284  for (i = nx; --i >= 0; indices >>= 4) {
1285  bits32 tcolor = (bits32)colors[indices & 0xf];
1286 
1287  dest -= 4;
1288  dest[3] = (byte)tcolor;
1289  dest[2] = (byte)(tcolor >> 8);
1290  tcolor >>= 16;
1291  dest[1] = (byte)tcolor;
1292  dest[0] = (byte)(tcolor >> 8);
1293  }
1294  break;
1295  case 3: /* 24 */
1296  for (i = nx; --i >= 0; indices >>= 4) {
1297  bits32 tcolor = (bits32)colors[indices & 0xf];
1298 
1299  dest -= 3;
1300  dest[2] = (byte) tcolor;
1301  dest[1] = (byte)(tcolor >> 8);
1302  dest[0] = (byte)(tcolor >> 16);
1303  }
1304  break;
1305  case 2: /* 16 */
1306  for (i = nx; --i >= 0; indices >>= 4) {
1307  uint tcolor =
1308  (uint)colors[indices & 0xf];
1309 
1310  dest -= 2;
1311  dest[1] = (byte)tcolor;
1312  dest[0] = (byte)(tcolor >> 8);
1313  }
1314  break;
1315  case 1: /* 8 */
1316  for (i = nx; --i >= 0; indices >>= 4)
1317  *--dest = (byte)colors[indices & 0xf];
1318  break;
1319  }
1320  }
1321  if (y == 0)
1322  break;
1323 
1324  if (plane_mask & 1)
1325  STEP_ROW(cursor[0], 0);
1326  if (plane_mask & 2)
1327  STEP_ROW(cursor[1], 1);
1328  if (plane_mask & 4)
1329  STEP_ROW(cursor[2], 2);
1330  if (plane_mask & 8)
1331  STEP_ROW(cursor[3], 3);
1332  }
1333 }
1334 
1335 /*
1336  * Render the combined halftone for nplanes > 4. This routine assumes
1337  * that we can form a gx_color_index value by the bitwise or index values
1338  * for each of the individual components.
1339  */
1340 private void
1341 set_color_ht_gt_4(byte *dest_data, uint dest_raster, int px, int py,
1342  int w, int h, int depth, int special, int num_planes,
1343  gx_color_index plane_mask, gx_device *dev,
1344  const color_values_pair_t *pvp,
1345  gx_color_index colors[MAX_DCC_16],
1346  const gx_const_strip_bitmap * sbits[MAX_DCC])
1347 {
1348  int x, y;
1349  tile_cursor_t cursor[MAX_DCC];
1350  int dbytes = depth >> 3;
1351  byte *dest_row =
1352  dest_data + dest_raster * (h - 1) + (w * depth) / 8;
1353  int pmin, pmax;
1354  gx_color_index base_color = 0;
1355 
1356  /* Compute the range of active planes. */
1357  if (plane_mask == 0)
1358  pmin = 0, pmax = -1;
1359  else {
1360  for (pmin = 0; !((plane_mask >> pmin) & 1); )
1361  ++pmin;
1362  for (pmax = 0; (plane_mask >> pmax) > 1; )
1363  ++pmax;
1364  }
1365  if_debug6('h',
1366  "[h]color_ht_gt_4: x=%d y=%d w=%d h=%d plane_mask=0x%lu depth=%d\n",
1367  px, py, w, h, (ulong)plane_mask, depth);
1368 
1369  /* Do one-time cursor initialization. */
1370  {
1371  int endx = w + px;
1372  int lasty = h - 1 + py;
1373  int i;
1374 
1375  for (i = pmin; i <= pmax; ++i)
1376  if ((plane_mask >> i) & 1)
1377  init_tile_cursor(i, &cursor[i], sbits[i], endx, lasty);
1378  }
1379 
1380  /* Pre-load the color value for the non halftoning planes. */
1381  {
1382  int i;
1383 
1384  for (i = 0; i < num_planes; ++i)
1385  if ((~plane_mask >> i) & 1)
1386  base_color |= colors[2 * i];
1387  }
1388 
1389  /* Now compute the actual tile. */
1390  for (y = h; ; dest_row -= dest_raster) {
1391  byte *dest = dest_row;
1392  int i;
1393 
1394  --y;
1395  for (x = w; x > 0;) {
1396  gx_color_index tcolor = base_color;
1397 
1398  for (i = pmin; i <= pmax; ++i)
1399  if ((plane_mask >> i) & 1) {
1400  /* Get the next bit from an individual mask. */
1401  tile_cursor_t *ptc = &cursor[i];
1402  byte tile_bit;
1403 
1404 b: if (ptc->bit_shift < 8)
1405  tile_bit = *ptc->data >> ptc->bit_shift++;
1406  else if (ptc->data > ptc->row) {
1407  tile_bit = *--(ptc->data);
1408  ptc->bit_shift = 1;
1409  } else {
1410  /* Wrap around. */
1411  ptc->data += ptc->xbytes;
1412  ptc->bit_shift = 8 - ptc->xbits;
1413  goto b;
1414  }
1415  tcolor |= colors[2 * i + (tile_bit & 1)];
1416  }
1417  --x;
1418  switch (dbytes) {
1419  case 0: /* 4 -- might be 2, but we don't support this */
1420  if (x & 1) { /* odd nibble */
1421  *--dest = (byte)tcolor;
1422  } else { /* even nibble */
1423  *dest = (*dest & 0xf) + ((byte)tcolor << 4);
1424  }
1425  break;
1426  case 4: /* 32 */
1427  dest[-4] = (byte)(tcolor >> 24);
1428  case 3: /* 24 */
1429  dest[-3] = (byte)(tcolor >> 16);
1430  case 2: /* 16 */
1431  dest[-2] = (byte)(tcolor >> 8);
1432  case 1: /* 8 */
1433  dest[-1] = (byte)tcolor;
1434  dest -= dbytes;
1435  break;
1436  }
1437  }
1438  if (y == 0)
1439  break;
1440  for (i = pmin; i <= pmax; ++i)
1441  if ((plane_mask >> i) & 1)
1442  STEP_ROW(cursor[i], i);
1443  }
1444 }