File: cgmin.c

package info (click to toggle)
yorick 1.5.08-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,508 kB
  • ctags: 7,937
  • sloc: ansic: 75,604; cpp: 1,282; lisp: 1,217; sh: 1,026; makefile: 616; fortran: 19
file content (2482 lines) | stat: -rw-r--r-- 67,874 bytes parent folder | download
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
/*
 * CGMIN.C
 *
 * $Id: cgmin.c,v 1.1 1993/08/27 17:19:10 munro Exp $
 *
 * Define the CGM reader/echoer for GIST.
 *
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#include "play.h"
#include "cgmin.h"
#include "engine.h"

#include <stdio.h>
#include "pstdio.h"

#include <string.h>
#include "pstdlib.h"

extern void CGMinfo(void);  /* defined here */

/* function that returns the number of pages in a CGM file family */
extern int CGM_pages(void);

/* Some commercial GKS packages (e.g.- ATC) set the palette to
   0==black, 1==white, then use color 1 as the foreground color, but
   print it as black (sigh).  This flag tries to accomodate such
   deviant behavior.  */
extern int bg0fg1;
int bg0fg1= 0;

typedef struct CGM CGM;
typedef unsigned char Octet;

Engine *outEngines[8];
int outTypes[8];

static int EchoPage(int number, int inc);

static CGM *NewCGM(char *name, p_file *file, int maxPages);
static CGM *DeleteCGM(CGM *cgm);
static void NextName(char *filename);
static int BeginCatalog(CGM *cgm);

static long ReadParameters(long nOctets, GpColor *colors, long maxColors);
static int SkipParameters(long nOctets);
static long ReadElement(int *klass, int *id);

static long DePascalify(char *text, Octet *pasctxt, long nMax);
static int Snarf16(Octet *param);
static long SnarfInteger(Octet *param);
static GpReal SnarfReal(Octet *param);
static void RememberPoint(GpReal x0, GpReal y0);
static char *SnarfMoreText(Octet *param);
static char *SnarfText(Octet *param);
static char *PathifyText(char *text, int path);
static GpReal SnarfSA(Octet *param, int md, GpReal dflt);
static int SnarfColor(Octet *param, long nOctets);
static void ConvertPoints(long n, short *vdc, GpReal *ndcx, GpReal *ndcy);
static int CheckScratch(long nPoints);

static void SetVDC(int *vdc);
static void GSetClip(int on);
static void ResetPalette(int forPage);

static int DoClass0(int id, long nOctets);
static int DoClass1(int id, long nOctets);
static int DoClass2(int id, long nOctets);
static int DoClass3(int id, long nOctets);
static int DoClass4(int id, long nOctets);
static int DoClass5(int id, long nOctets);
static int DoClass6(int id, long nOctets);
static int DoClass7(int id, long nOctets);

#define NCAR_RECORD_SIZE 1440L
#define N_BUFSIZE 4096L
static unsigned long n_fread(void *ptr, unsigned long nbytes, CGM *cgm);
static int n_fseek(CGM *cgm, long offset, int relative);
static long n_ftell(CGM *cgm);
static long n_fskip(void *ptr, long nbytes, CGM *cgm);

/* ------------------------------------------------------------------------ */

static int currentPage= 0;

static char metafileName[256];                    /* from BEGIN METAFILE */
static char pictureName[256];          /* from most recent BEGIN PICTURE */
static char metafileDescription[256];       /* from METAFILE DESCRIPTION */
static int isGistCGM= 0;  /* set if metafileDescription begins "Gist;  " */

static int nBytesInt= 2;
static int vdcIllegible, realIllegible, indexIllegible,
  cvalIllegible, cndxIllegible, cselIllegible;

#define N_GIST_FONTS 20
static char *gistFonts[]= {
  "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
  "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
  "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique",
  "Symbol", "Symbol", "Symbol", "Symbol",
  "NewCenturySchlbk-Roman", "NewCenturySchlbk-Bold",
  "NewCenturySchlbk-Italic", "NewCenturySchlbk-BoldItalic" };
static int defaultNumbers[]= {
  0, 8, 4, 12, 1, 9, 5, 13, 2, 10, 6, 14, 3, 11, 7, 15, 16, 17, 18, 19 };

/* Transformation from vdc->ndc is known on a per-page basis */
#define DEFAULT_METRIC (1.0/(25545.24*(ONE_INCH/25.4)))
static GpReal cgmScale= (1.0/25545.24), cgmScaleX= (1.0/25545.24),
  cgmScaleY= (1.0/25545.24), cgmOffsetX= 0.0, cgmOffsetY= 0.0;
int cgmLandscape= 0;
extern int cgmScaleToFit;
int cgmScaleToFit= 0;

/* May as well have a permanently allocated palette of maximal size */
static GpColorCell cgmPalette[256], defaultPalette[256];
static int cgmNColors= 0, defaultNColors=  0, cgmNColMax;
static int cgmColorChange= 0, alteredPalette= 0, setXPalette= 0,
  needPalette= 0;

struct CGMDesc {
  /* metafile descriptor elements */
  int colorMaxI, colorMaxV[6];
  int *fonts, nfonts, maxFonts;

  /* picture descriptor elements */
  int scaling;
  GpReal metric;
  int lwidth, msize, ewidth;
  int vdc[4];
  struct unused_color { unsigned char red, green, blue, gray; } background;

#define ABSTRACT 0
#define METRIC 1
#define INDEXED 0
#define DIRECT 1
#define ABSOLUTE 0
#define SCALED 1
};

/* cgmDesc represents default metafile and picture descriptor settings as
   defined in the ANSI X3.122 - 1986 (Part 3) standard */
struct CGMDesc cgmDesc= {
  63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
  0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };

/* pageDesc represents cgmDesc, as modified by the defaults
   replacement section of the metafile header */
struct CGMDesc pageDesc= {
  63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
  0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };

/* pictDesc represents pageDesc, as modified
   by any picture descriptor elements */
struct CGMDesc pictDesc= {
  63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
  0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };

struct CGMControls {
  int auxColor, transparency;
  short clipBox[4];
  int clipOn, clipDefault;
};

/* cgmControls represent default control settings as defined in the
   ANSI X3.122 - 1986 (Part 3) standard */
struct CGMControls cgmControls= {
  0, 1, {0, 0, 32767, 32767}, 1, 1 };

/* pageControls represent cgmControls, as modified by the defaults
   replacement section of the metafile header */
struct CGMControls pageControls= {
  0, 1, {0, 0, 32767, 32767}, 1, 1 };

/* pictControls represents pageControls, as modified
   by any picture descriptor elements */
struct CGMControls pictControls= {
  0, 1, {0, 0, 32767, 32767}, 1, 1 };

struct NonGistAttribs {
  /* non-Gist constructs */
  int eVisible;         /* edge visibility separate from gistA.e.type */
  int hindex, pindex;   /* separate hatch and pattern indices */
  GpReal upX, upY, baseX, baseY;  /* character up and base vectors */
  GpReal expand, spacing;         /* other text attributes */
  int prec, path;                 /* text precision, path */
};

struct CGMAttributes {

  /* line attributes (GpLines, GpDisjoint) */
  GpLineAttribs l;

  /* marker attributes (GpMarkers) */
  GpMarkerAttribs m;

  /* filled area attributes (GpFill) */
  GpFillAttribs f;

  /* text attributes (GpText) */
  GpTextAttribs t;

  /* non-Gist constructs */
  struct NonGistAttribs ng;
};

/* cgmDefaults represent default attribute settings as defined in the
   ANSI X3.122 - 1986 (Part 3) standard */
static struct CGMAttributes cgmDefaults= {
  { FG_COLOR, L_SOLID, 1.0 },                     /* line attributes */
  { FG_COLOR, M_ASTERISK, 1.0 },                  /* marker attributes */
  { FG_COLOR, F_SOLID },                          /* fill attributes */
  { FG_COLOR, 0, 0.0156,
      TX_RIGHT, TH_NORMAL, TV_NORMAL },           /* text attributes */

  {0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0}   /* non-Gist attributes,
						     except pattern table */
  };

/* pageDefaults represent cgmDefaults, as modified by the defaults
   replacement section of the metafile header */
static struct CGMAttributes pageDefaults= {
  { FG_COLOR, L_SOLID, 1.0 },                     /* line attributes */
  { FG_COLOR, M_ASTERISK, 1.0 },                /* marker attributes */
  { FG_COLOR, F_SOLID },                          /* fill attributes */
  { FG_COLOR, 0, 0.0156,
      TX_RIGHT, TH_NORMAL, TV_NORMAL },           /* text attributes */

  {0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0}   /* non-Gist attributes,
						     except pattern table */
  };

/* current values of non-Gist attributes
   (current values of others in gistA) */
static struct NonGistAttribs nonGistA= {
  0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0};

/* ------------------------------------------------------------------------ */

struct CGM {
  char *name;
  p_file *file;   /* non-zero only if currently open */
  CGM *prev, *next;
  int firstPage, nPages, maxPages;
  long *pageAddress, catalogEnd;

  /* additional stuff to read NCAR CGM files */
  unsigned char *buffer;
  long pos, valid;
  int ncar;
  long ncar_rec, ncar_len;
};

static CGM *currentCGM= 0, *firstCGM= 0, *catalogCGM= 0;

static int cgmCmdLen;  /* 2 or 4, length of previous ReadElement command */
static Octet *cmdString= 0;
static Octet *currentCmd;
static long cmdLength;

/* Name incrementer copied from cgm.c */
static void NextName(char *filename)
{
  int i, len= filename? strlen(filename) : 0;
  if (len>4 && (strcmp(filename+len-4, ".cgm")==0 ||
		strcmp(filename+len-4, ".CGM")==0)) i= len-4;
  else if (len>5 && (strcmp(filename+len-5, ".ncgm")==0 ||
		     strcmp(filename+len-5, ".NCGM")==0)) i= len-5;
  else i= len;
  while (i-- > 0) {
    if (filename[i]=='9') {
      filename[i]= '0';
    } else {
      if (filename[i]=='Z' || filename[i]>='z') filename[i]= '0';
      else filename[i]++;
      break;
    }
  }
}

