File: gdevmrop.c

package info (click to toggle)
gs 5.10-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 14,960 kB
  • ctags: 25,299
  • sloc: ansic: 164,376; makefile: 3,020; cpp: 2,237; sh: 1,219; asm: 684; tcl: 434; perl: 56
file content (1108 lines) | stat: -rw-r--r-- 35,026 bytes parent folder | download | duplicates (2)
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/* Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer to
  the GNU General Public License for full details.
  
  Everyone is granted permission to copy, modify and redistribute GNU
  Ghostscript, but only under the conditions described in the GNU General
  Public License.  A copy of this license is supposed to have been given to
  you along with GNU Ghostscript so you can know your rights and
  responsibilities.  It should be in a file named COPYING.  Among other
  things, the copyright notice and this notice must be preserved on all
  copies.
  
  Aladdin Enterprises is not affiliated with the Free Software Foundation or
  the GNU Project.  GNU Ghostscript, as distributed by Aladdin Enterprises,
  does not depend on any other GNU software.
*/

/* gdevmrop.c */
/* RasterOp / transparency / render algorithm implementation for */
/* memory devices */
#include "memory_.h"
#include "gx.h"
#include "gsbittab.h"
#include "gserrors.h"
#include "gsropt.h"
#include "gxdcolor.h"
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gxdevrop.h"
#include "gdevmrop.h"

/* Define whether we implement transparency correctly, or whether we */
/* implement it as documented in the H-P manuals. */
#define TRANSPARENCY_PER_H_P

/**************** NOTE: ****************
 * The 2- and 4-bit cases don't handle transparency right.
 * The 8- and 24-bit cases haven't been tested.
 * The 16- and 32-bit cases aren't implemented.
 ***************** ****************/

#define mdev ((gx_device_memory *)dev)

/* Forward references */
private gs_rop3_t gs_transparent_rop(P1(gs_logical_operation_t lop));

#define chunk byte

/* Calculate the X offset for a given Y value, */
/* taking shift into account if necessary. */
#define x_offset(px, ty, textures)\
  ((textures)->shift == 0 ? (px) :\
   (px) + (ty) / (textures)->rep_height * (textures)->rep_shift)

/* ---------------- Initialization ---------------- */

void
gs_roplib_init(gs_memory_t *mem)
{	/* Replace the default and forwarding copy_rop procedures. */
	gx_default_copy_rop_proc = gx_real_default_copy_rop;
	gx_forward_copy_rop_proc = gx_forward_copy_rop;
	gx_default_strip_copy_rop_proc = gx_real_default_strip_copy_rop;
	gx_forward_strip_copy_rop_proc = gx_forward_strip_copy_rop;
}

/* ---------------- Debugging aids ---------------- */

#ifdef DEBUG

private void
trace_copy_rop(const char *cname, 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)
{	dprintf4("%s: dev=0x%lx(%s) depth=%d\n",
		 cname, (ulong)dev, dev->dname, dev->color_info.depth);
	dprintf4("  source data=0x%lx x=%d raster=%u id=%lu colors=",
		 (ulong)sdata, sourcex, sraster, (ulong)id);
	if ( scolors )
	  dprintf2("(%lu,%lu);\n", scolors[0], scolors[1]);
	else
	  dputs("none;\n");
	if ( textures )
	  dprintf8("  textures=0x%lx size=%dx%d(%dx%d) raster=%u shift=%d(%d)",
		   (ulong)textures, textures->size.x, textures->size.y,
		   textures->rep_width, textures->rep_height, textures->raster,
		   textures->shift, textures->rep_shift);
	else
	  dputs("  textures=none");
	if ( tcolors )
	  dprintf2(" colors=(%lu,%lu)\n", tcolors[0], tcolors[1]);
	else
	  dputs(" colors=none\n");
	dprintf7("  rect=(%d,%d),(%d,%d) phase=(%d,%d) op=0x%x\n",
		 x, y, x + width, y + height, phase_x, phase_y,
		 (uint)lop);
	if ( gs_debug_c('B') )
	{ if ( sdata )
	    debug_dump_bitmap(sdata, sraster, height, "source bits");
	  if ( textures && textures->data )
	    debug_dump_bitmap(textures->data, textures->raster,
			      textures->size.y, "textures bits");
	}
}

#endif

/* ---------------- Monobit RasterOp ---------------- */

int
mem_mono_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 = gs_transparent_rop(lop); /* handle transparency */
	gx_strip_bitmap no_texture;
	bool invert;
	uint draster = mdev->raster;
	uint traster;
	int line_count;
	byte *drow;
	const byte *srow;
	int ty;

	/* If map_rgb_color isn't the default one for monobit memory */
	/* devices, palette might not be set; set it now if needed. */
	if ( mdev->palette.data == 0 )
	  gdev_mem_mono_set_inverted(mdev,
				     (*dev_proc(dev, map_rgb_color))
				       (dev, (gx_color_value)0,
				       (gx_color_value)0, (gx_color_value)0)
				      != 0);
	invert = mdev->palette.data[0] != 0;

