1 /* Copyright (C) 2001-2006 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, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 /* $Id: gdevmpla.c 8250 2007-09-25 13:31:24Z giles $ */
14 /* Any-depth planar "memory" (stored bitmap) device */
15 #include "memory_.h"
16 #include "gx.h"
17 #include "gserrors.h"
18 #include "gsbitops.h"
19 #include "gxdevice.h"
20 #include "gxdevmem.h"		/* semi-public definitions */
21 #include "gxgetbit.h"
22 #include "gdevmem.h"		/* private definitions */
23 #include "gdevmpla.h"		/* interface */
24 
25 /* procedures */
26 static dev_proc_open_device(mem_planar_open);
27 declare_mem_procs(mem_planar_copy_mono, mem_planar_copy_color, mem_planar_fill_rectangle);
28 static dev_proc_strip_tile_rectangle(mem_planar_strip_tile_rectangle);
29 static dev_proc_get_bits_rectangle(mem_planar_get_bits_rectangle);
30 
31 /*
32  * Set up a planar memory device, after calling gs_make_mem_device but
33  * before opening the device.  The pre-existing device provides the color
34  * mapping procedures, but not the drawing procedures.  Requires: num_planes
35  * > 0, plane_depths[0 ..  num_planes - 1] > 0, sum of plane depths =
36  * mdev->color_info.depth.
37  *
38  * Note that this is the only public procedure in this file, and the only
39  * sanctioned way to set up a planar memory device.
40  */
41 int
gdev_mem_set_planar(gx_device_memory * mdev,int num_planes,const gx_render_plane_t * planes)42 gdev_mem_set_planar(gx_device_memory * mdev, int num_planes,
43 		    const gx_render_plane_t *planes /*[num_planes]*/)
44 {
45     int total_depth;
46     int same_depth = planes[0].depth;
47     gx_color_index covered = 0;
48     int pi;
49 
50     if (num_planes < 1 || num_planes > GX_DEVICE_COLOR_MAX_COMPONENTS)
51 	return_error(gs_error_rangecheck);
52     for (pi = 0, total_depth = 0; pi < num_planes; ++pi) {
53 	int shift = planes[pi].shift;
54 	int plane_depth = planes[pi].depth;
55 	gx_color_index mask;
56 
57 	if (shift < 0 || plane_depth > 16 ||
58 	    !gdev_mem_device_for_bits(plane_depth))
59 	    return_error(gs_error_rangecheck);
60 	mask = (((gx_color_index)1 << plane_depth) - 1) << shift;
61 	if (covered & mask)
62 	    return_error(gs_error_rangecheck);
63 	covered |= mask;
64 	if (plane_depth != same_depth)
65 	    same_depth = 0;
66 	total_depth += plane_depth;
67     }
68     if (total_depth > mdev->color_info.depth)
69 	return_error(gs_error_rangecheck);
70     mdev->num_planes = num_planes;
71     memcpy(mdev->planes, planes, num_planes * sizeof(planes[0]));
72     mdev->plane_depth = same_depth;
73     /* Change the drawing procedures. */
74     set_dev_proc(mdev, open_device, mem_planar_open);
75     set_dev_proc(mdev, fill_rectangle, mem_planar_fill_rectangle);
76     set_dev_proc(mdev, copy_mono, mem_planar_copy_mono);
77     set_dev_proc(mdev, copy_color, mem_planar_copy_color);
78     set_dev_proc(mdev, copy_alpha, gx_default_copy_alpha);
79     set_dev_proc(mdev, strip_tile_rectangle, mem_planar_strip_tile_rectangle);
80     set_dev_proc(mdev, strip_copy_rop, gx_default_strip_copy_rop);
81     set_dev_proc(mdev, get_bits_rectangle, mem_planar_get_bits_rectangle);
82     return 0;
83 }
84 
85 /* Open a planar memory device. */
86 static int
mem_planar_open(gx_device * dev)87 mem_planar_open(gx_device * dev)
88 {
89     gx_device_memory *const mdev = (gx_device_memory *)dev;
90 
91     /* Check that we aren't trying to open a chunky device as planar. */
92     if (mdev->num_planes == 0)
93 	return_error(gs_error_rangecheck);
94     return gdev_mem_open_scan_lines(mdev, dev->height);
95 }
96 
97 /*
98  * We execute drawing operations by patching a few parameters in the
99  * device structure and then calling the procedure appropriate to the
100  * plane depth.
101  */
102 typedef struct mem_save_params_s {
103     int depth;			/* color_info.depth */
104     byte *base;
105     byte **line_ptrs;
106 } mem_save_params_t;
107 #define MEM_SAVE_PARAMS(mdev, msp)\
108   (msp.depth = mdev->color_info.depth,\
109    msp.base = mdev->base,\
110    msp.line_ptrs = mdev->line_ptrs)
111 #define MEM_SET_PARAMS(mdev, plane_depth)\
112   (mdev->color_info.depth = plane_depth, /* maybe not needed */\
113    mdev->base = mdev->line_ptrs[0],\
114    mdev->raster = bitmap_raster(mdev->width * plane_depth))
115 #define MEM_RESTORE_PARAMS(mdev, msp)\
116   (mdev->color_info.depth = msp.depth,\
117    mdev->base = msp.base,\
118    mdev->line_ptrs = msp.line_ptrs)
119 
120 /* Fill a rectangle with a color. */
121 static int
mem_planar_fill_rectangle(gx_device * dev,int x,int y,int w,int h,gx_color_index color)122 mem_planar_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
123 			  gx_color_index color)
124 {
125     gx_device_memory * const mdev = (gx_device_memory *)dev;
126     mem_save_params_t save;
127     int pi;
128 
129     MEM_SAVE_PARAMS(mdev, save);
130     for (pi = 0; pi < mdev->num_planes; ++pi) {
131 	int plane_depth = mdev->planes[pi].depth;
132 	gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
133 	const gx_device_memory *mdproto =
134 	    gdev_mem_device_for_bits(plane_depth);
135 
136 	MEM_SET_PARAMS(mdev, plane_depth);
137 	dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h,
138 					  (color >> mdev->planes[pi].shift) &
139 					  mask);
140 	mdev->line_ptrs += mdev->height;
141     }
142     MEM_RESTORE_PARAMS(mdev, save);
143     return 0;
144 }
145 
146 /* Copy a bitmap. */
147 static int
mem_planar_copy_mono(gx_device * dev,const byte * base,int sourcex,int sraster,gx_bitmap_id id,int x,int y,int w,int h,gx_color_index color0,gx_color_index color1)148 mem_planar_copy_mono(gx_device * dev, const byte * base, int sourcex,
149 		     int sraster, gx_bitmap_id id, int x, int y, int w, int h,
150 		     gx_color_index color0, gx_color_index color1)
151 {
152     gx_device_memory * const mdev = (gx_device_memory *)dev;
153     mem_save_params_t save;
154     int pi;
155 
156     MEM_SAVE_PARAMS(mdev, save);
157     for (pi = 0; pi < mdev->num_planes; ++pi) {
158 	int plane_depth = mdev->planes[pi].depth;
159 	int shift = mdev->planes[pi].shift;
160 	gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
161 	const gx_device_memory *mdproto =
162 	    gdev_mem_device_for_bits(plane_depth);
163 	gx_color_index c0 =
164 	    (color0 == gx_no_color_index ? gx_no_color_index :
165 	     (color0 >> shift) & mask);
166 	gx_color_index c1 =
167 	    (color1 == gx_no_color_index ? gx_no_color_index :
168 	     (color1 >> shift) & mask);
169 
170 	MEM_SET_PARAMS(mdev, plane_depth);
171 	if (c0 == c1)
172 	    dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h, c0);
173 	else
174 	    dev_proc(mdproto, copy_mono)
175 		(dev, base, sourcex, sraster, id, x, y, w, h, c0, c1);
176 	mdev->line_ptrs += mdev->height;
177     }
178     MEM_RESTORE_PARAMS(mdev, save);
179     return 0;
180 }
181 
182 /* Copy a color bitmap. */
183 /* This is slow and messy. */
184 static int
mem_planar_copy_color(gx_device * dev,const byte * base,int sourcex,int sraster,gx_bitmap_id id,int x,int y,int w,int h)185 mem_planar_copy_color(gx_device * dev, const byte * base, int sourcex,
186 		      int sraster, gx_bitmap_id id,
187 		      int x, int y, int w, int h)
188 {
189     gx_device_memory * const mdev = (gx_device_memory *)dev;
190 #define BUF_LONGS 100	/* arbitrary, >= 1 */
191 #define BUF_BYTES (BUF_LONGS * ARCH_SIZEOF_LONG)
192     union b_ {
193 	ulong l[BUF_LONGS];
194 	byte b[BUF_BYTES];
195     } buf;
196     int source_depth = dev->color_info.depth;
197     mem_save_params_t save;
198     int pi;
199 
200     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
201     MEM_SAVE_PARAMS(mdev, save);
202     for (pi = 0; pi < mdev->num_planes; ++pi) {
203 	int plane_depth = mdev->planes[pi].depth;
204 	int shift = mdev->planes[pi].shift;
205 	gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
206 	const gx_device_memory *mdproto =
207 	    gdev_mem_device_for_bits(plane_depth);
208 	/*
209 	 * Divide up the transfer into chunks that can be assembled
210 	 * within the fixed-size buffer.  This code can be simplified
211 	 * a lot if all planes have the same depth, by simply using
212 	 * copy_color to transfer one column at a time, but it might
213 	 * be very inefficient.
214 	 */
215 	uint plane_raster = bitmap_raster(plane_depth * w);
216 	int br, bw, bh, cx, cy, cw, ch, ix, iy;
217 
218 	MEM_SET_PARAMS(mdev, plane_depth);
219 	if (plane_raster > BUF_BYTES) {
220 	    br = BUF_BYTES;
221 	    bw = BUF_BYTES * 8 / plane_depth;
222 	    bh = 1;
223 	} else {
224 	    br = plane_raster;
225 	    bw = w;
226 	    bh = BUF_BYTES / plane_raster;
227 	}
228 	/*
229 	 * We could do the extraction with get_bits_rectangle
230 	 * selecting a single plane, but this is critical enough
231 	 * code that we more or less replicate it here.
232 	 */
233 	for (cy = y; cy < y + h; cy += ch) {
234 	    ch = min(bh, y + h - cy);
235 	    for (cx = x; cx < x + w; cx += cw) {
236 		int sx = sourcex + cx - x;
237 		const byte *source_base = base + sraster * (cy - y);
238 		int source_bit = 0;
239 
240 		cw = min(bw, x + w - cx);
241 		if (sx) {
242 		    int xbit = sx * source_depth;
243 
244 		    source_base += xbit >> 3;
245 		    source_bit = xbit & 7;
246 		}
247 		for (iy = 0; iy < ch; ++iy) {
248 		    sample_load_declare_setup(sptr, sbit, source_base,
249 					      source_bit, source_depth);
250 		    sample_store_declare_setup(dptr, dbit, dbbyte,
251 					       buf.b + br * iy,
252 					       0, plane_depth);
253 
254 		    for (ix = 0; ix < cw; ++ix) {
255 			gx_color_index value;
256 
257 			sample_load_next_any(value, sptr, sbit, source_depth);
258 			value = (value >> shift) & mask;
259 			sample_store_next16(value, dptr, dbit, plane_depth,
260 					    dbbyte);
261 		    }
262 		    sample_store_flush(dptr, dbit, plane_depth, dbbyte);
263 		    source_base += sraster;
264 		}
265 		/*
266 		 * Detect and bypass the possibility that copy_color is
267 		 * defined in terms of copy_mono.
268 		 */
269 		if (plane_depth == 1)
270 		    dev_proc(mdproto, copy_mono)
271 			(dev, buf.b, 0, br, gx_no_bitmap_id, cx, cy, cw, ch,
272 			 (gx_color_index)0, (gx_color_index)1);
273 		else
274 		    dev_proc(mdproto, copy_color)
275 			(dev, buf.b, 0, br, gx_no_bitmap_id, cx, cy, cw, ch);
276 	    }
277 	}
278 	mdev->line_ptrs += mdev->height;
279     }
280     MEM_RESTORE_PARAMS(mdev, save);
281     return 0;
282 #undef BUF_BYTES
283 #undef BUF_LONGS
284 }
285 
286 static int
mem_planar_strip_tile_rectangle(gx_device * dev,const gx_strip_bitmap * tiles,int x,int y,int w,int h,gx_color_index color0,gx_color_index color1,int px,int py)287 mem_planar_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
288 				int x, int y, int w, int h,
289 				gx_color_index color0, gx_color_index color1,
290 				int px, int py)
291 {
292     gx_device_memory * const mdev = (gx_device_memory *)dev;
293     mem_save_params_t save;
294     int pi;
295 
296     /* We can't split up the transfer if the tile is colored. */
297     if (color0 == gx_no_color_index && color1 == gx_no_color_index)
298 	return gx_default_strip_tile_rectangle
299 	    (dev, tiles, x, y, w, h, color0, color1, px, py);
300     MEM_SAVE_PARAMS(mdev, save);
301     for (pi = 0; pi < mdev->num_planes; ++pi) {
302 	int plane_depth = mdev->planes[pi].depth;
303 	int shift = mdev->planes[pi].shift;
304 	gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
305 	const gx_device_memory *mdproto =
306 	    gdev_mem_device_for_bits(plane_depth);
307 	gx_color_index c0 =
308 	    (color0 == gx_no_color_index ? gx_no_color_index :
309 	     (color0 >> shift) & mask);
310 	gx_color_index c1 =
311 	    (color1 == gx_no_color_index ? gx_no_color_index :
312 	     (color1 >> shift) & mask);
313 
314 	MEM_SET_PARAMS(mdev, plane_depth);
315 	if (c0 == c1)
316 	    dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h, c0);
317 	else {
318 	    /*
319 	     * Temporarily replace copy_mono in case strip_tile_rectangle is
320 	     * defined in terms of it.
321 	     */
322 	    set_dev_proc(dev, copy_mono, dev_proc(mdproto, copy_mono));
323 	    dev_proc(mdproto, strip_tile_rectangle)
324 		(dev, tiles, x, y, w, h, c0, c1, px, py);
325 	}
326 	mdev->line_ptrs += mdev->height;
327     }
328     MEM_RESTORE_PARAMS(mdev, save);
329     set_dev_proc(dev, copy_mono, mem_planar_copy_mono);
330     return 0;
331 }
332 
333 /*
334  * Repack planar into chunky format.  This is an internal procedure that
335  * implements the straightforward chunky case of get_bits_rectangle, and
336  * is also used for the general cases.
337  */
338 static int
planar_to_chunky(gx_device_memory * mdev,int x,int y,int w,int h,int offset,uint draster,byte * dest)339 planar_to_chunky(gx_device_memory *mdev, int x, int y, int w, int h,
340 		 int offset, uint draster, byte *dest)
341 {
342     int num_planes = mdev->num_planes;
343     sample_load_declare(sptr[GX_DEVICE_COLOR_MAX_COMPONENTS],
344 			sbit[GX_DEVICE_COLOR_MAX_COMPONENTS]);
345     sample_store_declare(dptr, dbit, dbbyte);
346     int ddepth = mdev->color_info.depth;
347     int direct =
348 	(mdev->color_info.depth != num_planes * mdev->plane_depth ? 0 :
349 	 mdev->planes[0].shift == 0 ? -mdev->plane_depth : mdev->plane_depth);
350     int pi, ix, iy;
351 
352     /* Check whether the planes are of equal size and sequential. */
353     /* If direct != 0, we already know they exactly fill the depth. */
354     if (direct < 0) {
355 	for (pi = 0; pi < num_planes; ++pi)
356 	    if (mdev->planes[pi].shift != pi * -direct) {
357 		direct = 0; break;
358 	    }
359     } else if (direct > 0) {
360 	for (pi = 0; pi < num_planes; ++pi)
361 	    if (mdev->planes[num_planes - 1 - pi].shift != pi * direct) {
362 		direct = 0; break;
363 	    }
364     }
365     for (iy = y; iy < y + h; ++iy) {
366 	byte **line_ptr = mdev->line_ptrs + iy;
367 
368 	for (pi = 0; pi < num_planes; ++pi, line_ptr += mdev->height) {
369 	    int plane_depth = mdev->planes[pi].depth;
370 	    int xbit = x * plane_depth;
371 
372 	    sptr[pi] = *line_ptr + (xbit >> 3);
373 	    sample_load_setup(sbit[pi], xbit & 7, plane_depth);
374 	}
375 	{
376 	    int xbit = offset * ddepth;
377 
378 	    dptr = dest + (iy - y) * draster + (xbit >> 3);
379 	    sample_store_setup(dbit, xbit & 7, ddepth);
380 	}
381 	if (direct == -8) {
382 	    /* 1 byte per component, lsb first. */
383 	    switch (num_planes) {
384 	    case 3: {
385 		const byte *p0 = sptr[2];
386 		const byte *p1 = sptr[1];
387 		const byte *p2 = sptr[0];
388 
389 		for (ix = w; ix > 0; --ix, dptr += 3) {
390 		    dptr[0] = *p0++;
391 		    dptr[1] = *p1++;
392 		    dptr[2] = *p2++;
393 		}
394 	    }
395 	    continue;
396 	    case 4:
397 		for (ix = w; ix > 0; --ix, dptr += 4) {
398 		    dptr[0] = *sptr[3]++;
399 		    dptr[1] = *sptr[2]++;
400 		    dptr[2] = *sptr[1]++;
401 		    dptr[3] = *sptr[0]++;
402 		}
403 		continue;
404 	    default:
405 		break;
406 	    }
407 	}
408 	sample_store_preload(dbbyte, dptr, dbit, ddepth);
409 	for (ix = w; ix > 0; --ix) {
410 	    gx_color_index color = 0;
411 
412 	    for (pi = 0; pi < num_planes; ++pi) {
413 		int plane_depth = mdev->planes[pi].depth;
414 		uint value;
415 
416 		sample_load_next16(value, sptr[pi], sbit[pi], plane_depth);
417 		color |= (gx_color_index)value << mdev->planes[pi].shift;
418 	    }
419 	    sample_store_next_any(color, dptr, dbit, ddepth, dbbyte);
420 	}
421 	sample_store_flush(dptr, dbit, ddepth, dbbyte);
422     }
423     return 0;
424 }
425 
426 /* Copy bits back from a planar memory device. */
427 static int
mem_planar_get_bits_rectangle(gx_device * dev,const gs_int_rect * prect,gs_get_bits_params_t * params,gs_int_rect ** unread)428 mem_planar_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
429 			      gs_get_bits_params_t * params,
430 			      gs_int_rect ** unread)
431 {
432     /* This duplicates most of mem_get_bits_rectangle.  Tant pis. */
433     gx_device_memory * const mdev = (gx_device_memory *)dev;
434     gs_get_bits_options_t options = params->options;
435     int x = prect->p.x, w = prect->q.x - x, y = prect->p.y, h = prect->q.y - y;
436     int num_planes = mdev->num_planes;
437     gs_get_bits_params_t copy_params;
438     int code;
439 
440     if (options == 0) {
441 	/*
442 	 * Unfortunately, as things stand, we have to support
443 	 * GB_PACKING_CHUNKY.  In fact, we can't even claim to support
444 	 * GB_PACKING_PLANAR, because there is currently no way to
445 	 * describe the particular planar packing format that the device
446 	 * actually stores.
447 	 */
448 	params->options =
449 	    (GB_ALIGN_STANDARD | GB_ALIGN_ANY) |
450 	    (GB_RETURN_COPY | GB_RETURN_POINTER) |
451 	    (GB_OFFSET_0 | GB_OFFSET_SPECIFIED | GB_OFFSET_ANY) |
452 	    (GB_RASTER_STANDARD | GB_RASTER_SPECIFIED | GB_RASTER_ANY) |
453 	    /*
454 	    (mdev->num_planes == mdev->color_info.depth ?
455 	     GB_PACKING_CHUNKY | GB_PACKING_PLANAR | GB_PACKING_BIT_PLANAR :
456 	     GB_PACKING_CHUNKY | GB_PACKING_PLANAR)
457 	    */
458 	    GB_PACKING_CHUNKY |
459 	    GB_COLORS_NATIVE | GB_ALPHA_NONE;
460 	return_error(gs_error_rangecheck);
461     }
462     if ((w <= 0) | (h <= 0)) {
463 	if ((w | h) < 0)
464 	    return_error(gs_error_rangecheck);
465 	return 0;
466     }
467     if (x < 0 || w > dev->width - x ||
468 	y < 0 || h > dev->height - y
469 	)
470 	return_error(gs_error_rangecheck);
471 
472     /*
473      * If the request is for exactly one plane, hand it off to a device
474      * temporarily tweaked to return just that plane.
475      */
476     if (!(~options & (GB_PACKING_PLANAR | GB_SELECT_PLANES))) {
477 	/* Check that only a single plane is being requested. */
478 	int pi;
479 
480 	for (pi = 0; pi < num_planes; ++pi)
481 	    if (params->data[pi] != 0)
482 		break;
483 	if (pi < num_planes) {
484 	    int plane = pi++;
485 
486 	    for (; pi < num_planes; ++pi)
487 		if (params->data[pi] != 0)
488 		    break;
489 	    if (pi == num_planes) {
490 		mem_save_params_t save;
491 
492 		copy_params = *params;
493 		copy_params.options =
494 		    (options & ~(GB_PACKING_ALL | GB_SELECT_PLANES)) |
495 		    GB_PACKING_CHUNKY;
496 		copy_params.data[0] = copy_params.data[plane];
497 		MEM_SAVE_PARAMS(mdev, save);
498 		mdev->line_ptrs += mdev->height * plane;
499 		MEM_SET_PARAMS(mdev, mdev->planes[plane].depth);
500 		code = mem_get_bits_rectangle(dev, prect, &copy_params,
501 					      unread);
502 		MEM_RESTORE_PARAMS(mdev, save);
503 		if (code >= 0) {
504 		    params->data[plane] = copy_params.data[0];
505 		    return code;
506 		}
507 	    }
508 	}
509     }
510     /*
511      * We can't return the requested plane by itself.  Fall back to
512      * chunky format.  This is somewhat painful.
513      *
514      * The code here knows how to produce just one chunky format:
515      * GB_COLORS_NATIVE, GB_ALPHA_NONE, GB_RETURN_COPY.
516      * For any other format, we generate this one in a buffer and
517      * hand it off to gx_get_bits_copy.  This is *really* painful.
518      */
519     if (!(~options & (GB_COLORS_NATIVE | GB_ALPHA_NONE |
520 		      GB_PACKING_CHUNKY | GB_RETURN_COPY))) {
521 	int offset = (options & GB_OFFSET_SPECIFIED ? params->x_offset : 0);
522 	uint draster =
523 	    (options & GB_RASTER_SPECIFIED ? params->raster :
524 	     bitmap_raster((offset + w) * mdev->color_info.depth));
525 
526 	planar_to_chunky(mdev, x, y, w, h, offset, draster, params->data[0]);
527     } else {
528 	/*
529 	 * Do the transfer through an intermediate buffer.
530 	 * The buffer must be large enough to hold at least one pixel,
531 	 * i.e., GX_DEVICE_COLOR_MAX_COMPONENTS 16-bit values.
532 	 * The algorithms are very similar to those in copy_color.
533 	 */
534 #define BUF_LONGS\
535   max(100, (GX_DEVICE_COLOR_MAX_COMPONENTS * 2 + sizeof(long) - 1) /\
536       sizeof(long))
537 #define BUF_BYTES (BUF_LONGS * ARCH_SIZEOF_LONG)
538 	union b_ {
539 	    ulong l[BUF_LONGS];
540 	    byte b[BUF_BYTES];
541 	} buf;
542 	int br, bw, bh, cx, cy, cw, ch;
543 	int ddepth = mdev->color_info.depth;
544 	uint raster = bitmap_raster(ddepth * mdev->width);
545 	gs_get_bits_params_t dest_params;
546 
547 	if (raster > BUF_BYTES) {
548 	    br = BUF_BYTES;
549 	    bw = BUF_BYTES * 8 / ddepth;
550 	    bh = 1;
551 	} else {
552 	    br = raster;
553 	    bw = w;
554 	    bh = BUF_BYTES / raster;
555 	}
556 	copy_params.options =
557 	    GB_COLORS_NATIVE | GB_PACKING_CHUNKY | GB_ALPHA_NONE |
558 	    GB_RASTER_STANDARD;
559 	copy_params.raster = raster;
560 	dest_params = *params;
561 	for (cy = y; cy < y + h; cy += ch) {
562 	    ch = min(bh, y + h - cy);
563 	    for (cx = x; cx < x + w; cx += cw) {
564 		cw = min(bw, x + w - cx);
565 		planar_to_chunky(mdev, cx, cy, cw, ch, 0, br, buf.b);
566 		dest_params.x_offset = params->x_offset + cx - x;
567 		code = gx_get_bits_copy(dev, 0, cw, ch, &dest_params,
568 					&copy_params, buf.b, br);
569 		if (code < 0)
570 		    return code;
571 	    }
572 	    dest_params.data[0] += ch * raster;
573 	}
574 #undef BUF_BYTES
575 #undef BUF_LONGS
576     }
577     return 0;
578 }
579