ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.22
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.21: +7 -14 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.22 static const char RCSid[] = "$Id: pcompos.c,v 2.21 2003/02/22 02:07:27 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * pcompos.c - program to composite pictures.
6     *
7     * 6/30/87
8     */
9    
10     #include <stdio.h>
11 greg 2.16 #include <math.h>
12 schorsch 2.22 #include <time.h>
13 greg 2.16
14 schorsch 2.22 #include "copyright.h"
15 greg 2.21
16 schorsch 2.22 #include "platform.h"
17 greg 1.1 #include "color.h"
18 greg 1.10 #include "resolu.h"
19 greg 1.1
20 greg 2.15 #define MAXFILE 64
21 greg 1.1
22 greg 2.17 #define HASMIN 1
23     #define HASMAX 2
24    
25 greg 1.1 /* output picture size */
26     int xsiz = 0;
27     int ysiz = 0;
28     /* input dimensions */
29     int xmin = 0;
30     int ymin = 0;
31     int xmax = 0;
32     int ymax = 0;
33    
34     COLR bgcolr = BLKCOLR; /* background color */
35    
36 greg 1.9 int labelht = 24; /* label height */
37    
38 greg 1.1 int checkthresh = 0; /* check threshold value */
39    
40 greg 2.14 char Command[] = "<Command>";
41     char Label[] = "<Label>";
42    
43 greg 1.1 char *progname;
44    
45     struct {
46 greg 1.7 char *name; /* file or command name */
47 greg 1.1 FILE *fp; /* stream pointer */
48     int xres, yres; /* picture size */
49     int xloc, yloc; /* anchor point */
50 greg 2.17 int flags; /* HASMIN, HASMAX */
51 greg 1.1 COLR thmin, thmax; /* thresholds */
52     } input[MAXFILE]; /* our input files */
53    
54     int nfile; /* number of files */
55    
56 greg 2.19 char ourfmt[LPICFMT+1] = PICFMT;
57 greg 1.6 int wrongformat = 0;
58 greg 1.1
59 greg 2.21 FILE *lblopen();
60     void quit();
61 greg 1.7
62 greg 2.15
63 greg 1.1 tabputs(s) /* print line preceded by a tab */
64     char *s;
65     {
66 greg 1.6 char fmt[32];
67    
68 greg 2.18 if (isheadid(s))
69 gwlarson 2.20 return(0);
70 greg 2.19 if (formatval(fmt, s)) {
71     if (globmatch(ourfmt, fmt)) {
72     wrongformat = 0;
73     strcpy(ourfmt, fmt);
74     } else
75     wrongformat = 1;
76     } else {
77 greg 1.6 putc('\t', stdout);
78     fputs(s, stdout);
79     }
80 gwlarson 2.20 return(0);
81 greg 1.1 }
82    
83    
84     main(argc, argv)
85     int argc;
86     char *argv[];
87     {
88 greg 1.7 int ncolumns = 0;
89 greg 1.9 int autolabel = 0;
90 greg 2.11 int curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
91 greg 2.17 int xsgn, ysgn;
92 greg 1.9 char *thislabel;
93 greg 1.1 int an;
94 schorsch 2.22 SET_DEFAULT_BINARY();
95     SET_FILE_BINARY(stdin);
96     SET_FILE_BINARY(stdout);
97 greg 1.1 progname = argv[0];
98    
99     for (an = 1; an < argc && argv[an][0] == '-'; an++)
100     switch (argv[an][1]) {
101     case 'x':
102 greg 2.11 xsiz = atoi(argv[++an]);
103 greg 1.1 break;
104     case 'y':
105 greg 2.11 ysiz = atoi(argv[++an]);
106 greg 1.1 break;
107     case 'b':
108     setcolr(bgcolr, atof(argv[an+1]),
109     atof(argv[an+2]),
110     atof(argv[an+3]));
111     an += 3;
112     break;
113 greg 1.7 case 'a':
114     ncolumns = atoi(argv[++an]);
115     break;
116 greg 2.11 case 's':
117     spacing = atoi(argv[++an]);
118     break;
119     case 'o':
120     curx = x0 = atoi(argv[++an]);
121     cury = atoi(argv[++an]);
122     break;
123 greg 1.7 case 'l':
124 greg 1.9 switch (argv[an][2]) {
125     case 'a':
126     autolabel++;
127     break;
128     case 'h':
129     labelht = atoi(argv[++an]);
130     break;
131     case '\0':
132     goto dofiles;
133     default:
134     goto userr;
135     }
136 greg 1.7 break;
137 greg 1.1 case '\0':
138     case 't':
139     goto dofiles;
140     default:
141     goto userr;
142     }
143     dofiles:
144 greg 2.18 newheader("RADIANCE", stdout);
145 greg 2.21 fputnow(stdout);
146 greg 1.1 for (nfile = 0; an < argc; nfile++) {
147 greg 1.9 if (nfile >= MAXFILE)
148     goto toomany;
149 greg 2.6 thislabel = NULL;
150 greg 2.17 input[nfile].flags = 0;
151     xsgn = ysgn = '-';
152     while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'
153     || argv[an][0] == '=')) {
154 greg 1.1 switch (argv[an][1]) {
155     case 't':
156     checkthresh = 1;
157     if (argv[an][0] == '-') {
158 greg 2.17 input[nfile].flags |= HASMIN;
159 greg 1.1 setcolr(input[nfile].thmin,
160     atof(argv[an+1]),
161     atof(argv[an+1]),
162     atof(argv[an+1]));
163 greg 2.17 } else if (argv[an][0] == '+') {
164     input[nfile].flags |= HASMAX;
165 greg 1.1 setcolr(input[nfile].thmax,
166     atof(argv[an+1]),
167     atof(argv[an+1]),
168     atof(argv[an+1]));
169 greg 2.17 } else
170     goto userr;
171     an++;
172 greg 1.1 break;
173 greg 1.9 case 'l':
174     if (strcmp(argv[an], "-l"))
175     goto userr;
176 greg 2.17 thislabel = argv[++an];
177 greg 1.9 break;
178 greg 2.17 case '+':
179     case '-':
180     case '0':
181     if (argv[an][0] != '=')
182     goto userr;
183     xsgn = argv[an][1];
184     ysgn = argv[an][2];
185     if (ysgn != '+' && ysgn != '-' && ysgn != '0')
186     goto userr;
187     break;
188 greg 1.1 case '\0':
189     if (argv[an][0] == '-')
190     goto getfile;
191 greg 1.9 goto userr;
192 greg 1.1 default:
193     goto userr;
194     }
195 greg 2.17 an++;
196     }
197 greg 1.1 getfile:
198 greg 1.7 if (argc-an < (ncolumns ? 1 : 3))
199 greg 1.1 goto userr;
200 greg 2.6 if (autolabel && thislabel == NULL)
201     thislabel = argv[an];
202 greg 1.1 if (!strcmp(argv[an], "-")) {
203     input[nfile].name = "<stdin>";
204     input[nfile].fp = stdin;
205     } else {
206 greg 2.4 if (argv[an][0] == '!') {
207 greg 2.14 input[nfile].name = Command;
208 greg 2.4 input[nfile].fp = popen(argv[an]+1, "r");
209     } else {
210     input[nfile].name = argv[an];
211     input[nfile].fp = fopen(argv[an], "r");
212     }
213     if (input[nfile].fp == NULL) {
214 greg 1.5 perror(argv[an]);
215 greg 1.1 quit(1);
216     }
217     }
218     an++;
219     /* get header */
220     printf("%s:\n", input[nfile].name);
221 greg 1.6 getheader(input[nfile].fp, tabputs, NULL);
222     if (wrongformat) {
223 greg 2.21 fprintf(stderr, "%s: incompatible input format\n",
224 greg 1.6 input[nfile].name);
225     quit(1);
226     }
227 greg 1.1 /* get picture size */
228 greg 1.4 if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
229 greg 1.10 input[nfile].fp) < 0) {
230 greg 1.1 fprintf(stderr, "%s: bad picture size\n",
231     input[nfile].name);
232     quit(1);
233     }
234 greg 1.7 if (ncolumns > 0) {
235     if (curcol >= ncolumns) {
236 greg 2.11 cury = ymax + spacing;
237     curx = x0;
238 greg 1.7 curcol = 0;
239     }
240     input[nfile].xloc = curx;
241     input[nfile].yloc = cury;
242 greg 2.11 curx += input[nfile].xres + spacing;
243 greg 1.7 curcol++;
244     } else {
245     input[nfile].xloc = atoi(argv[an++]);
246 greg 2.17 if (xsgn == '+')
247     input[nfile].xloc -= input[nfile].xres;
248     else if (xsgn == '0')
249     input[nfile].xloc -= input[nfile].xres/2;
250 greg 1.7 input[nfile].yloc = atoi(argv[an++]);
251 greg 2.17 if (ysgn == '+')
252     input[nfile].yloc -= input[nfile].yres;
253     else if (ysgn == '0')
254     input[nfile].yloc -= input[nfile].yres/2;
255 greg 1.7 }
256 greg 1.1 if (input[nfile].xloc < xmin)
257     xmin = input[nfile].xloc;
258     if (input[nfile].yloc < ymin)
259     ymin = input[nfile].yloc;
260     if (input[nfile].xloc+input[nfile].xres > xmax)
261     xmax = input[nfile].xloc+input[nfile].xres;
262     if (input[nfile].yloc+input[nfile].yres > ymax)
263     ymax = input[nfile].yloc+input[nfile].yres;
264 greg 1.9 if (thislabel != NULL) {
265     if (++nfile >= MAXFILE)
266     goto toomany;
267 greg 2.14 input[nfile].name = Label;
268 greg 2.17 input[nfile].flags = 0;
269 greg 2.7 input[nfile].xres = input[nfile-1].xres;
270     input[nfile].yres = labelht;
271 greg 1.9 if ((input[nfile].fp = lblopen(thislabel,
272     &input[nfile].xres,
273     &input[nfile].yres)) == NULL)
274 greg 1.7 goto labelerr;
275     input[nfile].xloc = input[nfile-1].xloc;
276     input[nfile].yloc = input[nfile-1].yloc +
277 greg 1.9 input[nfile-1].yres-input[nfile].yres;
278 greg 1.7 }
279 greg 1.1 }
280     if (xsiz <= 0)
281     xsiz = xmax;
282 greg 2.11 else if (xsiz > xmax)
283     xmax = xsiz;
284 greg 1.1 if (ysiz <= 0)
285     ysiz = ymax;
286 greg 2.11 else if (ysiz > ymax)
287     ymax = ysiz;
288 greg 1.1 /* add new header info. */
289     printargs(argc, argv, stdout);
290 greg 2.19 if (strcmp(ourfmt, PICFMT))
291     fputformat(ourfmt, stdout); /* print format if known */
292 greg 1.10 putchar('\n');
293     fprtresolu(xsiz, ysiz, stdout);
294 greg 1.1
295     compos();
296    
297     quit(0);
298     userr:
299 greg 2.11 fprintf(stderr,
300     "Usage: %s [-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
301 greg 1.8 progname);
302 greg 2.17 fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
303 greg 1.7 quit(1);
304 greg 1.9 toomany:
305     fprintf(stderr, "%s: only %d files and labels allowed\n",
306     progname, MAXFILE);
307     quit(1);
308 greg 1.7 labelerr:
309     fprintf(stderr, "%s: error opening label\n", progname);
310 greg 1.1 quit(1);
311     }
312    
313    
314     compos() /* composite pictures */
315     {
316     COLR *scanin, *scanout;
317     int y;
318     register int x, i;
319    
320 greg 1.3 scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
321 greg 1.1 if (scanin == NULL)
322     goto memerr;
323     scanin -= xmin;
324     if (checkthresh) {
325     scanout = (COLR *)malloc(xsiz*sizeof(COLR));
326     if (scanout == NULL)
327     goto memerr;
328     } else
329     scanout = scanin;
330     for (y = ymax-1; y >= 0; y--) {
331     for (x = 0; x < xsiz; x++)
332     copycolr(scanout[x], bgcolr);
333     for (i = 0; i < nfile; i++) {
334     if (input[i].yloc > y ||
335     input[i].yloc+input[i].yres <= y)
336     continue;
337     if (freadcolrs(scanin+input[i].xloc,
338     input[i].xres, input[i].fp) < 0) {
339 greg 1.2 fprintf(stderr, "%s: read error (y==%d)\n",
340     input[i].name,
341     y-input[i].yloc);
342 greg 1.1 quit(1);
343     }
344     if (y >= ysiz)
345     continue;
346     if (checkthresh) {
347     x = input[i].xloc+input[i].xres;
348     if (x > xsiz)
349     x = xsiz;
350     for (x--; x >= 0 && x >= input[i].xloc; x--) {
351 greg 2.17 if (input[i].flags & HASMIN &&
352 greg 1.1 cmpcolr(scanin[x], input[i].thmin) <= 0)
353     continue;
354 greg 2.17 if (input[i].flags & HASMAX &&
355 greg 1.1 cmpcolr(scanin[x], input[i].thmax) >= 0)
356     continue;
357     copycolr(scanout[x], scanin[x]);
358     }
359     }
360     }
361     if (y >= ysiz)
362     continue;
363     if (fwritecolrs(scanout, xsiz, stdout) < 0) {
364 greg 1.5 perror(progname);
365 greg 1.1 quit(1);
366     }
367     }
368     return;
369     memerr:
370 greg 1.5 perror(progname);
371 greg 1.1 quit(1);
372     }
373    
374    
375     int
376     cmpcolr(c1, c2) /* compare two colr's (improvisation) */
377     register COLR c1, c2;
378     {
379     register int i, j;
380    
381     j = 4; /* check exponents first! */
382     while (j--)
383     if (i = c1[j] - c2[j])
384     return(i);
385     return(0);
386 greg 1.9 }
387    
388    
389     FILE *
390     lblopen(s, xp, yp) /* open pipe to label generator */
391     char *s;
392     int *xp, *yp;
393     {
394     char com[128];
395     FILE *fp;
396    
397 greg 2.7 sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
398 greg 1.9 if ((fp = popen(com, "r")) == NULL)
399     return(NULL);
400     if (checkheader(fp, COLRFMT, NULL) < 0)
401     goto err;
402 greg 1.10 if (fgetresolu(xp, yp, fp) < 0)
403 greg 1.9 goto err;
404     return(fp);
405     err:
406     pclose(fp);
407     return(NULL);
408 greg 1.1 }
409    
410    
411 greg 2.21 void
412 greg 1.1 quit(code) /* exit gracefully */
413     int code;
414     {
415 greg 2.13 register int i;
416     /* close input files */
417     for (i = 0; i < nfile; i++)
418 greg 2.14 if (input[i].name == Command || input[i].name == Label)
419     pclose(input[i].fp);
420     else
421     fclose(input[i].fp);
422 greg 1.1 exit(code);
423     }