summaryrefslogtreecommitdiff
path: root/gs/src/gxclrect.c
blob: ee4c172a493ebc80657147ef3194c7806c9852bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/* gxclrect.c */
/* Rectangle-oriented command writing for command list */
#include "gx.h"
#include "gserrors.h"
#include "gsutil.h"			/* for gs_next_ids */
#include "gxdevice.h"
#include "gxdevmem.h"			/* must precede gxcldev.h */
#include "gxcldev.h"

#define cdev cwdev

/* ---------------- Writing utilities ---------------- */

#define cmd_set_rect(rect)\
  ((rect).x = x, (rect).y = y,\
   (rect).width = width, (rect).height = height)

/* Write a rectangle. */
private int
cmd_size_rect(register const gx_cmd_rect *prect)
{	return
	  cmd_sizew(prect->x) + cmd_sizew(prect->y) +
	  cmd_sizew(prect->width) + cmd_sizew(prect->height);
}
private byte *
cmd_put_rect(register const gx_cmd_rect *prect, register byte *dp)
{	cmd_putw(prect->x, dp);
	cmd_putw(prect->y, dp);
	cmd_putw(prect->width, dp);
	cmd_putw(prect->height, dp);
	return dp;
}

int
cmd_write_rect_cmd(gx_device_clist_writer *cldev, gx_clist_state *pcls,
  int op, int x, int y, int width, int height)
{	int code;
	int dx = x - pcls->rect.x;
	int dy = y - pcls->rect.y;
	int dwidth = width - pcls->rect.width;
	int dheight = height - pcls->rect.height;
#define check_range_xy(rmin, rmax)\
  ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
   (unsigned)(dy - rmin) <= (rmax - rmin))
#define check_range_w(rmin, rmax)\
  ((unsigned)(dwidth - rmin) <= (rmax - rmin))
#define check_ranges(rmin, rmax)\
  (check_range_xy(rmin, rmax) && check_range_w(rmin, rmax) &&\
   (unsigned)(dheight - rmin) <= (rmax - rmin))
	cmd_set_rect(pcls->rect);
	if ( dheight == 0 && check_range_w(cmd_min_dw_tiny, cmd_max_dw_tiny) &&
	     check_range_xy(cmd_min_dxy_tiny, cmd_max_dxy_tiny)
	   )
	  {	byte op_tiny = op + 0x20 + dwidth - cmd_min_dw_tiny;
		byte *dp;

		if ( dx == width - dwidth && dy == 0 )
		  { code = set_cmd_put_op(dp, cldev, pcls, op_tiny + 8, 1);
		    if (code < 0)
		      return code;
		  }
		else
		  { code = set_cmd_put_op(dp, cldev, pcls, op_tiny, 2);
		    if (code < 0)
		      return code;
		    dp[1] = (dx << 4) + dy - (cmd_min_dxy_tiny * 0x11);
		  }
	  }
#define rmin cmd_min_short
#define rmax cmd_max_short
	else if ( check_ranges(rmin, rmax) )
	   {	int dh = dheight - cmd_min_dxy_tiny;
		byte *dp;

		if ( (unsigned)dh <= cmd_max_dxy_tiny - cmd_min_dxy_tiny &&
		     dh != 0 && dy == 0
		   )
		   {	op += dh;
			code = set_cmd_put_op(dp, cldev, pcls, op + 0x10, 3);
			if (code < 0)
			  return code;
			if_debug3('L', "    rs2:%d,%d,0,%d\n",
				  dx, dwidth, dheight);
		   }
		else
		   {	code = set_cmd_put_op(dp, cldev, pcls, op + 0x10, 5);
			if (code < 0)
			  return code;
			if_debug4('L', "    rs4:%d,%d,%d,%d\n",
				  dx, dwidth, dy, dheight);
			dp[3] = dy - rmin;
			dp[4] = dheight - rmin;
		   }
		dp[1] = dx - rmin;
		dp[2] = dwidth - rmin;
	   }
