ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.12
Committed: Thu Aug 13 10:49:04 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +22 -4 lines
Log Message:
improved reaping of child processes

File Contents

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