File: plotPNM.c

package info (click to toggle)
magic 8.3.105%2Bds.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 17,128 kB
  • sloc: ansic: 175,615; sh: 7,634; tcl: 4,536; lisp: 2,554; makefile: 946; cpp: 587; python: 389; csh: 148; awk: 140
file content (1475 lines) | stat: -rw-r--r-- 36,814 bytes parent folder | download | duplicates (2)
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
/*
 * plotPNM.c --
 *
 * This file contains procedures that generate PNM format files
 * to describe a section of layout.
 *
 *     *********************************************************************
 *     * Copyright (C) 2000 Cornell University                             *
 *     * Permission to use, copy, modify, and distribute this              *
 *     * software and its documentation for any purpose and without        *
 *     * fee is hereby granted, provided that the above copyright          *
 *     * notice appear in all copies.  Cornell University                  *
 *     * makes no representations about the suitability of this            *
 *     * software for any purpose.  It is provided "as is" without         *
 *     * express or implied warranty.  Export of this software outside     *
 *     * of the United States of America may require an export license.    *
 *     *********************************************************************
 *
 * R. Timothy Edwards
 * Copyright (C) 2004
 * MultiGiG, Inc.
 * Scotts Valley, CA
 *
 * Cleaned up the code, including optimization for speed
 * Added:  Non-Manhattan geometry handling, 24-bit color,
 * plot styles automatically generated from display styles,
 * downsampling for large plots, extended syntax for the
 * technology file description.
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/plot/plotPNM.c,v 1.3 2010/06/24 12:37:25 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <string.h>
#include <math.h>

#include "utils/magic.h"
#include "utils/geometry.h"
#include "utils/geofast.h"
#include "tiles/tile.h"
#include "utils/hash.h"
#include "database/database.h"
#include "utils/tech.h"
#include "utils/malloc.h"
#include "utils/utils.h"
#include "utils/styles.h"
#include "windows/windows.h"
#include "graphics/graphics.h"
#include "dbwind/dbwtech.h"
#include "dbwind/dbwind.h"
#include "utils/main.h"
#include "commands/commands.h"
#include "textio/textio.h"
#include "utils/signals.h"
#include "plot/plotInt.h"

#define LANCZOS_KERNEL_SIZE 1024
#define PI 3.14159265

/* Structure for saving R, G, B components of colors	*/
/* from a non-default colormap.				*/

typedef struct _pnmcolor {
    unsigned char r, g, b;
} pnmcolor;

pnmcolor *PNMcolors = NULL;
static int ncolors = 0;

#define PIXELSZ sizeof(pnmcolor)

int PlotPNMmaxmem = 64 * 1024;	/* 64MB */
int PlotPNMdownsample = 0;	/* No downsampling by default */
unsigned char PlotPNMBG = 0xff;	/* White background by default */

#ifdef VERSATEC
bool PlotPNMRTL = FALSE;	/* If true, filter output through HP driver */
#endif

/*
 * Local variables, modified/shared by callbacks.
 */

int 		Init_Error;
float 		lk[2 * LANCZOS_KERNEL_SIZE + 1];
int		*lkstep; /* lanczos kernel steps */
pnmcolor 	*rtile;
int		tile_xsize, tile_ysize;
int		ds_xsize, ds_ysize;
Rect		bb;
unsigned long	BBinit;
int		tile_yshift, tile_xshift;
int 		im_x, im_y;
int		im_yoffset;
int 		y_pixels;

/* Structure for saving styles that are different from	*/
/* the styles loaded for this technology.		*/

typedef struct _dstyle {
    char *name;
    int init;
    unsigned int wmask;
    pnmcolor color;
} dstyle;

dstyle *Dstyles = NULL;
static int ndstyles = 0;

/* Structure which records how to paint a tile type.	*/

typedef struct _pstyle {
    unsigned int wmask;
    pnmcolor color;
} pstyle;

pstyle *PaintStyles = NULL;

/* Forward declarations */

extern void PlotLoadStyles();
extern void PlotLoadColormap();
extern pnmcolor PNMColorBlend();
extern pnmcolor PNMColorIndexAndBlend();

/*
 * ----------------------------------------------------------------------------
 *
 * Function for output of PNM line data to HPRTL format
 *
 * ----------------------------------------------------------------------------
 */

#ifdef VERSATEC

struct plotRTLdata {
   FILE *outfile;
   unsigned char *outbytes;
};

int
pnmRTLLineFunc(linebuffer, arg)
    unsigned char *linebuffer;
    struct plotRTLdata *arg;
{
    int size;

    size = PlotRTLCompress(linebuffer, arg->outbytes, im_x * 3);
    fprintf(arg->outfile, "\033*b%dW", size);
    fwrite(arg->outbytes, size, 1, arg->outfile);
    return 0;
}

#endif

/*
 * ----------------------------------------------------------------------------
 *
 * Function for output of PNM line data to file fp
 *
 * ----------------------------------------------------------------------------
 */