#undef rmin
#undef rmax
	else if ( dy >= -2 && dy <= 1 && dheight >= -2 && dheight <= 1 &&
		  (dy + dheight) != -4
		)
	  {	byte *dp;
		int rcsize = 1 + cmd_sizew(x) + cmd_sizew(width);
		code = set_cmd_put_op(dp, cldev, pcls,
			       op + ((dy + 2) << 2) + dheight + 2, rcsize);
		if (code < 0)
		  return code;
		++dp;
		cmd_put2w(x, width, dp);
	  }
	else
	   {	byte *dp;
		int rcsize = 1 + cmd_size_rect(&pcls->rect);
		code = set_cmd_put_op(dp, cldev, pcls, op, rcsize);
		if (code < 0)
		  return code;
		if_debug5('L', "    r%d:%d,%d,%d,%d\n",
			  rcsize - 1, dx, dwidth, dy, dheight);
		cmd_put_rect(&pcls->rect, dp + 1);
	   }
	return 0;
}

/* ---------------- Driver procedures ---------------- */

int
clist_fill_rectangle(gx_device *dev, int x, int y, int width, int height,
  gx_color_index color)
{	int code;
	fit_fill(dev, x, y, width, height);
	BEGIN_RECT
	  TRY_RECT
	    code = cmd_disable_lop(cdev, pcls);
	    if ( color != pcls->colors[1] )
	      code = code < 0 ? code :
		cmd_put_color(cdev, pcls, &clist_select_color1,
	                      color, &pcls->colors[1]);
	    code = code < 0 ? code :
	      cmd_write_rect_cmd(cdev, pcls, cmd_op_fill_rect, x, y,
				 width, height);
	  HANDLE_RECT(code);
	END_RECT
	return 0;
}

int
clist_strip_tile_rectangle(gx_device *dev, const gx_strip_bitmap *tile,
  int x, int y, int width, int height,
  gx_color_index color0, gx_color_index color1, int px, int py)
{	int code;
	int depth =
	  (color1 == gx_no_color_index && color0 == gx_no_color_index ?
	   dev->color_info.depth : 1);

	fit_fill(dev, x, y, width, height);
	BEGIN_RECT
	ulong offset_temp;

	TRY_RECT
	  code = cmd_disable_lop(cdev, pcls);
	HANDLE_RECT(code);
	if ( !cls_has_tile_id(cdev, pcls, tile->id, offset_temp) )
	   {	code = 0;
		if (tile->id != gx_no_bitmap_id)
		  TRY_RECT
		    code = clist_change_tile(cdev, pcls, tile, depth);
		  HANDLE_RECT_UNLESS( code,
		   (code != gs_error_VMerror || !cdev->error_is_retryable) );
		if (code < 0)
		  {	
			/* ok if gx_default... does retries internally: it's self-sufficient */
			code =
			 gx_default_strip_tile_rectangle(dev, tile,
						x, y, width, height,
						color0, color1, px, py);
			if ( code < 0 )
			  ERROR_RECT(code);
			goto endr;
		  }
	   }
	TRY_RECT
	  code = 0;
	  if ( color0 != pcls->tile_colors[0] || color1 != pcls->tile_colors[1] )
	    code = cmd_set_tile_colors(cdev, pcls, color0, color1);
	  if ( px != pcls->tile_phase.x || py != pcls->tile_phase.y )
	    code = code < 0 ? code : cmd_set_tile_phase(cdev, pcls, px, py);
	  code = code < 0 ? code :
	    cmd_write_rect_cmd(cdev, pcls, cmd_op_tile_rect, x, y,
			       width, height);
	HANDLE_RECT(code);
endr:	;
	END_RECT
	return 0;
}

