summaryrefslogtreecommitdiff
path: root/devices/gdevcdj.c
blob: 8515ddebe9805fccb16186ad361370561ba6eafb (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
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
/* Copyright (C) 2001-2023 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 the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/* HP and Canon colour printer drivers */

/*
 * HP DeskJet 505J Support (dj505j.dev)
 *   -- taken from dj505j driver for Ghostscript 2.6.1 by Kazunori Asayama
 * NEC PICTY180 (PC-PR101J/180) Support (picty180.dev)
 * HP LaserJet 4V/4LJ Pro dither Suport (lj4dithp.dev)
 *
 *     Norihito Ohmori <ohmori@p.chiba-u.ac.jp>
 *     April 15, 1999
 */

/****************************************************************
 * The code in this file was contributed by the authors whose names and/or
 * e-mail addresses appear below: Aladdin Enterprises takes no
 * responsibility for it.  In the past, we have tried to keep it working,
 * but too many users have made too many "improvements" without regard to
 * the overall structure; the last three "improvements" required me to spend
 * several hours fixing each one so that the code worked again at all.  For
 * this reason, no further changes to this file will be accepted.  We are
 * planning eventually to get these drivers rewritten from scratch.
 *
 *				L. Peter Deutsch
 *				Aladdin Enterprises
 *				February 28, 1996
 ****************************************************************/

/*
 * Change history:
 *	2000-08-20 Jonathan Kamens <jik@kamens.brookline.ma.us>:
 *	  change to support printers with different X and Y resolution.
 */

/*
 * Important compilation notes (YA).
 *
 * You may also try the cdj550cmyk driver after having defined
 * USE_CDJ550_CMYK and added the needed definition in devs.mak. Not tried!
 * (I have a BJC!) Also note that modes descriptions of CMYK printing
 * is done under the BJC section of devices.doc.
 *
 * CMYK to RGB conversion is made a la GhostScript unless you define
 * the preprocessor symbol USE_ADOBE_CMYK_RGB.
 *
 *   Ghostscript:	R = (1.0 - C) * (1.0 - K)
 *   Adobe:		R = 1.0 - min(1.0, C + K)
 *
 * (and similarly for G and B).  Ghostscript claims its method achieves
 * better results.
 *
 * For the BJC drivers, define BJC_DEFAULT_CENTEREDAREA if you want to
 * have the  same top and bottom margins (default to use the tallest
 * imageable area available, usually with a top margin smaller than
 * the bottom one). Defining USE_RECOMMENDED_MARGINS has the same
 * effect and also sets these margins to 12.4 mm. Other compilation
 * defines are explained in devices.doc.
 *
 * You can also define BJC_INIT_800_AS_600 to not use BJC-800-specific code
 * in the page initialization sequence (normally not useful to you at all,
 * just for my debugging of the driver margins).
 *
 */

#include "std.h"		/* to stop stdlib.h redefining types */
#include <stdlib.h>		/* for rand() */
#include "gdevprn.h"
#include "gdevpcl.h"
#include "gsparam.h"
#include "gsstate.h"

/* Conversion stuff. */
#include "gxlum.h"

/* Canon stuff */

#include "gdevbjc.h"

/***
 *** This file contains multiple drivers.  The main body of code, and all
 *** but the DesignJet driver, were contributed by George Cameron;
 *** please contact g.cameron@biomed.abdn.ac.uk if you have questions.
 *     1 - cdj500:	HP DeskJet 500C
 *     2 - cdj550:	HP DeskJet 550C
 *     3 - pjxl300:	HP PaintJet XL300
 *     4 - pj:		HP PaintJet
 *     5 - pjxl:	HP PaintJet XL
 *     6 - declj250:	DEC LJ250
 *** The DesignJet 650C driver was contributed by Koert Zeilstra;
 *** please contact koert@zen.cais.com if you have questions.
 *     7 - dnj650c      HP DesignJet 650C
 *** The LaserJet 4 driver with dithering was contributed by Eckhard
 *** Rueggeberg; please contact eckhard@ts.go.dlr.de if you have questions.
 *     8 - lj4dith:	HP LaserJet 4 with dithering
 *** The ESC/P driver (for Epson ESC/P compatible printers) was written by
 *** Yoshio Kuniyoshi <yoshio@nak.math.keio.ac.jp>, but is not maintained at
 *** the moment.
 *     9 - esc/p:       Epson ESC/P-compatible printers
 *** The BJC600 driver (which also works for BJC4000) was written first
 *** by Yoshio Kuniyoshi <yoshio@nak.math.keio.ac.jp> and later modified by
 *** Yves Arrouye <yves.arrouye@usa.net>. The current driver has been
 *** completely rewritten by me (YA) for good color handling.
 *    10 - bjc600:	BJC 600//4000 printers
 *** The BJC800 driver is based on the bjc600 one. By YA too.
 *    11 - bjc800:	BJC 800 printer
 *    12 - dj505j:      HP DeskJet 505J
 *    13 - pixty180:    NEC PICTY 180 (PC-PR101J/180)
 ***/

/*
 * All of the HP-like drivers have 8-bit (monochrome), 16-bit and 24-bit
 *     (colour) and for the DJ 550C 32-bit, (colour, cmyk mode)
 *     options in addition to the usual 1-bit and 3-bit modes
 * It is also possible to set various printer-specific parameters
 *     from the gs command line, eg.
 *
 *  gs -sDEVICE=cdj550 -dBitsPerPixel=16 -dDepletion=1 -dShingling=2 tiger.eps
 *
 * Please consult the appropriate section in the devices.doc file for
 * further details on all these drivers.
 *
 * All of the BJC-like drivers have 1-bit and 8-bit monochrome modes, 8-bit,
 *     16-bit, 24-bit and 32-bit colour cmyk mode (the 8-bit monochrome mode
 *     is called "4-bit". If you want to add a CMYK printer, look at the
 *     bjc6000/bjc800 devices declarations and initialization.
 *
 * If you want to support different color components for the same depth
 * on a non-CMYK printer, look how this is done for CMYK printers in
 * cdj_set_bpp.
 *
 */

/*
 * This taken from gsdparam.c. I hope it will be useable directly some day.
 *
 */

#define BEGIN_ARRAY_PARAM(pread, pname, pa, psize, e)\
  switch ( ncode = pread(plist, (oname = pname), &pa) )\
  {\
  case 0:\
        if ( pa.size != psize )\
          code = gs_error_rangecheck;\
        else {
/* The body of the processing code goes here. */
/* If it succeeds, it should do a 'break'; */
/* if it fails, it should set ecode and fall through. */
#define END_PARAM(pa, e)\
        }\
        goto e;\
  default:\
        code = ncode;\
e:	param_signal_error(plist, oname, code);\
  case 1:\
        pa.data = 0;		/* mark as not filled */\
  }

static int cdj_param_check_bytes(gs_param_list *, gs_param_name, const byte *, uint, bool);
static int cdj_param_check_float(gs_param_list *, gs_param_name, double, bool);
#define cdj_param_check_string(plist, pname, str, is_defined)\
  cdj_param_check_bytes(plist, pname, (const byte *)(str), strlen(str),\
                        is_defined)

/*
 * Drivers stuff.
 *
 */

#define DESKJET_PRINT_LIMIT  0.04	/* 'real' top margin? */
#define PAINTJET_PRINT_LIMIT 0.0	/* This is a guess.. */
#define ESC_P_PRINT_LIMIT    0.335

/* Margins are left, bottom, right, top. */
#define DESKJET_MARGINS_LETTER   (float)0.25, (float)0.50, (float)0.25, (float)0.167
#define DESKJET_MARGINS_A4       (float)0.125, (float)0.50, (float)0.143, (float)0.167
#define DESKJET_505J_MARGINS     (float)0.125, (float)0.50, (float)0.125, (float)0.167
#define DESKJET_505J_MARGINS_COLOR (float)0.125, (float)0.665, (float)0.125, (float)0.167
#define LJET4_MARGINS  		 (float)0.26, (float)0.0, (float)0.0, (float)0.0
/* The PaintJet and DesignJet seem to have the same margins */
/* regardless of paper size. */
#define PAINTJET_MARGINS         (float)0.167, (float)0.167, (float)0.167, (float)0.167
#define DESIGNJET_MARGINS        (float)0.167, (float)0.167, (float)0.167, (float)0.167

/*
 * With ESC/P commands, BJC-600 can print no more than 8 inches width.
 * So it can't use full width of letter size paper.  Since non printable
 * left side area is 0.134 inch, we set up margins as follows.
 *
 * Note to readers: the bjc drivers series do *not* use ESC/P commands
 * but raster ops. Configuration of these drivers can be done through
 * the gdevbjc.h file.
 *
 */
#define ESC_P_MARGINS_LETTER    (float)0.134, (float)(0.276+0.2), \
                                (float)(0.366+0.01), (float)0.335
#define ESC_P_MARGINS_A4        (float)0.134, (float)(0.276+0.2), \
                                (float)(0.166+0.01), (float)0.335

/* Define bits-per-pixel for generic drivers - default is 24-bit mode */
#ifndef BITSPERPIXEL
#  define BITSPERPIXEL 24
#endif

/*
 * The following use of size_of rather than sizeof is required to work
 * around a bug in Microsoft Visual C++ 5.0, which considers the THRESHOLD
 * value (128 << SHIFT) to be unsigned because SHIFT is unsigned (because
 * sizeof() is unsigned).
 */
#define W size_of(word)
#define I size_of(int)

#define invert_word(v)\
     ((v) >> 24) + (((v) >> 8) & 0xff00L) +\
         (((word)(v) << 8) & 0xff0000L) + ((word)(v) << 24)

/* Printer types */
#define DJ500C   0
#define DJ550C   1
#define DJ505J   2
#define PJXL300  3
#define PJ180    4
#define PJXL180  5
#define DECLJ250 6
#define DNJ650C  7
#define LJ4DITH  8
#define ESC_P	 9
#define BJC600	 10
#define BJC800	 11

/* No. of ink jets (used to minimise head movements) */
#define HEAD_ROWS_MONO 50
#define HEAD_ROWS_COLOUR 16

/* Colour mapping procedures */
static dev_proc_map_cmyk_color (gdev_cmyk_map_cmyk_color);
static dev_proc_map_rgb_color (gdev_cmyk_map_rgb_color);

static dev_proc_map_rgb_color (gdev_pcl_map_rgb_color);
static dev_proc_map_color_rgb (gdev_pcl_map_color_rgb);
static dev_proc_decode_color  (gdev_cmyk_map_color_cmyk);
static dev_proc_encode_color  (gdev_pcl_map_gray_color);
static dev_proc_decode_color  (gdev_pcl_map_color_gray);

/* Print-page, parameters and miscellaneous procedures */
static dev_proc_open_device(dj500c_open);
static dev_proc_open_device(dj550c_open);
static dev_proc_open_device(dj505j_open);
static dev_proc_open_device(dnj650c_open);
static dev_proc_open_device(lj4dith_open);
static dev_proc_open_device(pj_open);
static dev_proc_open_device(pjxl_open);
static dev_proc_open_device(pjxl300_open);
static dev_proc_open_device(escp_open);
static dev_proc_open_device(bjc_open);

static dev_proc_print_page(declj250_print_page);
static dev_proc_print_page(dj500c_print_page);
static dev_proc_print_page(dj550c_print_page);
static dev_proc_print_page(dj505j_print_page);
static dev_proc_print_page(picty180_print_page);
static dev_proc_print_page(dnj650c_print_page);
static dev_proc_print_page(lj4dith_print_page);
static dev_proc_print_page(lj4dithp_print_page);
static dev_proc_print_page(pj_print_page);
static dev_proc_print_page(pjxl_print_page);
static dev_proc_print_page(pjxl300_print_page);
static dev_proc_print_page(escp_print_page);
static dev_proc_print_page(bjc_print_page);

static dev_proc_get_params(cdj_get_params);
static dev_proc_get_params(pjxl_get_params);
static dev_proc_get_params(bjc_get_params);
#define ep_get_params cdj_get_params

static dev_proc_put_params(cdj_put_params);
static dev_proc_put_params(pj_put_params);
static dev_proc_put_params(pjxl_put_params);
static dev_proc_put_params(bjc_put_params);
#define ep_put_params cdj_put_params

/* The device descriptors */

#define gx_prn_colour_device_common \
    gx_prn_device_common; \
    short cmyk;	  	/* 0: not CMYK-capable, > 0: printing CMYK, */ \
                        /* < 0 : CMYK-capable, not printing CMYK */ \
    uint default_depth;	/* Used only for CMYK-capable printers now. */ \
    uint correction

typedef struct gx_device_cdj_s gx_device_cdj;
struct gx_device_cdj_s {
        gx_device_common;
        gx_prn_colour_device_common;
        int shingling;		  /* Interlaced, multi-pass printing */
        int depletion;		  /* 'Intelligent' dot-removal */
};

typedef struct gx_device_pjxl_s gx_device_pjxl;
struct gx_device_pjxl_s {
        gx_device_common;
        gx_prn_colour_device_common;
        int printqual;            /* Mechanical print quality */
        int rendertype;           /* Driver or printer dithering control */
};

typedef struct gx_device_hp_s gx_device_hp;
struct gx_device_hp_s {
        gx_device_common;
        gx_prn_colour_device_common;
};

typedef struct gx_device_hp_s gx_device_pj;

typedef struct gx_device_bjc600_s gx_device_bjc600;
typedef struct gx_device_bjc800_s gx_device_bjc800;

typedef struct gx_device_bjc800_s gx_device_bjc;

#define bjc_params_common \
    bool manualFeed;		/* Use manual feed */ \
    int mediaType;		/* Cf. strings below */ \
    bool mediaWeight_isSet;	/* Say if weight is an integer or null */ \
    int mediaWeight;		/* Weigth of the media */ \
    int printQuality;		/* Cf. strings below */ \
    bool ditheringType;		/* Do dithering */ \
    int colorComponents;	/* The number of *desired* color comps */ \
    int printColors		/* 0: Transparent, \
                                   1: C, 2: M, 4: Y, 7: K (Color decomp). \
                                   if > 8, print in black ink. */

typedef struct {
    bjc_params_common;

    bool monochromePrint;	/* Print with black only */
} bjc600_params;

typedef struct {
    bjc_params_common;
} bjc_params;

typedef bjc_params bjc800_params;

#define gx_bjc_device_common \
    gx_device_common; \
    gx_prn_colour_device_common; \
    int ptype; \
    float printLimit

struct gx_device_bjc600_s {
        gx_bjc_device_common;
        bjc600_params bjc_p;
};
struct gx_device_bjc800_s {
        gx_bjc_device_common;
        bjc800_params bjc_p;
};

typedef struct {
    gx_device_common;
    gx_prn_colour_device_common;
} gx_device_colour_prn;

/* Use the cprn_device macro to access generic fields (like cmyk,
   default_depth and correction), and specific macros for specific
   devices. */

#define cprn_device     ((gx_device_colour_prn*) pdev)

#define cdj       ((gx_device_cdj *)pdev)
#define pjxl      ((gx_device_pjxl *)pdev)
#define pj	((gx_device_pj *)pdev)

#define bjc             ((gx_device_bjc*) pdev)
#define bjc600          ((gx_device_bjc600*) pdev)
#define bjc800          ((gx_device_bjc800*) pdev)

#define bjcparams	(bjc->bjc_p)
#define bjc600params	(bjc600->bjc_p)
#define bjc800params	(bjc800->bjc_p)

#define bjcversion(p)	(((gx_device_bjc*) pdev)->ptype == BJC800 ? \
    BJC_BJC800_VERSION : BJC_BJC600_VERSION)
#define bjcversionstring(p)	(((gx_device_bjc*) pdev)->ptype == BJC800 ? \
    BJC_BJC800_VERSIONSTR : BJC_BJC600_VERSIONSTR)

#define bjcthickpaper(l) \
    (bjcparams.mediaWeight_isSet && bjcparams.mediaWeight > l)
#define bjc600thickpaper() bjcthickpaper(BJC600_MEDIAWEIGHT_THICKLIMIT)
#define bjc800thickpaper() bjcthickpaper(BJC800_MEDIAWEIGHT_THICKLIMIT)

/* The basic structure for all printers. Note the presence of the cmyk, depth
   and correct fields even if soem are not used by all printers. */

#define prn_colour_device_body(dtype, init, dname, w10, h10, xdpi, ydpi, lm, bm, rm, tm, ncomp, depth, mg, mc, dg, dc, print_page, cmyk, correct)\
    prn_device_body(dtype, init, dname, w10, h10, xdpi, ydpi, lm, bm, rm, tm, ncomp, depth, mg, mc, dg, dc, print_page), cmyk, depth /* default */, correct

/* Note: the computation of color_info values here must match */
/* the computation in the cdj_set_bpp procedure below. */

#define prn_hp_colour_device(dtype, init, dev_name, x_dpi, y_dpi, bpp, print_page, correct)\
    prn_colour_device_body(dtype, init, dev_name,\
    DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0,\
    (bpp == 32 ? 4 : (bpp == 1 || bpp == 8) ? 1 : 3), bpp,\
    (bpp >= 8 ? 255 : 1), (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0),\
    (bpp >= 8 ? 256 : 2), (bpp >= 8 ? 256 : bpp > 1 ? 2 : 0),\
    print_page, 0 /* cmyk */, correct)

