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
|
/* 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_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);
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;
}
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);
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;
if (dev_spec_op == gxdso_pattern_handles_clip_path)
return 1;
if (dev_spec_op == gxdso_pattern_shfill_doesnt_need_path)
return 1;
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)
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;
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);
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);
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)
{
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;
/* 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 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.
*/
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_rop(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);
}
--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 != 0) {
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
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;
}
|