ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.19
Committed: Mon Oct 16 11:40:09 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +11 -5 lines
Log Message:
added compatibility with alternate RADIANCE picture formats

File Contents

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