#define prn_cmyk_colour_device(dtype, init, dev_name, x_dpi, y_dpi, bpp, print_page, correct)\
    prn_colour_device_body(dtype, init, dev_name,\
    DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0,\
    ((bpp == 1 || bpp == 4) ? 1 : 4), bpp,\
    (bpp > 8 ? 255 : 1), (1 << (bpp >> 2)) - 1, /* max_gray, max_color */\
    (bpp > 8 ? 256 : 2), (bpp > 8 ? 256 : bpp > 1 ? 2 : 0),\
    print_page, 1 /* cmyk */, correct)

#define bjc_device(dtype, p, d, x, y, b, pp, c) \
    prn_cmyk_colour_device(dtype, p, d, x, y, b, pp, c)

#define cdj_device(init, dev_name, x_dpi, y_dpi, bpp, print_page, correction, shingling, depletion)\
{ prn_hp_colour_device(gx_device_cdj, init, dev_name, x_dpi, y_dpi, bpp, print_page, correction),\
    shingling,\
    depletion\
}

#define pjxl_device(init, dev_name, x_dpi, y_dpi, bpp, print_page, printqual, rendertype)\
{ prn_hp_colour_device(gx_device_pjxl, init, dev_name, x_dpi, y_dpi, bpp, print_page, 0), \
    printqual,\
    rendertype\
}

#define pj_device(init, dev_name, x_dpi, y_dpi, bpp, print_page)\
{ prn_hp_colour_device(gx_device_pj, init, dev_name, x_dpi, y_dpi, bpp, print_page, 0) }

#define bjc600_device(init, dev_name, x_dpi, y_dpi, bpp, print_page, t, mf, mt, mws, mw, pq, dt, cc, pc, mp) \
{ bjc_device(gx_device_bjc600, init, dev_name, x_dpi, y_dpi, bpp, print_page, 0),\
    t, 0., { mf, mt, mws, mw, pq, dt, cc, pc, mp }\
}
#define bjc800_device(init, dev_name, x_dpi, y_dpi, bpp, print_page, t, mf, mt, mws, mw, pq, dt, cc, pc) \
{ bjc_device(gx_device_bjc800, init, dev_name, x_dpi, y_dpi, bpp, print_page, 0),\
    t, 0., { mf, mt, mws, mw, pq, dt, cc, pc }\
}

/* Since the print_page doesn't alter the device, this device can print in the background */
static void
hp_colour_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_bg(dev);

    set_dev_proc(dev, map_rgb_color, gdev_pcl_map_rgb_color);
    set_dev_proc(dev, map_color_rgb, gdev_pcl_map_color_rgb);
    set_dev_proc(dev, get_params, cdj_get_params);
    set_dev_proc(dev, put_params, cdj_put_params);

    if (dev->color_info.num_components == 1) {
        set_dev_proc(dev, encode_color, gdev_pcl_map_gray_color);
        set_dev_proc(dev, decode_color, gdev_pcl_map_color_gray);
    } else {
        set_dev_proc(dev, encode_color, gdev_pcl_map_rgb_color);
        set_dev_proc(dev, decode_color, gdev_pcl_map_color_rgb);
    }
}

static void
cdj500_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, dj500c_open);
}

static void
cdj550_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, dj550c_open);
}

#ifdef USE_CDJ550_CMYK
static void
cdj550cmyk_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_bg(dev);

    set_dev_proc(dev, open_device, dj550c_open);
    set_dev_proc(dev, get_params, cdj_get_params);
    set_dev_proc(dev, put_params, cdj_put_params);
    set_dev_proc(dev, map_cmyk_color, gdev_cmyk_map_cmyk_color);
    set_dev_proc(dev, encode_color, gdev_cmyk_map_cmyk_color);
    set_dev_proc(dev, decode_color, gdev_cmyk_map_color_cmyk);
}
#endif

static void
dj505j_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, dj505j_open);
}

static void
dnj650c_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, dnj650c_open);
}

static void
lj4dith_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, lj4dith_open);
}

static void
pj_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, pj_open);
    set_dev_proc(dev, get_params, gdev_prn_get_params);
    set_dev_proc(dev, put_params, pj_put_params);
}

static void
pjxl_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, pjxl_open);
    set_dev_proc(dev, get_params, pjxl_get_params);
    set_dev_proc(dev, put_params, pjxl_put_params);
}

static void
pjxl300_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, pjxl300_open);
    set_dev_proc(dev, get_params, pjxl_get_params);
    set_dev_proc(dev, put_params, pjxl_put_params);
}

static void
bjc_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_bg(dev);

    set_dev_proc(dev, open_device, bjc_open);
    set_dev_proc(dev, get_params, bjc_get_params);
    set_dev_proc(dev, put_params, bjc_put_params);
    set_dev_proc(dev, map_cmyk_color, gdev_cmyk_map_cmyk_color);
    set_dev_proc(dev, encode_color, gdev_cmyk_map_cmyk_color);
    set_dev_proc(dev, decode_color, gdev_cmyk_map_color_cmyk);
}

static void
escp_initialize_device_procs(gx_device *dev)
{
    hp_colour_initialize_device_procs(dev);

    set_dev_proc(dev, open_device, escp_open);
    set_dev_proc(dev, get_params, ep_get_params);
    set_dev_proc(dev, put_params, ep_put_params);
}

gx_device_cdj far_data gs_cdjmono_device =
cdj_device(cdj500_initialize_device_procs, "cdjmono", 300, 300, 1,
           dj500c_print_page, 4, 0, 1);

gx_device_cdj far_data gs_cdeskjet_device =
cdj_device(cdj500_initialize_device_procs, "cdeskjet", 300, 300, 24,
           dj500c_print_page, 4, 2, 1);

gx_device_cdj far_data gs_cdjcolor_device =
cdj_device(cdj500_initialize_device_procs, "cdjcolor", 300, 300, 24,
           dj500c_print_page, 4, 2, 1);

gx_device_cdj far_data gs_cdj500_device =
cdj_device(cdj500_initialize_device_procs, "cdj500", 300, 300, BITSPERPIXEL,
           dj500c_print_page, 4, 2, 1);

gx_device_cdj far_data gs_cdj550_device =
cdj_device(cdj550_initialize_device_procs, "cdj550", 300, 300, BITSPERPIXEL,
           dj550c_print_page, 0, 2, 1);

#ifdef USE_CDJ550_CMYK
gx_device_cdj far_data gs_cdj550cmyk_device = {
    prn_cmyk_colour_device(cdj550cmyk_initialize_device_procs, "cdj550cmyk", 300, 300,
        BITSPERPIXEL, dj550c_print_page, 0), 2, 1
};
#endif

gx_device_cdj far_data gs_picty180_device =
cdj_device(cdj550_initialize_device_procs, "picty180", 300, 300, BITSPERPIXEL,
           picty180_print_page, 0, 2, 1);

gx_device_cdj far_data gs_dj505j_device =
cdj_device(dj505j_initialize_device_procs, "dj505j", 300, 300, 1,
           dj505j_print_page, 4, 0, 1);

gx_device_pj far_data gs_declj250_device =
pj_device(pj_initialize_device_procs, "declj250", 180, 180, BITSPERPIXEL,
          declj250_print_page);

gx_device_cdj far_data gs_dnj650c_device =
cdj_device(dnj650c_initialize_device_procs, "dnj650c", 300, 300, BITSPERPIXEL,
           dnj650c_print_page, 0, 2, 1);

gx_device_cdj far_data gs_lj4dith_device =
cdj_device(lj4dith_initialize_device_procs, "lj4dith", 600, 600, 8,
           lj4dith_print_page, 4, 0, 1);

gx_device_cdj far_data gs_lj4dithp_device =
cdj_device(lj4dith_initialize_device_procs, "lj4dithp", 600, 600, 8,
           lj4dithp_print_page, 4, 0, 1);

gx_device_pj far_data gs_pj_device =
pj_device(pj_initialize_device_procs, "pj", 180, 180, BITSPERPIXEL,
          pj_print_page);

gx_device_pjxl far_data gs_pjxl_device =
pjxl_device(pjxl_initialize_device_procs, "pjxl", 180, 180, BITSPERPIXEL,
            pjxl_print_page, 0, 0);

gx_device_pjxl far_data gs_pjxl300_device =
pjxl_device(pjxl300_initialize_device_procs, "pjxl300", 300, 300, BITSPERPIXEL,
            pjxl300_print_page, 0, 0);

gx_device_cdj far_data gs_escp_device =
cdj_device(escp_initialize_device_procs, "escp", 360, 360, 8,
           escp_print_page, 0, 0, 1);

gx_device_cdj far_data gs_escpc_device =
cdj_device(escp_initialize_device_procs, "escpc", 360, 360, 24,
           escp_print_page, 0, 0, 1);

/* Args of bjc drivers are manualFeed, mediaType, printQuality, printColor,
   mediaWeight_isSet, mediaWeight, (monochromePrint) */

gx_device_bjc600 far_data gs_bjc600_device =
    bjc600_device(
        bjc_initialize_device_procs,
        BJC_BJC600,
        BJC600_DEFAULT_RESOLUTION,
        BJC600_DEFAULT_RESOLUTION,
        BJC600_DEFAULT_BITSPERPIXEL,
        bjc_print_page,
        BJC600,
        BJC600_DEFAULT_MANUALFEED,
        BJC600_DEFAULT_MEDIATYPE,
        BJC600_DEFAULT_SETMEDIAWEIGHT,
        BJC600_DEFAULT_MEDIAWEIGHT,
        BJC600_DEFAULT_PRINTQUALITY,
        BJC600_DEFAULT_DITHERINGTYPE,
        BJC600_DEFAULT_COLORCOMPONENTS,
        BJC600_DEFAULT_PRINTCOLORS,
        BJC600_DEFAULT_MONOCHROMEPRINT);

gx_device_bjc800 far_data gs_bjc800_device =
    bjc800_device(
        bjc_initialize_device_procs,
        BJC_BJC800,
        BJC800_DEFAULT_RESOLUTION,
        BJC800_DEFAULT_RESOLUTION,
        BJC800_DEFAULT_BITSPERPIXEL,
        bjc_print_page,
        BJC800,
        BJC800_DEFAULT_MANUALFEED,
        BJC800_DEFAULT_MEDIATYPE,
        BJC800_DEFAULT_SETMEDIAWEIGHT,
        BJC800_DEFAULT_MEDIAWEIGHT,
        BJC800_DEFAULT_PRINTQUALITY,
        BJC800_DEFAULT_DITHERINGTYPE,
        BJC600_DEFAULT_COLORCOMPONENTS,
        BJC800_DEFAULT_PRINTCOLORS);

/* Forward references */
static int gdev_pcl_mode1compress(const byte *, const byte *, byte *);
static int hp_colour_open(gx_device *, int);
static int hp_colour_print_page(gx_device_printer *, gp_file *, int);
static int cdj_put_param_int(gs_param_list *, gs_param_name, int *, int, int, int);
static uint gdev_prn_rasterwidth(const gx_device_printer *, int);
static int cdj_put_param_bpp(gx_device *, gs_param_list *, int, int, int);
static int cdj_set_bpp(gx_device *, int, int);
static void cdj_expand_line(word *, int, short, int, int);
static int bjc_fscmyk(byte**, byte*[4][4], int**, int, int);

/* String parameters manipulation */

typedef struct {
    const char* p_name;
    int p_value;
} stringParamDescription;

static const byte* paramValueToString(const stringParamDescription*, int);
static int paramStringValue(const stringParamDescription*,
    const byte*, int, int*);

static int put_param_string(gs_param_list*, const byte*,
    gs_param_string*, const stringParamDescription*, int *, int);
static int get_param_string(gs_param_list*, const byte*,
    gs_param_string*, const stringParamDescription*, int, bool, int);

/* Open the printer and set up the margins. */
static int
dj500c_open(gx_device *pdev)
{ return hp_colour_open(pdev, DJ500C);
}

static int
dj550c_open(gx_device *pdev)
{  return hp_colour_open(pdev, DJ550C);
}

static int
dj505j_open(gx_device *pdev)
{ return hp_colour_open(pdev, DJ505J);
}

static int
dnj650c_open(gx_device *pdev)
{ return hp_colour_open(pdev, DNJ650C);
}

static int
lj4dith_open(gx_device *pdev)
{ return hp_colour_open(pdev, LJ4DITH);
}

static int
pjxl300_open(gx_device *pdev)
{ return hp_colour_open(pdev, PJXL300);
}

static int
pj_open(gx_device *pdev)
{ return hp_colour_open(pdev, PJ180);
}

static int
pjxl_open(gx_device *pdev)
{ return hp_colour_open(pdev, PJXL180);
}

static int
escp_open(gx_device *pdev)
{ return hp_colour_open(pdev, ESC_P);
}

static int
bjc_open(gx_device *pdev)
{  return hp_colour_open(pdev, bjc->ptype);
}

static int
hp_colour_open(gx_device *pdev, int ptype)
{	/* Change the margins if necessary. */
  static const float dj_a4[4] = { DESKJET_MARGINS_A4 };
  static const float dj_letter[4] = { DESKJET_MARGINS_LETTER };
  static const float dj_505j[4] = { DESKJET_505J_MARGINS };
  static const float dj_505jc[4] = { DESKJET_505J_MARGINS_COLOR };
  static const float lj4_all[4] = { LJET4_MARGINS };
  static const float pj_all[4] = { PAINTJET_MARGINS };
  static const float dnj_all[4] = { DESIGNJET_MARGINS };
  static const float ep_a4[4] = { ESC_P_MARGINS_A4 };
  static const float ep_letter[4] = { ESC_P_MARGINS_LETTER };

  static float bjc_a3[4] = { BJC_MARGINS_A3 };		/* Not const! */
  static float bjc_letter[4] = { BJC_MARGINS_LETTER };  /* Not const! */
  static float bjc_a4[4] = { BJC_MARGINS_A4 };		/* Not const! */

  const float *m = (float *) 0;

  /* Set up colour params if put_params has not already done so */
  if (pdev->color_info.num_components == 0)
    {	int code = cdj_set_bpp(pdev, pdev->color_info.depth,
            pdev->color_info.num_components);
        if ( code < 0 )
          return code;
    }

  switch (ptype) {
  case DJ500C:
  case DJ550C:
    m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? dj_a4 :
         dj_letter);
    break;
  case DJ505J:
        m = pdev->color_info.num_components > 1 ? dj_505jc : dj_505j;
    break;
  case DNJ650C:
    m = dnj_all;
    break;
  case LJ4DITH:
    m = lj4_all;
    break;
  case PJ180:
  case PJXL300:
  case PJXL180:
    m = pj_all;
    break;
  case ESC_P:
    m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? ep_a4 :
         ep_letter);
    break;
  case BJC600:
  case BJC800:
    switch (gdev_pcl_paper_size(pdev)) {
        case PAPER_SIZE_LEGAL:
        case PAPER_SIZE_LETTER:
            m = bjc_letter;
            break;

        case PAPER_SIZE_A0:
        case PAPER_SIZE_A1:
        case PAPER_SIZE_A3:
            m = bjc_a3;
            break;

        default:
            m = bjc_a4;
    }

#ifndef USE_FIXED_MARGINS
    if (ptype == BJC800) {
        ((float *) m)[1] = (float)BJC_HARD_LOWER_LIMIT;
    }
#endif

    bjc->printLimit = m[3];		/* The real hardware limit. */

#ifdef BJC_DEFAULT_CENTEREDAREA
    if (m[3] < m[1]) {
        ((float *) m)[3] = m[1];  	/* Top margin = bottom one. */
    } else {
        ((float *) m)[1] = m[3];  	/* Bottom margin = top one. */
    }
#endif

    break;

  }
  if (m != 0)
    gx_device_set_margins(pdev, m, true);
  return gdev_prn_open(pdev);
}

/* Added parameters for DeskJet 5xxC */

/* Get parameters.  In addition to the standard and printer
 * parameters, we supply shingling and depletion parameters,
 * and control over the bits-per-pixel used in output rendering */
static int
cdj_get_params(gx_device *pdev, gs_param_list *plist)
{	int code = gdev_prn_get_params(pdev, plist);
        if ( code < 0 ||
            (code = param_write_int(plist, "BlackCorrect", (int *)&cdj->correction)) < 0 ||
            (code = param_write_int(plist, "Shingling", &cdj->shingling)) < 0 ||
            (code = param_write_int(plist, "Depletion", &cdj->depletion)) < 0
           )
          return code;

        return code;
}

/* Put parameters. */
static int
cdj_put_params(gx_device *pdev, gs_param_list *plist)
{	int correction = cdj->correction;
        int shingling = cdj->shingling;
        int depletion = cdj->depletion;
        int bpp = 0;
        int code = 0;

        code = cdj_put_param_int(plist, "BlackCorrect", &correction, 0, 9, code);
        code = cdj_put_param_int(plist, "Shingling", &shingling, 0, 2, code);
        code = cdj_put_param_int(plist, "Depletion", &depletion, 1, 3, code);
        code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);

        if ( code < 0 )
          return code;
        code = cdj_put_param_bpp(pdev, plist, bpp, bpp, 0);
        if ( code < 0 )
          return code;

        cdj->correction = correction;
        cdj->shingling = shingling;
        cdj->depletion = depletion;
        return 0;
}

/* Added parameters for PaintJet XL and PaintJet XL300 */

/* Get parameters.  In addition to the standard and printer
 * parameters, we supply print_quality and render_type
 * parameters, together with bpp control. */