#ifdef DEBUG
	if ( gs_debug_c('b') )
	  trace_copy_rop("mem_mono_strip_copy_rop",
			 dev, sdata, sourcex, sraster,
			 id, scolors, textures, tcolors,
			 x, y, width, height, phase_x, phase_y, lop);
	if ( gs_debug_c('B') )
	  debug_dump_bitmap(scan_line_base(mdev, y), mdev->raster,
			    height, "initial dest bits");
#endif

	/*
	 * RasterOp is defined as operating in RGB space; in the monobit
	 * case, this means black = 0, white = 1.  However, most monobit
	 * devices use the opposite convention.  To make this work,
	 * we must precondition the Boolean operation by swapping the
	 * order of bits end-for-end and then inverting.
	 */

	if ( invert )
	  rop = byte_reverse_bits[rop] ^ 0xff;

	/*
	 * From this point on, rop works in terms of device pixel values,
	 * not RGB-space values.
	 */

	/* Modify the raster operation according to the source palette. */
	if ( scolors != 0 )
	{	/* Source with palette. */
		switch ( (int)((scolors[1] << 1) + scolors[0]) )
		{
		case 0: rop = rop3_know_S_0(rop); break;
		case 1: rop = rop3_invert_S(rop); break;
		case 2: break;
		case 3: rop = rop3_know_S_1(rop); break;
		}
	}

	/* Modify the raster operation according to the texture palette. */
	if ( tcolors != 0 )
	{	/* Texture with palette. */
		switch ( (int)((tcolors[1] << 1) + tcolors[0]) )
		{
		case 0: rop = rop3_know_T_0(rop); break;
		case 1: rop = rop3_invert_T(rop); break;
		case 2: break;
		case 3: rop = rop3_know_T_1(rop); break;
		}
	}

	/* Handle constant source and/or texture, and other special cases. */
	{ gx_color_index color0, color1;

	switch ( rop_usage_table[rop] )
	  {
	  case rop_usage_none:
	    /* We're just filling with a constant. */
	    return (*dev_proc(dev, fill_rectangle))
	      (dev, x, y, width, height, (gx_color_index)(rop & 1));
	  case rop_usage_D:
	    /* This is either D (no-op) or ~D. */
	    if ( rop == rop3_D )
	      return 0;
	    /* Code no_S inline, then finish with no_T. */
	    fit_fill(dev, x, y, width, height);
	    sdata = mdev->base;
	    sourcex = x;
	    sraster = 0;
	    goto no_T;
	  case rop_usage_S:
	    /* This is either S or ~S, which copy_mono can handle. */
	    if ( rop == rop3_S )
	      color0 = 0, color1 = 1;
	    else
	      color0 = 1, color1 = 0;
do_copy:    return (*dev_proc(dev, copy_mono))
	      (dev, sdata, sourcex, sraster, id, x, y, width, height,
	       color0, color1);
	  case rop_usage_DS:
	    /* This might be a case that copy_mono can handle. */
#define copy_case(c0, c1) color0 = c0, color1 = c1; goto do_copy;
	    switch ( (uint)rop ) /* cast shuts up picky compilers */
	      {
	      case rop3_D & rop3_not(rop3_S): copy_case(gx_no_color_index, 0);
	      case rop3_D | rop3_S: copy_case(gx_no_color_index, 1);
	      case rop3_D & rop3_S: copy_case(0, gx_no_color_index);
	      case rop3_D | rop3_not(rop3_S): copy_case(1, gx_no_color_index);
	      default: ;
	      }
#undef copy_case
	    fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
no_T:	    /* Texture is not used; textures may be garbage. */
	    no_texture.data = mdev->base;	/* arbitrary */
	    no_texture.raster = 0;
	    no_texture.size.x = width;
	    no_texture.size.y = height;
	    no_texture.rep_width = no_texture.rep_height = 1;
	    no_texture.rep_shift = no_texture.shift = 0;
	    textures = &no_texture;
	    break;
	  case rop_usage_T:
	    /* This is either T or ~T, which tile_rectangle can handle. */
	    if ( rop == rop3_T )
	      color0 = 0, color1 = 1;
	    else
	      color0 = 1, color1 = 0;
do_tile:    return (*dev_proc(dev, strip_tile_rectangle))
	      (dev, textures, x, y, width, height, color0, color1,
	       phase_x, phase_y);
	  case rop_usage_DT:
	    /* This might be a case that tile_rectangle can handle. */
#define tile_case(c0, c1) color0 = c0, color1 = c1; goto do_tile;
	    switch ( (uint)rop ) /* cast shuts up picky compilers */
	      {
	      case rop3_D & rop3_not(rop3_T): tile_case(gx_no_color_index, 0);
	      case rop3_D | rop3_T: tile_case(gx_no_color_index, 1);
	      case rop3_D & rop3_T: tile_case(0, gx_no_color_index);
	      case rop3_D | rop3_not(rop3_T): tile_case(1, gx_no_color_index);
	      default: ;
	      }
#undef tile_case
	    fit_fill(dev, x, y, width, height);
	    /* Source is not used; sdata et al may be garbage. */
	    sdata = mdev->base;		/* arbitrary, as long as all */
					/* accesses are valid */
	    sourcex = x;		/* guarantee no source skew */
	    sraster = 0;
	    break;
	  default:		/* rop_usage_[D]ST */
	    fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
	  }
	}

