summaryrefslogtreecommitdiff
path: root/gs/contrib/gdevlx50.c
blob: 008a7cc37561b52aa6d129ad425cb71135a35403 (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
/*

   Copyright (C) 1999,2000  Peter B. West <pbwest@netscape.net>
   Portions Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 U.S.A.

   This program may also be distributed as part of Aladdin Ghostscript,
   under the terms of the Aladdin Free Public License (the "License").

   Every copy of Aladdin Ghostscript must include a copy of the
   License, normally in a plain ASCII text file named PUBLIC.  The
   License grants you the right to copy, modify and redistribute
   Aladdin Ghostscript, but only under certain conditions described in
   the License.  Among other things, the License requires that the
   copyright notice and this notice be preserved on all copies.

  The author, Peter B. West, may be contacted via e-mail as
  pbwest@netscape.net
  or by post at
  1/32 Bent Street
  Toowong, Brisbane,
  QLD, AUSTRALIA. 4066

  http://www.powerup.com.au/~pbwest

  Based on the driver for the Lexmark 5700, pioneered by
    Stephen Taylor  setaylor@ma.ultranet.com  staylor@cs.wpi.edu
  and on other pioneering work on the Lexmark 7000 by 
    Henryk Paluch <paluch@bimbo.fjfi.cvut.cz>
 
  My heartfelt thanks to these blokes (i.e. guys, fellows).

  I should also like to thank Marija Svilans, through whom I discovered the
  work of Stephen and Henryk.

  My deepest praise and thanksgiving is reserved for the Author of all that
  is Good, Beautiful and True; my Lord and my God, Jesus Christ.
  "...all things were made through Him, and without Him was not anything
   made that was made."  John 1:3
 
  Peter B. West pbwest@netscape.net
  (First release: Friday, 8th October, 1999.)
  
*/

/*$Id: gdevlx50.c,v 1.4 2002/07/30 18:53:21 easysw Exp $*/
/*
 * Lexmark 5000 ink-jet printer driver for Ghostscript
 *
 * Black and colour cartridges supported - NO PHOTO CARTRIDGE support yet.
 *
 * defines the lx5000 device for printing in black-and-white and colour
 * at 600x600 (default), 1200x600 & 300x600 dpi, unidirectional.
 *
 * I use the command
 * gs -sOutputFile=/dev/lp0 -sDEVICE=lx5000 file.ps
 *
 * For black-only printing, use
 * gs -sOutputFile=/dev/lp0 -sDEVICE=lx5000 -dBitsPerPixel=1 file.ps
 * For compatibility with earlier versions of the code, the following
 *  equivalent command is still supported.
 * gs -sOutputFile=/dev/lp0 -sDEVICE=lx5000 -dCMYK=false file.ps
 *
 * For 1200x600 printing, add the argument
 * -r1200x600
 * For 300x600 printing, add the argument
 * -r300x600
 *
 * A number of integer valued options are available.
 * These options are accessed as arguments of the form
 *	-doptionname=optionvalue
 * 	Optionname		Optionvalue range
 *	BitsPerPixel		1 or 4
 *	HeadSeparation		1 - 30
 *	AlignA			0 - 30
 *	AlignB			0 - 15
 *	DryingTime		0 - 60
 *
 * BitsPerPixel currently only defines Black-only (1) or CMYK (4).
 *  -dBitsPerPixel=4 is the default, and is eauivalent to the older form
 *  -dCMYK=true
 * [ HeadSeparation varies from print-cartridge to print-cartridge and
 *  16 (the default) usually works fine. Stephen Taylor]
 *
 * AlignA is the horizontal alignment adjustment between the colour and
 *  the black pens.  This distance will vary with each pair of cartridges.
 *
 * AlignB is the vertical alignment distance between the colour and black
 *  pens, and will also vary with each pair of cartridges.
 *
 * These values can be determined by running the alignment test; i.e,
 * by writing the printer data file blckalgn.out to the device.
 *
 *  cat blckalgn.out >/dev/lp0
 *
 * This file is now part of the distribution for the driver.
 *
 * DryingTime is used to include a delay after printing a page, to allow the
 * page to dry to some extent before a subsequent page is printed and fed on
 * top of it.  The range of values is 0-60.
 * 
 * Ancilliary files:
 *
 * blckalgn.out		As mentioned above,this is a file of printer commands
 *			which will print the allignment page for the 5000.
 *   Usage:  cat blckalgn.out >/dev/lp0 (or appropriate printer device)
 *
 * blckhcln.out		This is a file of printer commands to print the head
 *			cleaning sequences.
 *   Usage:  cat blckhcln.out >/dev/lp0
 *
 * showcarts.out	This is a file with the printer command to move the
 *			printer cartridges into position for changing a
 *			cartridge.
 *   Usage:  cat showcarts.out >/dev/lp0
 *
 * parkcarts.out	This is a file with the printer command to move the
 *			printer cartridges back to a parked position.
 *   Usage:  cat parkcarts.out >/dev/lp0
 *
 ==================   K N O W N   P R O B L E M S   =======================
 
  PROBLEMS WITH 1200dpi horizontally

  This release was delayed by a bug in the production of black output
  (including black pixels in colour output) at 1200 Xdpi which
  manifested as the truncation of black swipes.  In the RedHat test
  page, for example, the black background to the hat in the RedHat
  logo would abruptly halt, and no more of the black swipe would be
  printed.  This was difficult to track down, because of the sheer
  size of the otuput file produced, and I ran out of time to find and
  fix it, although I was beginning to suspect a problem with the
  parallel driver.  (It is always tempting to blame someone else.)

  When I recently returned to this problem, and tried the test print
  again on the unchanged binary, the bug had disappeared.  In the
  meantime, I had added more physical memory to the system, and
  upgraded my 6.0 system to the 6.1 kernel (2.2.12), the 6.1 glibc
  version (2.1.2-11) and miscellaneous other 6.1 rpms.

  All was not lost, however.  When I tested black-only printing at
  1200dpi, the problem re-appeared.  I have come to the conclusion
  that the 5000 simply cannot print at 1200dpi.  I have tested this by
  printing solid bars of black at 600 and 1200 dpi.  The 600dpi bar
  (all nozzles on for most of the width of the paper) produces the
  expected bar.  Doing the same thing at 1200dpi produces movement of
  the printhead, but only the beginnings of a bar.  Reducing the
  demand by printing from only every second nozzle get an intermediate
  result; the printing of the bar starts, but the nozzle lines trail
  off and dry up at various points in the traversal of the
  page. leaving an effect somthing like a tattered flag.

  How does the Windows driver do it?  When printing a high density
  black-only page, the driver prints repeated overlays offset by half
  the height of the print swathe.  Although I have not analysed these
  swathes in detail, I suspect that the function of this "halving" is
  simply to halve the printed pixel density, and thereby eliminate the
  problem discussed above.

  I have taken the "half-height" printing method for granted ever
  since I read a passing reference to it in the H-P Jounal articles on
  PPA, but I misunderstood it.  I had thought that it was a way of
  performing a pseudo-doubling of the VERTICAL print density by
  overprinting adjacent pairs of lines.  I was originally unaware that
  the 5000 supported native 1200x600 resolution.  The problem, as far
  as I can determine, is that the 5000 does NOT support such printing.
  And if the printer is not going to support 1200dpi horizontally,
  neither am I.

  PROBLEMS WITH PRINTER DATA "JAMMING"

  I am experiencing an annoying problem with my 5000 on my AST Bravo
  P100 (yes, really) system under RedHat linux (6.0 with kernel
  2.2.12-20 and glibc 2.1.2-11).  The printer frequently fails to
  respond between print jobs, and sometimes between pages of the same
  print job.  The only way to wake it up is to power cycle the
  printer.  I have no idea why this is, of whether it is a general
  problem.  I would appreciate all feedback on this problem.

  I have received feedback on this from Bob Clark
  <rlc@c317689-a.scllg1.pa.home.com>.  He realised that the printer
  was queueing status data to be read from the same device.  When this
  queue (which appears to be 64 bytes long) fills up, the printer
  stalls until it is reset (by a power cycle or by some as yet
  undiscovered escape sequence), or until the status data is read.
  That's the problem, but I do not have a clean solution yet.

 *=================================================
   Other requirements for compiling this driver:-
  =================================================
   The Ghostscript 6.0 source distribution, available from
      http://www.cs.wisc.edu/~ghost/aladdin/get600.html

   This includes reasonable html documentation on compiling GS.

   For users of RPM-based systems like Red Hat, I have a source RPM
   available which automates most of the process of compiling and
   installing Ghostscript.  A discussion of the details of compiling
   and installing with this source RPM is available on my web page at
   <http://localhost:80/~pbw/ghostscript/compileGS.html>.

   The RPM includes a number of patch files which are applied to the
   pristine Ghostscript 6.0 sources.  Users of non-RPM systems can
   extract these patches from the cpio archive which is also noted in
   the instructions.

   I have compiled and tested on a RedHat 6.0 i386 system, using
   egcs-2.91.66 19990314/Linux (egcs-1.1.2 release), so the relevant
   system-specific .mak file for me was unix-gcc.mak.  The .mak file
   of general application is contrib.mak.

 */

/************************************************************************
 *			I N C L U D E   F I L E S			*
 ************************************************************************/
#include <unistd.h>
#include "gdevprn.h"
#include "gsparam.h"
#include "gsmalloc.h"

/************************************************************************
 *		     R C S   I D E N T   S T R I N G S			*
 ************************************************************************/
const char Id[]		=
	"$Id: gdevlx50.c,v 1.4 2002/07/30 18:53:21 easysw Exp $";
const char RCSFile[]	= "$RCSFile$";
const char Revision[]	= "$Revision: 1.4 $";
const char Author[]	=
	"Peter B. West   pbwest@netscape.net\n"
	"$Author: easysw $\n";
/*===========================================================================
 In gxdevice.h, the default width and height are expressed in 10ths of an
 inch.  The conversion to mm will therefore, not be exact.

 These values are immediately converted to (x,y) pixels for storage in the
 width and height fields defined in the gx_device_common macro which is
 defined in gxdevcli.h.  These in turn are converted to points and stored
 in the float MediaSize[2] array in gx_device_common by the
 std_device_part_2_ macro in gxdevice.h.

 The same macro stores the (x,y)dpi values in the float arrays
 HWResolution[2] and MarginsHWResolution[2].

 The macro prn_device_std_margins_body in gdevprn.h provides for X and Y
 offsets, as well as left, bottom, right and top margin values.  X & Y are
 expressed in inches, as are all the other margin values.

 The X & Y offsets are known as Margins in gx_device_common.	They provide
 the offset from the physical page corner (presumably top left, but may
 depend on the position of 0,0) to 0,0 in the device co-ordinate system.
 These values are converted by prn_device_std_margins_body into _negated_
 (x,y)dpi and stored in the float Margins[2] array.

 The left, bottom, right and top margin values are converted to points,
 and stored in the float HWMargins[4] array.  Note that these values are
 defined relative to the physical paper, not the co-ordinate system.  The
 Margins array is used to transform from physical page offsets to the
 device co-ordinate system.

 The implications of this are that the X & Y offset from page corner to
 0,0 should be defined as accurately as possible.  The other four margins
 should then be defined for a comprehensive coverage of the physical page.
 The definitions of paper size given in 10ths of an inch in gxdevice.h are
 a constraint.

 I presume that the intention of the transformation from 0,0 to
 the physical page is to leave the defined width and height mapped
 onto the physical page, and for the left and top margins to then restore
 those two dimensions of the accessible page.  The result would be, if the
 values of X & Y offset precisely reversed the values of left and top
 HWMargins, that the top left hand printable point would be at device
 co-ordinates 0,0.  The bottom and right HWMargins would then provide the
 necessary clipping of the co-ordinate system to keep output within the
 printable area.

 In fact, this is the way the x, Y offsets and the left and top margin
 values are set up in the default prn_device_std_body macro in gdevprn.h.
 In that macro, the X, Y offset values (expressed in inches) are simply
 copied from the left and top margins.

 What I have found with my 5000, is that it can print on virtually the
 whole area of the paper.  This shows some remarkable paper handling cap-
 ability.  This capability almost demands that 0,0 in the device co-ordin-
 ate system be _outside_ the paper area.  It is.  On my printer, the X
 offset from the physical page top left corner is 48 600ths of an inch -
 a little less than 6 points.

 The physical paper is a little narrower than 8.3 inches (210.82 mm), so
 the attempt to print right to the 8.3" margin falls short by about 16
 600ths, or about 2 points.  When 0,0 falls _within_ the page, the combin-
 ed margin clipping ensures that the driver will never issue printer com-
 mands which exceed the defined width and height of the page.  What
 happens when 0,0 is outside the page?  This implies, especially when the
 hardware is capable of printing to the edges of the paper, that the
 driver may issue print commands whose offsets exceed the defined width of
 the page, in order to print to the right edge of the paper.	I don't know
 whether Ghostscript will allow this.

                   Print width
              |<------------ 8.3" ----------->|
              |                               |
              |                               |
              +-------------- ~ --------------+
                                              |<-->| 2 pts
              |      |<----- Paper width --------->|
        6 pts |<---->|                             |
                     +-------------- ~ ------------+

 In this case, the X offset is going to be negative, and the device co-
 ordinate value of X for the left edge of the paper is going to almost 50
 600ths.  It is a good idea to apply some restrictions on printing to the
 edge of the paper, so a HWMargin should be applied.

 The problem with setting the margins is this: Ghostscript knows where the
 top and left sides of the page are relative to 0,0, because it is spec-
 ifically informed of these values.  It has no way of knowing where the
 bottom and right of the page are, except from the height and width values.
 These are only provided to an accuracy of a tenth of an inch, so the
 assumed right and bottom margins may fall short of or overrun the edge of
 the paper.

 In these circumstances, it seems a good idea to provide HWMargin values
 designed to enforce a uniform margin around all of the edges.  The left &
 top margins can be given directly; the others must be worked out so that
 when applied with reference to the height and width parameters, they
 result in the same margin.

 For example, on my 5000, 0,0 is approximately 48/600" (about 6 points) to
 the left of the physical page.  When I print at an offset from 0 of
 8.3", the default A4 width, the print is approximately 32/600" (about 4
 points) inside the right edge of the page.  That is, 8.3" is (48-32)/600"
 too wide for the physical page.

 4 points (4/72") is 0.05555", and 32/600" is 0.053333".  If I set a left
 HWMargin of 0.053", and Ghostscript obliges by mapping a new printable
 origin 4 points inside the left edge of the paper, with 8.3" of print-
 able line to the right of it, my left margin will need to account for the
 extra 16/600" that GS thinks exists on the right, and for the actual
 margin of 0.053" that I want to establish on the right.  I think.

 The interest in the top margin is that it seems to be keyed to the colour
 pens.  These form three blocks of nozzles vertically aligned in the
 colour print head.

 ---    -- +-----+                 v Alignment B
  ^     ^  |     | v              --- +-----+ ---
  |     64 |  C  | -- --- +-----+ --- |     |  ^
  |  v  v  |     | 40  ^  |     |  ^  |     |  |
  |  -- -- +-----+ --  |  |     |     |     |  |
  |  24            ^   |  |     |     |     |  |
  |  -- -- +-----+     |  |     |     |     |  |
  |  ^  ^  |     |     |  |     |     |     |
 240    64 |  M  |    192 |  K  |     |  K  | 208
  |  v  v  |     |     |  |     |     |     |
  |  -- -- +-----+     |  |     |     |     |  |
  |  24            v   |  |     |     |     |  |
  |  -- -- +-----+ --  |  |     |     |     |  |
  |  ^  ^  |     | 40  v  |     |     |     |  |
  |     64 |  Y  | -- --- +-----+     |     |  v
  v     v  |     | ^                  +-----+ ---
 ---    -- +-----+

 The colour pens have 64 nozzles each, and the black has 208.  Black can
 also be driven in colour compatibility mode, when it uses 192 nozzles
 only, the same as the total number of colour nozzles.  In order to align
 the black nozzles with the colour, an alignment value is specified in the
 initialization sequence for the page.  This value is in turn determined
 by printing an alignment page under control of the printer driver (See
 ancilliary files above.)

 Note that when an odd number is specified as Alignment B, an odd numbered
 nozzle will be mapped as nozzle 0 of the 192 nozzle black pen, so the
 even/odd association of nozzle numbers will be reversed; i.e., nozzle 0
 will become and ODD nozzle.

 The paper handler seems to assume that the paper load position is with
 the page lined up immediately before the lowest nozzle of the YELLOW pen.
 (Note that the paper is being fed in from the bottom of the printheads.)
 When I issue a paper feed instruction of 480 (240x2) 1200ths, and print a
 three-colour bar, the top of the CYAN pen is just on the physical page.
 Efectively, the Y component of 0,0 is right at the top of the page, with
 respect to the YELLOW pen.  Y=0 with respect to the 192 nozzle black pen
 is always constant, and with respect to the 208 pen, can be determined
 from Alignment B.


 *===========================================================================*/

/************************************************************************
 *			B A S I C   M A C R O S				*
 ************************************************************************/

#define A5_11_5000	1
#define A5_11_5700	2


#define ALIGN_A_DEF	15
#define ALIGN_A_OFFSET	5	/* in pageinit escape sequence */
#define ALIGN_B_DEF	8
#define ALIGN_B_OFFSET	6	/* in pageinit escape sequence */

#define HEADSEP_DEF	16
				/* number of pixels between even columns in */
				/* output and odd ones */
#define DRY_TIME_DEF	0	/* Default pause for page to dry */


/************************************************************************
 *	     P R I N T E R   T Y P E   D E F I N I T I O N S	 	*
 ************************************************************************/

#ifndef LX_BI
#   define LX_UNI
				/* or define LX_BI */
#endif
#define MIN_LX5000_X	300
#define MAX_LX5000_X	1200
#define DEF_LX5000_X	600
#define MIN_LX5000_Y	600
#define MAX_LX5000_Y	1200
#define DEF_LX5000_Y	MIN_LX5000_Y

#ifndef LX5000_XDPI
#   define LX5000_XDPI	DEF_LX5000_X
#endif
#ifndef LX5000_YDPI
#   define LX5000_YDPI	DEF_LX5000_Y
#endif

				/* Multiply lines by FEED_FACTOR when feeding
				   paper.  The 5000 feeds in 1200ths. */
#define FEED_FACTOR	( 1200/LX5000_YDPI )

#define LX5000_XOFFSET_TO_0_0_XDPI	(-48.0)
#define LX5000_YOFFSET_TO_0_0_YDPI	(0.0)

#define LX5000_XOFFSET_TO_0_0	( LX5000_XOFFSET_TO_0_0_XDPI/LX5000_XDPI )
#define LX5000_YOFFSET_TO_0_0	( LX5000_YOFFSET_TO_0_0_YDPI/LX5000_YDPI )

/* The theory */
#define LX5000_LEFT_HWMARGIN_INS	0.053
#define LX5000_BOTTOM_HWMARGIN_INS	0.067
#define LX5000_RIGHT_HWMARGIN_INS	0.08
#define LX5000_TOP_HWMARGIN_INS		0.053
/**/
/* The practice
??
*/

#define LINE_PAD_BYTES	8
				/* number of bytes of padding on each end of */
				/* scan line to account for head separation */
				/* N.B. Keep this value 16-bit aligned.  */

#define RIGHTWARD	0
#define LEFTWARD	1

#define SWIPE_WORD_BITS	16
				/* Bits per directory word */
#define BLACK_NOZZLES	208
				/* height of printhead in pixels */
#define _1COLOUR_NOZZLES 64
				/* Height of one colour pen in pixels */

				/* Height of colour compatible black swipe */
#define BLK_COLOUR_NOZZLES	(_1COLOUR_NOZZLES * 3)

				/* number of shorts described by each */
				/* full black column directory */
#define BLK_SWIPE_WORDS		( BLACK_NOZZLES / SWIPE_WORD_BITS )
#define COLOUR_SWIPE_WORDS	( BLK_COLOUR_NOZZLES / SWIPE_WORD_BITS )
				/* and for a colour swipe command */
#define _1COLOUR_WORDS		( _1COLOUR_NOZZLES / SWIPE_WORD_BITS )
				/* and for a single colour pen */

#define BLK_DIRECTORY_MASK	(( ~0 << BLK_SWIPE_WORDS ) ^ ~0 )
#define COLOUR_DIRECTORY_MASK	(( ~0 << COLOUR_SWIPE_WORDS ) ^ ~0 )
#define DIRECTORY_TYPE_BIT	0x2000
				/* Directory type: Set = normal;
					   unset = repeat compression */

#define COLOUR_PEN_GAP	24	/* Defined in equivalent nozzles.  This will */
				/* also be the offset from the top of the */
				/* first colour pen to the top of the colour */
				/* compatible black pen. */
#define COLOUR_PEN_DIFF	( _1COLOUR_NOZZLES + COLOUR_PEN_GAP )
				/* The difference between corresponding */
				/* nozzle positions (e.g. bottom nozzle) on */
				/* contiguous colour pens. */

				/* The intial pen scanline positions are */
				/* defined with reference to the first */
				/* printable line (line 0) on the page.  The */
				/* initial colour cartridge position is */
				/* immediately above line 0. */
#define INITIAL_YELLOW_BOTTOM_LINE	( -1 )

				/*  The pen buffer size must be a power of */
				/*  2, so that the index into the buffer */
				/*  will either wrap within that size on */
				/*  increment, or be able to be masked to */
				/*  force a wrap of the index into the */
				/*  circular pen buffers.  See discussion */
				/*  of pen buffers in lx5000_print_page(). */
#define COLOUR_BUF_MASK		0xff
				/* 2^8 - 1 */
#define COLOUR_BUF_LINES	( COLOUR_BUF_MASK + 1 )

/************************************************************************
 *		S W I P E   C O M M A N D   M A C R O S			*
 ************************************************************************/

#define SWIPE_LEADER	0x1b, '*', 4
				/* Lead-in bytes for swipe command */

#define CMDLEN_X	3
				/* Index in swipe command of 4 byte */

#define DIRECTION_X	7
				/* Index in swipe cmd of direction flag */

#define UNIDIRECTIONAL	0
#define BIDIRECTIONAL	1

#define HEADSPEED_X	8
				/* Index in swipe cmd of head speed */
				/* Head speeds for various densities */
#define _300X		1
#define _600X		2
#define _1200X		5

#define PEN_X		9
				/* Index of 2 byte pen selectors */
#define BLACK0		1
#define BLACK1		1
#define COLOUR0		2
#define COLOUR1		0

#define NOZZLE_COUNT_X	11
				/* Index of nozzle count selector */
#define _192NOZZLES	0x18
#define _208NOZZLES	0x1a

#define UNKNOWN1_X	12
				/* Index of an unknown byte */
#define UNKNOWN1VAL	0
#define NUM_COLUMNS_X	13
				/* Index in swipe command of 2 byte */
				/* column count				*/
#define _1ST_COLUMN_X	15
				/* Index in swipe command of 2 byte */
				/* first column horizontal offset */
#define LAST_COLUMN_X	17
				/* Index in swipe command of 2 byte */
				/* last column horizontal offset	*/

#define SWIPE_HDR_END_X	19
#define SWIPE_HDR_END_LEN	7
#define SWIPE_HDR_END	0, 0, '+', 'p', 'b', 'w', 1
				/* Final byte sequence of swipe cmd header */

#define SWIPE_HDR_LEN	( SWIPE_HDR_END_X + SWIPE_HDR_END_LEN )

/************************************************************************
 *		C O L O U R   H A N D L I N G   M A C R O S		*
 ************************************************************************/
/* Macros for the relative position on the colour components in */
/* gx_color_index, the driver defined representation of individual */
/* colours. */

#define BLACK_X		0
#define YELLOW_X	1
#define MAGENTA_X	2
#define CYAN_X		3

#define LO_PEN		0
#define HI_PEN		1

				/* Colour defines for lx5000 black printer */

#   define NUM_COMPONENTS_BLK	1
#   define BITS_PER_PIXEL_BLK	1
#   define MAX_GREY_BLK		1
#   define MAX_RGB_BLK		0
#   define DITHER_GREYS_BLK	2
#   define DITHER_COLOURS_BLK	0

				/* Colour defines for lx5000 colour printer */
#   define NUM_COMPONENTS_CMY	4
#   define BITS_PER_PIXEL_CMY	4
#   define MAX_GREY_CMY		1
#   define MAX_RGB_CMY		1
#   define DITHER_GREYS_CMY	2
#   define DITHER_COLOURS_CMY	2

#   define MIN_COLOUR		BLACK_X
#   define MAX_COLOUR		CYAN_X
#   define BLACK_PEN		BLACK_X
#   define _1ST_CMY_COLOUR	YELLOW_X
#   define LAST_CMY_COLOUR	CYAN_X

#define BPP			BITS_PER_PIXEL_CMY
				/* N.B. This is only required for
				   processCMYKline */
#   define PIXEL_MASK		(( 1 << BPP ) - 1 )
#   define INITIAL_PIXEL_SHIFT	( ( sizeof( uchar ) * 8 ) - BPP )

#define NUM_COLOURS		( MAX_COLOUR - MIN_COLOUR + 1 )
#define PENS_PER_COLOUR	2
#define NUM_PENS		( NUM_COLOURS * PENS_PER_COLOUR )

#define BITS_PER_COLOUR		(BITS_PER_PIXEL_CMY / NUM_COMPONENTS_CMY)

#define DEF_NUM_COLOURS		NUM_COLOURS
#define DEF_PENS_PER_COLOUR	1
#define DEF_LINE_INCREMENT	1

/* !!!!!!!!!!!!!!!!!!!!! WARNING, WARNING, WILL ROBINSON !!!!!!!!!!!!!!!!!!!!*/
				/* If BPP ever exceeds 8, this will break
				   badly, as will the whole of processCMYKline.
				   PIXELS_PER_BYTE is used when skipping over
				   empty bytes in processCMYKline().  Each
				   empty scanline byte skips over this many
				   bits in the colourBufs.	*/
#define PIXELS_PER_BYTE		(8 / BPP)

#define COLOUR_MASK	(( 1 << ( BITS_PER_COLOUR ) ) - 1 )

/************************************************************************
 *			Memory allocation macros			*
 *	    ( For use with getColourBufs & releaseAllocations )		*
 ************************************************************************/
#define ALLOCATE	true
#define DEALLOCATE	false

/************************************************************************
 *		" F U N C T I O N A L "   M A C R O S			*
 ************************************************************************/
/************************************************************************
 *			Macro to fill the swipe header			*
 ************************************************************************/

#define FILL_SWIPE_HEADER( swipeHdr, len, dir, speed, pen0, pen1, nozzles, \
	unknown, numcols, firstcol, lastcol ) \
	    swipeHdr[ CMDLEN_X ]	= ( ( len >> 24 ) & 0xff ); \
	    swipeHdr[ CMDLEN_X + 1 ]	= ( ( len >> 16 ) & 0xff ); \
	    swipeHdr[ CMDLEN_X + 2 ]	= ( ( len >> 8 ) & 0xff ); \
	    swipeHdr[ CMDLEN_X + 3 ]	= ( len & 0xff ); \
	    swipeHdr[ DIRECTION_X ]	= dir; \
	    swipeHdr[ HEADSPEED_X ]	= speed; \
	    swipeHdr[ PEN_X ]		= pen0; \
	    swipeHdr[ PEN_X + 1 ]	= pen1; \
	    swipeHdr[ NOZZLE_COUNT_X ]	= nozzles; \
	    swipeHdr[ UNKNOWN1_X ]	= unknown; \
	    swipeHdr[ NUM_COLUMNS_X ]	= ( numcols >> 8 ); \
	    swipeHdr[ NUM_COLUMNS_X + 1 ] = ( numcols & 0xff ); \
	    swipeHdr[ _1ST_COLUMN_X ]      = ( firstcol >> 8 ); \
	    swipeHdr[ _1ST_COLUMN_X + 1 ]  = ( firstcol & 0xff ); \
	    swipeHdr[ LAST_COLUMN_X ]      = ( lastcol >> 8 ); \
	    swipeHdr[ LAST_COLUMN_X + 1 ]  = ( lastcol & 0xff )


