ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 1.6
Committed: Thu Apr 18 14:35:19 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +18 -3 lines
Log Message:
added format information to headers

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1987 Regents of the University of California */
2    
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    
18 greg 1.5 #define MAXFILE 32
19 greg 1.1
20     /* output picture size */
21     int xsiz = 0;
22     int ysiz = 0;
23     /* input dimensions */
24     int xmin = 0;
25     int ymin = 0;
26     int xmax = 0;
27     int ymax = 0;
28    
29     COLR bgcolr = BLKCOLR; /* background color */
30    
31     int checkthresh = 0; /* check threshold value */
32    
33     char *progname;
34    
35     struct {
36     char *name; /* file name */
37     FILE *fp; /* stream pointer */
38     int xres, yres; /* picture size */
39     int xloc, yloc; /* anchor point */
40     int hasmin, hasmax; /* has threshold values */
41     COLR thmin, thmax; /* thresholds */
42     } input[MAXFILE]; /* our input files */
43    
44     int nfile; /* number of files */
45    
46 greg 1.6 int wrongformat = 0;
47 greg 1.1
48 greg 1.6
49 greg 1.1 tabputs(s) /* print line preceded by a tab */
50     char *s;
51     {
52 greg 1.6 char fmt[32];
53    
54     if (isformat(s)) {
55     formatval(fmt, s);
56     wrongformat = strcmp(fmt, COLRFMT);
57     } else {
58     putc('\t', stdout);
59     fputs(s, stdout);
60     }
61 greg 1.1 }
62    
63    
64     main(argc, argv)
65     int argc;
66     char *argv[];
67     {
68     double atof();
69     int an;
70    
71     progname = argv[0];
72    
73     for (an = 1; an < argc && argv[an][0] == '-'; an++)
74     switch (argv[an][1]) {
75     case 'x':
76     xmax = xsiz = atoi(argv[++an]);
77     break;
78     case 'y':
79     ymax = ysiz = atoi(argv[++an]);
80     break;
81     case 'b':
82     setcolr(bgcolr, atof(argv[an+1]),
83     atof(argv[an+2]),
84     atof(argv[an+3]));
85     an += 3;
86     break;
87     case '\0':
88     case 't':
89     goto dofiles;
90     default:
91     goto userr;
92     }
93     dofiles:
94     for (nfile = 0; an < argc; nfile++) {
95     if (nfile >= MAXFILE) {
96     fprintf(stderr, "%s: too many files\n", progname);
97     quit(1);
98     }
99     input[nfile].hasmin = input[nfile].hasmax = 0;
100     while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
101     switch (argv[an][1]) {
102     case 't':
103     checkthresh = 1;
104     if (argv[an][0] == '-') {
105     input[nfile].hasmin = 1;
106     setcolr(input[nfile].thmin,
107     atof(argv[an+1]),
108     atof(argv[an+1]),
109     atof(argv[an+1]));
110     } else {
111     input[nfile].hasmax = 1;
112     setcolr(input[nfile].thmax,
113     atof(argv[an+1]),
114     atof(argv[an+1]),
115     atof(argv[an+1]));
116     }
117     an += 2;
118     break;
119     case '\0':
120     if (argv[an][0] == '-')
121     goto getfile;
122     /* fall through */
123     default:
124     goto userr;
125     }
126     getfile:
127     if (argc-an < 3)
128     goto userr;
129     if (!strcmp(argv[an], "-")) {
130     input[nfile].name = "<stdin>";
131     input[nfile].fp = stdin;
132     } else {
133     input[nfile].name = argv[an];
134     if ((input[nfile].fp = fopen(argv[an], "r")) == NULL) {
135 greg 1.5 perror(argv[an]);
136 greg 1.1 quit(1);
137     }
138     }
139     an++;
140     /* get header */
141     printf("%s:\n", input[nfile].name);
142 greg 1.6 getheader(input[nfile].fp, tabputs, NULL);
143     if (wrongformat) {
144     fprintf(stderr, "%s: not a Radiance picture\n",
145     input[nfile].name);
146     quit(1);
147     }
148 greg 1.1 /* get picture size */
149 greg 1.4 if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
150     input[nfile].fp) != (YMAJOR|YDECR)) {
151 greg 1.1 fprintf(stderr, "%s: bad picture size\n",
152     input[nfile].name);
153     quit(1);
154     }
155     input[nfile].xloc = atoi(argv[an++]);
156     input[nfile].yloc = atoi(argv[an++]);
157     if (input[nfile].xloc < xmin)
158     xmin = input[nfile].xloc;
159     if (input[nfile].yloc < ymin)
160     ymin = input[nfile].yloc;
161     if (input[nfile].xloc+input[nfile].xres > xmax)
162     xmax = input[nfile].xloc+input[nfile].xres;
163     if (input[nfile].yloc+input[nfile].yres > ymax)
164     ymax = input[nfile].yloc+input[nfile].yres;
165     }
166     if (xsiz <= 0)
167     xsiz = xmax;
168     if (ysiz <= 0)
169     ysiz = ymax;
170     /* add new header info. */
171     printargs(argc, argv, stdout);
172 greg 1.6 fputformat(COLRFMT, stdout);
173 greg 1.1 printf("\n-Y %d +X %d\n", ysiz, xsiz);
174    
175     compos();
176    
177     quit(0);
178     userr:
179     fprintf(stderr, "Usage: %s [-x xres][-y yres][-b r g b] ", progname);
180     fprintf(stderr, "[-t min1][+t max1] file1 x1 y1 ..\n");
181     quit(1);
182     }
183    
184    
185     compos() /* composite pictures */
186     {
187     COLR *scanin, *scanout;
188     int y;
189     register int x, i;
190    
191 greg 1.3 scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
192 greg 1.1 if (scanin == NULL)
193     goto memerr;
194     scanin -= xmin;
195     if (checkthresh) {
196     scanout = (COLR *)malloc(xsiz*sizeof(COLR));
197     if (scanout == NULL)
198     goto memerr;
199     } else
200     scanout = scanin;
201     for (y = ymax-1; y >= 0; y--) {
202     for (x = 0; x < xsiz; x++)
203     copycolr(scanout[x], bgcolr);
204     for (i = 0; i < nfile; i++) {
205     if (input[i].yloc > y ||
206     input[i].yloc+input[i].yres <= y)
207     continue;
208     if (freadcolrs(scanin+input[i].xloc,
209     input[i].xres, input[i].fp) < 0) {
210 greg 1.2 fprintf(stderr, "%s: read error (y==%d)\n",
211     input[i].name,
212     y-input[i].yloc);
213 greg 1.1 quit(1);
214     }
215     if (y >= ysiz)
216     continue;
217     if (checkthresh) {
218     x = input[i].xloc+input[i].xres;
219     if (x > xsiz)
220     x = xsiz;
221     for (x--; x >= 0 && x >= input[i].xloc; x--) {
222     if (input[i].hasmin &&
223     cmpcolr(scanin[x], input[i].thmin) <= 0)
224     continue;
225     if (input[i].hasmax &&
226     cmpcolr(scanin[x], input[i].thmax) >= 0)
227     continue;
228     copycolr(scanout[x], scanin[x]);
229     }
230     }
231     }
232     if (y >= ysiz)
233     continue;
234     if (fwritecolrs(scanout, xsiz, stdout) < 0) {
235 greg 1.5 perror(progname);
236 greg 1.1 quit(1);
237     }
238     }
239     return;
240     memerr:
241 greg 1.5 perror(progname);
242 greg 1.1 quit(1);
243     }
244    
245    
246     int
247     cmpcolr(c1, c2) /* compare two colr's (improvisation) */
248     register COLR c1, c2;
249     {
250     register int i, j;
251    
252     j = 4; /* check exponents first! */
253     while (j--)
254     if (i = c1[j] - c2[j])
255     return(i);
256     return(0);
257     }
258    
259    
260     quit(code) /* exit gracefully */
261     int code;
262     {
263     exit(code);
264     }