static int
pjxl_get_params(gx_device *pdev, gs_param_list *plist)
{	int code = gdev_prn_get_params(pdev, plist);
        if ( code < 0 ||
            (code = param_write_int(plist, "PrintQuality", &pjxl->printqual)) < 0 ||
            (code = param_write_int(plist, "RenderType", &pjxl->rendertype)) < 0
           )
          return code;

        return code;
}

/* Put parameters. */
static int
pjxl_put_params(gx_device *pdev, gs_param_list *plist)
{	int printqual = pjxl->printqual;
        int rendertype = pjxl->rendertype;
        int bpp = 0, real_bpp = 0;
        int code = 0;

        code = cdj_put_param_int(plist, "PrintQuality", &printqual, -1, 1, code);
        code = cdj_put_param_int(plist, "RenderType", &rendertype, 0, 10, code);
        code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);

        if ( code < 0 )
          return code;
        real_bpp = bpp;
        if ( rendertype > 0 )
          {	/* If printer is doing the dithering, we must have a
                 * true-colour mode, ie. 16 or 24 bits per pixel */
                if ( bpp > 0 && bpp < 16 )
                  real_bpp = 24;
          }
        code = cdj_put_param_bpp(pdev, plist, bpp, real_bpp, 0);
        if ( code < 0 )
          return code;

        pjxl->printqual = printqual;
        pjxl->rendertype = rendertype;
        return 0;
}

/* Added parameters for PaintJet */

/* Put parameters.  In addition to the standard and printer */
/* parameters, we allow control of the bits-per-pixel */
static int
pj_put_params(gx_device *pdev, gs_param_list *plist)
{	int bpp = 0;
        int code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, 0);

        if ( code < 0 )
          return code;
        return cdj_put_param_bpp(pdev, plist, bpp, bpp, 0);
}

static stringParamDescription bjc_processColorsStrings[] = {
    { "DeviceGray",		1 },
    { "DeviceRGB",		3 },
    { "DeviceCMYK",		4 },
    { 0 }
};

static stringParamDescription bjc_mediaTypeStrings[] = {
    { "PlainPaper",		BJC_MEDIA_PLAINPAPER },
    { "CoatedPaper",		BJC_MEDIA_COATEDPAPER },
    { "TransparencyFilm",	BJC_MEDIA_TRANSPARENCYFILM },
    { "Envelope",		BJC_MEDIA_ENVELOPE },
    { "Card",			BJC_MEDIA_CARD},
    { "Other",		        BJC_MEDIA_OTHER },
    { 0 }
};

static stringParamDescription bjc600_printQualityStrings[] = {
    { "Normal",	0 },
    { "High",	1 },
    { "Draft",  2 },
    { 0 }
};

static stringParamDescription bjc800_printQualityStrings[] = {
    { "Normal",		0 },
    { "High",		1 },
    { "Low",    	3 },
    { "Draft",		4 },
    { 0 },
};

static stringParamDescription bjc_ditheringTypeStrings[] = {
    { "None",			BJC_DITHER_NONE },
    { "Floyd-Steinberg",	BJC_DITHER_FS },
    { 0 }
};

static int
bjc_get_params(gx_device *pdev, gs_param_list *plist)
{
    int code = gdev_prn_get_params(pdev, plist);
    int ncode;

    gs_param_string pmedia;
    gs_param_string pquality;
    gs_param_string dithering;

    if (code < 0) return_error(code);

    if ((ncode = param_write_bool(plist, BJC_OPTION_MANUALFEED,
        &bjcparams.manualFeed)) < 0) {
        code = ncode;
    }

    code = get_param_string(plist, (unsigned char *)BJC_OPTION_MEDIATYPE, &pmedia,
        bjc_mediaTypeStrings, bjcparams.mediaType, true, code);

    code = get_param_string(plist, (unsigned char *)BJC_OPTION_PRINTQUALITY, &pquality,
        (bjc->ptype == BJC800 ? bjc800_printQualityStrings :
        bjc600_printQualityStrings), bjcparams.printQuality,
        true, code);

    code = get_param_string(plist, (unsigned char *)BJC_OPTION_DITHERINGTYPE, &dithering,
        bjc_ditheringTypeStrings, bjcparams.ditheringType, true, code);

    if ((ncode = param_write_int(plist, BJC_OPTION_PRINTCOLORS,
        &bjcparams.printColors)) < 0) {
        code = ncode;
    }

    if ((ncode = (bjcparams.mediaWeight_isSet ?
        param_write_int(plist, BJC_OPTION_MEDIAWEIGHT,
            &bjcparams.mediaWeight) :
        param_write_null(plist, BJC_OPTION_MEDIAWEIGHT))) < 0) {
        code = ncode;
    }

    if (bjc->ptype != BJC800) {
        if ((ncode = param_write_bool(plist, BJC_OPTION_MONOCHROMEPRINT,
            &bjc600params.monochromePrint)) < 0) {
            code = ncode;
        }
    }

    /**/ {
        float version;
        gs_param_string versionString;

        bool bTrue = true;

        version = bjcversion(pdev);
        versionString.data = (byte *)bjcversionstring(pdev);

        versionString.size = strlen((char *)versionString.data);
        versionString.persistent = true;

        if ((ncode = param_write_float(plist, BJC_DEVINFO_VERSION,
                &version)) < 0) {
            code = ncode;
        }
        if ((ncode = param_write_string(plist, BJC_DEVINFO_VERSIONSTRING,
            &versionString)) < 0) {
            code = ncode;
        }

        if ((ncode = param_write_bool(plist, BJC_DEVINFO_OUTPUTFACEUP,
            &bTrue)) < 0) {
            code = ncode;
        }
    }

    return code;
}

/* Put properties for the bjc drivers. */

static int
bjc_put_params(gx_device *pdev, gs_param_list *plist)
{
    int bpp = 0, ccomps = 0;

    int code = 0;
    int ncode;

    bool aBool = true;

    const char* oname = (const char*) 0;

    bjc600_params new600Params;
    bjc800_params new800Params;

    bjc_params* params;

    gs_param_string pprocesscolors;
    gs_param_string pmedia;
    gs_param_string pquality;
    gs_param_string dithering;

    gs_param_float_array hwra;

    if (bjc->ptype != BJC800) {
        new600Params = bjc600params;
        params = (bjc_params*) &new600Params;
    } else {
        new800Params = bjc800params;
        params = (bjc_params*) &new800Params;
    }

    if ((code = cdj_put_param_int(plist, "BitsPerPixel",
        &bpp, 1, 32, code)) != 1) {
        bpp = pdev->color_info.depth;
    }

    if ((code = put_param_string(plist, (unsigned char *)"ProcessColorModel",
        &pprocesscolors, bjc_processColorsStrings, &ccomps, code)) != 1) {
        ccomps = pdev->color_info.num_components;
    }

    if ((ncode = param_read_bool(plist, oname = BJC_OPTION_MANUALFEED,
        &params->manualFeed)) < 0) {
        param_signal_error(plist, oname, code = ncode);
    }

    code = put_param_string(plist, (unsigned char *)BJC_OPTION_MEDIATYPE, &pmedia,
        bjc_mediaTypeStrings, &params->mediaType, code);

    code = cdj_put_param_int(plist, BJC_OPTION_PRINTCOLORS,
        &params->printColors, 0, 15, code);

    code = put_param_string(plist, (unsigned char *)BJC_OPTION_PRINTQUALITY, &pquality,
        (bjc->ptype == BJC800 ? bjc800_printQualityStrings :
        bjc600_printQualityStrings), &params->printQuality, code);

    code = put_param_string(plist, (unsigned char *)BJC_OPTION_DITHERINGTYPE, &dithering,
        bjc_ditheringTypeStrings, &params->ditheringType, code);

    switch (ncode = param_read_int(plist,
        oname = BJC_OPTION_MEDIAWEIGHT, &params->mediaWeight)) {
        case 0:
            if (params->mediaWeight <= 0) {
                ncode = gs_error_rangecheck;
            } else {
                params->mediaWeight_isSet = 1;
                break;
            }
            goto mwe;

        default:
            if ((ncode = param_read_null(plist, oname)) == 0) {
                params->mediaWeight_isSet = 0;
                break;
            }
mwe:   	    param_signal_error(plist, oname, code = ncode);

        case 1:
            break;
    }

    if (bjc->ptype != BJC800) {
        bjc600_params* params600 = (bjc600_params*) params;
        if ((ncode = param_read_bool(plist,
            oname = BJC_OPTION_MONOCHROMEPRINT,
            &params600->monochromePrint)) < 0) {
            param_signal_error(plist, oname, code = ncode);
        }
    }

    if ((ncode = cdj_param_check_float(plist, BJC_DEVINFO_VERSION,
        bjcversion(pdev), true)) < 0) {
        code = ncode;
    }
    if ((ncode = cdj_param_check_string(plist, BJC_DEVINFO_VERSIONSTRING,
        bjcversionstring(pdev), true)) < 0) {
        code = ncode;
    }

    if ((ncode = param_read_bool(plist, oname = BJC_DEVINFO_OUTPUTFACEUP,
        &aBool)) < 0) {
        param_signal_error(plist, oname, code = ncode);
    } else if (aBool != true) {
        param_signal_error(plist, oname, code = ncode = gs_error_rangecheck);
    }

    /* Check for invalid resolution. The array macros are taken from
       gsdparam.c and modified to use oname, ncode and code instead
       of param_name, code and ecode respectively. */

    BEGIN_ARRAY_PARAM(param_read_float_array, "HWResolution", hwra, 2, hwre)
        if ( hwra.data[0] <= 0 || hwra.data[1] <= 0 ||
             hwra.data[0] != hwra.data[1] )
            ncode = gs_error_rangecheck;
        else {
#ifdef BJC_STRICT
            if (hwra.data[0] != BJC_RESOLUTION_LOW &&
                hwra.data[0] != BJC_RESOLUTION_NORMAL &&
                hwra.data[0] != BJC_RESOLUTION_HIGH) {
                ncode = gs_error_rangecheck;
            }
#else
            /* A small hack for checking resolution without logarithms. */

            /**/ {
                int n;

                for (n = 0; n < 8 * sizeof(n); ++n) {
                    float res = (float)(BJC_RESOLUTION_BASE * n);

                    if (res == hwra.data[0]) break;

                    if (res > hwra.data[0]) {
                        ncode = gs_error_rangecheck;
                    }
                }

                if (n == 8 * sizeof(n)) {
                    ncode = gs_error_rangecheck;
                }
            }
#endif
            if (ncode < 0) {
                code = ncode;
            } else {
                break;
            }
        }
    END_PARAM(hwra, hwre)

    if ((ncode = cdj_put_param_bpp(pdev, plist, bpp, bpp, ccomps)) < 0) {
        code = ncode;
    }

    if (code < 0)
        return code;

    if (bpp == 1) {
        params->ditheringType = BJC_DITHER_NONE;
    }

    /* Write values that did change */

    if (bjc->ptype != BJC800) {
        bjc600params = new600Params;
    } else {
        bjc800params = new800Params;
    }

    return code;
}

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

/* The DeskJet500C can compress (mode 9) */
static int
dj500c_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, DJ500C);
}

/* The DeskJet550C can compress (mode 9) */
static int
dj550c_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, DJ550C);
}

/* The Picty180C can compress (mode 9) */
/* This printer need switching mode using PJL */
static int
picty180_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{  int ret_code;
  /* Ensure we're operating in PCL mode */
  gp_fputs("\033%-12345X@PJL ENTER LANGUAGE = PCLSLEEK\n", prn_stream);
  ret_code =  hp_colour_print_page(pdev, prn_stream, DJ550C);
  /* Reenter switch-configured language */
  gp_fputs("\033%-12345X", prn_stream);
  return ret_code;
}

/* The DeskJet505J can compress  */
static int
dj505j_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, DJ505J);
}

/* The DesignJet650C can compress (mode 1) */
static int
dnj650c_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, DNJ650C);
}

static int
lj4dith_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, LJ4DITH);
}

static int
lj4dithp_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{ int ret_code;
  /* Ensure we're operating in PCL mode */
  gp_fputs("\033%-12345X@PJL\r\n@PJL ENTER LANGUAGE = PCL\r\n", prn_stream);
  ret_code = hp_colour_print_page(pdev, prn_stream, LJ4DITH);
  /* Reenter switch-configured language */
  gp_fputs("\033%-12345X", prn_stream);
  return ret_code;
}

/* The PJXL300 can compress (modes 2 & 3) */
static int
pjxl300_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{ int ret_code;
  /* Ensure we're operating in PCL mode */
  gp_fputs("\033%-12345X@PJL enter language = PCL\n", prn_stream);
  ret_code = hp_colour_print_page(pdev, prn_stream, PJXL300);
  /* Reenter switch-configured language */
  gp_fputs("\033%-12345X", prn_stream);
  return ret_code;
}

/* The PaintJet XL can compress (modes 2 & 3) */
static int
pjxl_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, PJXL180);
}

/* The PaintJet can compress (mode 1) */
static int
pj_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, PJ180);
}

/* The LJ250 can compress (mode 1) */
static int
declj250_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{ int ret_code;
  gp_fputs("\033%8", prn_stream);	/* Enter PCL emulation mode */
  ret_code = hp_colour_print_page(pdev, prn_stream, DECLJ250);
  gp_fputs("\033%@", prn_stream);	/* Exit PCL emulation mode */
  return ret_code;
}

/* The BJC-600 cannot compress w/o raster image commands. */
static int
escp_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, ESC_P);
}

/* The BJC-600 can compress w/ raster image commands. */
static int
bjc_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
  return hp_colour_print_page(pdev, prn_stream, bjc->ptype);
}

/* MACROS FOR DITHERING (we use macros for compact source and faster code) */
/* Floyd-Steinberg dithering. Often results in a dramatic improvement in
 * subjective image quality, but can also produce dramatic increases in
 * amount of printer data generated and actual printing time!! Mode 9 2D
 * compression is still useful for fairly flat colour or blank areas but its
 * compression is much less effective in areas where the dithering has
 * effectively randomised the dot distribution. */

#define SHIFT ((I * 8) - 13)
#define RSHIFT ((I * 8) - 16)
#define RANDOM (((rand() << RSHIFT) % (MAXVALUE / 2))  - MAXVALUE / 4);
#define MINVALUE  0
#define MAXVALUE  (255 << SHIFT)
#define THRESHOLD (128 << SHIFT)
#define C 8

#define FSdither(inP, out, errP, Err, Bit, Offset, Element)\
        oldErr = Err;\
        Err = (errP[Element] + ((Err * 7 + C) >> 4) + ((int)inP[Element] << SHIFT));\
        if (Err > THRESHOLD) {\
          out |= Bit;\
          Err -= MAXVALUE;\
        }\
        errP[Element + Offset] += ((Err * 3 + C) >> 4);\
        errP[Element] = ((Err * 5 + oldErr + C) >> 4);

/* Here we rely on compiler optimisation to remove lines of the form
 * (if (1 >= 4) {...}, ie. the constant boolean expressions */

/* The original code is in the #else part. Since by default NEW_DITHER
   is not defined, the old code is used. No enhancement is visible for the
   bjc600 drivers with the new code, anyway :-( */

#ifdef NEW_DITHER

#define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
{\
    if (scan == 0) {       /* going_up */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        int oldErr;\
        bitmask = 0x80;\
        for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
          if (n >= 4) {\
              FSdither(dp, k, ep, kErr, bitmask, -n, 0);\
          }\
          if (n >= 3) {\
              FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
              FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
          }\
          FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
          dp += n, ep += n;\
        }\
        if (n >= 4)\
          *kP++ = k;\
        if (n >= 3) {\
          *cP++ = c;\
          *mP++ = m;\
        }\
        *yP++ = y;\
      }\
    } else {		/* going_down */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        int oldErr;\
        bitmask = 0x01;\
        for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
          dp -= n, ep -= n;\
          FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
          if (n >= 3) {\
            FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
            FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
          }\
          if (n >= 4) {\
            FSdither(dp, k, ep, kErr, bitmask, n, 0);\
          }\
        }\
        *--yP = y;\
        if (n >= 3)\
          { *--mP = m;\
            *--cP = c;\
          }\
        if (n >= 4)\
          *--kP = k;\
      }\
    }\
}

#else

#define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
{\
    if (scan == 0) {       /* going_up */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        int oldErr;\
        bitmask = 0x80;\
        for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
          if (n >= 4) {\
            if (*dp) {\
              FSdither(dp, k, ep, kErr, bitmask, -n, 0);\
              cErr = mErr = yErr = 0;\
            } else {\
              FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
              FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
              FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
            }\
          } else {\
            if (n >= 3) {\
              FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
              FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
            }\
            FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
          }\
          dp += n, ep += n;\
        }\
        if (n >= 4)\
          *kP++ = k;\
        if (n >= 3) {\
          *cP++ = c;\
          *mP++ = m;\
        }\
        *yP++ = y;\
      }\
    } else {		/* going_down */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        int oldErr;\
        bitmask = 0x01;\
        for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
          dp -= n, ep -= n;\
          if (n >= 4) {\
            if (*dp) {\
              FSdither(dp, k, ep, kErr, bitmask, n, 0);\
              cErr = mErr = yErr = 0;\
            } else {\
              FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
              FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
              FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
            }\
          } else {\
            FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
            if (n >= 3) {\
              FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
              FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
            }\
          }\
        }\
        *--yP = y;\
        if (n >= 3)\
          { *--mP = m;\
            *--cP = c;\
          }\
        if (n >= 4)\
          *--kP = k;\
      }\
    }\
}

#endif

/* END MACROS FOR DITHERING */