#ifdef DEBUG
	if_debug1('b', "final rop=0x%x\n", rop);
#endif

	/* Set up transfer parameters. */
	line_count = height;
	srow = sdata;
	drow = scan_line_base(mdev, y);
	traster = textures->raster;
	ty = y + phase_y;

	/* Loop over scan lines. */
	for ( ; line_count-- > 0; drow += draster, srow += sraster, ++ty )
	{	int sx = sourcex;
		int dx = x;
		int w = width;
		const byte *trow =
		  textures->data + (ty % textures->rep_height) * traster;
		int xoff = x_offset(phase_x, ty, textures);
		int nw;

		/* Loop over (norizontal) copies of the tile. */
		for ( ; w > 0; sx += nw, dx += nw, w -= nw )
		{	int dbit = dx & 7;
			int sbit = sx & 7;
			int sskew = sbit - dbit;
			int tx = (dx + xoff) % textures->rep_width;
			int tbit = tx & 7;
			int tskew = tbit - dbit;
			int left = nw = min(w, textures->size.x - tx);
			byte lmask = 0xff >> dbit;
			byte rmask = 0xff << (~(dbit + nw - 1) & 7);
			byte mask = lmask;
			int nx = 8 - dbit;
			byte *dptr = drow + (dx >> 3);
			const byte *sptr = srow + (sx >> 3);
			const byte *tptr = trow + (tx >> 3);

			if ( sskew < 0 )
			  --sptr, sskew += 8;
			if ( tskew < 0 )
			  --tptr, tskew += 8;
			for ( ; left > 0;
			      left -= nx, mask = 0xff, nx = 8,
			        ++dptr, ++sptr, ++tptr
			    )
			    {	byte dbyte = *dptr;
#define fetch1(ptr, skew)\
  (skew ? (ptr[0] << skew) + (ptr[1] >> (8 - skew)) : *ptr)
				byte sbyte = fetch1(sptr, sskew);
				byte tbyte = fetch1(tptr, tskew);
#undef fetch1
				byte result =
				  (*rop_proc_table[rop])(dbyte, sbyte, tbyte);
				if ( left <= nx )
				  mask &= rmask;
				*dptr = (mask == 0xff ? result :
					 (result & mask) | (dbyte & ~mask));
			 }
		}
	}
#ifdef DEBUG
	if ( gs_debug_c('B') )
	  debug_dump_bitmap(scan_line_base(mdev, y), mdev->raster,
			    height, "final dest bits");
#endif
	return 0;
}

/* ---------------- Fake RasterOp for 2- and 4-bit devices ---------------- */

int
mem_gray_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)
{	gx_color_index scolors2[2];
	const gx_color_index *real_scolors = scolors;
	gx_color_index tcolors2[2];
	const gx_color_index *real_tcolors = tcolors;
	gx_strip_bitmap texture2;
	const gx_strip_bitmap *real_texture = textures;
	long tdata;
	int depth = dev->color_info.depth;
	int log2_depth = depth >> 1;		/* works for 2, 4 */
	gx_color_index max_pixel = (1 << depth) - 1;
	int code;

#ifdef DEBUG
	if ( gs_debug_c('b') )
	  trace_copy_rop("mem_gray_strip_copy_rop",
			 dev, sdata, sourcex, sraster,
			 id, scolors, textures, tcolors,
			 x, y, width, height, phase_x, phase_y, lop);
#endif
	if ( scolors )
	{	/* We can't handle "real" source colors. */
		if ( (scolors[0] | scolors[1]) & ~max_pixel )
		  return_error(gs_error_rangecheck);
		scolors2[0] = scolors[0] & 1;
		scolors2[1] = scolors[1] & 1;
		real_scolors = scolors2;
	}
	if ( textures )
	{	texture2 = *textures;
		texture2.size.x <<= log2_depth;
		texture2.rep_width <<= log2_depth;
		texture2.shift <<= log2_depth;
		texture2.rep_shift <<= log2_depth;
		real_texture = &texture2;
	}
	if ( tcolors )
	{	/* We can't handle monobit textures. */
		if ( tcolors[0] != tcolors[1] )
		  return_error(gs_error_rangecheck);
		/* For polybit textures with colors other than */
		/* all 0s or all 1s, fabricate the data. */
		if ( tcolors[0] != 0 && tcolors[0] != max_pixel )
		{	real_tcolors = 0;
			*(byte *)&tdata = (byte)tcolors[0] << (8 - depth);
			texture2.data = (byte *)&tdata;
			texture2.raster = align_bitmap_mod;
			texture2.size.x = texture2.rep_width = depth;
			texture2.size.y = texture2.rep_height = 1;
			texture2.id = gx_no_bitmap_id;
			texture2.shift = texture2.rep_shift = 0;
			real_texture = &texture2;
		}
		else
		{	tcolors2[0] = tcolors2[1] = tcolors[0] & 1;
			real_tcolors = tcolors2;
		}
	}
	dev->width <<= log2_depth;
	code = mem_mono_strip_copy_rop(dev, sdata,
		(real_scolors == NULL ? sourcex << log2_depth : sourcex),
		sraster, id, real_scolors, real_texture, real_tcolors,
		x << log2_depth, y, width << log2_depth, height,
		phase_x << log2_depth, phase_y, lop);
	dev->width >>= log2_depth;
	return code;
}