/************************************************************************
 *   Macros to manipulate bit pointers in bitBuf & scanPixels structs	*
 ************************************************************************/
#define BIT_TO_MASK( n ) ( 0x80 >> ( n ) )
#define INC_BIT( bitPtr ) \
do { \
    if ( ! ((bitPtr).xBit >>= 1 )) \
	    { (bitPtr).xByte++ ; (bitPtr).xBit = 0x80; } \
} while (0)

#define DEC_BIT( bitPtr ) \
do { \
    if ( ! ( (bitPtr).xBit = (( (bitPtr).xBit << 1 ) & 0xff ))) \
	{ (bitPtr).xByte-- ; (bitPtr).xBit = 1; } \
} while (0)

/************************************************************************
 *		Macro for defining gx_device_procs structure		*
 ************************************************************************/

#define lx5000_proctab(get_params, put_params, map_color_rgb, map_cmyk_color)\
{	gdev_prn_open,\
	gx_default_get_initial_matrix,\
	NULL,	/* sync_output */\
	gdev_prn_output_page,\
	gdev_prn_close,\
	NULL,	/* map_rgb_color */\
	map_color_rgb,\
	NULL,	/* fill_rectangle */\
	NULL,	/* tile_rectangle */\
	NULL,	/* copy_mono */\
	NULL,	/* copy_color */\
	NULL,	/* draw_line */\
	NULL,	/* get_bits */\
	get_params,\
	put_params,\
	map_cmyk_color,\
	NULL,	/* get_xfont_procs */\
	NULL,	/* get_xfont_device */\
	NULL,	/* map_rgb_alpha_color */\
	gx_page_device_get_page_device	/* get_page_device */\
}