#define CPbit(inP, out, Bit, Element)\
  if (inP[Element]) {\
    out |= Bit;\
  }

#define COPYline(scan, i, j, plane_size, cP, mP, yP, kP, n)\
{\
    if (scan == 0) {       /* going_up */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        bitmask = 0x80;\
        for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
          if (n >= 4) {\
            CPbit(dp, k, bitmask, 0);\
          } \
          if (n >= 3) {\
            CPbit(dp, c, bitmask, n - 3);\
            CPbit(dp, m, bitmask, n - 2);\
          }\
          CPbit(dp, y, bitmask, n - 1);\
          dp += n, ep += n;\
        }\
        if (n >= 4)\
          *kP++ = k;\
        if (n >= 3) {\
          *cP++ = c;\
          *mP++ = m;\
        }\
        *yP++ = y;\
      }\
    } else {		/* going_down */\
      for (i = 0; i < plane_size; i++) {\
        byte c, y, m, k, bitmask;\
        bitmask = 0x01;\
        for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
          dp -= n, ep -= n;\
          if (n >= 4) {\
            CPbit(dp, k, bitmask, 0);\
          }\
          if (n >= 3) {\
            CPbit(dp, m, bitmask, n - 2);\
            CPbit(dp, c, bitmask, n - 3);\
          }\
          CPbit(dp, y, bitmask, n - 1);\
        }\
        *--yP = y;\
        if (n >= 3)\
          { *--mP = m;\
            *--cP = c;\
          }\
        if (n >= 4)\
          *--kP = k;\
      }\
    }\
}

/* Some convenient shorthand .. */
#define x_dpi        (pdev->x_pixels_per_inch)
#define y_dpi        (pdev->y_pixels_per_inch)
#define CONFIG_16BIT "\033*v6W\000\003\000\005\006\005"
#define CONFIG_24BIT "\033*v6W\000\003\000\010\010\010"

/* To calculate buffer size as next greater multiple of both parameter and W */
#define calc_buffsize(a, b) (((((a) + ((b) * W) - 1) / ((b) * W))) * W)

/*
 * Miscellaneous functions for Canon BJC-600 printers in raster command mode.
 */
#define fputshort(n, f) gp_fputc((n)%256,f);gp_fputc((n)/256,f)

static int
bjc_cmd(byte cmd, int argsize, byte *arg, gx_device_printer *pdev,
        gp_file *f)
{
  gp_fputs("\033(", f);
  gp_fputc(cmd, f);
  fputshort(argsize, f);
  gp_fwrite(arg, sizeof(byte), argsize, f);

  return 0;
}

static int
bjc_raster_cmd_sub(char c, int rastsize, byte *data, gp_file *f)
{
  gp_fputs("\033(A", f);
  fputshort(rastsize + 1, f);
  gp_fputc(c, f);
  gp_fwrite(data, sizeof(byte), rastsize, f);
  gp_fputc('\015', f);

  return 0;
}

static int
bjc_raster_cmd(int c_id, int rastsize, byte *data, gx_device_printer *pdev,
               gp_file *f)
{
    if (bjcparams.printColors == BJC_COLOR_ALLBLACK) {
        bjc_raster_cmd_sub('K', rastsize, data, f);
    } else if (pdev->color_info.num_components == 1) {
        if (bjcparams.printColors & BJC_COLOR_BLACK) {
            bjc_raster_cmd_sub('K', rastsize, data, f);
        } else {
            if (bjcparams.printColors & BJC_COLOR_YELLOW)
                bjc_raster_cmd_sub('Y', rastsize, data, f);
            if (bjcparams.printColors & BJC_COLOR_MAGENTA)
                bjc_raster_cmd_sub('M', rastsize, data, f);
            if (bjcparams.printColors & BJC_COLOR_CYAN)
                bjc_raster_cmd_sub('C', rastsize, data, f);
        }
    }else {			/* Color decomposition */
        static byte ymckCodes[] = {
            BJC_COLOR_YELLOW,
            BJC_COLOR_MAGENTA,
            BJC_COLOR_CYAN,
            BJC_COLOR_BLACK,
        };

        if (bjcparams.printColors & (int) ymckCodes[c_id]) {
            bjc_raster_cmd_sub("YMCK"[c_id], rastsize, data, f);
        }
    }

    return 0;
}