/* ---------------- RasterOp with 8-bit gray / 24-bit RGB ---------------- */

int
mem_gray8_rgb24_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_color_index const_source = gx_no_color_index;
	gx_color_index const_texture = gx_no_color_index;
	uint draster = mdev->raster;
	int line_count;
	byte *drow;
	int depth = dev->color_info.depth;
	int bpp = depth >> 3;		/* bytes per pixel, 1 or 3 */
	gx_color_index all_ones = ((gx_color_index)1 << depth) - 1;
	gx_color_index strans =
	  (lop & lop_S_transparent ? all_ones : gx_no_color_index);
	gx_color_index ttrans =
	  (lop & lop_T_transparent ? all_ones : gx_no_color_index);

	/* Check for constant source. */
	if ( scolors != 0 && scolors[0] == scolors[1] )
	{	/* Constant source */
		const_source = scolors[0];
		if ( const_source == 0 )
		  rop = rop3_know_S_0(rop);
		else if ( const_source == all_ones )
		  rop = rop3_know_S_1(rop);
	}
	else if ( !rop3_uses_S(rop) )
	  const_source = 0;		/* arbitrary */

	/* Check for constant texture. */
	if ( tcolors != 0 && tcolors[0] == tcolors[1] )
	{	/* Constant texture */
		const_texture = tcolors[0];
		if ( const_texture == 0 )
		  rop = rop3_know_T_0(rop);
		else if ( const_texture == all_ones )
		  rop = rop3_know_T_1(rop);
	}
	else if ( !rop3_uses_T(rop) )
	  const_texture = 0;		/* arbitrary */

	/* Adjust coordinates to be in bounds. */
	if ( const_source == gx_no_color_index )
	{	fit_copy(dev, sdata, sourcex, sraster, id,
			 x, y, width, height);
	}
	else
	{	fit_fill(dev, x, y, width, height);
	}

	/* Set up transfer parameters. */
	line_count = height;
	drow = scan_line_base(mdev, y) + x * bpp;

	/*
	 * There are 18 cases depending on whether each of the source and
	 * texture is constant, 1-bit, or multi-bit, and on whether the
	 * depth is 8 or 24 bits.  We divide first according to constant
	 * vs. non-constant, and then according to 1- vs. multi-bit, and
	 * finally according to pixel depth.  This minimizes source code,
	 * but not necessarily time, since we do some of the divisions
	 * within 1 or 2 levels of loop.
	 */

#define dbit(base, i) ((base)[(i) >> 3] & (0x80 >> ((i) & 7)))
/* 8-bit */
#define cbit8(base, i, colors)\
  (dbit(base, i) ? (byte)colors[1] : (byte)colors[0])
#define rop_body_8()\
  if ( s_pixel == strans ||	/* So = 0, s_tr = 1 */\
       t_pixel == ttrans	/* Po = 0, p_tr = 1 */\
     )\
    continue;\
  *dptr = (*rop_proc_table[rop])(*dptr, s_pixel, t_pixel)
/* 24-bit */
#define get24(ptr)\
  (((gx_color_index)(ptr)[0] << 16) | ((gx_color_index)(ptr)[1] << 8) | (ptr)[2])
#define put24(ptr, pixel)\
  (ptr)[0] = (byte)((pixel) >> 16),\
  (ptr)[1] = (byte)((uint)(pixel) >> 8),\
  (ptr)[2] = (byte)(pixel)
#define cbit24(base, i, colors)\
  (dbit(base, i) ? colors[1] : colors[0])
