summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipsk/emspecial.c
blob: 38e450d050e486799fe062762367a4864b416859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
/* $Id$
 *   emspecial.c
 *   This routine handles the emTeX special commands.
 */
#include "dvips.h" /* The copyright notice in that file is included too!*/

#include <ctype.h>
#include <stdlib.h>
/*
 *   The external declarations:
 */
#include "protos.h"

#ifdef EMTEX
/* emtex specials, added by rjl */

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

static long emmax = 161;

/*
 *   We define these seek constants if they don't have their
 *   values already defined.
 */
#ifndef SEEK_SET
#define SEEK_SET (0)
#endif
#ifndef SEEK_END
#define SEEK_END (2)
#endif

struct empt {
   struct empt *next;
   shalfword point;
   integer x, y;
};

static struct empt **empoints = NULL;
boolean emused = FALSE;  /* true if em points used on this page */
integer emx, emy;

struct emunit {
   const char *unit;
   float factor;
};
struct emunit emtable[] = {
  {"pt",72.27},
  {"pc",72.27/12},
  {"in",1.0},
  {"bp",72.0},
  {"cm",2.54},
  {"mm",25.4},
  {"dd",72.27/(1238.0/1157)},
  {"cc",72.27/12/(1238.0/1157)},
  {"sp",72.27*65536},
  {0,0.0}
};

static void emgraph(char *filename, float emwidth, float emheight);

/* clear the empoints array if necessary */
void
emclear(void)
{
   int i;
   if (emused && empoints)
      for (i=0; i<emmax; i++)
         empoints[i] = 0;
   emused = FALSE;
}

/* put an empoint into the empoints array */
static struct empt *
emptput(shalfword point, integer x, integer y)
{
   struct empt *p;
   int start;

   emused = TRUE;
   start = point % emmax;
   p = empoints[start];
   while ( p ) {
      if ( p->point == point )
         break;
      p = p->next;
   }
   if (p == 0) {
      p = (struct empt *)mymalloc(sizeof(struct empt));
      p->next = empoints[start];
      empoints[start] = p;
   }
   p->point = point;
   p->x = x;
   p->y = y;
   return(p);
}

/* get an empoint from the empoints array */
static struct empt *
emptget(shalfword point)
{
   struct empt *p;
   int start;

   start = point % emmax;
   if (emused == TRUE) {
      p = empoints[start];
      while (p) {
	 if (p->point == point)
	    return p;
	 p = p->next;
      }
   }
   sprintf(errbuf,"!em: point %d not defined",point);
   specerror(errbuf);
   return(NULL); /* never returns due to error */
}


/* convert width into dpi units */
static float
emunits(float width, char *unit)
{
struct emunit *p;
	for (p=emtable; p->unit; p++) {
	   if (strcmp(p->unit,unit)==0)
		return( width * actualdpi / p->factor );
	}
	return (-1.0); /* invalid unit */
}

/* The main routine for \special{em:graph ...} called from dospecial.c */
/* the line cut parameter is not supported (and is ignored) */

