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