summaryrefslogtreecommitdiff
path: root/gs/base/gxclrect.c
blob: 06928969df710c4ef5765e291903f8f59cf8203c (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
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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* 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"
#include "gxclpath.h"
#include "gxdevsop.h"

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

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

/* Write a rectangle. */
static 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);
}
static 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_hl_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
                      int op, int x, int y, int width, int height, 
                      bool extended_command)
{
    byte *dp;
    int code;
    int rcsize;

    cmd_set_rect(pcls->rect);
    if (extended_command) { 
        rcsize = 2 + cmd_size_rect(&pcls->rect);
        code = set_cmd_put_op(dp, cldev, pcls, cmd_opv_extend, rcsize);
        dp[1] = op;
        dp += 2;
    } else {
        rcsize = 1 + cmd_size_rect(&pcls->rect);
        code = set_cmd_put_op(dp, cldev, pcls, op, rcsize);
        dp += 1;
    }
    if (code < 0)
        return code;
    if_debug5('L', "rect hl r%d:%d,%d,%d,%d\n", 
        rcsize - 1, pcls->rect.x, pcls->rect.y, pcls->rect.width, pcls->rect.height);
    cmd_put_rect(&pcls->rect, dp);
    return 0;
}

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 dx = x - pcls->rect.x;
    int dy = y - pcls->rect.y;
    int dwidth = width - pcls->rect.width;
    int dheight = height - pcls->rect.height;
    byte *dp;
    int code;

#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;

        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;

        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
        ) {
        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 {
        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;
}

/* Put out a fill or tile rectangle command for fillpage. */
int
cmd_write_page_rect_cmd(gx_device_clist_writer * cldev, int op)
{
    /* Since cmd_write_rect_cmd always writes 0x0 rectangle as a tiny one,
       here we write a "big" rectangle with the size 0x0.
       The clist reader must handle this case especially.
     */
    int rcsize = 1 + 4 * cmd_sizew(0);
    byte *dp;
    int x = 0, y = 0, width = 0, height = 0; /* For cmd_set_rect */
    gx_clist_state *pcls1;
    int code;

    if_debug0('L', "[L]fillpage beg\n");
    code = set_cmd_put_all_op(dp, cldev, op, rcsize);
    if (code < 0)
        return code;
    for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++)
        cmd_set_rect(pcls1->rect);
    ++dp;
    cmd_putw(0, dp);
    cmd_putw(0, dp);
    cmd_putw(0, dp);
    cmd_putw(0, dp);
    if_debug0('L', "[L]fillpage end\n");
    return 0;
}

static inline byte *
cmd_put_frac31_color(gx_device_clist_writer * cldev, const frac31 *c, byte *dp)
{
    int num_components = cldev->clist_color_info.num_components;
    int j;

    for (j = 0; j < num_components; j++)
        dp = cmd_put_frac31(c[j], dp);
    return dp;
}

static inline int
cmd_size_frac31_color(gx_device_clist_writer * cldev, const frac31 *c)
{
    int j, s = 0;
    int num_components = cldev->clist_color_info.num_components;

    for (j = 0; j < num_components; j++)
        s += cmd_size_frac31(c[j]);
    return s;
}

static int
cmd_write_trapezoid_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
                    int op,  const gs_fixed_edge *left, const gs_fixed_edge *right,
                    fixed ybot, fixed ytop, int options,
                    const gs_fill_attributes *fa,
                    const frac31 *c0, const frac31 *c1,
                    const frac31 *c2, const frac31 *c3)
{
    byte *dp;
    int rcsize;
    int code;
    int colors_mask = 0;

    rcsize = 1 + cmd_sizew(left->start.x) + cmd_sizew(left->start.y)
               + cmd_sizew(left->end.x) + cmd_sizew(left->end.y)
               + cmd_sizew(right->start.x) + cmd_sizew(right->start.y)
               + cmd_sizew(right->end.x) + cmd_sizew(right->end.y)
               + cmd_sizew(options);

    if (!(options & 4))
        rcsize += cmd_sizew(ybot) + cmd_sizew(ytop);
    if (options & 2) {
        rcsize += cmd_sizew(fa->clip->p.x) + cmd_sizew(fa->clip->p.y)
                + cmd_sizew(fa->clip->q.x) + cmd_sizew(fa->clip->q.y);
        if (c0 != NULL) {
            colors_mask += 1;
            rcsize += cmd_size_frac31_color(cldev, c0);
        }
        if (c1 != NULL) {
            colors_mask += 2;
            rcsize += cmd_size_frac31_color(cldev, c1);
        }
        if (c2 != NULL) {
            colors_mask += 4;
            rcsize += cmd_size_frac31_color(cldev, c2);
        }
        if (c3 != NULL) {
            colors_mask += 8;
            rcsize += cmd_size_frac31_color(cldev, c3);
        }
        rcsize += cmd_sizew(colors_mask);
    }
    code = set_cmd_put_op(dp, cldev, pcls, op, rcsize);
    if (code < 0)
        return code;
    dp++;
    cmd_putw(left->start.x, dp);
    cmd_putw(left->start.y, dp);
    cmd_putw(left->end.x, dp);
    cmd_putw(left->end.y, dp);
    cmd_putw(right->start.x, dp);
    cmd_putw(right->start.y, dp);
    cmd_putw(right->end.x, dp);
    cmd_putw(right->end.y, dp);
    cmd_putw(options, dp);
    if (!(options & 4)) {
        cmd_putw(ybot, dp);
        cmd_putw(ytop, dp);
    }
    if_debug6('L', "    t%d:%ld,%ld,%ld,%ld   %ld\n",
              rcsize - 1, left->start.x, left->start.y, left->end.x, left->end.y, ybot);
    if_debug6('L', "    t%ld,%ld,%ld,%ld   %ld   o=%d\n",
              right->start.x, right->start.y, right->end.x, right->end.y, ytop, options);
    if (options & 2) {
        cmd_putw(fa->clip->p.x, dp);
        cmd_putw(fa->clip->p.y, dp);
        cmd_putw(fa->clip->q.x, dp);
        cmd_putw(fa->clip->q.y, dp);
        cmd_putw(colors_mask, dp);
        if (c0 != NULL)
            dp = cmd_put_frac31_color(cldev, c0, dp);
        if (c1 != NULL)
            dp = cmd_put_frac31_color(cldev, c1, dp);
        if (c2 != NULL)
            dp = cmd_put_frac31_color(cldev, c2, dp);
        if (c3 != NULL)
            cmd_put_frac31_color(cldev, c3, dp);
    }
    return 0;
}

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