static int
bjc_init_page(gx_device_printer *pdev, gp_file *f)
{
    byte pagemargins[3], resolution[4], paperloading[2];

    /* Compute page margins. */

    pagemargins[0] = (byte) ((float) pdev->height / pdev->y_pixels_per_inch
        * 10 + .5);
    pagemargins[1] = (byte) 1;
    pagemargins[2] = (byte) ((pdev->width / pdev->x_pixels_per_inch * 10) -
        pdev->HWMargins[0] / 7.2 - pdev->HWMargins[2] / 7.2 + .5);

    /* Cheat to keep margins into bounds (while waiting to have the right
       margins for big papers. */

    switch (bjc->ptype) {
        case BJC800:
            if (pagemargins[2] > 114) pagemargins[2] = 114;
            break;

        default:
            if (pagemargins[2] > 80) pagemargins[2] = 80;
            break;
    }

    /* Initialize resolution argument. */

    resolution[0] = (byte) ((int)pdev->y_pixels_per_inch / 256);
    resolution[1] = (byte) ((int)pdev->y_pixels_per_inch % 256);
    resolution[2] = (byte) ((int)pdev->x_pixels_per_inch / 256);
    resolution[3] = (byte) ((int)pdev->x_pixels_per_inch % 256);

    /* Initialize paper loading argument. */

    paperloading[0] = 0x10 + ((1 - bjcparams.manualFeed) << 2);
    paperloading[1] = bjcparams.mediaType << 4;

    /* Reinitialize printer in raster mode. */

    gp_fputs("\033[K", f);
    fputshort(2, f);
    gp_fputc(0x00, f);
    gp_fputc(0x0f, f);

    /* Set page mode on (ignore data at end of page) */

    bjc_cmd('a', 1, (byte*) "\001", pdev, f);

    /* Set page margins */

    bjc_cmd('g', 3, pagemargins, pdev, f);

    /* Set compression on (this is PackBits compression a la TIFF/Mac) */

    bjc_cmd('b', 1, (byte*) "\001", pdev, f);

    /* Set paper loading. */

    bjc_cmd('l', 2, paperloading, pdev, f);

    /* Set printing method. */

#ifndef BJC_INIT_800_AS_600
    if (bjc->ptype == BJC800) {
#else
    if (0) {
#endif
        byte printmode[2];

        printmode[0] = bjcparams.printQuality;

        /* Modes not used are 3 (CN, Color Normal) and 2 (TP+ (?)) */

        switch (bjcparams.printQuality) {
            case BJC_QUALITY_DRAFT:
                printmode[0] = 4;			/* Draft */
                break;
        }

        printmode[1] = (bjcparams.mediaType >= BJC_MEDIA_ENVELOPE ? 1 :
            bjc800thickpaper());

        bjc_cmd('c', 2, printmode, pdev, f);
    } else /* BJC600 */ {
        byte printmeth[3];

        printmeth[0] = 0x10 + ((1 - bjcparams.manualFeed) << 2);
        printmeth[1] = (bjcparams.mediaType << 4) + bjcparams.printQuality;
        printmeth[2] = (bjcparams.printQuality == BJC_QUALITY_HIGH ?
            0x10 : 0) + (bjcparams.mediaType >= BJC_MEDIA_ENVELOPE ? 1 :
                 bjc600thickpaper());

        bjc_cmd('c', 3, printmeth, pdev, f);
    }

    /* Set raster resolution */

    bjc_cmd('d', 4, resolution, pdev, f);

    return 0;
}

static int
bjc_v_skip(int n, gx_device_printer *pdev, gp_file *f)
{
    if (n) {
        gp_fputs("\033(e", f);
        gp_fputc(2, f);
        gp_fputc(0, f);
        gp_fputc(n / 256, f);
        gp_fputc(n % 256, f);
    }

    return 0;
}

static int
bjc_finish_page(gx_device_printer *pdev, gp_file *f)
{
    bjc_cmd('a', 1, (byte*) "\000", pdev, f);
    bjc_cmd('b', 1, (byte*) "\000", pdev, f);
    gp_fputc('\014', f);
    gp_fputs("\033@", f);

    return 0;
}

/* 1D runlength compression for BJC-600
 * this code is borrowed from gdevpcl.c:gdev_pcl_mode2compress.
 */
static int
bjc_compress(const byte *row, const byte *end_row, byte *compressed)
{
  register const byte *exam = row;
  register byte *cptr = compressed; /* output pointer into compressed bytes */

  while ( exam < end_row ) {
    /* Search ahead in the input looking for a run */
    /* of at least 4 identical bytes. */
    const byte *compr = exam;
    const byte *end_dis;
    const byte *next;
    register byte test, test2;

    test = *exam;
    while ( exam + 1 < end_row ) {
      test2 = *++exam;
      if ( test == test2 )
          break;
      test = test2;
    }

    /* Find out how long the run is */
    end_dis = exam - 1;
    if ( exam == end_row ) { /* no run */
      next = --end_row;
    } else {

      next = exam + 1;
      while ( next < end_row && *next == test ) next++;
    }

    /* Now [compr..end_dis) should be encoded as dissimilar, */
    /* and [end_dis..next) should be encoded as similar. */
    /* Note that either of these ranges may be empty. */

    for ( ; ; ) {	/* Encode up to 128 dissimilar bytes */
      uint count = end_dis - compr; /* uint for faster switch */
      switch ( count ) { /* Use memcpy only if it's worthwhile. */
      case 6: cptr[6] = compr[5];
      case 5: cptr[5] = compr[4];
      case 4: cptr[4] = compr[3];
      case 3: cptr[3] = compr[2];
      case 2: cptr[2] = compr[1];
      case 1: cptr[1] = compr[0];
        *cptr = count - 1;
        cptr += count + 1;
      case 0: /* all done */
        break;
      default:
        if ( count > 128 ) count = 128;
        *cptr++ = count - 1;
        memcpy(cptr, compr, count);
        cptr += count, compr += count;
        continue;
      }
      break;
    }

    {	/* Encode up to 128 similar bytes. */
      /* Note that count may be <0 at end of row. */
      int count = next - end_dis;
      if (next < end_row || test != 0)
        while ( count > 0 ) {

          int this = (count > 128 ? 128 : count);
          *cptr++ = 257 - this;
          *cptr++ = (byte)test;
          count -= this;
        }
      exam = next;
    }
  }
  return cptr - compressed;
}

/*
 * For the ESC/P mode, resolution is fixed as 360dpi and we must transform
 * image data to serialized data.
 */
typedef struct {
  word *storage;
  uint  storage_size_words;
  byte *raster_buf[4][BJC_HEAD_ROWS];
  byte *print_buf;
  int   num_comps;
  int   plane_size;
  int   img_rows;
  int   ln_idx;
  int   vskip1;
  int   vskip2;
  const gs_memory_t *mem;
} ep_globals;

#define row_bytes (img_rows / 8)
#define min_rows (32)		/* for optimization of text image printing */

static int
ep_print_image(gp_file *prn_stream, ep_globals *eg, char cmd, byte *data, int size)
{
  int i, real_rows;
  static const char color[4] = {4,1,2,0};

  switch (cmd) {
  case 3:			/* Black */
  case 2:			/* Cyan */
  case 1:			/* Magenta */
  case 0:			/* Yellow */
    memcpy(eg->raster_buf[((int) cmd)][eg->ln_idx+eg->vskip2], data, size);
    return 0;
  case 'B':			/* blank line skip */
    if (!eg->ln_idx) {
      eg->vskip1 += size;
    } else if (size >= eg->img_rows - (eg->ln_idx+eg->vskip2) || eg->ln_idx+eg->vskip2 >= min_rows) {
      /* The 'I' cmd must precede 'B' cmd! */
      eg->vskip2 += size;
      ep_print_image(prn_stream, eg, 'F', 0, 0); /* flush and reset status */
    } else {
      eg->vskip2 += size;
    }
    return 0;
  case 'I':			/* Increment index */
    eg->ln_idx += eg->vskip2 + 1;
    eg->vskip2 = 0;
    if (eg->ln_idx < eg->img_rows) return 0;
    /* if eg->raster_buf filled up, then fall through here and flush buffer */
  case 'F':			/* flush print buffer */
    if (!eg->ln_idx) return 0;	/* The end of the page. */

    /* before print the image, perform vertical skip. */
    while (eg->vskip1 >= (255*2)) {
      gp_fputs("\033J\377", prn_stream); /* n/180in. feeding */
      eg->vskip1 -= (255*2);
    }
    if (eg->vskip1 > 255) {
      gp_fputs("\033J\200", prn_stream);
      eg->vskip1 -= 256;
    }
    if (eg->vskip1) {
      /* n/360in. feeding */
      gp_fputs("\033|J", prn_stream); gp_fputc(0, prn_stream); gp_fputc(eg->vskip1, prn_stream);
    }

    /* Optimize the number of nozzles to be used. */
    if (eg->ln_idx > 56) {		/* use 64 nozzles */
      real_rows = 64;
    } else if (eg->ln_idx > 48) {	/* use 56 nozzles */
      real_rows = 56;
    } else if (eg->ln_idx > 32) {	/* use 48 nozzles */
      real_rows = 48;
    } else {			/* use 32 nozzles */
      real_rows = 32;
    }

    for (i = 0; i < eg->num_comps; i++) {
      int lnum, hskip, print_size, img_rows;
      byte *p0, *p1, *p2, *p3;
      byte *inp, *inbuf, *outp, *outbuf;

      img_rows = real_rows;	/* Note that this img_rows is not the one in
                                 * the globals struct. */
      outbuf = eg->print_buf;

      /* Transpose raster image for serial printer image */
      for (lnum=0; lnum < img_rows; lnum+=8, outbuf++) {
        inbuf = inp = eg->raster_buf[i][lnum];
        for (outp = outbuf; inp < inbuf+eg->plane_size; inp++, outp += img_rows) {
          memflip8x8(inp, eg->plane_size, outp, row_bytes);
        }
      }

      /* Set color */
      if (eg->num_comps == 1) {
        /* Don't set color (to enable user setting). */
        gp_fputc('\015', prn_stream);
      } else {
        /* set color to one of CMYK. */
        gp_fputs("\015\033r", prn_stream);
        gp_fputc(color[i], prn_stream);
      }

      *(outp = eg->print_buf + eg->plane_size * img_rows) = 1; /* sentinel */

      p0 = p3 = eg->print_buf;

      /* print image p0 to p1 and h skip p1 to p2 if p2<outp,
       * then make p0=p2 and continue */
      while (p0 < outp) {
        static const word zeros[8] = {0,0,0,0,0,0,0,0};

        if (p3 < outp) {
          /* p1 is the head of running zeros. */
          /* note that h skip unit is 1/180inch */
          for (p1 = p3; !memcmp(p3, zeros, row_bytes*2); p3 += row_bytes*2);
          /* p2 is the head of non zero image. */
          p2 = p3;
        redo:
          for (p3 += row_bytes; p3 < outp && memcmp(p3, zeros, row_bytes); p3 += row_bytes);
          if (p3 < outp && memcmp(p3+row_bytes, zeros, row_bytes)) goto redo;
        } else p1 = p2 = outp;

        if (p0 < p1) {	/* print the image between p0 and p1 */
          print_size = ((p1 < outp) ? p1 : outp) - p0;
          gp_fputs("\033|B", prn_stream); gp_fputc(img_rows, prn_stream);
          fputshort(print_size, prn_stream);
          gp_fwrite(p0, sizeof(byte), print_size, prn_stream);
        }
        if (p1 < p2) {	/* skip running zeros from p1 to p2 */
          hskip = (((p2 < outp) ? p2 : outp) - p1) / row_bytes / 2;
          gp_fputs("\033\\", prn_stream);
          fputshort(hskip, prn_stream);
        }
        p0 = p2;
      }
    }
    return ep_print_image(prn_stream, eg, 'R', 0, eg->vskip2 + eg->ln_idx);
  case 'R':			/* Reset status */
    eg->ln_idx = 0;
    eg->vskip1 = size;
    eg->vskip2 = 0;
    memset(eg->storage, 0, eg->storage_size_words * W);
    return 0;
  default:			/* This should not happen */
    errprintf(eg->mem, "ep_print_image: illegal command character `%c'.\n", cmd);
    return 1;
  }

  /* NOT REACHED */
}

/* Send the page to the printer.  Compress each scan line. */
static int
hp_colour_print_page(gx_device_printer * pdev, gp_file * prn_stream, int ptype)
{
  uint raster_width = gdev_prn_rasterwidth(pdev, 1);
/*  int line_size = gdev_prn_rasterwidth(pdev, 0); */
  int line_size = gdev_prn_raster(pdev);
  int line_size_words = (line_size + W - 1) / W;
  int line_size_padded;
  int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  int num_comps = pdev->color_info.num_components;
  int bits_per_pixel = pdev->color_info.depth;
  int storage_bpp = bits_per_pixel;
  int expanded_bpp = bits_per_pixel;
  int plane_size, databuff_size;
  int combined_escapes = 1;
  int errbuff_size = 0;
  int outbuff_size = 0;
  int compression = 0;
  int scan = 0;
  int *errors[2] = { 0 }; /* Shut up coverity */
  int code = 0;
  const char *cid_string = (const char*) 0;
  byte *data[4], *plane_data[4][4], *out_data;
  byte *out_row, *out_row_alt;
  word *storage;
  uint storage_size_words;
  ep_globals eg;

  memset(&eg, 0, sizeof(eg));
  eg.img_rows=BJC_HEAD_ROWS;
  eg.mem=pdev->memory;

  /* Tricks and cheats ... */
  switch (ptype) {
  case DJ550C:
    if (num_comps == 3 && !cprn_device->cmyk)
      num_comps = 4;                      /* 4-component printing */
    break;
  case ESC_P:
    if (bits_per_pixel == 24)	/* prefer 3-component printing for bpp=24. */
      num_comps = 3;
    else
      if (num_comps != 1)
        num_comps = 4;
    break;
  case PJXL300:
  case PJXL180:
    if (pjxl->rendertype > 0) {
      if (bits_per_pixel < 16)
        pjxl->rendertype = 0;
      else {
        /* Control codes for CID sequence */
        cid_string = (bits_per_pixel == 16) ? CONFIG_16BIT : CONFIG_24BIT;
        /* Pretend we're a monobit device so we send the data out unchanged */
        bits_per_pixel = storage_bpp = expanded_bpp = 1;
        num_comps = 1;
      }
    }
    break;
  }

  if (cprn_device->cmyk <= 0) {
      if (storage_bpp == 8 && num_comps >= 3)
          bits_per_pixel = expanded_bpp = 3;	/* Only 3 bits of each byte used */
  }

  /* line_size = width * storage_bpp/8 (e.g. 9180) */
  /* storage_bpp = number of bits to store a single pixel in the output (e.g. 24) */
  /* plane_size = number of pixels we are going to process for each line (basically
   * line_size/storage_bpp rounded up a bit to allow for padding the output buffer
   * to word size). */
  plane_size = calc_buffsize(line_size, storage_bpp);
  eg.plane_size = plane_size;
  /* line_size_padded = how many pixels we'd actually get in the padded storage. */
  line_size_padded = plane_size * storage_bpp;
  /* BUT, if we're going to process plane_size pixels rather than line_size/storage_bpp
   * pixels, we ought to calculate future things based upon line_size_padded. */

  if (bits_per_pixel == 1) {		/* Data printed direct from i/p */
    databuff_size = 0;			/* so no data buffer required, */
    outbuff_size = plane_size * 4;	/* but need separate output buffers */
  }

  if (bits_per_pixel > 4) {		/* Error buffer for FS dithering */
    storage_bpp = expanded_bpp =
      num_comps * 8;			/* 8, 24 or 32 bits */

    if (cprn_device->cmyk > 0) {	/* Use CMYK dithering algorithm. */
        errbuff_size = 4 * (5 + 1 + 1 + line_size_padded + 1 + 2) * I;
    } else {			/* Use original (RGB) dithering. */
        errbuff_size =			/* 4n extra values for line ends */
            calc_buffsize((plane_size * expanded_bpp + num_comps * 4) * I, 1);
    }
  }

  databuff_size = plane_size * storage_bpp;

  storage_size_words = ((plane_size + plane_size) * num_comps +
                        databuff_size + errbuff_size + outbuff_size) / W;

  storage = (ulong *) gs_malloc(pdev->memory, storage_size_words, W, "hp_colour_print_page");
  eg.storage_size_words = (plane_size * (num_comps + 1)) / W * eg.img_rows
      + 16;			/* Redundant space for sentinel and aligning. */
  eg.storage = (word *) gs_malloc(pdev->memory, eg.storage_size_words, W, "ep_print_buffer");

  /*
   * The principal data pointers are stored as pairs of values, with
   * the selection being made by the 'scan' variable. The function of the
   * scan variable is overloaded, as it controls both the alternating
   * raster scan direction used in the Floyd-Steinberg dithering and also
   * the buffer alternation required for line-difference compression.
   *
   * Thus, the number of pointers required is as follows:
   *
   *   errors:      2  (scan direction only)
   *   data:        4  (scan direction and alternating buffers)
   *   plane_data:  4  (scan direction and alternating buffers)
   */

  if (storage == 0 || eg.storage == 0) /* can't allocate working area */
    return_error(gs_error_VMerror);
  else {
    int i, j;
    byte *p = out_data = out_row = (byte *)storage;
    byte *ep_p = (byte *)eg.storage;
    data[0] = data[1] = data[2] = p;
    data[3] = p + databuff_size;
    out_row_alt = out_row + plane_size * 2;
    if (bits_per_pixel > 1) {
      p += databuff_size;
    }
    if (bits_per_pixel > 4) {
      errors[0] = (int *)p + num_comps * 2;
      errors[1] = errors[0] + databuff_size;
      p += errbuff_size;
    }
    for (i = 0; i < num_comps; i++) {
      plane_data[0][i] = plane_data[2][i] = p;
      p += plane_size;
    }
    for (i = 0; i < num_comps; i++) {
      plane_data[1][i] = p;
      plane_data[3][i] = p + plane_size;
      p += plane_size;
    }
    if (bits_per_pixel == 1) {
      out_data = out_row = p;	  /* size is outbuff_size * 4 */
      out_row_alt = out_row + plane_size * 2;
      data[1] += databuff_size;   /* coincides with plane_data pointers */
      data[3] += databuff_size;
    }
    for (i = 0; i < num_comps; i++) {
      for (j = 0; j < eg.img_rows; j++) {
        eg.raster_buf[i][j] = ep_p;
        ep_p += plane_size;
      }
      /* Make a sentinel and align to word size.  */
      eg.print_buf = (byte *)((intptr_t)(ep_p + sizeof(word)) & ~(sizeof(word)-1));
    }
    eg.num_comps = num_comps;
  }

  /* Initialize printer. */
  if (ptype == DJ505J) {
    gp_fputs("\033@",prn_stream); /* Reset printer */
    gp_fprintf(prn_stream,"\033_R%c%c", /* Set resolution */
               (int)x_dpi & 0xff,((int)x_dpi >> 8) & 0xff);
  } else if (ptype == BJC600 || ptype == BJC800) {
    bjc_init_page(pdev, prn_stream);
  } else {
      if (ptype == LJ4DITH)  {
          gp_fputs("\033*rB", prn_stream);
      } else {
          gp_fputs("\033*rbC", prn_stream);                   /* End raster graphics */
      }
      gp_fprintf(prn_stream, "\033*t%dR", (int)x_dpi);
  /* Set resolution */
  }

  /* Clear temp storage */
  memset(storage, 0, storage_size_words * W);

#define DOFFSET (dev_t_margin(pdev) - DESKJET_PRINT_LIMIT)	/* Print position */
#define POFFSET (dev_t_margin(pdev) - PAINTJET_PRINT_LIMIT)
#define EOFFSET (dev_t_margin(pdev) - ESC_P_PRINT_LIMIT)
#define BOFFSET (dev_t_margin(pdev) - bjc->printLimit)
  switch (ptype) {
  case LJ4DITH:
    /* Page size, orientation, top margin & perforation skip */
    gp_fprintf(prn_stream, "\033&l%dA\033&l0o0e0L\033*r0F", paper_size);
    gp_fprintf(prn_stream, "\033*p0x0Y" ); /* These Offsets are hacked ! */
    gp_fprintf(prn_stream, "\033&u600D\033*r1A" );
    /* Select data compression */
    compression = 3;
    combined_escapes = 0;
    break;
  case DJ500C:
  case DJ550C:
    /* Page size, orientation, top margin & perforation skip */
    gp_fprintf(prn_stream, "\033&l%daolE", paper_size);
    /* Set depletion and shingling levels */
    gp_fprintf(prn_stream, "\033*o%dd%dQ", cdj->depletion, cdj->shingling);
    /* Move to top left of printed area */
    gp_fprintf(prn_stream, "\033*p%dY", (int)(300 * DOFFSET));
    /* Set number of planes ((-)1 is mono, (-)3 is (cmy)rgb, -4 is cmyk),
     * and raster width, then start raster graphics */
    gp_fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
    /* Select data compression */
    compression = 9;
    break;
  case DJ505J:
    /* Set depletion and shingling levels */
    gp_fprintf(prn_stream, "\033_D%c\033_E%c",
      cdj->depletion, cdj->shingling);
    /* Move to top left of printed area */
    gp_fwrite("\033_N\000", 4, 1, prn_stream);
    gp_fwrite("\033_J\xc4\xff", 5, 1, prn_stream);
    /* Set number of planes ((-)1 is mono, (-)3 is (cmy)rgb, -4 is cmyk),
     * and raster width, then start raster graphics */
    gp_fprintf(prn_stream, "\033_U%c%c",
      (0xffff - num_comps + 1) & 0xff, ((0xffff - num_comps + 1) >> 8) & 0xff);
    gp_fprintf(prn_stream,
      "\033_S%c%c", raster_width & 0xff, (raster_width >> 8) & 0xff);
    /* set origin */
    gp_fwrite("\033_A\001", 4, 1, prn_stream);
    compression = 1;
    combined_escapes = 0;
    break;
  case DNJ650C:
    if (pdev->x_pixels_per_inch == 600) {
        /* set resolution to 600dpi 1st through PJL command */
        gp_fprintf(prn_stream,"\033%%-12345X@PJL SET RESOLUTION = 600\n");
    }
    gp_fprintf (prn_stream, "\033%%0B"); /* Enter HPGL/2 mode */
    gp_fprintf (prn_stream, "BP5,1"); /* Turn off autorotation */
    gp_fprintf (prn_stream, "PS%d,%d",
             (int)((pdev->height/pdev->y_pixels_per_inch)*1016),
             (int)((pdev->width/pdev->x_pixels_per_inch)*1016)); /* Set length/width of page */
    gp_fprintf (prn_stream, "PU"); /* Pen up */
    gp_fprintf (prn_stream, "PA%d,%d", 0, 0); /* Move pen to upper-left */
    gp_fprintf (prn_stream, "\033%%1A"); /* Enter HP-RTL mode */
    gp_fprintf (prn_stream, "\033&a1N"); /* No negative motion - allow plotting
                                                while receiving */
    if (pdev->x_pixels_per_inch == 600)
        gp_fprintf (prn_stream, "\033*t600R"); /* request 600dpi via HP RTL */
    { static const char temp[] = {
        033, '*', 'v', '6', 'W',
        000 /* color model */,
        000 /* pixel encoding mode */,
        003 /* number of bits per index */,
        010 /* bits red */,
        010 /* bits green */,
        010 /* bits blue */
      };
      gp_fwrite (temp, 1, sizeof(temp), prn_stream);
    }

    /* Set raster width */
    gp_fprintf(prn_stream, "\033*r%dS", raster_width);
    /* Start raster graphics */
    gp_fprintf(prn_stream, "\033*r1A");

    /* Select data compression */
    compression = 1;
    /* No combined escapes for raster transfers */
    combined_escapes = 0;
    break;
  case PJXL300:
    /* Page size, orientation, top margin & perforation skip */
    gp_fprintf(prn_stream, "\033&l%daolE", paper_size);
    /* Set no-negative-motion mode, for faster (unbuffered) printing */
    gp_fprintf(prn_stream, "\033&a1N");
    /* Set print quality */
    gp_fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
    /* Move to top left of printed area */
    gp_fprintf(prn_stream, "\033*p%dY", (int)(300 * POFFSET));
    /* Configure colour setup */
    if (pjxl->rendertype > 0) {
      /* Set render type */
      gp_fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
      /* Configure image data */
      gp_fputs(cid_string, prn_stream);
      /* Set raster width, then start raster graphics */
      gp_fprintf(prn_stream, "\033*r%ds1A", raster_width);
    } else {
      /* Set number of planes (1 is mono, 3 is rgb),
       * and raster width, then start raster graphics */
      gp_fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
    }
    /* No combined escapes for raster transfers */
    combined_escapes = 0;
    break;
  case PJXL180:
    /* Page size, orientation, top margin & perforation skip */
    gp_fprintf(prn_stream, "\033&l%daolE", paper_size);
    /* Set print quality */
    gp_fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
    /* Move to top left of printed area */
    gp_fprintf(prn_stream, "\033*p%dY", (int)(180 * POFFSET));
    /* Configure colour setup */
    if (pjxl->rendertype > 0) {
      /* Set render type */
      gp_fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
      /* Configure image data */
      gp_fputs(cid_string, prn_stream);
      /* Set raster width, then start raster graphics */
      gp_fprintf(prn_stream, "\033*r%ds1A", raster_width);
    } else {
      /* Set number of planes (1 is mono, 3 is rgb),
       * and raster width, then start raster graphics */
      gp_fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
    }
    break;
  case PJ180:
  case DECLJ250:
    /* Disable perforation skip */
    gp_fprintf(prn_stream, "\033&lL");
    /* Move to top left of printed area */
    gp_fprintf(prn_stream, "\033&a%dV", (int)(720 * POFFSET));
    /* Set number of planes (1 is mono, 3 is rgb),
     * and raster width, then start raster graphics */
    gp_fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
    if (ptype == DECLJ250) {
      /* No combined escapes for raster transfers */
      combined_escapes = 0;
      /* From here on, we're a standard Paintjet .. */
      ptype = PJ180;
    }
    /* Select data compression */
    compression = 1;
    break;
  case ESC_P:
    /* Move to top left of printed area (must be modified for large movement(YK))*/
    if ((int)(EOFFSET*360)) gp_fprintf(prn_stream, "\033|J%c%c", 0, (int)(360*EOFFSET));
    combined_escapes = 0;
    break;
  case BJC600:
  case BJC800:
    /* Move to top left of printed area */
    bjc_v_skip((int)(pdev->HWResolution[1] * BOFFSET), pdev, prn_stream);
    combined_escapes = 0;
    compression = 2;		/* BJC600 uses the same method as mode 2 compression */
    break;
  }

  /* Unfortunately, the Paintjet XL300 PCL interpreter introduces a
   * version of the PCL language which is different to all earlier HP
   * colour and mono inkjets, in that it loses the very useful ability
   * to use combined escape sequences with the raster transfer
   * commands. In this respect, it is incompatible even with the older
   * 180 dpi PaintJet and PaintJet XL printers!  Another regrettable
   * omission is that 'mode 9' compression is not supported, as this
   * mode can give both computational and PCL file size advantages. */

  if (ptype == DJ505J) {
    gp_fprintf(prn_stream, "\033_M%c", compression);
  } else if (combined_escapes) {
    /* From now on, all escape commands start with \033*b, so we
     * combine them (if the printer supports this). */
    gp_fputs("\033*b", prn_stream);
     /* Set compression if the mode has been defined. */
    if (compression)
      gp_fprintf(prn_stream, "%dm", compression);
  }
  else if (ptype == BJC600 || ptype == BJC800)
      ;				/* Currently, nothing to do. */
  else
    if (compression)
      gp_fprintf(prn_stream, "\033*b%dM", compression);

  /* Send each scan line in turn */
  {
    int cErr, mErr, yErr, kErr;
    int this_pass, lnum, i;
    int start_rows;
    int lend, num_blank_lines = 0;

    word rmask = ~(word) 0 << ((-pdev->width * storage_bpp) & (W * 8 - 1));

    lend = pdev->height -
        (int)((dev_t_margin(pdev) + dev_b_margin(pdev)) * y_dpi);

    switch (ptype) {
        case BJC600:
        case BJC800:
            start_rows = BJC_HEAD_ROWS;
            break;

        /* Inhibit blank line printing for RGB-only printers, since in
         * this case 'blank' means black!  Also disabled for XL300 due to
         * an obscure bug in the printer's firmware */

        case PJ180:
        case PJXL180:
        case PJXL300:
            start_rows = -1;
            break;

        default:
            start_rows = (num_comps == 1) ? HEAD_ROWS_MONO - 1 :
                HEAD_ROWS_COLOUR - 1;
            break;
    }

    cErr = mErr = yErr = kErr = 0;

    if (bits_per_pixel > 4) { /* Randomly seed initial error buffer */
      if (cprn_device->cmyk > 0 && expanded_bpp == 32) {
          bjc_fscmyk(data, plane_data, errors, plane_size, -1);
      } else {
          int *ep = errors[0];
          for (i = 0; i < databuff_size; i++) {
              *ep++ = RANDOM;
          }
      }
    }

    this_pass = start_rows;
    for (lnum = 0; lnum < lend; lnum++) {
      word *data_words = (word *)data[scan];
      register word *end_data = data_words + line_size_words;

      code = gdev_prn_copy_scan_lines(pdev, lnum, data[scan], line_size);
      if (code < 0)
          goto xit;

      /* Mask off 1-bits beyond the line width. */
      end_data[-1] &= rmask;

      /* Remove trailing 0s. */
      while (end_data > data_words && end_data[-1] == 0)
        end_data--;
      if (ptype != DNJ650C)	/* DesignJet can't skip blank lines ? ? */
        if (end_data == data_words) {	/* Blank line */
          num_blank_lines++;
          continue;
        }
      /* Skip blank lines if any */
      if (num_blank_lines > 0) {
        if (ptype == DJ505J) {
          gp_fprintf(prn_stream,"\033_Y%c%c",
          num_blank_lines & 0xff, (num_blank_lines >> 8) & 0xff);
        } else if (ptype == ESC_P) {
          ep_print_image(prn_stream, &eg, 'B', 0, num_blank_lines);
        } else if (ptype == BJC600 || ptype == BJC800) {
            bjc_v_skip(num_blank_lines, pdev, prn_stream);
        } else if (num_blank_lines < this_pass) {
          /* Moving down from current position
           * causes head motion on the DeskJets, so
           * if the number of lines is within the
           * current pass of the print head, we're
           * better off printing blanks. */
          this_pass -= num_blank_lines;
          if (combined_escapes) {
            gp_fputc('y', prn_stream);   /* Clear current and seed rows */
            for (; num_blank_lines; num_blank_lines--)
              gp_fputc('w', prn_stream);
          } else {
#if 0
/**************** The following code has been proposed ****************/
/**************** as a replacement: ****************/
            gp_fputs("\033*b1Y", prn_stream);   /* Clear current and seed rows */
            if ( num_blank_lines > 1 )
              gp_fprintf(prn_stream, "\033*b%dY", num_blank_lines - 1);
            num_blank_lines = 0;
#else
            gp_fputs("\033*bY", prn_stream);   /* Clear current and seed rows */
            if (ptype == DNJ650C) {
              gp_fprintf (prn_stream, "\033*b%dY", num_blank_lines);
              num_blank_lines = 0;
            }
            else {
              for (; num_blank_lines; num_blank_lines--)
                gp_fputs("\033*bW", prn_stream);
            }
#endif
          }
        } else {
          if (combined_escapes)
            gp_fprintf(prn_stream, "%dy", num_blank_lines);
          else
            gp_fprintf(prn_stream, "\033*b%dY", num_blank_lines);
        }
        memset(plane_data[1 - scan][0], 0, plane_size * num_comps);
        num_blank_lines = 0;
        this_pass = start_rows;
      }
      {			/* Printing non-blank lines */
        register byte *kP = plane_data[scan + 2][3];
        register byte *cP = plane_data[scan + 2][2];
        register byte *mP = plane_data[scan + 2][1];
        register byte *yP = plane_data[scan + 2][0];
        register byte *dp = data[scan + 2];
        register int *ep = errors[scan];
        int zero_row_count;
        int i, j;
        byte *odp;

        if (this_pass)
          this_pass--;
        else
          this_pass = start_rows;

        if (expanded_bpp > bits_per_pixel) {  /* Expand line if required */
            cdj_expand_line(data_words, line_size,
                cprn_device->cmyk,
                bits_per_pixel, expanded_bpp);
        }

        /* In colour modes, we have some bit-shuffling to do before
         * we can print the data; in FS mode we also have the
         * dithering to take care of. */
        switch (expanded_bpp) {    /* Can be 1, 3, 8, 24 or 32 */
        case 3:
          /* Transpose the data to get pixel planes. */
          for (i = 0, odp = plane_data[scan][0]; i < databuff_size;
               i += 8, odp++) {	/* The following is for 16-bit
                                 * machines */
#define spread3(c)\
    { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
            static ulong spr40[8] = spread3(0x40);
            static ulong spr08[8] = spread3(8);
            static ulong spr02[8] = spread3(2);
            register byte *dp = data[scan] + i;
            register ulong pword =
            (spr40[dp[0]] << 1) +
            (spr40[dp[1]]) +
            (spr40[dp[2]] >> 1) +
            (spr08[dp[3]] << 1) +
            (spr08[dp[4]]) +
            (spr08[dp[5]] >> 1) +
            (spr02[dp[6]]) +
            (spr02[dp[7]] >> 1);
            odp[0] = (byte) (pword >> 16);
            odp[plane_size] = (byte) (pword >> 8);
            odp[plane_size * 2] = (byte) (pword);
          }
          break;

        case 8:
          switch (ptype) {
              case BJC600:
              case BJC800:
                  if (bjcparams.ditheringType == BJC_DITHER_NONE) {
                      COPYline(scan, i, j, plane_size, cP, mP, yP, kP, 1);
                      break;
                  }

              default:
                  FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
                          cP, mP, yP, kP, 1);
          }
          break;
        case 24:
          FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
                  cP, mP, yP, kP, 3);
          break;
        case 32:
          if (cprn_device->cmyk > 0) {
              bjc_fscmyk(data, plane_data, errors, plane_size, scan);
          } else {
              FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
                      cP, mP, yP, kP, 4);
          }
          break;

        } /* switch(expanded_bpp) */

        /* Make sure all black is in the k plane */

        if (num_comps == 4 && (cprn_device->cmyk <= 0 || expanded_bpp != 32)) {
            register word *kp = (word *)plane_data[scan][3];
            register word *cp = (word *)plane_data[scan][2];
            register word *mp = (word *)plane_data[scan][1];
            register word *yp = (word *)plane_data[scan][0];
            if (bits_per_pixel > 4) {  /* Done as 4 planes */
                for (i = 0; i < plane_size / W; i++) {
                    word bits = *cp & *mp & *yp;
                    *kp++ |= bits;
                    bits = ~bits;
                    *cp++ &= bits;
                    *mp++ &= bits;
                    *yp++ &= bits;
                }
            } else {  /* This has really been done as 3 planes */
                for (i = 0; i < plane_size / W; i++) {
                    word bits = *cp & *mp & *yp;
                    *kp++ = bits;
                    bits = ~bits;
                    *cp++ &= bits;
                    *mp++ &= bits;
                    *yp++ &= bits;
                }
            }
        }

        /* Transfer raster graphics in the order (K), C, M, Y */

        for (zero_row_count = 0, i = num_comps - 1; i >= 0; i--) {
          int output_plane = 1;
          int out_count = 0;

          switch (ptype) {
          case DJ500C:    /* Always compress using mode 9 */
          case DJ550C:
            out_count = gdev_pcl_mode9compress(plane_size,
                                               plane_data[scan][i],
                                               plane_data[1 - scan][i],
                                               out_data);

            /* This optimisation allows early termination of the
             * row, but this doesn't work correctly in an alternating
             * mode 2 / mode 3 regime, so we only use it with mode 9
             * compression */
           if (out_count == 0)
             { output_plane = 0;      /* No further output for this plane */
               if (i == 0)
                 gp_fputc('w', prn_stream);
               else
                 zero_row_count++;
             }
           else
             { for (; zero_row_count; zero_row_count--)
                 gp_fputc('v', prn_stream);
             }
            break;
          case DJ505J:
            out_count = gdev_pcl_mode1compress((const byte *)
                                               plane_data[scan][i],
                                               (const byte *)
                                               plane_data[scan][i] + plane_size - 1,
                                               out_data);
            break;
          case PJ180:
          case DNJ650C:
            if (num_comps > 1)
              { word *wp = (word *)plane_data[scan][i];
                for (j = 0; j < plane_size / W; j++, wp++)
                  *wp = ~*wp;
              }
            out_count = gdev_pcl_mode1compress((const byte *)
                                               plane_data[scan][i],
                                               (const byte *)
                                               plane_data[scan][i] + plane_size - 1,
                                               out_data);
            break;
          case PJXL180:    /* Need to invert data as CMY not supported */
            if (num_comps > 1)
              { word *wp = (word *)plane_data[scan][i];
                for (j = 0; j < plane_size / W; j++, wp++)
                  *wp = ~*wp;
              }
            /* fall through .. */
          case PJXL300:     /* Compression modes 2 and 3 are both
                             * available.  Try both and see which one
                             * produces the least output data. */
          case LJ4DITH:
            { const byte *plane = plane_data[scan][i];
              byte *prev_plane = plane_data[1 - scan][i];
              const word *row = (word *)plane;
              const word *end_row = row + plane_size/W;
              int count2 = gdev_pcl_mode2compress(row, end_row, out_row_alt);
              int count3 = gdev_pcl_mode3compress(plane_size, plane, prev_plane, out_row);
              int penalty = combined_escapes ? strlen("#m") : strlen("\033*b#M");
              int penalty2 = (compression == 2 ? 0 : penalty);
              int penalty3 = (compression == 3 ? 0 : penalty);

              if (count3 + penalty3 < count2 + penalty2)
                { if ( compression != 3 ) {
                    if (combined_escapes)
                      gp_fputs("3m", prn_stream);
                    else
                      gp_fputs("\033*b3M", prn_stream);
                    compression = 3;
                  }
                  out_data = out_row;
                  out_count = count3;
                }
              else
                { if ( compression != 2 ) {
                    if (combined_escapes)
                      gp_fputs("2m", prn_stream);
                    else
                      gp_fputs("\033*b2M", prn_stream);
                    compression = 2;
                  }
                  out_data = out_row_alt;
                  out_count = count2;
                }
            }
            break;
          case BJC600:
          case BJC800:
            { const byte *plane = (byte *)plane_data[scan][i];
              int count2 = bjc_compress(plane, plane + plane_size, out_row_alt);

              out_data = out_row_alt;
              out_count = count2;
            }
            break;
          }
          if (output_plane) {
            if (ptype == DJ505J)
              gp_fprintf(prn_stream, "\033_%c%c%c",
                      "WVVV"[i], out_count & 0xff, (out_count >> 8) & 0xff);
            else if (combined_escapes)
              gp_fprintf(prn_stream, "%d%c", out_count, "wvvv"[i]);
            else if (ptype == BJC600 || ptype == BJC800) {
              if (out_count)
                  bjc_raster_cmd(num_comps == 1 ? 3 : i,
                                out_count, out_data, pdev, prn_stream);
              if (i == 0) bjc_v_skip(1, pdev, prn_stream);
            } else if (ptype == ESC_P)
                ep_print_image(prn_stream, &eg, (char)i, plane_data[scan][i], plane_size);
            else
              gp_fprintf(prn_stream, "\033*b%d%c", out_count, "WVVV"[i]);
            if (ptype < ESC_P)
              gp_fwrite(out_data, sizeof(byte), out_count, prn_stream);
          }

        } /* Transfer Raster Graphics ... */
        if (ptype == ESC_P)
            ep_print_image(prn_stream, &eg, 'I', 0, 0); /* increment line index */
        scan = 1 - scan;          /* toggle scan direction */
      }	  /* Printing non-blank lines */
    }     /* for lnum ... */
  }       /* send each scan line in turn */

  if (combined_escapes)
    gp_fputs("0M", prn_stream);

  /* end raster graphics */
  if (ptype == BJC600 || ptype == BJC800) {
    bjc_finish_page(pdev, prn_stream);
  } else if (ptype == DJ505J)
    gp_fputs("\033_C", prn_stream);
  else if (ptype != ESC_P)
    gp_fputs("\033*rbC\033E", prn_stream);

  /* eject page */
  if (ptype == PJ180)
    gp_fputc('\f', prn_stream);
  else if (ptype == DJ505J)
    gp_fputs("\f\033@", prn_stream);
  else if (ptype == DNJ650C)
    gp_fputs ("\033*rC\033%0BPG;", prn_stream);
  else if (ptype == BJC600 || ptype == BJC800)
      ;				/* Already done */
  else if (ptype == ESC_P) {
    ep_print_image(prn_stream, &eg, 'F', 0, 0); /* flush print buffer */
    gp_fputs("\014\033@", prn_stream);	/* reset after eject page */
  } else
    gp_fputs("\033&l0H", prn_stream);

