ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.16
Committed: Fri Jun 4 14:47:01 1993 UTC (30 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +2 -0 lines
Log Message:
added math.h for atof() usage

File Contents

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