int
pnmLineFunc(linebuffer, fp)
    unsigned char *linebuffer;
    FILE *fp;
{
    fwrite(linebuffer, im_x * 3, 1, fp);
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * pnmRenderRegion --
 *
 *      Antialiased rendering.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Writes output to file.
 *
 * ----------------------------------------------------------------------------
 */

void
pnmRenderRegion(scale, scale_over_2, normal, temp, func, arg)
     float scale;
     int scale_over_2;
     float normal;		/* normalizing factor */
     float *temp;		/* passed so we don't have to allocate it
				 * on every call.
				 */
     int (*func)();		/* Function to call per line of output */
     ClientData arg;		/* Arguments to function */
{
    int i, j;
    int jmax;
    int x, y;
    int dx, dy;
    int ds_over_2;
    pnmcolor *color;
    float r, g, b;
    unsigned char *linebuffer, *lineptr;

    jmax = MIN(y_pixels, im_yoffset + 1);
    ds_over_2 = scale_over_2 >> PlotPNMdownsample;

    linebuffer = mallocMagic(im_x * 3);

    /* x, y : pixel coords */

    if (ds_over_2 == 0)
    {
	for (j = 0; j < jmax; j++)
	{
	    lineptr = linebuffer;
	    y = scale * (y_pixels - 1 - j);
	    y >>= PlotPNMdownsample;
	    for (i = 0; i < im_x; i++)
	    {
		x = scale * i;
	        x >>= PlotPNMdownsample;
		color = rtile + x + y * ds_xsize;
		*lineptr++ = color->r;
		*lineptr++ = color->g;
		*lineptr++ = color->b;
	    }
	    (*func)(linebuffer, arg);
	}
    }
    else
    {
	/* When the scale is small enough, we have to resort to antialiasing */

	float lkval;
	int tidx;

	for (j = 0; j < jmax; j++) {
	    y = scale_over_2 + scale * (y_pixels - 1 - j);
	    y >>= PlotPNMdownsample;
	    lineptr = linebuffer;
	    for (i = 0; i < im_x; i++) {
		x = scale_over_2 + scale * i;
	        x >>= PlotPNMdownsample;
		for (dx = -ds_over_2; dx < ds_over_2; dx++)
		{
		    r = 0.0; g = 0.0; b = 0.0;
		    for (dy = -ds_over_2; dy < ds_over_2; dy++)
		    {
			if (dy + y >= ds_ysize) continue;
			/* grab rgb for (x + dx, y + dy) */
			color = rtile + (x + dx) + (y + dy) * ds_xsize;

			lkval = lk[lkstep[dy + ds_over_2]];
			r += (float)color->r * lkval;
			g += (float)color->g * lkval;
			b += (float)color->b * lkval;
		    }
		    tidx = 3 * (dx + ds_over_2);
		    temp[tidx++] = r;
		    temp[tidx++] = g;
		    temp[tidx] = b;
		}
		r = 0.0; g = 0.0; b = 0.0;
		for (dx = 0; dx < 2 * ds_over_2; dx++)
		{
		    tidx = 3 * dx;
		    lkval = lk[lkstep[dx]];
		    r += temp[tidx++] * lkval;
		    g += temp[tidx++] * lkval;
		    b += temp[tidx] * lkval;
		}
		r /= normal;
		g /= normal;
		b /= normal;
		*lineptr++ = (unsigned char)r;
		*lineptr++ = (unsigned char)g;
		*lineptr++ = (unsigned char)b;
	    }
	    (*func)(linebuffer, arg);
	}
    }
    freeMagic(linebuffer);
}



/*
 * ----------------------------------------------------------------------------
 *
 * pnmBBOX --
 *
 *      Callback for DBTreeSrTiles; compute bounding box of plot
 *
 * Results:
 *	Always return 0 to keep search going.
 *
 * Side effects:
 *	Modifies BBinit, updates bounding box "bb"
 *
 * ----------------------------------------------------------------------------
 */

int
pnmBBOX (tile,cxp)
     Tile *tile;
     TreeContext *cxp;
{
    Rect targetRect, sourceRect;
    SearchContext *scx = cxp->tc_scx;
    Rect *arg;
    TileType type;

    if (!IsSplit(tile))
	if ((type = TiGetType(tile)) == TT_SPACE)
	    return 0;

    /* grab rectangle from tile */
    TITORECT(tile, &targetRect);

    /* coordinate transform */
    GEOTRANSRECT(&scx->scx_trans, &targetRect, &sourceRect);

    /* Clip */
    arg = (Rect *)cxp->tc_filter->tf_arg;
    GEOCLIP(&sourceRect, arg);

    /* compute bbox */
    if (!BBinit)
	bb = sourceRect;
    else
    {
	bb.r_xbot = MIN(bb.r_xbot, sourceRect.r_xbot);
	bb.r_ybot = MIN(bb.r_ybot, sourceRect.r_ybot);
	bb.r_xtop = MAX(bb.r_xtop, sourceRect.r_xtop);
	bb.r_ytop = MAX(bb.r_ytop, sourceRect.r_ytop);
    }

    BBinit = 1;
    return 0;
}


/*
 * ----------------------------------------------------------------------------
 *
 * pnmTile --
 *
 *      Callback for DBTreeSrTiles; paints tiles in the current rtile buffer.
 *
 * Results:
 *	Return 0 to keep search going unless an error condition was
 *	encountered.
 *
 * Side effects:
 *	Modifies rtile array.
 *
 * ----------------------------------------------------------------------------
 */

int
pnmTile (tile, cxp)
     Tile *tile;
     TreeContext *cxp;
{
    SearchContext *scx = cxp->tc_scx;
    Rect targetRect, sourceRect, *clipRect;
    int type, j, x, y, dx, dy;
    pnmcolor *t;
    pnmcolor col;

    if ((type = (int)TiGetTypeExact(tile)) == TT_SPACE)
	return 0;

    /* undefined type; paint nothing */
    if (!IsSplit(tile))
	if (PaintStyles[type].wmask == 0) return 0;

    /* grab rectangle from tile */
    TITORECT(tile, &targetRect);

    /* coordinate transform */
    GEOTRANSRECT(&scx->scx_trans, &targetRect, &sourceRect);

    /* Clip */
    clipRect = (Rect *)cxp->tc_filter->tf_arg;

    /* Handle non-Manhattan geometry */
    if (IsSplit(tile))
    {
	TileType dinfo;
	int w, h, llx, lly, urx, ury;
	Rect scaledClip;

        type = (SplitSide(tile)) ? SplitRightType(tile) : SplitLeftType(tile);
	if (type == TT_SPACE) return 0;
	else if (PaintStyles[type].wmask == 0) return 0;

	llx = sourceRect.r_xbot - tile_xshift;
	lly = sourceRect.r_ybot - tile_yshift;
	llx >>= PlotPNMdownsample;
	lly >>= PlotPNMdownsample;
	dx = sourceRect.r_xtop - sourceRect.r_xbot;
	dy = sourceRect.r_ytop - sourceRect.r_ybot;
	dx >>= PlotPNMdownsample;
	dy >>= PlotPNMdownsample;
	urx = llx + dx;
	ury = lly + dy;

	col = PaintStyles[type].color;
	scaledClip = *clipRect;
	scaledClip.r_xbot -= tile_xshift;
	scaledClip.r_xtop -= tile_xshift;
	scaledClip.r_ybot -= tile_yshift;
	scaledClip.r_ytop -= tile_yshift;
	scaledClip.r_xbot >>= PlotPNMdownsample;
	scaledClip.r_xtop >>= PlotPNMdownsample;
	scaledClip.r_ybot >>= PlotPNMdownsample;
	scaledClip.r_ytop >>= PlotPNMdownsample;

	/* The following structures could be much better	*/
	/* written for considerable speedup. . .		*/

	dinfo = DBTransformDiagonal(TiGetTypeExact(tile), &scx->scx_trans);
	if (((dinfo & TT_SIDE) >> 1) != (dinfo & TT_DIRECTION))
	{
	    /* work top to bottom */
	    for (y = ury - 1; y >= lly; y--)
	    {
		if (y >= scaledClip.r_ytop) continue;
		else if (y < scaledClip.r_ybot) break;
		if (dinfo & TT_SIDE)	/* work right to left */
		{
		    for (x = urx - 1; x >= llx; x--)
		    {
			if (x >= scaledClip.r_xtop) continue;
			else if (x < scaledClip.r_xbot) break;
			if (((urx - x) * dy) > ((ury - y) * dx)) break;
			t = rtile + x + ds_xsize * y;
			*t = PNMColorBlend(t, &col);
		    }
		}
		else	/* work left to right */
		{
		    for (x = llx; x < urx; x++)
		    {
			if (x < scaledClip.r_xbot) continue;
			else if (x >= scaledClip.r_xtop) break;
			if (((x - llx) * dy)  > ((ury - y) * dx)) break;
			t = rtile + x + ds_xsize * y;
			*t = PNMColorBlend(t, &col);
		    }
		}
	    }
	}
	else	/* work bottom to top */
	{
	    for (y = lly; y < ury; y++)
	    {
		if (y < scaledClip.r_ybot) continue;
		else if (y >= scaledClip.r_ytop) break;
		if (dinfo & TT_SIDE)	/* work right to left */
		{
		    for (x = urx; x >= llx; x--)
		    {
			if (x >= scaledClip.r_xtop) continue;
			else if (x < scaledClip.r_xbot) break;
			if (((urx - x) * dy) > ((y - lly) * dx)) break;
			t = rtile + x + ds_xsize * y;
			*t = PNMColorBlend(t, &col);
		    }
		}
		else	/* work left to right */
		{
		    for (x = llx; x < urx; x++)
		    {
			if (x < scaledClip.r_xbot) continue;
			else if (x >= scaledClip.r_xtop) break;
			if (((x - llx) * dy) > ((y - lly) * dx)) break;
			t = rtile + x + ds_xsize * y;
			*t = PNMColorBlend(t, &col);
		    }
		}
	    }
	}
	return 0;
    }

    GEOCLIP(&sourceRect, clipRect);

    /* paint rectangle */

    x = sourceRect.r_xbot - tile_xshift;
    y = sourceRect.r_ybot - tile_yshift;

    /* stop the search on an error condition */
    /* (this should not happen---is guaranteed by GEOCLIP */
    if ((x < 0) || (y < 0) || (x >= tile_xsize) || (y >= tile_ysize))
	return 1;

    x >>= PlotPNMdownsample;
    y >>= PlotPNMdownsample;

    dx = sourceRect.r_xtop - sourceRect.r_xbot;
    dy = sourceRect.r_ytop - sourceRect.r_ybot;
    dx >>= PlotPNMdownsample;
    dy >>= PlotPNMdownsample;

    col = PaintStyles[type].color;
    t = rtile + x + ds_xsize * y;

    for (dy; dy > 0; dy--)
    {
	for (j = 0; j < dx; j++)
	{
	    *t = PNMColorBlend(t, &col);
	    t++;
	}
	t = t - dx + ds_xsize;
    }

    /* Continue search function */
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotPNM --
 *
 * 	This procedure generates a PNM file to describe an area of
 *	a layout.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotPNM(fileName, scx, layers, xMask, width)
    char *fileName;			/* Name of PNM file to write. */
    SearchContext *scx;			/* The use and area and transformation
					 * in this describe what to plot.
					 */
    TileTypeBitMask *layers;		/* Tells what layers to plot.  Only
					 * paint layers in this mask, and also
					 * expanded according to xMask, are
					 * plotted.  If L_LABELS is set, then
					 * labels on the layers are also
					 * plotted, if expanded according to
					 * xMask.  If L_CELL is set, then
					 * subcells that are unexpanded
					 * according to xMask are plotted as
					 * bounding boxes.
					 */
    int xMask;				/* An expansion mask, used to indicate
					 * the window whose expansion status
					 * will be used to determine
					 * visibility.  Zero means treat
					 * everything as expanded.
					 */
    int  width;		        	/* Indicates the width of the
					 * plot, in pixels.
					 */
{
    FILE *fp;
    Rect bbox;
    int bb_ysize, bb_xsize;
    int i, x, y, tile_ydelta;
    int save_ds, iter;
    int scale_over_2, ds_over_2;
    float *strip;
    float scale, invscale, scaledown, normal;

#ifdef VERSATEC
    struct plotRTLdata rtl_args;
    char command[200], tempFile[200];
#endif

    // Sanity check on PaintStyles---may be NULL if plot section
    // was missing from techfile.  If so, run default init/final
    // procedures, flag a warning, and continue.

    if (PaintStyles == NULL)
    {
	TxError ("Warning:  No plot section in techfile, using defaults.\n");
	PlotPNMTechInit();
	PlotPNMTechFinal();
    }

    if (width <= 0)
    {
	TxError ("PNM module given negative pixel width; cannot plot\n");
	return;
    }

    if (Init_Error)
    {
	TxError ("PNM module initialization had failed; cannot plot\n");
	return;
    }

    /* image:
     *    -----
     *   | xxx |
     *   | xxx |
     *   | xxx |
     *    -----
     *
     * Use -scale/2 to scale/2 magic coordinates for each output pixel.
     *
     */

    /* Rendering Tile:
     *
     *    0.. bbox size + 2 * scale_over_2.
     *
     * To sample, pixel (i,j) will be at:
     *     (scale_over_2 + scale*i, scale_over_2 + scale*j)
     *
     * Given an initial pixel position at (i,j), we sample from
     *     -scale_over_2 to scale_over_2
     */

    /* Compute bounding box size in lambda */
    BBinit = 0;
    DBTreeSrTiles(scx, layers, xMask, pnmBBOX, (ClientData)&scx->scx_area);

    /* Initial bounding box size */
    bb_ysize = bb.r_ytop - bb.r_ybot;
    bb_xsize = bb.r_xtop - bb.r_xbot;

    /* Determine value of "scale" from the total pixel width. */
    scale = (float)bb_xsize / (float)width;
    invscale = 1.0 / scale;

    scale = 1.0 / invscale;
    if ((scale > 2) || (invscale != ceil(invscale)))
	scale_over_2 = (int) ceil(scale / 2.0);
    else
	scale_over_2 = 0;

    /* bump search context by scale_over_2 pixels on each side */
    scx->scx_area.r_xbot = bb.r_xbot - scale_over_2;
    scx->scx_area.r_ybot = bb.r_ybot - scale_over_2;
    scx->scx_area.r_xtop = bb.r_xtop + scale_over_2;
    scx->scx_area.r_ytop = bb.r_ytop + scale_over_2;

    /* Recalculate bounding box with extended boundary */
    bb_ysize = bb.r_ytop - bb.r_ybot;
    bb_xsize = bb.r_xtop - bb.r_xbot;

    tile_xsize = bb_xsize + 2 * scale_over_2;

    /* check for empty region */
    if (BBinit == 0 || tile_xsize <= 0 || bb_ysize <= 0)
    {
	TxPrintf ("Empty region, no plot\n");
	return;
    }

    /*
     * Compute memory requirements; a single pixel line needs a tile
     * that has size "xsize" by "scale." To keep inter-tile overlap low,
     * we insist that a single tile must have at least 3*scale pixels in
     * it.
     */

    save_ds = PlotPNMdownsample;
    while ((PlotPNMmaxmem * 1024) <
		((3 * scale + 2 * scale_over_2) * PIXELSZ * tile_xsize)
		/ (1 << (PlotPNMdownsample * 2)))
	PlotPNMdownsample++;

    if (PlotPNMdownsample != save_ds)
    {
	TxPrintf ("%dX downsampling forced by memory size requirements.\n",
		PlotPNMdownsample);
	TxPrintf ("Current: %d KB; Required for non-downsampled image: %d KB\n",
		PlotPNMmaxmem, (int) (1023 + ((3 * scale + 2 * scale_over_2)
		* PIXELSZ * tile_xsize) / 1024) / (1 << (save_ds * 2)));
	TxPrintf ("Use \"plot parameter pnmmaxmem\" to increase allocation.\n");
    }

    /*
     * Compute the maximum y size for a tile.
     */

    tile_ysize = PlotPNMmaxmem * 1024 / (PIXELSZ * tile_xsize);
    tile_ydelta = (tile_ysize - scale_over_2 * 2);

    /* Determine the amount shifted in Y for each consecutively */
    /* computed region.						*/
    /* tile_ydelta is the amount of shift in magic units.	*/
    /* y_pixels is the amount of shift in PNM pixels.		*/
    /* tile_ydelta MUST EQUAL y_pixels * scale.  If not, then	*/
    /* we need to back-compute a better tile_ysize value.  	*/

    y_pixels = tile_ydelta / scale;
    if (y_pixels == 0) y_pixels = 1;
    if (y_pixels * scale != tile_ydelta)
    {
	tile_ydelta = scale * y_pixels;
	tile_ysize = tile_ydelta + (scale_over_2 * 2);
    }

    /* If there's enough memory allocation, tile_ysize bounds the whole plot */
    if (tile_ysize > (bb_ysize + 2 * scale_over_2))
    {
	tile_ysize = bb_ysize + 2 * scale_over_2;
	tile_ydelta = bb_ysize;
	y_pixels = tile_ydelta / scale;
    }

    ds_xsize = tile_xsize >> PlotPNMdownsample;
    ds_ysize = tile_ysize >> PlotPNMdownsample;
    ds_over_2 = scale_over_2 >> PlotPNMdownsample;

    rtile = (pnmcolor *) mallocMagic((ds_xsize * ds_ysize) * PIXELSZ);

    /* bump search context by scale_over_2 pixels on each side */
    scx->scx_area.r_ybot = scx->scx_area.r_ytop - tile_ysize;
    tile_yshift = scx->scx_area.r_ybot;
    tile_xshift = scx->scx_area.r_xbot;

    im_x = (int)(bb_xsize / scale);
    im_y = (int)(bb_ysize / scale);

#ifdef VERSATEC
    if (PlotPNMRTL)
    {
	if (fileName == NULL)
	{
	    int result;

	    sprintf(tempFile, "%s/magicPlotXXXXXX", PlotTempDirectory);
	    result = mkstemp(tempFile);
	    if (result == -1)
	    {
		TxError("Failed to create temporary filename for %s\n", tempFile);
		return;
	    }
	    fileName = tempFile;
	}
	rtl_args.outfile = PaOpen(fileName, "w", (char *)NULL, ".",
		(char *)NULL, (char **)NULL);
	if (rtl_args.outfile == NULL)
	{
	    TxError("Couldn't open file \"%s\" to write plot.\n", fileName);
	    return;
	}

	switch (PlotVersPlotType)
	{
	    case HPGL2:		/* Write HPGL2 header */
		/* Universal Command Language. */
		fprintf(rtl_args.outfile, "\033%%-12345X");
		/* Reset printer; set HPGL2 mode. */
		fprintf(rtl_args.outfile, "@PJL ENTER LANGUAGE=HPGL2\r\n");
		fprintf(rtl_args.outfile, "\033E\033%%0B");
		/* Declare name; disable auto-rotate */
		fprintf(rtl_args.outfile, "BP1,\"MAGIC\",5,1;");
		/* Enter RTL mode. */
		fprintf(rtl_args.outfile, "\033%%0A");
		/* Source mode opaque */
		fprintf(rtl_args.outfile, "\033*v1N");
		/* Drop through */

	    case HPRTL:		/* Write HPRTL header */
		/* Direct pixel mode, 8 bits/component */
		fwrite("\033*v6W\000\003\010\010\010\010", 11, 1, rtl_args.outfile);
		/* Image width in pixels. */
		fprintf(rtl_args.outfile, "\033*r%dS", im_x);
		/* Image height in pixels.*/
		fprintf(rtl_args.outfile, "\033*r%dT", im_y);
		/* No negative motion. */
		fprintf(rtl_args.outfile, "\033&a1N");
		/* Mode 2 row compression */
		/* But, we REALLY ought to have delta row compression here. . . */
		fprintf(rtl_args.outfile, "\033*b2M");
		/* Printer resolution in DPI */
		fprintf(rtl_args.outfile, "\033*t%dR", PlotVersDotsPerInch);
		/* Start raster data */
		fprintf(rtl_args.outfile, "\033*r%cA",
			(PlotVersPlotType == HPGL2) ? '1' : '0');
		break;
	}

	/* Reserve enough space for run-length encoding compression */
	rtl_args.outbytes = mallocMagic((im_x * 3) + ((im_x * 3) / 127) + 1);
    }
    else
#endif
    {
	/* open PNM file */

	fp = PaOpen (fileName, "w", ".pnm", ".", NULL, NULL);
	if (fp == NULL)
	{
	    TxError ("Could not open file `%s' for writing\n", fileName);
	    goto done;
	}

	fprintf (fp, "P6\n");
	fprintf (fp, "%d %d\n", im_x, im_y);
	fprintf (fp, "255\n");
    }

    im_yoffset = im_y - 1;

    TxPrintf ("PNM image dimensions: %d x %d\n", im_x, im_y);
#if 0
    TxPrintf ("Region size: %d x %d\n", tile_xsize, tile_ysize);
    TxPrintf ("Pixels per region: %d\n", y_pixels);
    TxPrintf ("Scale: %g\n", scale);
    TxPrintf ("Antialiasing overlap: %d\n", scale_over_2);
    if (PlotPNMdownsample > 0)
    {
	TxPrintf ("Downsampling: %d\n", PlotPNMdownsample);
	TxPrintf ("Downsampled region size: %d x %d\n", ds_xsize, ds_ysize);
    }
#endif

    strip = (float *) mallocMagic((unsigned) (ds_over_2 * 2 * 3 * sizeof(float)));
    lkstep = (int *) mallocMagic((unsigned) (ds_over_2 * 2 * sizeof(int)));

    scaledown = scale / (2 * (1 << PlotPNMdownsample));
    for (x = -ds_over_2; x < ds_over_2; x++)
    {
	lkstep[ds_over_2 + x] = ((float)ABS(x)) / scaledown * LANCZOS_KERNEL_SIZE;
	if (lkstep[ds_over_2 + x] >= LANCZOS_KERNEL_SIZE)
	    lkstep[ds_over_2 + x] = LANCZOS_KERNEL_SIZE - 1;
    }

    /* Compute the normalization factor (what to divide by after adding up the	*/
    /* weighted values of all pixels in the kernel area).			*/

    normal = 0.0;
    for (x = 0; x < 2 * ds_over_2; x++)
	for (y = 0; y < 2 * ds_over_2; y++)
	    normal += lk[lkstep[x]] * lk[lkstep[y]];

    iter = 0;
    while (im_yoffset >= 0)
    {
	/* If this is a slow rendering, then we'll announce	*/
	/* the progress every 20 steps.				*/

	if ((++iter) % 10 == 0)
	{
	    TxPrintf("%g%% done\n",
			100 * (float)(im_y - im_yoffset + 1) / (float)im_y);
	    TxFlushOut();
	}

	/* Clear tile memory with the background gray level */

	memset((void *)rtile, PlotPNMBG,
		(size_t)(ds_xsize * ds_ysize * PIXELSZ));

	if (SigInterruptPending)
	{
	    TxPrintf (" *** interrupted ***\n");
	    goto done;
	}
	/* Use the "UniqueTiles" function to avoid painting contacts twice */
	DBTreeSrUniqueTiles(scx, layers, xMask, pnmTile, (ClientData)&scx->scx_area);

	/* anti-aliased rendering */

#ifdef VERSATEC
	if (PlotPNMRTL)
	    pnmRenderRegion(scale, scale_over_2, normal, strip, pnmRTLLineFunc,
			(ClientData)(&rtl_args));
	else
#endif
	    pnmRenderRegion(scale, scale_over_2, normal, strip, pnmLineFunc,
			(ClientData)fp);

	/* advance to the next strip */
	im_yoffset -= y_pixels;			/* in output coords */
	tile_yshift -= tile_ydelta;		/* in magic coords */
	scx->scx_area.r_ybot -= tile_ydelta;
	scx->scx_area.r_ytop -= tile_ydelta;
    }

    /* TxPrintf ("Save to file `%s', scale = %f\n", fileName, scale);*/
#ifdef VERSATEC
    if (PlotPNMRTL)
    {
	switch (PlotVersPlotType)
	{
	    case HPRTL:
		PlotHPRTLTrailer(rtl_args.outfile);
		break;
	    case HPGL2:
		PlotHPGL2Trailer(rtl_args.outfile);
		break;
	}
	fflush(rtl_args.outfile);
	fclose(rtl_args.outfile);
	freeMagic(rtl_args.outbytes);

	/* Run spooler */
	sprintf(command, PlotVersCommand, PlotVersPrinter, fileName);
	if (system(command) != 0)
	{
	    TxError("Couldn't execute spooler command to print \"%s\"\n",
			fileName);
	}
    }
    else
#endif
	fclose (fp);

done:
    PlotPNMdownsample = save_ds;
    freeMagic(rtile);
    freeMagic(strip);
    freeMagic(lkstep);
    return;
}


/*
 * ----------------------------------------------------------------------------
 *
 * lanczos_kernel --
 *
 * 	Compute the value of the lanczos kernel at the given position.
 *
 *
 * Results:
 *	Returns kernel value at arg x.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

float lanczos_kernel(i, n)
    int i, n;
{
    double x;		/* position at which to evaluate the lanczos kernel */

    if (i == 0)
	return (float)1.0;
    else
	x = (double)i / (double)n;

    return (float)(sin(PI * x) / (PI * x)) * (sin(PI * 0.5 * x) / (PI * 0.5 * x));
}

/*
 * ----------------------------------------------------------------------------
 *	Color blending functions.
 *
 *	PNMColorBlend blends two colors denoted by R, G, B components (0-255).
 *	"c_have" is the color that is already present, and "c_put" is the
 *	color being overlaid.
 *
 *	PNMColorIndexAndBlend blends an R, G, B component color with a color
 *	indexed into a colormap table.
 *
 *	Both functions return an R, G, B component color.
 * ----------------------------------------------------------------------------
 */

pnmcolor
PNMColorBlend(c_have, c_put)
    pnmcolor *c_have, *c_put;
{
    pnmcolor loccolor;
    short r, g, b;

    /* "127" is half the background color (which should be derived) */

    r = (short)c_put->r - 127 + (short)c_have->r / 2;
    g = (short)c_put->g - 127 + (short)c_have->g / 2;
    b = (short)c_put->b - 127 + (short)c_have->b / 2;

    loccolor.r = (r < 0) ? 0 : (unsigned char)r;
    loccolor.g = (g < 0) ? 0 : (unsigned char)g;
    loccolor.b = (b < 0) ? 0 : (unsigned char)b;

    return loccolor;
}

pnmcolor
PNMColorIndexAndBlend(c_have, cidx)
    pnmcolor *c_have;
    int cidx;
{
    pnmcolor loccolor, *c_put;
    int ir, ig, ib;
    short r, g, b;

    if ((ncolors > 0) && (cidx < ncolors))
    {
	c_put = &PNMcolors[cidx];
	r = (short)c_put->r;
	g = (short)c_put->g;
	b = (short)c_put->b;
    }
    else
    {
	GrGetColor(cidx, &ir, &ig, &ib);
	r = (short)ir;
	g = (short)ig;
	b = (short)ib;
    }

    /* "127" is half the background color (which should be derived) */

    r += (short)c_have->r / 2 - 127;
    g += (short)c_have->g / 2 - 127;
    b += (short)c_have->b / 2 - 127;

    loccolor.r = (r < 0) ? 0 : (unsigned char)r;
    loccolor.g = (g < 0) ? 0 : (unsigned char)g;
    loccolor.b = (b < 0) ? 0 : (unsigned char)b;

    return loccolor;
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotPNMTechInit --
 *
 * 	Called when magic starts up.
 *
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Initializes lk[...] array with the lanczos kernel.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotPNMTechInit()
{
    int i;

    /* Clear out any old information */

    if (PaintStyles != NULL)
	freeMagic(PaintStyles);

    PaintStyles = (pstyle *)mallocMagic(DBNumUserLayers * sizeof(pstyle));
    for (i = 0; i < DBNumUserLayers; i++)
    {
	PaintStyles[i].wmask = 0;
	PaintStyles[i].color.r = 0xff;
	PaintStyles[i].color.g = 0xff;
	PaintStyles[i].color.b = 0xff;
    }

    Init_Error = 0;

     /* Initialize Lanczos kernel */
    for (i = 0; i <= 2 * LANCZOS_KERNEL_SIZE; i++)
	lk[i] = lanczos_kernel(i, LANCZOS_KERNEL_SIZE);
}



/*
 * ----------------------------------------------------------------------------
 *
 * PlotPNMTechLine --
 *
 * 	Parse a magic technology file line for the pnm plot style
 *
 * Results:
 *	Return TRUE always (no errors flagged).
 *
 * Side effects:
 *	Modifies paintstyles[] array.
 *
 * ----------------------------------------------------------------------------
 */

	/* ARGSUSED */
bool
PlotPNMTechLine(sectionName, argc, argv)
    char *sectionName;		/* Name of this section (unused). */
    int argc;			/* Number of arguments on line. */
    char *argv[];		/* Pointers to fields of line. */
{
    int i, j, k, style;
    void PlotPNMSetDefaults();	/* Forward declaration */

    if (!strncmp(argv[0], "color", 5))
	PlotLoadColormap((argc == 1) ? NULL : argv[1]);
    else if (!strncmp(argv[0], "dstyle", 6))
	PlotLoadStyles((argc == 1) ? NULL : argv[1]);
    else if (!strncmp(argv[0], "default", 7))
	PlotPNMSetDefaults();
    else if (!strncmp(argv[0], "draw", 4))
    {
	if (argc == 2)
	{
	    /* Use the default drawing style(s) for this type.	*/

	    i = (int)DBTechNameType(argv[1]);
	    if (i >= 0 && i < DBNumUserLayers)
	    {
		for (j = 0; j < DBWNumStyles; j++)
		{
		    style = j + TECHBEGINSTYLES;
		    if (TTMaskHasType(DBWStyleToTypes(j), i))
		    {
			PaintStyles[i].wmask |= GrStyleTable[style].mask;
			PaintStyles[i].color =
				PNMColorIndexAndBlend(&PaintStyles[i].color,
				GrStyleTable[style].color);
		    }
		}
	    }
	}
	else if (argc == 3)
	{
	    pstyle savestyle;
	    bool newcolor = FALSE;

	    /* Use the specified drawing style(s) instead of the */
	    /* display drawing styles (used to override crosses	 */
	    /* on contacts and such).				 */

	    k = (int)DBTechNameType(argv[1]);
	    if (k >= 0 && k < DBNumUserLayers)
	    {
	    	savestyle = PaintStyles[k];
		PaintStyles[k].wmask = 0;
		PaintStyles[k].color.r = 255;
		PaintStyles[k].color.g = 255;
		PaintStyles[k].color.b = 255;

		for (j = 2; j < argc; j++)
		{
		    /* Use the specified display style, or the internal one */
		    if (ndstyles > 0)
		    {
			for (i = 0; i < ndstyles; i++)
			{
			    if (!strcmp(Dstyles[i].name, argv[j]))
			    {
				PaintStyles[k].wmask |= Dstyles[i].wmask;
				PaintStyles[k].color =
					PNMColorBlend(&PaintStyles[k].color,
						&Dstyles[i].color);
				newcolor = TRUE;
			    }
			}
		    }
		    else
		    {
			i = (int)GrGetStyleFromName(argv[j]);
			if (i >= 0)
			{
			    PaintStyles[k].wmask |= GrStyleTable[i].mask;
			    PaintStyles[k].color =
					PNMColorIndexAndBlend(&PaintStyles[k].color,
					GrStyleTable[i].color);
			    newcolor = TRUE;
			}
			else
			    TxError("Unknown drawing style \"%s\" for PNM plot.\n",
					argv[j]);
		    }

		    /* In case of error, revert to the default style */

		    if (newcolor == FALSE)
			PaintStyles[k] = savestyle;
		}
	    }
	    else
		TxError("Unknown magic layer \"%s\" for PNM plot.\n", argv[1]);
	}
    }
    else if (!strncmp(argv[0], "map", 3))
    {
	k = (int)DBTechNameType(argv[1]);
	if (k >= 0 && k < DBNumUserLayers)
	{
	    for (j = 2; j < argc; j++)
	    {
		i = (int)DBTechNameType(argv[j]);
		if (i >= 0)
		{
		    PaintStyles[k].wmask |= PaintStyles[i].wmask;
		    PaintStyles[k].color = PNMColorBlend(&PaintStyles[k].color,
				&PaintStyles[i].color);
		}
	    }
	}
    }
    return TRUE;
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotPNMSetDefaults --
 *
 *	Generate default colors for the PNM plot style from existing
 *	graphics colors for the window.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotPNMSetDefaults()
{
    int i, j, style;

    for (i = TT_SPACE + 1; i < DBNumUserLayers; i++)
    {
	for (j = 0; j < DBWNumStyles; j++)
	{
	    style = j + TECHBEGINSTYLES;
	    if (TTMaskHasType(DBWStyleToTypes(j), i))
	    {
		PaintStyles[i].wmask |= GrStyleTable[style].mask;
		PaintStyles[i].color =
			PNMColorIndexAndBlend(&PaintStyles[i].color,
			GrStyleTable[style].color);
	    }
	}
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotPNMTechFinal --
 *
 *	Routine to be run at the end of reading the  "plot pnm" techfile
 *	section.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The "Dstyles" array is no longer needed and is free'd.
 *	The "PNMTypeTable" is malloc'd and entries filled.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotPNMTechFinal()
{
    int i;

    for (i = 0; i < ndstyles; i++)
	freeMagic(Dstyles[i].name);

    if (Dstyles != NULL)
    {
	freeMagic(Dstyles);
	Dstyles = NULL;
	ndstyles = 0;
    }

    if (PNMcolors != NULL)
    {
	freeMagic(PNMcolors);
	PNMcolors = NULL;
	ncolors = 0;
    }

    /* If no "draw" or "map" lines were declared in the technology	*/
    /* file, then we put together a default style where we use the	*/
    /* display dstyles for each layer.  We detect the condition as	*/
    /* having all wmask values 0 in the PaintStyles array.		*/

    for (i = TT_SPACE + 1; i < DBNumUserLayers; i++)
	if (PaintStyles[i].wmask != 0)
	    break;

    if (i < DBNumUserLayers)
	return;

    PlotPNMSetDefaults();
}


/*
 * ----------------------------------------------------------------------------
 *
 * PlotLoadStyles --
 *
 * 	Read in the plotting styles for rendering.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Initializes arrays for drawing/plotting.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotLoadStyles(filename)
    char *filename;
{
    FILE *inp;
    char fullName[256];
    char *buf;
    int newsec;
    int ord, mask, color, outline, nfill, stipple;
    int ir, ig, ib;
    char shortname;
    char longname[128];
    char fill[42];

    if (filename == NULL)
    {
	(void) sprintf(fullName, "%.100s.7bit.mraster_dstyle", DBWStyleType);
	buf = fullName;
    }
    else
    {
	buf = filename;
    }
    inp = PaOpen(buf, "r", (char *)NULL, ".", SysLibPath, (char **) NULL);
    if (inp == NULL)
    {
	TxError ("PNM plot: Could not open display style file\n");
	Init_Error = 1;
	return;
    }

    buf = fullName;	/* reuse this space for input */

    ndstyles = 0;
    Dstyles = (dstyle *)mallocMagic(DBWNumStyles * sizeof(dstyle));

    /* Read in the dstyle file */
    newsec = FALSE;
    while (fgets (buf, 256, inp))
    {
	if (buf[0] == '#') continue;
	if (StrIsWhite (buf, FALSE))
	{
	    newsec = TRUE;
	    continue;
	}
	else if (newsec)
	{
	    if (strncmp (buf, "display_styles", 14) != 0)
		goto dstyle_err;
	    newsec = FALSE;
 	}
	else
	{
	    if (sscanf (buf, "%d %d %d %d %40s %d %c %126s",
		    &ord, &mask, &color, &outline, fill, &stipple,
		    &shortname, longname) !=  8)
		goto dstyle_err;
	    if (ndstyles == DBWNumStyles)
		goto dstyle_err;
	    Dstyles[ndstyles].wmask = mask;
	    if ((ncolors > 0) && (color >=0) && (color < ncolors))
	    {
		Dstyles[ndstyles].color = PNMcolors[color];
	    }
	    else
	    {
		GrGetColor(color, &ir, &ig, &ib);
		Dstyles[ndstyles].color.r = (unsigned char)ir;
		Dstyles[ndstyles].color.g = (unsigned char)ig;
		Dstyles[ndstyles].color.b = (unsigned char)ib;
	    }
	    Dstyles[ndstyles].name = StrDup(NULL, longname);
	    ndstyles++;
	    if (ndstyles == DBWNumStyles) break;
	}
    }
    fclose (inp);
    return;

dstyle_err:
    Init_Error = 1;
    TxError ("Format error in display style file\n");
    fclose (inp);
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotLoadColormap --
 *
 * 	Read in the colormap for rendering.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Initializes arrays for drawing/plotting.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotLoadColormap(filename)
    char *filename;
{
    FILE *inp;
    char fullName[256];
    char *buf;
    int red, blue, green;

    /* read in color map */
    if (filename == NULL)
    {
	(void) sprintf(fullName, "%.100s.7bit.mraster.cmap", DBWStyleType);
	buf = fullName;
    }
    else
	buf = filename;

    inp = PaOpen(buf, "r", (char *) NULL, ".", SysLibPath, (char **) NULL);
    if (inp == NULL)
    {
	TxError("Couldn't open colormap file \"%s\"\n", buf);
	Init_Error = 1;
	return;
    }
    buf = fullName;	/* reuse this space for input */

    ncolors = 0;
    PNMcolors = (pnmcolor *)mallocMagic(128 * PIXELSZ);

    while (fgets (buf, 256, inp)) {
      if (buf[0] == '#') continue;
      if (StrIsWhite (buf, FALSE))
	continue;
      if (ncolors == 128) {
	goto color_err;
      }
      if (sscanf (buf, "%d %d %d", &red, &green, &blue) != 3) {
	goto color_err;
      }
      PNMcolors[ncolors].r = (unsigned char)red;
      PNMcolors[ncolors].g = (unsigned char)green;
      PNMcolors[ncolors].b = (unsigned char)blue;
      ncolors++;
    }
    fclose(inp);
    return;

color_err:
    Init_Error = 1;
    TxError ("Format error in colormap file\n");
    fclose (inp);
}