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