int
clist_copy_mono(gx_device *dev,
    const byte *data, int data_x, int raster, gx_bitmap_id id,
    int x, int y, int width, int height,
    gx_color_index color0, gx_color_index color1)
{	int y0;
	gx_bitmap_id orig_id = id;

	fit_copy(dev, data, data_x, raster, id, x, y, width, height);
	y0 = y;
	BEGIN_RECT
	int dx = data_x & 7;
	int w1 = dx + width;
	const byte *row = data + (y - y0) * raster + (data_x >> 3);
	int code;

	TRY_RECT
	  code = cmd_disable_lop(cdev, pcls);
	  code = code < 0 ? code : cmd_disable_clip(cdev, pcls);
	  if ( color0 != pcls->colors[0] )
	    code = code < 0 ? code : cmd_set_color0(cdev, pcls, color0);
	  if ( color1 != pcls->colors[1] )
	    code = code < 0 ? code : cmd_set_color1(cdev, pcls, color1);
	HANDLE_RECT(code);
	/* Don't bother to check for a possible cache hit: */
	/* tile_rectangle and fill_mask handle those cases. */
copy:	{	gx_cmd_rect rect;
		int rsize;
		byte op = (byte)cmd_op_copy_mono;
		byte *dp;
		uint csize;
		uint compress;
		int code;

		rect.x = x, rect.y = y;
		rect.width = w1, rect.height = height;
		rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
		TRY_RECT
		  code = cmd_put_bits(cdev, pcls, row, w1, height, raster,
				      rsize, (orig_id == gx_no_bitmap_id ?
				      1 << cmd_compress_rle :
				      cmd_mask_compress_any),
				      &dp, &csize);
		HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);

	   compress = (uint)code;
		if ( code < 0 )
		  { /* The bitmap was too large; split up the transfer. */
		    if ( height > 1 )
		      {	/* Split the transfer by reducing the height.
			 * See the comment above BEGIN_RECT in gxcldev.h.
			 */
			height >>= 1;
			goto copy;
		      }
		    {	/* Split a single (very long) row. */
			int w2 = w1 >> 1;
			NEST_RECT;
			code = clist_copy_mono(dev, row, dx,
				raster, gx_no_bitmap_id, x, y,
				w2, 1, color0, color1);
			if (code >= 0)
			  code = clist_copy_mono(dev, row, dx + w2,
				  raster, gx_no_bitmap_id, x + w2, y,
				  w1 - w2, 1, color0, color1);
			UNNEST_RECT;
			if ( code < 0 )
			  ERROR_RECT(code);
			continue;
		    }
		  }
		op += compress;
		if ( dx )
		  { *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
		    *dp++ = cmd_set_misc_data_x + dx;
		  }
		*dp++ = cmd_count_op(op, csize);
		cmd_put2w(x, y, dp);
		cmd_put2w(w1, height, dp);
		pcls->rect = rect;
	   }
	END_RECT
	return 0;
}

int
clist_copy_color(gx_device *dev,
  const byte *data, int data_x, int raster, gx_bitmap_id id,
  int x, int y, int width, int height)
{	int depth = dev->color_info.depth;
	int y0;
	int data_x_bit;

	fit_copy(dev, data, data_x, raster, id, x, y, width, height);
	y0 = y;
	data_x_bit = data_x * depth;
	BEGIN_RECT
	int dx = (data_x_bit & 7) / depth;
	int w1 = dx + width;
	const byte *row = data + (y - y0) * raster + (data_x_bit >> 3);
	int code;

	TRY_RECT
	  code = cmd_disable_lop(cdev, pcls);
	  code = code < 0 ? code : cmd_disable_clip(cdev, pcls);
	HANDLE_RECT(code);
	if ( pcls->color_is_alpha )
	  { byte *dp;
	    TRY_RECT
	      code= set_cmd_put_op(dp, cdev, pcls, cmd_opv_set_copy_color, 1);
	    HANDLE_RECT(code);
	    pcls->color_is_alpha = 0;
	  }
copy:	  {	gx_cmd_rect rect;
		int rsize;
		byte op = (byte)cmd_op_copy_color_alpha;
		byte *dp;
		uint csize;
		uint compress;

		rect.x = x, rect.y = y;
		rect.width = w1, rect.height = height;
		rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
	        TRY_RECT
		  code = cmd_put_bits(cdev, pcls, row, w1 * depth,
				      height, raster, rsize,
				      1 << cmd_compress_rle, &dp, &csize);
		HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);

	   compress = (uint)code;
		if ( code < 0 )
		  { /* The bitmap was too large; split up the transfer. */
		    if ( height > 1 )
		      {	/* Split the transfer by reducing the height.
			 * See the comment above BEGIN_RECT in gxcldev.h.
			 */
			height >>= 1;
			goto copy;
		      }
		    {	/* Split a single (very long) row. */
			int w2 = w1 >> 1;
			NEST_RECT;
			code = clist_copy_color(dev, row, dx,
				raster, gx_no_bitmap_id, x, y,
				w2, 1);
			if (code >= 0)
			  code = clist_copy_color(dev, row, dx + w2,
				  raster, gx_no_bitmap_id, x + w2, y,
				  w1 - w2, 1);
			UNNEST_RECT;
			if ( code < 0 )
			  ERROR_RECT(code);
			continue;
		    }
		  }
		op += compress;
		if ( dx )
		  { *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
		    *dp++ = cmd_set_misc_data_x + dx;
		  }
		*dp++ = cmd_count_op(op, csize);
		cmd_put2w(x, y, dp);
		cmd_put2w(w1, height, dp);
		pcls->rect = rect;

	  }
	END_RECT
	return 0;
}

