1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 /* Default implementation of device get_bits[_rectangle] */
17 #include "memory_.h"
18 #include "gx.h"
19 #include "gserrors.h"
20 #include "gxdevice.h"
21 #include "gxdevmem.h"
22 #include "gxgetbit.h"
23 #include "gxlum.h"
24 #include "gdevmem.h"
25 #include "gxdevsop.h"
26 
27 int
gx_no_get_bits(gx_device * dev,int y,byte * data,byte ** actual_data)28 gx_no_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
29 {
30     return_error(gs_error_unknownerror);
31 }
32 int
gx_default_get_bits(gx_device * dev,int y,byte * data,byte ** actual_data)33 gx_default_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
34 {	/*
35          * Hand off to get_bits_rectangle, being careful to avoid a
36          * possible recursion loop.
37          */
38     dev_proc_get_bits((*save_get_bits)) = dev_proc(dev, get_bits);
39     gs_int_rect rect;
40     gs_get_bits_params_t params;
41     int code;
42 
43     rect.p.x = 0, rect.p.y = y;
44     rect.q.x = dev->width, rect.q.y = y + 1;
45     params.options =
46         (actual_data ? GB_RETURN_POINTER : 0) | GB_RETURN_COPY |
47         (GB_ALIGN_STANDARD | GB_OFFSET_0 | GB_RASTER_STANDARD |
48     /* No depth specified, we always use native colors. */
49          GB_PACKING_CHUNKY | GB_COLORS_NATIVE | GB_ALPHA_NONE);
50     params.x_offset = 0;
51     params.raster = bitmap_raster(dev->width * dev->color_info.depth);
52     params.data[0] = data;
53     set_dev_proc(dev, get_bits, gx_no_get_bits);
54     code = (*dev_proc(dev, get_bits_rectangle))
55         (dev, &rect, &params, NULL);
56     if (actual_data)
57         *actual_data = params.data[0];
58     set_dev_proc(dev, get_bits, save_get_bits);
59     return code;
60 }
61 
62 /*
63  * Determine whether we can satisfy a request by simply using the stored
64  * representation.  dev is used only for color_info.{num_components, depth}.
65  */
66 static bool
requested_includes_stored(const gx_device * dev,const gs_get_bits_params_t * requested,const gs_get_bits_params_t * stored)67 requested_includes_stored(const gx_device *dev,
68                           const gs_get_bits_params_t *requested,
69                           const gs_get_bits_params_t *stored)
70 {
71     gs_get_bits_options_t both = requested->options & stored->options;
72 
73     if (!(both & GB_PACKING_ALL))
74         return false;
75     if (stored->options & GB_SELECT_PLANES) {
76         /*
77          * The device only provides a subset of the planes.
78          * Make sure it provides all the requested ones.
79          */
80         int i;
81         int n = (stored->options & GB_PACKING_BIT_PLANAR ?
82                  dev->color_info.depth : dev->color_info.num_components);
83 
84         if (!(requested->options & GB_SELECT_PLANES) ||
85             !(both & (GB_PACKING_PLANAR | GB_PACKING_BIT_PLANAR))
86             )
87             return false;
88         for (i = 0; i < n; ++i)
89             if (requested->data[i] && !stored->data[i])
90                 return false;
91     }
92     if (both & GB_COLORS_NATIVE)
93         return true;
94     if (both & GB_COLORS_STANDARD_ALL) {
95         if ((both & GB_ALPHA_ALL) && (both & GB_DEPTH_ALL))
96             return true;
97     }
98     return false;
99 }
100 
101 /*
102  * Try to implement get_bits_rectangle by returning a pointer.
103  * Note that dev is used only for computing the default raster
104  * and for color_info.depth.
105  * This routine does not check x or h for validity.
106  */
107 int
gx_get_bits_return_pointer(gx_device * dev,int x,int h,gs_get_bits_params_t * params,const gs_get_bits_params_t * stored,byte ** stored_base)108 gx_get_bits_return_pointer(gx_device * dev, int x, int h,
109                            gs_get_bits_params_t *params,
110                            const gs_get_bits_params_t *stored,
111                            byte **stored_base)
112 {
113     gs_get_bits_options_t options = params->options;
114     gs_get_bits_options_t both = options & stored->options;
115 
116     if (!(options & GB_RETURN_POINTER) ||
117         !requested_includes_stored(dev, params, stored)
118         )
119         return -1;
120     /*
121      * See whether we can return the bits in place.  Note that even if
122      * OFFSET_ANY isn't set, x_offset and x don't have to be equal: their
123      * bit offsets only have to match modulo align_bitmap_mod * 8 (to
124      * preserve alignment) if ALIGN_ANY isn't set, or mod 8 (since
125      * byte alignment is always required) if ALIGN_ANY is set.
126      */
127     {
128         int depth = dev->color_info.depth;
129         /*
130          * For PLANAR devices, we assume that each plane consists of
131          * depth/num_components bits.  This is wrong in general, but if
132          * the device wants something else, it should implement
133          * get_bits_rectangle itself.
134          */
135         uint dev_raster = gx_device_raster(dev, true);
136         uint raster =
137             (options & (GB_RASTER_STANDARD | GB_RASTER_ANY) ? dev_raster :
138              params->raster);
139         byte *base;
140 
141         if (h <= 1 || raster == dev_raster) {
142             int x_offset =
143                 (options & GB_OFFSET_ANY ? x :
144                  options & GB_OFFSET_0 ? 0 : params->x_offset);
145 
146             if (x_offset == x) {
147                 base = stored_base[0];
148                 params->x_offset = x;
149             } else {
150                 uint align_mod =
151                     (options & GB_ALIGN_ANY ? 8 : align_bitmap_mod * 8);
152                 int bit_offset = x - x_offset;
153                 int bytes;
154 
155                 if (bit_offset & (align_mod - 1))
156                     return -1;	/* can't align */
157                 if (depth & (depth - 1)) {
158                     /* step = lcm(depth, align_mod) */
159                     int step = depth / igcd(depth, align_mod) * align_mod;
160 
161                     bytes = bit_offset / step * step;
162                 } else {
163                     /* Use a faster algorithm if depth is a power of 2. */
164                     bytes = bit_offset & (-depth & -(int)align_mod);
165                 }
166                 base = stored_base[0] + arith_rshift(bytes, 3);
167                 params->x_offset = (bit_offset - bytes) / depth;
168             }
169             params->options =
170                 GB_ALIGN_STANDARD | GB_RETURN_POINTER | GB_RASTER_STANDARD |
171                 (stored->options & ~GB_PACKING_ALL) /*see below for PACKING*/ |
172                 (params->x_offset == 0 ? GB_OFFSET_0 : GB_OFFSET_SPECIFIED);
173             if (both & GB_PACKING_CHUNKY) {
174                 params->options |= GB_PACKING_CHUNKY;
175                 params->data[0] = base;
176             } else {
177                 int n =
178                     (stored->options & GB_PACKING_BIT_PLANAR ?
179                        (params->options |= GB_PACKING_BIT_PLANAR,
180                         dev->color_info.depth) :
181                        (params->options |= GB_PACKING_PLANAR,
182                         dev->color_info.num_components));
183                 int i;
184 
185                 for (i = 0; i < n; ++i) {
186                     if (!(both & GB_SELECT_PLANES) || stored->data[i] != 0) {
187                         params->data[i] = base;
188                     }
189                     if (i < n-1) {
190                         base += stored_base[dev->height]-stored_base[0];
191                         stored_base += dev->height;
192                     }
193                 }
194             }
195             return 0;
196         }
197     }
198     return -1;
199 }
200 
201 /*
202  * Implement gx_get_bits_copy (see below) for the case of converting
203  * 4-bit CMYK to 24-bit RGB with standard mapping, used heavily by PCL.
204  */
205 static void
gx_get_bits_copy_cmyk_1bit(byte * dest_line,uint dest_raster,const byte * src_line,uint src_raster,int src_bit,int w,int h)206 gx_get_bits_copy_cmyk_1bit(byte *dest_line, uint dest_raster,
207                            const byte *src_line, uint src_raster,
208                            int src_bit, int w, int h)
209 {
210     for (; h > 0; dest_line += dest_raster, src_line += src_raster, --h) {
211         const byte *src = src_line;
212         byte *dest = dest_line;
213         bool hi = (src_bit & 4) != 0;  /* last nibble processed was hi */
214         int i;
215 
216         for (i = w; i > 0; dest += 3, --i) {
217             uint pixel =
218                 ((hi = !hi)? *src >> 4 : *src++ & 0xf);
219 
220             if (pixel & 1)
221                 dest[0] = dest[1] = dest[2] = 0;
222             else {
223                 dest[0] = (byte)((pixel >> 3) - 1);
224                 dest[1] = (byte)(((pixel >> 2) & 1) - 1);
225                 dest[2] = (byte)(((pixel >> 1) & 1) - 1);
226             }
227         }
228     }
229 }
230 
231 /*
232  * Convert pixels between representations, primarily for get_bits_rectangle.
233  * stored indicates how the data are actually stored, and includes:
234  *      - one option from the GB_PACKING group;
235  *      - if h > 1, one option from the GB_RASTER group;
236  *      - optionally (and normally), GB_COLORS_NATIVE;
237  *      - optionally, one option each from the GB_COLORS_STANDARD, GB_DEPTH,
238  *      and GB_ALPHA groups.
239  * Note that dev is used only for color mapping.  This routine assumes that
240  * the stored data are aligned.
241  *
242  * Note: this routine does not check x, w, h for validity.
243  *
244  * The code for converting between standard and native colors has been
245  * factored out into single-use procedures strictly for readability.
246  * A good optimizing compiler would compile them in-line.
247  */
248 static int
249     gx_get_bits_std_to_native(gx_device * dev, int x, int w, int h,
250                                   gs_get_bits_params_t * params,
251                               const gs_get_bits_params_t *stored,
252                               const byte * src_base, uint dev_raster,
253                               int x_offset, uint raster),
254     gx_get_bits_native_to_std(gx_device * dev, int x, int w, int h,
255                               gs_get_bits_params_t * params,
256                               const gs_get_bits_params_t *stored,
257                               const byte * src_base, uint dev_raster,
258                               int x_offset, uint raster, uint std_raster);
259 int
gx_get_bits_copy(gx_device * dev,int x,int w,int h,gs_get_bits_params_t * params,const gs_get_bits_params_t * stored,const byte * src_base,uint dev_raster)260 gx_get_bits_copy(gx_device * dev, int x, int w, int h,
261                  gs_get_bits_params_t * params,
262                  const gs_get_bits_params_t *stored,
263                  const byte * src_base, uint dev_raster)
264 {
265     gs_get_bits_options_t options = params->options;
266     gs_get_bits_options_t stored_options = stored->options;
267     int x_offset = (options & GB_OFFSET_0 ? 0 : params->x_offset);
268     int depth = dev->color_info.depth;
269     int bit_x = x * depth;
270     const byte *src = src_base;
271     /*
272      * If the stored representation matches a requested representation,
273      * we can copy the data without any transformations.
274      */
275     bool direct_copy = requested_includes_stored(dev, params, stored);
276     int code = 0;
277 
278     /*
279      * The request must include either GB_PACKING_CHUNKY or
280      * GB_PACKING_PLANAR + GB_SELECT_PLANES, GB_RETURN_COPY,
281      * and an offset and raster specification.  In the planar case,
282      * the request must include GB_ALIGN_STANDARD, the stored
283      * representation must include GB_PACKING_CHUNKY, and both must
284      * include GB_COLORS_NATIVE.
285      */
286     if ((~options & GB_RETURN_COPY) ||
287         !(options & (GB_OFFSET_0 | GB_OFFSET_SPECIFIED)) ||
288         !(options & (GB_RASTER_STANDARD | GB_RASTER_SPECIFIED))
289         )
290         return_error(gs_error_rangecheck);
291     if (options & GB_PACKING_CHUNKY) {
292         byte *data = params->data[0];
293         int end_bit = (x_offset + w) * depth;
294         uint std_raster =
295             (options & GB_ALIGN_STANDARD ? bitmap_raster(end_bit) :
296              (end_bit + 7) >> 3);
297         uint raster =
298             (options & GB_RASTER_STANDARD ? std_raster : params->raster);
299         int dest_bit_x = x_offset * depth;
300         int skew = bit_x - dest_bit_x;
301 
302         /*
303          * If the bit positions line up, use bytes_copy_rectangle.
304          * Since bytes_copy_rectangle doesn't require alignment,
305          * the bit positions only have to match within a byte,
306          * not within align_bitmap_mod bytes.
307          */
308         if (!(skew & 7) && direct_copy) {
309             int bit_w = w * depth;
310 
311             bytes_copy_rectangle(data + (dest_bit_x >> 3), raster,
312                                  src + (bit_x >> 3), dev_raster,
313                               ((bit_x + bit_w + 7) >> 3) - (bit_x >> 3), h);
314         } else if (direct_copy) {
315             /*
316              * Use the logic already in mem_mono_copy_mono to copy the
317              * bits to the destination.  We do this one line at a time,
318              * to avoid having to allocate a line pointer table.
319              */
320             gx_device_memory tdev;
321             byte *line_ptr = data;
322             int bit_w = w * depth;
323 
324             tdev.line_ptrs = &tdev.base;
325             for (; h > 0; line_ptr += raster, src += dev_raster, --h) {
326                 /* Make sure the destination is aligned. */
327                 int align = ALIGNMENT_MOD(line_ptr, align_bitmap_mod);
328 
329                 tdev.base = line_ptr - align;
330                 /* set up parameters required by copy_mono's fit_copy */
331                 tdev.width = dest_bit_x + (align << 3) + bit_w;
332                 tdev.height = 1;
333                 (*dev_proc(&mem_mono_device, copy_mono))
334                     ((gx_device *) & tdev, src, bit_x, dev_raster, gx_no_bitmap_id,
335                      dest_bit_x + (align << 3), 0, bit_w, 1,
336                      (gx_color_index) 0, (gx_color_index) 1);
337             }
338         } else if (options & ~stored_options & GB_COLORS_NATIVE) {
339             /* Convert standard colors to native. */
340             code = gx_get_bits_std_to_native(dev, x, w, h, params, stored,
341                                              src_base, dev_raster,
342                                              x_offset, raster);
343             options = params->options;
344         } else {
345             /* Convert native colors to standard. */
346             code = gx_get_bits_native_to_std(dev, x, w, h, params, stored,
347                                              src_base, dev_raster,
348                                              x_offset, raster, std_raster);
349             options = params->options;
350         }
351         params->options =
352             (options & (GB_COLORS_ALL | GB_ALPHA_ALL)) | GB_PACKING_CHUNKY |
353             (options & GB_COLORS_NATIVE ? 0 : options & GB_DEPTH_ALL) |
354             (options & GB_ALIGN_STANDARD ? GB_ALIGN_STANDARD : GB_ALIGN_ANY) |
355             GB_RETURN_COPY |
356             (x_offset == 0 ? GB_OFFSET_0 : GB_OFFSET_SPECIFIED) |
357             (raster == std_raster ? GB_RASTER_STANDARD : GB_RASTER_SPECIFIED);
358     } else if (!(~options &
359                  (GB_PACKING_PLANAR | GB_SELECT_PLANES | GB_ALIGN_STANDARD)) &&
360                (stored_options & GB_PACKING_CHUNKY) &&
361                ((options & stored_options) & GB_COLORS_NATIVE)
362                ) {
363         uchar num_planes = dev->color_info.num_components;
364         int dest_depth = depth / num_planes;
365         bits_plane_t source, dest;
366         int plane = -1;
367         uchar i;
368 
369         /* Make sure only one plane is being requested. */
370         for (i = 0; i < num_planes; ++i)
371             if (params->data[i] != 0) {
372                 if (plane >= 0)
373                     return_error(gs_error_rangecheck); /* > 1 plane */
374                 plane = i;
375             }
376         /* Ensure at least one plane is requested */
377         if (plane < 0)
378             return_error(gs_error_rangecheck); /* No planes */
379         source.data.read = src_base;
380         source.raster = dev_raster;
381         source.depth = depth;
382         source.x = x;
383         dest.data.write = params->data[plane];
384         dest.raster =
385             (options & GB_RASTER_STANDARD ?
386              bitmap_raster((x_offset + w) * dest_depth) : params->raster);
387         if (dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN)
388             dest.depth = dev->color_info.comp_bits[plane];
389         else
390             dest.depth = dest_depth;
391         dest.x = x_offset;
392         return bits_extract_plane(&dest, &source,
393                                   (num_planes - 1 - plane) * dest_depth,
394                                   w, h);
395     } else
396         return_error(gs_error_rangecheck);
397     return code;
398 }
399 
400 /*
401  * Convert standard colors to native.  Note that the source
402  * may have depths other than 8 bits per component.
403  */
404 static int
gx_get_bits_std_to_native(gx_device * dev,int x,int w,int h,gs_get_bits_params_t * params,const gs_get_bits_params_t * stored,const byte * src_base,uint dev_raster,int x_offset,uint raster)405 gx_get_bits_std_to_native(gx_device * dev, int x, int w, int h,
406                           gs_get_bits_params_t * params,
407                           const gs_get_bits_params_t *stored,
408                           const byte * src_base, uint dev_raster,
409                           int x_offset, uint raster)
410 {
411     int depth = dev->color_info.depth;
412     int dest_bit_offset = x_offset * depth;
413     byte *dest_line = params->data[0] + (dest_bit_offset >> 3);
414     int ncolors =
415         (stored->options & GB_COLORS_RGB ? 3 :
416          stored->options & GB_COLORS_CMYK ? 4 :
417          stored->options & GB_COLORS_GRAY ? 1 : -1);
418     int ncomp = ncolors +
419         ((stored->options & (GB_ALPHA_FIRST | GB_ALPHA_LAST)) != 0);
420     int src_depth = GB_OPTIONS_DEPTH(stored->options);
421     int src_bit_offset = x * src_depth * ncomp;
422     const byte *src_line = src_base + (src_bit_offset >> 3);
423     gx_color_value src_max = (1 << src_depth) - 1;
424 #define v2cv(value) ((ulong)(value) * gx_max_color_value / src_max)
425     gx_color_value alpha_default = src_max;
426 
427     params->options &= ~GB_COLORS_ALL | GB_COLORS_NATIVE;
428     for (; h > 0; dest_line += raster, src_line += dev_raster, --h) {
429         int i;
430         const byte *src = src_line;
431         int sbit = src_bit_offset & 7;
432         byte *dest = dest_line;
433         int dbit = dest_bit_offset & 7;
434         byte dbyte = (dbit ? (byte)(*dest & (0xff00 >> dbit)) : 0);
435 
436 #define v2frac(value) ((long)(value) * frac_1 / src_max)
437 
438         for (i = 0; i < w; ++i) {
439             int j;
440             uchar k;
441             frac sc[4], dc[GX_DEVICE_COLOR_MAX_COMPONENTS];
442             gx_color_value v[GX_DEVICE_COLOR_MAX_COMPONENTS];
443             gx_color_value va = alpha_default;
444             gx_color_index pixel;
445             bool do_alpha = false;
446             subclass_color_mappings scm;
447 
448             scm = get_color_mapping_procs_subclass(dev);
449 
450             /* Fetch the source data. */
451             if (stored->options & GB_ALPHA_FIRST) {
452                 if (sample_load_next16(&va, &src, &sbit, src_depth) < 0)
453                     return_error(gs_error_rangecheck);
454                 va = v2cv(va);
455                 do_alpha = true;
456             }
457             for (j = 0; j < ncolors; ++j) {
458                 gx_color_value vj;
459 
460                 if (sample_load_next16(&vj, &src, &sbit, src_depth) < 0)
461                     return_error(gs_error_rangecheck);
462                 sc[j] = v2frac(vj);
463             }
464             if (stored->options & GB_ALPHA_LAST) {
465                 if (sample_load_next16(&va, &src, &sbit, src_depth) < 0)
466                     return_error(gs_error_rangecheck);
467                 va = v2cv(va);
468                 do_alpha = true;
469             }
470 
471             /* Convert and store the pixel value. */
472             if (do_alpha) {
473                 for (j = 0; j < ncolors; j++)
474                     v[j] = frac2cv(sc[j]);
475                 if (ncolors == 1)
476                     v[2] = v[1] = v[0];
477                 pixel = dev_proc(dev, map_rgb_alpha_color)
478                     (dev, v[0], v[1], v[2], va);
479             } else {
480 
481                 switch (ncolors) {
482                 case 1:
483                     map_gray_subclass(scm, sc[0], dc);
484                     break;
485                 case 3:
486                     map_rgb_subclass(scm, 0, sc[0], sc[1], sc[2], dc);
487                     break;
488                 case 4:
489                     map_cmyk_subclass(scm, sc[0], sc[1], sc[2], sc[3], dc);
490                     break;
491                 default:
492                     return_error(gs_error_rangecheck);
493                 }
494 
495                 for (k = 0; k < dev->color_info.num_components; k++)
496                     v[k] = frac2cv(dc[k]);
497 
498                 pixel = dev_proc(dev, encode_color)(dev, v);
499             }
500             if (sizeof(pixel) > 4) {
501                 if (sample_store_next64(pixel, &dest, &dbit, depth, &dbyte) < 0)
502                     return_error(gs_error_rangecheck);
503             }
504             else {
505                 if (sample_store_next32(pixel, &dest, &dbit, depth, &dbyte) < 0)
506                     return_error(gs_error_rangecheck);
507             }
508         }
509         sample_store_flush(dest, dbit, dbyte);
510     }
511     return 0;
512 }
513 
514 /*
515  * Convert native colors to standard.  Only GB_DEPTH_8 is supported.
516  */
517 static int
gx_get_bits_native_to_std(gx_device * dev,int x,int w,int h,gs_get_bits_params_t * params,const gs_get_bits_params_t * stored,const byte * src_base,uint dev_raster,int x_offset,uint raster,uint std_raster)518 gx_get_bits_native_to_std(gx_device * dev, int x, int w, int h,
519                           gs_get_bits_params_t * params,
520                           const gs_get_bits_params_t *stored,
521                           const byte * src_base, uint dev_raster,
522                           int x_offset, uint raster, uint std_raster)
523 {
524     int depth = dev->color_info.depth;
525     int src_bit_offset = x * depth;
526     const byte *src_line = src_base + (src_bit_offset >> 3);
527     gs_get_bits_options_t options = params->options;
528     int ncomp =
529         (options & (GB_ALPHA_FIRST | GB_ALPHA_LAST) ? 4 : 3);
530     byte *dest_line = params->data[0] + x_offset * ncomp;
531     byte *mapped[16];
532     int dest_bytes;
533     int i;
534 
535     if (!(options & GB_DEPTH_8)) {
536         /*
537          * We don't support general depths yet, or conversion between
538          * different formats.  Punt.
539          */
540         return_error(gs_error_rangecheck);
541     }
542 
543     /* Pick the representation that's most likely to be useful. */
544     if (options & GB_COLORS_RGB)
545         params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_RGB,
546             dest_bytes = 3;
547     else if (options & GB_COLORS_CMYK)
548         params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_CMYK,
549             dest_bytes = 4;
550     else if (options & GB_COLORS_GRAY)
551         params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_GRAY,
552             dest_bytes = 1;
553     else
554         return_error(gs_error_rangecheck);
555     /* Recompute the destination raster based on the color space. */
556     if (options & GB_RASTER_STANDARD) {
557         uint end_byte = (x_offset + w) * dest_bytes;
558 
559         raster = std_raster =
560             (options & GB_ALIGN_STANDARD ?
561              bitmap_raster(end_byte << 3) : end_byte);
562     }
563     /* Check for the one special case we care about, namely that we have a
564      * device that uses cmyk_1bit_map_cmyk_color, or equivalent. We do not
565      * check function pointers directly, as this is defeated by forwarding
566      * devices, but rather use a dev_spec_op. */
567     if (((options & (GB_COLORS_RGB | GB_ALPHA_FIRST | GB_ALPHA_LAST))
568            == GB_COLORS_RGB) &&
569         (dev_proc(dev, dev_spec_op)(dev, gxdso_is_std_cmyk_1bit, NULL, 0) > 0)) {
570         gx_get_bits_copy_cmyk_1bit(dest_line, raster,
571                                    src_line, dev_raster,
572                                    src_bit_offset & 7, w, h);
573         return 0;
574     }
575     if (options & (GB_ALPHA_FIRST | GB_ALPHA_LAST))
576         ++dest_bytes;
577     /* Clear the color translation cache. */
578     for (i = (depth > 4 ? 16 : 1 << depth); --i >= 0; )
579         mapped[i] = 0;
580     for (; h > 0; dest_line += raster, src_line += dev_raster, --h) {
581         const byte *src = src_line;
582         int bit = src_bit_offset & 7;
583         byte *dest = dest_line;
584 
585         for (i = 0; i < w; ++i) {
586             gx_color_index pixel = 0;
587             gx_color_value rgba[4];
588 
589             if (sizeof(pixel) > 4) {
590                 if (sample_load_next64((uint64_t *)&pixel, &src, &bit, depth) < 0)
591                     return_error(gs_error_rangecheck);
592             }
593             else {
594                 if (sample_load_next32((uint32_t *)&pixel, &src, &bit, depth) < 0)
595                     return_error(gs_error_rangecheck);
596             }
597             if (pixel < 16) {
598                 if (mapped[pixel]) {
599                     /* Use the value from the cache. */
600                     memcpy(dest, mapped[pixel], dest_bytes);
601                     dest += dest_bytes;
602                     continue;
603                 }
604                 mapped[pixel] = dest;
605             }
606             (*dev_proc(dev, map_color_rgb_alpha)) (dev, pixel, rgba);
607             if (options & GB_ALPHA_FIRST)
608                 *dest++ = gx_color_value_to_byte(rgba[3]);
609             /* Convert to the requested color space. */
610             if (options & GB_COLORS_RGB) {
611                 dest[0] = gx_color_value_to_byte(rgba[0]);
612                 dest[1] = gx_color_value_to_byte(rgba[1]);
613                 dest[2] = gx_color_value_to_byte(rgba[2]);
614                 dest += 3;
615             } else if (options & GB_COLORS_CMYK) {
616                 /* Use the standard RGB to CMYK algorithm, */
617                 /* with maximum black generation and undercolor removal. */
618                 gx_color_value white = max(rgba[0], max(rgba[1], rgba[2]));
619 
620                 dest[0] = gx_color_value_to_byte(white - rgba[0]);
621                 dest[1] = gx_color_value_to_byte(white - rgba[1]);
622                 dest[2] = gx_color_value_to_byte(white - rgba[2]);
623                 dest[3] = gx_color_value_to_byte(gx_max_color_value - white);
624                 dest += 4;
625             } else {	/* GB_COLORS_GRAY */
626                 /* Use the standard RGB to Gray algorithm. */
627                 *dest++ = gx_color_value_to_byte(
628                                 ((rgba[0] * (ulong) lum_red_weight) +
629                                  (rgba[1] * (ulong) lum_green_weight) +
630                                  (rgba[2] * (ulong) lum_blue_weight) +
631                                    (lum_all_weights / 2))
632                                 / lum_all_weights);
633             }
634             if (options & GB_ALPHA_LAST)
635                 *dest++ = gx_color_value_to_byte(rgba[3]);
636         }
637     }
638     return 0;
639 }
640 
641 /* ------ Default implementations of get_bits_rectangle ------ */
642 
643 int
gx_no_get_bits_rectangle(gx_device * dev,const gs_int_rect * prect,gs_get_bits_params_t * params,gs_int_rect ** unread)644 gx_no_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
645                        gs_get_bits_params_t * params, gs_int_rect ** unread)
646 {
647     return_error(gs_error_unknownerror);
648 }
649 
650 int
gx_default_get_bits_rectangle(gx_device * dev,const gs_int_rect * prect,gs_get_bits_params_t * params,gs_int_rect ** unread)651 gx_default_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
652                        gs_get_bits_params_t * params, gs_int_rect ** unread)
653 {
654     dev_proc_get_bits_rectangle((*save_get_bits_rectangle)) =
655         dev_proc(dev, get_bits_rectangle);
656     int depth = dev->color_info.depth;
657     uint min_raster = (dev->width * depth + 7) >> 3;
658     gs_get_bits_options_t options = params->options;
659     int code;
660 
661     /* Avoid a recursion loop. */
662     set_dev_proc(dev, get_bits_rectangle, gx_no_get_bits_rectangle);
663     /*
664      * If the parameters are right, try to call get_bits directly.  Note
665      * that this may fail if a device only implements get_bits_rectangle
666      * (not get_bits) for a limited set of options.  Note also that this
667      * must handle the case of the recursive call from within
668      * get_bits_rectangle (see below): because of this, and only because
669      * of this, it must handle partial scan lines.
670      */
671     if (prect->q.y == prect->p.y + 1 &&
672         !(~options &
673           (GB_RETURN_COPY | GB_PACKING_CHUNKY | GB_COLORS_NATIVE)) &&
674         (options & (GB_ALIGN_STANDARD | GB_ALIGN_ANY)) &&
675         ((options & (GB_OFFSET_0 | GB_OFFSET_ANY)) ||
676          ((options & GB_OFFSET_SPECIFIED) && params->x_offset == 0)) &&
677         ((options & (GB_RASTER_STANDARD | GB_RASTER_ANY)) ||
678          ((options & GB_RASTER_SPECIFIED) &&
679           params->raster >= min_raster)) &&
680         unread == NULL
681         ) {
682         byte *data = params->data[0];
683         byte *row = data;
684 
685         if (!(prect->p.x == 0 && prect->q.x == dev->width)) {
686             /* Allocate an intermediate row buffer. */
687             row = gs_alloc_bytes(dev->memory, min_raster,
688                                  "gx_default_get_bits_rectangle");
689 
690             if (row == 0) {
691                 code = gs_note_error(gs_error_VMerror);
692                 goto ret;
693             }
694         }
695         code = (*dev_proc(dev, get_bits)) (dev, prect->p.y, row,
696                 (params->options & GB_RETURN_POINTER) ? &params->data[0]
697                                                       : NULL );
698         if (code >= 0) {
699             if (row != data) {
700                 if (prect->p.x == 0 && params->data[0] != row
701                     && params->options & GB_RETURN_POINTER) {
702                     /*
703                      * get_bits returned an appropriate pointer: we can
704                      * avoid doing any copying.
705                      */
706                     DO_NOTHING;
707                 } else {
708                     /* Copy the partial row into the supplied buffer. */
709                     int width_bits = (prect->q.x - prect->p.x) * depth;
710                     gx_device_memory tdev;
711 
712                     tdev.width = width_bits;
713                     tdev.height = 1;
714                     tdev.line_ptrs = &tdev.base;
715                     tdev.base = data;
716                     tdev.raster = bitmap_raster(width_bits);
717                     code = (*dev_proc(&mem_mono_device, copy_mono))
718                         ((gx_device *) & tdev,
719                          (params->options & GB_RETURN_POINTER) ? params->data[0] : row,
720                          prect->p.x * depth,
721                          min_raster, gx_no_bitmap_id, 0, 0, width_bits, 1,
722                          (gx_color_index) 0, (gx_color_index) 1);
723                     params->data[0] = data;
724                 }
725                 gs_free_object(dev->memory, row,
726                                "gx_default_get_bits_rectangle");
727             }
728             params->options =
729                 GB_ALIGN_STANDARD | GB_OFFSET_0 | GB_PACKING_CHUNKY |
730                 GB_ALPHA_NONE | GB_COLORS_NATIVE | GB_RASTER_STANDARD |
731                 (params->data[0] == data ? GB_RETURN_COPY : GB_RETURN_POINTER);
732             goto ret;
733         }
734     } {
735         /* Do the transfer row-by-row using a buffer. */
736         int x = prect->p.x, w = prect->q.x - x;
737         int bits_per_pixel = depth;
738         byte *row;
739 
740         if (options & GB_COLORS_STANDARD_ALL) {
741             /*
742              * Make sure the row buffer can hold the standard color
743              * representation, in case the device decides to use it.
744              */
745             int bpc = GB_OPTIONS_MAX_DEPTH(options);
746             int nc =
747             (options & GB_COLORS_CMYK ? 4 :
748              options & GB_COLORS_RGB ? 3 : 1) +
749             (options & (GB_ALPHA_ALL - GB_ALPHA_NONE) ? 1 : 0);
750             int bpp = bpc * nc;
751 
752             if (bpp > bits_per_pixel)
753                 bits_per_pixel = bpp;
754         }
755         row = gs_alloc_bytes(dev->memory, (bits_per_pixel * w + 7) >> 3,
756                              "gx_default_get_bits_rectangle");
757         if (row == 0) {
758             code = gs_note_error(gs_error_VMerror);
759         } else {
760             uint dev_raster = gx_device_raster(dev, true);
761             uint raster =
762             (options & GB_RASTER_SPECIFIED ? params->raster :
763              options & GB_ALIGN_STANDARD ? bitmap_raster(depth * w) :
764              (depth * w + 7) >> 3);
765             gs_int_rect rect;
766             gs_get_bits_params_t copy_params;
767             gs_get_bits_options_t copy_options =
768                 (GB_ALIGN_STANDARD | GB_ALIGN_ANY) |
769                 (GB_RETURN_COPY | GB_RETURN_POINTER) |
770                 (GB_OFFSET_0 | GB_OFFSET_ANY) |
771                 (GB_RASTER_STANDARD | GB_RASTER_ANY) | GB_PACKING_CHUNKY |
772                 GB_COLORS_NATIVE | (options & (GB_DEPTH_ALL | GB_COLORS_ALL)) |
773                 GB_ALPHA_ALL;
774             byte *dest = params->data[0];
775             int y;
776 
777             rect.p.x = x, rect.q.x = x + w;
778             code = 0;
779             for (y = prect->p.y; y < prect->q.y; ++y) {
780                 rect.p.y = y, rect.q.y = y + 1;
781                 copy_params.options = copy_options;
782                 copy_params.data[0] = row;
783                 code = (*save_get_bits_rectangle)
784                     (dev, &rect, &copy_params, NULL);
785                 if (code < 0)
786                     break;
787                 if (copy_params.options & GB_OFFSET_0)
788                     copy_params.x_offset = 0;
789                 params->data[0] = dest + (y - prect->p.y) * raster;
790                 code = gx_get_bits_copy(dev, copy_params.x_offset, w, 1,
791                                         params, &copy_params,
792                                         copy_params.data[0], dev_raster);
793                 if (code < 0)
794                     break;
795             }
796             gs_free_object(dev->memory, row, "gx_default_get_bits_rectangle");
797             params->data[0] = dest;
798         }
799     }
800   ret:set_dev_proc(dev, get_bits_rectangle, save_get_bits_rectangle);
801     return (code < 0 ? code : 0);
802 }
803