ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/meta2tga.c
Revision: 1.1
Committed: Sat Feb 22 02:07:26 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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