static CGM *NewCGM(char *name, p_file *file, int maxPages)
{
  CGM *cgm= (CGM *)p_malloc(sizeof(CGM)+strlen(name)+1);
  long *pageAddress= (long *)p_malloc(sizeof(long)*maxPages);

  if (!cgm || !pageAddress) {
    if (cgm) p_free(cgm);
    if (pageAddress) p_free(pageAddress);
    Warning("memory manager failed trying to open ", name);
    return 0;
  }

  cgm->name= ((char *)cgm)+sizeof(CGM);
  strcpy(cgm->name, name);
  if (!file) {
    NextName(cgm->name);
    file= p_fopen(cgm->name, "rb");
    if (!file) {
      p_free(cgm);
      p_free(pageAddress);
      return 0;
    }
  }
  cgm->file= file;
  cgm->prev= cgm->next= 0;
  cgm->firstPage= 1;
  cgm->nPages= 0;
  cgm->maxPages= maxPages;
  cgm->catalogEnd= -1;
  cgm->pageAddress= pageAddress;

  cgm->buffer = p_malloc(4096);
  cgm->pos = cgm->valid = 0;
  cgm->ncar = 0;
  cgm->ncar_rec = cgm->ncar_len = -1L;

  return cgm;
}

static CGM *DeleteCGM(CGM *cgm)
{
  CGM *next= cgm->next;
  if (cgm->prev) cgm->prev->next= next;
  if (cgm->pageAddress) p_free(cgm->pageAddress);
  if (cgm->buffer) p_free(cgm->buffer);
  p_free(cgm);
  return next;
}

static int BeginCatalog(CGM *cgm)
{
  CGM *oldCGM= currentCGM;
  int klass, id, bad;
  long nOctets;

  do {
    currentCGM= cgm;

    nOctets= ReadElement(&klass, &id);

    if (klass==0 && id==1) {
      bad= 0;
      for (;;) {
	SkipParameters(nOctets);
	nOctets= ReadElement(&klass, &id);
	if (nOctets>=0) {
	  if (klass==0) {
	    if (id==3 || id==2) break;  /* begin picture or end metafile */
	    else if (id!=0) {
	      if (cgm->ncar)
		Warning("bad class 0 element in metafile descriptor", "");
	      bad= 1;
	      break;
	    }
	  } else if (klass!=1 && klass!=6 && klass!=7) {
	    if (cgm->ncar)
	      Warning("bad element class in metafile descriptor", "");
	    bad= 1;
	    break;
	  }
	} else {
	  bad= 1;
	  break;
	}
      }
    } else {
      if (cgm->ncar) Warning("BEGIN METAFILE element missing", "");
      bad= 1;
    }

    if (bad) {
      int ncar = cgm->ncar;
      cgm->ncar = 1;
      if (ncar || n_fseek(cgm, 0L, 0)) {
	currentCGM= oldCGM;
	Warning(cgm->name, " is not a binary CGM, cannot open");
	return 1;
      }
    }
  } while (bad);

  if (id==3) {
    cgm->pageAddress[0]= n_ftell(cgm)-cgmCmdLen;
    SkipParameters(nOctets);
    cgm->catalogEnd= n_ftell(cgm);
    cgm->nPages= 1;
  } else {
    cgm->catalogEnd= -1;
  }

  currentCGM= oldCGM;

  return 0;
}

int CatalogCGM(void)
{
  int klass, id, nPage;
  long nOctets;
  CGM *tmp, *cgm= currentCGM;

  if (!catalogCGM) return 0;

  while (catalogCGM->catalogEnd<0) {
    if (catalogCGM!=currentCGM) {
      p_fclose(catalogCGM->file);
      catalogCGM->file= 0;
    }
    nPage= catalogCGM->firstPage+catalogCGM->nPages;
    catalogCGM= NewCGM(catalogCGM->name, (void *)0, 256);
    if (!catalogCGM || BeginCatalog(catalogCGM)) {
      if (catalogCGM) {
	p_fclose(catalogCGM->file);
	DeleteCGM(catalogCGM);
	catalogCGM= 0;
      }
      return 0;
    }
    catalogCGM->firstPage= nPage;
  }

  n_fseek(catalogCGM, catalogCGM->catalogEnd, 0);

  for (;;) {
    currentCGM= catalogCGM;
    for (;;) {
      nOctets= ReadElement(&klass, &id);
      if (nOctets>=0) {
	if (klass==0) {
	  if (id==3 || id==2) break;  /* begin picture or end metafile */
	}
	SkipParameters(nOctets);
      } else {
	Warning("missing END METAFILE element in ", catalogCGM->name);
	klass= 0;
	id= 2;
	break;
      }
    }
    currentCGM= cgm;

    if (id==3) {
      if (catalogCGM->maxPages==catalogCGM->nPages) {
	long *pa= (long *)p_malloc(sizeof(long)*(catalogCGM->maxPages+256));
	int i;
	if (!pa) {
	  catalogCGM->catalogEnd= -1;
	  if (catalogCGM!=currentCGM) p_fclose(catalogCGM->file);
	  catalogCGM= 0;
	  Warning("memory manager failed cataloging ", catalogCGM->name);
	  break;
	}
	for (i=0 ; i<catalogCGM->nPages ; i++)
	  pa[i]= catalogCGM->pageAddress[i];
	p_free(catalogCGM->pageAddress);
	catalogCGM->pageAddress= pa;
	catalogCGM->maxPages+= 256;
      }
      catalogCGM->pageAddress[catalogCGM->nPages++]=
	n_ftell(catalogCGM)-cgmCmdLen;
      currentCGM= catalogCGM;
      SkipParameters(nOctets);
      currentCGM= cgm;
      catalogCGM->catalogEnd= n_ftell(catalogCGM);
      break;

    } else { /* found END METAFILE */
      tmp= catalogCGM;
      tmp->catalogEnd= -1;
      if (tmp!=currentCGM) {
	p_fclose(tmp->file);
	tmp->file= 0;
      }
      tmp= tmp->next= NewCGM(catalogCGM->name, (void *)0, 256);
      if (tmp) {
	if (BeginCatalog(tmp)) {
	  p_fclose(tmp->file);
	  DeleteCGM(tmp);
	  tmp= 0;
	} else {
	  tmp->prev= catalogCGM;
	  tmp->firstPage= catalogCGM->firstPage+catalogCGM->nPages;
	}
      }
      catalogCGM= tmp;
      if (!tmp) return 0;
    }
  }

  return 1;
}

/* ------------------------------------------------------------------------ */

int OpenCGM(char *file)
{
  p_file *f= p_fopen(file, "rb");
  CGM *cgm;
  int klass, id;
  long nOctets;

  if (!f) {
    Warning("unable to open CGM input file: ", file);
    return 1;
  }

  cgm= NewCGM(file, f, 256);
  if (!cgm) {
    p_fclose(f);
    return 1;
  }

  cmdString= 0;  /* just to be sure... */
  if (BeginCatalog(cgm)) {
    p_fclose(f);
    DeleteCGM(cgm);
    return 1;
  }

  if (n_fseek(cgm, 0L, 0)) {
    p_fclose(f);
    DeleteCGM(cgm);
    Warning("fseek failed for CGM, cannot open ", file);
    return 1;
  }

  if (currentCGM) p_fclose(currentCGM->file);
  if (catalogCGM && catalogCGM!=currentCGM) p_fclose(catalogCGM->file);
  while (firstCGM) firstCGM= DeleteCGM(firstCGM);
  currentCGM= firstCGM= catalogCGM= cgm;

  /* Read metafile descriptor, setting page defaults */
  for (;;) {
    nOctets= ReadElement(&klass, &id);
    if (nOctets<0) break;
    if (klass==0) {
      if (id==2 || id==3) break;
      else DoClass0(id, nOctets);
    } else if (klass==1) DoClass1(id, nOctets);
    else if (klass==6) DoClass6(id, nOctets);
    else if (klass==7) DoClass7(id, nOctets);
  }

  currentPage= 1;
  return 0;
}

int ReadCGM(int *mPage, int *nPage, int *sPage, int nPageGroups)
{
  int m0=0, m=1, n, s= 1;
  int echo_page_return= 0;

  if (!currentCGM) return 1;

  while (nPageGroups-- > 0) {
    m0= *mPage++;
    n= *nPage++;
    s= *sPage++;
    if ((m0<n && s<=0) || (m0>n && s>=0) || (s==0 && m0!=n)) {
      Warning("illegal page group, showing only first frame", "");
      n= m0;
      s= 1;
    } else if (s==0) {
      s= 1;
    }

    for (m=m0 ; s>0? m<=n : m>=n ; m+= s) {
      echo_page_return= EchoPage(m, m==m0? s : 0);
      if (echo_page_return) break;
    }
  }

  if (m==m0) {
    /* Last page group was empty, display first or last page */
    int offset;
    if (s>0) {
      while (currentCGM->next) currentCGM= currentCGM->next;
      while (currentCGM->nPages==0 && currentCGM->prev)
	currentCGM= currentCGM->prev;
      offset= currentCGM->nPages-1;
    } else {
      while (currentCGM->prev) currentCGM= currentCGM->prev;
      while (currentCGM->nPages==0 && currentCGM->next)
	currentCGM= currentCGM->next;
      offset= 0;
    }
    if (currentCGM->nPages>0) {
      if (echo_page_return!=2) EchoPage(currentCGM->firstPage+offset, 1);
    } else {
      Warning("no pictures in CGM file(s) ", currentCGM->name);
    }
  }

  return 0;
}

int CGMRelative(int offset)
{
  return currentPage+offset;
}

void CGMinfo(void)
{
  if (currentCGM) {
    CGM *last= currentCGM;
    char msg[80];
    while (last->next) last= last->next;
    sprintf(msg, "At page %d out of %s%d in CGM file ",
	    currentPage, catalogCGM? ">=":"",
	    last->firstPage+last->nPages-1);
    p_stdout(msg);
    p_stdout(firstCGM->name);  /* name might be very long */
    p_stdout("\n");
  } else {
    p_stdout("(No CGM input file, use open command)\n");
  }
}