/************************************************************************
 *   T Y P E D E F S   E N U M S   &   E X T E R N A L   S T O R A G E	*
 ************************************************************************/
				/* The procedure descriptors */
				/* declare functions */
private dev_proc_print_page(lx5000_print_page);
private dev_proc_get_params(lx5000_get_params);
private dev_proc_put_params(lx5000_put_params);

private dev_proc_map_cmyk_color(lx5000_map_cmyk_color);
private dev_proc_map_color_rgb(lx5000_map_color_rgb);

private const gx_device_procs lx5000_procs = 
    lx5000_proctab(
                     lx5000_get_params,
		     lx5000_put_params,
		     lx5000_map_color_rgb,
		     lx5000_map_cmyk_color
		     );

				/* The device descriptors */
				/* define a subclass containing useful state */
				/* a sub-class of gx_device_printer */
typedef struct lx5000_device_s {
    gx_device_common;
    gx_prn_device_common;
    int		alignA;
    int		alignB;
    int		headSeparation;
    int		dryTime;	/* Seconds delay at end of page for drying */
    int		pensPerColour;	/* 1 for 600Y; 2 for 1200Y */
    int		lineIncrement;	/* 1 for 600Y; 2 for 1200Y */
    int		scanLineBytes;	/* Returned by GS */
    int		penLineBytes;	/* When scanline colour elements are each
				   reduced to a single bit, this is the result-
				   ing line length in bytes.	*/
    int		penLineLen;	/* penLineBytes + BOL + EOL padding */
    int		penBufSize;	/* penLineLen * no. of lines */
    int		swipeBufSize;	/* calculated size of a swipe command buffer */
    bool	isCMYK;
} lx5000_device;

				/* Define a structure for a pointer to an */
				/* individual bit in a scanline	 */
typedef struct bufBit_s {
    byte 	*xByte;		/* Pointer to byte in buffer */
    uchar	xBit;		/* Mask for invidual bit in byte */
} bufBit;


typedef struct penData_s {
    int		topLine;	/* Top printable line of this pen */
    int		bottomLine;	/* Bottom printable line of this pen */
    int		nextPrintLine;	/* Next line to be printed, this pen */

    int		initialBottomLine;
    int		bottomToBottomYellow;
    int		topToBottomYellow;
    int		finalLine;
} penData;

				/* Structure for extracting pixels from the
				   initial scan line, which must be int
				   aligned.			*/
typedef struct scanPixels_s {
    uchar	*scanByte;	/* Pointer to a byte in the scan buffer */
    int		pixShift;	/* Shift required to get next pixel to */
} scanPixels;			/* the LSBits of the word. */

				/* Standard lx5000 device */
lx5000_device far_data gs_lx5000_device = {
    prn_device_margins_body
    	( lx5000_device,
	  lx5000_procs,
	  "lx5000",
	  DEFAULT_WIDTH_10THS,
	  DEFAULT_HEIGHT_10THS,
	  LX5000_XDPI,		/* x dpi */
	  LX5000_YDPI,		/* y dpi */
				/* Offset inches from page left to 0,0 */
	  LX5000_XOFFSET_TO_0_0,
				/* Offset inches from page top to 0,0 */
	  LX5000_YOFFSET_TO_0_0,
	  LX5000_LEFT_HWMARGIN_INS,	/* margins */
	  LX5000_BOTTOM_HWMARGIN_INS,
	  LX5000_RIGHT_HWMARGIN_INS,
	  LX5000_TOP_HWMARGIN_INS,
	  NUM_COMPONENTS_CMY,	/* colour info  */
	  BITS_PER_PIXEL_CMY,	/*	"	*/
	  MAX_GREY_CMY,		/*	"	*/
	  MAX_RGB_CMY,		/*	"	*/
	  DITHER_GREYS_CMY,	/*	"	*/
	  DITHER_COLOURS_CMY,	/*	"	*/
	  lx5000_print_page
	  ),
    ALIGN_A_DEF,		/* default(!) horizontal pen alignment	*/
    ALIGN_B_DEF,		/* default(!) vertical pen alignment	*/
    HEADSEP_DEF,		/* default headSeparation value */
    DRY_TIME_DEF,		/* Default page drying time */
    DEF_PENS_PER_COLOUR,
    DEF_LINE_INCREMENT,
    0,				/* scanLineBytes */
    0,				/* penLineBytes */
    0,				/* penLineLen */
    0,				/* penBufSize */
    0,				/* swipeBufSize */
    true			/* isCMYK - defaults to using colour	*/
};

private const gx_device_color_info color_info_cmy =
     dci_values(NUM_COMPONENTS_CMY,
		BITS_PER_PIXEL_CMY,
		MAX_GREY_CMY,
		MAX_RGB_CMY,
		DITHER_GREYS_CMY,
		DITHER_COLOURS_CMY);

private const gx_device_color_info color_info_blk =
     dci_values(NUM_COMPONENTS_BLK,
		BITS_PER_PIXEL_BLK,
		MAX_GREY_BLK,
		MAX_RGB_BLK,
		DITHER_GREYS_BLK,
		DITHER_COLOURS_BLK);

/************************************************************************
 ************************************************************************
 *		     D R I V E R   P R O C E D U R E S			*
 ************************************************************************
 ************************************************************************/

/************************************************************************
 *		    U T I L I T Y   P R O C E D U R E S			*
 ************************************************************************/

/*----------------------------------------------------------------------*
 *	i n i t P e n C o n s t a n t s ( )				*
 *----------------------------------------------------------------------*
 *----------------------------------------------------------------------*/
private void
initPenConstants( lx5000_device *lx5000dev,
		  penData pens[NUM_COLOURS][PENS_PER_COLOUR] )
{
				/* Indexed by pensPerColour.  */
    static const int	nozzleCount[ NUM_COLOURS ][ PENS_PER_COLOUR + 1 ] =
    {
	{ -1, BLACK_NOZZLES, (BLACK_NOZZLES / 2) },
	{ -1, _1COLOUR_NOZZLES, (_1COLOUR_NOZZLES / 2) },
	{ -1, _1COLOUR_NOZZLES, (_1COLOUR_NOZZLES / 2) },
	{ -1, _1COLOUR_NOZZLES, (_1COLOUR_NOZZLES / 2) }
    };

    int		colour, pen;
    int		pensPerColour	= lx5000dev->pensPerColour;

    pens[YELLOW_X][LO_PEN].initialBottomLine	= -1;
    pens[MAGENTA_X][LO_PEN].initialBottomLine =
				pens[YELLOW_X][LO_PEN].initialBottomLine
							- COLOUR_PEN_DIFF;
    pens[CYAN_X][LO_PEN].initialBottomLine =
				pens[MAGENTA_X][LO_PEN].initialBottomLine
							- COLOUR_PEN_DIFF;
    pens[BLACK_X][LO_PEN].initialBottomLine =
				pens[YELLOW_X][LO_PEN].initialBottomLine
						- COLOUR_PEN_GAP
				+ ( SWIPE_WORD_BITS - lx5000dev->alignB );

    for ( colour = 0; colour < NUM_COLOURS; colour++ )
    {
	pens[colour][LO_PEN].bottomToBottomYellow
				= pens[YELLOW_X][LO_PEN].initialBottomLine
	    			- pens[colour][LO_PEN].initialBottomLine;
    }

    if ( pensPerColour == 1 )
    {
	pens[BLACK_X][LO_PEN].topToBottomYellow =
					BLK_COLOUR_NOZZLES + COLOUR_PEN_GAP
						+ lx5000dev->alignB - 1;
	pens[YELLOW_X][LO_PEN].topToBottomYellow =  _1COLOUR_NOZZLES - 1;
	pens[MAGENTA_X][LO_PEN].topToBottomYellow =
				pens[YELLOW_X][LO_PEN].topToBottomYellow
						+ COLOUR_PEN_DIFF;
	pens[CYAN_X][LO_PEN].topToBottomYellow =
				pens[MAGENTA_X][LO_PEN].topToBottomYellow
						+ COLOUR_PEN_DIFF;

	for ( colour = 0; colour < NUM_COLOURS; colour++ )
	{
	    pens[colour][HI_PEN].topToBottomYellow
				= pens[colour][LO_PEN].topToBottomYellow;
	    pens[colour][HI_PEN].initialBottomLine
				= pens[colour][LO_PEN].initialBottomLine;
	    pens[colour][HI_PEN].bottomToBottomYellow
				= pens[colour][LO_PEN].bottomToBottomYellow;
	}
    }
    else			/* pensPerColour > 1 */
    {
	pens[BLACK_X][HI_PEN].topToBottomYellow =
					BLK_COLOUR_NOZZLES + COLOUR_PEN_GAP
						+ lx5000dev->alignB - 1;
	pens[BLACK_X][LO_PEN].topToBottomYellow =
					pens[BLACK_X][HI_PEN].topToBottomYellow
					- nozzleCount[BLACK_X][pensPerColour];
	pens[YELLOW_X][HI_PEN].topToBottomYellow =  _1COLOUR_NOZZLES - 1;
	pens[YELLOW_X][LO_PEN].topToBottomYellow =
				pens[YELLOW_X][HI_PEN].topToBottomYellow
					- nozzleCount[YELLOW_X][pensPerColour];
	pens[MAGENTA_X][HI_PEN].topToBottomYellow =
				pens[YELLOW_X][HI_PEN].topToBottomYellow
						+ COLOUR_PEN_DIFF;
	pens[MAGENTA_X][LO_PEN].topToBottomYellow =
				pens[MAGENTA_X][HI_PEN].topToBottomYellow
					-nozzleCount[MAGENTA_X][pensPerColour];
	pens[CYAN_X][HI_PEN].topToBottomYellow =
				pens[MAGENTA_X][HI_PEN].topToBottomYellow
						+ COLOUR_PEN_DIFF;
	pens[CYAN_X][LO_PEN].topToBottomYellow =
				pens[CYAN_X][HI_PEN].topToBottomYellow
					- nozzleCount[CYAN_X][pensPerColour];

	for ( colour = 0; colour < NUM_COLOURS; colour++ )
	{
	    pens[colour][HI_PEN].bottomToBottomYellow =
				    pens[colour][LO_PEN].bottomToBottomYellow
				    + nozzleCount[colour][pensPerColour];
	    pens[colour][HI_PEN].initialBottomLine =
				    pens[colour][LO_PEN].initialBottomLine
				    - nozzleCount[colour][pensPerColour];
	}
    }

    for ( colour = 0; colour < NUM_COLOURS; colour++ )
    {
	for ( pen = 0; pen < PENS_PER_COLOUR; pen++ )
	{
	    pens[colour][pen].finalLine =
		( lx5000dev->height ) + pens[colour][pen].topToBottomYellow;
	}
    }
}

/*----------------------------------------------------------------------*
 *	p a g e I n i t ( )						*
 *----------------------------------------------------------------------*
 *----------------------------------------------------------------------*/
private void
pageInit( unsigned int alignA, unsigned int alignB, FILE *prn_stream )
{
    static char page_init[] = {
        0x1b, '*', 'm', 0, 0x40, ALIGN_A_DEF, ALIGN_B_DEF, 0xf, 0xf
    };

    page_init[ALIGN_A_OFFSET] = (uchar)alignA;
    page_init[ALIGN_B_OFFSET] = (uchar)alignB;
    
    fwrite( page_init, 1, sizeof( page_init ), prn_stream );
}