int
clist_fillpage(gx_device * dev, gs_imager_state *pis, gx_drawing_color *pdcolor)
{
    gx_device_clist_writer * const cdev = &((gx_device_clist *)dev)->writer;
    gx_clist_state * pcls = cdev->states; /* Use any. */
    int code;

    do {
        code = cmd_put_drawing_color(cdev, pcls, pdcolor, NULL, devn_not_tile);
        if (code >= 0)
            code = cmd_write_page_rect_cmd(cdev, cmd_op_fill_rect);
    } while (RECT_RECOVER(code));
    return code;
}

int
clist_fill_rectangle(gx_device * dev, int rx, int ry, int rwidth, int rheight,
                     gx_color_index color)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int code;
    cmd_rects_enum_t re;

    crop_fill(cdev, rx, ry, rwidth, rheight);
    if (rwidth <= 0 || rheight <= 0)
        return 0;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        RECT_STEP_INIT(re);
        re.pcls->colors_used.or |= color;
        re.pcls->band_complexity.uses_color |= ((color != 0xffffff) && (color != 0));
        do {
            code = cmd_disable_lop(cdev, re.pcls);
            if (code >= 0 && color != re.pcls->colors[1]) {
                code = cmd_put_color(cdev, re.pcls, &clist_select_color1,
                                     color, &re.pcls->colors[1]);
            }
            if (code >= 0) {
                code = cmd_write_rect_cmd(cdev, re.pcls, cmd_op_fill_rect, rx, re.y,
                                          rwidth, re.height);
            }
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        re.y += re.height;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
    } while (re.y < re.yend);
    return 0;
}

/* This is used in fills from devn color types */
int
clist_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect,
    const gs_imager_state *pis, const gx_drawing_color *pdcolor,
    const gx_clip_path *pcpath)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int code;
    int rx, ry, rwidth, rheight;
    cmd_rects_enum_t re;

    rx = rect->p.x;
    ry = rect->p.y;
    rwidth = rect->q.x - rx;
    rheight = rect->q.y - ry;

    crop_fill(cdev, rx, ry, rwidth, rheight);
    if (rwidth <= 0 || rheight <= 0)
        return 0;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        RECT_STEP_INIT(re);
        re.pcls->colors_used.or = 0xffffffff;
        re.pcls->band_complexity.uses_color = true;
        do {
            code = cmd_disable_lop(cdev, re.pcls);
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, 
                                         devn_not_tile);
            if (code >= 0) {
                code = cmd_write_rect_hl_cmd(cdev, re.pcls, cmd_op_fill_rect_hl,
                                             rx, re.y, rwidth, re.height, false);
            }
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        re.y += re.height;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
    } while (re.y < re.yend);
    return 0;
}

static inline int
clist_write_fill_trapezoid(gx_device * dev,
    const gs_fixed_edge *left, const gs_fixed_edge *right,
    fixed ybot, fixed ytop, int options,
    const gx_drawing_color *pdcolor, gs_logical_operation_t lop,
    const gs_fill_attributes *fa,
    const frac31 *c0, const frac31 *c1,
    const frac31 *c2, const frac31 *c3)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int code;
    cmd_rects_enum_t re;
    int ry, rheight;
    bool swap_axes = (options & 1);

    if (options & 4) {
        if (swap_axes) {
            ry = fixed2int(max(min(min(left->start.x, left->end.x), right->start.x), fa->clip->p.x));
            rheight = fixed2int_ceiling(min(max(max(left->start.x, left->end.x), right->start.x), fa->clip->q.x)) - ry;
        } else {
            ry = fixed2int(max(min(min(left->start.y, left->end.y), right->start.y), fa->clip->p.y));
            rheight = fixed2int_ceiling(min(max(max(left->start.y, left->end.y), right->start.y), fa->clip->q.y)) - ry;
        }
    } else {
        /* fixme: this may give a significant overestimation,
           so the command will be written to many bands.
           Would like to know a better range by X
           with computing intersections of sides with ybot, ytop. */
        if (swap_axes) {
            ry = fixed2int(min(left->start.x, left->end.x));
            rheight = fixed2int_ceiling(max(right->start.x, right->end.x)) - ry;
        } else {
            ry = fixed2int(ybot);
            rheight = fixed2int_ceiling(ytop) - ry;
        }
    }
    crop_fill_y(cdev, ry, rheight);
    if (rheight <= 0)
        return 0;
    if (cdev->permanent_error < 0)
        return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        RECT_STEP_INIT(re);
        do {
            if (pdcolor != NULL) {
                code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, 
                                             devn_not_tile);
                if (code == gs_error_unregistered)
                    return code;
                if (code < 0) {
                    /* Something went wrong, use the default implementation. */
                    return gx_default_fill_trapezoid(dev, left, right, ybot, ytop, swap_axes, pdcolor, lop);
                }
                code = cmd_update_lop(cdev, re.pcls, lop);
            } else
                code = 0;
            if (code >= 0) {
                /* Dont't want to shorten the trapezoid by the band boundary,
                   keeping in mind a further optimization with writing same data to all bands. */
                code = cmd_write_trapezoid_cmd(cdev, re.pcls, cmd_opv_fill_trapezoid, left, right,
                                          ybot, ytop, options, fa, c0, c1, c2, c3);
            }
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        re.y += re.height;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
    } while (re.y < re.yend);
    return 0;
}