xit:
  /* free temporary storage */
  gs_free(pdev->memory, (char *) eg.storage, eg.storage_size_words, W, "ep_print_buffer");
  gs_free(pdev->memory, (char *) storage, storage_size_words, W, "hp_colour_print_page");

  return code;
}

/*
 * Row compression for the H-P PaintJet.
 * Compresses data from row up to end_row, storing the result
 * starting at compressed.  Returns the number of bytes stored.
 * The compressed format consists of a byte N followed by a
 * data byte that is to be repeated N+1 times.
 * In the worst case, the `compressed' representation is
 * twice as large as the input.
 * We complement the bytes at the same time, because
 * we accumulated the image in complemented form.
 */
static int
gdev_pcl_mode1compress(const byte *row, const byte *end_row, byte *compressed)
{	register const byte *in = row;
        register byte *out = compressed;
        while ( in < end_row )
           {	byte test = *in++;
                const byte *run = in;
                while ( in < end_row && *in == test ) in++;
                /* Note that in - run + 1 is the repetition count. */
                while ( in - run > 255 )
                   {	*out++ = 255;
                        *out++ = test;
                        run += 256;
                   }
                *out++ = in - run;
                *out++ = test;
           }
        return out - compressed;
}

/*
 * Map a CMYK color to a color index. We just use depth / 4 bits per color
 * to produce the color index.
 *
 * Important note: CMYK values are stored in the order K, C, M, Y because of
 * the way the HP drivers work.
 *
 */

#define gx_color_value_to_bits(cv, b) \
    ((cv) >> (gx_color_value_bits - (b)))
#define gx_bits_to_color_value(cv, b) \
    ((cv) << (gx_color_value_bits - (b)))

#define gx_cmyk_value_bits(c, m, y, k, b) \
    (((COLROUND_ROUND(k)) << (3 * (b))) | \
     ((COLROUND_ROUND(c)) << (2 * (b))) | \
     ((COLROUND_ROUND(m)) << (b)) | \
     ((COLROUND_ROUND(y))))

#define gx_value_cmyk_bits(v, c, m, y, k, b) \
    (k) = COLDUP_DUP(((v) >> (3 * (b))) & ((1 << (b)) - 1)), \
    (c) = COLDUP_DUP(((v) >> (2 * (b))) & ((1 << (b)) - 1)), \
    (m) = COLDUP_DUP(((v) >> (b)) & ((1 << (b)) - 1)), \
    (y) = COLDUP_DUP((v) & ((1 << (b)) - 1))

static gx_color_index
gdev_cmyk_map_cmyk_color(gx_device* pdev, const gx_color_value cv[])
{
    gx_color_value cyan, magenta, yellow, black;
    gx_color_index color;
    cyan = cv[0]; magenta = cv[1]; yellow = cv[2]; black = cv[3];
    switch (pdev->color_info.depth) {
        case 1:
           color = (cyan | magenta | yellow | black) > gx_max_color_value / 2 ?
               (gx_color_index) 1 : (gx_color_index) 0;
           break;

        default: {
            COLROUND_VARS;
            int nbits = pdev->color_info.depth>>2;
            COLROUND_SETUP(nbits);

            color = gx_cmyk_value_bits(cyan, magenta, yellow, black, nbits);
         }
   }

   return color;
}

/* Mapping of RGB colors to gray values. */

static gx_color_index
gdev_cmyk_map_rgb_color(gx_device *pdev, const gx_color_value cv[])
{
    gx_color_value r, g, b;
    r = cv[0]; g = cv[1]; b = cv[2];
    if (gx_color_value_to_byte(r & g & b) == 0xff) {
        return (gx_color_index) 0;	/* White */
    } else {
        gx_color_value c = gx_max_color_value - r;
        gx_color_value m = gx_max_color_value - g;
        gx_color_value y = gx_max_color_value - b;

        switch (pdev->color_info.depth) {
        case 1:
            return (c | m | y) > gx_max_color_value / 2 ?
                (gx_color_index) 1 : (gx_color_index) 0;
            /*NOTREACHED*/
            break;

        case 8:
            return ((ulong) c * lum_red_weight * 10
                    + (ulong) m * lum_green_weight * 10
                    + (ulong) y * lum_blue_weight * 10)
                        >> (gx_color_value_bits + 2);
            /*NOTREACHED*/
            break;
        }
    }

    return (gx_color_index) 0;	/* This should never happen. */
}

/* Mapping of CMYK colors. */

static int
gdev_cmyk_map_color_cmyk(gx_device *pdev, gx_color_index color, gx_color_value pcmyk[])
{
    switch (pdev->color_info.depth) {
        case 1:
           pcmyk[0] = gx_max_color_value * (1 - color);
           break;

        case 8:
           if (pdev->color_info.num_components == 1) {
               gx_color_value value = (gx_color_value) color ^ 0xff;

               pcmyk[0] = (value << 8) + value;

               break;
           }

        default: {
            unsigned long bcyan, bmagenta, byellow, black;
            int nbits = pdev->color_info.depth>>2;
            COLDUP_VARS;

            COLDUP_SETUP(nbits);
            gx_value_cmyk_bits(color, bcyan, bmagenta, byellow, black, nbits);

            pcmyk[0] = bcyan;
            pcmyk[1] = bmagenta;
            pcmyk[2] = byellow;
            pcmyk[3] = black;
        }
    }

    return 0;
}

/*
 * Map a r-g-b color to a color index.
 * We complement the colours, since we're using cmy anyway, and
 * because the buffering routines expect white to be zero.
 * Includes colour balancing, following HP recommendations, to try
 * and correct the greenish cast resulting from an equal mix of the
 * c, m, y, inks by reducing the cyan component to give a truer black.
 */

/* Simple black generation/under-color removal with BG(k) = UG(k) = k. YA. */

#define bg_and_ucr(c, c_v, m, m_v, y, y_v, k) \
    do { \
       register byte cv = c_v, mv = m_v, yv = y_v, kv; \
 \
        kv = (cv > mv ? mv : cv); \
        kv = (yv > k ? k : y); \
        y = yv - kv; m = mv - kv; c = cv -kv; k = kv; \
   } while (0)