void
emspecial(char *p)
{
float emwidth, emheight;
shalfword empoint1, empoint2;
struct empt *empoint;
char emunit[30];
char emstr[500];
char *emp;

        hvpos();
	for (emp = p+3; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
	if (strncmp(emp, "linewidth", 9) == 0) {
	   /* code for linewidth */
	   for (emp = emp+9; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
	   sscanf(emp, "%f%2s", &emwidth, emunit);
	   emwidth = emunits(emwidth,emunit);
	   if (emwidth!=-1.0) {
	      snprintf(emstr, sizeof(emstr), "%.1f setlinewidth", emwidth);
	      cmdout(emstr);
#ifdef DEBUG
   if (dd(D_SPECIAL))
      fprintf(stderr, "em special: Linewidth set to %.1f dots\n",
		emwidth);
#endif
	   } else {
	      sprintf(errbuf,"Unknown em: special width");
	      specerror(errbuf);
	   }
	}
        else if (strncmp(emp, "moveto", 6) == 0) {
#ifdef DEBUG
   if (dd(D_SPECIAL))
#ifdef SHORTINT
      fprintf(stderr, "em special: moveto %ld,%ld\n", hh, vv);
#else
      fprintf(stderr, "em special: moveto %d,%d\n", hh, vv);
#endif
#endif
           emx = hh;
           emy = vv;
        }
        else if (strncmp(emp, "lineto", 6) == 0) {
#ifdef DEBUG
   if (dd(D_SPECIAL))
#ifdef SHORTINT
      fprintf(stderr, "em special: lineto %ld,%ld\n", hh, vv);
#else
      fprintf(stderr, "em special: lineto %d,%d\n", hh, vv);
#endif
#endif
	   cmdout("np");
	   numout(emx);
	   numout(emy);
	   cmdout("a");
	   numout(hh);
	   numout(vv);
	   cmdout("li");
	   cmdout("st");
           emx = hh;
           emy = vv;
        }
	else if (strncmp(emp, "point", 5) == 0) {
           if (empoints == NULL) {
              empoints =
              (struct empt **)mymalloc((integer)emmax * sizeof(struct empt *));
              emused = TRUE;
              emclear();
           }
	   for (emp = emp+5; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
           empoint1 = (shalfword)atoi(emp);
           empoint = emptput(empoint1,hh,vv);
#ifdef DEBUG
   if (dd(D_SPECIAL))
#ifdef SHORTINT
      fprintf(stderr, "em special: Point %d is %ld,%ld\n",
#else
      fprintf(stderr, "em special: Point %d is %d,%d\n",
#endif
		empoint->point, empoint->x, empoint->y);
#endif
	}
	else if (strncmp(emp, "line", 4) == 0) {
	   for (emp = emp+4; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
           empoint1 = (shalfword)atoi(emp);
	   for (; *emp && isdigit((unsigned char)*emp); emp++); /* skip point 1 */
	   if ( *emp && strchr("hvp",*emp)!=0 )
	      emp++;  /* skip line cut */
	   for (; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
	   if ( *emp && (*emp==',') )
	      emp++; /*  skip comma separator */
	   for (; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
           empoint2 = (shalfword)atoi(emp);
	   for (; *emp && isdigit((unsigned char)*emp); emp++); /* skip point 2 */
	   if ( *emp && strchr("hvp",*emp)!=0 )
	      emp++;  /* skip line cut */
	   for (; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
	   if ( *emp && (*emp==',') )
	      emp++; /*  skip comma separator */
	   emwidth = -1.0;
	   emunit[0]='\0';
	   sscanf(emp, "%f%2s", &emwidth, emunit);
	   emwidth = emunits(emwidth,emunit);
#ifdef DEBUG
   if (dd(D_SPECIAL))
      fprintf(stderr, "em special: Line from point %d to point %d\n",
		empoint1, empoint2);
#endif
	   cmdout("np");
	   if (emwidth!=-1.0) {
#ifdef DEBUG
   if (dd(D_SPECIAL))
   fprintf(stderr,"em special: Linewidth temporarily set to %.1f dots\n",
		emwidth);
#endif
	   	strcpy(emstr,"currentlinewidth");
	   	cmdout(emstr);
	        snprintf(emstr, sizeof(emstr), "%.1f setlinewidth", emwidth);
	        cmdout(emstr);
	   }
           empoint = emptget(empoint1);
	   numout(empoint->x);
	   numout(empoint->y);
	   cmdout("a");
           empoint = emptget(empoint2);
	   numout(empoint->x);
	   numout(empoint->y);
	   cmdout("li");
	   cmdout("st");
	   if (emwidth!=-1.0) {
	   	strcpy(emstr,"setlinewidth");
	   	cmdout(emstr);
	   }
	}
	else if (strncmp(emp, "message", 7) == 0) {
           fprintf_str(stderr, "em message: %s\n", emp+7);
	}
	else if (strncmp(emp, "graph", 5) == 0) {
	   int i;
	   for (emp = emp+5; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */
	   for (i=0; *emp && !isspace((unsigned char)*emp) && !(*emp==','); emp++) {
	      if (i > (int)sizeof(emstr) - 2) {
                fprintf(stderr, "em:graph: special too long, truncating\n");
                break;
	      }
	      emstr[i++] = *emp; /* copy filename */
	   }
	   emstr[i] = '\0';
	   /* now get optional width and height */
	   emwidth = emheight = -1.0;	/* no dimension is <= 0 */
	   for (; *emp && ( isspace((unsigned char)*emp) || (*emp==',') ); emp++)
	    ;  /* skip blanks and comma */
	   if (*emp) {
	      sscanf(emp, "%f%2s", &emwidth, emunit); /* read width */
	      emwidth = emunits(emwidth,emunit); /* convert to pixels */
	      for (; *emp && (*emp=='.'||isdigit((unsigned char)*emp)||isalpha((unsigned char)*emp)); emp++)
	       ; /* skip width dimension */
	      for (; *emp && ( isspace((unsigned char)*emp) || (*emp==',') ); emp++)
	       ;  /* skip blanks and comma */
	      if (*emp) {
	         sscanf(emp, "%f%2s", &emheight, emunit); /* read height */
	         emheight = emunits(emheight,emunit)*vactualdpi/actualdpi;
	      }
	   }
	   if (emstr[0]) {
	      emgraph(emstr,emwidth,emheight);
	   }
	   else {
              fprintf(stderr, "em:graph: no file given\n");
	   }
	}
	else {
           sprintf(errbuf,
	      "Unknown em: command (%s) in \\special will be ignored", p);
           specerror(errbuf);
	}
	return;
   }


/* em:graph routines */

/* The graphics routines currently decode 3 types of IBM-PC based graphics   */
/* files: .pcx, .bmp, .msp. The information on the formats has occasionally  */
/* been sketchy, and subject to interpretation. I have attempted to implement*/
/* these routines to correctly decode the graphics file types mentioned.     */
/* The compressed .bmp file type has not been tested fully due to a lack of  */
/* test files.                                                               */
/*                                                                           */
/* The method of reading in the headers of the binary files is ungainly, but */
/* portable. Failure to use a byte by byte read and convert method will      */
/* result in portability problems. Specifically there is no requirement that */
/* elements of a C structure be contiguous, only that they have an ascending */
/* order of addresses.                                                       */
/*                                                                           */
/* The current implementations of the graphics format to postscript          */
/* conversion are not the most efficient possible. Specifically: color       */
/* formats are converted to RGB ratios prior to using a simple thresholding  */
/* method to determine if the postscript bit is to be set. The thresholding  */
/* method is compabible with that used in emtex's drivers.                   */
/*                                                                           */
/* Please send bug reports relating to the em:graph routines to:             */
/*                maurice@bruce.cs.monash.edu.au                             */
/*                                                                           */
/* My thanks to Russell Lang for his assistance with the implementation of   */
/* these routines in a manner compatible with dvips and for the optimization */
/* of some routines.                                                         */
/*                                                                           */
/*                                                   Maurice Castro          */
/*                                                   8 Oct 92                */

/* Routines to read binary numbers from IBM PC file types */
static integer
readinteger(FILE *f)
{
   integer i;
   int r;

   i = 0;
   for (r = 0; r < 4; r++)
   {
      i = i |  ( ((integer) fgetc(f)) << (8*r) );
   }
   return(i);
}

static halfword
readhalfword(FILE *f)
{
   halfword i;
   int r;

   i = 0;
   for (r = 0; r < 2; r++)
   {
     i = i |  ( ((halfword) fgetc(f)) << (8*r) );
   }
   return(i);
}

#define readquarterword(f) ((unsigned char)fgetc(f))
#define tobyte(x) ((x/8) + (x%8 ? 1 : 0))

/* These routines will decode PCX files produced by Microsoft
 * Windows Paint.  Other programs may not produce files which
 * will be successfully decoded, most notably version 1.xx of
 * PC Paint. */

/*
 *   Type declarations.  integer must be a 32-bit signed; shalfword must
 *   be a sixteen-bit signed; halfword must be a sixteen-bit unsigned;
 *   quarterword must be an eight-bit unsigned.
 */
typedef struct
{
	quarterword man;
	quarterword ver;
	quarterword enc;
	quarterword bitperpix;
	halfword xmin;
	halfword ymin;
	halfword xmax;
	halfword ymax;
	halfword hres;
	halfword vres;
	quarterword pal[48];
	quarterword reserved;
	quarterword colorplanes;
	halfword byteperline;
	halfword paltype;
	quarterword fill[58];
} PCXHEAD;

static int
PCXreadhead(FILE *pcxf, PCXHEAD *pcxh)
{
	pcxh->man = readquarterword(pcxf);
	pcxh->ver = readquarterword(pcxf);
	pcxh->enc = readquarterword(pcxf);
	pcxh->bitperpix = readquarterword(pcxf);
	pcxh->xmin = readhalfword(pcxf);
	pcxh->ymin = readhalfword(pcxf);
	pcxh->xmax = readhalfword(pcxf);
	pcxh->ymax = readhalfword(pcxf);
	pcxh->hres = readhalfword(pcxf);
	pcxh->vres = readhalfword(pcxf);
	fread(pcxh->pal, 1, 48, pcxf);
	pcxh->reserved = readquarterword(pcxf);
	pcxh->colorplanes = readquarterword(pcxf);
	pcxh->byteperline = readhalfword(pcxf);
	pcxh->paltype = readhalfword(pcxf);
	fread(pcxh->fill, 1, 58, pcxf);

	if (feof(pcxf))
		return(0);				/* fail */
	if (pcxh->man != 0x0a)
		return(0);				/* fail */
	if (pcxh->enc != 0x1)
		return(0);				/* fail */
	return(1);					/* success */
	}

static int
PCXreadline(FILE *pcxf,
	    unsigned char *pcxbuf,
	    unsigned int byteperline)
{
	int n;
	int c;
	int i;

	n = 0;
	memset(pcxbuf,0,byteperline);
	do {
		c = fgetc(pcxf);
		if ((c & 0xc0) == 0xc0) {
			i = c & 0x3f;
			c = fgetc(pcxf) & 0xff;
			while ((i--) && (n < byteperline)) pcxbuf[n++] = c;
		}
		else {
			pcxbuf[n++] = c & 0xff;
		}
	} while (n < byteperline);
	if (c==EOF) n=0;
	return(n);
}

static void
PCXgetpalette(FILE *pcxf, PCXHEAD *pcxh,
	      unsigned char *r,
	      unsigned char *g,
	      unsigned char *b)
{
	int i;

	/* clear palette */
	for (i=0; i < 256; i++) {
		r[i] = 0;
		g[i] = 0;
		b[i] = 0;
	}

	switch (pcxh->ver) {
		case 0:
			/* version 2.5 of PC Paint Brush */
			for (i=0; i < 16; i++) {
				r[i] = pcxh->pal[i*3];
				g[i] = pcxh->pal[i*3+1];
				b[i] = pcxh->pal[i*3+2];
			}
			break;
		case 2:
			if (pcxh->colorplanes != 1) {
			    /* version 2.8 of PC Paint Brush with valid Palette */
			for (i=0; i < 16; i++) {
				r[i] = pcxh->pal[i*3];
				g[i] = pcxh->pal[i*3+1];
				b[i] = pcxh->pal[i*3+2];
			    }
			}
			else {	/* not sure if this is correct - rjl */
				/* mono palette */
				r[0] = 0x00; g[0] = 0x00; b[0] = 0x00;
				r[1] = 0xff; g[1] = 0xff; b[1] = 0xff;
			}
			break;
		case 3:
			/* version 2.8 of PC Paint Brush with no valid Palette */
			/* either mono or default */

			if (pcxh->colorplanes != 1) {
				/* Color default palette - should be checked */
				r[0] = 0x00;		g[0] = 0x00;		b[0] = 0x00;
				r[1] = 0x80;		g[1] = 0x00; 		b[1] = 0x00;
				r[2] = 0x00; 		g[2] = 0x80; 		b[2] = 0x00;
				r[3] = 0x80;		g[3] = 0x80;	 	b[3] = 0x00;
				r[4] = 0x00;		g[4] = 0x00; 		b[4] = 0x80;
				r[5] = 0x80;		g[5] = 0x00; 		b[5] = 0x80;
				r[6] = 0x00;		g[6] = 0x80;	 	b[6] = 0x80;
				r[7] = 0x80;		g[7] = 0x80; 		b[7] = 0x80;
				r[8] = 0xc0;		g[8] = 0xc0;		b[8] = 0xc0;
				r[9] = 0xff;		g[9] = 0x00; 		b[9] = 0x00;
				r[10] = 0x00; 		g[10] = 0xff; 		b[10] = 0x00;
				r[11] = 0xff; 		g[11] = 0xff; 		b[11] = 0x00;
				r[12] = 0x00; 		g[12] = 0x00; 		b[12] = 0xff;
				r[13] = 0xff; 		g[13] = 0x00; 		b[13] = 0xff;
				r[14] = 0x00; 		g[14] = 0xff; 		b[14] = 0xff;
				r[15] = 0xff; 		g[15] = 0xff; 		b[15] = 0xff;
			}
			else {
				/* mono palette */
				r[0] = 0x00;		g[0] = 0x00;		b[0] = 0x00;
				r[1] = 0xff;		g[1] = 0xff; 		b[1] = 0xff;
			}
			break;
		case 5:
		default:
			/* version 3.0 of PC Paint Brush or Better */
			fseek(pcxf, -769L, SEEK_END);
			/* if signature byte is correct then read the palette */
			/* otherwise copy the existing palette */
			if (fgetc(pcxf) == 12) {
				for (i=0; i < 256; i++) {
					r[i] = fgetc(pcxf);
					g[i] = fgetc(pcxf);
					b[i] = fgetc(pcxf);
				}
			}
			else {
				for (i=0; i < 16; i++) {
					r[i] = pcxh->pal[i*3];
					g[i] = pcxh->pal[i*3+1];
					b[i] = pcxh->pal[i*3+2];
				}
			}
			break;
	}
}

static void
PCXshowpicture(FILE *pcxf, int wide, int high, int bytes,
	       int cp, int bp, unsigned char *r,
	       unsigned char *g, unsigned char *b)
{
	int x;
	int y;
	int c;
	unsigned char *rowa[4];				/* row array */
	unsigned char *row;
	int p;
	unsigned char *pshexa;
	int xdiv, xmod, xbit;
	int width;
	int bytewidth;

	bytewidth = tobyte(wide);
	width = bytewidth * 8;

	/* output the postscript image size preamble */
	cmdout("/picstr ");
	numout((integer)tobyte(wide));
	cmdout("string def");

	numout((integer)width);
	numout((integer)high);
	numout((integer)1);
	newline();

	cmdout("[");
	numout((integer)width);
	numout((integer)0);
	numout((integer)0);
	numout((integer)-high);
	numout((integer)0);
	numout((integer)0);
	cmdout("]");

	nlcmdout("{currentfile picstr readhexstring pop} image");

	/* allocate the postscript hex output array */
	pshexa = (unsigned char *) mymalloc((integer)bytewidth);

	/* make sure that you start at the right point in the file */
	fseek(pcxf, (long) sizeof(PCXHEAD), SEEK_SET);

	/* malloc the lines */
	row = (unsigned char *) mymalloc((integer)bytes * cp);
	for (c = 0; c < cp; c++)
		rowa[c] = row + bytes*c;

	for (y = 0; y < high; y++) {
		/* clear the postscript hex array */
		memset(pshexa,0xff,bytewidth);

		/* read in all the color planes for a row of pixels */
		PCXreadline(pcxf, rowa[0], bytes*cp);

		/* process each pixel */
		for (x = 0; x < wide; x++) {
			/* build up the pixel value from color plane entries */
			p = 0;
			xdiv = x>>3;
			xmod = 7 - (x&7);
			xbit = 1 << xmod;
			switch(bp) {
				case 1:
					for (c = 0; c < cp; c++) {
						row = rowa[c];
						p |= ( (unsigned)(row[xdiv] & xbit) >> xmod ) << c;
					}
					break;
				case 4:  /* untested - most programs use 1 bit/pixel, 4 planes */
					row = rowa[0]; /* assume single plane */
					p = ( x & 1 ? row[xdiv] : row[xdiv]>>4 ) & 0x0f;
					break;
				case 8:
					row = rowa[0]; /* assume single plane */
					p = (unsigned) (row[x]);
					break;
				default:
					fprintf(stderr, "em:graph: Unable to Decode this PCX file\n");
					return;
			}
			if ((r[p] < 0xff) || (g[p] < 0xff) || (b[p] < 0xff))
				pshexa[xdiv] &= (~xbit);
			else
				pshexa[xdiv] |= xbit;
		}
		newline();
		mhexout(pshexa,(long)bytewidth);
	}
	free(pshexa);
	free(rowa[0]);
}

static void
imagehead(char *filename, int wide, int high,
	  float emwidth, float emheight)
{
	char *fullname = NULL, *name;
	if (!quiet) {
#ifdef KPATHSEA
	    fullname = (char *)kpse_find_file (filename, pictpath, 0);
#endif
	    if (!fullname)
		name = filename;
	    else
		name = fullname;
	    if (strlen(name) + prettycolumn > STDOUTSIZE) {
		fprintf(stderr,"\n");
		prettycolumn = 0;
	    }
	    fprintf_str(stderr,"<%s",name);
	    fflush(stderr);
	    prettycolumn += 2+strlen(name);
	    if (fullname) free (fullname);
	}
	hvpos();
	nlcmdout("@beginspecial @setspecial");
	if (!disablecomments) {
		cmdout("%%BeginDocument: em:graph");
		cmdout(filename);
		newline();
	}
	/* set the image size */
	if (emwidth <= 0.0)  emwidth = (float)wide;
	if (emheight <= 0.0)  emheight = (float)high;
	floatout(emwidth*72/actualdpi);
	floatout(emheight*72/vactualdpi);
	newline();
	cmdout("scale");
#ifdef DEBUG
	if (dd(D_SPECIAL)) {
	  fprintf(stderr,
	    "\nem:graph: %s width  %d pixels scaled to %.1f pixels\n",
	    filename, wide, emwidth);
	  fprintf_str(stderr,
	    "em:graph: %s height %d pixels scaled to %.1f pixels\n",
	    filename, high, emheight);
   	}
#endif
}

static void
imagetail(void)
{
	if (!disablecomments) {
	    fprintf(bitfile, "\n%%%%EndDocument\n");
	    linepos = 0;
	}
	nlcmdout("@endspecial");
	if (!quiet) {
	    fprintf(stderr,">");
	    fflush(stderr);
	}
}

static void
pcxgraph(FILE *pcxf, char *filename,
	 float emwidth, float  emheight /* dimension in pixels */ )
{
	PCXHEAD pcxh;
	unsigned char red[256];
	unsigned char green[256];
	unsigned char blue[256];
	int wide;
	int high;
	int bytes;
	int cp;
	int bpp;

	if (!PCXreadhead(pcxf, &pcxh)) {
		sprintf(errbuf,"em:graph: Unable to Read Valid PCX Header");
		specerror(errbuf);
	}
	PCXgetpalette(pcxf, &pcxh, red, green, blue);

	/* picture size calculation */
	wide = (pcxh.xmax - pcxh.xmin) + 1;
	high = (pcxh.ymax - pcxh.ymin) + 1;
	bytes = pcxh.byteperline;
	cp = pcxh.colorplanes;
	bpp = pcxh.bitperpix;

	imagehead(filename,wide,high,emwidth,emheight);
	PCXshowpicture(pcxf, wide, high, bytes, cp, bpp, red, green, blue);
	imagetail();
}

/* Microsoft Paint routines */
struct wpnt_1 {
	quarterword id[4];
	halfword width;
	halfword high;
	halfword x_asp;
	halfword y_asp;
	halfword x_asp_prn;
	halfword y_asp_prn;
	halfword width_prn;
	halfword high_prn;
	integer chk_sum;
	halfword chk_head;
};

#define WPAINT_1 1
#define WPAINT_2 2

static void
MSP_2_ps(FILE *f, int wide, int high)
{
	char *line;
	char *l;
	int i;
	int j;
	unsigned char a, b, c;
	int d;
	int width;
	halfword *linelen;

	/* an undocumented format - based on a type of run length encoding    */
	/* the format is made up of a list of line lengths, followed by a set */
	/* of lines. Each line is made up of 2 types of entries:              */
	/* 	1) A 3 term entry (0 a b): the byte b is repeated a times     */
	/*	2) A variable length entry (a xxxx....xxxx): a bytes are read */
	/*	   from the file.                                             */
	/* These entries are combined to build up a line                      */

	width = tobyte(wide)*8;

	/* output the postscript image size preamble */
	cmdout("/picstr");
	numout((integer)tobyte(wide));
	cmdout("string def");

	numout((integer)width);
	numout((integer)high);
	numout((integer)1);

	cmdout("[");
	numout((integer)width);
	numout((integer)0);
	numout((integer)0);
	numout((integer)-high);
	numout((integer)0);
	numout((integer)0);
	cmdout("]");

	nlcmdout("{currentfile picstr readhexstring pop} image");

	fseek(f, 32, SEEK_SET);

	/* read in the table of line lengths */
	linelen = (halfword *) mymalloc((integer)sizeof(halfword) * high);
	for (i = 0; i < high; i++) {
		linelen[i] = readhalfword(f);
		if (feof(f))
			return;
	}

	line = (char *) mymalloc((integer)tobyte(wide));
	for (i = 0; i < high; i++) {
		memset(line, 0xff, tobyte(wide));
		l = line;
		if (linelen[i] != 0) {
			d = linelen[i];
			while (d) {
				a = fgetc(f);
				d--;
				if (a == 0) {
					b = fgetc(f);
					c = fgetc(f);
					d -= 2;
					for (j = 0; j < b; j++)
						*l++ = c;
				}
				else {
					for (j = 0; j < a; j++)
						*l++ = fgetc(f);
					d -= j;
				}
			}
		}
		newline();
		mhexout((unsigned char *) line,(long)tobyte(wide));
	}
	free(linelen);
	free(line);
}

static void
MSP_1_ps(FILE *f, int wide, int high)
{
	char *line;
	int i;
	int width;

	width = tobyte(wide)*8;
	/* an partly documented format - see The PC Sourcebook                */
	/* the format is made up of a simple bitmap.                          */

	/* output the postscript image size preamble */
	cmdout("/picstr");
	numout((integer)tobyte(wide));
	cmdout("string def");

	numout((integer)width);
	numout((integer)high);
	numout((integer)1);

	cmdout("[");
	numout((integer)width);
	numout((integer)0);
	numout((integer)0);
	numout((integer)-high);
	numout((integer)0);
	numout((integer)0);
	cmdout("]");

	nlcmdout("{currentfile picstr readhexstring pop} image");

	fseek(f, 32, SEEK_SET);

	line = (char *) mymalloc((integer)tobyte(wide));
	for (i = 0; i < high; i++) {
		fread(line, 1, tobyte(wide), f);
		newline();
		mhexout((unsigned char *) line,(long)tobyte(wide));
	}
	free(line);
}


static void
mspgraph(FILE *f, char *filename, float emwidth, float emheight)
{
	struct wpnt_1 head;
	int paint_type = 0;

        /* read the header of the file and figure out what it is */
	fread(head.id, 1, 4, f);
	head.width = readhalfword(f);
	head.high = readhalfword(f);
	head.x_asp = readhalfword(f);
	head.y_asp = readhalfword(f);
	head.x_asp_prn = readhalfword(f);
	head.y_asp_prn = readhalfword(f);
	head.width_prn = readhalfword(f);
	head.high_prn = readhalfword(f);
	head.chk_sum = readinteger(f);
	head.chk_head = readhalfword(f);

	if (feof(f)) {
		fprintf(stderr, "em:graph: Unable to Read Valid MSP Header\n");
		return;
	}

        /* check the id bytes */
	if (!memcmp(head.id, "DanM", 4))
        	paint_type = WPAINT_1;
	if (!memcmp(head.id, "LinS", 4))
		paint_type = WPAINT_2;


	imagehead(filename,head.width,head.high,emwidth,emheight);
	switch (paint_type) {
		case WPAINT_1:
                	MSP_1_ps(f, head.width, head.high);
			break;
                case WPAINT_2:
                	MSP_2_ps(f, head.width, head.high);
			break;
		default:
			sprintf(errbuf, "em:graph: Unknown MSP File Type");
			specerror(errbuf);
	}
	imagetail();
}

/* ------------------------------------------------------------------------ */
/* .BMP file structures */
struct rgbquad {
	char blue;
	char green;
	char red;
	char res;
};

struct bitmapinfoheader {
	integer size;
	integer width;
	integer height;
	halfword planes;			/* must be set to 1 */
	halfword bitcount;			/* 1, 4, 8 or 24 */
	integer compression;
	integer sizeimage;
	integer xpelspermeter;
	integer ypelspermeter;
	integer clrused;
	integer clrimportant;
};

#ifdef WIN32
#undef RGB
#endif

/* constants for the compression field */
#define RGB 0L
#define RLE8 1L
#define RLE4 2L

struct bitmapfileheader {
	char type[2];
	integer size;
	halfword reserved1;
	halfword reserved2;
	integer offbits;
};

static void
rgbread(FILE *f, int w, int b, char *s)
{
	int i;

	/* set the line to white */
	memset(s, 0xff, ((w*b)/8)+1);

	/* read in all the full bytes */
	for (i = 0; i < (w * b) / 8; i++)
		*s++ = fgetc(f);

	/* read in a partly filled byte */
	if ((w * b) % 8) {
		i++;
		*s++ = fgetc(f);
	}

	/* check that we are on a 32 bit boundary; otherwise align */
	while (i % 4 != 0) {
		fgetc(f);
		i++;
	}
}

unsigned rle_dx = 0;	/* delta command horizontal offset */
unsigned rle_dy = 0;	/* delta command vertical offset */

/* checked against output from Borland Resource Workshop */
static void
rle4read(FILE *f, int w, int b, char *s)
{
	int i;
	int limit;
	int ch;
	unsigned cnt;
	int hi;

	limit = (w*b)/8;
	i = 0;
	hi = TRUE;
	/* set the line to white */
	memset(s, 0xff, limit+1);

	if (rle_dy) {
	    rle_dy--;
	    return;
	}

	if (rle_dx) {
	    for (; rle_dx>1; rle_dx-=2) {
		s++;
		i++;
	    }
	    if (rle_dx)
	    	hi = FALSE;
	}

	while (i<=limit) {
	    cnt = fgetc(f);
	    ch  = fgetc(f);
	    if (cnt == 0) { /* special operation */
		switch(ch) {
		    case 0:	/* EOL */
			return;	/* this is our way out */
		    case 1:	/* End of Bitmap */
		    	return;
		    case 2:	/* Delta */  /* untested */
			rle_dx = fgetc(f) + i*2 + (hi ? 1 : 0);
			rle_dy = fgetc(f);
			return;
		    default:	/* next cnt bytes are absolute */
		    	/* must be aligned on word boundary */
			if (!hi)
				fprintf(stderr,"em:graph: RLE4 absolute is not byte aligned\n");
			for (cnt = ch; cnt>0 && i<=limit; cnt-=2) {
		    	    i++;
			    *s++ = fgetc(f);
			}
			if (ch % 4)	/* word align file */
			    fgetc(f);
  		}
	    }
	    else {   /* cnt is repeat count */
		if (!hi) {   /* we are about to place the low 4 bits */
		    ch = ((ch>>4)&0x0f) | ((ch<<4)&0xf0); /* swap order */
		    i++;
		    *s = (*s & 0xf0) | (ch & 0x0f);
                    s++;
		    hi = TRUE;
		    cnt--;
		}
		/* we are about to place the high 4 bits */
		for (; cnt>1 && i<=limit; cnt-=2) { /* place the whole bytes */
		    i++;
		    *s++ = ch;
	        }
		if (cnt) { /* place the partial byte */
		    *s = (*s & 0x0f) | (ch & 0xf0);
		    hi = FALSE;
		}
	    }
  	}
}

/* untested */
static void
rle8read(FILE *f, int w, int b, char *s)
{
	int i;
	int limit;
	int ch;
	unsigned cnt;

	limit = (w*b)/8;
	i = 0;
	/* set the line to white */
	memset(s, 0xff, limit+1);

	if (rle_dy) {
	    rle_dy--;
	    return;
	}

	if (rle_dx) {
	    for (; rle_dx > 0; rle_dx--) {
		s++;
		i++;
	    }
	}

	while (i<=limit) {
	    cnt = fgetc(f);
	    ch  = fgetc(f);
	    if (cnt == 0) { /* special operation */
		switch(ch) {
		    case 0:	/* EOL */
			return;	/* this is our way out */
		    case 1:	/* End of Bitmap */
			return;
		    case 2:	/* Delta */  /* untested */
			rle_dx = fgetc(f) + i;
			rle_dy = fgetc(f);
			return;
		    default:	/* next cnt bytes are absolute */
			for (cnt = ch; cnt>0 && i<=limit; cnt--) {
		    	    i++;
			    *s++ = fgetc(f);
		        }
			if (ch % 2)	/* word align file */
			    fgetc(f);
		}
	    }
	    else { /* cnt is repeat count */
		for (; cnt>0 && i<=limit; cnt--) {
		    i++;
		    *s++ = ch;
		}
	    }
	}
}

static void
bmpgraph(FILE *f, char *filename, float emwidth, float emheight)
{
	struct bitmapfileheader bmfh;
	struct bitmapinfoheader bmih;

	#define ISBLACKSIZ 256
	unsigned char isblack[ISBLACKSIZ];
	unsigned char rr;
	unsigned char gg;
	unsigned char bb;
	unsigned char c = 0;

	char *line;
	char *pshexa;

	int clrtablesize;
	int i;
	int j;

	unsigned char omask;
	int oroll;

	unsigned char emask = 0;
	integer ewidth = 0;
	int isOS2;

        /* read the header of the file */
	fread(bmfh.type, 1, 2, f);
	bmfh.size = readinteger(f);
	bmfh.reserved1 = readhalfword(f);
	bmfh.reserved2 = readhalfword(f);
	bmfh.offbits = readinteger(f);
	if (feof(f)) {
		sprintf(errbuf, "em:graph: Unable to Read Valid BMP Header\n");
		specerror(errbuf);
		return;
	}

	bmih.size = readinteger(f);
	if (bmih.size == 12) { /* OS2 bitmap */
		isOS2 = TRUE;
		bmih.width = readhalfword(f);
		bmih.height = readhalfword(f);
		bmih.planes = readhalfword(f);
		bmih.bitcount = readhalfword(f);
		/* the following don't exist in OS2 format so fill with 0's */
		bmih.compression = RGB;
		bmih.sizeimage = 0;
		bmih.xpelspermeter = 0;
		bmih.ypelspermeter = 0;
		bmih.clrused = 0;
		bmih.clrimportant = 0;
	}
	else { /* Windows bitmap */
		isOS2 = FALSE;
		bmih.width = readinteger(f);
		bmih.height = readinteger(f);
		bmih.planes = readhalfword(f);
		bmih.bitcount = readhalfword(f);
		bmih.compression = readinteger(f);
		bmih.sizeimage = readinteger(f);
		bmih.xpelspermeter = readinteger(f);
		bmih.ypelspermeter = readinteger(f);
		bmih.clrused = readinteger(f);
		bmih.clrimportant = readinteger(f);
	}

	if (feof(f)) {
		sprintf(errbuf, "em:graph: Unable to Read Valid BMP Info");
		specerror(errbuf);
		return;
	}

	if (memcmp(bmfh.type, "BM", 2)) {
		sprintf(errbuf, "em:graph: Unknown BMP File Type");
		specerror(errbuf);
		return;
	}

	if ((bmih.compression == RLE4) && (bmih.bitcount != 4)) {
		sprintf(errbuf, "em:graph: Can't do BMP RLE4 with %d bits per pixel",
			bmih.bitcount);
		specerror(errbuf);
		return;
	}

	if ((bmih.compression == RLE8) && (bmih.bitcount != 8)) {
		sprintf(errbuf, "em:graph: Can't do BMP RLE8 with %d bits per pixel\n",
			bmih.bitcount);
		specerror(errbuf);
		return;
	}

	imagehead(filename,(int)bmih.width,(int)bmih.height,emwidth,emheight);

	/* determine the size of the color table to read */
	clrtablesize = 0;
	if (bmih.clrused == 0) {
		switch (bmih.bitcount) {
			case 1:
				clrtablesize = 2;
				break;
			case 4:
				clrtablesize = 16;
				break;
			case 8:
				clrtablesize = 256;
				break;
			case 24:
				break;
		}
	}
        else
		clrtablesize = bmih.clrused;

	if (clrtablesize > ISBLACKSIZ) {
		/* This is wrong, since we won't read the whole file below.
		   But we can't give correct output without more work,
		   and it's unlikely these specials are still in use.  */
		sprintf(errbuf,
		   "em color table size (%d) larger than %d; output incorrect",
		        clrtablesize, ISBLACKSIZ);
   		specerror(errbuf);
		clrtablesize = ISBLACKSIZ;
	}
	
	/* read in the color table */
	for (i = 0; i < clrtablesize; i++) {
		bb = fgetc(f);
		gg = fgetc(f);
		rr = fgetc(f);
		isblack[i] = (rr < 0xff) || (gg < 0xff) || (bb < 0xff);
		if (!isOS2)
			(void) fgetc(f);
	}

	line = (char *) mymalloc((integer)((bmih.width * bmih.bitcount) / 8) + 1);
	pshexa = (char *) mymalloc((integer)tobyte(bmih.width));

	/* output the postscript image size preamble */
	cmdout("/picstr");
	numout((integer)tobyte(bmih.width));
	cmdout("string def");

	numout((integer)bmih.width);
	numout((integer)bmih.height);
	numout((integer)1);

	cmdout("[");
	numout((integer)bmih.width);
	numout((integer)0);
	numout((integer)0);
	numout((integer)bmih.height);
	numout((integer)0);
	numout((integer)bmih.height);
	cmdout("]");

	nlcmdout("{currentfile picstr readhexstring pop} image");

	if (bmih.bitcount == 1) {
		if (bmih.width%8)
			emask = (1<<(8-(bmih.width%8)))-1;	/* mask for edge of bitmap */
		else
			emask = 0;
		ewidth = tobyte(bmih.width);
	}

	/* read in all the lines of the file */
	for (i = 0; i < bmih.height; i++) {
		memset(pshexa,0xff,tobyte(bmih.width));
		switch (bmih.compression) {
		    case RGB:
			rgbread(f, (int) bmih.width, (int) bmih.bitcount, line);
			break;
		    case RLE4:
			rle4read(f, (int) bmih.width, (int) bmih.bitcount, line);
			break;
		    case RLE8:
			rle8read(f, (int) bmih.width, (int) bmih.bitcount, line);
			break;
		    default:
			sprintf(errbuf,"em:graph: Unknown BMP compression\n");
			specerror(errbuf);
			return;
		}

		omask = 0x80;
		oroll = 7;

		if (bmih.bitcount == 1) {
		    if (isblack[0])
			for (j = 0; j < ewidth; j++)
				pshexa[j] = line[j];
		    else
			for (j = 0; j < ewidth; j++)
				pshexa[j] = ~line[j];
		    pshexa[ewidth-1] |= emask;
		}
		else {
		    for (j = 0; j < bmih.width; j++) {
			switch (bmih.bitcount) {
				case 4:
					c = line[j>>1];
					if (!(j&1))
						c >>= 4;
					c = isblack[ c & 0x0f ];
					break;
				case 8:
					c = isblack[ (int)(line[j]) ];
					break;
				case 24:
					rr = line[j*3];
					gg = line[j*3+1];
					bb = line[j*3+2];
					c = (rr < 0xff) || (gg < 0xff) || (bb < 0xff);
					break;
			}
			if (c)
			    pshexa[j/8] &= ~omask;
			else
			    pshexa[j/8] |= omask;
			oroll--;
			omask >>= 1;
			if (oroll < 0) {
			    omask = 0x80;
			    oroll = 7;
			}
		    }
		}
		newline();
		mhexout((unsigned char *) pshexa,(long)tobyte(bmih.width));
	}
	imagetail();
	free(pshexa);
	free(line);
}
/* ------------------------------------------------------------------------ */

#define PCX 0
#define MSP 1
#define BMP 2
const char *extarr[]=
{ ".pcx", ".msp", ".bmp", NULL };

static void
emgraph(char *filename, float emwidth, float emheight)
{
	char fname[500];
	int filetype;
	FILE *f;
	char *env;
	char id[4];
	int i;

	strcpy(fname, filename);

	/* find the file */
	f = search(figpath, fname, READBIN);
	if (f == (FILE *)NULL) {
   	    if ( (env = getenv("DVIDRVGRAPH")) != NULL )
#ifdef KPATHSEA
		f = search((kpse_file_format_type)env,filename,READBIN);
#else
		f = search(env,filename,READBIN);
#endif
	}
	/* if still haven't found it try adding extensions */
	if (f == (FILE *)NULL) {
	    i = 0;
	    while (extarr[i] != NULL) {
		strcpy(fname, filename);
		strcat(fname, extarr[i]);
		f = search(figpath, fname, READBIN);
		if (f == (FILE *)NULL) {
	    	    if ( (env = getenv("DVIDRVGRAPH")) != NULL )
#ifdef KPATHSEA
			f = search((kpse_file_format_type)env,filename,READBIN);
#else
			f = search(env,filename,READBIN);
#endif
		}
		if (f != (FILE *)NULL)
		    break;
		i++;
	    }
	}

	filetype = -1;
	if (f != (FILE *)NULL) {
	    for (i=0; i<4; i++) {
		id[i] = readquarterword(f);
	    }
	    if ( (id[0] == 0x0a) && (id[2] == 0x01) )
		filetype = PCX;
	    if (!memcmp(id, "DanM", 4))
		filetype = MSP;
	    if (!memcmp(id, "LinS", 4))
		filetype = MSP;
	    if (!memcmp(id, "BM", 2))
		filetype = BMP;
	    fseek(f, 0L, SEEK_SET);
	}

	switch (filetype) {
		case PCX:
			pcxgraph(f, fname, emwidth, emheight);
			break;
		case MSP:
			mspgraph(f, fname, emwidth, emheight);
			break;
		case BMP:
			bmpgraph(f, fname, emwidth, emheight);
			break;
		default:
			snprintf(fname, sizeof(fname),
                                 "em:graph: %s: File not found", filename);
			error(fname);
	}
	if (f != (FILE *)NULL)
	    close_file(f);
}

#else
void
emspecial(char *p)
{
	sprintf(errbuf,"emTeX specials not compiled in this version");
	specerror(errbuf);
}
#endif /* EMTEX */