int
clist_fill_trapezoid(gx_device * dev,
    const gs_fixed_edge *left, const gs_fixed_edge *right,
    fixed ybot, fixed ytop, bool swap_axes,
    const gx_drawing_color *pdcolor, gs_logical_operation_t lop)
{
    return clist_write_fill_trapezoid(dev, left, right,
        ybot, ytop, swap_axes, pdcolor, lop, NULL, NULL, NULL, NULL, NULL);
}

int
clist_fill_linear_color_trapezoid(gx_device * dev, const gs_fill_attributes *fa,
        const gs_fixed_point *p0, const gs_fixed_point *p1,
        const gs_fixed_point *p2, const gs_fixed_point *p3,
        const frac31 *c0, const frac31 *c1,
        const frac31 *c2, const frac31 *c3)
{
    gs_fixed_edge left, right;
    int code;

    left.start = *p0;
    left.end = *p1;
    right.start = *p2;
    right.end = *p3;
    code = clist_write_fill_trapezoid(dev, &left, &right,
        fa->ystart, fa->yend, fa->swap_axes | 2, NULL, fa->lop, fa, c0, c1, c2, c3);
    if (code < 0)
        return code;
    /* NOTE : The return value 0 for the fill_linear_color_trapezoid method means
       that the device requests a further decomposition of the trapezoid.
       Currently we have no interface for checking whether the target device
       can handle a trapezoid without an attempt to fill it.
       Therefore the clist writer device must always return 1,
       and the clist reader must perform a further decomposition
       if the target device requests it.
     */
    return 1;
}

int
clist_fill_linear_color_triangle(gx_device * dev, const gs_fill_attributes *fa,
        const gs_fixed_point *p0, const gs_fixed_point *p1,
        const gs_fixed_point *p2,
        const frac31 *c0, const frac31 *c1, const frac31 *c2)
{
    gs_fixed_edge left, right;
    int code;

    left.start = *p0;
    left.end = *p1;
    right.start = *p2;
    right.end.x = right.end.y = 0; /* unused. */

    code = clist_write_fill_trapezoid(dev, &left, &right,
        0, 0, fa->swap_axes | 2 | 4, NULL, fa->lop, fa, c0, c1, c2, NULL);
    if (code < 0)
        return code;
    /* NOTE : The return value 0 for the fill_linear_color_triangle method means
       that the device requests a further decomposition of the trapezoid.
       Currently we have no interface for checking whether the target device
       can handle a trapezoid without an attempt to fill it.
       Therefore the clist writer device must always return 1,
       and the clist reader must perform a further decomposition
       if the target device requests it.
     */
    return 1;
}

int
clist_dev_spec_op(gx_device *pdev, int dev_spec_op, void *data, int size)
{
    gs_pattern1_instance_t *pinst = (gs_pattern1_instance_t *)data;
    gx_bitmap_id id = (gx_bitmap_id)size;
    gx_device_clist_common * const cdev = &((gx_device_clist *)pdev)->common;

    if (dev_spec_op == gxdso_pattern_handles_clip_path)
        return 1;
    if (dev_spec_op == gxdso_pattern_shfill_doesnt_need_path)
        return 1;
    if (dev_spec_op == gxdso_is_native_planar)
        return cdev->is_planar;
    if (dev_spec_op == gxdso_supports_devn) {
        cmm_dev_profile_t *dev_profile;
        int code;
        code = dev_proc(cdev, get_profile)((gx_device*) cdev, &dev_profile);
        if (code == 0) {
            return dev_profile->supports_devn;
        } else {
            return 0;
        }
    }
    return gx_default_dev_spec_op(pdev, dev_spec_op, pinst, id);
}

#define dev_proc_pattern_manage(proc)\
  dev_t_proc_pattern_manage(proc, gx_device)

/* Based heavily off of clist_strip_tile_rectangle */
int
clist_strip_tile_rect_devn(gx_device * dev, const gx_strip_bitmap * tile,
                           int rx, int ry, int rwidth, int rheight,
                           const gx_drawing_color *pdcolor0, 
                           const gx_drawing_color *pdcolor1, int px, int py)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int depth = 1;
    int code;
    cmd_rects_enum_t re;

    crop_fill(cdev, rx, ry, rwidth, rheight);
    if (rwidth <= 0 || rheight <= 0)
        return 0;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        ulong offset_temp;

        RECT_STEP_INIT(re);
        re.pcls->colors_used.or = 0xffffffff;
        re.pcls->band_complexity.uses_color = true;
        do {
            code = cmd_disable_lop(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        /* Change the tile if needed */
        if (!cls_has_tile_id(cdev, re.pcls, tile->id, offset_temp)) {
            if (tile->id != gx_no_bitmap_id) {
                do {
                    code = clist_change_tile(cdev, re.pcls, tile, depth);
                } while (RECT_RECOVER(code));
                if (code < 0 && !(code != gs_error_VMerror || !cdev->error_is_retryable) && SET_BAND_CODE(code))
                    goto error_in_rect;
            } else
                code = -1; 
            if (code < 0) {
                return_error(gs_error_unregistered); 
            }
        }
        do {
            code = 0;
            /* Write out the devn colors */
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor0, &re, 
                                         devn_tile0);
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor1, &re, 
                                         devn_tile1);
            /* Set the tile phase */
            if (px != re.pcls->tile_phase.x || py != re.pcls->tile_phase.y) {
                if (code >= 0)
                    code = cmd_set_tile_phase(cdev, re.pcls, px, py);
            }
            /* Write out the actually command to fill with the devn colors */
            if (code >= 0) {
                code = cmd_write_rect_hl_cmd(cdev, re.pcls, 
                                             cmd_opv_ext_tile_rect_hl, rx, re.y,
                                             rwidth, re.height, true);
            }
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        re.y += re.height;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
    } while (re.y < re.yend);
    return 0;
}