#define rop_body_24()\
  if ( s_pixel == strans ||	/* So = 0, s_tr = 1 */\
       t_pixel == ttrans	/* Po = 0, p_tr = 1 */\
     )\
    continue;\
  { gx_color_index d_pixel = get24(dptr);\
    d_pixel = (*rop_proc_table[rop])(d_pixel, s_pixel, t_pixel);\
    put24(dptr, d_pixel);\
  }

	if ( const_texture != gx_no_color_index )	/**** Constant texture ****/
	  {
	    if ( const_source != gx_no_color_index )	/**** Constant source & texture ****/
	      {
		for ( ; line_count-- > 0; drow += draster )
		  { byte *dptr = drow;
		    int left = width;
		    if ( bpp == 1 )	/**** 8-bit destination ****/
#define s_pixel (byte)const_source
#define t_pixel (byte)const_texture
		      for ( ; left > 0; ++dptr, --left )
			{ rop_body_8();
			}
#undef s_pixel
#undef t_pixel
		    else		/**** 24-bit destination ****/
#define s_pixel const_source
#define t_pixel const_texture
		      for ( ; left > 0; dptr += 3, --left )
			{ rop_body_24();
			}
#undef s_pixel
#undef t_pixel
		  }
	      }
	    else			/**** Data source, const texture ****/
	      { const byte *srow = sdata;
		for ( ; line_count-- > 0; drow += draster, srow += sraster )
		  { byte *dptr = drow;
		    int left = width;
		    if ( scolors )	/**** 1-bit source ****/
		      {	int sx = sourcex;
			if ( bpp == 1 )	/**** 8-bit destination ****/
#define t_pixel (byte)const_texture
			  for ( ; left > 0; ++dptr, ++sx, --left )
			    { byte s_pixel = cbit8(srow, sx, scolors);
			      rop_body_8();
			    }
#undef t_pixel
			else		/**** 24-bit destination ****/
#define t_pixel const_texture
			  for ( ; left > 0; dptr += 3, ++sx, --left )
			    { bits32 s_pixel = cbit24(srow, sx, scolors);
			      rop_body_24();
			    }
#undef t_pixel
		      }
		    else if ( bpp == 1)	/**** 8-bit source & dest ****/
		      {	const byte *sptr = srow + sourcex;
#define t_pixel (byte)const_texture
			for ( ; left > 0; ++dptr, ++sptr, --left )
			  { byte s_pixel = *sptr;
			    rop_body_8();
			  }
#undef t_pixel
		      }
		    else		/**** 24-bit source & dest ****/
		      {	const byte *sptr = srow + sourcex * 3;
#define t_pixel const_texture
			for ( ; left > 0; dptr += 3, sptr += 3, --left )
			  { bits32 s_pixel = get24(sptr);
			    rop_body_24();
			  }
#undef t_pixel
		      }
		  }
	      }
	  }
	else if ( const_source != gx_no_color_index )	/**** Const source, data texture ****/
	  {	uint traster = textures->raster;
		int ty = y + phase_y;

		for ( ; line_count-- > 0; drow += draster, ++ty )
		  { /* Loop over copies of the tile. */
		    int dx = x, w = width, nw;
		    byte *dptr = drow;
		    const byte *trow =
		      textures->data + (ty % textures->size.y) * traster;
		    int xoff = x_offset(phase_x, ty, textures);

		    for ( ; w > 0; dx += nw, w -= nw )
		      { int tx = (dx + xoff) % textures->rep_width;
			int left = nw = min(w, textures->size.x - tx);
			const byte *tptr = trow;

			if ( tcolors )	/**** 1-bit texture ****/
			  { if ( bpp == 1 )	/**** 8-bit dest ****/
#define s_pixel (byte)const_source
			      for ( ; left > 0; ++dptr, ++tx, --left )
				{ byte t_pixel = cbit8(tptr, tx, tcolors);
				  rop_body_8();
				}
#undef s_pixel
			    else		/**** 24-bit dest ****/
#define s_pixel const_source
			      for ( ; left > 0; dptr += 3, ++tx, --left )
				{ bits32 t_pixel = cbit24(tptr, tx, tcolors);
				  rop_body_24();
				}
#undef s_pixel
			  }
			else if ( bpp == 1 )	/**** 8-bit T & D ****/
			  { tptr += tx;
#define s_pixel (byte)const_source
			    for ( ; left > 0; ++dptr, ++tptr, --left )
			      {	byte t_pixel = *tptr;
				rop_body_8();
			      }
#undef s_pixel
			  }
			else			/**** 24-bit T & D ****/
			  { tptr += tx * 3;
#define s_pixel const_source
			    for ( ; left > 0; dptr += 3, tptr += 3, --left )
			      {	bits32 t_pixel = get24(tptr);
				rop_body_24();
			      }
#undef s_pixel
			  }
		      }
		  }
	  }
	else				/**** Data source & texture ****/
	  {
	uint traster = textures->raster;
	int ty = y + phase_y;
	const byte *srow = sdata;

	/* Loop over scan lines. */
	for ( ; line_count-- > 0; drow += draster, srow += sraster, ++ty )
	{	/* Loop over copies of the tile. */
		int sx = sourcex;
		int dx = x;
		int w = width;
		int nw;
		byte *dptr = drow;
		const byte *trow =
		  textures->data + (ty % textures->size.y) * traster;
		int xoff = x_offset(phase_x, ty, textures);

		for ( ; w > 0; dx += nw, w -= nw )
		{	/* Loop over individual pixels. */
			int tx = (dx + xoff) % textures->rep_width;
			int left = nw = min(w, textures->size.x - tx);
			const byte *tptr = trow;

			/*
			 * For maximum speed, we should split this loop
			 * into 7 cases depending on source & texture
			 * depth: (1,1), (1,8), (1,24), (8,1), (8,8),
			 * (24,1), (24,24).  But since we expect these
			 * cases to be relatively uncommon, we just
			 * divide on the destination depth.
			 */
			if ( bpp == 1 )		/**** 8-bit destination ****/
			  { const byte *sptr = srow + sx;
			    tptr += tx;
			    for ( ; left > 0; ++dptr, ++sptr, ++tptr, ++sx, ++tx, --left )
			      { byte s_pixel =
				 (scolors ? cbit8(srow, sx, scolors) : *sptr);
				byte t_pixel =
				 (tcolors ? cbit8(tptr, tx, tcolors) : *tptr);
				rop_body_8();
			      }
			  }
			else			/**** 24-bit destination ****/
			  { const byte *sptr = srow + sx * 3;
			    tptr += tx * 3;
			    for ( ; left > 0; dptr += 3, sptr += 3, tptr += 3, ++sx, ++tx, --left )
			      { bits32 s_pixel =
				  (scolors ? cbit24(srow, sx, scolors) :
				   get24(sptr));
				bits32 t_pixel =
				  (tcolors ? cbit24(tptr, tx, tcolors) :
				   get24(tptr));
				rop_body_24();
			      }
			  }
		}
	}
	}
