1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: meta2bmp.c,v 1.4 2019/11/18 22:12:32 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Program to convert meta-files to BMP 8-bit color-mapped format |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "rtprocess.h" |
11 |
#include "meta.h" |
12 |
#include "plot.h" |
13 |
#include "rast.h" |
14 |
#include "bmpfile.h" |
15 |
#include "targa.h" |
16 |
|
17 |
#define MAXALLOC 100000 |
18 |
#define DXSIZE 400 /* default x resolution */ |
19 |
#define DYSIZE 400 /* default y resolution */ |
20 |
#define XCOM "pexpand +vOCImsp -DP %s | psort +y" |
21 |
|
22 |
|
23 |
char *progname; |
24 |
|
25 |
SCANBLOCK outblock; |
26 |
|
27 |
int dxsiz = DXSIZE, dysiz = DYSIZE; |
28 |
|
29 |
int maxalloc = MAXALLOC; |
30 |
|
31 |
int ydown = 0; |
32 |
|
33 |
static char outname[64]; |
34 |
static char *outtack = NULL; |
35 |
|
36 |
static BMPWriter *bmpw = NULL; |
37 |
|
38 |
static int lineno = 0; |
39 |
|
40 |
static short condonly = FALSE, |
41 |
conditioned = FALSE; |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
char * |
47 |
findtack(char *s) /* find place to tack on suffix */ |
48 |
{ |
49 |
while (*s && *s != '.') |
50 |
s++; |
51 |
return(s); |
52 |
} |
53 |
|
54 |
|
55 |
int |
56 |
main( |
57 |
int argc, |
58 |
char **argv |
59 |
) |
60 |
{ |
61 |
FILE *fp; |
62 |
char comargs[200], command[300]; |
63 |
|
64 |
progname = *argv++; |
65 |
argc--; |
66 |
|
67 |
condonly = FALSE; |
68 |
conditioned = FALSE; |
69 |
|
70 |
while (argc && **argv == '-') { |
71 |
switch (*(*argv+1)) { |
72 |
case 'c': |
73 |
condonly = TRUE; |
74 |
break; |
75 |
case 'r': |
76 |
conditioned = TRUE; |
77 |
break; |
78 |
case 'm': |
79 |
minwidth = atoi(*++argv); |
80 |
argc--; |
81 |
break; |
82 |
case 'x': |
83 |
dxsiz = atoi(*++argv); |
84 |
argc--; |
85 |
break; |
86 |
case 'y': |
87 |
dysiz = atoi(*++argv); |
88 |
argc--; |
89 |
break; |
90 |
case 'o': |
91 |
strcpy(outname, *++argv); |
92 |
outtack = findtack(outname); |
93 |
argc--; |
94 |
break; |
95 |
default: |
96 |
error(WARNING, "unknown option"); |
97 |
break; |
98 |
} |
99 |
argv++; |
100 |
argc--; |
101 |
} |
102 |
|
103 |
if (conditioned) { |
104 |
if (argc) |
105 |
while (argc) { |
106 |
fp = efopen(*argv, "r"); |
107 |
plot(fp); |
108 |
fclose(fp); |
109 |
argv++; |
110 |
argc--; |
111 |
} |
112 |
else |
113 |
plot(stdin); |
114 |
if (lineno) |
115 |
nextpage(); |
116 |
} else { |
117 |
comargs[0] = '\0'; |
118 |
while (argc) { |
119 |
strcat(comargs, " "); |
120 |
strcat(comargs, *argv); |
121 |
argv++; |
122 |
argc--; |
123 |
} |
124 |
sprintf(command, XCOM, comargs); |
125 |
if (condonly) |
126 |
return(system(command)); |
127 |
else { |
128 |
if ((fp = popen(command, "r")) == NULL) |
129 |
error(SYSTEM, "cannot execute input filter"); |
130 |
plot(fp); |
131 |
pclose(fp); |
132 |
if (lineno) |
133 |
nextpage(); |
134 |
} |
135 |
} |
136 |
|
137 |
return(0); |
138 |
} |
139 |
|
140 |
|
141 |
void |
142 |
thispage(void) /* rewind current file */ |
143 |
{ |
144 |
if (lineno) |
145 |
error(USER, "cannot restart page in thispage"); |
146 |
} |
147 |
|
148 |
|
149 |
void |
150 |
initfile(void) /* initialize this file */ |
151 |
{ |
152 |
static const unsigned char cmap[8][3] = {{0,0,0}, {0,0,255}, {0,188,0}, |
153 |
{255,152,0}, {0,200,200}, {255,0,255}, {179,179,0}, {255,255,255}}; |
154 |
static int filenum = 0; |
155 |
BMPHeader *hp; |
156 |
int i; |
157 |
|
158 |
hp = BMPmappedHeader(dxsiz, dysiz, 0, 256); |
159 |
if (hp == NULL) |
160 |
error(USER, "Illegal image parameter(s)"); |
161 |
hp->compr = BI_RLE8; |
162 |
for (i = 0; i < 8; i++) { |
163 |
hp->palette[i].r = cmap[i][2]; |
164 |
hp->palette[i].g = cmap[i][1]; |
165 |
hp->palette[i].b = cmap[i][0]; |
166 |
} |
167 |
hp->impColors = 8; |
168 |
if (outtack != NULL) { |
169 |
sprintf(outtack, "%d.bmp", ++filenum); |
170 |
bmpw = BMPopenOutputFile(outname, hp); |
171 |
} else { |
172 |
bmpw = BMPopenOutputStream(stdout, hp); |
173 |
} |
174 |
if (bmpw == NULL) |
175 |
error(USER, "Cannot create output"); |
176 |
} |
177 |
|
178 |
|
179 |
void |
180 |
nextpage(void) /* advance to next page */ |
181 |
{ |
182 |
|
183 |
if (lineno == 0) |
184 |
return; |
185 |
if (bmpw != NULL) { |
186 |
while (lineno < dysiz) { |
187 |
nextblock(); |
188 |
outputblock(); |
189 |
} |
190 |
BMPcloseOutput(bmpw); |
191 |
bmpw = NULL; |
192 |
} |
193 |
lineno = 0; |
194 |
|
195 |
} |
196 |
|
197 |
|
198 |
#define MINRUN 4 |
199 |
|
200 |
void |
201 |
printblock(void) /* output scanline block to file */ |
202 |
{ |
203 |
int i; |
204 |
unsigned char *scanline; |
205 |
|
206 |
if (lineno == 0) |
207 |
initfile(); |
208 |
for (i = outblock.ybot; i <= outblock.ytop && i < dysiz; i++) { |
209 |
scanline = outblock.cols[i-outblock.ybot] + outblock.xleft; |
210 |
memcpy((void *)bmpw->scanline, (void *)scanline, |
211 |
sizeof(uint8)*(outblock.xright-outblock.xleft+1)); |
212 |
if (BMPwriteScanline(bmpw) != BIR_OK) |
213 |
error(SYSTEM, "Error writing BMP file"); |
214 |
lineno++; |
215 |
} |
216 |
|
217 |
} |