int CGM_pages(void)
{
  int num_pgs= 0;
  if (currentCGM) {
    CGM *last= currentCGM;
    while (last->next) last= last->next;
    num_pgs= last->firstPage+last->nPages-1;
  }
  return num_pgs;
}

static unsigned long
n_fread(void *ptr, unsigned long nbytes, CGM *cgm)
{
  if (!cgm->ncar && nbytes>0) {
    long valid = cgm->valid;
    long n = cgm->pos & (N_BUFSIZE-1);
    unsigned char *buf = cgm->buffer + n;
    unsigned char *out = ptr;
    long nread = 0;
    for (;;) {
      if (!valid) {
        valid = p_fread(cgm->file, cgm->buffer, N_BUFSIZE);
        if (valid < 0) return -1L;
        cgm->valid = valid;
        if (!valid) return nread;
      }
      n = valid - n;
      if (n > 0) {
        if (n >= nbytes) {
          memcpy(out, buf, nbytes);
          cgm->pos += nbytes;
          if (n == nbytes) cgm->valid = 0;
          return nread+nbytes;
        }
        memcpy(out, buf, n);
        out += n;
        nread += n;
        nbytes -= n;
        cgm->pos += n;
      }
      buf = cgm->buffer;
      cgm->valid = valid = 0;
      n = 0;
    }
  } else {
    return n_fskip(ptr, nbytes, cgm);
  }           
}

static int
n_fseek(CGM *cgm, long offset, int relative)
{
  long n = cgm->pos & (N_BUFSIZE-1);
  long pos0 = cgm->pos-n;
  long rel;

  if (relative) {
    if (cgm->ncar)
      /* this is from SkipParameters -- no choice but to read it all */
      return (n_fskip((void *)0, offset, cgm)==offset)? 0 : -1;
    offset += cgm->pos;
  }

  if (cgm->ncar) {
    /* offset either 0 or something returned by n_ftell */
    if (!offset) offset = 4;      /* offset==0 actually means offset==4 */
    rel = (offset-4) - ((offset-4)%NCAR_RECORD_SIZE);
    if (rel != cgm->ncar_rec) {
      long block = rel - (rel & (N_BUFSIZE-1));
      if (block!=pos0 || !cgm->valid) {
        long valid;
        cgm->valid = 0;
        cgm->pos = rel+4;
        if (p_fseek(cgm->file, block)) return -1;
        valid = p_fread(cgm->file, cgm->buffer, N_BUFSIZE);
        if (valid < 0) return -1;
        cgm->valid = valid;
        pos0 = block;
      }
      /* ncar record 4-byte header contains 2-byte record length
       * followed by 1 byte record type tag */
      cgm->ncar_rec = rel;
      if (rel+4-block <= cgm->valid)
        cgm->ncar_len = (((unsigned long)cgm->buffer[rel-block])<<8) |
          cgm->buffer[rel-block+1] |
          (((unsigned long)cgm->buffer[rel-block+2])<<16);
      else
        cgm->ncar_len = 0;
    }
  }

  rel = offset - pos0;
  if (rel >= 0) {
    if (rel < cgm->valid) {
      cgm->pos = pos0 + rel;
      if (cgm->valid) return 0;
      /* may need to reset eof indicator by calling p_fseek */
    }
  } else {
    if (offset < 0) return -1;
  }
  cgm->valid = 0;
  cgm->pos = offset;
  n = offset & (N_BUFSIZE-1);
  return p_fseek(cgm->file, offset-n);
}

static long
n_ftell(CGM *cgm)
{
  return cgm->pos;
}

static long
n_fskip(void *ptr, long nbytes, CGM *cgm)
{
  if (nbytes>0) {
    long valid = cgm->valid;
    long n = cgm->pos & (N_BUFSIZE-1);
    unsigned char *buf = cgm->buffer + n;
    unsigned char *out = ptr;
    long nread = 0;
    long rec_max = 0;
    for (;;) {
      if (!valid) {
        valid = p_fread(cgm->file, cgm->buffer, N_BUFSIZE);
        if (valid < 0) return -1L;
        cgm->valid = valid;
        if (!valid) return nread;
      }
      /* 3rd byte of ncar_len is tag, 0x3- means CGM record */
      if ((cgm->ncar_len&0xf00000)==0x300000) {
        rec_max = (cgm->ncar_rec+4+(cgm->ncar_len&0xffff)) - (cgm->pos-n);
        if (valid < rec_max) n = valid - n;
        else n = rec_max - n;
        if (n > 0) {
          if (n >= nbytes) {
            if (out) memcpy(out, buf, nbytes);
            cgm->pos += nbytes;
            if (valid<=rec_max && n==nbytes) cgm->valid = 0;
            return nread+nbytes;
          }
          if (out) memcpy(out, buf, n), out+=n;
          nread += n;
          nbytes -= n;
          cgm->pos += n;
        }
        if (valid <= rec_max) cgm->valid = valid = 0;
      }
      if (valid) {
        if (n_fseek(cgm, cgm->ncar_rec+NCAR_RECORD_SIZE+4, 0))
          return -1L;
        valid = cgm->valid;
        n = cgm->pos & (N_BUFSIZE-1);
        buf = cgm->buffer + n;
      } else {
        n = 0;
        buf = cgm->buffer;
      }
    }
  }
  return nbytes;
}

/* ------------------------------------------------------------------------ */

static int EchoPage(int number, int inc)
{
  CGM *cgm= currentCGM;
  int klass, id;
  long nOctets;
  int value= 0;

  if (!cgm) return 1;

  while (number<cgm->firstPage && cgm->prev) cgm= cgm->prev;
  while (number>=cgm->firstPage+cgm->nPages && cgm->next) cgm= cgm->next;

  if (number>=cgm->firstPage+cgm->nPages) {
    if (cgm->catalogEnd>0) {
      if (!amBatch) {
	p_stdout("gist: STANDBY -- still cataloging ");
	p_stdout(cgm->name);
	p_stdout("\n");
      }
      while (CatalogCGM()) {
	if (cgm->catalogEnd<0) cgm= cgm->next; /* cgm->next must be non-0! */
	if (number<cgm->firstPage+cgm->nPages) break;
      }
    }
    if (number>=cgm->firstPage+cgm->nPages) {
      if (inc<0) return 0;
      else if (inc==0) return 1;
      number= cgm->firstPage+cgm->nPages-1;
      while (cgm->nPages==0 && cgm->prev) cgm= cgm->prev;
      if (cgm->nPages==0) return 1;
      value= 2;  /* distinguish cases where we still draw something */
    }
  } else if (number<cgm->firstPage) {
    if (inc>0) return 0;
    else if (inc==0) return 1;
    number= cgm->firstPage;
    while (cgm->nPages==0 && cgm->next) cgm= cgm->next;
    if (cgm->nPages==0) return 1;
    value= 2;  /* distinguish cases where we still draw something */
  }

  if (cgm!=currentCGM) {
    if (cgm!=catalogCGM) {
      cgm->file= p_fopen(cgm->name, "rb");
      if (!cgm->file) {
	Warning("no longer able to open CGM ", cgm->name);
	return 1;
      }
    }
    if (currentCGM!=catalogCGM) {
      p_fclose(currentCGM->file);
      currentCGM->file= 0;
    }
    currentCGM= cgm;
  }

  currentPage= number;

  if (n_fseek(cgm, cgm->pageAddress[number-cgm->firstPage], 0)) {
    Warning("fseek failed to reach specified page in ", cgm->name);
    return 1;
  }

  gistError[0]= '\0';

  for (;;) {
    nOctets= ReadElement(&klass, &id);
    if (nOctets<0) break;
    if (klass==0) {
      if (id==5) { DoClass0(id, nOctets); break; }
      if (id==0 || id==3 || id==4) DoClass0(id, nOctets);
      else {
	Warning("misplaced delimiter element in CGM ", cgm->name);
	break;
      }
    } else if (klass==1) {
      Warning("ignoring misplaced class 1 element in CGM ", cgm->name);
    } else if (klass==2) DoClass2(id, nOctets);
    else if (klass==3) DoClass3(id, nOctets);
    else if (klass==4) DoClass4(id, nOctets);
    else if (klass==5) DoClass5(id, nOctets);
    else if (klass==6) DoClass6(id, nOctets);
    else if (klass==7) DoClass7(id, nOctets);
    else {
      Warning("ignoring element of unknown class in CGM ", cgm->name);
    }
  }
  GpFlush(0);

  if (gistError[0]) Warning("(WARNING) ", gistError);

  if (currentCGM==catalogCGM &&
      currentPage==catalogCGM->firstPage+catalogCGM->nPages-1) {
    catalogCGM->catalogEnd= n_ftell(catalogCGM);
  }

  return value;
}

/* ------------------------------------------------------------------------ */
/* Allow for architectures in which a short does not have the same
   size or order as presumed in the CGM binary encoding standard
   (a short presumed to be 2 bytes in big-endian order, i.e.- MSB first).  */

#ifdef HAS_CGM_ORDER
#define CGM_WORDS(octets, nwords) ((short *)octets)

#else
#define CGM_WORDS(octets, nwords) \
  (not_cgm_order?Deorder(octets, nwords):(short *)octets)
static int not_cgm_order = -1;
static short *Deorder(Octet *octets, long nwords);
static short *Deorder(Octet *octets, long nwords)
{
  short *words = (short *)octets;    /* reordering is done in place */
  short mask = (short)(-1 ^ 0xffff); /* mask for sign extension */
  long n = nwords;
  if (not_cgm_order < 0) {
    short cgm_order = 1;
    unsigned char *cgm_test = (unsigned char *)(&cgm_order);
    not_cgm_order = cgm_test[0]!=0;
    if (!not_cgm_order) return words;
  }
  words += n;
  octets += n<<1;
  while (n--) {
    octets -= 2;
    *(--words) = octets[0]<<8 | octets[1];
  }
  if (mask) {
    long i;
    for (i=0 ; i<nwords ; i++) if (words[i] & 0x8000) words[i] |= mask;
  }
  return words;
}
#endif