int
clist_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tile,
                           int rx, int ry, int rwidth, int rheight,
               gx_color_index color0, gx_color_index color1, int px, int py)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int depth =
        (color1 == gx_no_color_index && color0 == gx_no_color_index ?
         cdev->clist_color_info.depth : 1);
    gx_color_index colors_used =
        (color1 == gx_no_color_index && color0 == gx_no_color_index ?
         /* We can't know what colors will be used: assume the worst. */
         ((gx_color_index)1 << depth) - 1 :
         (color0 == gx_no_color_index ? 0 : color0) |
         (color1 == gx_no_color_index ? 0 : color1));
    int code;
    cmd_rects_enum_t re;

    crop_fill(cdev, rx, ry, rwidth, rheight);
    if (rwidth <= 0 || rheight <= 0)
        return 0;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        ulong offset_temp;

        RECT_STEP_INIT(re);
        re.pcls->colors_used.or |= colors_used;
        re.pcls->band_complexity.uses_color |=
            ((color0 != gx_no_color_index) && (color0 != 0xffffff) && (color0 != 0)) ||
            ((color1 != gx_no_color_index) && (color1 != 0xffffff) && (color1 != 0));

        do {
            code = cmd_disable_lop(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        if (!cls_has_tile_id(cdev, re.pcls, tile->id, offset_temp)) {
            if (tile->id != gx_no_bitmap_id) {
                do {
                    code = clist_change_tile(cdev, re.pcls, tile, depth);
                } while (RECT_RECOVER(code));
                if (code < 0 && !(code != gs_error_VMerror || !cdev->error_is_retryable) && SET_BAND_CODE(code))
                    goto error_in_rect;
            } else
                code = -1; /* Force the default implementation. Should never happen. */
            if (code < 0) {
                /* ok if gx_default... does retries internally: */
                /* it's self-sufficient */
                code = gx_default_strip_tile_rectangle(dev, tile,
                                                       rx, re.y, rwidth, re.height,
                                                       color0, color1,
                                                       px, py);
                if (code < 0 && SET_BAND_CODE(code))
                    goto error_in_rect;
                goto endr;
            }
        }
        do {
            code = 0;
            if (color0 != re.pcls->tile_colors[0] || color1 != re.pcls->tile_colors[1])
                code = cmd_set_tile_colors(cdev, re.pcls, color0, color1);
            if (px != re.pcls->tile_phase.x || py != re.pcls->tile_phase.y) {
                if (code >= 0)
                    code = cmd_set_tile_phase(cdev, re.pcls, px, py);
            }
            if (code >= 0)
                code = cmd_write_rect_cmd(cdev, re.pcls, cmd_op_tile_rect, rx, re.y,
                                          rwidth, re.height);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
endr:;
        re.y += re.height;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
    } while (re.y < re.yend);
    return 0;
}

int
clist_copy_mono(gx_device * dev,
                const byte * data, int data_x, int raster, gx_bitmap_id id,
                int rx, int ry, int rwidth, int rheight,
                gx_color_index color0, gx_color_index color1)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int y0;
    gx_bitmap_id orig_id = id;
    gx_color_index colors_used =
        (color0 == gx_no_color_index ? 0 : color0) |
        (color1 == gx_no_color_index ? 0 : color1);
    bool uses_color =
        ((color0 != gx_no_color_index) && (color0 != 0xffffff) && (color0 != 0)) ||
        ((color1 != gx_no_color_index) && (color1 != 0xffffff) && (color1 != 0));
    cmd_rects_enum_t re;

    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
    y0 = ry;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        int dx = data_x & 7;
        int w1 = dx + rwidth;
        const byte *row = data + (re.y - y0) * raster + (data_x >> 3);
        int code;

        RECT_STEP_INIT(re);
        re.pcls->colors_used.or |= colors_used;
        re.pcls->band_complexity.uses_color |= uses_color;
        do {
            code = cmd_disable_lop(cdev, re.pcls);
            if (code >= 0)
                code = cmd_disable_clip(cdev, re.pcls);
            if (color0 != re.pcls->colors[0] && code >= 0)
                code = cmd_set_color0(cdev, re.pcls, color0);
            if (color1 != re.pcls->colors[1] && code >= 0)
                code = cmd_set_color1(cdev, re.pcls, color1);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        /* 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_planes;
        byte *dp;
        uint csize;
        uint compress;
        int code;

        rect.x = rx, rect.y = re.y;
        rect.width = w1, rect.height = re.height;
        rsize = (dx ? 3 : 1) + cmd_size_rect(&rect) + cmd_sizew(0); /* planar_height 0 */
        do {
            code = cmd_put_bits(cdev, re.pcls, row, w1, re.height, raster,
                                rsize, (orig_id == gx_no_bitmap_id ?
                                        1 << cmd_compress_rle :
                                        cmd_mask_compress_any),
                                &dp, &csize);
        } while (RECT_RECOVER(code));
        if (code < 0 && !(code == gs_error_limitcheck) && SET_BAND_CODE(code))
            goto error_in_rect;
        compress = (uint)code;
        if (code < 0) {
            /* The bitmap was too large; split up the transfer. */
            if (re.height > 1) {
                /*
                 * Split the transfer by reducing the height.
                 * See the comment above FOR_RECTS in gxcldev.h.
                 */
                re.height >>= 1;
                goto copy;
            } else {
                /* Split a single (very long) row. */
                int w2 = w1 >> 1;

                ++cdev->driver_call_nesting;
                {
                    code = clist_copy_mono(dev, row, dx,
                                           raster, gx_no_bitmap_id, rx, re.y,
                                           w2, 1, color0, color1);
                    if (code >= 0)
                        code = clist_copy_mono(dev, row, dx + w2,
                                               raster, gx_no_bitmap_id,
                                               rx + w2, re.y,
                                               w1 - w2, 1, color0, color1);
                }
                --cdev->driver_call_nesting;
                if (code < 0 && SET_BAND_CODE(code))
                    goto error_in_rect;
                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);
        /* Store the plane_height */
        cmd_putw(0, dp);
        cmd_put2w(rx, re.y, dp);
        cmd_put2w(w1, re.height, dp);
        re.pcls->rect = rect;
        }
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
        re.y -= re.height;
    } while ((re.y += re.height) < re.yend);
    return 0;
}