/*----------------------------------------------------------------------*
 *	p a g e E n d ( )						*
 *----------------------------------------------------------------------*
 *----------------------------------------------------------------------*/
private void
pageEnd( FILE *prn_stream )
{
    static const char page_end[] = {
	0x1b,'*', 7, 0x65
    };

    fwrite( page_end, 1, sizeof( page_end ), prn_stream );
				/* Do it twice - that's what the Windows */
				/* driver for the 5000 does	*/
    fwrite( page_end, 1, sizeof( page_end ), prn_stream );
}

/*----------------------------------------------------------------------*
 *	f e e d P a p e r ( )						*
 *----------------------------------------------------------------------*
 *----------------------------------------------------------------------*/
private void
feedPaper( lx5000_device *lx5000dev, int newLine, int *currentLine,
	   penData pens[NUM_COLOURS][PENS_PER_COLOUR],
	   FILE *prn_stream )
{
    static const char feed_paper[] = {
	0x1b, '*', 3
    };

    ushort	_1200ths;
    int		colour, pen;

    if ( newLine >= *currentLine )
    {
	_1200ths = (ushort)(( newLine - *currentLine ) * FEED_FACTOR );
	fwrite( feed_paper, 1, sizeof( feed_paper ), prn_stream );
	putc( _1200ths >> 8 , prn_stream);
	putc( _1200ths & 0xff, prn_stream );

	*currentLine			= newLine;

	for ( colour = 0;
	      colour < lx5000dev->color_info.num_components;
	      colour++ )
	    for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	    {
		pens[colour][pen].topLine =
		    newLine - pens[colour][pen].topToBottomYellow;
		pens[colour][pen].bottomLine =
		    newLine - pens[colour][pen].bottomToBottomYellow;
	    }

    }
}


/*----------------------------------------------------------------------*
 *	g e t C o l o u r B u f s ( )					*
 *----------------------------------------------------------------------*
  Return pointers to the allocated buffers in those pointers whose addresses
  have been passed as parameters, or release the allocations.

  Allocation is indicated by a true value of the argument `allocate'.

  The buffers themselves are allocated and the pointers to them are kept
  in static storage.  On the first call, the buffers are allocated, and the
  pointers to them are maintained in static storage.  On all subsequent
  calls, the pointer contents are simply returned.

  Deallocation is indicated by a false value of the argument `allocate'.

  If the pointers are non-null, the allocated memory is released, and
  the pointer values are set to NULL.

  N.B.  The arrays are defined in terms of the maximum values for the array
  dimensions, even though the actual usage of each dimension is dynamically
  determined.  E.g. colourBufs is dimensioned [NUM_COLOURS],
  even though the number of colour dimensions actually used is determined
  by the variable numColours, which will be 1 if the driver is being used
  in black-only mode.
 *----------------------------------------------------------------------*/
int
getColourBufs( lx5000_device *lx5000dev,
	       byte **lineBufferPtr, byte *colourBufPtrs[],
	       byte **swipeBufPtr, bool allocate )
{
    static byte  	*colourBufs[ NUM_COLOURS ];
				/* Only an array of pointers; OK if too big */
    static byte 	*lineBuffer	= NULL;
    static byte 	*swipeBuf	= NULL;

    int	colour;
    int	colourBufNull	= 0;
    int	numColours	= lx5000dev->color_info.num_components;

    if ( allocate )
    {
	if ( lineBuffer == NULL )
	{
				/*  Initialise the pen buffers	*/
	    for ( colour = 0; colour < numColours; colour++ )
		colourBufs[ colour ] = NULL;

	    /*---------------------------------------------------------------*
	      Derive the size of a colour buffer line:  If the number of
	      components is one and the number of bits per pixel is one, then
	      there is only one bit per pixel in the scan line, so the colour
	      buffer line is the same size.

	      Otherwise, reduce each gx_color_index element in the scan line to
	      one bit in the penprint buffer.
	      *--------------------------------------------------------------*/

	    if (lx5000dev->color_info.num_components > GX_DEVICE_COLOR_MAX_COMPONENTS)
		return_error(gs_error_limitcheck);

	    lx5000dev->scanLineBytes = gdev_mem_bytes_per_scan_line(
						(gx_device *)lx5000dev );
	    lx5000dev->penLineBytes	=
		( lx5000dev->color_info.num_components == 1
			    && lx5000dev->color_info.depth == 1 )
		? lx5000dev->scanLineBytes
		: ( lx5000dev->scanLineBytes / lx5000dev->color_info.num_components);
	    
	    lx5000dev->penLineLen =
			lx5000dev->penLineBytes + ( LINE_PAD_BYTES * 2);
    
	    lx5000dev->penBufSize
				= lx5000dev->penLineLen * COLOUR_BUF_LINES;

	    /* swipeBuf size:
	     * No. of columns = No. of bits in the linebuf
	     * Bits per column = maximum swipe height
	     * Bytes per column = Bits per column / 8 + 2 byte directory
	     * Total bytes = Bytes/column * no. of columns + header bytes
	     */
	    lx5000dev->swipeBufSize	=
		((lx5000dev->penLineLen * 8)
		 * ((BLACK_NOZZLES / 8) + 2) + SWIPE_HDR_LEN);

				/* Allocate a buffer for a single scan line */
	    lineBuffer	= (byte *)gs_alloc_byte_array
		( gs_lib_ctx_get_non_gc_memory_t(), lx5000dev->scanLineBytes, 1,
		  "lx5000_print_page(lineBuffer)" );

	    swipeBuf	= (byte *)gs_alloc_byte_array
		( gs_lib_ctx_get_non_gc_memory_t(), lx5000dev->swipeBufSize, 1,
		  "lx5000_print_page(swipeBuf)" );

	    for ( colour = 0 ; colour < numColours; colour++ )
	    {
		if ( ( colourBufs[colour] =
		       (byte *)gs_alloc_byte_array
		       ( gs_lib_ctx_get_non_gc_memory_t(), lx5000dev->penBufSize, 1,
			 "lx5000_print_page(colourBufs)"
			 )
		       ) == NULL )
		{
		    colourBufNull = 1;
		    colour = numColours;
		}
	    }
				/* Check allocations */
	    if ( lineBuffer == NULL || colourBufNull || swipeBuf == NULL ) {
		getColourBufs( lx5000dev, lineBufferPtr, colourBufPtrs,
				    swipeBufPtr, DEALLOCATE );
		return_error( gs_error_VMerror );
	    }
	}
	/* Clear the black buffer, iff ! isCMYK.  If CMYK, scan lines are
	   processed by processCMYKlines(), and buffer lines are cleared
	   individually, before a new scan line is processed.  As part of this
	   clearing, the line EOL and BOL pads are also cleared.
	   If ! CMYK, the black-only scan line is read or copied directly
	   into the black buffer, without clearing the buffer.  Therefore, the
	   line pad regions must either be cleared individually for each line
	   processed, or cleared once when the buffer is allocated.
	*/
	if ( ! lx5000dev->isCMYK )
	    memset( colourBufs[BLACK_X], 0, lx5000dev->penBufSize );
	   
				/* Return the values */
	*lineBufferPtr	= lineBuffer;
	*swipeBufPtr	= swipeBuf;
	for ( colour = 0; colour < numColours; colour++ )
	    colourBufPtrs[colour] = colourBufs[colour];

	return 0;
    }
    else			/* Deallocate the buffers */
    {
	for ( colour = 0; colour < numColours; colour++ )
	{
	    if ( colourBufs[colour] != NULL )
		gs_free_object( gs_lib_ctx_get_non_gc_memory_t(),
				(char *)colourBufs[colour],
				"lx5000_print_page(colourBufs)" );
	    colourBufs[ colour ]	= NULL;
	    colourBufPtrs[ colour ]	= NULL;
	}
	if ( swipeBuf != NULL )
	    gs_free_object( gs_lib_ctx_get_non_gc_memory_t(),
			    (char *)swipeBuf, "lx5000_print_page(swipeBuf)" );
	swipeBuf	= NULL;
	*swipeBufPtr	= NULL;
	if ( lineBuffer != NULL )
	    gs_free_object( gs_lib_ctx_get_non_gc_memory_t(),
		    (char *)lineBuffer, "lx5000_print_page(lineBuffer)" );
	lineBuffer	= NULL;
	*lineBufferPtr	= NULL;
	return 0;
    }
}


/*----------------------------------------------------------------------*
 *	p r o c e s s C M Y K l i n e ( )				*
 *----------------------------------------------------------------------*
  Given a scan line number, a pointer to a scan line, an array of pointers to
  individual colour buffers, and an array of the individual pen scanBit
  arrays, distribute the scan line elements into the colour buffers.

  N.B. The empty indicator for each line for each pen buffer must also be
  set by this procedure.
  
  Things like dithering may well end up in here.
 *----------------------------------------------------------------------*/
private void
processCMYKline( lx5000_device *lx5000dev, int linenum,
		 byte *lineBuffer, byte *scanLine,
		 byte *colourBufs[NUM_COLOURS],
		 bufBit
		 colourLines[NUM_COLOURS][PENS_PER_COLOUR][COLOUR_BUF_LINES],
		 penData pens[NUM_COLOURS][PENS_PER_COLOUR],
		 bool lineEmpty[NUM_COLOURS][ COLOUR_BUF_LINES ] )
{
    int		colour;
    int		numColours = lx5000dev->color_info.num_components;

    uchar	colourBits[ 8 ];
    uchar	lineIndex;
    uchar	scanPixel;
    scanPixels	nextPixel;
    bufBit	nextBit[ 8 ];
				/* The end of the (scan)line. */
    uchar	*scanEnd	= scanLine + lx5000dev->scanLineBytes;

    nextPixel.scanByte	= scanLine;  /* Set up the moving pixel pointer. */
    nextPixel.pixShift	= INITIAL_PIXEL_SHIFT;
    lineIndex		= (uchar)( linenum & COLOUR_BUF_MASK );

    for ( colour = 0; colour < numColours; colour++ )
    {
				/* Set up the moving output bit pointer for
				   each of the colours.	*/
	nextBit[ colour ].xByte	= colourBufs[ colour ] + LINE_PAD_BYTES
				+ ( lineIndex * lx5000dev->penLineLen );
	nextBit[ colour ].xBit	= BIT_TO_MASK( 0 );
				/* Clear the colourBuf line if necessary */
	if ( ! lineEmpty[ colour ][ lineIndex ] )
	{
	    memset( nextBit[ colour ].xByte - LINE_PAD_BYTES,
		    0, lx5000dev->penLineLen );
	    lineEmpty[ colour ][ lineIndex ] = true;
	}
    }
				/* Is the line empty? */
    if (( ! *scanLine ) &&
	! memcmp( scanLine, scanLine + 1, lx5000dev->scanLineBytes - 1 ))
	return;			/* N.B. empty flag is pre-set to true */

    while ( nextPixel.scanByte < scanEnd )
    {
	int	colour;
	int	cmy;		/* Used to detect if C+M+Y set */
	int	skipPixels, skipBytes, skipBits;
				/* Number of consecutive empty pixels, equiv-
				   alent bytes and remainder bits just skipped
				   over in the CMYK line being processed. */

				/* Get next pixel and increment the pointer */
	scanPixel =
	    ( ( *(nextPixel.scanByte) >> nextPixel.pixShift ) & PIXEL_MASK );

		/* pixShift is the number of bits to right-shift the scanByte
		   before applying PIXEL_MASK.  When we attempt to decrement
		   it past 0, it's time to look at the next scanByte.
		   N.B.  We assume that the subtraction of BPP will end with
		   zero, i.e., that there are an integral number of BPPs in
		   INITIAL_PIXEL_SHIFT.  We test for <= 0 just
		   in case this condition gets violated.
		*/
	skipPixels = 0;
	if (  nextPixel.pixShift <= 0 )
	{
	    nextPixel.scanByte++;
	    nextPixel.pixShift = INITIAL_PIXEL_SHIFT;
				/* If the next byte is empty, skip it */
	    while (( ! *nextPixel.scanByte ) && nextPixel.scanByte < scanEnd )
	    {
		nextPixel.scanByte++;
		skipPixels += PIXELS_PER_BYTE;
	    }
	}
	else
	    nextPixel.pixShift -= BPP;
				/* Get the black bit(s) */
	colourBits[ BLACK_X ]	= scanPixel & COLOUR_MASK;
	scanPixel		>>= BITS_PER_COLOUR;
				/* Set to catch all ANDed bits from colours */
	cmy			= ~0;
	for ( colour = YELLOW_X; colour <= CYAN_X; colour++ )
	{
	    colourBits[ colour ] = scanPixel & COLOUR_MASK;
	    cmy			&= scanPixel & COLOUR_MASK;
	    scanPixel		>>= BITS_PER_COLOUR;
	}
	if ( ( cmy ^ COLOUR_MASK ) == 0 ) /* C, M & Y all set == COLOUR_MASK */
	{
	    colourBits[ YELLOW_X ] =
		colourBits[ MAGENTA_X ] = colourBits[ CYAN_X ] = 0;
	    colourBits[ BLACK_X ] = COLOUR_MASK;
	}
				/* Now set the colourBuf bits */
	skipBytes = skipPixels >> 3;
	skipBits  = skipPixels & 7;
	for ( colour = 0; colour < numColours; colour++ )
	{
	    if ( colourBits[ colour ] )
	    {
		*( nextBit[ colour ].xByte )	|= nextBit[ colour ].xBit;
		lineEmpty[ colour ][ lineIndex ] = false;
	    }
	    INC_BIT( nextBit[ colour ] );
	    if ( skipPixels )
	    {
		int	bitSkip = skipBits;

		nextBit[ colour ].xByte += skipBytes;
		while ( bitSkip-- )
		{
		    INC_BIT( nextBit[ colour ] );
		}
	    }
	}
    }
}