#undef rop_body_8
#undef rop_body_24
#undef dbit
#undef cbit8
#undef cbit24
	return 0;
}

/* ---------------- Default copy_rop implementations ---------------- */

#undef mdev

int
gx_real_default_copy_rop(gx_device *dev,
  const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  const gx_color_index *scolors,
  const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  int x, int y, int width, int height,
  int phase_x, int phase_y, gs_logical_operation_t lop)
{	const gx_strip_bitmap *textures;
	gx_strip_bitmap tiles;

	if ( texture == 0 )
	  textures = 0;
	else
	  { *(gx_tile_bitmap *)&tiles = *texture;
	    tiles.rep_shift = tiles.shift = 0;
	    textures = &tiles;
	  }
	return (*dev_proc(dev, strip_copy_rop))
	  (dev, sdata, sourcex, sraster, id, scolors, textures, tcolors,
	   x, y, width, height, phase_x, phase_y, lop);
}

int
gx_real_default_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)
{	/*
	 * The default implementation uses get_bits to read out the
	 * pixels, the memory device implementation to do the operation,
	 * and copy_color to write the pixels back.
	 */
	int depth = dev->color_info.depth;
	const gx_device_memory *mdproto = gdev_mem_device_for_bits(depth);
	gx_device_memory mdev;
	uint draster = gx_device_raster(dev, true);
	bool uses_d = rop3_uses_D(gs_transparent_rop(lop));
	byte *row;
	int code;
	int py;

#ifdef DEBUG
	if ( gs_debug_c('b') )
	  trace_copy_rop("gx_default_strip_copy_rop",
			 dev, sdata, sourcex, sraster,
			 id, scolors, textures, tcolors,
			 x, y, width, height, phase_x, phase_y, lop);
#endif
	if ( mdproto == 0 )
	  return_error(gs_error_rangecheck);
	if ( sdata == 0 )
	  { fit_fill(dev, x, y, width, height);
	  }
	else
	  { fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
	  }
	gs_make_mem_device(&mdev, mdproto, 0, -1, dev);
	mdev.width = width;
	mdev.height = 1;
	mdev.bitmap_memory = &gs_memory_default;
	code = (*dev_proc(&mdev, open_device))((gx_device *)&mdev);
	if ( code < 0 )
	  return code;
	row = gs_malloc(1, draster, "copy_rop buffer");
	if ( row == 0 )
	  { (*dev_proc(&mdev, close_device))((gx_device *)&mdev);
	    return_error(gs_error_VMerror);
	  }
	for ( py = y; py < y + height; ++py )
	  { byte *data;

	    if ( uses_d )
	      { code = (*dev_proc(dev, get_bits))(dev, py, row, &data);
		if ( code < 0 )
		  break;
		code = (*dev_proc(&mdev, copy_color))((gx_device *)&mdev,
			data, x, draster, gx_no_bitmap_id,
			0, 0, width, 1);
		if ( code < 0 )
		  return code;
	      }
	    code = (*dev_proc(&mdev, strip_copy_rop))((gx_device *)&mdev,
			sdata + (py - y) * sraster, sourcex, sraster,
			gx_no_bitmap_id, scolors, textures, tcolors,
			0, 0, width, 1, phase_x + x, phase_y + py,
			lop);
	    if ( code < 0 )
	      break;
	    code = (*dev_proc(&mdev, get_bits))((gx_device *)&mdev, 0, row, &data);
	    if ( code < 0 )
	      break;
	    code = (*dev_proc(dev, copy_color))(dev,
			data, 0, draster, gx_no_bitmap_id,
			x, py, width, 1);
	    if ( code < 0 )
	      break;
	  }
	gs_free(row, 1, draster, "copy_rop buffer");
	(*dev_proc(&mdev, close_device))((gx_device *)&mdev);
	return code;
}