/* The code duplication between this and clist_copy_mono needs to be removed */
int
clist_copy_planes(gx_device * dev,
                  const byte * data, int data_x, int raster, gx_bitmap_id id,
                  int rx, int ry, int rwidth, int rheight, int plane_height)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int y0;
    gx_bitmap_id orig_id = id;
    cmd_rects_enum_t re;
    int bpc = dev->color_info.depth / dev->color_info.num_components;

    if (rwidth <= 0 || rheight <= 0)
        return 0;

    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);

    y0 = ry;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        int dx = data_x % (8/bpc);
        int w1 = dx + rwidth;
        const byte *row = data + (re.y - y0) * raster + (data_x / (8/bpc));
        int code;


        RECT_STEP_INIT(re);
        do {
            code = cmd_disable_lop(cdev, re.pcls);
            if (code >= 0)
                code = cmd_disable_clip(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        /* Don't bother to check for a possible cache hit: */
        /* tile_rectangle and fill_mask handle those cases. */
copy:
        /* We require that in the copy_planes case all the planes fit into
         * a single cbuf. */
        if (plane_height > 0) {
            int bytes_row = ((w1*bpc+7)/8 + 7) & ~7;
            int maxheight = (cbuf_size - 0x100) / bytes_row / cdev->color_info.num_components;
            if (re.height > maxheight)
                re.height = maxheight;
            if (re.height == 0)
            {
                /* Split a single (very long) row. */
                int w2 = w1 >> 1;

		re.height = 1;
                ++cdev->driver_call_nesting;
                {
                    code = clist_copy_planes(dev, row, dx, raster,
                                             gx_no_bitmap_id, rx, re.y,
                                             w2, 1, plane_height);
                    if (code >= 0)
                        code = clist_copy_planes(dev, row, dx + w2,
                                                 raster, gx_no_bitmap_id,
                                                 rx + w2, re.y,
                                                 w1 - w2, 1, plane_height);
                }
                --cdev->driver_call_nesting;
                if (code < 0 && SET_BAND_CODE(code))
                    goto error_in_rect;
                continue;
            }
        }
	{
        gx_cmd_rect rect;
        int rsize;
        byte op = (byte) cmd_op_copy_mono_planes;
        byte *dp;
        uint csize;
        uint compress;
        int code;

        rect.x = rx, rect.y = re.y;
        rect.width = w1, rect.height = re.height;
        rsize = (dx ? 3 : 1) + cmd_size_rect(&rect) + cmd_sizew(plane_height);
        do {
            int plane;
            /* Copy the 0th plane - this is the one the op goes in. */
            code = cmd_put_bits(cdev, re.pcls, row, w1*bpc, re.height, raster,
                                rsize, (bpc == 1 ? (orig_id == gx_no_bitmap_id ?
                                                    1 << cmd_compress_rle :
                                                    cmd_mask_compress_any) : 0),
                                &dp, &csize);
            compress = (uint)code;
            if (plane_height > 0) {
                for (plane = 1; plane < cdev->color_info.num_components && (code >= 0); plane++)
                {
                    byte *dummy_dp;
                    uint csize2;
                    /* Copy subsequent planes - 1 byte header used to send the
                     * compression type. */
                    code = cmd_put_bits(cdev, re.pcls,
                                        row + plane_height * raster,
                                        w1*bpc, re.height, raster, 1,
                                        (bpc == 1 ?
                                         (orig_id == gx_no_bitmap_id ?
                                          1 << cmd_compress_rle :
                                          cmd_mask_compress_any) : 0),
                                        &dummy_dp, &csize2);
                    if (code >= 0)
                        *dummy_dp = code;

                    csize += csize2;
                }
            }
        } while (RECT_RECOVER(code));
        if (code < 0 && !(code == gs_error_limitcheck) && SET_BAND_CODE(code))
            goto error_in_rect;
        if (code < 0) {
            /* The bitmap was too large; split up the transfer. */
            if (re.height > 1) {
                /*
                 * Split the transfer by reducing the height.
                 * See the comment above FOR_RECTS in gxcldev.h.
                 */
                re.height >>= 1;
                goto copy;
            } else {
                /* Split a single (very long) row. */
                int w2 = w1 >> 1;

                ++cdev->driver_call_nesting;
                {
                    code = clist_copy_planes(dev, row, dx, raster,
                                             gx_no_bitmap_id, rx, re.y,
                                             w2, 1, plane_height);
                    if (code >= 0)
                        code = clist_copy_planes(dev, row, dx + w2,
                                                 raster, gx_no_bitmap_id,
                                                 rx + w2, re.y,
                                                 w1 - w2, 1, plane_height);
                }
                --cdev->driver_call_nesting;
                if (code < 0 && SET_BAND_CODE(code))
                    goto error_in_rect;
                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_putw(plane_height, dp);
        cmd_put2w(rx, re.y, dp);
        cmd_put2w(w1, re.height, dp);

        re.pcls->rect = rect;
        }
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
        re.y -= re.height;
    } while ((re.y += re.height) < re.yend);
    return 0;
}

int
clist_copy_color(gx_device * dev,
                 const byte * data, int data_x, int raster, gx_bitmap_id id,
                 int rx, int ry, int rwidth, int rheight)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    int depth = cdev->clist_color_info.depth; /* Could be different than target
                                                 if 1.4 device */
    int y0;
    int data_x_bit;
    /* We can't know what colors will be used: assume the worst. */
    gx_color_index colors_used = ((gx_color_index)1 << depth) - 1;
    cmd_rects_enum_t re;

    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
    y0 = ry;
    data_x_bit = data_x * depth;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        int dx = (data_x_bit & 7) / depth;
        int w1 = dx + rwidth;
        const byte *row = data + (re.y - y0) * raster + (data_x_bit >> 3);
        int code;

        RECT_STEP_INIT(re);
        re.pcls->colors_used.or |= colors_used;
        re.pcls->band_complexity.uses_color = 1;

        do {
            code = cmd_disable_lop(cdev, re.pcls);
            if (code >= 0)
                code = cmd_disable_clip(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        if (re.pcls->color_is_alpha) {
            byte *dp;

            do {
                code =
                    set_cmd_put_op(dp, cdev, re.pcls, cmd_opv_set_copy_color, 1);
            } while (RECT_RECOVER(code));
            if (code < 0 && SET_BAND_CODE(code))
                goto error_in_rect;
            re.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 = rx, rect.y = re.y;
            rect.width = w1, rect.height = re.height;
            rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
            do {
                code = cmd_put_bits(cdev, re.pcls, row, w1 * depth,
                                    re.height, raster, rsize,
                                    1 << cmd_compress_rle, &dp, &csize);
            } while (RECT_RECOVER(code));
            if (code < 0 && !(code == gs_error_limitcheck) && SET_BAND_CODE(code))
                goto error_in_rect;
            compress = (uint)code;
            if (code < 0) {
                /* The bitmap was too large; split up the transfer. */
                if (re.height > 1) {
                    /* Split the transfer by reducing the height.
                     * See the comment above FOR_RECTS in gxcldev.h.
                     */
                    re.height >>= 1;
                    goto copy;
                } else {
                    /* Split a single (very long) row. */
                    int w2 = w1 >> 1;

                    ++cdev->driver_call_nesting;
                    {
                        code = clist_copy_color(dev, row, dx,
                                                raster, gx_no_bitmap_id,
                                                rx, re.y, w2, 1);
                        if (code >= 0)
                            code = clist_copy_color(dev, row, dx + w2,
                                                    raster, gx_no_bitmap_id,
                                                    rx + w2, re.y, w1 - w2, 1);
                    }
                    --cdev->driver_call_nesting;
                    if (code < 0 && SET_BAND_CODE(code))
                        goto error_in_rect;
                    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(rx, re.y, dp);
            cmd_put2w(w1, re.height, dp);
            re.pcls->rect = rect;
        }
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
        re.y -= re.height;
    } while ((re.y += re.height) < re.yend);
    return 0;
}

int
clist_copy_alpha(gx_device * dev, const byte * data, int data_x,
           int raster, gx_bitmap_id id, int rx, int ry, int rwidth, int rheight,
                 gx_color_index color, int depth)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    /* 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 = ilog2(depth);
    int y0;
    int data_x_bit;
    cmd_rects_enum_t re;

    /* If the target can't perform copy_alpha, exit now */
    if (depth > 1 && (cdev->disable_mask & clist_disable_copy_alpha) != 0)
        return_error(gs_error_unknownerror);

    fit_copy(dev, data, data_x, raster, id, rx, ry, rwidth, rheight);
    y0 = ry;
    data_x_bit = data_x << log2_depth;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        int dx = (data_x_bit & 7) >> log2_depth;
        int w1 = dx + rwidth;
        const byte *row = data + (re.y - y0) * raster + (data_x_bit >> 3);
        int code;

        RECT_STEP_INIT(re);
        re.pcls->colors_used.or |= color;
        do {
            code = cmd_disable_lop(cdev, re.pcls);
            if (code >= 0)
                code = cmd_disable_clip(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        if (!re.pcls->color_is_alpha) {
            byte *dp;

            do {
                code =
                    set_cmd_put_op(dp, cdev, re.pcls, cmd_opv_set_copy_alpha, 1);
            } while (RECT_RECOVER(code));
            if (code < 0 && SET_BAND_CODE(code))
                goto error_in_rect;
            re.pcls->color_is_alpha = 1;
        }
        if (color != re.pcls->colors[1]) {
            do {
                code = cmd_set_color1(cdev, re.pcls, color);
            } while (RECT_RECOVER(code));
            if (code < 0 && SET_BAND_CODE(code))
                goto error_in_rect;
        }
copy:{
            gx_cmd_rect rect;
            int rsize;
            byte op = (byte) cmd_op_copy_color_alpha;
            byte *dp;
            uint csize;
            uint compress;

            rect.x = rx, rect.y = re.y;
            rect.width = w1, rect.height = re.height;
            rsize = (dx ? 4 : 2) + cmd_size_rect(&rect);
            do {
                code = cmd_put_bits(cdev, re.pcls, row, w1 << log2_depth,
                                    re.height, raster, rsize,
                                    1 << cmd_compress_rle, &dp, &csize);
            } while (RECT_RECOVER(code));
            if (code < 0 && !(code == gs_error_limitcheck) && SET_BAND_CODE(code))
                goto error_in_rect;
            compress = (uint)code;
            if (code < 0) {
                /* The bitmap was too large; split up the transfer. */
                if (re.height > 1) {
                    /* Split the transfer by reducing the height.
                     * See the comment above FOR_RECTS in gxcldev.h.
                     */
                    re.height >>= 1;
                    goto copy;
                } else {
                    /* Split a single (very long) row. */
                    int w2 = w1 >> 1;

                    ++cdev->driver_call_nesting;
                    {
                        code = clist_copy_alpha(dev, row, dx,
                                                raster, gx_no_bitmap_id, rx, re.y,
                                                w2, 1, color, depth);
                        if (code >= 0)
                            code = clist_copy_alpha(dev, row, dx + w2,
                                                    raster, gx_no_bitmap_id,
                                                    rx + w2, re.y, w1 - w2, 1,
                                                    color, depth);
                    }
                    --cdev->driver_call_nesting;
                    if (code < 0 && SET_BAND_CODE(code))
                        goto error_in_rect;
                    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(rx, re.y, dp);
            cmd_put2w(w1, re.height, dp);
            re.pcls->rect = rect;
        }
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
        re.y -= re.height;
    } while ((re.y += re.height) < re.yend);
    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 rx, int ry, int rwidth, int rheight,
                     int phase_x, int phase_y, gs_logical_operation_t lop)
{
    return clist_strip_copy_rop2(dev, sdata, sourcex, sraster, id,
                                 scolors, textures, tcolors,
                                 rx, ry, rwidth, rheight, phase_x, phase_y,
                                 lop, 0);
}

int
clist_strip_copy_rop2(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 rx, int ry, int rwidth, int rheight,
                     int phase_x, int phase_y, gs_logical_operation_t lop,
                     uint planar_height)
{
    gx_device_clist_writer * const cdev =
        &((gx_device_clist *)dev)->writer;
    gs_rop3_t rop = lop_rop(lop);
    gx_strip_bitmap tile_with_id;
    const gx_strip_bitmap *tiles = textures;
    int y0;
    /* Compute the set of possible colors that this operation can generate. */
    gx_color_index all = ((gx_color_index)1 << cdev->clist_color_info.depth) - 1;
    bool subtractive = dev->color_info.num_components >= 4; /****** HACK ******/
    gx_color_index S =
        (scolors ? scolors[0] | scolors[1] : sdata ? all : 0);
    gx_color_index T =
        (tcolors ? tcolors[0] | tcolors[1] : textures ? all : 0);
    gs_rop3_t color_rop =
        (subtractive ? byte_reverse_bits[rop ^ 0xff] : rop);
    bool slow_rop;
    cmd_rects_enum_t re;

    if (scolors != 0 && scolors[0] != scolors[1]) {
        crop_fill(cdev, rx, ry, rwidth, rheight);
    } else {
        crop_copy(cdev, sdata, sourcex, sraster, id, rx, ry, rwidth, rheight);
    }
    if (rwidth <= 0 || rheight <= 0)
        return 0;
    /*
     * On CMYK devices, RasterOps must be executed with complete pixels
     * if the operation involves the destination.
     * This is because the black plane interacts with the other planes
     * in the conversion between RGB and CMYK.  Check for this now.
     */
    {
        gs_rop3_t rop_used = rop;

        if (scolors && (scolors[0] == scolors[1]))
            rop_used = (scolors[0] == gx_device_black(dev) ?
                        rop3_know_S_0(rop_used) :
                        scolors[0] == gx_device_white(dev) ?
                        rop3_know_S_1(rop_used) : rop_used);
        if (tcolors && (tcolors[0] == tcolors[1]))
            rop_used = (tcolors[0] == gx_device_black(dev) ?
                        rop3_know_T_0(rop_used) :
                        tcolors[0] == gx_device_white(dev) ?
                        rop3_know_T_1(rop_used) : rop_used);
        slow_rop = !(rop == rop3_0 || rop == rop3_1 ||
                     rop == rop3_S || rop == rop3_T);
    }
    y0 = ry;
    if (cdev->permanent_error < 0)
      return (cdev->permanent_error);
    RECT_ENUM_INIT(re, ry, rheight);
    do {
        const byte *row = sdata + (re.y - y0) * sraster;
        gx_color_index D;
        int code;

        RECT_STEP_INIT(re);
        D = re.pcls->colors_used.or;
        /* Reducing D, S, T to rop_operand (which apparently is 32 bit) appears safe
           due to 'all' a has smaller snumber of significant bits. */
        re.pcls->colors_used.or =
            ((rop_proc_table[color_rop])((rop_operand)D, (rop_operand)S, (rop_operand)T) & all) | D;
        re.pcls->colors_used.slow_rop |= slow_rop;
        re.pcls->band_complexity.nontrivial_rops |= slow_rop;
        if (rop3_uses_T(rop)) {
            if (tcolors == 0 || tcolors[0] != tcolors[1]) {
                ulong offset_temp;

                if (!cls_has_tile_id(cdev, re.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(dev->memory, 1);
                        tiles = &tile_with_id;
                    }
                    do {
                        code = clist_change_tile(cdev, re.pcls, tiles,
                                                 (tcolors != 0 ? 1 :
                                                  cdev->clist_color_info.depth));
                    } while (RECT_RECOVER(code));
                    if (code < 0 && !(code == gs_error_limitcheck) && SET_BAND_CODE(code))
                        goto error_in_rect;
                    if (code < 0) {
                        /*
                         * The error is a limitcheck: 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 data_shift = 0, phase_shift = 0, raster;
                        int new_phase = phase_x;
                        int tile_space_phase;
                        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(dev->memory, min(re.height, rep_height));
                        line_tile = *tiles;
                        line_tile.size.y = 1;
                        line_tile.rep_height = 1;
                        raster = line_tile.raster;
                        if (line_tile.num_planes > 0)
                            line_tile.raster *= tiles->size.y;
                        /* The rasterizer assumes tile phase relatively to the rectangle origin,
                           (see x_offset in gdevmr8n.c), so compute "the tile phase in the tile space"
                           with same expression : */
                        tile_space_phase = (rx + phase_x) % tiles->rep_width;
                        if (tile_space_phase + rwidth <= tiles->rep_width) {
                            /* Narrow the tile to prevent buffer overflow - bug 689588.
                               Note currently we don't narrow "wrapped" tiles (because bug 689588 doesn't need) :
                               when tile_space_phase < rep_width && tile_space_phase + rwidth > rep_width,
                               each line to be converted into 2 ones.
                            */
                            int depth = cdev->clist_color_info.depth;

                            if (line_tile.num_planes > 0)
                                depth /= line_tile.num_planes;
#			    if 0
                            /* Align bitmap data : */
                            data_shift = ((tile_space_phase * depth) >> (log2_align_bitmap_mod + 3)) << log2_align_bitmap_mod;
#			    else
                            data_shift = tile_space_phase * depth / 8; /* No bitmap data alignment because we'll only write it to clist.  */
#			    endif
                            phase_shift = data_shift * 8 / depth;
                            line_tile.rep_width = rwidth + (tile_space_phase - phase_shift);
                            /* Normally line_tile.raster must account trailing row alignment bytes and
                               to be computed as bitmap_raster(line_tile.rep_width * depth);
                               but we can't apply it here because the trailing alignment bytes may be absent
                               due to data_shift. We believe it is not harmful because we just write the data to clist,
                               and because the bitmap height is 1.
                               The clist reader must provide the trailing bytes if the rasterizer needs them.
                             */
                            if (line_tile.num_planes <= 1)
                                line_tile.raster = (line_tile.rep_width * depth + 7) / 8;
                            line_tile.size.x = line_tile.rep_width;
                            line_tile.shift = 0;
                            new_phase = (tile_space_phase - phase_shift - rx % line_tile.rep_width);
                            /* Provide a positive phase for clist reader : */
                            new_phase = (new_phase + line_tile.rep_width) % line_tile.rep_width;
                        }
                        for (iy = 0; iy < re.height; ++iy) {
                            line_tile.data = tiles->data + raster *
                                ((re.y + iy + phase_y) % rep_height) + data_shift;
                            line_tile.id = ids + (iy % rep_height);
                            ++cdev->driver_call_nesting;
                            {
                                code = clist_strip_copy_rop2(dev,
                                        (sdata == 0 ? 0 : row + iy * sraster),
                                        sourcex, sraster,
                                        gx_no_bitmap_id, scolors,
                                        &line_tile, tcolors,
                                        rx, re.y + iy, rwidth, 1,
                                        new_phase, 0, lop, planar_height);
                            }
                            --cdev->driver_call_nesting;
                            if (code < 0 && SET_BAND_CODE(code))
                                goto error_in_rect;
                        }
                        continue;
                    }
                    if (((phase_x != re.pcls->tile_phase.x) && (tiles->rep_width > 1)) ||
                        ((phase_y != re.pcls->tile_phase.y) && (tiles->rep_height > 1))) {
                        do {
                            code = cmd_set_tile_phase(cdev, re.pcls, phase_x,
                                                      phase_y);
                        } while (RECT_RECOVER(code));
                        if (code < 0 && SET_BAND_CODE(code))
                            goto error_in_rect;
                    }
                }
            }
            /* Set the tile colors. */
            do {
                code =
                    (tcolors != 0 ?
                     cmd_set_tile_colors(cdev, re.pcls, tcolors[0], tcolors[1]) :
                     cmd_set_tile_colors(cdev, re.pcls, gx_no_color_index,
                                         gx_no_color_index));
            } while (RECT_RECOVER(code));
            if (code < 0 && SET_BAND_CODE(code))
                goto error_in_rect;
        }
        do {
            code = 0;
            if (lop != re.pcls->lop)
                code = cmd_set_lop(cdev, re.pcls, lop);
            if (code >= 0)
                code = cmd_enable_lop(cdev, re.pcls);
        } while (RECT_RECOVER(code));
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        /* Set lop_enabled to -1 so that fill_rectangle / copy_* */
        /* won't attempt to set it to 0. */
        re.pcls->lop_enabled = -1;
        ++cdev->driver_call_nesting;
        {
            if (scolors != NULL) {
                if (scolors[0] == scolors[1])
                    code = clist_fill_rectangle(dev, rx, re.y, rwidth, re.height,
                                                scolors[1]);
                else
                    code = clist_copy_mono(dev, row, sourcex, sraster, id,
                                           rx, re.y, rwidth, re.height,
                                           scolors[0], scolors[1]);
            } else if (planar_height) {
                code = clist_copy_planes(dev, row, sourcex, sraster, id,
                                         rx, re.y, rwidth, re.height,
                                         planar_height);
            } else {
                code = clist_copy_color(dev, row, sourcex, sraster, id,
                                        rx, re.y, rwidth, re.height);
            }
        }
        --cdev->driver_call_nesting;
        re.pcls->lop_enabled = 1;
        if (code < 0 && SET_BAND_CODE(code))
            goto error_in_rect;
        continue;
error_in_rect:
        if (!(cdev->error_is_retryable && cdev->driver_call_nesting == 0 &&
                SET_BAND_CODE(clist_VMerror_recover_flush(cdev, re.band_code)) >= 0))
            return re.band_code;
        re.y -= re.height;
    } while ((re.y += re.height) < re.yend);
    return 0;
}