/*----------------------------------------------------------------------*
 *	r e f r e s h B u f f e r ( )					*
 *----------------------------------------------------------------------*
 Given pointers to the values of the next line to retrieve from GS, and
 the next printable line, the GS line buffer, the per colour bit buffers,
 the per colour arrays containing the per line `empty'
 indicators, and the per pen penData structs, containing the next print
 line for each pen, refresh the buffer, and perform initial processing on
 the lines read from GS.

 When the scan line is for a black-only device, the line bits can be read
 directly into the appropriate colour buffer at the appropriate place.

 When the scan lines hold colour info, each line must undergo CMYK pro-
 cessing to derive the individual arrays of single-colour bits.

 Refreshing the buffers involves reading lines into the circular colour
 buffers until COLOUR_BUF_LINES have been read, or until the available
 lines have been exhausted.

 On return, nextLineToPrint must be set to the said line number, or beyond
 the end of the page, if no more remain to be printed.
 *----------------------------------------------------------------------*/
private void
refreshBuffer( lx5000_device *lx5000dev,
	       int *nextLineToGet, int *nextLineToPrint,
	       byte *lineBuffer, byte *colourBufs[],
	       bufBit
	       colourLines[NUM_COLOURS][PENS_PER_COLOUR][COLOUR_BUF_LINES],
	       penData pens[NUM_COLOURS][PENS_PER_COLOUR],
	       bool lineEmpty[NUM_COLOURS][COLOUR_BUF_LINES] )
{
    byte	*lineBuf;
    uchar	nextToGet;	/* Circular buffer pointer, 0 to ff */
    int		bufferOffset;
    int		colour, pen;
    int		_1stPrintable;	/* Across all pens */
    int		numColours = lx5000dev->color_info.num_components;

				/* Establish the next line to print, if it
				   is already in the buffer.		*/
    _1stPrintable		= pens[YELLOW_X][LO_PEN].finalLine;
    for ( colour = 0; colour < numColours; colour++ )
    {
	for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	    if ( pens[ colour ][ pen ].nextPrintLine < _1stPrintable)
		_1stPrintable = pens[ colour ][ pen ].nextPrintLine;
    }
    *nextLineToPrint = _1stPrintable;

    nextToGet = (uchar)(*nextLineToGet & COLOUR_BUF_MASK);
				/* nextLineToPrint may be set high (i.e. not
				   known), or may be a known print line. */
    while (  ( *nextLineToGet < *nextLineToPrint
	       || ( *nextLineToGet - *nextLineToPrint ) < COLOUR_BUF_LINES )
	     && *nextLineToGet < lx5000dev->height )
    {
	bufferOffset = LINE_PAD_BYTES + ( lx5000dev->penLineLen * nextToGet );

	if ( ! lx5000dev->isCMYK )
	{
				/* For black-only, read the bits directly into
				   the colour buffer. */
	    gdev_prn_get_bits( (gx_device_printer *)lx5000dev, *nextLineToGet,
			       colourBufs[BLACK_X] + bufferOffset, &lineBuf );
				/* If necessary, copy the bits into the actual
				   buffer.		*/
	    if ( lineBuf != colourBufs[BLACK_X] + bufferOffset )
		memcpy(colourBufs[BLACK_X] + bufferOffset,
		       lineBuf, lx5000dev->penLineBytes);
				/* Check for a printing line */
	    if ( *(colourBufs[BLACK_X] + bufferOffset) != 0
		 || memcmp( colourBufs[BLACK_X] + bufferOffset,
			    colourBufs[BLACK_X] + bufferOffset + 1,
			    lx5000dev->scanLineBytes - 1 ))
		lineEmpty[ BLACK_X ][  nextToGet ] = false;
	    else
		lineEmpty[ BLACK_X ][  nextToGet ] = true;
	}
	else			/* CMYK printing */
	{
				/* Process a CMYK line from GS */
	    gdev_prn_get_bits( (gx_device_printer *)lx5000dev,
			       *nextLineToGet, lineBuffer, &lineBuf );
	    processCMYKline( lx5000dev, *nextLineToGet, lineBuffer, lineBuf,
			     colourBufs, colourLines, pens, lineEmpty );
	}
	for (colour = 0; colour < numColours; colour++ )
	{
				/* Check for printing line -
				   N.B. For two-pen colour, this becomes more
				   complicated.  The relationship between the
				   first print line of the lower and of the
				   upper pen is determined by odd/even line
				   numbers, although this is not an essential
				   relationship.  Blank lines at the top of
				   the lower pen could be skipped, and the
				   first available line could be printed by
				   the lower pen. */
	    if ( ! lineEmpty[colour][nextToGet] )
	    {
		if ( pens[colour][LO_PEN].nextPrintLine > *nextLineToGet )
		    pens[colour][LO_PEN].nextPrintLine = *nextLineToGet;
		if ( *nextLineToPrint > *nextLineToGet )
		    *nextLineToPrint = *nextLineToGet;
	    }
	}
	++*nextLineToGet;
	nextToGet = ( *nextLineToGet & COLOUR_BUF_MASK ); /* N.B. This is safer
				   than just doing the increment, in case the
				   buffer length changes.		*/
    }
				/* Check that no buffer padding is necessary */
    if ( *nextLineToPrint < lx5000dev->height )
    {
	while ( ( *nextLineToGet - *nextLineToPrint ) < COLOUR_BUF_LINES )
	{			/* Last refresh fell short of COLOUR_BUF_LINES
				   because last line of page was reached.
				   Pad the buffer to COLOUR_BUF_LINES with 0
					   (no print) bytes.  */
	    for ( colour = 0; colour < numColours; colour++ )
	    {			/* Fill the colour line buffer, incl pad */
		memset( colourBufs[colour]
			+ ( lx5000dev->penLineLen * nextToGet ),
			0, lx5000dev->penLineLen );
				/* Set empty indicator - note that firstBit
				   is NOT set here. */
		lineEmpty[colour][nextToGet] = true;
	    }
	    ++*nextLineToGet;
	    nextToGet = ( *nextLineToGet & COLOUR_BUF_MASK );
	}
    }
}

/*----------------------------------------------------------------------*
 *	c a r t r i d g e M o v e T o ( )				*
 *----------------------------------------------------------------------*
  Given the array of penBits structures for each pen, containing the
  nextPrintLine value for the pen, return the line position to
  which the colour cartridge must be moved to print that line with the
  TOP nozzle of the required pen.
  If the print line for any pen is within the current range of that pen,
  a line less than the current colour head position will be returned.
  If colour is not defined, return a value off the end of the page.
 *----------------------------------------------------------------------*/
private int
cartridgeMoveTo( lx5000_device *lx5000dev,
		 penData pens[NUM_COLOURS][PENS_PER_COLOUR],
		 int _1stColour, int lastColour )
{
    if ( _1stColour != BLACK_X && ! lx5000dev->isCMYK )
				/* Send back an off-the-page value */
	return pens[CYAN_X][LO_PEN].finalLine;
    else
    {
	int	colour, pen, minLine;

	minLine	= pens[CYAN_X][LO_PEN].finalLine; /* Initialise high  */
				/* For each pen, calculate a moveto value
				   which will print the next line with the
				   first nozzle of that pen.
				   Return the minimum of these values. */
	for ( colour = _1stColour; colour <= lastColour; colour++ )
	    for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
		if ( ( pens[colour][pen].nextPrintLine
		       + pens[colour][pen].topToBottomYellow )
		     < minLine )
		    minLine =
			pens[colour][pen].nextPrintLine
			+ pens[colour][pen].topToBottomYellow;

	return minLine;
    }
}
/*----------------------------------------------------------------------*
 *	s e t C o l o u r C o l u m n E x t e n t ( )			*
 *----------------------------------------------------------------------*
  Given a colour buffer, a single array of colourBits structures,
  1st and last lines, 1st nozzle, and the addresses of
  _1stColumn, lastColumn and columnExtent variables, calculate and set
  these column variables after stripping leading and trailing white space.
 *----------------------------------------------------------------------*/
private void
setColourColumnExtent( lx5000_device *lx5000dev, byte *colourBuf,
		       bool lineEmpty[COLOUR_BUF_LINES],
		       int _1stLine, int _1stNozzle, int lastLine,
		       int *_1stColumn, int *lastColumn, int *columnExtent )
{
    uchar	_1stIndex;
    int		firstPrintByte, lastPrintByte;
    int		line;

    line		= _1stLine;
    firstPrintByte	= lx5000dev->penLineBytes;
    lastPrintByte	= 0;

    for ( ; line <= lastLine; line += lx5000dev->lineIncrement )
    {				/* For each active line in swipe */
	int	columnByte;

	_1stIndex	= line & COLOUR_BUF_MASK;
	if ( lineEmpty[ _1stIndex ] )
	    continue;		/* If the line is empty, skip it. */
				/* Otherwise, scan from the beginning to any
				   previously found non-zero byte for the
				   beginning of the active column extent. */
	for ( columnByte = 0; columnByte < firstPrintByte; columnByte++ )
	    if ( colourBuf[ _1stIndex * lx5000dev->penLineLen
			  + LINE_PAD_BYTES + columnByte ] )
	    {
		firstPrintByte = columnByte;
		break;
	    }
				/* Scan from the end of the line to any pre-
				   viously found non-zero byte for the end of
				   the active column extent.		*/
	for ( columnByte = lx5000dev->penLineBytes;
	      columnByte > lastPrintByte; columnByte-- )
	    if ( colourBuf[ _1stIndex * lx5000dev->penLineLen
			  + LINE_PAD_BYTES + columnByte ] )
	    {
		lastPrintByte = columnByte;
		break;
	    }
    }
				/* If we have no extent, there are no
				   printable lines for this pen.	*/
    if ( firstPrintByte > lastPrintByte )
    {
	*_1stColumn	= firstPrintByte * 8;
	*lastColumn	= lastPrintByte * 8;
	*columnExtent	= 0;
	return;
    }

    *_1stColumn		= firstPrintByte * 8;
    *lastColumn		= lastPrintByte * 8 + 7 + lx5000dev->headSeparation;
    *columnExtent	= ( *lastColumn - *_1stColumn ) + 1;
    
}

/*----------------------------------------------------------------------*
 *	s e t C o l o u r L i n e s ( )					*
 *----------------------------------------------------------------------*
  Given a single colour/pen buffer, a single array of bufBit structures,
  1st and last lines, 1st nozzle, print direction, and
  _1stColumn, lastColumn and columnExtent variables, set up the pointers
  to the first actual column of each line, taking account of even and odd
  nozzle numbers.
 *----------------------------------------------------------------------*/
private void
setColourLines( lx5000_device *lx5000dev,
		byte *colourBuf, bufBit colourLines[COLOUR_BUF_LINES],
		int _1stLine, int _1stNozzle, int lastLine, int direction,
		int _1stColumn, int lastColumn )
{
    uchar	_1stIndex;
    int		line, nozzle, bit, headSep, headSepBytes, headSepBits;
    int		firstPrintByte, lastPrintByte, lastPrintBit;
    bufBit	startPtr;
    
    /* Now initialize the penBits bufBit structures to point to the
       beginning and end of the extent for each of the active lines in
       this swipe
       
       Adjust the beginning for all EVEN nozzles by moving it to the
       left by HeadSeparation bits.
    */

    headSep		= lx5000dev->headSeparation;
    if ( lx5000dev->x_pixels_per_inch == 300.0 )
	headSep		= headSep >> 1;	/* Halve the value at 300 dpi */
    if ( lx5000dev->x_pixels_per_inch == 1200.0 )
	headSep		= headSep << 1; /* Double the value at 1200dpi */
    headSepBytes	= headSep / 8;
    headSepBits		= headSep % 8;

    firstPrintByte	= _1stColumn / 8;
    lastPrintByte	= lastColumn / 8;
    lastPrintBit	= lastColumn % 8;
        
    if ( direction == RIGHTWARD )
    {
				/* Set up the ODD columns first */
	startPtr.xByte	= colourBuf + LINE_PAD_BYTES + firstPrintByte;
	startPtr.xBit	= BIT_TO_MASK( 0 );
	line		= _1stLine;
	nozzle		= _1stNozzle;	/* Is 1st nozzle effectively odd? */
	if ( ! ( nozzle & 1 ) )	/* Nozzle is even, so increment */
	    line += lx5000dev->lineIncrement;
	for ( ; line <= lastLine; line += ( lx5000dev->lineIncrement << 1 ) )
	{
	    _1stIndex	= (uchar)( line & COLOUR_BUF_MASK );
	    colourLines[_1stIndex].xByte	=
		startPtr.xByte + _1stIndex * lx5000dev->penLineLen;
	    colourLines[_1stIndex].xBit	= startPtr.xBit;
	}
				/* Set up EVEN columns */
	startPtr.xByte -= headSepBytes;
	for ( bit = 0; bit < headSepBits; bit++ )
	{
	    DEC_BIT( startPtr );
	}
	line	= _1stLine;
	nozzle	= _1stNozzle;	/* Is 1st nozzle effectively even? */
	if ( nozzle & 1 )	/* Nozzle is odd, so increment */
	    line += lx5000dev->lineIncrement;
	for ( ; line <= lastLine; line += ( lx5000dev->lineIncrement << 1 ) )
	{
	    _1stIndex	= (uchar)( line & COLOUR_BUF_MASK );
	    colourLines[_1stIndex].xByte	=
		startPtr.xByte + _1stIndex * lx5000dev->penLineLen;
	    colourLines[_1stIndex].xBit	= startPtr.xBit;
	}
    }
    else			/* direction == LEFTWARD */
    {
				/* Set up the ODD columns first */
	startPtr.xByte	= colourBuf + LINE_PAD_BYTES + lastPrintByte;
	startPtr.xBit	= BIT_TO_MASK( lastPrintBit );
	line		= _1stLine;
	nozzle		= _1stNozzle;	/* Is 1st nozzle effectively odd? */
	if ( ! ( nozzle & 1 ) )	/* Nozzle is even, so increment */
	    line += lx5000dev->lineIncrement;
	for ( ; line <= lastLine; line += ( lx5000dev->lineIncrement << 1 ) )
	{
	    _1stIndex	= (uchar)( line & COLOUR_BUF_MASK );
	    colourLines[_1stIndex].xByte	=
		startPtr.xByte + _1stIndex * lx5000dev->penLineLen;
	    colourLines[_1stIndex].xBit	= startPtr.xBit;
	}
				/* Set up EVEN columns */
	startPtr.xByte -= headSepBytes;
	for ( bit = 0; bit < headSepBits; bit++ )
	{
	    DEC_BIT( startPtr );
	}
	line	= _1stLine;
	nozzle	= _1stNozzle;	/* Is 1st nozzle effectively even? */
	if ( nozzle & 1 ) /* Nozzle is odd, so increment */
	    line += lx5000dev->lineIncrement;
	for ( ; line <= lastLine; line += ( lx5000dev->lineIncrement << 1 ) )
	{
	    _1stIndex	= (uchar)( line & COLOUR_BUF_MASK );
	    colourLines[_1stIndex].xByte	=
		startPtr.xByte + _1stIndex * lx5000dev->penLineLen;
	    colourLines[_1stIndex].xBit	= startPtr.xBit;
	}
    }
}

