summaryrefslogtreecommitdiff
path: root/test/pcl-unprint.c
blob: 7dfd31c2a7fd15ce490476717f80a710258987e0 (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
/*
 *   pclunprint.c - convert an HP PCL file into an image file for viewing.
 *
 *   Copyright 2000 Dave Hill (dave@minnie.demon.co.uk)
 *
 *   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, see <https://www.gnu.org/licenses/>.
 *
 * Revision History:
 *
 *   See ChangeLog
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gutenprint/util.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

/*
 * Size of buffer used to read file
 */
#define READ_SIZE 16384

/*
 * Largest data attached to a command. 1024 means that we can have up to 8192
 * pixels in a row
 */
#define MAX_DATA 16384

FILE *read_fd,*write_fd;
char read_buffer[READ_SIZE];
char data_buffer[MAX_DATA];
char initial_command[3];
int initial_command_index;
char final_command;
int numeric_arg;
int max_data_len = 0;

int read_pointer;
int read_size;
int eof;
int combined_command = 0;
int skip_output = 0;

/*
 * Data about the image
 */

typedef struct {
    int colour_type;		/* Mono, 3/4 colour */
    int pixel_type;		/* Mono, CMY, RGB */
    int black_depth;		/* 2 level, 4 level */
    int cyan_depth;		/* 2 level, 4 level */
    int magenta_depth;		/* 2 level, 4 level */
    int yellow_depth;		/* 2 level, 4 level */
    int lcyan_depth;		/* 2 level, 4 level */
    int lmagenta_depth;		/* 2 level, 4 level */
    int image_width;
    int image_height;
    int compression_type;	/* Uncompressed or TIFF */
} image_t;

/*
 * collected data read from file
 */

typedef struct {
    char **black_bufs;			/* Storage for black rows */
    int black_data_rows_per_row;	/* Number of black rows */
    char **cyan_bufs;
    int cyan_data_rows_per_row;
    char **magenta_bufs;
    int magenta_data_rows_per_row;
    char **yellow_bufs;
    int yellow_data_rows_per_row;
    char **lcyan_bufs;
    int lcyan_data_rows_per_row;
    char **lmagenta_bufs;
    int lmagenta_data_rows_per_row;
    int buffer_length;
    int active_length;			/* Length of output data */
    int output_depth;
    int input_depth;
    int pixels_depth;
} output_t;

#define PCL_MONO 1
#define PCL_CMY 3
#define PCL_CMYK 4
#define PCL_CMYKcm 6
#define PCL_RGB 65

#define PCL_COMPRESSION_NONE 0
#define PCL_COMPRESSION_RUNLENGTH 1
#define PCL_COMPRESSION_TIFF 2
#define PCL_COMPRESSION_DELTA 3
#define PCL_COMPRESSION_ADAPTIVE 5
#define PCL_COMPRESSION_CRDR 9		/* Compressed row delta replacement */

/* PCL COMMANDS */

typedef enum {
	PCL_RESET = 1,
	PCL_MEDIA_SIZE,
	PCL_PERF_SKIP,
	PCL_TOP_MARGIN,
	PCL_MEDIA_TYPE,
	PCL_MEDIA_SOURCE,
	PCL_SHINGLING,
	PCL_RASTERGRAPHICS_QUALITY,
	PCL_DEPLETION,
	PCL_CONFIGURE,
	PCL_RESOLUTION,
	PCL_COLOURTYPE,
	PCL_COMPRESSIONTYPE,
	PCL_LEFTRASTER_POS,
	PCL_TOPRASTER_POS,
	PCL_RASTER_WIDTH,
	PCL_RASTER_HEIGHT,
	PCL_START_RASTER,
	PCL_END_RASTER,
	PCL_END_COLOUR_RASTER,
	PCL_DATA,
	PCL_DATA_LAST,
	PCL_PRINT_QUALITY,
	PCL_ENTER_PJL,
	PCL_GRAY_BALANCE,
	PCL_DRIVER_CONFIG,
	PCL_PAGE_ORIENTATION,
	PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS,
	PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS,
	PCL_UNIT_OF_MEASURE,
	PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT,
	PCL_PALETTE_CONFIGURATION,
	PCL_LPI,
	PCL_CPI,
	PCL_PAGE_LENGTH,
	PCL_NUM_COPIES,
	PCL_DUPLEX,
	PCL_MEDIA_SIDE,
	PCL_RTL_CONFIGURE,
	PCL_ENTER_PCL,
	PCL_ENTER_HPGL2,
	PCL_NEGATIVE_MOTION,
	PCL_MEDIA_DEST,
	PCL_JOB_SEPARATION,
	PCL_LEFT_OFFSET_REGISTRATION,
	PCL_TOP_OFFSET_REGISTRATION,
	PCL_PRINT_DIRECTION,
	PCL_LEFT_MARGIN,
	PCL_RIGHT_MARGIN,
	PCL_RESET_MARGINS,PCL_TEXT_LENGTH,
	/* Added by rlk 2017-12-25 */
	PCL_RASTER_RENDER,	/* Rendering algorithm */
	PCL_COLOR_COMPONENT_1,	/* Color component 1 */
	PCL_COLOR_COMPONENT_2,	/* Color component 2 */
	PCL_COLOR_COMPONENT_3,	/* Color component 3 */
	PCL_ASSIGN_COLOR_INDEX,
	PCL_SET_FOREGROUND_COLOR,
	PCL_SET_VMI,
	PCL_SEED_ROW_SOURCE,
} command_t;

typedef struct {
  const char initial_command[3];		/* First part of command */
  const char final_command;			/* Last part of command */
  int has_data;					/* Data follows */
  int clean_state;				/* Leaves printer state clean */
  command_t command;				/* Command name */
  const char *description;			/* Text for printing */
} commands_t;

const commands_t pcl_commands[] =
    {
/* Two-character sequences ESC <x> */
	{ "E", '\0', 0, 1, PCL_RESET, "PCL RESET" },
	{ "9", '\0', 0, 0, PCL_RESET_MARGINS, "Reset Margins" },
	{ "%", 'A', 0, 0, PCL_ENTER_PCL, "PCL mode" },
	{ "%", 'B', 0, 0, PCL_ENTER_HPGL2, "HPGL/2 mode" },
	{ "%", 'X', 0, 0, PCL_ENTER_PJL, "UEL/Enter PJL mode" },
/* Parameterised sequences */
/* Raster positioning */
	{ "&a", 'G', 0, 0, PCL_MEDIA_SIDE, "Set Media Side" },
	{ "&a", 'H', 0, 0, PCL_LEFTRASTER_POS, "Left Raster Position" },
	{ "&a", 'L', 0, 0, PCL_LEFT_MARGIN, "Left Margin by Column" },
	{ "&a", 'M', 0, 0, PCL_RIGHT_MARGIN, "Right Margin by Column" },
	{ "&a", 'N', 0, 0, PCL_NEGATIVE_MOTION, "Negative Motion" },
	{ "&a", 'P', 0, 0, PCL_PRINT_DIRECTION, "Print Direction" },
	{ "&a", 'V', 0, 0, PCL_TOPRASTER_POS, "Top Raster Position" },
/* Characters */
	{ "&k", 'H', 0, 0, PCL_CPI, "Characters per Inch" },
/* Media */
	{ "&l", 'A', 0, 0, PCL_MEDIA_SIZE , "Media Size" },
	{ "&l", 'C', 0, 0, PCL_SET_VMI, "Set Vertical Line Spacing" },
	{ "&l", 'D', 0, 0, PCL_LPI , "Lines per Inch" },
	{ "&l", 'E', 0, 0, PCL_TOP_MARGIN , "Top Margin" },
	{ "&l", 'F', 0, 0, PCL_TEXT_LENGTH , "Text Length" },
	{ "&l", 'G', 0, 0, PCL_MEDIA_DEST, "Media Destination" },
	{ "&l", 'H', 0, 0, PCL_MEDIA_SOURCE, "Media Source" },
	{ "&l", 'L', 0, 0, PCL_PERF_SKIP , "Perf. Skip" },
	{ "&l", 'M', 0, 0, PCL_MEDIA_TYPE , "Media Type" },
	{ "&l", 'O', 0, 0, PCL_PAGE_ORIENTATION, "Page Orientation" },
	{ "&l", 'P', 0, 0, PCL_PAGE_LENGTH, "Page Length in Lines" },
	{ "&l", 'S', 0, 0, PCL_DUPLEX, "Duplex mode" },
	{ "&l", 'T', 0, 0, PCL_JOB_SEPARATION, "Job Separation" },
	{ "&l", 'U', 0, 0, PCL_LEFT_OFFSET_REGISTRATION, "Left Offset Registration" },
	{ "&l", 'X', 0, 0, PCL_NUM_COPIES, "Number of copies" },
	{ "&l", 'Z', 0, 0, PCL_TOP_OFFSET_REGISTRATION, "Top Offset Registration" },
/* Units */
	{ "&u", 'D', 0, 0, PCL_UNIT_OF_MEASURE, "Unit of Measure" },	/* from bpd05446 */
/* Raster data */
	{ "*b", 'B', 0, 0, PCL_GRAY_BALANCE, "Gray Balance" },	/* from PCL Developer's Guide 6.0 */
	{ "*b", 'M', 0, 0, PCL_COMPRESSIONTYPE, "Compression Type" },
	{ "*b", 'S', 0, 0, PCL_SEED_ROW_SOURCE, "Seed row # source" },
	{ "*b", 'V', 1, 0, PCL_DATA, "Data, intermediate" },
	{ "*b", 'W', 1, 0, PCL_DATA_LAST, "Data, last" },
	{ "*b", 'Y', 0, 0, PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT, "Relative Vertical Pixel Movement" },
/* Palette */
	{ "*d", 'W', 1, 0, PCL_PALETTE_CONFIGURATION, "Palette Configuration" },
/* Plane configuration */
	{ "*g", 'W', 1, 0, PCL_CONFIGURE, "Configure Raster Data" },
/* Raster Graphics */
	{ "*o", 'D', 0, 0, PCL_DEPLETION, "Depletion" },
	{ "*o", 'M', 0, 0, PCL_PRINT_QUALITY, "Print Quality" },
	{ "*o", 'Q', 0, 0, PCL_SHINGLING, "Raster Graphics Shingling" },
	{ "*o", 'W', 1, 0, PCL_DRIVER_CONFIG, "Driver Configuration Command" },
/* Cursor Positioning */
	{ "*p", 'X', 0, 0, PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS, "Horizontal Cursor Positioning by Dots" },
	{ "*p", 'Y', 0, 0, PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS, "Vertical Cursor Positioning by Dots" },
/* Raster graphics */
	{ "*r", 'A', 0, 0, PCL_START_RASTER, "Start Raster Graphics" },
	{ "*r", 'B', 0, 0, PCL_END_RASTER, "End Raster Graphics"},
	{ "*r", 'C', 0, 0, PCL_END_COLOUR_RASTER, "End Colour Raster Graphics" },
	{ "*r", 'Q', 0, 0, PCL_RASTERGRAPHICS_QUALITY, "Raster Graphics Quality" },
	{ "*r", 'S', 0, 0, PCL_RASTER_WIDTH, "Raster Width" },
	{ "*r", 'T', 0, 0, PCL_RASTER_HEIGHT, "Raster Height" },
	{ "*r", 'U', 0, 0, PCL_COLOURTYPE, "Colour Type" },
/* Resolution */
	{ "*t", 'R', 0, 0, PCL_RESOLUTION, "Resolution" },
	{ "*t", 'J', 0, 0, PCL_RASTER_RENDER, "Render Algorithm" },
/* RTL/PCL5 */
	{ "*v", 'W', 1, 0, PCL_RTL_CONFIGURE, "RTL Configure Image Data" },
	{ "*v", 'A', 0, 0, PCL_COLOR_COMPONENT_1, "Color Component 1" },
	{ "*v", 'B', 0, 0, PCL_COLOR_COMPONENT_2, "Color Component 2" },
	{ "*v", 'C', 0, 0, PCL_COLOR_COMPONENT_3, "Color Component 3" },
	{ "*v", 'I', 0, 0, PCL_ASSIGN_COLOR_INDEX, "Assign Color Index" },
	{ "*v", 'S', 0, 0, PCL_SET_FOREGROUND_COLOR, "Set Foreground Color" },
   };

int pcl_find_command (void);
void fill_buffer (void);
void pcl_read_command (void);
void write_grey (output_t *output, image_t *image);
void write_pixel (output_t *output, image_t *image);
void write_colour (output_t *output, image_t *image);
int decode_tiff (char *in_buffer, int data_length, char *decode_buf,
                 int maxlen);
int decode_delta (char *in_buffer, int data_length, char *decode_buf,
		  int maxlen);
void pcl_reset (image_t *i);
int depth_to_rows (int depth);


/*
 * pcl_find_command(). Search the commands table for the command.
 */

int pcl_find_command(void)
{

    int num_commands = sizeof(pcl_commands) / sizeof(commands_t);
    int i;

    for (i=0; i < num_commands; i++) {
	if ((strcmp(initial_command, pcl_commands[i].initial_command) == 0) &&
	    (final_command == pcl_commands[i].final_command))
		return(i);
    }

    return (-1);
}

/*
 * fill_buffer() - Read a new chunk from the input file
 */

void fill_buffer(void)
{

    if ((read_pointer == -1) || (read_pointer >= read_size)) {
	read_size = (int) fread(&read_buffer, sizeof(char), READ_SIZE, read_fd);

#ifdef DEBUG
	fprintf(stderr, "Read %d characters\n", read_size);
#endif

	if (read_size == 0) {
#ifdef DEBUG
	    fprintf(stderr, "No More to read!\n");
#endif
	    eof = 1;
	    return;
	}
	read_pointer = 0;
    }
}

/*
 * pcl_read_command() - Read the data stream and parse the next PCL
 * command.
 */

void pcl_read_command(void)
{

    char c;
    int minus;
    int skipped_chars;

/*
   Precis from the PCL Developer's Guide 6.0:-

   There are two formats for PCL commands; "Two Character" and
   "Parameterised".

   A "Two Character" command is: ESC <x>, where <x> has a decimal
   value between 48 and 126 (inclusive).

   A "Parameterised" command is: ESC <x> <y> [n] <z>
   where x, y and z are characters, and [n] is an optional number.
   The character <x> has a decimal value between 33 and 47 (incl).
   The character <y> has a decimal value between 96 and 126 (incl).

   Some commands are followed by data, in this case, [n] is the
   number of bytes to read, otherwise <n> is a numeric argument.
   The number <n> can consist of +, - and 0-9 (and .).

   The character <z> is either in the range 64-94 for a termination
   or 96-126 for a combined command. (The ref guide gives these
   as "96-126" and "64-96" which cannot be right as 96 appears twice!)

   It is possible to combine parameterised commands if the
   command prefix is the same, e.g. ESC & l 26 a 0 L is the same as
   ESC & l 26 A ESC & l 0 L. The key to this is that the terminator for
   the first command is in the range 96-126 (lower case).

   There is a problem with the "escape command" (ESC %) as it does not
   conform to this specification, so we have to check for it specifically!
*/

#define PCL_DIGIT(c) (((unsigned int) c >= 48) && ((unsigned int) c <= 57))
#define PCL_TWOCHAR(c) (((unsigned int) c >= 48) && ((unsigned int) c <= 126))
#define PCL_PARAM(c) (((unsigned int) c >= 33) && ((unsigned int) c <= 47))
#define PCL_PARAM2(c) (((unsigned int) c >= 96) && ((unsigned int) c <= 126))
#define PCL_COMBINED_TERM(c) (((unsigned int) c >= 96) && ((unsigned int) c <= 126))
#define PCL_TERM(c) (((unsigned int) c >= 64) && ((unsigned int) c <= 94))
#define PCL_CONVERT_TERM(c) (c - (char) 32)

    numeric_arg=0;
    minus = 0;
    final_command = '\0';

    fill_buffer();
    if (eof == 1)
	return;

    c = read_buffer[read_pointer++];
#ifdef DEBUG
    fprintf(stderr, "Got %c\n", c);
#endif

/*
 * If we are not in a "combined command", we are looking for ESC
 */

    if (combined_command == 0) {
	int non_ff_skipped_chars = 0;
	if(c != (char) 0x1b) {

/*
 * all we can do is to chew through the file looking for another ESC.
 */

	    skipped_chars = 0;
	    fprintf(stderr, "        ");
	    while (c != (char) 0x1b) {
		if (c == (char) 0x0c)
		    fprintf(stderr, " FF");
		else
		    non_ff_skipped_chars++;
		skipped_chars++;
		fill_buffer();
		if (eof == 1) {
		    fprintf(stderr, "ERROR: EOF looking for ESC!\n");
		    return;
		}
		c = read_buffer[read_pointer++];
	    }
	    if (non_ff_skipped_chars != 0)
	        fprintf(stderr, "ERROR: No ESC found (out of sync?) searching... ");
	    fprintf(stderr, " %d character%s skipped.\n", skipped_chars,
		    skipped_chars == 1 ? "" : "s");
	}

/*
 * We got an ESC, process normally
 */

	initial_command_index=0;
	initial_command[initial_command_index] = '\0';
	fill_buffer();
	if (eof == 1) {
	    fprintf(stderr, "ERROR: EOF after ESC!\n");
	    return;
	}

/* Get first command letter */

	c = read_buffer[read_pointer++];
	initial_command[initial_command_index++] = c;

#ifdef DEBUG
	fprintf(stderr, "Got %c\n", c);
#endif

/* Check to see if this character forms a "two character" command,
   or is a special command. */

	if (PCL_TWOCHAR(c)) {
#ifdef DEBUG
	    fprintf(stderr, "Two character command\n");
#endif
	    initial_command[initial_command_index] = '\0';
	    return;
	}	/* two character check */

/* Now check for a "parameterised" sequence. */

	else if (PCL_PARAM(c)) {
#ifdef DEBUG
	    fprintf(stderr, "Parameterised command\n");
#endif

/* Get the next character in the command */

	    fill_buffer();
	    if (eof == 1) {

#ifdef DEBUG
		fprintf(stderr, "EOF in middle of command!\n");
#endif
		eof = 0;		/* Ignore it */
		initial_command[initial_command_index] = '\0';
		return;
	    }
	    c = read_buffer[read_pointer++];
#ifdef DEBUG
	    fprintf(stderr, "Got %c\n", c);
#endif

/* Check that it is legal and store it */

	    if (PCL_PARAM2(c)) {
		initial_command[initial_command_index++] = c;
		initial_command[initial_command_index] = '\0';

/* Get the next character in the command then fall into the numeric part */

		fill_buffer();
		if (eof == 1) {

#ifdef DEBUG
		    fprintf(stderr, "EOF in middle of command!\n");
#endif
		    eof = 0;		/* Ignore it */
		    return;
		}
		c = read_buffer[read_pointer++];
#ifdef DEBUG
		fprintf(stderr, "Got %c\n", c);
#endif

	    }
	    else {
/* The second character is not legal. If the first character is '%' then allow it
 * through */

		    if (initial_command[0] == '%') {
#ifdef DEBUG
			fprintf(stderr, "ESC%% command\n");
#endif
			initial_command[initial_command_index] = '\0';
		    }
		    else {
			fprintf(stderr, "ERROR: Illegal second character %c in parameterised command.\n",
		    c);
			initial_command[initial_command_index] = '\0';
			return;
		    }
	    }
	}	/* Parameterised check */

/* If we get here, the command is illegal */

	else {
	    fprintf(stderr, "ERROR: Illegal first character %c in command.\n",
		c);
	    initial_command[initial_command_index] = '\0';
	    return;
	}
    }						/* End of (combined_command) */

/*
   We get here if either this is a combined sequence, or we have processed
   the beginning of a parameterised sequence. There is an optional number
   next, which may be preceded by "+" or "-". FIXME We should also handle
   decimal points.
*/

    if ((c == '-') || (c == '+') || (PCL_DIGIT(c))) {
	if (c == '-')
	    minus = 1;
	else if (c == '+')
	    minus = 0;
	else
	    numeric_arg = (int) (c - '0');

/* Continue until non-numeric seen */

	while (1) {
	    fill_buffer();
	    if (eof == 1) {
		fprintf(stderr, "ERROR: EOF in middle of command!\n");
		return;
	    }
	    c = read_buffer[read_pointer++];

#ifdef DEBUG
	    fprintf(stderr, "Got %c\n", c);
#endif

	    if (! PCL_DIGIT(c)) {
		break;		/* End of loop */
	    }
	    numeric_arg = (10 * numeric_arg) + (int) (c - '0');
	}
    }

/*
   We fell out of the loop when we read a non-numeric character.
   Treat this as the terminating character and check for a combined
   command. We should check that the letter is a valid terminator,
   but it doesn't matter as we'll just not recognize the command!
 */

    combined_command = (PCL_COMBINED_TERM(c) != 0);
    if (combined_command == 1) {
#ifdef DEBUG
	fprintf(stderr, "Combined command\n");
#endif
	final_command = PCL_CONVERT_TERM(c);
    }
    else
	final_command = c;

    if (minus == 1)
	numeric_arg = -numeric_arg;

    return;
}

/*
 * write_grey() - write out one line of mono PNM image
 */

/* FIXME - multiple levels */

void write_grey(output_t *output,	/* I: data */
		image_t *image)		/* I: Image data */
{
    int wholebytes = image->image_width / 8;
    int crumbs = image->image_width - (wholebytes * 8);
    char *buf = output->black_bufs[0];

    int i, j;
    char tb[8];

#ifdef DEBUG
    fprintf(stderr, "Data Length: %d, wholebytes: %d, crumbs: %d\n",
	output->active_length, wholebytes, crumbs);
#endif

    for (i=0; i < wholebytes; i++) {
	for (j=0; j < 8; j++) {
	    tb[j] = (((buf[i] >> (7-j)) & 1));
	    tb[j] = output->output_depth - tb[j];
	}
	(void) fwrite(&tb[0], sizeof(char), 8, write_fd);
    }
    for (j=0; j < crumbs; j++) {
	tb[j] = (((buf[wholebytes] >> (7-j)) & 1));
	tb[j] = output->output_depth - tb[j];
    }
    (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs, write_fd);
}


/*
 * write_pixel() - write out one line of pixel PNM image
 */

/* FIXME - other than direct by pixel */
void write_pixel(output_t *output,	/* I: data */
		image_t *image)		/* I: Image data */
{
  unsigned char *buf = (unsigned char *) (output->black_bufs[0]);
  int i;
  if (image->colour_type == PCL_CMY) {
    for (i = 0; i < image->image_width * 3; i++) {
      buf[i] = -buf[i];
    }
  }
  (void) fwrite(buf, sizeof(char), image->image_width * 3, write_fd);
  /* Needed for delta compression */
  if (image->colour_type == PCL_CMY) {
    for (i = 0; i < image->image_width * 3; i++) {
      buf[i] = -buf[i];
    }
  }
}

/*
 * write_colour() - Write out one row of RGB PNM data.
 */

/* FIXME - multiple levels and CMYK/CMYKcm */

void write_colour(output_t *output,		/* I: Data buffers */
		 image_t *image)		/* I: Image data */
{
    int wholebytes = image->image_width / 8;
    int crumbs = image->image_width - (wholebytes * 8);

    int i, j, jj;
    char tb[8*3];

    char *cyan_buf;
    char *magenta_buf;
    char *yellow_buf;
    char *black_buf;

    return;
    cyan_buf = output->cyan_bufs[0];
    magenta_buf = output->magenta_bufs[0];
    yellow_buf = output->yellow_bufs[0];
    if (image->colour_type != PCL_CMY)
	black_buf = output->black_bufs[0];
    else
	black_buf = NULL;

    fprintf(stderr, "Data Length: %d, wholebytes: %d, crumbs: %d, planes: %d\n",
	output->active_length, wholebytes, crumbs, image->colour_type);

    fprintf(stderr, "Cyan: ");
    for (i=0; i < output->active_length; i++) {
	fprintf(stderr, "%02x ", (unsigned char) cyan_buf[i]);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "Magenta: ");
    for (i=0; i < output->active_length; i++) {
	fprintf(stderr, "%02x ", (unsigned char) magenta_buf[i]);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "Yellow: ");
    for (i=0; i < output->active_length; i++) {
	fprintf(stderr, "%02x ", (unsigned char) yellow_buf[i]);
    }
    fprintf(stderr, "\n");
    if (image->colour_type == PCL_CMYK) {
	fprintf(stderr, "Black: ");
	for (i=0; i < output->active_length; i++) {
	    fprintf(stderr, "%02x ", (unsigned char) black_buf[i]);
	}
	fprintf(stderr, "\n");
    }

    if (output->pixels_depth == 1) {
	if (image->colour_type == PCL_CMY) {
	    for (i=0; i < wholebytes; i++) {
		for (j=0,jj=0; j < 8; j++,jj+=3) {
		    tb[jj] = (((cyan_buf[i] >> (7-j)) & 1));
		    tb[jj] = output->output_depth - tb[jj];
		    tb[jj+1] = (((magenta_buf[i] >> (7-j)) & 1));
		    tb[jj+1] = output->output_depth - tb[jj+1];
		    tb[jj+2] = (((yellow_buf[i] >> (7-j)) & 1));
		    tb[jj+2] = output->output_depth - tb[jj+2];
		}
		(void) fwrite(&tb[0], sizeof(char), (size_t) (8*3), write_fd);
	    }
	    for (j=0,jj=0; j < crumbs; j++,jj+=3) {
		tb[jj] = (((cyan_buf[wholebytes] >> (7-j)) & 1));
		tb[jj] = output->output_depth - tb[jj];
		tb[jj+1] = (((magenta_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+1] = output->output_depth - tb[jj+1];
		tb[jj+2] = (((yellow_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+2] = output->output_depth - tb[jj+2];
	    }
	    (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs*3, write_fd);
	}
	else {
	    for (i=0; i < wholebytes; i++) {
		for (j=0,jj=0; j < 8; j++,jj+=3) {
    #if !defined OUTPUT_CMYK_ONLY_K && !defined OUTPUT_CMYK_ONLY_CMY
		    tb[jj] = ((((cyan_buf[i]|black_buf[i]) >> (7-j)) & 1));
		    tb[jj+1] = ((((magenta_buf[i]|black_buf[i]) >> (7-j)) & 1));
		    tb[jj+2] = ((((yellow_buf[i]|black_buf[i]) >> (7-j)) & 1));
    #endif
    #ifdef OUTPUT_CMYK_ONLY_K
		    tb[jj] = (((black_buf[i] >> (7-j)) & 1));
		    tb[jj+1] = (((black_buf[i] >> (7-j)) & 1));
		    tb[jj+2] = (((black_buf[i] >> (7-j)) & 1));
    #endif
    #ifdef OUTPUT_CMYK_ONLY_CMY
		    tb[jj] = (((cyan_buf[i] >> (7-j)) & 1));
		    tb[jj+1] = (((magenta_buf[i] >> (7-j)) & 1));
		    tb[jj+2] = (((yellow_buf[i] >> (7-j)) & 1));
    #endif
		    tb[jj] = output->output_depth - tb[jj];
		    tb[jj+1] = output->output_depth - tb[jj+1];
		    tb[jj+2] = output->output_depth - tb[jj+2];
		}
		(void) fwrite(&tb[0], sizeof(char), (size_t) (8*3), write_fd);
	    }
	    for (j=0,jj=0; j < crumbs; j++,jj+=3) {
    #if !defined OUTPUT_CMYK_ONLY_K && !defined OUTPUT_CMYK_ONLY_CMY
		tb[jj] = ((((cyan_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
		tb[jj+1] = ((((magenta_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
		tb[jj+2] = ((((yellow_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
    #endif
    #ifdef OUTPUT_CMYK_ONLY_K
		tb[jj] = (((black_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+1] = (((black_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+2] = (((black_buf[wholebytes] >> (7-j)) & 1));
    #endif
    #ifdef OUTPUT_CMYK_ONLY_CMY
		tb[jj] = (((cyan_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+1] = (((magenta_buf[wholebytes] >> (7-j)) & 1));
		tb[jj+2] = (((yellow_buf[wholebytes] >> (7-j)) & 1));
    #endif
		tb[jj] = output->output_depth - tb[jj];
		tb[jj+1] = output->output_depth - tb[jj+1];
		tb[jj+2] = output->output_depth - tb[jj+2];
	    }
	    (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs*3, write_fd);
	}
    }
}
/*
 * decode_tiff() - Uncompress a TIFF encoded buffer
 */

int decode_tiff(char *in_buffer,		/* I: Data buffer */
		int data_length,		/* I: Length of data */
		char *decode_buf,		/* O: decoded data */
		int maxlen)			/* I: Max length of decode_buf */
{
/* The TIFF coding consists of either:-
 *
 * (0 <= count <= 127) (count+1 bytes of data) for non repeating data
 * or
 * (-127 <= count <= -1) (data) for 1-count bytes of repeating data
 */

    int count;
    int pos = 0;
    int dpos = 0;
#ifdef DEBUG
    int i;
#endif

    while(pos < data_length ) {

	count = in_buffer[pos];

	if ((count >= 0) && (count <= 127)) {
#ifdef DEBUG
	    fprintf(stderr, "%d bytes of nonrepeated data\n", count+1);
	    fprintf(stderr, "DATA: ");
	    for (i=0; i< (count+1); i++) {
		fprintf(stderr, "%02x ", (unsigned char) in_buffer[pos + 1 + i]);
	    }
	    fprintf(stderr, "\n");
#endif
	    if ((dpos + count + 1) > maxlen) {
		fprintf(stderr, "ERROR: Too much expanded data (%d > %d)!\n", dpos + count + 1, maxlen);
		exit(EXIT_FAILURE);
	    }
	    memcpy(&decode_buf[dpos], &in_buffer[pos+1], (size_t) (count + 1));
	    dpos += count + 1;
	    pos += count + 2;
	}
	else if ((count >= -127) && (count < 0)) {
#ifdef DEBUG
	    fprintf(stderr, "%02x repeated %d times\n", (unsigned char) in_buffer[pos + 1], 1 - count);
#endif
	    if ((dpos + 1 - count) > maxlen) {
		fprintf(stderr, "ERROR: Too much expanded data (%d > %d)!\n", dpos + 1 - count, maxlen);
		exit(EXIT_FAILURE);
	    }
	    memset(&decode_buf[dpos], in_buffer[pos + 1], (size_t) (1 - count));
	    dpos += 1 - count;
	    pos += 2;
	}
	else {
	    fprintf(stderr, "ERROR: Illegal TIFF count: %d, skipped\n", count);
	    pos += 2;
	}
    }

#ifdef DEBUG
    fprintf(stderr, "TIFFOUT: ");
    for (i=0; i< dpos; i++) {
	fprintf(stderr, "%02x ", (unsigned char) decode_buf[i]);
    }
    fprintf(stderr, "\n");
#endif
    return(dpos);
}

/*
 * decode_delta() - Uncompress a delta encoded buffer
 */

int decode_delta(char *in_buffer,		/* I: Data buffer */
		 int data_length,		/* I: Length of data */
		 char *decode_buf,		/* I/O: decoded data */
		 int maxlen)			/* I: Max length of decode_buf */
{

    unsigned command_byte = 0;
    unsigned delta_bytes = 0;
    unsigned offset_from_last = 0;
    unsigned next_offset = 0;

    int pos = 0;
    int dpos = 0;
#ifdef DEBUG
    int i = 0;
    int j = 0;
#endif

#ifdef DEBUG
    fprintf(stderr, ">>>Decode delta %p %d %p %d\n", in_buffer, data_length,
	    decode_buf, maxlen);
#endif
    while(pos < data_length ) {

	command_byte = in_buffer[pos];
	delta_bytes = (((unsigned char) command_byte) >> 5) + 1;
	offset_from_last = (command_byte & ((1 << 5) - 1));
	if (offset_from_last == 31) {
	  do {
	    next_offset = (unsigned char) in_buffer[++pos];
	    offset_from_last += next_offset;
	  } while (next_offset == 0xff);
	}
#ifdef DEBUG
	fprintf(stderr, "%d: command %02x (delta %d, offset %d)\n", i,
		(unsigned char) command_byte, delta_bytes, offset_from_last);
	fprintf(stderr, "    pos %d dpos %d data_length %d maxlen %d\n",
		pos, dpos, data_length, maxlen);
#endif
	pos++;
	if (data_length - pos < delta_bytes) {
	    fprintf(stderr, "ERROR: data overrun in delta (delta %d, remaining %d)\n",
		    delta_bytes, data_length - pos);
	}
	dpos += offset_from_last;
	(void) memcpy(decode_buf + dpos, in_buffer + pos, delta_bytes);
#ifdef DEBUG
	fprintf(stderr, "    ");
	for (j = 0; j < delta_bytes; j++) {
	    fprintf(stderr, "%02x ", (unsigned char) in_buffer[pos + j]);
	}
	fprintf(stderr, "\n");
#endif
	pos += delta_bytes;
	dpos += delta_bytes;
#ifdef DEBUG
	    fprintf(stderr, "    pos %d dpos %d data_length %d maxlen %d\n",
		    pos, dpos, data_length, maxlen);
	i++;
#endif
    }
    /*
    fprintf(stderr, "   Decode delta %d %d %d\n", pos, data_length, dpos);
    */

#ifdef DEBUG
    fprintf(stderr, "DELTAOUT: ");
    for (i=0; i< dpos; i++) {
	fprintf(stderr, "%02x ", (unsigned char) decode_buf[i]);
    }
    fprintf(stderr, "\n");
#endif
    return(dpos);
}

/*
 * pcl_reset() - Rest image parameters to default
 */

void pcl_reset(image_t *i)
{
    i->colour_type = PCL_MONO;
    i->pixel_type = PCL_MONO;
    i->black_depth = 2;		/* Assume mono */
    i->cyan_depth = 0;
    i->magenta_depth = 0;
    i->yellow_depth = 0;
    i->lcyan_depth = 0;
    i->lmagenta_depth = 0;
    i->image_width = -1;
    i->image_height = -1;
    i->compression_type = 0;	/* should this be NONE? */
}

/*
 * depth_to_rows() - convert the depth of the colour into the number
 * of data rows needed to represent it. Assumes that depth is a power
 * of 2, FIXME if not!
 */

int depth_to_rows(int depth)
{
    int rows;

    if (depth == 0)
	return(0);

    for (rows = 1; rows < 8; rows++) {
    if ((depth >> rows) == 1)
	return(rows);
    }
    fprintf(stderr, "ERROR: depth %d too big to handle in depth_to_rows()!\n",
	depth);
    return(0);	/* ?? */
}

static void
print_command(int index, int arg)
{
  char buf[16];
  const commands_t *cmd = &(pcl_commands[index]);
  (void) snprintf(buf, 15, "%s%d%c", cmd->initial_command, arg, cmd->final_command);
  fprintf(stderr, "%-8s (%s): ", buf, pcl_commands[index].description);
}

/*
 * Main
 */

int main(int argc, char *argv[])
{

    int command_index;
    command_t command;
    int i, j;				/* Loop/general variables */
    int image_row_counter = -1;		/* Count of current row */
    int total_image_rows = 0;		/* All rows seen */
    int current_data_row = -1;		/* Count of data rows received for this output row */
    int expected_data_rows_per_row = -1;
					/* Expected no of data rows per output row */
    image_t image_data;			/* Data concerning image */
    long filepos = -1;
    int wrote_header = 0;
    int state_is_clean = 1;

/*
 * Holders for the decoded lines
 */

    output_t output_data;

/*
 * The above pointers (when allocated) are then copied into this
 * variable in the correct order so that the received data can
 * be stored.
 */

    char **received_rows;

    output_data.black_bufs = NULL;		/* Storage for black rows */
    output_data.black_data_rows_per_row = 0;	/* Number of black rows */
    output_data.cyan_bufs = NULL;
    output_data.cyan_data_rows_per_row = 0;
    output_data.magenta_bufs = NULL;
    output_data.magenta_data_rows_per_row = 0;
    output_data.yellow_bufs = NULL;
    output_data.yellow_data_rows_per_row = 0;
    output_data.lcyan_bufs = NULL;
    output_data.lcyan_data_rows_per_row = 0;
    output_data.lmagenta_bufs = NULL;
    output_data.lmagenta_data_rows_per_row = 0;
    output_data.buffer_length = 0;
    output_data.active_length = 0;
    output_data.output_depth = 0;
    output_data.input_depth = 1;
    output_data.pixels_depth = 1;

    received_rows = NULL;

    if(argc == 1){
	read_fd = stdin;
	write_fd = stdout;
    }
    else if(argc == 2){
	read_fd = fopen(argv[1],"r");
	write_fd = stdout;
    }
    else {
	if(*argv[1] == '-'){
	    read_fd = stdin;
	    write_fd = fopen(argv[2],"w");
	}
	else {
	    read_fd = fopen(argv[1],"r");
	    write_fd = fopen(argv[2],"w");
	}
    }

    if (read_fd == (FILE *)NULL) {
	fprintf(stderr, "ERROR: Error Opening input file.\n");
	exit (EXIT_FAILURE);
    }

    if (write_fd == (FILE *)NULL) {
	fprintf(stderr, "ERROR: Error Opening output file.\n");
	exit (EXIT_FAILURE);
    }

    read_pointer=-1;
    eof=0;
    initial_command_index=0;
    initial_command[initial_command_index] = '\0';
    numeric_arg=0;
    final_command = '\0';


    pcl_reset(&image_data);

    while (1) {
	pcl_read_command();
	if (eof == 1) {
	    if (! state_is_clean)
		fprintf(stderr, "EOF while reading command.\n");
	    (void) fclose(read_fd);
	    (void) fclose(write_fd);
	    exit(EXIT_SUCCESS);
	}

#ifdef DEBUG
	fprintf(stderr, "initial_command: %s, numeric_arg: %d, final_command: %c\n",
	    initial_command, numeric_arg, final_command);
#endif

	command_index = pcl_find_command();
	if (command_index == -1) {
	    fprintf(stderr, "ERROR: Unknown (and unhandled) command: %s%d%c\n", initial_command,
		numeric_arg, final_command);
/* We may have to skip some data here */
	}
	else {
	    command = pcl_commands[command_index].command;
	    state_is_clean = pcl_commands[command_index].clean_state;
	    if (pcl_commands[command_index].has_data == 1) {

/* Read the data into data_buffer */

#ifdef DEBUG
		fprintf(stderr, "Data: ");
#endif

		if (numeric_arg > MAX_DATA) {
		    fprintf(stderr, "ERROR: Too much data (%d), increase MAX_DATA (%d)!\n", numeric_arg, MAX_DATA);
		    exit(EXIT_FAILURE);
		}

		for (i=0; i < numeric_arg; i++) {
		    fill_buffer();
		    if (eof == 1) {
			fprintf(stderr, "ERROR: Unexpected EOF whilst reading data\n");
			exit(EXIT_FAILURE);
		    }
		    data_buffer[i] = read_buffer[read_pointer++];

#ifdef DEBUG
		    fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
#endif

		}

#ifdef DEBUG
		fprintf(stderr, "\n");
#endif

	    }
	    switch(command) {
	    case PCL_RESET :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		pcl_reset(&image_data);
		break;

	    case PCL_RESET_MARGINS :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		break;

	    case PCL_START_RASTER :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");

/* Make sure we have all the stuff needed to work out what we are going
   to write out. */

		i = 0;		/* use as error indicator */

		if (image_data.image_width == -1) {
		    fprintf(stderr, "ERROR: Image width not set!\n");
		    i++;
		}
		if (image_data.image_height == -1) {
		    fprintf(stderr, "WARNING: Image height not set!\n");
		}

		if ((image_data.black_depth != 0) &&
		    (image_data.black_depth != 2)) {
		    fprintf(stderr, "WARNING: Only 2 level black dithers handled.\n");
		}
		if ((image_data.cyan_depth != 0) &&
		    (image_data.cyan_depth != 2)) {
		    fprintf(stderr, "WARNING: Only 2 level cyan dithers handled.\n");
		}
		if ((image_data.magenta_depth != 0) &&
		    (image_data.magenta_depth != 2)) {
		    fprintf(stderr, "WARNING: Only 2 level magenta dithers handled.\n");
		}
		if ((image_data.yellow_depth != 0) &&
		    (image_data.yellow_depth != 2)) {
		    fprintf(stderr, "WARNING: only 2 level yellow dithers handled.\n");
		}
		if (image_data.lcyan_depth != 0) {
		    fprintf(stderr, "WARNING: Light cyan dithers not yet handled.\n");
		}
		if (image_data.lmagenta_depth != 0) {
		    fprintf(stderr, "WARNING: Light magenta dithers not yet handled.\n");
		}

		if (image_data.compression_type != PCL_COMPRESSION_NONE &&
		    image_data.compression_type != PCL_COMPRESSION_TIFF &&
		    image_data.compression_type != PCL_COMPRESSION_DELTA) {
		    fprintf(stderr,
			"Sorry, only 'no compression' or 'tiff compression' handled.\n");
		    i++;
		}

		if (i != 0) {
		    fprintf(stderr, "PNM output suppressed, will continue diagnostic output.\n");
		    skip_output = 1;
		}

		if (skip_output == 0) {
		    if (! wrote_header) {
			if (image_data.colour_type == PCL_MONO &&
			    image_data.pixel_type == PCL_MONO)
			    (void) fputs("P5\n", write_fd);	/* Raw, Grey */
			else
			    (void) fputs("P6\n", write_fd);	/* Raw, RGB */

			(void) fputs("# Written by pclunprint.\n", write_fd);

    /*
     * Remember the file position where we wrote the image width and height
     * (you don't want to know why!)
     */

			filepos = ftell(write_fd);

			fprintf(write_fd, "%10d %10d\n", image_data.image_width,
			    image_data.image_height);
		    }

/*
 * Write the depth of the image
 */

		    if (image_data.black_depth != 0)
			output_data.output_depth = image_data.black_depth - 1;
		    else
			output_data.output_depth = image_data.cyan_depth - 1;
		    if (! wrote_header) {
			if (output_data.pixels_depth > 1)
			    fprintf(write_fd, "%d\n", 255);
			else
			    fprintf(write_fd, "%d\n", output_data.output_depth);
		    }
		    wrote_header = 1;


		    image_row_counter = 0;
		    current_data_row = 0;

		    output_data.black_data_rows_per_row = depth_to_rows(image_data.black_depth);
		    output_data.cyan_data_rows_per_row = depth_to_rows(image_data.cyan_depth);
		    output_data.magenta_data_rows_per_row = depth_to_rows(image_data.magenta_depth);
		    output_data.yellow_data_rows_per_row = depth_to_rows(image_data.yellow_depth);
		    output_data.lcyan_data_rows_per_row = depth_to_rows(image_data.lcyan_depth);
		    output_data.lmagenta_data_rows_per_row = depth_to_rows(image_data.lmagenta_depth);

/*
 * Allocate some storage for the expected planes
 */

		    output_data.buffer_length = (image_data.image_width + 7) / 8;

		    if (output_data.black_data_rows_per_row != 0) {
			output_data.black_bufs = stp_zalloc(output_data.black_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.black_data_rows_per_row; i++) {
			    output_data.black_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }
		    if (output_data.cyan_data_rows_per_row != 0) {
			output_data.cyan_bufs = stp_zalloc(output_data.cyan_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.cyan_data_rows_per_row; i++) {
			    output_data.cyan_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }
		    if (output_data.magenta_data_rows_per_row != 0) {
			output_data.magenta_bufs = stp_zalloc(output_data.magenta_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.magenta_data_rows_per_row; i++) {
			    output_data.magenta_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }
		    if (output_data.yellow_data_rows_per_row != 0) {
			output_data.yellow_bufs = stp_zalloc(output_data.yellow_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.yellow_data_rows_per_row; i++) {
			    output_data.yellow_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }
		    if (output_data.lcyan_data_rows_per_row != 0) {
			output_data.lcyan_bufs = stp_zalloc(output_data.lcyan_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.lcyan_data_rows_per_row; i++) {
			     output_data.lcyan_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }
		    if (output_data.lmagenta_data_rows_per_row != 0) {
			output_data.lmagenta_bufs = stp_zalloc(output_data.lmagenta_data_rows_per_row * sizeof (char *));
			for (i=0; i < output_data.lmagenta_data_rows_per_row; i++) {
			    output_data.lmagenta_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
			}
		    }

/*
 * Now store the pointers in the right order to make life easier in the
 * decoding phase
 */

		    expected_data_rows_per_row = output_data.black_data_rows_per_row +
			output_data.cyan_data_rows_per_row + output_data.magenta_data_rows_per_row +
			output_data.yellow_data_rows_per_row + output_data.lcyan_data_rows_per_row +
			output_data.lmagenta_data_rows_per_row;

		    received_rows = stp_malloc(expected_data_rows_per_row * sizeof(char *));
		    j = 0;
		    for (i = 0; i < output_data.black_data_rows_per_row; i++)
			received_rows[j++] = output_data.black_bufs[i];
		    for (i = 0; i < output_data.cyan_data_rows_per_row; i++)
			received_rows[j++] = output_data.cyan_bufs[i];
		    for (i = 0; i < output_data.magenta_data_rows_per_row; i++)
			received_rows[j++] = output_data.magenta_bufs[i];
		    for (i = 0; i < output_data.yellow_data_rows_per_row; i++)
			received_rows[j++] = output_data.yellow_bufs[i];
		    for (i = 0; i < output_data.lcyan_data_rows_per_row; i++)
			received_rows[j++] = output_data.lcyan_bufs[i];
		    for (i = 0; i < output_data.lmagenta_data_rows_per_row; i++)
			received_rows[j++] = output_data.lmagenta_bufs[i];
		}
		break;

	    case PCL_END_RASTER :
	    case PCL_END_COLOUR_RASTER :
		print_command(command_index, numeric_arg);

		if (skip_output == 0) {

/*
 * Check that we got the correct number of rows of data. If the expected number is
 * -1, we have to go back and fill in the PNM parameters (which is why we remembered
 * where they were in the file!)
 */

		    if (image_data.image_height == -1) {
			image_data.image_height = image_row_counter;
			total_image_rows += image_row_counter;
			if (fseek(write_fd, filepos, SEEK_SET) != -1) {
			    fprintf(write_fd, "%10d %10d\n", image_data.image_width,
				total_image_rows);
			    fseek(write_fd, 0L, SEEK_END);
			}
		    }

		    if (image_row_counter != image_data.image_height)
			fprintf(stderr, "ERROR: Row count mismatch. Expected %d rows, got %d rows.\n",
			    image_data.image_height, image_row_counter);
		    else
			fprintf(stderr, "\t%d rows processed, max %d.\n",
				image_row_counter, max_data_len);

		    image_data.image_height = -1;
		    image_row_counter = -1;

		    if (output_data.black_data_rows_per_row != 0) {
			for (i=0; i < output_data.black_data_rows_per_row; i++) {
			    stp_free(output_data.black_bufs[i]);
			}
			stp_free(output_data.black_bufs);
			output_data.black_bufs = NULL;
		    }
		    output_data.black_data_rows_per_row = 0;
		    if (output_data.cyan_data_rows_per_row != 0) {
			for (i=0; i < output_data.cyan_data_rows_per_row; i++) {
			    stp_free(output_data.cyan_bufs[i]);
			}
			stp_free(output_data.cyan_bufs);
			output_data.cyan_bufs = NULL;
		    }
		    output_data.cyan_data_rows_per_row = 0;
		    if (output_data.magenta_data_rows_per_row != 0) {
			for (i=0; i < output_data.magenta_data_rows_per_row; i++) {
			    stp_free(output_data.magenta_bufs[i]);
			}
			stp_free(output_data.magenta_bufs);
			output_data.magenta_bufs = NULL;
		    }
		    output_data.magenta_data_rows_per_row = 0;
		    if (output_data.yellow_data_rows_per_row != 0) {
			for (i=0; i < output_data.yellow_data_rows_per_row; i++) {
			    stp_free(output_data.yellow_bufs[i]);
			}
			stp_free(output_data.yellow_bufs);
			output_data.yellow_bufs = NULL;
		    }
		    output_data.yellow_data_rows_per_row = 0;
		    if (output_data.lcyan_data_rows_per_row != 0) {
			for (i=0; i < output_data.lcyan_data_rows_per_row; i++) {
			    stp_free(output_data.lcyan_bufs[i]);
			}
			stp_free(output_data.lcyan_bufs);
			output_data.lcyan_bufs = NULL;
		    }
		    output_data.lcyan_data_rows_per_row = 0;
		    if (output_data.lmagenta_data_rows_per_row != 0) {
			for (i=0; i < output_data.lmagenta_data_rows_per_row; i++) {
			    stp_free(output_data.lmagenta_bufs[i]);
			}
			stp_free(output_data.lmagenta_bufs);
			output_data.lmagenta_bufs = NULL;
		    }
		    output_data.lmagenta_data_rows_per_row = 0;
		    stp_free(received_rows);
		    received_rows = NULL;
		}
		break;

	    case PCL_SET_VMI :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		break;

	    case PCL_MEDIA_SIZE :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 2 :
		    fprintf(stderr, "Letter\n");
		    break;
		case 3 :
		    fprintf(stderr, "Legal\n");
		    break;
		case 6 :
		    fprintf(stderr, "Tabloid\n");
		    break;
		case 26 :
		    fprintf(stderr, "A4\n");
		    break;
		case 27 :
		    fprintf(stderr, "A3\n");
		    break;
		case 101 :
		    fprintf(stderr, "Custom\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_MEDIA_TYPE :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "Plain\n");
		    break;
		case 1 :
		    fprintf(stderr, "Bond\n");
		    break;
		case 2 :
		    fprintf(stderr, "Premium\n");
		    break;
		case 3 :
		    fprintf(stderr, "Glossy/Photo\n");
		    break;
		case 4 :
		    fprintf(stderr, "Transparency\n");
		    break;
		case 5 :
		    fprintf(stderr, "Quick-dry Photo\n");
		    break;
		case 6 :
		    fprintf(stderr, "Quick-dry Transparency\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_MEDIA_SOURCE :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case -2 :
		    fprintf(stderr, "FEED CURRENT\n");
		    break;
		case 0 :
		    fprintf(stderr, "EJECT\n");
		    break;
		case 1 :
		    fprintf(stderr, "LJ Tray 2 or Portable CSF or DJ Tray\n");
		    break;
		case 2 :
		    fprintf(stderr, "Manual\n");
		    break;
		case 3 :
		    fprintf(stderr, "Envelope\n");
		    break;
		case 4 :
		    fprintf(stderr, "LJ Tray 3 or Desktop CSF or DJ Tray 2\n");
		    break;
		case 5 :
		    fprintf(stderr, "LJ Tray 4 or DJ optional\n");
		    break;
		case 7 :
		    fprintf(stderr, "DJ Autosource\n");
		    break;
		case 8 :
		    fprintf(stderr, "LJ Tray 1\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_SHINGLING :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "None\n");
		    break;
		case 1 :
		    fprintf(stderr, "2 passes\n");
		    break;
		case 2 :
		    fprintf(stderr, "4 passes\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_RASTERGRAPHICS_QUALITY :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "(set by printer controls)\n");
		case 1 :
		    fprintf(stderr, "Draft\n");
		    break;
		case 2 :
		    fprintf(stderr, "High\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_DEPLETION :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 1 :
		    fprintf(stderr, "None\n");
		    break;
		case 2 :
		    fprintf(stderr, "25%%\n");
		    break;
		case 3 :
		    fprintf(stderr, "50%%\n");
		    break;
		case 5 :
		    fprintf(stderr, "50%% with gamma correction\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_PRINT_QUALITY :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case -1 :
		    fprintf(stderr, "Draft\n");
		    break;
		case 0 :
		    fprintf(stderr, "Normal\n");
		    break;
		case 1 :
		    fprintf(stderr, "Presentation\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT :
		print_command(command_index, numeric_arg);

		if (skip_output == 0) {

/* Check that we are in raster mode */

		    if (expected_data_rows_per_row == -1)
			fprintf(stderr, "ERROR: raster data without start raster!\n");

/*
  What we need to do now is to write out "N" rows of all-white data to
  simulate the vertical slew
*/

		    for (i=0; i<expected_data_rows_per_row; i++)
		    {
			memset(received_rows[i], 0, (size_t) output_data.buffer_length * sizeof(char) * output_data.output_depth);
		    }
		    for (i=0; i<numeric_arg; i++)
		    {
			if (image_data.colour_type == PCL_MONO) {
			    if (image_data.pixel_type == PCL_MONO) {
				write_grey(&output_data, &image_data);
			    } else {
				write_pixel(&output_data, &image_data);
			    }
			} else {
			    write_colour(&output_data, &image_data);
			}
			image_row_counter++;
		    }
		}
		fprintf(stderr, "\n");
		break;

	    case PCL_DUPLEX :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "None\n");
		    break;
		case 1 :
		    fprintf(stderr, "Duplex No Tumble (Long Edge)\n");
		    break;
		case 2 :
		    fprintf(stderr, "Duplex Tumble (Short Edge)\n");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)\n", numeric_arg);
		    break;
		}
		break;

	    case PCL_DRIVER_CONFIG :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "(ignored)");
		fprintf(stderr, " Data: ");
		for (i=0; i < numeric_arg; i++) {
		    fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
		}
		fprintf(stderr, "\n");
		fprintf(stderr, "         Device: ");
		switch (data_buffer[0]) {
		case 6:
		  fprintf(stderr, "Color Laser Printer\n");
		  break;
		default:
		  fprintf(stderr, "Unknown\n");
		  break;
		}
		fprintf(stderr, "         Function: ");
		switch (data_buffer[1]) {
		case 0:
		  fprintf(stderr, "Lightness %d\n", data_buffer[2]);
		  break;
		case 1:
		  fprintf(stderr, "Saturation %d\n", data_buffer[2]);
		  break;
		case 3:
		  fprintf(stderr, "Scaling Algorithm: ");
		  switch (data_buffer[2]) {
		  case 0:
		    fprintf(stderr, "Pixel Replication\n");
		    break;
		  case 1:
		    fprintf(stderr, "Bilinear Interpolation\n");
		    break;
		  case 2:
		    fprintf(stderr, "Modified Bilinear Interpolation\n");
		    break;
		  default:
		    fprintf(stderr, "Unknown\n");
		  }
		  break;
		case 4:
		  fprintf(stderr, "Color Treatment: ");
		  switch (data_buffer[2]) {
		  case 0:
		    fprintf(stderr, "No Adjustment\n");
		    break;
		  case 1:
		    fprintf(stderr, "Process Blue\n");
		    break;
		  case 2:
		    fprintf(stderr, "Vivid Graphics\n");
		    break;
		  case 3:
		    fprintf(stderr, "Transparency\n");
		    break;
		  case 4:
		    fprintf(stderr, "Out of Gamut\n");
		    break;
		  case 5:
		    fprintf(stderr, "CIE Lab Match\n");
		    break;
		  case 6:
		    fprintf(stderr, "Screen Match\n");
		    break;
		  default:
		    fprintf(stderr, "Unknown\n");
		  }
		  break;
		case 5:
		  fprintf(stderr, "Download Color Map: ");
		  switch (data_buffer[2]) {
		  case 1:
		    fprintf(stderr, "CMY Color Space\n");
		    break;
		  case 3:
		    fprintf(stderr, "CIE Lab Color Space\n");
		    break;
		  default:
		    fprintf(stderr, "Unknown\n");
		  }
		  break;
		default:
		  fprintf(stderr, "Unknown\n");
		  break;
		}
		break;
	    case PCL_PERF_SKIP :
	    case PCL_TOP_MARGIN :
	    case PCL_RESOLUTION :
	    case PCL_LEFTRASTER_POS :
	    case PCL_TOPRASTER_POS :
	    case PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS :
	    case PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS :
	    case PCL_PALETTE_CONFIGURATION :
	    case PCL_UNIT_OF_MEASURE :
	    case PCL_GRAY_BALANCE :
	    case PCL_LPI :
	    case PCL_CPI :
	    case PCL_PAGE_LENGTH :
	    case PCL_NUM_COPIES :
	    case PCL_ENTER_PCL :
	    case PCL_NEGATIVE_MOTION :
	    case PCL_JOB_SEPARATION :
	    case PCL_LEFT_OFFSET_REGISTRATION :
	    case PCL_TOP_OFFSET_REGISTRATION :
	    case PCL_PRINT_DIRECTION :
	    case PCL_LEFT_MARGIN :
	    case PCL_RIGHT_MARGIN :
	    case PCL_TEXT_LENGTH :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "(ignored)");
		if (pcl_commands[command_index].has_data == 1) {
		    fprintf(stderr, " Data: ");
		    for (i=0; i < numeric_arg; i++) {
			fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
		    }
		}
		fprintf(stderr, "\n");
		break;

	    case PCL_COLOURTYPE :
		print_command(command_index, numeric_arg);
		image_data.colour_type = -numeric_arg;
		switch (image_data.colour_type) {
		    case PCL_MONO :
			fprintf(stderr, "MONO\n");
			break;
		    case PCL_CMY :
			fprintf(stderr, "CMY (one cart)\n");
			image_data.black_depth = 0;	/* Black levels */
			image_data.cyan_depth = 2;	/* Cyan levels */
			image_data.magenta_depth = 2;	/* Magenta levels */
			image_data.yellow_depth = 2;	/* Yellow levels */
			break;
		    case PCL_CMYK :
			fprintf(stderr, "CMYK (two cart)\n");
			image_data.black_depth = 2;	/* Black levels */
			image_data.cyan_depth = 2;	/* Cyan levels */
			image_data.magenta_depth = 2;	/* Magenta levels */
			image_data.yellow_depth = 2;	/* Yellow levels */
			break;
		    default :
			fprintf(stderr, "Unknown (%d)\n", -numeric_arg);
			break;
		}
		break;

	    case PCL_COMPRESSIONTYPE :
		print_command(command_index, numeric_arg);
		image_data.compression_type = numeric_arg;
		switch (image_data.compression_type) {
		    case PCL_COMPRESSION_NONE :
			fprintf(stderr, "NONE\n");
			break;
		    case PCL_COMPRESSION_RUNLENGTH :
			fprintf(stderr, "Runlength\n");
			break;
		    case PCL_COMPRESSION_TIFF :
			fprintf(stderr, "TIFF\n");
			break;
		    case PCL_COMPRESSION_DELTA :
			fprintf(stderr, "DELTA\n");
			break;
		    case PCL_COMPRESSION_ADAPTIVE :
			fprintf(stderr, "ADAPTIVE (mono only)\n");
			break;
		    case PCL_COMPRESSION_CRDR :
			fprintf(stderr, "Compressed Row Delta Replacement\n");
			break;
		    default :
			fprintf(stderr, "Unknown (%d)\n", image_data.compression_type);
			break;
		}
		break;

	    case PCL_RASTER_WIDTH :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		image_data.image_width = numeric_arg;
		break;

	    case PCL_RASTER_HEIGHT :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		image_data.image_height = numeric_arg;
		break;

	    case PCL_SEED_ROW_SOURCE :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\n");
		break;

	    case PCL_RTL_CONFIGURE :
		print_command(command_index, numeric_arg);
		for (i = 0; i < numeric_arg; i++)
		  fprintf(stderr, " %02x", (unsigned char) data_buffer[i]);
		fprintf(stderr, "\n");
		fprintf(stderr, "         Color Space: ");
		switch (data_buffer[0]) {
		case 0:
		  fprintf(stderr, "DeviceRGB\n");
		  image_data.pixel_type = PCL_RGB;
		  break;
		case 1:
		  fprintf(stderr, "DeviceCMY\n");
		  image_data.pixel_type = PCL_CMY;
		  break;
		case 2:
		  fprintf(stderr, "sRGB\n");
		  image_data.pixel_type = PCL_RGB;
		  break;
		default:
		  fprintf(stderr, "Unknown\n");
		  break;
		}
		fprintf(stderr, "         Encoding: ");
		switch (data_buffer[1]) {
		case 0:
		case 1:
		  fprintf(stderr, "Indexed by %s, %d bits/index",
			  data_buffer[1] == 0 ? "Plane" : "Pixel",
			  data_buffer[2]);
		  output_data.input_depth = data_buffer[2];
		  break;
		case 2:
		case 3:
		  fprintf(stderr, "Direct by %s, depth %d\n",
			  data_buffer[1] == 2 ? "Plane" : "Pixel",
			  data_buffer[3]);
		  output_data.pixels_depth = 3;
		  output_data.input_depth = data_buffer[3];
		  break;
		default:
		  fprintf(stderr, "Unknown\n");
		  break;
		}
	      break;

	    case PCL_CONFIGURE :
		print_command(command_index, numeric_arg);
		fprintf(stderr, "\tFormat: %d\n", data_buffer[0]);
		if (data_buffer[0] == 2) {

/*
 * the data that follows depends on the colour type (buffer[1]). The size
 * of the data should be 2 + (6 * number of planes).
 */

		    fprintf(stderr, "\tOutput Planes: ");
		    image_data.colour_type = data_buffer[1]; 	/* # output planes */
		    switch (image_data.colour_type) {
			case PCL_MONO :
			    fprintf(stderr, "MONO\n");

/* Size should be 8 */

			    if (numeric_arg != 8)
				fprintf(stderr, "ERROR: Expected 8 bytes of data, got %d\n", numeric_arg);

			    fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char)data_buffer[3],
				((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
			    image_data.black_depth = data_buffer[7];	/* Black levels */
			    break;
			case PCL_CMY :
			    fprintf(stderr, "CMY (one cart)\n");

/* Size should be 20 */

			    if (numeric_arg != 20)
				fprintf(stderr, "ERROR: Expected 20 bytes of data, got %d\n", numeric_arg);

			    fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
				((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
			    fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
			    ((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
			    fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
				((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
			    image_data.black_depth = 0;
			    image_data.cyan_depth = data_buffer[7];		/* Cyan levels */
			    image_data.magenta_depth = data_buffer[13];	/* Magenta levels */
			    image_data.yellow_depth = data_buffer[19];	/* Yellow levels */
			    break;
			case PCL_CMYK :
			    fprintf(stderr, "CMYK (two cart)\n");

/* Size should be 26 */

			    if (numeric_arg != 26)
				fprintf(stderr, "ERROR: Expected 26 bytes of data, got %d\n", numeric_arg);

			    fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
				((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
			    fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
				((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
			    fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
				((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
			    fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[20]<<8)+(unsigned char) data_buffer[21],
				((unsigned char) data_buffer[22]<<8)+(unsigned char) data_buffer[23], data_buffer[25]);
			    image_data.black_depth = data_buffer[7];	/* Black levels */
			    image_data.cyan_depth = data_buffer[13];	/* Cyan levels */
			    image_data.magenta_depth = data_buffer[19];	/* Magenta levels */
			    image_data.yellow_depth = data_buffer[25];	/* Yellow levels */
			    break;
			case PCL_CMYKcm :
			    fprintf(stderr, "CMYKcm (two cart photo)\n");

/* Size should be 38 */

			    if (numeric_arg != 38)
				fprintf(stderr, "ERROR: Expected 38 bytes of data, got %d\n", numeric_arg);

			    fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
				((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
			    fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
				((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
			    fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
				((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
			    fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[20]<<8)+(unsigned char) data_buffer[21],
				((unsigned char) data_buffer[22]<<8)+(unsigned char) data_buffer[23], data_buffer[25]);
			    fprintf(stderr, "\tLight Cyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[26]<<8)+(unsigned char) data_buffer[27],
				((unsigned char) data_buffer[28]<<8)+(unsigned char) data_buffer[29], data_buffer[31]);
			    fprintf(stderr, "\tLight Magenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[32]<<8)+(unsigned char) data_buffer[33],
				((unsigned char) data_buffer[34]<<8)+(unsigned char) data_buffer[35], data_buffer[37]);
			    image_data.black_depth = data_buffer[7];	/* Black levels */
			    image_data.cyan_depth = data_buffer[13];	/* Cyan levels */
			    image_data.magenta_depth = data_buffer[19];	/* Magenta levels */
			    image_data.yellow_depth = data_buffer[25];	/* Yellow levels */
			    image_data.lcyan_depth = data_buffer[31];	/* Cyan levels */
			    image_data.lmagenta_depth = data_buffer[37];	/* Magenta levels */
			    break;
			default :
			    fprintf(stderr, "Unknown (%d)\n", data_buffer[1]);
			    break;
		    }
		}
		else {
		    fprintf(stderr, "Unknown format %d\n", data_buffer[0]);
		    fprintf(stderr, "Data: ");
		    for (i=0; i < numeric_arg; i++) {
			fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
		    }
		    fprintf(stderr, "\n");
		}
		break;

	    case PCL_DATA :
	    case PCL_DATA_LAST :
		if (skip_output == 1) {
		    print_command(command_index, numeric_arg);
		    fprintf(stderr, "\n");
		}
		else {

/*
 * Make sure that we have enough data to process this command!
 */

		    if (expected_data_rows_per_row == -1)
			fprintf(stderr, "ERROR: raster data without start raster!\n");

		    if (numeric_arg > max_data_len)
		        max_data_len = numeric_arg;
/*
 * The last flag indicates that this is the end of the planes for a row
 * so we check it against the number of planes we have seen and are
 * expecting.
 */

		    if (command == PCL_DATA_LAST) {
			if (current_data_row != (expected_data_rows_per_row - 1))
			    fprintf(stderr, "ERROR: 'Last Plane' set on plane %d of %d!\n",
				current_data_row, expected_data_rows_per_row);
		    }
		    else {
			if (current_data_row == (expected_data_rows_per_row - 1))
			    fprintf(stderr, "ERROR: Expected 'last plane', but not set!\n");
		    }

/*
 * Accumulate the data rows for each output row,then write the image.
 */
/*
		    fprintf(stderr, ">>>>>Current %d %p %p %p\n",
			    current_data_row, received_rows,
			    data_buffer, received_rows[current_data_row]);
*/

		    if (image_data.compression_type == PCL_COMPRESSION_NONE) {
			memcpy(received_rows[current_data_row], &data_buffer, (size_t) numeric_arg);
			output_data.active_length = numeric_arg * output_data.input_depth * output_data.pixels_depth;
		    }
		    else if (image_data.compression_type == PCL_COMPRESSION_TIFF) {
			output_data.active_length = decode_tiff(data_buffer, numeric_arg, received_rows[current_data_row], output_data.buffer_length * output_data.input_depth * output_data.pixels_depth);

		    }
		    else if (image_data.compression_type == PCL_COMPRESSION_DELTA) {
			output_data.active_length = decode_delta(data_buffer, numeric_arg, received_rows[current_data_row], output_data.buffer_length * output_data.input_depth * output_data.pixels_depth);
		    }
		    /*
		    fprintf(stderr, "<<<<<Current %d %p %p %p\n",
			    current_data_row, received_rows,
			    data_buffer, received_rows[current_data_row]);
		    */
		    if (command == PCL_DATA_LAST) {
			if (image_data.colour_type == PCL_MONO) {
			    if (image_data.pixel_type == PCL_MONO) {
				write_grey(&output_data, &image_data);
			    } else {
				write_pixel(&output_data, &image_data);
			    }
			} else {
			    write_colour(&output_data, &image_data);
			}
			current_data_row = 0;
			image_row_counter++;
		    }
		    else
			current_data_row++;
		}
		break;

	    case PCL_ENTER_HPGL2 :
	    case PCL_ENTER_PJL : {
		    int c;
		    print_command(command_index, numeric_arg);

/*
 * This is a special command. Read up to the next ESC and output it.
 */

		    c = 0;
		    while (1) {
			fill_buffer();
			if (eof == 1) {
			    fprintf(stderr, "\n");
			    break;
			}
			c = read_buffer[read_pointer++];
			if (c == '\033')
			    break;
			fprintf(stderr, "%c", c);
		    }

/*
 * Now we are sitting at the "ESC" that is the start of the next command.
 */
		    read_pointer--;
		    fprintf(stderr, "\n");
		    fprintf(stderr, "End of %s\n", pcl_commands[command_index].description);
		}
		break;

	    case PCL_PAGE_ORIENTATION :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "Portrait");
		    break;
		case 1 :
		    fprintf(stderr, "Landscape");
		    break;
		case 2 :
		    fprintf(stderr, "Reverse Portrait");
		    break;
		case 3 :
		    fprintf(stderr, "Reverse Landscape");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)", numeric_arg);
		    break;
		}
		fprintf(stderr, " (ignored)\n");
		break;

	    case PCL_MEDIA_SIDE :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "Next side");
		    break;
		case 1 :
		    fprintf(stderr, "Front side");
		    break;
		case 2 :
		    fprintf(stderr, "Back side");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)", numeric_arg);
		    break;
		}
		fprintf(stderr, " (ignored)\n");
		break;

	    case PCL_MEDIA_DEST :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 1 :
		    fprintf(stderr, "Upper Output bin");
		    break;
		case 2 :
		    fprintf(stderr, "Lower (Rear) Output bin");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)", numeric_arg);
		    break;
		}
		fprintf(stderr, " (ignored)\n");
		break;

	    case PCL_RASTER_RENDER :
		print_command(command_index, numeric_arg);
		switch (numeric_arg) {
		case 0 :
		    fprintf(stderr, "Continuous tone detail (high lpi)");
		    break;
		case 3 :
		    fprintf(stderr, "Device-best dither");
		    break;
		case 15 :
		    fprintf(stderr, "Continuous tone smooth (high lpi)");
		    break;
		case 18 :
		    fprintf(stderr, "Continuous tone basic (low lpi)");
		    break;
		default :
		    fprintf(stderr, "Unknown (%d)", numeric_arg);
		    break;
		}
		fprintf(stderr, " (ignored)\n");
	      break;
	    case PCL_COLOR_COMPONENT_1 :
	    case PCL_COLOR_COMPONENT_2 :
	    case PCL_COLOR_COMPONENT_3 :
	    case PCL_ASSIGN_COLOR_INDEX :
		print_command(command_index, numeric_arg);
		fprintf(stderr, " (ignored)\n");
		break;
	    case PCL_SET_FOREGROUND_COLOR :
		print_command(command_index, numeric_arg);
		fprintf(stderr, " (ignored)\n");
		break;

	    default :
		fprintf(stderr, "ERROR: No handler for %s!\n", pcl_commands[command_index].description);
		break;
	    }
	}
    }
}