static gx_color_index
gdev_pcl_map_rgb_color(gx_device *pdev, const gx_color_value cv[])
{
  gx_color_value r, g, b;
  r = cv[0]; g = cv[1]; b = cv[2];
  if (gx_color_value_to_byte(r & g & b) == 0xff)
    return (gx_color_index)0;         /* white */
  else {
    int correction = cprn_device->correction;
    gx_color_value c = gx_max_color_value - r;
    gx_color_value m = gx_max_color_value - g;
    gx_color_value y = gx_max_color_value - b;

    /* Colour correction for better blacks when using the colour ink
     * cartridge (on the DeskJet 500C only). We reduce the cyan component
     * by some fraction (eg. 4/5) to correct the slightly greenish cast
     * resulting from an equal mix of the three inks */
    if (correction) {
      ulong maxval, minval, range;

      maxval = c >= m ? (c >= y ? c : y) : (m >= y ? m : y);
      if (maxval > 0) {
        minval = c <= m ? (c <= y ? c : y) : (m <= y? m : y);
        range = maxval - minval;

#define shift (gx_color_value_bits - 12)
        c = ((c >> shift) * (range + (maxval * correction))) /
          ((maxval * (correction + 1)) >> shift);
      }
    }

    switch (pdev->color_info.depth) {
    case 1:
      return ((c | m | y) > gx_max_color_value / 2 ?
              (gx_color_index)1 : (gx_color_index)0);
    case 8:
      if (pdev->color_info.num_components >= 3)
#define gx_color_value_to_1bit(cv) ((cv) >> (gx_color_value_bits - 1))
        return (gx_color_value_to_1bit(c) +
                (gx_color_value_to_1bit(m) << 1) +
                (gx_color_value_to_1bit(y) << 2));
      else
#define red_weight 306
#define green_weight 601
#define blue_weight 117
        return ((((ulong)c * red_weight +
                  (ulong)m * green_weight +
                  (ulong)y * blue_weight)
                 >> (gx_color_value_bits + 2)));
    case 16:
        /* FIXME: Simple truncation is not ideal. Should round really. */
#define gx_color_value_to_5bits(cv) ((cv) >> (gx_color_value_bits - 5))
#define gx_color_value_to_6bits(cv) ((cv) >> (gx_color_value_bits - 6))
      return (gx_color_value_to_5bits(y) +
              (gx_color_value_to_6bits(m) << 5) +
              (gx_color_value_to_5bits(c) << 11));
    case 24:
      return (gx_color_value_to_byte(y) +
              (gx_color_value_to_byte(m) << 8) +
              ((ulong)gx_color_value_to_byte(c) << 16));
    case 32:
      { return ((c == m && c == y) ? ((ulong)gx_color_value_to_byte(c) << 24)
     : (gx_color_value_to_byte(y) +
        (gx_color_value_to_byte(m) << 8) +
        ((ulong)gx_color_value_to_byte(c) << 16)));
      }
    }
  }
  return (gx_color_index)0;   /* This never happens */
}

/* Map a color index to a r-g-b color. */
static int
gdev_pcl_map_color_rgb(gx_device *pdev, gx_color_index color,
                            gx_color_value prgb[3])
{
  /* For the moment, we simply ignore any black correction */
  switch (pdev->color_info.depth) {
  case 1:
    prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
    break;
  case 8:
      if (pdev->color_info.num_components >= 3)
        { gx_color_value c = (gx_color_value)color ^ 7;
          prgb[0] = -(c & 1);
          prgb[1] = -((c >> 1) & 1);
          prgb[2] = -(c >> 2);
        }
      else
        { gx_color_value value = (gx_color_value)color ^ 0xff;
          prgb[0] = prgb[1] = prgb[2] = (value << 8) + value;
        }
    break;
  case 16:
    { gx_color_value c = (gx_color_value)color ^ 0xffff;
      ushort value = c >> 11;
      prgb[0] = ((value << 11) + (value << 6) + (value << 1) +
                 (value >> 4)) >> (16 - gx_color_value_bits);
      value = (c >> 6) & 0x3f;
      prgb[1] = ((value << 10) + (value << 4) + (value >> 2))
        >> (16 - gx_color_value_bits);
      value = c & 0x1f;
      prgb[2] = ((value << 11) + (value << 6) + (value << 1) +
                 (value >> 4)) >> (16 - gx_color_value_bits);
    }
    break;
  case 24:
    { gx_color_index c = color ^ 0xffffff;
      prgb[0] = gx_color_value_from_byte((gx_color_value)(c >> 16));
      prgb[1] = gx_color_value_from_byte((gx_color_value)((c >> 8) & 0xff));
      prgb[2] = gx_color_value_from_byte((gx_color_value)(c & 0xff));
    }
    break;
  case 32:
#define  gx_maxcol gx_color_value_from_byte(gx_color_value_to_byte(gx_max_color_value))
    { gx_color_value w = gx_maxcol - gx_color_value_from_byte(color >> 24);
      prgb[0] = w - gx_color_value_from_byte((color >> 16) & 0xff);
      prgb[1] = w - gx_color_value_from_byte((color >> 8) & 0xff);
      prgb[2] = w - gx_color_value_from_byte(color & 0xff);
    }
    break;
  }
  return 0;
}

static gx_color_index
gdev_pcl_map_gray_color(gx_device *pdev, const gx_color_value cv[])
{
  gx_color_value r = cv[0];
  if (gx_color_value_to_byte(r) == 0xff)
    return (gx_color_index)0;         /* white */
  else {
    int correction = cprn_device->correction;
    gx_color_value c = gx_max_color_value - r;
    gx_color_value m = gx_max_color_value - r;

    /* Colour correction for better blacks when using the colour ink
     * cartridge (on the DeskJet 500C only). We reduce the cyan component
     * by some fraction (eg. 4/5) to correct the slightly greenish cast
     * resulting from an equal mix of the three inks */
    if (correction && c > 0) {
        c = ((c >> shift) * (c * correction)) /
             ((c * (correction + 1)) >> shift);
    }

    switch (pdev->color_info.depth) {
    case 1:
      return (c > gx_max_color_value / 2 ?
              (gx_color_index)1 : (gx_color_index)0);
    case 8:
      if (pdev->color_info.num_components >= 3)
        return (gx_color_value_to_1bit(c) +
                (gx_color_value_to_1bit(c) << 1) +
                (gx_color_value_to_1bit(c) << 2));
      else
        return ((((ulong)c * red_weight +
                  (ulong)m * (green_weight + blue_weight))
                 >> (gx_color_value_bits + 2)));
    case 16:
        /* FIXME: Simple truncation is not ideal. Should round really. */
      return (gx_color_value_to_5bits(c) +
              (gx_color_value_to_6bits(c) << 5) +
              (gx_color_value_to_5bits(c) << 11));
    case 24:
      return (gx_color_value_to_byte(c) +
              (gx_color_value_to_byte(c) << 8) +
              ((ulong)gx_color_value_to_byte(c) << 16));
    case 32:
      return ((ulong)gx_color_value_to_byte(c) << 24);
    }
  }
  return (gx_color_index)0;   /* This never happens */
}

static int
gdev_pcl_map_color_gray(gx_device *pdev, gx_color_index color,
                        gx_color_value *cv)
{
  /* For the moment, we simply ignore any black correction */
  switch (pdev->color_info.depth) {
  case 1:
    cv[0] = -((gx_color_value)color ^ 1);
    break;
  case 8:
      if (pdev->color_info.num_components >= 3)
        { gx_color_value c = (gx_color_value)color ^ 7;
          cv[0] = -(c & 1);
        }
      else
        { gx_color_value value = (gx_color_value)color ^ 0xff;
          cv[0] = (value << 8) + value;
        }
    break;
  case 16:
    { gx_color_value c = (gx_color_value)color ^ 0xffff;
      gx_color_value value = (c >> 6) & 0x3f;
      cv[0] = ((value << 10) + (value << 4) + (value >> 2))
        >> (16 - gx_color_value_bits);
    }
    break;
  case 24:
    { gx_color_index c = color ^ 0xffffff;
      cv[0] = gx_color_value_from_byte((gx_color_value)(c >> 16));
    }
    break;
  case 32:
#define  gx_maxcol gx_color_value_from_byte(gx_color_value_to_byte(gx_max_color_value))
    { gx_color_value w = gx_maxcol - gx_color_value_from_byte(color >> 24);
      cv[0] = w;
    }
    break;
  }
  return 0;
}

/*
 * Convert and expand scanlines:
 *
 * For devices with 3 components:
 *
 *       (a)	16 -> 24 bit   (1-stage)
 *       (b)	16 -> 32 bit   (2-stage)
 *   or  (c)	24 -> 32 bit   (1-stage)
 *
 * For devices with 4 components:
 *
 *       (a)    16 -> 32 bit   (1-stage)
 *       (b)     8 -> 32 bit   (2-stage)
 *  or   (c)    24 -> 32 bit   (1-stage)
 *
 */

static void
cdj_expand_line(word *line, int linesize, short cmyk, int bpp, int ebpp)
{
  int endline = linesize;
  byte *start = (byte *)line;
  register byte *in, *out;

  if (cmyk > 0) {
      if (bpp == 8) {
          in = start + endline;
          out = start + (endline *=  2);

          while (in > start) {
              register byte b0;
              register byte bs0, bs1, bs2, bs3;

              b0 = *--in;

              bs0 = b0 & 0x03;
              bs1 = (b0 >> 2) & 0x03;
              bs2 = (b0 >> 4) & 0x03;
              bs3 = (b0 >> 6) & 0x03;

              *--out = (bs0 << 2) + bs0 + (bs1 << 6) + (bs1 << 4);
              *--out = (bs2 << 2) + bs2 + (bs3 << 6) + (bs3 << 4);
          }
      }

      if (bpp == 24) {
          endline = (endline + 2) / 3;

          in = start + endline * 3;
          out = start + endline * 4;

          while (in > start) {
              register byte b0, b1, b2;

              b0 = *--in;
              b1 = *--in;
              b2 = *--in;

              *--out = (b0 << 2) + ((b0 >> 4) & 0x03);
              *--out = ((b1 & 0x0f) << 4) + ((b0 >> 6) << 2)
                  + ((b1 >> 2) & 0x03);
              *--out = ((b2 & 0x03) << 6) + ((b1 >> 4) << 2) + (b2 & 0x03);
              *--out = (b2 & 0xfc) + ((b2 >> 6) & 0x03);
          }
      } else if (ebpp == 32) {
          endline = (endline + 1) / 2;

          in = start + endline * 2;
          out = start + (endline *= 4);

          while (in > start) {
              register byte b0, b1;

              b0 = *--in;
              b1 = *--in;

              *--out = (b0 << 4) + ((b0 >> 4) & 0x07);
              *--out = (b0 & 0xf0) + ((b0 >> 4) & 0xf);
              *--out = (b1 << 4) + ((b1 >> 4) & 0x0f);
              *--out = (b1 & 0xf0) + ((b1 >> 4) & 0xf);
          }
      }
  } else /* cmyk > 0 */ {
      if (bpp == 16)              /* 16 to 24 (cmy) if required */
          { register byte b0, b1;
            endline = ((endline + 1) / 2);
            in = start + endline * 2;
            out = start + (endline *= 3);

            while (in > start)
                { b0 = *--in;
                  b1 = *--in;
                  *--out = (b0 << 3) + ((b0 >> 2) & 0x7);
                  *--out = (b1 << 5) + ((b0 >> 3)  & 0x1c) + ((b1 >> 1) & 0x3);
                  *--out = (b1 & 0xf8) + (b1 >> 5);
              }
        }

      if (ebpp == 32)             /* 24/32 (cmy) to 32 (cmyk) if required */
          { register byte c, m, y;
            endline = ((endline + 2) / 3);
            in = start + endline * 3;
            out = start + endline * 4;

            while (in > start)
                {
                  y = *--in;
                  m = *--in;
                  c = *--in;

                  if (c == y && c == m) {
                      *--out = 0, *--out = 0, *--out = 0;
                      *--out = c;
                  } else {
                      *--out = y, *--out = m, *--out = c;
                      *--out = 0;
                  }
              }
        }
  }
}

static int
cdj_put_param_int(gs_param_list *plist, gs_param_name pname, int *pvalue,
  int minval, int maxval, int ecode)
{	int code, value;
        switch ( code = param_read_int(plist, pname, &value) )
        {
        default:
                return code;
        case 1:
                return ecode;
        case 0:
                if ( value < minval || value > maxval )
                   param_signal_error(plist, pname, gs_error_rangecheck);
                *pvalue = value;
                return (ecode < 0 ? ecode : 1);
        }
}

static int
cdj_set_bpp(gx_device *pdev, int bpp, int ccomps)
{ gx_device_color_info *ci = &pdev->color_info;

  if (ccomps && bpp == 0) {
      if (cprn_device->cmyk) {
          switch (ccomps) {
              default:
		return_error(gs_error_rangecheck);
                  /*NOTREACHED*/
                  break;

              case 1:
                  bpp = 1;
                  break;

              case 3:
                  bpp = 24;
                  break;

              case 4:
                  switch (ci->depth) {
                      case 8:
                      case 16:
                      case 24:
                      case 32:
                          break;

                      default:
                          bpp = cprn_device->default_depth;
                          break;
                  }
                  break;
          }
      }
  }

  if (bpp == 0) {
      bpp = ci->depth;		/* Use the current setting. */
  }

  if (cprn_device->cmyk < 0) {

      /* Reset procedures because we may have been in another mode. */

      dev_proc(pdev, encode_color) = gdev_cmyk_map_cmyk_color;
      dev_proc(pdev, map_rgb_color) = NULL;
      dev_proc(pdev, decode_color) = gdev_cmyk_map_color_cmyk;

      if (pdev->is_open) gs_closedevice(pdev);
  }

  /* Check for valid bpp values */

  switch ( bpp )
    {
    case 16:
    case 32:
        if (cprn_device->cmyk && ccomps && ccomps != 4) goto bppe;
        break;

    case 24:
       if (!cprn_device->cmyk || ccomps == 0 || ccomps == 4) {
           break;
       } else if (ccomps == 1) {
           goto bppe;
       } else {

           /* 3 components 24 bpp printing for CMYK device. */

           cprn_device->cmyk = -1;
       }
       break;

    case 8:
        if (cprn_device->cmyk) {
            if (ccomps) {
                if (ccomps == 3) {
                    cprn_device->cmyk = -1;
                    bpp = 3;
                } else if (ccomps != 1 && ccomps != 4) {
                    goto bppe;
                }
            }
            if (ccomps != 1) break;
        } else {
            break;
        }

    case 1:
       if (ccomps != 1) goto bppe;

       if (cprn_device->cmyk && bpp != pdev->color_info.depth) {
           dev_proc(pdev, map_cmyk_color) = NULL;
           dev_proc(pdev, map_rgb_color) = gdev_cmyk_map_rgb_color;

           if (pdev->is_open) {
               gs_closedevice(pdev);
           }
       }
       break;

    case 3:
        if (!cprn_device->cmyk) {
            break;
        }

    default:
    bppe:  return_error(gs_error_rangecheck);
    }

    if (cprn_device->cmyk == -1) {
        dev_proc(pdev, map_cmyk_color) = NULL;
        dev_proc(pdev, map_rgb_color) = gdev_pcl_map_rgb_color;
        dev_proc(pdev, map_color_rgb) = gdev_pcl_map_color_rgb;

        if (pdev->is_open) {
            gs_closedevice(pdev);
        }
        if (pdev->is_open) {
            int			code;	/* Return code */
            gdev_space_params	sp;	/* Space parameter data */

            /* Reallocate memory for device */
            sp = ((gx_device_printer *)pdev)->space_params;

            if ((code = gdev_prn_reallocate_memory(pdev, &sp, pdev->width,
                                                   pdev->height)) < 0)
                return (code);
        }
    }

  switch (ccomps) {
      case 0:
          break;

      case 1:
          if (bpp != 1 && bpp != 8) goto cce;
          break;

      case 4:
          if (cprn_device->cmyk) {
              if (bpp >= 8) break;
          }

      case 3:
          if (bpp == 1 || bpp == 3 || bpp == 8 || bpp == 16
              || bpp == 24 || bpp == 32) {
              break;
          }

  cce:  default: return_error(gs_error_rangecheck);
  }

  if (cprn_device->cmyk) {
      if (cprn_device->cmyk > 0) {
          ci->num_components = ccomps ? ccomps : (bpp < 8 ? 1 : 4);
      } else {
          ci->num_components = ccomps ? ccomps : (bpp < 8 ? 1 : 3);
      }
      if (bpp != 1 && ci->num_components == 1) { /* We do dithered grays. */
          bpp = bpp < 8 ? 8 : bpp;
      }

      ci->max_color = (1 << (bpp >> 2)) - 1;
      ci->max_gray = (bpp >= 8 ? 255 : 1);

      if (ci->num_components == 1) {
          ci->dither_grays = (bpp >= 8 ? 256 : 2);
          ci->dither_colors = (bpp >= 8 ? 256 : bpp > 1 ? 2 : 0);
      } else {
          ci->dither_grays = (bpp > 8 ? 256 : 2);
          ci->dither_colors = (bpp > 8 ? 256 : bpp > 1 ? 2 : 0);
      }
  } else {
      ci->num_components = (bpp == 1 || bpp == 8 ? 1 : 3);
      ci->max_color = (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0);
      ci->max_gray = (bpp >= 8 ? 255 : 1);
      ci->dither_grays = (bpp >= 8 ? 256 : 2);
      ci->dither_colors = (bpp >= 8 ? 256 : bpp > 1 ? 2 : 0);
  }

  ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp);

  return 0;
}