/*----------------------------------------------------------------------*
 *	p r i n t S w i p e ( )						*
 *----------------------------------------------------------------------*
 *----------------------------------------------------------------------*/
private void
printSwipe( lx5000_device *lx5000dev, byte *colourBufs[NUM_COLOURS],
	     bufBit
	     colourLines[NUM_COLOURS][PENS_PER_COLOUR][COLOUR_BUF_LINES],
	     penData pens[NUM_COLOURS][PENS_PER_COLOUR],
	     bool lineEmpty[NUM_COLOURS][COLOUR_BUF_LINES], byte *swipeBuf,
	     int *nextLineToPrint, int *nextLineToGet, int direction,
	     int _1stColour, int lastColour, FILE *prn_stream )
{
    /*--------------------------------------------------------------------*
      For unidirectional colour swathes, the effective print direction on
      a 5000 is reversed.  The implication of this is that, once the extent
      of the printing columns has been determined, the data for the colour
      pens must be fed to the head bit-reversed; i.e., set the bit pointer
      to the last column to be printed, and decrement the bit pointer until
      all of the columns have been printed.
     *--------------------------------------------------------------------*/

    static const char swipeHeader[SWIPE_HDR_LEN] = {
	SWIPE_LEADER,
	0, 0, 0, 0,			/* command length */
	0,				/* direction */
	0,				/* head speed */
	0, 0,			/* pen selector */
	0,				/* nozzle count */
	0,				/* don't know */
	0, 0,			/* number of columns */
	0, 0,			/* 1st column */
	0, 0,			/* last column */
	SWIPE_HDR_END
    };

    static const int	wordsPerPen[NUM_COLOURS][PENS_PER_COLOUR + 1] =
    {
	{ -1,
	  BLACK_NOZZLES / SWIPE_WORD_BITS,
	  BLACK_NOZZLES / SWIPE_WORD_BITS / 2
	},
	{ -1,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS / 2
	},
	{ -1,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS / 2
	},
	{ -1,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS,
	  _1COLOUR_NOZZLES / SWIPE_WORD_BITS / 2
	}
    }; 

    int		_1stNozzle[NUM_COLOURS][PENS_PER_COLOUR];
				/* 1st active nozzle for pen */
    int		lastNozzle[NUM_COLOURS][PENS_PER_COLOUR];
				/* Last nozzle of this pen */
    int		_1stLine[NUM_COLOURS][PENS_PER_COLOUR];
				/* Line no. of first print line for pen */
    int		_1stPenColumn[NUM_COLOURS][PENS_PER_COLOUR];
    int		lastPenColumn[NUM_COLOURS][PENS_PER_COLOUR];
				/* Per-pen values... */
    int		penExtent[NUM_COLOURS][PENS_PER_COLOUR];
    int		_1stColumn;	/* ...and overall values of... */
    int		lastColumn;
    int		columnExtent;	/* Column range after */
				/* stripping leading and trailing white space*/
    int		column;		/* Loop variable - current column */
    int		line;		/* Loop variables - line corresponding to
				   current nozzle & temp line variable.  */
    uchar	lineIndex;	/* Circular index into pen buffer,
				   corresponding to current line. */
    int		swipeCmdLen;	/* Length of the swipe command */
    int		colour, pen;
    byte 	*outp;
    int		x_dpi		= (int)( lx5000dev->x_pixels_per_inch + 0.1 );

				/* Initialize worst case values for column
				   extremities.		*/
    _1stColumn	= lx5000dev->penLineBytes * 8
					+ 7 + lx5000dev->headSeparation;
    lastColumn	= 0;
				/* Set up extents on a per-colour basis. */
    for ( colour = _1stColour; colour <= lastColour; colour++ )
	for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	{
	    _1stLine[colour][pen]	= pens[colour][pen].nextPrintLine;
	    _1stNozzle[colour][pen]	= _1stLine[colour][pen]
						- pens[colour][pen].topLine;
	    lastNozzle[colour][pen]	= pens[colour][pen].bottomLine
	 					- pens[colour][pen].topLine;

	    setColourColumnExtent( lx5000dev, colourBufs[colour],
				   lineEmpty[colour], _1stLine[colour][pen],
				   _1stNozzle[colour][pen],
				   pens[colour][pen].bottomLine,
				   &_1stPenColumn[colour][pen],
				   &lastPenColumn[colour][pen],
				   &penExtent[colour][pen] );
				/* Get the overall extents for the swipe */
	    if ( _1stPenColumn[colour][pen] < _1stColumn )
		_1stColumn = _1stPenColumn[colour][pen];
	    if ( lastPenColumn[colour][pen] > lastColumn )
		lastColumn = lastPenColumn[colour][pen];
	}
				/* Set overall extent */
    columnExtent	= ( lastColumn - _1stColumn ) + 1;

				/* Set the column range for each of the colour
				   pens now that the full extent is known. */
    for ( colour = _1stColour; colour <= lastColour; colour++ )
	for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	{
	    setColourLines( lx5000dev, colourBufs[colour],
			    colourLines[colour][pen],
			    _1stLine[colour][pen], _1stNozzle[colour][pen],
			    pens[colour][pen].bottomLine, direction,
			    _1stColumn, lastColumn );
	}
    
    memcpy( swipeBuf, swipeHeader, SWIPE_HDR_LEN );
    outp	= swipeBuf + SWIPE_HDR_LEN;
				/* For each column of the output, build two
				   stripes.  One uses the standard directory
				   scheme, and the other uses the repeat
				   compression scheme.  Output the more
				   compact result. */
    for ( column = _1stColumn; column <= lastColumn; column++ )
    {
	ushort	dataword;	/* Loop transient - contents of swipe data
				   word currently under construction. */
	ushort	lastWord;	/* Last data word constructed for repeat
				   compression directory method.	*/
	int	nozzle;		/* Loop variable - current nozzle */
				/* The arrays of the two sets of data words
				   generated by each of the swipe construction
				   methods for a single swipe.  Does not
				   include the 16 bit directory word. */
	int	wordCount1;	/* The count of words in the column arrays, */
	int	wordCount2;	/* below.			*/
	ushort	column1[BLK_SWIPE_WORDS];
	ushort	column2[BLK_SWIPE_WORDS];
	int	columnWord;	/* Current word within column. */
	bufBit *lineBit;	/* Loop transient - current scan line bit */
	ushort	wordBit;	/* Current bit within swipe word - incremented
				   by right shifts. */
	ushort	directoryBit;	/* Current bit within directory word.  It
				   indicates the currently active set of 16
				   nozzles.  Incremented by left shifts. */
	ushort	directory1;	/* The directories for the two methods. Each */
	ushort	directory2;	/* bit controls one following 16bit word. */

	if ( _1stColour == BLACK_X )
	{
	    directory1	= (ushort)(BLK_DIRECTORY_MASK); /* empty */
	    directory2	= (ushort)(BLK_DIRECTORY_MASK); /* empty */
	}
	else
	{
	    directory1	= (ushort)(COLOUR_DIRECTORY_MASK); /* empty */
	    directory2	= (ushort)(COLOUR_DIRECTORY_MASK); /* empty */
	}
	directory1	|= DIRECTORY_TYPE_BIT; /* Normal directory */
	directory2	&= ~DIRECTORY_TYPE_BIT; /* Repeat compression
						   directory */
				/* LSBit of directory refers to 1st following
				   word */
	directoryBit	= 1;
	lastWord	= 0;
	wordCount1	= 0;
	wordCount2	= 0;
				/* Note that the order of the pens in the
				   datawords (top nozzle to bottom) is
				   CMY, the reverse of the numbering */
				/*------------------------------------------
				  N.B. What are the implications of this for
				  pen processing within a colour?
				  -----------------------------------------*/
	for ( colour = lastColour; colour >= _1stColour; colour-- )
	    for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	    {
		dataword	= 0;
		nozzle	= 0;
		line	= _1stLine[colour][pen];
				/* If there are any non-printing nozzles at
				   the top of the printhead, set up the entries
				   for them here.		*/
		while ( ( (  _1stNozzle[colour][pen] - nozzle )
			  >= SWIPE_WORD_BITS )
			&& ( nozzle <= lastNozzle[colour][pen] ) )
		{
		    if ( lastWord != 0 )
		    {
			directory2		&= ~directoryBit;
			column2[ wordCount2++ ]	=  dataword;
			lastWord		=  dataword;
		    }
		    nozzle		+= SWIPE_WORD_BITS;
		    directoryBit	<<= 1;
		}
				/* Shift over any remaining empty bits in
				   the current column word.  */
		wordBit = ( 1 << ( SWIPE_WORD_BITS - 1 ));
		wordBit >>= ( _1stNozzle[colour][pen] - nozzle );

		for ( columnWord = _1stNozzle[colour][pen] / SWIPE_WORD_BITS;
		      columnWord <
			  wordsPerPen[colour][lx5000dev->pensPerColour];
		      columnWord++ )
		{
		    while ( wordBit )
		    {
			lineIndex = (uchar)( line & COLOUR_BUF_MASK );
				/* Get the bit from the next line */
			lineBit   = &colourLines[colour][pen][lineIndex];
			if ( *(lineBit->xByte) & lineBit->xBit )
			    dataword |= wordBit;
			wordBit >>= 1;
			if ( direction == LEFTWARD )
			{
			    DEC_BIT( *lineBit );
			}
			else
			{
			    INC_BIT( *lineBit );
			}
			line += lx5000dev->lineIncrement;
		    }
				/* Do we have a non-null dataword? */
		    if ( dataword )/* For normal compression, every nonempty */
		    {		/* dataword is noted and sent to printer */
			directory1		&= ~directoryBit;
			column1[ wordCount1++ ]	=  dataword;
		    }
		    if ( dataword != lastWord ) /* For repeat compression, */
		    {		/* only datawords which change are recorded */
			directory2		&= ~directoryBit;
			column2[ wordCount2++ ]	=  dataword;
			lastWord		=  dataword;
		    }
				/* Look at next set of nozzles */
		    directoryBit <<= 1;
		    dataword	= 0;
				/* Reset dataword bit mask to 1st bit */
		    wordBit	= ( 1 << ( SWIPE_WORD_BITS - 1 ));
		}
	    }
				/* Output the smaller array of column words -
				   is it normal or repeat compressed? */
	if ( wordCount1 < wordCount2 )
	{			/* A normal directory + data */
	    int		i;

	    *outp++	= directory1 >> 8;
	    *outp++	= directory1 & 0xff;
	    for ( i = 0; i < wordCount1; i++ )
	    {
		*outp++	= column1[ i ] >> 8;
		*outp++	= column1[ i ] & 0xff;
	    }
	}
	else
	{			/* A repeat compression directory + data */
	    int		i;

	    *outp++	= directory2 >> 8;
	    *outp++	= directory2 & 0xff;
	    for ( i = 0; i < wordCount2; i++ )
	    {
		*outp++	= column2[ i ] >> 8;
		*outp++	= column2[ i ] & 0xff;
	    }
	}
    }
				/* Set up the header and output the swipe */
    swipeCmdLen	= outp - swipeBuf;
    {
	int	ps1, ps2, direction, density, nozzles;

	direction	= UNIDIRECTIONAL;
	if ( x_dpi == 300 )
	{
	    density	= _300X;
	}
	else
	{
	    if ( x_dpi == 1200 )
		density	= _1200X;
	    else
		density	= _600X;
	}

	if ( _1stColour == BLACK_X )
	{
	    ps1		= BLACK0;
	    ps2		= BLACK1;
	    nozzles	= _208NOZZLES;
	}
	else
	{
	    ps1		= COLOUR0;
	    ps2		= COLOUR1;
	    nozzles	= _192NOZZLES;
	}
	FILL_SWIPE_HEADER( swipeBuf, swipeCmdLen, direction, density,
			   ps1, ps2, nozzles, UNKNOWN1VAL,
			   columnExtent, _1stColumn, lastColumn );
    }
    fwrite( swipeBuf,1, outp - swipeBuf, prn_stream );

    for ( colour = _1stColour; colour <= lastColour; colour++ )
	for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	{
				/* Scan the buffer for the next print line,
				   if present.  If not, set sentinel value. */
	    if ( pens[colour][pen].nextPrintLine
		 <= pens[colour][pen].bottomLine
		 || pens[colour][pen].nextPrintLine >= *nextLineToGet )
	    {
		pens[colour][pen].nextPrintLine =
		    			pens[YELLOW_X][LO_PEN].finalLine;
		line = pens[colour][pen].bottomLine + 1;
		while ( line < *nextLineToGet )
		{
		    lineIndex	= (uchar)( line & COLOUR_BUF_MASK );
		    if ( ! lineEmpty[colour][ lineIndex ] )
		    {
			pens[colour][pen].nextPrintLine = line;
			break;
		    }
		    line += lx5000dev->lineIncrement;
		}
	    }
	}
}

