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