/* new_bpp == save_bpp or new_bpp == 0 means don't change bpp.
   ccomps == 0 means don't change number of color comps.
   If new_bpp != 0, it must be the value of the BitsPerPixel element of
     the plist; real_bpp may differ from new_bpp.
*/
static int
cdj_put_param_bpp(gx_device *pdev, gs_param_list *plist, int new_bpp,
  int real_bpp, int ccomps)
{
        if (new_bpp == 0 && ccomps == 0)
          return gdev_prn_put_params(pdev, plist);
        else
          {
                gx_device_color_info save_info;
                int save_bpp;
                int code;

                save_info = pdev->color_info;
                save_bpp = save_info.depth;
#define save_ccomps save_info.num_components
                if ( save_bpp == 8 && save_ccomps == 3 && !cprn_device->cmyk)
                  save_bpp = 3;
                code = cdj_set_bpp(pdev, real_bpp, ccomps);
                if ( code < 0 ) {
                  param_signal_error(plist, "BitsPerPixel", code);
                  param_signal_error(plist, "ProcessColorModel", code);
                  return code;
                }
                pdev->color_info.depth = new_bpp;  /* cdj_set_bpp maps 3/6 to 8 */
                code = gdev_prn_put_params(pdev, plist);
                if ( code < 0 )
                  {	cdj_set_bpp(pdev, save_bpp, save_ccomps);
                        return code;
                  }
                cdj_set_bpp(pdev, real_bpp, ccomps);	/* reset depth if needed */
                if ((cdj->color_info.depth != save_bpp ||
                     (ccomps != 0 && ccomps != save_ccomps))
                    && pdev->is_open )
                  return gs_closedevice(pdev);
                return 0;
#undef save_ccomps
          }
}

/* This returns either the number of pixels in a scan line, or the number
 * of bytes required to store the line, both clipped to the page margins */
static uint
gdev_prn_rasterwidth(const gx_device_printer *pdev, int pixelcount)
{
  ulong raster_width = (ulong)(pdev->width -
    pdev->x_pixels_per_inch * (dev_l_margin(pdev) + dev_r_margin(pdev)));
  return (pixelcount ?
          (uint)raster_width :
          (uint)((raster_width * pdev->color_info.depth + 7) >> 3));
}

/* Functions for manipulation params strings */

static const byte*
paramValueToString(const stringParamDescription* params, int value)
{

    for (; params->p_name; ++params) {
        if (params->p_value == value) {
            return (const byte *)params->p_name;
        }
    }

    return (const byte*) 0;
}

static int
paramStringValue(const stringParamDescription* params,
    const byte* name, int namelen, int* value)
{

    for (; params->p_name; ++params) {
        if (strncmp(params->p_name, (char *)name, namelen) == 0 &&
            params->p_name[namelen] == 0) {
            *value = params->p_value;
            return 1;
        }
    }

    return 0;
}

static int
put_param_string(gs_param_list* plist,
    const byte* pname, gs_param_string* pstring,
    const stringParamDescription* params, int *pvalue, int code)
{

    int ncode;

    if ((ncode = param_read_string(plist, (char *)pname, pstring)) < 0) {
        param_signal_error(plist, (char *)pname, code = ncode);
    } else if (ncode == 1) {
        pstring->data = 0, pstring->size = 0;
    } else {
        int value = 0;

        if (paramStringValue(params, pstring->data, pstring->size,
            &value) == 0) {
            param_signal_error(plist, (char *)pname, code = gs_error_rangecheck);
        } else {
            *pvalue = value;
        }
    }

    return code;
}

static int
get_param_string(gs_param_list* plist,
    const byte* pname, gs_param_string* pstring,
    const stringParamDescription* params, int pvalue, bool persist, int code)
{

    int ncode;

    pstring->data = paramValueToString(params, pvalue);

    if (pstring->data == (byte*) 0) {
        param_signal_error(plist, (char *)pname, ncode = gs_error_unknownerror);
    } else {
        pstring->size = strlen((char *)pstring->data);
        pstring->persistent = persist;
    }

    if ((ncode = param_write_string(plist, (char *)pname, pstring)) < 0) {
        code = ncode;
    }

    return code;
}

/*
 * This taken from gsdparam.c. I hope it will be useable directly some day.
 *
 */

static int
cdj_param_check_bytes(gs_param_list *plist, gs_param_name pname,
                      const byte *str, uint size, bool is_defined)
{       int code;
        gs_param_string new_value;
        switch ( code = param_read_string(plist, pname, &new_value) )
          {
          case 0:
                if ( is_defined && new_value.size == size &&
                     !memcmp((const char *)str, (const char *)new_value.data,
                             size)
                   )
                  break;
                code = gs_note_error(gs_error_rangecheck);
                goto e;
          default:
                if ( param_read_null(plist, pname) == 0 )
                  return 1;
e:              param_signal_error(plist, pname, code);
          case 1:
                ;
          }
        return code;
}

/* This is original code. */

static int
cdj_param_check_float(gs_param_list *plist, gs_param_name pname, double fval,
                      bool is_defined)
{       int code;
        float new_value;
        switch ( code = param_read_float(plist, pname, &new_value) )
          {
          case 0:
                if ( is_defined && new_value == (float)fval)
                  break;
                code = gs_note_error(gs_error_rangecheck);
                goto e;
          default:
                if ( param_read_null(plist, pname) == 0 )
                  return 1;
e:              param_signal_error(plist, pname, code);
          case 1:
                ;
          }
        return code;
}

/* The following dithering algorithm has been kindly given to me (YA) by
 * Klaus-Gunther Hess, I just adapted it for use with the code here. */

/*

(From KGH:)

Just about the features of the code:

    - Stored Color-Values are BYTES in the order C-M-Y-K.
      (Indices need to change with gdevcdj.c)

    - There are individual THRESHOLDs and SPOTSIZEs for
      the color-components. The following relation should
      be maintained:
                 SPOTSIZE = 2 * THRESHOLD + 1
      (The internal calculation is dedicated for limiting
       ink-density at the 720x720DpI-Resolution of the
       Epson-Printers, without loss of dynamic color-range)

    - In addition to that there are EMIN & EMAX-Values
      for the components. The Values are computed from
      the dithering-algorithm and can be replaced by
      constants, if neither the implementation nor
      THRESHOLD and SPOTSIZE can change.

    - The algorithm is tuned for speed. (K-only, if gray-
      levels are detected, with EMIN/EMAX-clipping of
      stored CMY-Errors. [Notice: cerr, merr, yerr are
      *not* reset to zero! Clearing them would cause
      regular patterns & "Halos" to appear!])

*/

/*
 * Macros, that represent the undisturbed dithering-algorithm
 *
 * FSerror:   compute the desired Value
 * FSdecide:  decision based on the value computed by FSerror
 * FSdiffuse: distribute remaining error among pixels
 */

#define FSerror(Val,Erow,Ecol) (Val + Erow + ((7 * Ecol)>>4))

#define FSdecide(Error,Threshold,Spotsize,Pixel,Bit) \
         if(Error > Threshold) {\
            Pixel |= Bit;\
            Error -= Spotsize;\
         }

#define FSdiffuse(Error,Erow,Ecol,Eprev)\
         Eprev  += (3 * Error + 8)>>4;\
         Erow    = (5 * Error + Ecol + 8)>>4;\
         Ecol    = Error;

/*
 * some aliases for values from the device-structure
 */
#define DIRECTION    direction[0]
#define CMYK_THRESHOLD(I) threshold[I]
#define SPOTSIZE(I)  spotsize[I]
#define EMIN(I)      emin[I]
#define EMAX(I)      emax[I]
#define NPIXEL       (plane_size * 8)

#define IDX_C	     1
#define IDX_M	     2
#define IDX_Y	     3
#define IDX_K	     0

#define ODX_C	     2
#define ODX_M	     1
#define ODX_Y	     0
#define ODX_K	     3

static int
bjc_fscmyk(byte** inplanes, byte* outplanes[4][4], int** errplanes,
    int plane_size, int scan) {

    byte* err = (byte*) errplanes[0];

/* =========================================================== */
   if(scan < 0) { /* scan <   0 -> initialize private buffer */
/* =========================================================== */

      int p,i,v;
      int *direction,*threshold,*spotsize,*emin,*emax;
      int *errv,*errc;
/*
 * allocate the error-buffer
 */
      /*KGHorig
      i = 4 * (5 + 1 + 1 + sd->stc.prt_pixels + 1) * sizeof(errv[0]);
      if((sd->stc.err_size < i) || (NULL == sd->stc.err)) {
         if(NULL != sd->stc.err)
            gs_free(sd->stc.err,sd->stc.err_size,1,"stcm/err");
         sd->stc.err_size = i;
         sd->stc.err = gs_malloc(sd->stc.err_size,1,"stcm/err");
         if(sd->stc.err == NULL) return_error(gs_error_VMerror);
      }
      */

      direction = (int *) err;
      threshold = direction + 4;
      spotsize  = threshold + 4;
      emin      = spotsize  + 4;
      emax      = emin      + 4;
      errc      = emax      + 4;
      errv      = errc      + 2*4;
/*
 * compute initial values
 */
      DIRECTION = -1;
      for(i = 0; i < 4; ++i) {
         float maxv = 1.0;
         /*KGHorig
         int j;
         if((sd->stc.xfer[i].size < 1) || (sd->stc.xfer[i].data == NULL)) {
            maxv = 1.0;
         } else {
            maxv = 1.0/255.0;
            for(j = 0; j < sd->stc.xfer[i].size; ++j)
               if(maxv < sd->stc.xfer[i].data[j])
                 maxv = sd->stc.xfer[i].data[j];
         }
         */
         CMYK_THRESHOLD(i) = (int)(127.0 / maxv + 0.5);
         SPOTSIZE(i)  = ((int) CMYK_THRESHOLD(i)<<1)+1;
         errc[3] = 0;
         FSdiffuse(CMYK_THRESHOLD(i),errv[0],errc[0],errv[-4]);
         FSdiffuse(CMYK_THRESHOLD(i),errv[0],errc[0],errv[-4]);
         EMAX(i) = errv[0];
         errc[0] = 0;
         FSdiffuse((-CMYK_THRESHOLD(i)),errv[0],errc[0],errv[-4]);
         FSdiffuse((-CMYK_THRESHOLD(i)),errv[0],errc[0],errv[-4]);
         EMIN(i) = errv[0];
      }

#ifdef CDJ_DEBUG_FS
      for(i = 0; i < 4; ++i) errprintf_nomem(
         "CMYK_THRESHOLD(%d)=%5d, spotsize(%d)=%5d, emin(%d)=%5d, emax(%d)=%5d\n",
         i,CMYK_THRESHOLD(i),i,SPOTSIZE(i),i,EMIN(i),i,EMAX(i));
#endif

      for(i = 0; i < 4; ++i) errc[i] = 0;

      for(p = 0; p < NPIXEL; ++p) {
         for(i = 0; i < 4; ++i) {
            /*KHGOrig
            if(sd->stc.flags & STCDFLAG0) v = 0;
            */
            if (0) v = 0;	/* Must provide a default for that. */
            else                      v = (rand() % SPOTSIZE(i)) - CMYK_THRESHOLD(i);
            FSdiffuse(v,errv[i],errc[i],errv[i-4]);
         }
         errv += i;
      }

/* =========================================================== */
   } else {        /* scan >=  0 -> scanline-processing       */
/* =========================================================== */

      int w,p,dir,thedir;
      byte *out[4],pixel[4],bit;
      /*KGHorig
      int  *width     = outplanes[scan];
      */
      int  *direction = (int *) err;
      int  *threshold = direction + 4;
      int  *spotsize  = threshold + 4;
      int  *emin      = spotsize  + 4;
      int  *emax      = emin      + 4;
      int  *errc      = emax      + 4;
      int  *errv      = errc      + 2*4;
      int   kerr,cerr,merr,yerr;

      byte* in;

      /*KGHorig
      if(sd->stc.flags & STCDFLAG1) {
      */
      if (0) {			/* Eventually will provide a flag for this. */
         cerr = merr = yerr = kerr = 0;
      } else {
         cerr = errc[0];
         merr = errc[1];
         yerr = errc[2];
         kerr = errc[3];
      }

      out[0]   = outplanes[scan + 2][ODX_C];
      out[1]   = outplanes[scan + 2][ODX_M];
      out[2]   = outplanes[scan + 2][ODX_Y];
      out[3]   = outplanes[scan + 2][ODX_K];
      pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0;

      if(DIRECTION < 0) { /* scan == 0, run backward */
         w      = NPIXEL;
         in     = inplanes[2] + 4 * (NPIXEL - 1);
         errv  += (w-1)<<2;
         dir    = -4;
         /*KGHorig
         if(w > 8) for(p = 0; p < 4; ++p) out[p] += (w-1)>>3;
         */
         thedir = -1;
         for (p = 0; p < 4; ++p) {
             out[p] += plane_size - 1;
         }
      } else {      /* run forward */
         w      = 1;
         in      = inplanes[3] - 4 * NPIXEL;
         dir    = 4;
         thedir = 1;
         for (p = 0; p < 4; ++p) {
             out[p] -= plane_size;
         }
      }             /* run backward/forward */

      /*KGHorig
      if(0 == (sd->stc.flags & STCDFLAG1)) DIRECTION = -DIRECTION;
      */
      if (1) DIRECTION = -DIRECTION; /* Scan in other direction. */

      bit = 0x80>>((w-1) & 7);
      w   = (w+7)>>3;

      for(p = NPIXEL; p; --p) { /* loop over pixels */

         int cmy = in[IDX_C] | in[IDX_M] | in[IDX_Y];
         int kv  = FSerror(in[IDX_K],errv[3],kerr);
         int cv;

         FSdecide(kv,CMYK_THRESHOLD(3),SPOTSIZE(3),pixel[3],bit);

         if(cmy) {

            if(pixel[3] & bit) { /* black known to fire */

               FSdiffuse(kv,errv[3],kerr,errv[3-dir]);

               cv  = FSerror(in[IDX_C],errv[0],cerr);
               cv -= SPOTSIZE(0);
               if ((cv+CMYK_THRESHOLD(0)) < 0) cv = -CMYK_THRESHOLD(0);
               FSdiffuse(cv,errv[0],cerr,errv[0-dir]);

               cv  = FSerror(in[IDX_M],errv[1],merr);
               cv -= SPOTSIZE(1);
               if ((cv+CMYK_THRESHOLD(1)) < 0) cv = -CMYK_THRESHOLD(1);

               FSdiffuse(cv,errv[1],merr,errv[1-dir]);

               cv  = FSerror(in[IDX_Y],errv[2],yerr);
               cv -= SPOTSIZE(2);
               if ((cv+CMYK_THRESHOLD(2)) < 0) cv = -CMYK_THRESHOLD(2);
               FSdiffuse(cv,errv[2],yerr,errv[2-dir]);

            } else {

               cv  = FSerror(in[IDX_C],errv[0],cerr);
               FSdecide(cv,CMYK_THRESHOLD(0),SPOTSIZE(0),pixel[0],bit);
               FSdiffuse(cv,errv[0],cerr,errv[0-dir]);

               cv  = FSerror(in[IDX_M],errv[1],merr);
               FSdecide(cv,CMYK_THRESHOLD(1),SPOTSIZE(1),pixel[1],bit);
               FSdiffuse(cv,errv[1],merr,errv[1-dir]);

               cv  = FSerror(in[IDX_Y],errv[2],yerr);
               FSdecide(cv,CMYK_THRESHOLD(2),SPOTSIZE(2),pixel[2],bit);
               FSdiffuse(cv,errv[2],yerr,errv[2-dir]);

               if(pixel[0] & pixel[1] & pixel[2] & bit) {
                  pixel[0] &= ~bit;
                  pixel[1] &= ~bit;
                  pixel[2] &= ~bit;
                  pixel[3] |=  bit;
                  kv -= SPOTSIZE(3);
                  if ((kv+CMYK_THRESHOLD(3)) < 0) kv = -CMYK_THRESHOLD(0);
                  FSdiffuse(kv,errv[3],kerr,errv[3-dir]);
               }
           }

         } else {

            FSdiffuse(kv,errv[3],kerr,errv[3-dir]);

            if(     errv[0] > EMAX(0)) errv[0] = EMAX(0);
            else if(errv[0] < EMIN(0)) errv[0] = EMIN(0);

            if(     errv[1] > EMAX(1)) errv[1] = EMAX(1);
            else if(errv[1] < EMIN(1)) errv[1] = EMIN(1);

            if(     errv[2] > EMAX(2)) errv[2] = EMAX(2);
            else if(errv[2] < EMIN(2)) errv[2] = EMIN(2);

         }

/*
 * Adjust indices
 */
         bit   = dir > 0 ? (bit>>1) : (bit<<1);
         if(bit == 0) {
            /*KGHorig
            if(((*out[0]  = pixel[0]) != 0) && (width[0] < w)) width[0] = w;
            if(((*out[1]  = pixel[1]) != 0) && (width[1] < w)) width[1] = w;
            if(((*out[2]  = pixel[2]) != 0) && (width[2] < w)) width[2] = w;
            if(((*out[3]  = pixel[3]) != 0) && (width[3] < w)) width[3] = w;
            */
            *out[0] = pixel[0];
            *out[1] = pixel[1];
            *out[2] = pixel[2];
            *out[3] = pixel[3];
            out[0] += thedir; out[1] += thedir;
            out[2] += thedir; out[3] += thedir;
            pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0;

            if(dir > 0) bit = 0x80;
            else        bit = 0x01;
            w    += dir>>2;
         }

         in += dir;
         errv += dir;
      }                                         /* loop over pixels */

      /*KGHorig
      if(0 == (sd->stc.flags & STCDFLAG1)) {
      */
      if (1) {
         cerr = errc[0] = cerr;
         merr = errc[1] = merr;
         yerr = errc[2] = yerr;
         kerr = errc[3] = kerr;
      }

/* =========================================================== */
   }                  /* initialization or scanline-Processing */
/* =========================================================== */

   return 0;
}