/* ------------------------------------------------------------------------ */

static Octet *paramList= 0;
static long maxParams= 0;
static long blockSize= 16384;  /* can hold any Gist-written data */

static char fontName[256];

static long ReadParameters(long nOctets, GpColor *colors, long maxColors)
{
  Octet *oldParams, cmd[2];
  long newSize, i;
  long n, nTot= 0;
  int more= nOctets & 0x8000;
  Octet *newParams= colors? colors : paramList;

  for (;;) {
    nOctets&= 0x7fff;
    n= nOctets;
    if (n&1) n++;

    if (!colors) {
      if (nTot+n > maxParams) {
	oldParams= paramList;
	newSize= (1+(nTot+n-1)/blockSize)*blockSize;
	paramList= (Octet *)p_malloc((sizeof(short)/2)*newSize);
	if (!paramList) {
	  Warning("memory manager failed in CGM read", "");
	  return -2L;
	}
	maxParams= newSize;
	for (i=0 ; i<nTot ; i++) paramList[i]= oldParams[i];
	if (oldParams) p_free(oldParams);
      }
      newParams= paramList;
    } else {
      if (nTot+n > maxColors) {
	SkipParameters(nOctets);
	return nTot;
      }
    }

    if (nOctets>0) {
      if (cmdString) {
	if (n>cmdLength) { n= cmdLength;  more= 0; }
	memcpy(paramList+nTot, currentCmd, n);
	currentCmd+= n;
	cmdLength-= n;
      } else {
	if (n_fread(newParams+nTot, n, currentCGM)!=n) {
	  Warning("fread failed on CGM file ", currentCGM->name);
	  return -1L;
	}
      }
      nTot+= nOctets;  /* yes, may be 1 less than n */
    }

    if (!more) break;

    if (cmdString) {
      if (2>cmdLength) break;
      cmd[0]= *currentCmd++;
      cmd[1]= *currentCmd++;
      cmdLength-= 2;
    } else {
      if (n_fread(cmd, 2L, currentCGM)!=2L) {
	Warning("fread failed on CGM file ", currentCGM->name);
	return -1L;
      }
    }
    nOctets= cmd[0]<<8 | cmd[1];
    more= nOctets & 0x8000;
  }

  return nTot;
}

static int SkipParameters(long nOctets)
{
  Octet cmd[2];
  int more= nOctets & 0x8000;

  for (;;) {
    nOctets&= 0x7fff;
    if (nOctets&1) nOctets++;

    if (nOctets>0) {
      if (cmdString) {
	if (nOctets>cmdLength) { nOctets= cmdLength;  more= 0; }
	currentCmd+= nOctets;
	cmdLength-= nOctets;
      } else {
	if (n_fseek(currentCGM, nOctets, 1)) {
	  Warning("fseek failed on CGM file ", currentCGM->name);
	  return 1;
	}
      }
    }

    if (!more) break;

    if (cmdString) {
      if (2>cmdLength) break;
      cmd[0]= *currentCmd++;
      cmd[1]= *currentCmd++;
      cmdLength-= 2;
    } else {
      if (n_fread(cmd, 2L, currentCGM)!=2L) {
	Warning("fread failed (partition) on CGM file ", currentCGM->name);
	return 1;
      }
    }
    nOctets= cmd[0]<<8 | cmd[1];
    more= nOctets & 0x8000;
  }

  return 0;
}

static long ReadElement(int *klass, int *id)
{
  Octet cmd[2];
  unsigned int n;

  if (cmdString) {
    if (2>cmdLength) { *klass= *id= 0;   cmdLength= 0;  return 0; }
    cmd[0]= *currentCmd++;
    cmd[1]= *currentCmd++;
    cmdLength-= 2;
  } else {
    if (n_fread(cmd, 2L, currentCGM)!=2L) {
      Warning("fread failed (command) on CGM file ", currentCGM->name);
      return -1L;
    }
  }
  cgmCmdLen= 2;

  *klass= cmd[0]>>4;
  *id= (cmd[0]<<3 | cmd[1]>>5) & 0x7f;
  n= cmd[1] & 0x1f;

  if (n==0x1f) {
    if (cmdString) {
      if (2>cmdLength) { *klass= *id= 0;   cmdLength= 0;  return 0; }
      cmd[0]= *currentCmd++;
      cmd[1]= *currentCmd++;
      cmdLength-= 2;
    } else {
      if (n_fread(cmd, 2L, currentCGM)!=2L) {
	Warning("fread failed (command) on CGM file ", currentCGM->name);
	return -1L;
      }
    }
    n= cmd[0]<<8 | cmd[1];
    cgmCmdLen= 4;
  }

  return (long)n;
}

static long DePascalify(char *text, Octet *pasctxt, long nMax)
{
  long nTot= nMax>0? 1 : 0;
  long nChars= nMax>0? *pasctxt++ : 0;
  int more= 0;

  if (nMax>0) nMax--;

  if (nChars==0xff) {
    if (nMax<2) {
      text[0]= '\0';
      return nTot+nMax;  /* i.e.- original nMax */
    }
    more= pasctxt[0]&0x80;
    nChars= (pasctxt[0]&0x7f)<<8 | pasctxt[1];
    pasctxt+= 2;
    nTot+= 2;
    nMax-= 2;
  }

  if (nChars>nMax) {
    nChars= nMax;
    more= 0;
  }

  if (nChars>0) memcpy(text, pasctxt, nChars);
  text[nChars]= '\0';
  pasctxt+= nChars;
  nTot+= nChars;
  nMax-= nChars;

  while (more) {
    if (nMax<2) return nTot+nMax;
    more= pasctxt[0]&0x80;
    nChars= (pasctxt[0]&0x7f)<<8 | pasctxt[1];
    pasctxt+= 2;
    nTot+= 2;
    nMax-= 2;
    if (nChars>nMax) {
      nChars= nMax;
      more= 0;
    }
    pasctxt+= nChars;
    nTot+= nChars;
    nMax-= nChars;
  }

  return nTot;
}

static int Snarf16(Octet *param)
{
  int value= param[0]<<8 | param[1];
  return value;
}

static long SnarfInteger(Octet *param)
{
  long value= 0;
  int nBytes= nBytesInt;
  while (nBytes-- > 0) value= (value<<8) | *param++;
  return value;
}

static GpReal SnarfReal(Octet *param)
{
  long ipart= param[0]<<8 | param[1];
  long fpart= param[2]<<8 | param[3];
  GpReal value= (GpReal)fpart/65536.0;
  if (ipart & 0x8000) ipart= (-1L ^ 0xffff) | ipart;
  return value + (GpReal)ipart;
}

static char cgmText[256];
static int cgmTextLen= 0;
static GpReal cgmX0, cgmY0;

static void RememberPoint(GpReal x0, GpReal y0)
{
  cgmX0= x0;
  cgmY0= y0;
}

static char *SnarfMoreText(Octet *param)
{
  cgmTextLen+= DePascalify(cgmText+cgmTextLen, param, 256-cgmTextLen)-1;
  return cgmText;
}

static char *SnarfText(Octet *param)
{
  cgmTextLen= 0;
  return SnarfMoreText(param);
}

/* ARGSUSED */
static char *PathifyText(char *text, int path)
{
  if (path) {
    long len= strlen(cgmText);
    int i;
    if (path!=1 && len>128) {
      len= 128;
      cgmText[len]= '\0';
    }
    if (path!=3) {
      char tmp;
      for (i=0 ; i<len-1-i ; i++) {
	tmp= cgmText[i];
	cgmText[i]= cgmText[len-1-i];
	cgmText[len-1-i]= tmp;
      }
    }
    if (path!=1) {
      cgmText[2*len-1]= '\0';
      for (i=len-1 ; i>0 ; i--) {
	cgmText[2*i]= cgmText[i];
	cgmText[2*i-1]= '\n';
      }
    }
  }
  return cgmText;
}

static GpReal SnarfSA(Octet *param, int md, GpReal dflt)
{
  if (md==SCALED) {
    return SnarfReal(param);
  } else { /* md==ABSOLUTE */
    return ((GpReal)Snarf16(param))*cgmScale/dflt;
  }
}

static int SnarfColor(Octet *param, long nOctets)
{
  int color;
  if (!cselIllegible && nOctets==1) {
    /* always takes this branch if isGistCGM */
    color= param[0];
  } else if (cselIllegible==16 && nOctets==2) {
    color= param[1];  /* could use Snarf16, but top byte meaningless */
  } else {
    color= FG_COLOR;
  }
  if (color!=FG_COLOR) {
    if (isGistCGM) {
      if (color<10) color= (-1-color)&0xff;    /* standard colors */
      else { color-= 10; needPalette= 1; }     /* palette colors */
    } else {
      if (bg0fg1 && color==0) color= BG_COLOR;
      else if (bg0fg1 && color==1) color= FG_COLOR;
      else { if (bg0fg1) color-= 2; needPalette= 1; }
    }
  }
  return color;
}

static void ConvertPoints(long n, short *vdc, GpReal *ndcx, GpReal *ndcy)
{
  long i;
  for (i=0 ; i<n ; i++) {
    ndcx[i]= cgmScaleX*((GpReal)vdc[2*i])+cgmOffsetX;
    ndcy[i]= cgmScaleY*((GpReal)vdc[2*i+1])+cgmOffsetY;
  }
}

static GpReal *ndcX= 0, *ndcY= 0;

static int CheckScratch(long nPoints)
{
  if (nPoints > (ndcY-ndcX)) {
    GpReal *sav= ndcX;
    nPoints= (1+(nPoints-1)/1024)*1024;
    ndcX= (GpReal *)p_malloc(sizeof(GpReal)*2*nPoints);
    if (!ndcX) {
      ndcX= sav;
      return 1;
    }
    if (sav) p_free(sav);
    ndcY= ndcX+nPoints;
  }
  return nPoints<=0;   /* i.e.- success (0) if nPoints>0 */
}

