1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: meta2tga.c,v 1.1 2003/02/22 02:07:26 greg Exp $"; |
3 |
#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 |
static unsigned char cmap[24] = {255,255,255, 255,152,0, 0,188,0, 0,0,255, |
165 |
179,179,0, 255,0,255, 0,200,200, 0,0,0}; |
166 |
struct hdStruct thead; |
167 |
register int i; |
168 |
|
169 |
if (outtack != NULL) { |
170 |
sprintf(outtack, "%d.tga", ++filenum); |
171 |
fout = efopen(outname, "w"); |
172 |
} |
173 |
if (fout == NULL) |
174 |
error(USER, "no output file"); |
175 |
thead.mapType = CM_HASMAP; |
176 |
thead.dataType = IM_CCMAP; |
177 |
thead.mapOrig = 0; |
178 |
thead.mapLength = 256; |
179 |
thead.CMapBits = 24; |
180 |
thead.XOffset = 0; |
181 |
thead.YOffset = 0; |
182 |
thead.x = dxsize; |
183 |
thead.y = dysize; |
184 |
thead.dataBits = 8; |
185 |
thead.imType = 0; |
186 |
putthead(&thead, NULL, fout); |
187 |
for (i = 0; i < 8*3; i++) |
188 |
putc(cmap[i], fout); |
189 |
while (i++ < 256*3) |
190 |
putc(0, fout); |
191 |
} |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
nextpage() /* advance to next page */ |
197 |
|
198 |
{ |
199 |
|
200 |
if (lineno == 0) |
201 |
return; |
202 |
if (fout != NULL) { |
203 |
while (lineno < dysize) { |
204 |
nextblock(); |
205 |
outputblock(); |
206 |
} |
207 |
fclose(fout); |
208 |
fout = NULL; |
209 |
} |
210 |
lineno = 0; |
211 |
|
212 |
} |
213 |
|
214 |
|
215 |
|
216 |
#define MINRUN 4 |
217 |
|
218 |
|
219 |
printblock() /* output scanline block to file */ |
220 |
|
221 |
{ |
222 |
int i, c2; |
223 |
register unsigned char *scanline; |
224 |
register int j, beg, cnt; |
225 |
|
226 |
if (lineno == 0) |
227 |
initfile(); |
228 |
for (i = outblock.ybot; i <= outblock.ytop && i < dysize; i++) { |
229 |
scanline = outblock.cols[i-outblock.ybot]; |
230 |
for (j = outblock.xleft; j <= outblock.xright; j += cnt) { |
231 |
for (beg = j; beg <= outblock.xright; beg += cnt) { |
232 |
for (cnt = 1; cnt < 128 && beg+cnt <= outblock.xright && |
233 |
scanline[beg+cnt] == scanline[beg]; cnt++) |
234 |
; |
235 |
if (cnt >= MINRUN) |
236 |
break; /* long enough */ |
237 |
} |
238 |
while (j < beg) { /* write out non-run */ |
239 |
if ((c2 = beg-j) > 128) c2 = 128; |
240 |
putc(c2-1, fout); |
241 |
while (c2--) |
242 |
putc(scanline[j++], fout); |
243 |
} |
244 |
if (cnt >= MINRUN) { /* write out run */ |
245 |
putc(127+cnt, fout); |
246 |
putc(scanline[beg], fout); |
247 |
} else |
248 |
cnt = 0; |
249 |
} |
250 |
lineno++; |
251 |
} |
252 |
|
253 |
} |
254 |
|
255 |
|
256 |
putint2(i, fp) /* put a 2-byte positive integer */ |
257 |
register int i; |
258 |
register FILE *fp; |
259 |
{ |
260 |
putc(i&0xff, fp); |
261 |
putc(i>>8&0xff, fp); |
262 |
} |
263 |
|
264 |
|
265 |
putthead(hp, ip, fp) /* write header to output */ |
266 |
struct hdStruct *hp; |
267 |
char *ip; |
268 |
register FILE *fp; |
269 |
{ |
270 |
if (ip != NULL) |
271 |
putc(strlen(ip), fp); |
272 |
else |
273 |
putc(0, fp); |
274 |
putc(hp->mapType, fp); |
275 |
putc(hp->dataType, fp); |
276 |
putint2(hp->mapOrig, fp); |
277 |
putint2(hp->mapLength, fp); |
278 |
putc(hp->CMapBits, fp); |
279 |
putint2(hp->XOffset, fp); |
280 |
putint2(hp->YOffset, fp); |
281 |
putint2(hp->x, fp); |
282 |
putint2(hp->y, fp); |
283 |
putc(hp->dataBits, fp); |
284 |
putc(hp->imType, fp); |
285 |
|
286 |
if (ip != NULL) |
287 |
fputs(ip, fp); |
288 |
|
289 |
return(ferror(fp) ? -1 : 0); |
290 |
} |