int
clist_copy_alpha(gx_device *dev, const byte *data, int data_x,
  int raster, gx_bitmap_id id, int x, int y, int width, int height,
  gx_color_index color, int depth)
{	/* I don't like copying the entire body of clist_copy_color */
	/* just to change 2 arguments and 1 opcode, */
	/* but I don't see any alternative that doesn't require */
	/* another level of procedure call even in the common case. */
	int log2_depth = depth >> 1;	/* works for 1,2,4 */
	int y0;
	int data_x_bit;

	fit_copy(dev, data, data_x, raster, id, x, y, width, height);
	y0 = y;
	data_x_bit = data_x << log2_depth;
	BEGIN_RECT
	int dx = (data_x_bit & 7) >> log2_depth;
	int w1 = dx + width;
	const byte *row = data + (y - y0) * raster + (data_x_bit >> 3);
	int code;

	TRY_RECT
	  code = cmd_disable_lop(cdev, pcls);
	  code = code < 0 ? code : cmd_disable_clip(cdev, pcls);
	HANDLE_RECT(code);
	if ( !pcls->color_is_alpha )
	  { byte *dp;
	    TRY_RECT
	      code = set_cmd_put_op(dp, cdev, pcls, cmd_opv_set_copy_alpha, 1);
	    HANDLE_RECT(code);
	    pcls->color_is_alpha = 1;
	  }
	if ( color != pcls->colors[1] )
	  {	TRY_RECT
		code = cmd_set_color1(cdev, pcls, color);
		HANDLE_RECT(code);
	  }
copy:	  {	gx_cmd_rect rect;
		int rsize;
		byte op = (byte)cmd_op_copy_color_alpha;
		byte *dp;
		uint csize;
		uint compress;

		rect.x = x, rect.y = y;
		rect.width = w1, rect.height = height;
		rsize = (dx ? 4 : 2) + cmd_size_rect(&rect);
		TRY_RECT
		code = cmd_put_bits(cdev, pcls, row, w1 << log2_depth,
				    height, raster, rsize,
				    1 << cmd_compress_rle, &dp, &csize);
		HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
	   compress = (uint)code;
		if ( code < 0 )
		  { /* The bitmap was too large; split up the transfer. */
		    if ( height > 1 )
		      {	/* Split the transfer by reducing the height.
			 * See the comment above BEGIN_RECT in gxcldev.h.
			 */
			height >>= 1;
			goto copy;
		      }
		    {	/* Split a single (very long) row. */
			int w2 = w1 >> 1;
			NEST_RECT;
			code = clist_copy_alpha(dev, row, dx,
				raster, gx_no_bitmap_id, x, y,
				w2, 1, color, depth);
			if (code >= 0)
			  code = clist_copy_alpha(dev, row, dx + w2,
				  raster, gx_no_bitmap_id, x + w2, y,
				  w1 - w2, 1, color, depth);
			UNNEST_RECT;
			if ( code < 0 )
			  ERROR_RECT(code);
			continue;
		    }
		  }
		op += compress;
		if ( dx )
		  { *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
		    *dp++ = cmd_set_misc_data_x + dx;
		  }
		*dp++ = cmd_count_op(op, csize);
		*dp++ = depth;
		cmd_put2w(x, y, dp);
		cmd_put2w(w1, height, dp);
		pcls->rect = rect;
	  }
	END_RECT
	return 0;
}