/*----------------------------------------------------------------------*
 *	l x 5 0 0 0 _ p r i n t _ p a g e ( )				*
 *----------------------------------------------------------------------*
  Send the page to the printer.
 *----------------------------------------------------------------------*/
private int
lx5000_print_page( gx_device_printer *pdev, FILE *prn_stream )
{
    /*
     Data structures for the buffer:
     
     ------------------------------------------------------------------*
     			C O L O U R   B U F F E R S
     ------------------------------------------------------------------*
     The colour buffers contain the individual colour bits that will
     actually be passed to the print heads; therefore, there is one bit
     per printable element.  So far, the correspondence between the
     gx_color_index elements in the scan line and the individual print
     bits is 1-to-1.
     
     For the colour driver, the colour buffers will be:
     		Cbuffer, Mbuffer, Ybuffer and Kbuffer
     while for the black driver, only Kbuffer is used.
     (See BUFFER HANDLING MACROS.)
     
     ------------------------------------------------------------------*
     		D E T E R M I N I N G   B U F F E R   S I Z E
     ------------------------------------------------------------------*
     The colour buffers are circular buffers: in order to implement this
     characteristic, the size of the buffer must be 2^n.  The buffers may
     then be indexed by the scan line number, masked by 2^n - 1.
     
     The buffer must be able to accommodate the maximum range of scan
     lines that might be affected by a single swipe command.  For colour
     pens this is 240 ( 3 x 64 pens + 2 x 24 inter-pen gaps).  For the
     black pen, this is 208.  Therefore, the minimum buffer size is 256.
     In fact, this is probably also the optimal buffer size, as there seems
     to be no advantage in buffering twice the number of scan line required
     to hold any individual swipe.
     
     ------------------------------------------------------------------*
     	   S T R U C T U R E   O F   T H E   C O L O U R   B U F F E R S
     ------------------------------------------------------------------*
     The colour buffers hold the data for individual colour printheads,
     extracted from the original data provided as scan lines.  When the
     data for each colour is extracted from the scan line, it is placed
     into a line of the colour buffer corresponding to the scan line.
     
     Within each line of the colour buffer, the data is structured like so:
     +-------+------------------- ~ ---------------------+-------+
     |  Pad  |    Scan line data bits for one colour     |  Pad  |
     | bytes |                                           | bytes |
     +-------+-------------------------------------------+-------+
     
     When the scan line data is first processed, the colour data is copied
     into the colour buffer in the position indicated above.  This line of
     data is padded on both ends.  The size of the pad is sufficient to
     account for the number of bits of head separation between the EVEN
     and ODD rows of nozzles in each print head; by default 16.
     
     The individual lines are accessed through an array of colourLines struct-
     ures.  This is defined in the TYPEDEFS & EXTERNAL STORAGE secion above.
     
     The colourLines structure contains a byte pointer which points to one of
     the bytes of an individual colour buffer line, and an unsigned char
     which is a single bit mask isolating one bit of the byte pointed to by
     the other element.
     		typedef struct bufBit_s {
     			byte *	xByte;
     			uchar	xBit;
     		} bufBit;
     
     The colourLines structure holds one instance of a bufBit; it points to
     the first bit of the scan line.  In general, the bit pointers for the
     first bit of the EVEN columns will point to the beginning of the
     extracted scan line data.  For the ODD columns, the first bit
     will point into the leading PAD bytes by 16 bits before the start of
     the actual scan line data.
     
     In this way, all of the complications of managing the separation
     between the EVEN and ODD columns on the heads is contained in the
     set-up procedures for the pointers in the scanLines array.  If it is
     required to reverse the adjustments of EVEN and ODD columns (for bi-
     directional printing, for example) this is achieved by adjusting the
     firstBit entries in this array.
     
     That, at any rate, is the theory.  It seemed like a good idea at the
     time.
      
     */

    static const unsigned char init_string[] = {
	0xa5, 0, 6, 0x40, 3, 3, 0xc0, 0x0f, 0x0f,
	0xa5, 0, 3, 0x40, 4, 5,
	0xa5, 0, 3, 0x40, 4, 6,
	0xa5, 0, 3, 0x40, 4, 7,
	0xa5, 0, 3, 0x40, 4, 8,
	0xa5, 0, 4, 0x40, 0xe0, 0xb, 3,
	0xa5, 0, 11, 0x40, 0xe0, 0x41, 0, 0, 0, 0, 0, 0, 0, A5_11_5000,
	0xa5, 0, 6, 0x40, 5, 0, 0, 0x80, 0,
	0x1b, '*', 7, 0x73, 0x30,
	0x1b, '*', 'm', 0, 0x14, 3, 0x84, 2, 0, 1, 0xf4,
	0x1b, '*', 7, 0x63,
	0x1b, '*', 'm', 0, 0x42, 0, 0,
	0xa5, 0, 5, 0x40, 0xe0, 0x80, 8, 7
    };

    byte 	*lineBuffer;
    byte 	*swipeBuffer;
    byte 	*colourBufs[ NUM_COLOURS ];
    bufBit	colourLines[NUM_COLOURS][PENS_PER_COLOUR][COLOUR_BUF_LINES];
    bool	lineEmpty[ NUM_COLOURS ][ COLOUR_BUF_LINES ];
    penData	pens[ NUM_COLOURS ][ PENS_PER_COLOUR ];
				/* nextLineToPrint is the top unprinted line
				   in the buffers.  It is adjusted as lines
				   are processed and printed.  Its sentinel
				   value is pens[YELLOW_X][LO_PEN].finalLine.
				   When any line
				   which is not blank is read from GS &
				   processed, nextLineToPrint will be set to
				   this line if it is less than the current
				   value.

				   nextLineToGet is the next scanline to read
				   from GS.				*/
    int		nextLineToPrint;
    int		nextLineToGet	= 0;
				/* The current bottom line position of
					  the colour cartridge.		*/
    int		bottomYellowLine;
    int		retval;
     
    lx5000_device *lx5000dev	= (lx5000_device *)pdev;

    nextLineToPrint	= pens[YELLOW_X][LO_PEN].finalLine;
    bottomYellowLine	= INITIAL_YELLOW_BOTTOM_LINE;
				/* Allocate the buffer storage */
    if ( ( retval = getColourBufs( lx5000dev, &lineBuffer, colourBufs,
				   &swipeBuffer, ALLOCATE ) )) 
	return retval;

    initPenConstants( lx5000dev, pens );
			/* Initialize the lineEmpty[] and pens[] arrays -
			   should happen during parameter setup process */
    {
	int	colour, pen, line;

	for ( colour = 0;
	      colour < lx5000dev->color_info.num_components;
	      colour++ )
	{
	    for ( pen = 0; pen < lx5000dev->pensPerColour; pen++ )
	    {
		pens[colour][pen].nextPrintLine =
		    			pens[YELLOW_X][LO_PEN].finalLine;

		pens[colour][pen].bottomLine	=
					pens[colour][pen].initialBottomLine;
		pens[colour][pen].topLine	=
			bottomYellowLine
			- pens[colour][pen].topToBottomYellow;
	    }
	    for ( line = 0; line < COLOUR_BUF_LINES; line++ )
		/* Force the zeroing of the line first time through */
		lineEmpty[colour][line]	= false;
	}
    }

				/* Initialize the printer and reset margins. */
    pageInit( (uchar)lx5000dev->alignA, (uchar)lx5000dev->alignB, prn_stream );
    fwrite( init_string, 1, sizeof( init_string ), prn_stream );

	/*--------------------------------------------------------------*
	 Assume that COLOUR_BUF_LINES of data are available, starting with
	 nextLineToPrint.  Work out which cartridge will be doing the
	 printing, and how far it will have to move.

	 Something is available to be printed.  This may (should?) involve
	 initial paper movement to line up the appropriate pen.  Deciding
	 on when to move the paper and by how much is tricky.  One aim is
	 to minimize paper movements.

	 Check for black vs colour printing first.  If the required
	 movement for any of the colour pens is less than the required
	 movement for the black pen, print a colour swathe, adjust all of
	 the colour buffer values, and the nextLineToPrint value if
	 necessary.

	 Then refreshBuffer() and take another look.  Sooner or later,
	 the next required movement of the colour cartridge will exceed
	 the movement required of the black cartridge.  Print a black
	 swathe.  Whether to move the black cartridge for this print is
	 determined by checking on the subsequent colour cartridge
	 movement.

	 If the difference between the required movement for the black
	 and the required movement for the colour is less than or equal
	 to one colour pen depth, do not move the pen before printing the
	 black swathe.

	 Adjust black buffer values, including nextLineToPrint, which will
	 (almost?) certainly change after a black print.  refreshBuffer()
	 and try again.

	 A note on 192 vs 208 black swathes.  I can see the reasons for
	 using the 192 swathes.  When printing solid blocks with some of
	 each colour and black on each scan line, use of 192 nozzles
	 obviates the need for any black-related head movement.  A black
	 swathe can be printed on every third movement of the heads, and,
	 in the circumstances sketched above, will use all 192 nozzles.

	 Obviously, the same simple algorithm can be used when the colours
	 are less densely packed.  The Alignment B value, coded into the
	 header of every swipe command, automatically adjusts the first
	 pen of the set of 192 within a range of 0-15, so that the 192
	 nozzles are symmetrically arranged with respect to the colour
	 pens.  The complication is that the EVEN/ODD status of the
	 nozzles in the 208 set is unaffected, even though the numbering
	 may change.

	 It originally seemed to me that it was completely unnecessary
	 to use the 192 nozzle set.  The alignment value was needed to
	 get the positioning right, but for all other purposes, the
	 208 set could be used, and adjustments made on the fly.
	 However, I when considering the printing of pseudo 1200Y
	 pages, which involves the use of half-height pseudo-pens, it
	 became clear that the calculation of the half-height of a
	 pens would be greatly simplified by the use of 192 nozzles.
	 This would leave the height of a pseudo-pen at an integral
	 number of 16-bit datawords.

	 As a result, I will use 192 nozzles only when printing at
	 Nx1200Ydpi.

	 *--------------------------------------------------------------*/
    refreshBuffer( lx5000dev, &nextLineToGet, &nextLineToPrint,
		   lineBuffer, colourBufs, colourLines, pens, lineEmpty );
    while ( nextLineToPrint < pdev->height )
    {
	int	blackDirection	= RIGHTWARD;
	int	colourDirection	= LEFTWARD;
				/* Target of next move of colour cartridge */
	int	nextColourLine;
	int	nextBlack208Line = cartridgeMoveTo( lx5000dev, pens,
							  BLACK_X,
							  BLACK_X );

				/* In Black-only mode, cartridgeMoveTo
				   will return an off-page value for the
				   colour pens.			*/
	nextColourLine = cartridgeMoveTo( lx5000dev, pens,
						   _1ST_CMY_COLOUR,
						   LAST_CMY_COLOUR );
	if (  lx5000dev->isCMYK  && nextColourLine <= nextBlack208Line )
	{			/* Move the head & print a 3-colour swathe */
	    feedPaper( lx5000dev, nextColourLine, &bottomYellowLine, pens,
		       prn_stream );
	    printSwipe( lx5000dev, colourBufs, colourLines, pens,
			 lineEmpty, swipeBuffer,
			 &nextLineToPrint, &nextLineToGet, colourDirection,
			 _1ST_CMY_COLOUR, LAST_CMY_COLOUR,
			 prn_stream );
	}
	else
	{
	    feedPaper( lx5000dev, nextBlack208Line, &bottomYellowLine,
			   pens, prn_stream );
	    printSwipe( lx5000dev, colourBufs, colourLines, pens,
			lineEmpty, swipeBuffer,
			&nextLineToPrint, &nextLineToGet, blackDirection,
			BLACK_X, BLACK_X,
			prn_stream );
	}
	refreshBuffer( lx5000dev, &nextLineToGet, &nextLineToPrint,
		       lineBuffer, colourBufs, colourLines, pens, lineEmpty );
    } /* ends the loop for swipes of the print head.*/

				/* Eject the page, reinitialize the printer */
    pageEnd( prn_stream );
    fflush( prn_stream );
    getColourBufs( lx5000dev, &lineBuffer, colourBufs, &swipeBuffer,
		   DEALLOCATE );
    if ( lx5000dev->dryTime )
	sleep( lx5000dev->dryTime );

    return 0;
}

/* 
 * There are a number of parameters which can differ between ink cartridges. 
 * The Windows driver asks you to recalibrate every time you load a new
 * cartridge.
 *
 * [The Lexmark 5700 black
 * cartridge has two columns of dots, separated by about 16 pixels.
 * This `head separation' distance can vary between cartridges, so
 * we provide a parameter to set it.  In my small experience I've not
 * set the corresponding parameter in windows to anything greater than 17
 * or smaller than 15, but it would seem that it can vary from 1 to 32,
 * based on the calibration choices offered.
 *		Stephen Taylor's comment on the 5700 ]
 *
 * N.B.  The above discussion relates to the 5700.  On the 5000, in the
 *  Windows driver at any rate, there is no adjustment for the pixel
 *  distance between the rows of jets on the one print head.
 *
 * There are, however, two values which are always encoded into the initial
 *  escape sequence that the Windows driver sends to the printer for each
 *  new page: alignA the horizontal alignment between the colour and the
 *  black pens, and alignB, the vertical alignment between the same pens.
 *
 * AlignC is a black-only alignment, which seems to have something to do
 *  with bi-directional alignment.
 *
 * AlignD is a colour-only alignment, corresponding to AlignC.
 *
 * [As I understand the rules laid out in gsparams.h,
 * lx5000_get_params is supposed to return the current values of parameters
 * and lx5000_put_params is supposed to set up values in the lx5000_device
 * structure which can be used by the lx5000_print_page routine.
 * I've copied my routines from gdevcdj.c
 *		Stephen Taylor]
 *
 * DryingTime is a delay to allow page drying before a new page is fed
 * through.
 *
 * CMYK is a boolean parameter which specifies colour capability.  Set
 *  -dCMYK=false for black-only printing.
 *
 * See lx5000_put_params() for the legal ranges of values for these parameters.
 *
 */