static void SetVDC(int *vdc)
{
 /* Try to fit the VDC extent onto an 8.5 by 11 inch page
    as best we can... */
 long dx= (long)vdc[2]-(long)vdc[0];
 long dy= (long)vdc[3]-(long)vdc[1];
 int landscape= (dy>0?dy:-dy) < (dx>0?dx:-dx);
 long xOffset, yOffset;

 if (landscape!=cgmLandscape) {
   int i;
   cgmLandscape= landscape;
   for (i=0 ; i<8 ; i++)
     if (outEngines[i]) GpLandscape(outEngines[i], landscape);
 }

 if (pictDesc.scaling==ABSTRACT) {
   /* Gist produced files (at default scale) have 11->26400 and
      8.5->20400 exactly.  For either of these cases, we want to
      reproduce the default cgmScale factor of 25545.24 exactly.  */
   if (cgmScaleToFit) {
     long dv= (dx>0?dx:-dx)<(dy>0?dy:-dy)? dy : dx;
     cgmScale= (19200.0/(GpReal)dv) / 25545.24;
     xOffset= (long)((0.25*ONE_INCH)/cgmScale);
     yOffset= (long)((2.75*ONE_INCH)/cgmScale);
   } else if (cgmLandscape) { /* 11-by-8.5 paper */
     if (17*dx > 22*dy) {
       yOffset= (17*dx-22*dy)/44;
       xOffset= 0;
       cgmScale= (26400.0/(GpReal)dx) / 25545.24;
     } else {
       xOffset= (22*dy-17*dx)/34;
       yOffset= 0;
       cgmScale= (20400.0/(GpReal)dy) / 25545.24;
     }
   } else {                   /* 8.5-by-11 paper */
     if (17*dy > 22*dx) {
       xOffset= (17*dy-22*dx)/44;
       yOffset= 0;
       cgmScale= (26400.0/(GpReal)dy) / 25545.24;
     } else {
       yOffset= (22*dx-17*dy)/17;
       xOffset= 0;
       cgmScale= (20400.0/(GpReal)dx) / 25545.24;
     }
   }
 } else { /* pictDesc.scaling==METRIC */
   xOffset= yOffset= 0;
   cgmScale= pictDesc.metric*(ONE_INCH/25.4);
 }
 if (cgmScale<0.0) cgmScale= -cgmScale;

 cgmScaleX= dx>0? cgmScale : -cgmScale;
 cgmScaleY= dy>0? cgmScale : -cgmScale;
 cgmOffsetX= -cgmScaleX*(GpReal)(vdc[0]-xOffset);
 cgmOffsetY= -cgmScaleY*(GpReal)(vdc[1]-yOffset);
}

static GpTransform unitTrans= {{0.0, 2.0, 0.0, 2.0}, {0.0, 2.0, 0.0, 2.0}};

static void GSetClip(int on)
{
  if (on) {
    GpReal xbox[2], ybox[2];
    ConvertPoints(2L, pictControls.clipBox, xbox, ybox);
    if (xbox[0]<xbox[1]) {
      gistT.viewport.xmin= gistT.window.xmin= xbox[0];
      gistT.viewport.xmax= gistT.window.xmax= xbox[1];
    } else {
      gistT.viewport.xmin= gistT.window.xmin= xbox[1];
      gistT.viewport.xmax= gistT.window.xmax= xbox[0];
    }    
    if (ybox[0]<ybox[1]) {
      gistT.viewport.ymin= gistT.window.ymin= ybox[0];
      gistT.viewport.ymax= gistT.window.ymax= ybox[1];
    } else {
      gistT.viewport.ymin= gistT.window.ymin= ybox[1];
      gistT.viewport.ymax= gistT.window.ymax= ybox[0];
    }
    GpSetTrans(&gistT);
    gistClip= 1;
  } else {
    GpSetTrans(&unitTrans);
    gistClip= 0;
  }
}

static void ResetPalette(int forPage)
{
  int i;

  if (forPage) {
    int offset= isGistCGM? 10 : (bg0fg1? 2 : 0);
    int refreshPalette;
    for (i=0 ; i<cgmNColors ; i++) cgmPalette[i]= defaultPalette[i];
    cgmNColors= defaultNColors;
    /* Reset color mode on all engines, and
       set palette on any new engines */
    for (i=0 ; i<8 ; i++) {
      if (outEngines[i]) {
	refreshPalette= (cgmNColors>offset &&
			 (alteredPalette ||
			  outEngines[i]->palette!=cgmPalette+offset ||
			  outEngines[i]->nColors!=cgmNColors-offset));
	if (outTypes[i]==2) {
	  setXPalette= refreshPalette;
	} else {
	  outEngines[i]->colorMode= 0;
	  if (refreshPalette) GpSetPalette(outEngines[i], cgmPalette+offset,
					   cgmNColors-offset);
	}
      }
    }
    alteredPalette= 0;
    needPalette= 0;

  } else {
    int g;
    cgmPalette[0] = P_RGB(255, 255, 255);     /* background */
    cgmPalette[1] = P_RGB(0, 0, 0);           /* foreground */
    cgmPalette[2] = P_RGB(0, 0, 0);           /* black */
    cgmPalette[3] = P_RGB(255, 255, 255);     /* white */
    cgmPalette[4] = P_RGB(255, 0, 0);         /* red */
    cgmPalette[5] = P_RGB(0, 255, 0);         /* green */
    cgmPalette[6] = P_RGB(0, 0, 255);         /* blue */
    cgmPalette[7] = P_RGB(0, 255, 255);       /* cyan */
    cgmPalette[8] = P_RGB(255, 0, 255);       /* magenta */
    cgmPalette[9] = P_RGB(255, 255, 0);       /* yellow */
    GpPutGray(10, cgmPalette);
    for (i=0 ; i<230 ; i++) {
      g = i + ((i+5)/9);
      cgmPalette[i+10] = P_RGB(g,g,g);        /* 230 grays */
    }
    GpPutRGB(230, cgmPalette+10);
    cgmNColors= 240;
    for (i=0 ; i<cgmNColors ; i++) defaultPalette[i]= cgmPalette[i];
    defaultNColors= cgmNColors;
    cgmNColMax= 256;
    alteredPalette= 1;  /* only used in forPage branch above */
  }

  cgmColorChange= 0;
}

static int cgmSmoothing= 0;

/* ------------------------------------------------------------------------ */

