ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.10
Committed: Tue Aug 11 18:40:57 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +5 -0 lines
Log Message:
added error message about using -a with -y

File Contents

# Content
1 /* Copyright (c) 1991 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 #include "resolu.h"
18
19 #define MAXFILE 64
20
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 int labelht = 24; /* label height */
33
34 int checkthresh = 0; /* check threshold value */
35
36 char *progname;
37
38 struct {
39 char *name; /* file or command name */
40 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 int wrongformat = 0;
50
51 FILE *popen(), *lblopen();
52
53
54 tabputs(s) /* print line preceded by a tab */
55 char *s;
56 {
57 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 }
67
68
69 main(argc, argv)
70 int argc;
71 char *argv[];
72 {
73 int ncolumns = 0;
74 int autolabel = 0;
75 int curcol = 0, curx = 0, cury = 0;
76 char *thislabel;
77 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 xmax = xsiz = atoi(argv[++an]);
85 break;
86 case 'y':
87 ymax = ysiz = atoi(argv[++an]);
88 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 case 'a':
96 ncolumns = atoi(argv[++an]);
97 break;
98 case 'l':
99 switch (argv[an][2]) {
100 case 'a':
101 autolabel++;
102 break;
103 case 'h':
104 labelht = atoi(argv[++an]);
105 break;
106 case '\0':
107 goto dofiles;
108 default:
109 goto userr;
110 }
111 break;
112 case '\0':
113 case 't':
114 goto dofiles;
115 default:
116 goto userr;
117 }
118 dofiles:
119 if (ysiz > 0 & ncolumns > 0) {
120 fprintf(stderr, "%s: -a option incompatible with -y\n",
121 progname);
122 quit(1);
123 }
124 for (nfile = 0; an < argc; nfile++) {
125 if (nfile >= MAXFILE)
126 goto toomany;
127 thislabel = NULL;
128 input[nfile].hasmin = input[nfile].hasmax = 0;
129 while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
130 switch (argv[an][1]) {
131 case 't':
132 checkthresh = 1;
133 if (argv[an][0] == '-') {
134 input[nfile].hasmin = 1;
135 setcolr(input[nfile].thmin,
136 atof(argv[an+1]),
137 atof(argv[an+1]),
138 atof(argv[an+1]));
139 } else {
140 input[nfile].hasmax = 1;
141 setcolr(input[nfile].thmax,
142 atof(argv[an+1]),
143 atof(argv[an+1]),
144 atof(argv[an+1]));
145 }
146 an += 2;
147 break;
148 case 'l':
149 if (strcmp(argv[an], "-l"))
150 goto userr;
151 thislabel = argv[an+1];
152 an += 2;
153 break;
154 case '\0':
155 if (argv[an][0] == '-')
156 goto getfile;
157 goto userr;
158 default:
159 goto userr;
160 }
161 getfile:
162 if (argc-an < (ncolumns ? 1 : 3))
163 goto userr;
164 if (autolabel && thislabel == NULL)
165 thislabel = argv[an];
166 if (!strcmp(argv[an], "-")) {
167 input[nfile].name = "<stdin>";
168 input[nfile].fp = stdin;
169 } else {
170 if (argv[an][0] == '!') {
171 input[nfile].name = "<Command>";
172 input[nfile].fp = popen(argv[an]+1, "r");
173 } else {
174 input[nfile].name = argv[an];
175 input[nfile].fp = fopen(argv[an], "r");
176 }
177 if (input[nfile].fp == NULL) {
178 perror(argv[an]);
179 quit(1);
180 }
181 }
182 an++;
183 /* get header */
184 printf("%s:\n", input[nfile].name);
185 getheader(input[nfile].fp, tabputs, NULL);
186 if (wrongformat) {
187 fprintf(stderr, "%s: not a Radiance picture\n",
188 input[nfile].name);
189 quit(1);
190 }
191 /* get picture size */
192 if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
193 input[nfile].fp) < 0) {
194 fprintf(stderr, "%s: bad picture size\n",
195 input[nfile].name);
196 quit(1);
197 }
198 if (ncolumns > 0) {
199 if (curcol >= ncolumns) {
200 cury = ymax;
201 curx = 0;
202 curcol = 0;
203 }
204 input[nfile].xloc = curx;
205 input[nfile].yloc = cury;
206 curx += input[nfile].xres;
207 curcol++;
208 } else {
209 input[nfile].xloc = atoi(argv[an++]);
210 input[nfile].yloc = atoi(argv[an++]);
211 }
212 if (input[nfile].xloc < xmin)
213 xmin = input[nfile].xloc;
214 if (input[nfile].yloc < ymin)
215 ymin = input[nfile].yloc;
216 if (input[nfile].xloc+input[nfile].xres > xmax)
217 xmax = input[nfile].xloc+input[nfile].xres;
218 if (input[nfile].yloc+input[nfile].yres > ymax)
219 ymax = input[nfile].yloc+input[nfile].yres;
220 if (thislabel != NULL) {
221 if (++nfile >= MAXFILE)
222 goto toomany;
223 input[nfile].name = "<Label>";
224 input[nfile].hasmin = input[nfile].hasmax = 0;
225 input[nfile].xres = input[nfile-1].xres;
226 input[nfile].yres = labelht;
227 if ((input[nfile].fp = lblopen(thislabel,
228 &input[nfile].xres,
229 &input[nfile].yres)) == NULL)
230 goto labelerr;
231 input[nfile].xloc = input[nfile-1].xloc;
232 input[nfile].yloc = input[nfile-1].yloc +
233 input[nfile-1].yres-input[nfile].yres;
234 }
235 }
236 if (xsiz <= 0)
237 xsiz = xmax;
238 if (ysiz <= 0)
239 ysiz = ymax;
240 /* add new header info. */
241 printargs(argc, argv, stdout);
242 fputformat(COLRFMT, stdout);
243 putchar('\n');
244 fprtresolu(xsiz, ysiz, stdout);
245
246 compos();
247
248 quit(0);
249 userr:
250 fprintf(stderr, "Usage: %s [-x xr][-y yr][-b r g b][-a n][-la][-lh h] ",
251 progname);
252 fprintf(stderr, "[-t min1][+t max1][-l lab] pic1 x1 y1 ..\n");
253 quit(1);
254 toomany:
255 fprintf(stderr, "%s: only %d files and labels allowed\n",
256 progname, MAXFILE);
257 quit(1);
258 labelerr:
259 fprintf(stderr, "%s: error opening label\n", progname);
260 quit(1);
261 }
262
263
264 compos() /* composite pictures */
265 {
266 COLR *scanin, *scanout;
267 int y;
268 register int x, i;
269
270 scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
271 if (scanin == NULL)
272 goto memerr;
273 scanin -= xmin;
274 if (checkthresh) {
275 scanout = (COLR *)malloc(xsiz*sizeof(COLR));
276 if (scanout == NULL)
277 goto memerr;
278 } else
279 scanout = scanin;
280 for (y = ymax-1; y >= 0; y--) {
281 for (x = 0; x < xsiz; x++)
282 copycolr(scanout[x], bgcolr);
283 for (i = 0; i < nfile; i++) {
284 if (input[i].yloc > y ||
285 input[i].yloc+input[i].yres <= y)
286 continue;
287 if (freadcolrs(scanin+input[i].xloc,
288 input[i].xres, input[i].fp) < 0) {
289 fprintf(stderr, "%s: read error (y==%d)\n",
290 input[i].name,
291 y-input[i].yloc);
292 quit(1);
293 }
294 if (y >= ysiz)
295 continue;
296 if (checkthresh) {
297 x = input[i].xloc+input[i].xres;
298 if (x > xsiz)
299 x = xsiz;
300 for (x--; x >= 0 && x >= input[i].xloc; x--) {
301 if (input[i].hasmin &&
302 cmpcolr(scanin[x], input[i].thmin) <= 0)
303 continue;
304 if (input[i].hasmax &&
305 cmpcolr(scanin[x], input[i].thmax) >= 0)
306 continue;
307 copycolr(scanout[x], scanin[x]);
308 }
309 }
310 }
311 if (y >= ysiz)
312 continue;
313 if (fwritecolrs(scanout, xsiz, stdout) < 0) {
314 perror(progname);
315 quit(1);
316 }
317 }
318 return;
319 memerr:
320 perror(progname);
321 quit(1);
322 }
323
324
325 int
326 cmpcolr(c1, c2) /* compare two colr's (improvisation) */
327 register COLR c1, c2;
328 {
329 register int i, j;
330
331 j = 4; /* check exponents first! */
332 while (j--)
333 if (i = c1[j] - c2[j])
334 return(i);
335 return(0);
336 }
337
338
339 FILE *
340 lblopen(s, xp, yp) /* open pipe to label generator */
341 char *s;
342 int *xp, *yp;
343 {
344 char com[128];
345 FILE *fp;
346
347 sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
348 if ((fp = popen(com, "r")) == NULL)
349 return(NULL);
350 if (checkheader(fp, COLRFMT, NULL) < 0)
351 goto err;
352 if (fgetresolu(xp, yp, fp) < 0)
353 goto err;
354 return(fp);
355 err:
356 pclose(fp);
357 return(NULL);
358 }
359
360
361 quit(code) /* exit gracefully */
362 int code;
363 {
364 int status;
365
366 if (code == 0) /* reap any children */
367 while (wait(&status) != -1)
368 if (code == 0)
369 code = status>>8 & 0xff;
370 exit(code);
371 }