int
clist_strip_copy_rop(gx_device *dev,
  const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  const gx_color_index *scolors,
  const gx_strip_bitmap *textures, const gx_color_index *tcolors,
  int x, int y, int width, int height,
  int phase_x, int phase_y, gs_logical_operation_t lop)
{	gs_rop3_t rop = lop_rop(lop);
	gx_strip_bitmap tile_with_id;
	const gx_strip_bitmap *tiles = textures;
	int y0;

	if ( scolors != 0 && scolors[0] != scolors[1] )
	  { fit_fill(dev, x, y, width, height);
	  }
	else
	  { fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
	  }
	y0 = y;
	/*
	 * We shouldn't need to put the logic below inside BEGIN/END_RECT,
	 * but the lop_enabled flags are per-band.
	 */
	BEGIN_RECT
	const byte *row = sdata + (y - y0) * sraster;
	int code;

	if ( rop3_uses_T(rop) )
	  { if ( tcolors == 0 || tcolors[0] != tcolors[1] )
	      { ulong offset_temp;
	        if ( !cls_has_tile_id(cdev, pcls, tiles->id, offset_temp) )
		  { /* Change tile.  If there is no id, generate one. */
		    if ( tiles->id == gx_no_bitmap_id )
		      { tile_with_id = *tiles;
		        tile_with_id.id = gs_next_ids(1);
			tiles = &tile_with_id;
		      }
		    TRY_RECT
		      code = clist_change_tile(cdev, pcls, tiles,
					       (tcolors != 0 ? 1 :
					        dev->color_info.depth));
		    HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
		    if ( code < 0 )
		      { /*
			 * The error is a limitcheck, so we have a tile that
			 * is too big to fit in the command reading buffer.
			 * For now, just divide up the transfer into scan
			 * lines.  (If a single scan line won't fit, punt.)
			 * Eventually, we'll need a way to transfer the tile
			 * in pieces.
			 */
			uint rep_height = tiles->rep_height;
			gs_id ids;
			gx_strip_bitmap line_tile;
			int iy;

			if ( rep_height == 1 ||
			     /****** CAN'T HANDLE SHIFT YET ******/
			     tiles->rep_shift != 0
			   )
			  return code;
			/*
			 * Allocate enough fake IDs, since the inner call on
			 * clist_strip_copy_rop will need them anyway.
			 */
			ids = gs_next_ids(min(height, rep_height));
			line_tile = *tiles;
			line_tile.size.y = 1;
			line_tile.rep_height = 1;
			for ( iy = 0; iy < height; ++iy )
			  { line_tile.data = tiles->data + line_tile.raster *
			      ((y + iy + phase_y) % rep_height);
			    line_tile.id = ids + (iy % rep_height);
			    /*
			     * Note that since we're only transferring
			     * a single scan line, phase_y is irrelevant;
			     * we may as well use the current tile phase
			     * so we don't have to write extra commands.
			     */
			    /* All strips must go out in same band list */
			    NEST_RECT;
			    code = clist_strip_copy_rop(dev,
				(sdata == 0 ? 0 : row + iy * sraster),
				sourcex, sraster,
				gx_no_bitmap_id, scolors, &line_tile, tcolors,
				x, y + iy, width, 1,
				phase_x, pcls->tile_phase.y, lop);
			    UNNEST_RECT;
			    if ( code < 0 )
			      ERROR_RECT(code);
			  }
			continue;
		      }
		    if ( phase_x != pcls->tile_phase.x ||
			 phase_y != pcls->tile_phase.y
		       )
		      TRY_RECT
			code = cmd_set_tile_phase(cdev, pcls, phase_x,
						  phase_y);
		      HANDLE_RECT(code);
		  }
	      }
	    /* Set the tile colors. */
	    TRY_RECT
	      code = (tcolors != 0 ?
		      cmd_set_tile_colors(cdev, pcls, tcolors[0], tcolors[1]) :
		      cmd_set_tile_colors(cdev, pcls, gx_no_color_index,
					  gx_no_color_index));
	    HANDLE_RECT(code);
	  }
	TRY_RECT
	  code = 0;
	  if ( lop != pcls->lop )
	    code = cmd_set_lop(cdev, pcls, lop);
	  code = code < 0 ? code :cmd_enable_lop(cdev, pcls);
	HANDLE_RECT(code);

	/* Set lop_enabled to -1 so that fill_rectangle / copy_* */
	/* won't attempt to set it to 0. */
	pcls->lop_enabled = -1;
	NEST_RECT;
	if ( scolors != 0 )
	  { if ( scolors[0] == scolors[1] )
	      code = clist_fill_rectangle(dev, x, y, width, height,
					  scolors[1]);
	    else
	      code = clist_copy_mono(dev, row, sourcex, sraster, id,
				     x, y, width, height,
				     scolors[0], scolors[1]);
	  }
	else
	  code = clist_copy_color(dev, row, sourcex, sraster, id,
				  x, y, width, height);
	UNNEST_RECT;
	pcls->lop_enabled = 1;
	if ( code < 0 )
	  ERROR_RECT(code);
	END_RECT
	return 0;	  
}