/*----------------------------------------------------------------------*
 *	l x 5 0 0 0 b _ g e t _ p a r a m s ( )				*
 *----------------------------------------------------------------------*/
private int
lx5000_get_params( gx_device *pdev, gs_param_list *plist )
/*----------------------------------------------------------------------*/
{       
    lx5000_device *lx5000dev	= (lx5000_device *)pdev;
    int code			= gdev_prn_get_params( pdev, plist );

    if ( code < 0 ||
	 ( code =
	  param_write_int( plist, "HeadSeparation",
			  &lx5000dev->headSeparation )) < 0 ||
	 ( code = param_write_int(plist, "AlignA", &lx5000dev->alignA)) < 0 ||
	 ( code = param_write_int(plist, "AlignB", &lx5000dev->alignB)) < 0 ||
	 ( code = param_write_bool( plist, "CMYK", &lx5000dev->isCMYK )) < 0 ||
	 ( code = param_write_bool( plist,
				    "DryingTime", &lx5000dev->dryTime )) < 0
	 )
	return code;
           
    return code;
}

/*----------------------------------------------------------------------*
 *	l x 5 0 0 0 b _ p u t _ p a r a m _ i n t ( )			*
 *----------------------------------------------------------------------*/
private int
lx5000_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 0:
	if ( value < minval || value > maxval )
	{
	    code =
		param_signal_error( plist, pname, gs_error_rangecheck );
	    ecode = ( code < 0 ? code : ecode );
	}
	else
	    *pvalue = value;
    case 1:
	return ecode;
    }
}

/*----------------------------------------------------------------------*
 *	l x 5 0 0 0 b _ p u t _ p a r a m _ b o o l ( )			*
 *----------------------------------------------------------------------*/
private int
lx5000_put_param_bool(
		      gs_param_list *plist, gs_param_name pname,
		      bool *pvalue,  int ecode
		      )
/*----------------------------------------------------------------------*/
{
    int 	code;

    switch ( code = param_read_bool( plist, pname, pvalue ) )
    {
    default:
	ecode = code;
	param_signal_error(plist, pname, ecode);
	return code;
    case 1:
    case 0:
	return ecode;
    }
}

/*----------------------------------------------------------------------*
 *	l x 5 0 0 0 b _ p u t _ p a r a m s ( )				*
 *----------------------------------------------------------------------*/
private int
lx5000_put_params( gx_device *pdev, gs_param_list *plist )
/*----------------------------------------------------------------------*/
/* put_params is supposed to check all the parameters before setting any. */
/*----------------------------------------------------------------------*/
{
    lx5000_device	*lx5000dev = (lx5000_device *)pdev;

    int 	ecode, code	= 0;
    int 	headSeparation	= lx5000dev->headSeparation;
    int 	alignA		= lx5000dev->alignA;
    int 	alignB		= lx5000dev->alignB;
    int		dryTime		= lx5000dev->dryTime;
    int		bitsPerPixel	= lx5000dev->isCMYK ? 4 : 1;
    int		oldBPP		= bitsPerPixel;
    bool	isCMYK		= lx5000dev->isCMYK;
    bool	isCMYK_old	= isCMYK;

    code = lx5000_put_param_int( plist, "HeadSeparation", &headSeparation,
				 8, 24, code ); /* 8 - 24 columns */

    code = lx5000_put_param_int( plist, "AlignA", &alignA, 0, 30, code );
				/* 0 - 30 columns */

    code = lx5000_put_param_int( plist, "AlignB", &alignB, 0, 15, code );
				/* 0 -15 nozzles */

    code = lx5000_put_param_int( plist, "DryingTime", &dryTime, 0, 60, code );
				/* 0 -60 seconds */

    code = lx5000_put_param_int
	( plist, "BitsPerPixel", &bitsPerPixel, 1, 4, code );
				/* Black cartridge only */

    code = lx5000_put_param_bool( plist, "CMYK", &isCMYK, code );
				/* Black cartridge only */

    /* Take precautions against input errors */
    if ( bitsPerPixel != 1 ) { bitsPerPixel = 4; }

    if ( isCMYK != isCMYK_old || bitsPerPixel != oldBPP )
    {
	bool	isCMYK_new;
	/* What has changed?  Assume that initial values were consistent,
	   and set isCMYK to a value consistent with the changed args
	   If both have changed, give precedence to isCMYK
	*/
	if ( bitsPerPixel != oldBPP )
	{
	    if ( bitsPerPixel == 1 )
	    { isCMYK_new = false; }
	    else
	    { isCMYK_new = true; };
	}
	if ( isCMYK != isCMYK_old )
	{ isCMYK_new = isCMYK; }

	isCMYK = isCMYK_new;
	bitsPerPixel = isCMYK ? 4 : 1;

	if ( isCMYK )
	{
	    lx5000dev->color_info = color_info_cmy;
	    dev_proc(pdev, map_cmyk_color) = lx5000_map_cmyk_color;
	    dev_proc(pdev, map_rgb_color) = NULL;
	    dev_proc(pdev, map_color_rgb) = lx5000_map_color_rgb;
	}
	else
	{
	    lx5000dev->color_info = color_info_blk;
	    dev_proc(pdev, map_cmyk_color) = NULL;
	    dev_proc(pdev, map_rgb_color) = gdev_prn_map_rgb_color;
	    dev_proc(pdev, map_color_rgb) = gdev_prn_map_color_rgb;
	}
	if ( pdev->is_open )
	    gs_closedevice(pdev);
    }

				/* call super class put_params */
    ecode = gdev_prn_put_params( pdev, plist );

    if ( ecode < 0 || code < 0 )
    {
	if ( isCMYK != isCMYK_old )
	{
	    if ( ! isCMYK )
	    {
		lx5000dev->color_info = color_info_cmy;
		dev_proc(pdev, map_cmyk_color) = lx5000_map_cmyk_color;
		dev_proc(pdev, map_rgb_color) = NULL;
		dev_proc(pdev, map_color_rgb) = lx5000_map_color_rgb;
	    }
	    else
	    {
		lx5000dev->color_info = color_info_blk;
		dev_proc(pdev, map_cmyk_color) = NULL;
		dev_proc(pdev, map_rgb_color) = gdev_prn_map_rgb_color;
		dev_proc(pdev, map_color_rgb) = gdev_prn_map_color_rgb;
	    }
	    if ( pdev->is_open )
		gs_closedevice(pdev);
	}
	return ecode < 0 ? ecode : code;
    }

				/* looks like everything okay; */
				/* go ahead and set parameters */
    lx5000dev->headSeparation = headSeparation;
    lx5000dev->alignA = alignA;
    lx5000dev->alignB = alignB;
    lx5000dev->dryTime = dryTime;
    lx5000dev->isCMYK = isCMYK;
				/* N.B. I am only setting these values here -
				   should they also be set as part of the "two
				   phase commit" of parameter changes?  My
				   code is the only place these things are
				   accessed.	*/
    if ( lx5000dev->y_pixels_per_inch == MAX_LX5000_Y )
    {
	lx5000dev->pensPerColour	= 2;
	lx5000dev->lineIncrement	= 2;
    }
    else
    {
	lx5000dev->pensPerColour	= 1;
	lx5000dev->lineIncrement	= 1;
    }
    if ( code == 1 ) return ecode; 
    return 0;
}


/*----------------------------------------------------------------------*
 * The following colour handling procedures are lifted from gdevbit.c
 *----------------------------------------------------------------------*/
/* Map color to RGB.  This has 3 separate cases, but since it is rarely */
/* used, we do a case test rather than providing 3 separate routines. */
private int
lx5000_map_color_rgb(gx_device * dev, gx_color_index color,
		     gx_color_value rgb[3])
{
    int depth = dev->color_info.depth;
    int ncomp = dev->color_info.num_components;
    int bpc = depth / ncomp;
    uint mask = (1 << bpc) - 1;

#define cvalue(c) ((gx_color_value)((ulong)(c) * gx_max_color_value / mask))

    /* Map CMYK back to RGB. */

    gx_color_index cshift = color;
    uint c, m, y, k;

    k = cshift & mask;
    cshift >>= bpc;
    y = cshift & mask;
    cshift >>= bpc;
    m = cshift & mask;
    c = cshift >> bpc;
    /* We use our improved conversion rule.... */
    rgb[0] = cvalue((mask - c) * (mask - k) / mask);
    rgb[1] = cvalue((mask - m) * (mask - k) / mask);
    rgb[2] = cvalue((mask - y) * (mask - k) / mask);

    return 0;
#undef cvalue
}

/* Map CMYK to color. */
private gx_color_index
lx5000_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{
    int bpc = dev->color_info.depth / 4;
    int drop = sizeof(gx_color_value) * 8 - bpc;
    gx_color_value cyan = cv[0], magenta = cv[1], yellow = cv[2], black = cv[3];
    gx_color_index color =
    ((((((cyan >> drop) << bpc) +
	(magenta >> drop)) << bpc) +
      (yellow >> drop)) << bpc) +
    (black >> drop);

    return (color == gx_no_color_index ? color ^ 1 : color);
}

/*=============== Clean up my #define's =================*/
#undef A5_11_5000
#undef A5_11_5700
#undef ALIGN_A_DEF
#undef ALIGN_A_OFFSET
#undef ALIGN_B_DEF
#undef ALIGN_B_OFFSET
#undef HEADSEP_DEF
#undef DRY_TIME_DEF
#undef LX_UNI
#undef MIN_LX5000_X
#undef MAX_LX5000_X
#undef DEF_LX5000_X
#undef MIN_LX5000_Y
#undef MAX_LX5000_Y
#undef DEF_LX5000_Y
#undef LX5000_XDPI
#undef LX5000_YDPI
#undef FEED_FACTOR
#undef LX5000_XOFFSET_TO_0_0_XDPI
#undef LX5000_YOFFSET_TO_0_0_YDPI
#undef LX5000_XOFFSET_TO_0_0
#undef LX5000_YOFFSET_TO_0_0
#undef LX5000_LEFT_HWMARGIN_INS
#undef LX5000_BOTTOM_HWMARGIN_INS
#undef LX5000_RIGHT_HWMARGIN_INS
#undef LX5000_TOP_HWMARGIN_INS
#undef LINE_PAD_BYTES
#undef RIGHTWARD
#undef LEFTWARD
#undef SWIPE_WORD_BITS
#undef BLACK_NOZZLES
#undef _1COLOUR_NOZZLES
#undef BLK_COLOUR_NOZZLES
#undef BLK_SWIPE_WORDS
#undef COLOUR_SWIPE_WORDS
#undef _1COLOUR_WORDS
#undef BLK_DIRECTORY_MASK
#undef COLOUR_DIRECTORY_MASK
#undef DIRECTORY_TYPE_BIT
#undef COLOUR_PEN_GAP
#undef COLOUR_PEN_DIFF
#undef INITIAL_YELLOW_BOTTOM_LINE
#undef COLOUR_BUF_MASK
#undef COLOUR_BUF_LINES
#undef SWIPE_LEADER
#undef CMDLEN_X
#undef DIRECTION_X
#undef UNIDIRECTIONAL
#undef BIDIRECTIONAL
#undef HEADSPEED_X
#undef _300X
#undef _600X
#undef _1200Y
#undef PEN_X
#undef BLACK0
#undef BLACK1
#undef COLOUR0
#undef COLOUR1
#undef NOZZLE_COUNT_X
#undef _192NOZZLES
#undef _208NOZZLES
#undef UNKNOWN1_X
#undef UNKNOWN1VAL
#undef NUM_COLUMNS_X
#undef _1ST_COLUMN_X
#undef LAST_COLUMN_X
#undef SWIPE_HDR_END_X
#undef SWIPE_HDR_END_LEN
#undef SWIPE_HDR_END
#undef SWIPE_HDR_LEN
#undef BLACK_X
#undef YELLOW_X
#undef MAGENTA_X
#undef CYAN_X
#undef LO_PEN
#undef HI_PEN
#undef NUM_COMPONENTS_BLK
#undef BITS_PER_PIXEL_BLK
#undef MAX_GREY_BLK
#undef MAX_RGB_BLK
#undef DITHER_GREYS_BLK
#undef DITHER_COLOURS_BLK
#undef NUM_COMPONENTS_CMY
#undef BITS_PER_PIXEL_CMY
#undef MAX_GREY_CMY
#undef MAX_RGB_CMY
#undef DITHER_GREYS_CMY
#undef DITHER_COLOURS_CMY
#undef MIN_COLOUR
#undef MAX_COLOUR
#undef BLACK_PEN
#undef _1ST_CMY_COLOUR
#undef LAST_CMY_COLOUR
#undef BPP
#undef PIXEL_MASK
#undef INITIAL_PIXEL_SHIFT
#undef NUM_COLOURS
#undef PENS_PER_COLOUR
#undef NUM_PENS
#undef BITS_PER_COLOUR
#undef DEF_NUM_COLOURS
#undef DEF_PENS_PER_COLOUR
#undef DEF_LINE_INCREMENT
#undef PIXELS_PER_BYTE
#undef COLOUR_MASK
#undef ALLOCATE
#undef DEALLOCATE
#undef FILL_SWIPE_HEADER
#undef BIT_TO_MASK
#undef INC_BIT
#undef DEC_BIT
#undef lx5000_proctab