ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.17
Committed: Wed Dec 29 09:48:12 1993 UTC (30 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +41 -15 lines
Log Message:
added =SS option for changing sense of anchor points

File Contents

# User Rev Content
1 greg 2.17 /* Copyright (c) 1993 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 greg 2.17 #define HASMIN 1
28     #define HASMAX 2
29    
30 greg 1.1 /* output picture size */
31     int xsiz = 0;
32     int ysiz = 0;
33     /* input dimensions */
34     int xmin = 0;
35     int ymin = 0;
36     int xmax = 0;
37     int ymax = 0;
38    
39     COLR bgcolr = BLKCOLR; /* background color */
40    
41 greg 1.9 int labelht = 24; /* label height */
42    
43 greg 1.1 int checkthresh = 0; /* check threshold value */
44    
45 greg 2.14 char Command[] = "<Command>";
46     char Label[] = "<Label>";
47    
48 greg 1.1 char *progname;
49    
50     struct {
51 greg 1.7 char *name; /* file or command name */
52 greg 1.1 FILE *fp; /* stream pointer */
53     int xres, yres; /* picture size */
54     int xloc, yloc; /* anchor point */
55 greg 2.17 int flags; /* HASMIN, HASMAX */
56 greg 1.1 COLR thmin, thmax; /* thresholds */
57     } input[MAXFILE]; /* our input files */
58    
59     int nfile; /* number of files */
60    
61 greg 1.6 int wrongformat = 0;
62 greg 1.1
63 greg 1.9 FILE *popen(), *lblopen();
64 greg 1.6
65 greg 2.15 extern char *malloc();
66 greg 1.7
67 greg 2.15
68 greg 1.1 tabputs(s) /* print line preceded by a tab */
69     char *s;
70     {
71 greg 1.6 char fmt[32];
72    
73     if (isformat(s)) {
74     formatval(fmt, s);
75     wrongformat = strcmp(fmt, COLRFMT);
76     } else {
77     putc('\t', stdout);
78     fputs(s, stdout);
79     }
80 greg 1.1 }
81    
82    
83     main(argc, argv)
84     int argc;
85     char *argv[];
86     {
87 greg 1.7 int ncolumns = 0;
88 greg 1.9 int autolabel = 0;
89 greg 2.11 int curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
90 greg 2.17 int xsgn, ysgn;
91 greg 1.9 char *thislabel;
92 greg 1.1 int an;
93 greg 2.15 #ifdef MSDOS
94     extern int _fmode;
95     _fmode = O_BINARY;
96     setmode(fileno(stdin), O_BINARY);
97     setmode(fileno(stdout), O_BINARY);
98     #endif
99 greg 1.1 progname = argv[0];
100    
101     for (an = 1; an < argc && argv[an][0] == '-'; an++)
102     switch (argv[an][1]) {
103     case 'x':
104 greg 2.11 xsiz = atoi(argv[++an]);
105 greg 1.1 break;
106     case 'y':
107 greg 2.11 ysiz = atoi(argv[++an]);
108 greg 1.1 break;
109     case 'b':
110     setcolr(bgcolr, atof(argv[an+1]),
111     atof(argv[an+2]),
112     atof(argv[an+3]));
113     an += 3;
114     break;
115 greg 1.7 case 'a':
116     ncolumns = atoi(argv[++an]);
117     break;
118 greg 2.11 case 's':
119     spacing = atoi(argv[++an]);
120     break;
121     case 'o':
122     curx = x0 = atoi(argv[++an]);
123     cury = atoi(argv[++an]);
124     break;
125 greg 1.7 case 'l':
126 greg 1.9 switch (argv[an][2]) {
127     case 'a':
128     autolabel++;
129     break;
130     case 'h':
131     labelht = atoi(argv[++an]);
132     break;
133     case '\0':
134     goto dofiles;
135     default:
136     goto userr;
137     }
138 greg 1.7 break;
139 greg 1.1 case '\0':
140     case 't':
141     goto dofiles;
142     default:
143     goto userr;
144     }
145     dofiles:
146     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     fprintf(stderr, "%s: not a Radiance picture\n",
224     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 1.6 fputformat(COLRFMT, stdout);
291 greg 1.10 putchar('\n');
292     fprtresolu(xsiz, ysiz, stdout);
293 greg 1.1
294     compos();
295    
296     quit(0);
297     userr:
298 greg 2.11 fprintf(stderr,
299     "Usage: %s [-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
300 greg 1.8 progname);
301 greg 2.17 fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
302 greg 1.7 quit(1);
303 greg 1.9 toomany:
304     fprintf(stderr, "%s: only %d files and labels allowed\n",
305     progname, MAXFILE);
306     quit(1);
307 greg 1.7 labelerr:
308     fprintf(stderr, "%s: error opening label\n", progname);
309 greg 1.1 quit(1);
310     }
311    
312    
313     compos() /* composite pictures */
314     {
315     COLR *scanin, *scanout;
316     int y;
317     register int x, i;
318    
319 greg 1.3 scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
320 greg 1.1 if (scanin == NULL)
321     goto memerr;
322     scanin -= xmin;
323     if (checkthresh) {
324     scanout = (COLR *)malloc(xsiz*sizeof(COLR));
325     if (scanout == NULL)
326     goto memerr;
327     } else
328     scanout = scanin;
329     for (y = ymax-1; y >= 0; y--) {
330     for (x = 0; x < xsiz; x++)
331     copycolr(scanout[x], bgcolr);
332     for (i = 0; i < nfile; i++) {
333     if (input[i].yloc > y ||
334     input[i].yloc+input[i].yres <= y)
335     continue;
336     if (freadcolrs(scanin+input[i].xloc,
337     input[i].xres, input[i].fp) < 0) {
338 greg 1.2 fprintf(stderr, "%s: read error (y==%d)\n",
339     input[i].name,
340     y-input[i].yloc);
341 greg 1.1 quit(1);
342     }
343     if (y >= ysiz)
344     continue;
345     if (checkthresh) {
346     x = input[i].xloc+input[i].xres;
347     if (x > xsiz)
348     x = xsiz;
349     for (x--; x >= 0 && x >= input[i].xloc; x--) {
350 greg 2.17 if (input[i].flags & HASMIN &&
351 greg 1.1 cmpcolr(scanin[x], input[i].thmin) <= 0)
352     continue;
353 greg 2.17 if (input[i].flags & HASMAX &&
354 greg 1.1 cmpcolr(scanin[x], input[i].thmax) >= 0)
355     continue;
356     copycolr(scanout[x], scanin[x]);
357     }
358     }
359     }
360     if (y >= ysiz)
361     continue;
362     if (fwritecolrs(scanout, xsiz, stdout) < 0) {
363 greg 1.5 perror(progname);
364 greg 1.1 quit(1);
365     }
366     }
367     return;
368     memerr:
369 greg 1.5 perror(progname);
370 greg 1.1 quit(1);
371     }
372    
373    
374     int
375     cmpcolr(c1, c2) /* compare two colr's (improvisation) */
376     register COLR c1, c2;
377     {
378     register int i, j;
379    
380     j = 4; /* check exponents first! */
381     while (j--)
382     if (i = c1[j] - c2[j])
383     return(i);
384     return(0);
385 greg 1.9 }
386    
387    
388     FILE *
389     lblopen(s, xp, yp) /* open pipe to label generator */
390     char *s;
391     int *xp, *yp;
392     {
393     char com[128];
394     FILE *fp;
395    
396 greg 2.7 sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
397 greg 1.9 if ((fp = popen(com, "r")) == NULL)
398     return(NULL);
399     if (checkheader(fp, COLRFMT, NULL) < 0)
400     goto err;
401 greg 1.10 if (fgetresolu(xp, yp, fp) < 0)
402 greg 1.9 goto err;
403     return(fp);
404     err:
405     pclose(fp);
406     return(NULL);
407 greg 1.1 }
408    
409    
410     quit(code) /* exit gracefully */
411     int code;
412     {
413 greg 2.13 register int i;
414     /* close input files */
415     for (i = 0; i < nfile; i++)
416 greg 2.14 if (input[i].name == Command || input[i].name == Label)
417     pclose(input[i].fp);
418     else
419     fclose(input[i].fp);
420 greg 1.1 exit(code);
421     }