int
gx_forward_copy_rop(gx_device *dev,
  const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  const gx_color_index *scolors,
  const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  int x, int y, int width, int height,
  int phase_x, int phase_y, gs_logical_operation_t lop)
{	gx_device *tdev = ((gx_device_forward *)dev)->target;
	dev_proc_copy_rop((*proc));

	if ( tdev == 0 )
	  tdev = dev, proc = gx_default_copy_rop;
	else
	  proc = dev_proc(tdev, copy_rop);
	return (*proc)(tdev, sdata, sourcex, sraster, id, scolors,
		       texture, tcolors, x, y, width, height,
		       phase_x, phase_y, lop);
}

int
gx_forward_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)
{	gx_device *tdev = ((gx_device_forward *)dev)->target;
	dev_proc_strip_copy_rop((*proc));

	if ( tdev == 0 )
	  tdev = dev, proc = gx_default_strip_copy_rop;
	else
	  proc = dev_proc(tdev, strip_copy_rop);
	return (*proc)(tdev, sdata, sourcex, sraster, id, scolors,
		       textures, tcolors, x, y, width, height,
		       phase_x, phase_y, lop);
}

int
gx_copy_rop_unaligned(gx_device *dev,
  const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  const gx_color_index *scolors,
  const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  int x, int y, int width, int height,
  int phase_x, int phase_y, gs_logical_operation_t lop)
{	const gx_strip_bitmap *textures;
	gx_strip_bitmap tiles;

	if ( texture == 0 )
	  textures = 0;
	else
	  { *(gx_tile_bitmap *)&tiles = *texture;
	    tiles.rep_shift = tiles.shift = 0;
	    textures = &tiles;
	  }
	return gx_strip_copy_rop_unaligned
	  (dev, sdata, sourcex, sraster, id, scolors, textures, tcolors,
	   x, y, width, height, phase_x, phase_y, lop);
}

int
gx_strip_copy_rop_unaligned(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)
{	dev_proc_strip_copy_rop((*copy_rop)) = dev_proc(dev, strip_copy_rop);
	int depth = (scolors == 0 ? dev->color_info.depth : 1);
	int step = sraster & (align_bitmap_mod - 1);

	/* Adjust the origin. */
	if ( sdata != 0 )
	  { uint offset =
	      (uint)(sdata - (const byte *)0) & (align_bitmap_mod - 1);
	    /* See copy_color above re the following statement. */
	    if ( depth == 24 )
	      offset += (offset % 3) *
		(align_bitmap_mod * (3 - (align_bitmap_mod % 3)));
	    sdata -= offset;
	    sourcex += (offset << 3) / depth;
	  }

	/* Adjust the raster. */
	if ( !step || sdata == 0 ||
	     (scolors != 0 && scolors[0] == scolors[1])
	   )
	  { /* No adjustment needed. */
	    return (*copy_rop)(dev, sdata, sourcex, sraster, id, scolors,
			       textures, tcolors, x, y, width, height,
			       phase_x, phase_y, lop);
	  }

	/* Do the transfer one scan line at a time. */
	{ const byte *p = sdata;
	  int d = sourcex;
	  int dstep = (step << 3) / depth;
	  int code = 0;
	  int i;

	  for ( i = 0; i < height && code >= 0;
	        ++i, p += sraster - step, d += dstep
	      )
	    code = (*copy_rop)(dev, p, d, sraster, gx_no_bitmap_id, scolors,
			       textures, tcolors, x, y + i, width, 1,
			       phase_x, phase_y, lop);
	  return code;
	}
}

/* ---------------- RasterOp texture device ---------------- */

public_st_device_rop_texture();

/* Device for clipping with a region. */
private dev_proc_fill_rectangle(rop_texture_fill_rectangle);
private dev_proc_copy_mono(rop_texture_copy_mono);
private dev_proc_copy_color(rop_texture_copy_color);

/* The device descriptor. */
private const gx_device_rop_texture far_data gs_rop_texture_device =
{	std_device_std_body(gx_device_rop_texture, 0, "rop source",
	  0, 0, 1, 1),
	{	NULL,		/* open_device */
		gx_forward_get_initial_matrix,
		NULL,		/* default_sync_output */
		NULL,		/* output_page */
		NULL,		/* close_device */
		gx_forward_map_rgb_color,
		gx_forward_map_color_rgb,
		rop_texture_fill_rectangle,
		NULL,		/* tile_rectangle */
		rop_texture_copy_mono,
		rop_texture_copy_color,
		NULL,		/* draw_line */
		NULL,		/* get_bits */
		gx_forward_get_params,
		gx_forward_put_params,
		gx_forward_map_cmyk_color,
		gx_forward_get_xfont_procs,
		gx_forward_get_xfont_device,
		gx_forward_map_rgb_alpha_color,
		gx_forward_get_page_device,
		NULL,		/* get_alpha_bits (no alpha) */
		gx_no_copy_alpha,		/* shouldn't be called */
		gx_forward_get_band,
		gx_no_copy_rop,			/* shouldn't be called */
		NULL,		/* fill_path */
		NULL,		/* stroke_path */
		NULL,		/* fill_mask */
		NULL,		/* fill_trapezoid */
		NULL,		/* fill_parallelogram */
		NULL,		/* fill_triangle */
		NULL,		/* draw_thin_line */
		NULL,		/* begin_image */
		NULL,		/* image_data */
		NULL,		/* end_image */
		NULL,		/* strip_tile_rectangle */
		NULL,		/* strip_copy_rop */
		gx_forward_get_clipping_box
	},
	0,			/* target */
	lop_default,		/* log_op */
	NULL			/* texture */
};
#define rtdev ((gx_device_rop_texture *)dev)

