1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
1.3 |
static const char RCSid[] = "$Id: meta2tga.c,v 1.2 2003/08/01 14:14:24 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Program to convert meta-files to Targa 8-bit color-mapped format |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
#define MAXALLOC 5000 |
10 |
|
|
|
11 |
|
|
#define DXSIZE 400 /* default x resolution */ |
12 |
|
|
|
13 |
|
|
#define DYSIZE 400 /* default y resolution */ |
14 |
|
|
|
15 |
|
|
#define XCOM "pexpand +vOCImsp -DP %s | psort +y" |
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
#include "meta.h" |
21 |
|
|
|
22 |
|
|
#include "plot.h" |
23 |
|
|
|
24 |
|
|
#include "rast.h" |
25 |
|
|
|
26 |
|
|
#include "targa.h" |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
char *progname; |
32 |
|
|
|
33 |
|
|
SCANBLOCK outblock; |
34 |
|
|
|
35 |
|
|
int dxsize = DXSIZE, dysize = DYSIZE; |
36 |
|
|
|
37 |
|
|
int maxalloc = MAXALLOC; |
38 |
|
|
|
39 |
|
|
int ydown = 0; |
40 |
|
|
|
41 |
|
|
static char outname[64]; |
42 |
|
|
static char *outtack = NULL; |
43 |
|
|
|
44 |
|
|
static FILE *fout; |
45 |
|
|
|
46 |
|
|
static int lineno = 0; |
47 |
|
|
|
48 |
|
|
static short condonly = FALSE, |
49 |
|
|
conditioned = FALSE; |
50 |
|
|
|
51 |
|
|
char * |
52 |
|
|
findtack(s) /* find place to tack on suffix */ |
53 |
|
|
register char *s; |
54 |
|
|
{ |
55 |
|
|
while (*s && *s != '.') |
56 |
|
|
s++; |
57 |
|
|
return(s); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
main(argc, argv) |
62 |
|
|
|
63 |
|
|
int argc; |
64 |
|
|
char **argv; |
65 |
|
|
|
66 |
|
|
{ |
67 |
|
|
FILE *fp; |
68 |
|
|
FILE *popen(); |
69 |
|
|
char comargs[200], command[300]; |
70 |
|
|
|
71 |
|
|
fout = stdout; |
72 |
|
|
progname = *argv++; |
73 |
|
|
argc--; |
74 |
|
|
|
75 |
|
|
condonly = FALSE; |
76 |
|
|
conditioned = FALSE; |
77 |
|
|
|
78 |
|
|
while (argc && **argv == '-') { |
79 |
|
|
switch (*(*argv+1)) { |
80 |
|
|
case 'c': |
81 |
|
|
condonly = TRUE; |
82 |
|
|
break; |
83 |
|
|
case 'r': |
84 |
|
|
conditioned = TRUE; |
85 |
|
|
break; |
86 |
|
|
case 'm': |
87 |
|
|
minwidth = atoi(*++argv); |
88 |
|
|
argc--; |
89 |
|
|
break; |
90 |
|
|
case 'x': |
91 |
|
|
dxsize = atoi(*++argv); |
92 |
|
|
argc--; |
93 |
|
|
break; |
94 |
|
|
case 'y': |
95 |
|
|
dysize = atoi(*++argv); |
96 |
|
|
argc--; |
97 |
|
|
break; |
98 |
|
|
case 'o': |
99 |
|
|
strcpy(outname, *++argv); |
100 |
|
|
outtack = findtack(outname); |
101 |
|
|
argc--; |
102 |
|
|
break; |
103 |
|
|
default: |
104 |
|
|
error(WARNING, "unknown option"); |
105 |
|
|
break; |
106 |
|
|
} |
107 |
|
|
argv++; |
108 |
|
|
argc--; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
if (conditioned) { |
112 |
|
|
if (argc) |
113 |
|
|
while (argc) { |
114 |
|
|
fp = efopen(*argv, "r"); |
115 |
|
|
plot(fp); |
116 |
|
|
fclose(fp); |
117 |
|
|
argv++; |
118 |
|
|
argc--; |
119 |
|
|
} |
120 |
|
|
else |
121 |
|
|
plot(stdin); |
122 |
|
|
if (lineno) |
123 |
|
|
nextpage(); |
124 |
|
|
} else { |
125 |
|
|
comargs[0] = '\0'; |
126 |
|
|
while (argc) { |
127 |
|
|
strcat(comargs, " "); |
128 |
|
|
strcat(comargs, *argv); |
129 |
|
|
argv++; |
130 |
|
|
argc--; |
131 |
|
|
} |
132 |
|
|
sprintf(command, XCOM, comargs); |
133 |
|
|
if (condonly) |
134 |
|
|
return(system(command)); |
135 |
|
|
else { |
136 |
|
|
if ((fp = popen(command, "r")) == NULL) |
137 |
|
|
error(SYSTEM, "cannot execute input filter"); |
138 |
|
|
plot(fp); |
139 |
|
|
pclose(fp); |
140 |
|
|
if (lineno) |
141 |
|
|
nextpage(); |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
return(0); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
thispage() /* rewind current file */ |
154 |
|
|
{ |
155 |
|
|
if (lineno) |
156 |
|
|
error(USER, "cannot restart page in thispage"); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
initfile() /* initialize this file */ |
162 |
|
|
{ |
163 |
|
|
static int filenum = 0; |
164 |
greg |
1.3 |
/* |
165 |
greg |
1.1 |
static unsigned char cmap[24] = {255,255,255, 255,152,0, 0,188,0, 0,0,255, |
166 |
|
|
179,179,0, 255,0,255, 0,200,200, 0,0,0}; |
167 |
greg |
1.3 |
*/ |
168 |
|
|
static unsigned char cmap[24] = {0,0,0, 0,0,255, 0,188,0, 255,152,0, |
169 |
|
|
0,200,200, 255,0,255, 179,179,0, 255,255,255}; |
170 |
greg |
1.1 |
struct hdStruct thead; |
171 |
|
|
register int i; |
172 |
|
|
|
173 |
|
|
if (outtack != NULL) { |
174 |
|
|
sprintf(outtack, "%d.tga", ++filenum); |
175 |
|
|
fout = efopen(outname, "w"); |
176 |
|
|
} |
177 |
|
|
if (fout == NULL) |
178 |
|
|
error(USER, "no output file"); |
179 |
|
|
thead.mapType = CM_HASMAP; |
180 |
|
|
thead.dataType = IM_CCMAP; |
181 |
|
|
thead.mapOrig = 0; |
182 |
|
|
thead.mapLength = 256; |
183 |
|
|
thead.CMapBits = 24; |
184 |
|
|
thead.XOffset = 0; |
185 |
|
|
thead.YOffset = 0; |
186 |
|
|
thead.x = dxsize; |
187 |
|
|
thead.y = dysize; |
188 |
|
|
thead.dataBits = 8; |
189 |
|
|
thead.imType = 0; |
190 |
|
|
putthead(&thead, NULL, fout); |
191 |
|
|
for (i = 0; i < 8*3; i++) |
192 |
|
|
putc(cmap[i], fout); |
193 |
|
|
while (i++ < 256*3) |
194 |
|
|
putc(0, fout); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
nextpage() /* advance to next page */ |
201 |
|
|
|
202 |
|
|
{ |
203 |
|
|
|
204 |
|
|
if (lineno == 0) |
205 |
|
|
return; |
206 |
|
|
if (fout != NULL) { |
207 |
|
|
while (lineno < dysize) { |
208 |
|
|
nextblock(); |
209 |
|
|
outputblock(); |
210 |
|
|
} |
211 |
|
|
fclose(fout); |
212 |
|
|
fout = NULL; |
213 |
|
|
} |
214 |
|
|
lineno = 0; |
215 |
|
|
|
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
#define MINRUN 4 |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
printblock() /* output scanline block to file */ |
224 |
|
|
|
225 |
|
|
{ |
226 |
|
|
int i, c2; |
227 |
|
|
register unsigned char *scanline; |
228 |
|
|
register int j, beg, cnt; |
229 |
|
|
|
230 |
|
|
if (lineno == 0) |
231 |
|
|
initfile(); |
232 |
|
|
for (i = outblock.ybot; i <= outblock.ytop && i < dysize; i++) { |
233 |
|
|
scanline = outblock.cols[i-outblock.ybot]; |
234 |
|
|
for (j = outblock.xleft; j <= outblock.xright; j += cnt) { |
235 |
|
|
for (beg = j; beg <= outblock.xright; beg += cnt) { |
236 |
|
|
for (cnt = 1; cnt < 128 && beg+cnt <= outblock.xright && |
237 |
|
|
scanline[beg+cnt] == scanline[beg]; cnt++) |
238 |
|
|
; |
239 |
|
|
if (cnt >= MINRUN) |
240 |
|
|
break; /* long enough */ |
241 |
|
|
} |
242 |
|
|
while (j < beg) { /* write out non-run */ |
243 |
|
|
if ((c2 = beg-j) > 128) c2 = 128; |
244 |
|
|
putc(c2-1, fout); |
245 |
|
|
while (c2--) |
246 |
|
|
putc(scanline[j++], fout); |
247 |
|
|
} |
248 |
|
|
if (cnt >= MINRUN) { /* write out run */ |
249 |
|
|
putc(127+cnt, fout); |
250 |
|
|
putc(scanline[beg], fout); |
251 |
|
|
} else |
252 |
|
|
cnt = 0; |
253 |
|
|
} |
254 |
|
|
lineno++; |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
putint2(i, fp) /* put a 2-byte positive integer */ |
261 |
|
|
register int i; |
262 |
|
|
register FILE *fp; |
263 |
|
|
{ |
264 |
|
|
putc(i&0xff, fp); |
265 |
|
|
putc(i>>8&0xff, fp); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
|
putthead(hp, ip, fp) /* write header to output */ |
270 |
|
|
struct hdStruct *hp; |
271 |
|
|
char *ip; |
272 |
|
|
register FILE *fp; |
273 |
|
|
{ |
274 |
|
|
if (ip != NULL) |
275 |
|
|
putc(strlen(ip), fp); |
276 |
|
|
else |
277 |
|
|
putc(0, fp); |
278 |
|
|
putc(hp->mapType, fp); |
279 |
|
|
putc(hp->dataType, fp); |
280 |
|
|
putint2(hp->mapOrig, fp); |
281 |
|
|
putint2(hp->mapLength, fp); |
282 |
|
|
putc(hp->CMapBits, fp); |
283 |
|
|
putint2(hp->XOffset, fp); |
284 |
|
|
putint2(hp->YOffset, fp); |
285 |
|
|
putint2(hp->x, fp); |
286 |
|
|
putint2(hp->y, fp); |
287 |
|
|
putc(hp->dataBits, fp); |
288 |
|
|
putc(hp->imType, fp); |
289 |
|
|
|
290 |
|
|
if (ip != NULL) |
291 |
|
|
fputs(ip, fp); |
292 |
|
|
|
293 |
|
|
return(ferror(fp) ? -1 : 0); |
294 |
|
|
} |