static int DoClass0(int id, long nOctets)
{
  /* Delimiter elements, class 0 */
  switch (id) {
  case 0:   /* NO-OP */
    SkipParameters(nOctets);
    break;

  case 1:   /* BEGIN METAFILE */
    {
      nOctets= ReadParameters(nOctets, (void *)0, 0L);
      DePascalify(metafileName, paramList, nOctets);
      /* Reset default page specifications to CGM defaults */
      pageDesc= cgmDesc;
      pageControls= cgmControls;
      pageDefaults= cgmDefaults;
      isGistCGM= 0;
      cgmLandscape= 0;
      /* set default palette */
      ResetPalette(0);
      /* reset illegibility flags */
      nBytesInt= 2;
      vdcIllegible= realIllegible= indexIllegible=
        cvalIllegible= cndxIllegible= cselIllegible= 0;
    }
    break;

  case 2:   /* END METAFILE */
    SkipParameters(nOctets);
    break;

  case 3:   /* BEGIN PICTURE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    DePascalify(pictureName, paramList, nOctets);
    /* Reset default page specifications */
    pictDesc= pageDesc;
    pictControls= pageControls;
    gistA.l= pageDefaults.l;
    gistA.m= pageDefaults.m;
    gistA.f= pageDefaults.f;
    gistA.t= pageDefaults.t;
    nonGistA= pageDefaults.ng;
    cgmSmoothing= 0;
    ResetPalette(1);

    /* Clear any previous picture, or begin a new page */
    GpClear(0, CONDITIONALLY);
#ifdef NEED_PICTURE_HOOKS
    {
      extern void begin_picture_hook(void);
      begin_picture_hook();
    }
#endif
    break;

  case 4:   /* BEGIN PICTURE BODY */
    SkipParameters(nOctets);
    SetVDC(pictDesc.vdc);
    GSetClip(pictControls.clipOn);
    break;

  case 5:   /* END PICTURE */
    SkipParameters(nOctets);
#ifdef NEED_PICTURE_HOOKS
    {
      extern void end_picture_hook(void);
      end_picture_hook();
    }
#endif
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 0 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass1(int id, long nOctets)
{
  /* Metafile descriptor elements, class 1 */
  switch (id) {
  case 1:   /* METAFILE VERSION */
    SkipParameters(nOctets);
    break;

  case 2:   /* METAFILE DESCRIPTION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    DePascalify(metafileDescription, paramList, nOctets);
    if (strncmp(metafileDescription, "Gist;  ", 7)==0) isGistCGM= 1;
    else isGistCGM= 0;
    if (!amBatch) {
      p_stdout(currentCGM->name);
      p_stdout(" metafile description:\n");
      p_stdout(metafileDescription + (isGistCGM? 7 : 0));
      p_stdout("\n");
    }
    break;

  case 3:   /* VDC TYPE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=2 || paramList[0]!=0 || paramList[1]!=0) {
      Warning("CGM illegible, does not use integer VDC space", "");
      vdcIllegible= 1;
      return 1;
    }
    break;

  case 4:   /* INTEGER PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==nBytesInt) nBytesInt= SnarfInteger(paramList)>>3;
    else nBytesInt= -1;
    break;

  case 5:   /* REAL PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=3*nBytesInt || SnarfInteger(paramList)!=1 ||
	SnarfInteger(paramList+nBytesInt)!=16 ||
	SnarfInteger(paramList+2*nBytesInt)!=16) {
      Warning("CGM partly illegible, not 32 bit fixed point reals", "");
      realIllegible= 1;
      return 1;
    }
    break;

  case 6:   /* INDEX PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=nBytesInt || SnarfInteger(paramList)!=16) {
      Warning("CGM partly illegible, not 16 bit index precision", "");
      indexIllegible= 1;
      return 1;
    }
    break;

  case 7:   /* COLOR PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=nBytesInt ||
	((cvalIllegible= SnarfInteger(paramList))!=8 && cvalIllegible!=16)) {
      Warning("CGM partly illegible, not 8 or 16 bit color precision", "");
      cvalIllegible= 1;
      return 1;
    } else {
      /* 8 is the default value, 16 grudgingly supported to allow ATC GKS
         produced files */
      if (cvalIllegible==8) cvalIllegible= 0;
    }
    break;

  case 8:   /* COLOR INDEX PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    { int cip= 8;
      if (nOctets!=nBytesInt ||
	  (cip= SnarfInteger(paramList))!=8) {
	Warning("CGM partly illegible, not 8 bit color index precision", "");
	cndxIllegible= cselIllegible= (cip==16? 16 : 1);
	return 1;
      }
    }
    break;

  case 9:   /* MAXIMUM COLOR INDEX */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==1 && !cndxIllegible) {
      pageDesc.colorMaxI= paramList[0];
      cgmNColMax= pageDesc.colorMaxI+1;
    }
    break;

  case 10:  /* COLOR VALUE EXTENT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==6 && !cvalIllegible) {
      pageDesc.colorMaxV[0]= paramList[0];
      pageDesc.colorMaxV[1]= paramList[1];
      pageDesc.colorMaxV[2]= paramList[2];
      pageDesc.colorMaxV[3]= paramList[3];
      pageDesc.colorMaxV[4]= paramList[4];
      pageDesc.colorMaxV[5]= paramList[5];
    } else if (nOctets==12 && cvalIllegible==16) {
      pageDesc.colorMaxV[0]= Snarf16(paramList);
      pageDesc.colorMaxV[1]= Snarf16(paramList+2);
      pageDesc.colorMaxV[2]= Snarf16(paramList+4);
      pageDesc.colorMaxV[3]= Snarf16(paramList+6);
      pageDesc.colorMaxV[4]= Snarf16(paramList+8);
      pageDesc.colorMaxV[5]= Snarf16(paramList+10);
    }
    break;

  case 11:  /* METAFILE ELEMENT LIST */
    SkipParameters(nOctets);
    break;

  case 12:  /* METAFILE DEFAULTS REPLACEMENT */
    {
      nOctets= ReadParameters(nOctets, (void *)0, 0L);
      if (nOctets>0) {
	int klass, id;
	cmdString= (Octet *)p_malloc(sizeof(Octet)*nOctets);
	if (!cmdString) {
	  Warning("memory manager failed in DoClass1(DEFAULTS)", "");
	  return 1;
	}
	memcpy(cmdString, paramList, nOctets);
	currentCmd= cmdString;
	cmdLength= nOctets;
	while (cmdLength>0) {
	  nOctets= ReadElement(&klass, &id);
	  if (nOctets>=0) {
	    switch (klass) {
	    case 2:
	      pictDesc= pageDesc;
	      DoClass2(id, nOctets);
	      pageDesc= pictDesc;
	      break;
	    case 3:
	      pictControls= pageControls;
	      DoClass3(id, nOctets);
	      pageControls= pictControls;
	      break;
	    case 5:
	      gistA.l= pageDefaults.l;
	      gistA.m= pageDefaults.m;
	      gistA.f= pageDefaults.f;
	      gistA.t= pageDefaults.t;
	      nonGistA= pageDefaults.ng;
	      DoClass5(id, nOctets);
	      pageDefaults.l= gistA.l;
	      pageDefaults.m= gistA.m;
	      pageDefaults.f= gistA.f;
	      pageDefaults.t= gistA.t;
	      pageDefaults.ng= nonGistA;
	      break;
	    case 6:
	      DoClass6(id, nOctets);
	      break;
	    default:
	      /* Other commands are illegal, but whining is pointless */
	      break;
	    }
	  }
	}
	p_free(cmdString);
	cmdString= 0;
      }
    }
    break;

  case 13:  /* FONT LIST */
    {
      long nPart;
      Octet *pasctxt= paramList;
      int i, *oldFonts;
      nOctets= ReadParameters(nOctets, (void *)0, 0L);
      pageDesc.nfonts= 0;
      do {
	nPart= DePascalify(fontName, pasctxt, nOctets);
	pasctxt+= nPart;
	nOctets-= nPart;
	for (i=0 ; i<N_GIST_FONTS ; i++)
	  if (strcmp(fontName, gistFonts[i])==0) break;
	if (i>=N_GIST_FONTS) i= 0;
	if (pageDesc.nfonts>=pageDesc.maxFonts) {
	  oldFonts= pageDesc.fonts;
	  pageDesc.fonts= (int *)p_malloc(sizeof(int)*(pageDesc.nfonts+20));
	  if (!pageDesc.fonts) {
	    pageDesc.fonts= oldFonts;
	    Warning("memory manager failed in DoClass1(FONT LIST)", "");
	    return 2;
	  }
	  pageDesc.maxFonts= pageDesc.nfonts+20;
	  for (i=0 ; i<pageDesc.nfonts ; i++) pageDesc.fonts[i]= oldFonts[i];
	  if (oldFonts && oldFonts!=defaultNumbers) p_free(oldFonts);
	}
	pageDesc.fonts[pageDesc.nfonts++]= i;
      } while (nOctets>0);
    }
    break;

  case 14:  /* CHARACTER SET LIST */
    SkipParameters(nOctets);
    break;

  case 15:  /* CHARACTER CODING ANNOUNCER */
    SkipParameters(nOctets);
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 1 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass2(int id, long nOctets)
{
  /* Picture descriptor elements, class 2 */
  switch (id) {
  case 1:   /* SCALING MODE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets>2 && paramList[1]) pictDesc.scaling= METRIC;
    else pictDesc.scaling= ABSTRACT;
    if (!realIllegible && nOctets==6) {
      if (pictDesc.scaling==METRIC) pictDesc.metric= SnarfReal(paramList+2);
      else pictDesc.metric= DEFAULT_METRIC;
    } else if (pictDesc.scaling==METRIC) {
      pictDesc.metric= DEFAULT_METRIC;
    }
    break;

  case 2:   /* COLOR SELECTION MODE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=2 || paramList[1]) {
      Warning("CGM partly illegible, not indexed color mode", "");
      cselIllegible= 1;
      return 1;
    } else cselIllegible= cndxIllegible;
    break;

  case 3:   /* LINE WIDTH SPECIFICATION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=2 || paramList[1]) pictDesc.lwidth= SCALED;
    else pictDesc.lwidth= ABSOLUTE;
    break;

  case 4:   /* MARKER SIZE SPECIFICATION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=2 || paramList[1]) pictDesc.msize= SCALED;
    else pictDesc.msize= ABSOLUTE;
    break;

  case 5:   /* EDGE WIDTH SPECIFICATION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=2 || paramList[1]) pictDesc.ewidth= SCALED;
    else pictDesc.ewidth= ABSOLUTE;
    break;

  case 6:   /* VDC EXTENT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==8) {
      short *vdc= CGM_WORDS(paramList, 4L);
      pictDesc.vdc[0]= vdc[0];
      pictDesc.vdc[1]= vdc[1];
      pictDesc.vdc[2]= vdc[2];
      pictDesc.vdc[3]= vdc[3];
      SetVDC(pictDesc.vdc);
      if (pictControls.clipDefault) {
	pictControls.clipBox[0]= vdc[0];
	pictControls.clipBox[1]= vdc[1];
	pictControls.clipBox[2]= vdc[2];
	pictControls.clipBox[3]= vdc[3];
      }
    }
    break;

  case 7:   /* BACKGROUND COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    {
      int red, green, blue;
      if (!cvalIllegible && nOctets==3) {
	/* This is actually ignored... */
	red= paramList[0];
	green= paramList[1];
	blue= paramList[2];
	/* GpPutNTSC(1, &pictDesc.background); */
      } else if (cvalIllegible==16 && nOctets==6) {
	/* This is actually ignored... */
	red= Snarf16(paramList)>>8;
	green= Snarf16(paramList+2)>>8;
	blue= Snarf16(paramList+4)>>8;
	/* GpPutNTSC(1, &pictDesc.background); */
      } else {
	nOctets= 0;
	red= green= blue= 0;
      }
      if (nOctets) {
	if (pictDesc.background.red!=red ||
	    pictDesc.background.green!=green ||
	    pictDesc.background.blue!=blue) {
	  char msg[80];
	  /* One warning each time the bozo tries to change it... */
	  sprintf(msg,
		  "WARNING- BACKGROUND COLOR %02x%02x%02x not supported.\n",
		  pictDesc.background.red, pictDesc.background.green,
		  pictDesc.background.blue);
	  p_stdout(msg);
	  p_stdout("   You may want to set \"Gist*background:\" X resource.\n");
	}
	pictDesc.background.red= red;
	pictDesc.background.green= green;
	pictDesc.background.blue= blue;
      }
    }
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 2 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass3(int id, long nOctets)
{
  /* Control elements, class 3 */
  switch (id) {
  case 1:   /* VDC INTEGER PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets!=nBytesInt || SnarfInteger(paramList)!=16) {
      Warning("CGM illegible, not 16 bit integer VDC coordinates", "");
      vdcIllegible= 1;
      return 1;
    } else vdcIllegible= 0;
    break;

  case 2:   /* VDC REAL PRECISION */
    SkipParameters(nOctets);
    break;

  case 3:   /* AUXILLIARY COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!cselIllegible && nOctets==1) pictControls.auxColor= paramList[0];
    break;

  case 4:   /* TRANSPARENCY */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) {
      pictControls.transparency= paramList[1];
      gistA.t.opaque= !pictControls.transparency;
    }
    break;

  case 5:   /* CLIP RECTANGLE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==8) {
      short *clipBox= CGM_WORDS(paramList, 4L);
      pictControls.clipBox[0]= clipBox[0];
      pictControls.clipBox[1]= clipBox[1];
      pictControls.clipBox[2]= clipBox[2];
      pictControls.clipBox[3]= clipBox[3];
      if (pictControls.clipOn) GSetClip(1);
      pictControls.clipDefault= 0;
    }
    break;

  case 6:   /* CLIP INDICATOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) {
      int clipOn= Snarf16(paramList);
      if (clipOn!=pictControls.clipOn) {
	pictControls.clipOn= clipOn;
	if (clipOn) GSetClip(1);
	else GSetClip(0);
      }
    }
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 3 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass4(int id, long nOctets)
{
  /* Graphical primitive elements, class 4 */

  if (cgmColorChange) {
    /* If a palette has been specified for this page, dump it to all
       non-X engines.  */
    int i, offset= isGistCGM? 10 : (bg0fg1? 2 : 0);
    for (i=0 ; i<8 ; i++) {
      if (outEngines[i] && outTypes[i]!=2) {
	outEngines[i]->colorMode= 1;
	GpSetPalette(outEngines[i], cgmPalette+offset, cgmNColors-offset);
      }
    }
    alteredPalette= 1;
    cgmColorChange= 0;
    setXPalette= 1;
  }
  if (setXPalette && (needPalette || id==9)) {
    /* X engines change palette only when necessary -- that is, if a
       non-standard color has been referenced, or this is a cell array.  */
    int i, offset= isGistCGM? 10 : (bg0fg1? 2 : 0);
    for (i=0 ; i<8 ; i++) {
      if (outEngines[i] && outTypes[i]==2) {
	outEngines[i]->colorMode= alteredPalette;
	GpSetPalette(outEngines[i], cgmPalette+offset, cgmNColors-offset);
      }
    }
    setXPalette= 0;
  }

  switch (id) {
  case 1:   /* POLYLINE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=4) {
      long nPoints= nOctets>>2;
      short *vdc= CGM_WORDS(paramList, nPoints<<1);
      int (*Lines)(long, const GpReal *, const GpReal *);

      if (cgmSmoothing) {
	long i;
	gistA.dl.closed= 0;
	gistA.dl.smooth= cgmSmoothing;
	gistA.dl.marks= gistA.dl.rays= 0;
	cgmSmoothing= 0;
	nPoints= nPoints/3;
	for (i=0 ; i<nPoints ; i++) {
	  vdc[2*i]= vdc[6*i];
	  vdc[2*i+1]= vdc[6*i+1];
	}
	Lines= &GaLines;
      } else {
	Lines= &GpLines;
      }

      if (!CheckScratch(nPoints)) {
	ConvertPoints(nPoints, vdc, ndcX, ndcY);
	Lines(nPoints, ndcX, ndcY);
      }
    }
    break;

  case 2:   /* DISJOINT POLYLINE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=8) {
      long nPoints= (nOctets>>2)&(~1L);
      short *vdc= CGM_WORDS(paramList, nPoints<<1);

      if (!CheckScratch(nPoints)) {
	long i, nSegs= nPoints>>1;
	for (i=0 ; i<nSegs ; i++) {
	  ndcX[i]= cgmScaleX*(GpReal)vdc[4*i]+cgmOffsetX;
	  ndcY[i]= cgmScaleY*(GpReal)vdc[4*i+1]+cgmOffsetY;
	  ndcX[i+nSegs]= cgmScaleX*(GpReal)vdc[4*i+2]+cgmOffsetX;
	  ndcY[i+nSegs]= cgmScaleY*(GpReal)vdc[4*i+3]+cgmOffsetY;
	}
	GpDisjoint(nSegs, ndcX, ndcY, ndcX+nSegs, ndcY+nSegs);
      }
    }
    break;

  case 3:   /* POLYMARKER */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=4) {
      long nPoints= nOctets>>2;
      short *vdc= CGM_WORDS(paramList, nPoints<<1);
      if (!CheckScratch(nPoints)) {
	ConvertPoints(nPoints, vdc, ndcX, ndcY);
	GpMarkers(nPoints, ndcX, ndcY);
      }
    }
    break;

  case 4:   /* TEXT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=7) {
      short ndc[2], final;
      GpReal x0, y0;
      char *text;
      ndc[0]= Snarf16(paramList);
      ndc[1]= Snarf16(paramList+2);
      ConvertPoints(1L, ndc, &x0, &y0);
      final= Snarf16(paramList+4);
      text= SnarfText(paramList+6);
      if (final) {
	GpText(x0, y0, PathifyText(text,nonGistA.path));
	cgmTextLen= 0;
      }
      else RememberPoint(x0, y0);
    }
    break;

  case 5:   /* RESTRICTED TEXT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=11) {
      short ndc[2], final;
      GpReal x0, y0;
      char *text;
      /* ignore box width, box height parameters */
      ndc[0]= Snarf16(paramList+4);
      ndc[1]= Snarf16(paramList+6);
      ConvertPoints(1L, ndc, &x0, &y0);
      final= Snarf16(paramList+8);
      text= SnarfText(paramList+10);
      if (final) {
	GpText(x0, y0, PathifyText(text,nonGistA.path));
	cgmTextLen= 0;
      }
      else RememberPoint(x0, y0);
    }
    break;

  case 6:   /* APPEND TEXT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=12) {
      short final;
      char *text;
      final= Snarf16(paramList+2);
      text= SnarfMoreText(paramList+2);
      if (final) GpText(cgmX0, cgmY0, PathifyText(text,nonGistA.path));
    }
    break;

  case 7:   /* POLYGON */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets>=4) {
      long nPoints= nOctets>>2;
      short *vdc= CGM_WORDS(paramList, nPoints<<1);
      if (!CheckScratch(nPoints)) {
	int type= gistA.e.type;
	if (!nonGistA.eVisible) gistA.e.type= L_NONE;
	ConvertPoints(nPoints, vdc, ndcX, ndcY);
	if (gistA.f.style!=F_EMPTY) {
	  GpFill(nPoints, ndcX, ndcY);
	} else if (gistA.e.type!=L_NONE) {
	  gistA.dl.closed= 1;
	  gistA.dl.smooth= 0;
	  gistA.dl.marks= gistA.dl.rays= 0;
	  GaLines(nPoints, ndcX, ndcY);
	  gistA.dl.closed= 0;
	}
	gistA.e.type= type;
      }
    }
    break;

  case 8:   /* POLYGON SET */
    SkipParameters(nOctets);
    break;

  case 9:   /* CELL ARRAY */
    if (!cselIllegible && !vdcIllegible &&
	(nOctets&0x7fff)>=14+3*nBytesInt && !(nBytesInt&1)) {
      long width, height;
      int colorPrec, mode;
      short pqr[6];

      nOctets-= ReadParameters(14L+3*nBytesInt, (void *)0, 0L);
      pqr[0]= Snarf16(paramList);
      pqr[1]= Snarf16(paramList+2);
      pqr[2]= Snarf16(paramList+4);
      pqr[3]= Snarf16(paramList+6);
      pqr[4]= Snarf16(paramList+8);
      pqr[5]= Snarf16(paramList+10);
      width= SnarfInteger(paramList+12);
      height= SnarfInteger(paramList+12+nBytesInt);
      colorPrec= SnarfInteger(paramList+12+2*nBytesInt);
      mode= Snarf16(paramList+12+3*nBytesInt);

      if (mode==1 && (colorPrec==0 || colorPrec==8) &&
	  pqr[4]==pqr[2] && pqr[5]==pqr[1]) {
	long nColumns= width&1? width+1 : width;
	long nCells= nColumns*height;
	GpReal pqx[2], pqy[2];
	GpColor *colors= (GpColor *)p_malloc(sizeof(GpColor)*nCells);
	if (colors) {
	  if (ReadParameters(nOctets, colors, nCells)==nCells) {
	    if (isGistCGM) {
	      long i;
	      for (i=0 ; i<nCells ; i++) colors[i]-= 10;
	    }  /* bg0fg1 ignored here */
	    ConvertPoints(2L, pqr, pqx, pqy);
	    GpCells(pqx[0], pqy[0], pqx[1], pqy[1], width, height,
		    nColumns, colors);
	  }
	  p_free(colors);
	} else {
	  SkipParameters(nOctets);
	  Warning("memory manager failed in DoClass4(CELL ARRAY)", "");
	}
      } else {
	SkipParameters(nOctets);
      }
    } else {
      SkipParameters(nOctets);
    }
    break;

  case 10:  /* GENERALIZED DRAWING PRIMITIVE */
    SkipParameters(nOctets);
    break;

  case 11:  /* RECTANGLE */
    SkipParameters(nOctets);
    break;

  case 12:  /* CIRCLE */
    SkipParameters(nOctets);
    break;

  case 13:  /* CIRCULAR ARC 3 POINT */
    SkipParameters(nOctets);
    break;

  case 14:  /* CIRCULAR ARC 3 POINT CLOSE */
    SkipParameters(nOctets);
    break;

  case 15:  /* CIRCULAR ARC CENTER */
    SkipParameters(nOctets);
    break;

  case 16:  /* CIRCULAR ARC CENTER CLOSE */
    SkipParameters(nOctets);
    break;

  case 17:  /* ELLIPSE */
    SkipParameters(nOctets);
    break;

  case 18:  /* ELLIPTICAL ARC */
    SkipParameters(nOctets);
    break;

  case 19:  /* ELLIPTICAL ARC CLOSE */
    SkipParameters(nOctets);
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 4 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass5(int id, long nOctets)
{
  /* Attribute elements, class 5 */
  switch (id) {
  case 1:   /* LINE BUNDLE INDEX */
    /* Treat line bundle index as identical to line type */

  case 2:   /* LINE TYPE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) gistA.l.type= Snarf16(paramList);
    if (gistA.l.type<=0 || gistA.l.type>5) {
      if (gistA.l.type<=0) gistA.l.type= 1-gistA.l.type;
      if (gistA.l.type>5) gistA.l.type= gistA.l.type%5 + 1;
    }
    break;

  case 3:   /* LINE WIDTH */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if ((pictDesc.lwidth==ABSOLUTE && !vdcIllegible && nOctets==2) ||
	(pictDesc.lwidth==SCALED && !realIllegible && nOctets==4))
      gistA.l.width= SnarfSA(paramList, pictDesc.lwidth,
			     DEFAULT_LINE_WIDTH);
    break;

  case 4:   /* LINE COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    gistA.l.color= SnarfColor(paramList, nOctets);
    break;

  case 5:   /* MARKER BUNDLE INDEX */
    /* Treat marker bundle index as identical to marker type */

  case 6:   /* MARKER TYPE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) gistA.m.type= Snarf16(paramList);
    if (gistA.m.type<=0 || gistA.m.type>5) {
      if (gistA.m.type<=0) gistA.m.type= 1-gistA.m.type;
      if (gistA.m.type>5) gistA.m.type= gistA.m.type%5 + 1;
    }
    break;

  case 7:   /* MARKER SIZE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if ((pictDesc.msize==ABSOLUTE && !vdcIllegible && nOctets==2) ||
	(pictDesc.msize==SCALED && !realIllegible && nOctets==4))
      gistA.m.size= SnarfSA(paramList, pictDesc.msize,
			    DEFAULT_MARKER_SIZE);
    break;

  case 8:   /* MARKER COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    gistA.m.color= SnarfColor(paramList, nOctets);
    break;

  case 9:   /* TEXT BUNDLE INDEX */
    /* Treat text bundle index as identical to text font index */

  case 10:  /* TEXT FONT INDEX */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) gistA.t.font= Snarf16(paramList);
    if (gistA.t.font==0) gistA.t.font= 1;
    gistA.t.font--;
    if (gistA.t.font<0 || gistA.t.font>=pageDesc.nfonts) {
      if (gistA.t.font<0) gistA.t.font= -gistA.t.font;
      if (gistA.t.font>=pageDesc.nfonts)
	gistA.t.font= gistA.t.font%pageDesc.nfonts;
    }
    gistA.t.font= pageDesc.fonts[gistA.t.font];
    break;

  case 11:  /* TEXT PRECISION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) nonGistA.prec= Snarf16(paramList);
    break;

  case 12:  /* CHARACTER EXPANSION FACTOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!realIllegible && nOctets==4)
      nonGistA.expand= SnarfReal(paramList);
    break;

  case 13:  /* CHARACTER SPACING */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!realIllegible && nOctets==4)
      nonGistA.spacing= SnarfReal(paramList);
    break;

  case 14:  /* TEXT COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    gistA.t.color= SnarfColor(paramList, nOctets);
    break;

  case 15:  /* CHARACTER HEIGHT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==2) {
      /* Point size is somewhat greater than CGM "character height"--
	 use the same factor of 0.8 used by the Gist CGMEngine to assure
	 that, at least, Gist-produced text is rendered correctly.  */
      gistA.t.height= (1.0/0.8)*cgmScale*((GpReal)Snarf16(paramList));
    }
    break;

  case 16:  /* CHARACTER ORIENTATION */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==8) {
      short vdc[2];
      vdc[0]= Snarf16(paramList);
      vdc[1]= Snarf16(paramList+2);
      ConvertPoints(1L, vdc, &nonGistA.upX, &nonGistA.upY);
      vdc[0]= Snarf16(paramList+4);
      vdc[1]= Snarf16(paramList+6);
      ConvertPoints(1L, vdc, &nonGistA.baseX, &nonGistA.baseY);
      if (nonGistA.baseX>nonGistA.baseY) {
	if (nonGistA.baseX>-nonGistA.baseY) gistA.t.orient= TX_RIGHT;
	else gistA.t.orient= TX_DOWN;
      } else {
	if (nonGistA.baseX>-nonGistA.baseY) gistA.t.orient= TX_UP;
	else gistA.t.orient= TX_LEFT;
      }
    }
    break;

  case 17:  /* TEXT PATH */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) nonGistA.path= Snarf16(paramList);
    break;

  case 18:  /* TEXT ALIGNMENT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets>4) {
      gistA.t.alignH= Snarf16(paramList);
      gistA.t.alignV= Snarf16(paramList+2);
      if (gistA.t.alignH<0 || gistA.t.alignH>3) gistA.t.alignH= 0;
      if (gistA.t.alignV<0 || gistA.t.alignV>5) gistA.t.alignV= 0;
    }
    break;

  case 19:  /* CHARACTER SET INDEX */
    SkipParameters(nOctets);
    break;

  case 20:  /* ALTERNATE CHARACTER SET INDEX */
    SkipParameters(nOctets);
    break;

  case 21:  /* FILL BUNDLE INDEX */
    /* Treat fill bundle index as identical to interior style */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) gistA.f.style= Snarf16(paramList);
    break;

  case 22:  /* INTERIOR STYLE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) gistA.f.style= Snarf16(paramList);
    break;

  case 23:  /* FILL COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    gistA.f.color= SnarfColor(paramList, nOctets);
    break;

  case 24:  /* HATCH INDEX */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) nonGistA.hindex= Snarf16(paramList);
    break;

  case 25:  /* PATTERN INDEX */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) nonGistA.pindex= Snarf16(paramList);
    break;

  case 26:  /* EDGE BUNDLE INDEX */
    /* Treat edge bundle index as identical to edge type */

  case 27:  /* EDGE TYPE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!indexIllegible && nOctets==2) gistA.e.type= Snarf16(paramList);
    break;

  case 28:  /* EDGE WIDTH */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if ((pictDesc.ewidth==ABSOLUTE && !vdcIllegible && nOctets==2) ||
	(pictDesc.ewidth==SCALED && !realIllegible && nOctets==4))
      gistA.e.width= SnarfSA(paramList, pictDesc.ewidth, DEFAULT_LINE_WIDTH);
    break;

  case 29:  /* EDGE COLOR */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    gistA.e.color= SnarfColor(paramList, nOctets);
    break;

  case 30:  /* EDGE VISIBILITY */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (nOctets==2) nonGistA.eVisible= Snarf16(paramList);
    break;

  case 31:  /* FILL REFERENCE POINT */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==4) {
      short refp[2];
      refp[0]= Snarf16(paramList);
      refp[1]= Snarf16(paramList+2);
      /* ConvertPoints(1L, refp, &gistA.f.refpX, &gistA.f.refpY); */
    }
    break;

  case 32:  /* PATTERN TABLE */
    SkipParameters(nOctets);
    break;

  case 33:  /* PATTERN SIZE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if (!vdcIllegible && nOctets==8) {
      short hw[2];
      short hx= Snarf16(paramList);
      short hy= Snarf16(paramList+2);
      short wx= Snarf16(paramList+4);
      short wy= Snarf16(paramList+6);
      if (hx<0) hx= -hx;
      if (hy<0) hy= -hy;
      if (wx<0) wx= -wx;
      if (wy<0) wy= -wy;
      if (wx<hx) hw[0]= hx;
      else hw[0]= wx;
      if (hy<wy) hw[1]= wy;
      else hw[1]= hy;
      /* ConvertPoints(1L, hw, &gistA.f.sizeX, &gistA.f.sizeY); */
    }
    break;

  case 34:  /* COLOR TABLE */
    nOctets= ReadParameters(nOctets, (void *)0, 0L);
    if ((!cndxIllegible || cndxIllegible==16) &&
	((!cvalIllegible && nOctets>=4) ||
	 (cvalIllegible==16 && nOctets>=7))) {
      int i, begin= (cndxIllegible? paramList[1] : paramList[0]);
      int i0= (cndxIllegible? 2 : 1);
      int nColors= cvalIllegible? (nOctets-i0)/6 : (nOctets-i0)/3;
      unsigned long mins[3], deltas[3];
      int r, g, b;
      mins[0]= pictDesc.colorMaxV[0];
      mins[1]= pictDesc.colorMaxV[1];
      mins[2]= pictDesc.colorMaxV[2];
      deltas[0]= pictDesc.colorMaxV[3]-mins[0];
      deltas[1]= pictDesc.colorMaxV[4]-mins[1];
      deltas[2]= pictDesc.colorMaxV[5]-mins[2];
#undef COLOR_8
#define COLOR_8(i,n) (255L*((n)-mins[i]))/deltas[i]
#undef COLOR_16
#define COLOR_16(i,n) (255L*(((unsigned int)(n))-mins[i]))/deltas[i]
      if (begin+nColors>cgmNColMax) nColors= cgmNColMax-begin;
      for (i=0 ; i<nColors ; i++) {
	if (!cvalIllegible) {
          r = COLOR_8(0,paramList[3*i+i0]);
          g = COLOR_8(1,paramList[3*i+i0+1]);
          b = COLOR_8(2,paramList[3*i+i0+2]);
	} else {
	  r = COLOR_16(0,Snarf16(paramList+6*i+i0));
	  g = COLOR_16(1,Snarf16(paramList+6*i+i0+2));
	  b = COLOR_16(2,Snarf16(paramList+6*i+i0+4));
	}
        cgmPalette[i+begin] = P_RGB(r, g, b);
      }
      if (nColors>0) {
	int offset= isGistCGM? 10 : (bg0fg1? 2 : 0);
	GpPutNTSC(nColors, cgmPalette+begin);
	if (!cgmColorChange || cgmNColors<begin+nColors)
	  cgmNColors= begin+nColors;
	if (cgmNColors>offset) cgmColorChange= 1;
      }
    }
    break;

  case 35:  /* ASPECT SOURCE FLAGS */
    SkipParameters(nOctets);
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 5 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass6(int id, long nOctets)
{
  /* Escape elements, class 6 */
  switch (id) {
  case 1:   /* ESCAPE */
    SkipParameters(nOctets);
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 6 CGM element", "");
    break;
  }

  return 0;
}

static int DoClass7(int id, long nOctets)
{
  /* External elements, class 7 */
  switch (id) {
  case 1:   /* MESSAGE */
    SkipParameters(nOctets);
    break;

  case 2:   /* APPLICATION DATA */
    if (isGistCGM) {
      nOctets= ReadParameters(nOctets, (void *)0, 0L);
      if (nOctets==3) cgmSmoothing= Snarf16(paramList);
    } else {
      SkipParameters(nOctets);
    }
    break;

  default:
    SkipParameters(nOctets);
    Warning("non-standard class 7 CGM element", "");
    break;
  }

  return 0;
}

/* ------------------------------------------------------------------------ */