/* Initialize a RasterOp source device. */
void
gx_make_rop_texture_device(gx_device_rop_texture *dev, gx_device *target,
  gs_logical_operation_t log_op, const gx_device_color *texture)
{	*dev = gs_rop_texture_device;
	/* Drawing operations are defaulted, non-drawing are forwarded. */
	gx_device_fill_in_procs((gx_device *)dev);
	dev->color_info = target->color_info;
	dev->target = target;
	dev->log_op = log_op;
	dev->texture = texture;
}

/* Fill a rectangle */
private int
rop_texture_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  gx_color_index color)
{	gx_rop_source_t source;

	source.sdata = NULL;
	source.sourcex = 0;
	source.sraster = 0;
	source.id = gx_no_bitmap_id;
	source.scolors[0] = source.scolors[1] = color;
	source.use_scolors = true;
	return gx_device_color_fill_rectangle(rtdev->texture,
				x, y, w, h, rtdev->target,
				rtdev->log_op, &source);
}

/* Copy a monochrome rectangle */
private int
rop_texture_copy_mono(gx_device *dev,
  const byte *data, int sourcex, int raster, gx_bitmap_id id,
  int x, int y, int w, int h,
  gx_color_index color0, gx_color_index color1)
{	gx_rop_source_t source;
	gs_logical_operation_t lop = rtdev->log_op;

	source.sdata = data;
	source.sourcex = sourcex;
	source.sraster = raster;
	source.id = id;
	source.scolors[0] = color0;
	source.scolors[1] = color1;
	source.use_scolors = true;
	/* Adjust the logical operation per transparent colors. */
	if ( color0 == gx_no_color_index )
	  lop = rop3_use_D_when_S_0(lop);
	else if ( color1 == gx_no_color_index )
	  lop = rop3_use_D_when_S_1(lop);
	return gx_device_color_fill_rectangle(rtdev->texture,
				x, y, w, h, rtdev->target,
				lop, &source);
}

/* Copy a color rectangle */
private int
rop_texture_copy_color(gx_device *dev,
  const byte *data, int sourcex, int raster, gx_bitmap_id id,
  int x, int y, int w, int h)
{	gx_rop_source_t source;

	source.sdata = data;
	source.sourcex = sourcex;
	source.sraster = raster;
	source.id = id;
	source.scolors[0] = source.scolors[1] = gx_no_color_index;
	source.use_scolors = true;
	return gx_device_color_fill_rectangle(rtdev->texture,
				x, y, w, h, rtdev->target,
				rtdev->log_op, &source);
}

/* ---------------- Internal routines ---------------- */

/* Compute the effective RasterOp for the 1-bit case, */
/* taking transparency into account. */
private gs_rop3_t
gs_transparent_rop(gs_logical_operation_t lop)
{	gs_rop3_t rop = lop_rop(lop);
	/*
	 * The algorithm for computing an effective RasterOp is presented,
	 * albeit obfuscated, in the H-P PCL5 technical documentation.
	 * Define So ("source opaque") and Po ("pattern opaque") as masks
	 * that have 1-bits precisely where the source or pattern
	 * respectively are not white (transparent).
	 * One applies the original RasterOp to compute an intermediate
	 * result R, and then computes the final result as
	 * (R & M) | (D & ~M) where M depends on transparencies as follows:
	 *	s_tr	p_tr	M
	 *	 0	 0	1
	 *	 0	 1	~So | Po (? Po ?)
	 *	 1	 0	So
	 *	 1	 1	So & Po
	 * The s_tr = 0, p_tr = 1 case seems wrong, but it's clearly
	 * specified that way in the "PCL 5 Color Technical Reference
	 * Manual."
	 *
	 * In the 1-bit case, So = ~S and Po = ~P, so we can apply the
	 * above table directly.
	 */
#define So rop3_not(rop3_S)
#define Po rop3_not(rop3_T)
#ifdef TRANSPARENCY_PER_H_P
#  define MPo (rop3_uses_S(rop) ? rop3_not(So) | Po : Po)
#else
#  define MPo Po
#endif
	/*
	 * If the operation doesn't use S or T, we must disregard the
	 * corresponding transparency flag.
	 */
#define source_transparent ((lop & lop_S_transparent) && rop3_uses_S(rop))
#define pattern_transparent ((lop & lop_T_transparent) && rop3_uses_T(rop))
	gs_rop3_t mask =
	  (source_transparent ?
	   (pattern_transparent ? So & Po : So) :
	   (pattern_transparent ? MPo : rop3_1));
#undef MPo
	return (rop & mask) | (rop3_D & ~mask);
}