1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* pcompos.c - program to composite pictures. |
6 |
|
* |
7 |
|
* 6/30/87 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include <math.h> |
11 |
|
|
12 |
< |
#include "color.h" |
12 |
> |
#include "copyright.h" |
13 |
|
|
14 |
+ |
#include "rtio.h" |
15 |
+ |
#include "platform.h" |
16 |
+ |
#include "paths.h" |
17 |
+ |
#include "rterror.h" |
18 |
+ |
#include "color.h" |
19 |
|
#include "resolu.h" |
20 |
|
|
21 |
< |
#define MAXFILE 64 |
21 |
> |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
22 |
> |
#undef getc |
23 |
> |
#define getc getc_unlocked |
24 |
> |
#endif |
25 |
|
|
26 |
+ |
#define MAXFILE 1024 |
27 |
+ |
|
28 |
+ |
#define HASMIN 1 |
29 |
+ |
#define HASMAX 2 |
30 |
+ |
|
31 |
|
/* output picture size */ |
32 |
|
int xsiz = 0; |
33 |
|
int ysiz = 0; |
43 |
|
|
44 |
|
int checkthresh = 0; /* check threshold value */ |
45 |
|
|
46 |
+ |
char StandardInput[] = "<stdin>"; |
47 |
+ |
char Command[] = "<Command>"; |
48 |
+ |
char Label[] = "<Label>"; |
49 |
+ |
|
50 |
|
char *progname; |
51 |
|
|
52 |
|
struct { |
54 |
|
FILE *fp; /* stream pointer */ |
55 |
|
int xres, yres; /* picture size */ |
56 |
|
int xloc, yloc; /* anchor point */ |
57 |
< |
int hasmin, hasmax; /* has threshold values */ |
58 |
< |
COLR thmin, thmax; /* thresholds */ |
57 |
> |
int flags; /* HASMIN, HASMAX */ |
58 |
> |
double thmin, thmax; /* thresholds */ |
59 |
|
} input[MAXFILE]; /* our input files */ |
60 |
|
|
61 |
|
int nfile; /* number of files */ |
62 |
|
|
63 |
+ |
int echoheader = 1; |
64 |
+ |
char ourfmt[MAXFMTLEN] = PICFMT; |
65 |
|
int wrongformat = 0; |
66 |
+ |
double common_expos = 1.; |
67 |
|
|
68 |
< |
FILE *popen(), *lblopen(); |
68 |
> |
double this_expos; |
69 |
|
|
70 |
+ |
static gethfunc headline; |
71 |
+ |
static void compos(void); |
72 |
+ |
static int cmpcolr(COLR c1, double lv2); |
73 |
+ |
static FILE * lblopen(char *s, int *xp, int *yp); |
74 |
|
|
75 |
< |
tabputs(s) /* print line preceded by a tab */ |
76 |
< |
char *s; |
75 |
> |
|
76 |
> |
|
77 |
> |
static int |
78 |
> |
headline( /* print line preceded by a tab */ |
79 |
> |
char *s, |
80 |
> |
void *p |
81 |
> |
) |
82 |
|
{ |
83 |
< |
char fmt[32]; |
83 |
> |
char fmt[MAXFMTLEN]; |
84 |
|
|
85 |
< |
if (isformat(s)) { |
86 |
< |
formatval(fmt, s); |
87 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
88 |
< |
} else { |
89 |
< |
putc('\t', stdout); |
85 |
> |
if (isheadid(s)) |
86 |
> |
return(0); |
87 |
> |
if (isexpos(s)) |
88 |
> |
this_expos *= exposval(s); |
89 |
> |
if (formatval(fmt, s)) { |
90 |
> |
if (globmatch(ourfmt, fmt)) { |
91 |
> |
wrongformat = 0; |
92 |
> |
strcpy(ourfmt, fmt); |
93 |
> |
} else |
94 |
> |
wrongformat = 1; |
95 |
> |
} else if (echoheader) { |
96 |
> |
fputc('\t', stdout); |
97 |
|
fputs(s, stdout); |
98 |
|
} |
99 |
+ |
return(0); |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
< |
main(argc, argv) |
104 |
< |
int argc; |
105 |
< |
char *argv[]; |
103 |
> |
int |
104 |
> |
main( |
105 |
> |
int argc, |
106 |
> |
char *argv[] |
107 |
> |
) |
108 |
|
{ |
109 |
|
int ncolumns = 0; |
110 |
|
int autolabel = 0; |
111 |
< |
int curcol = 0, curx = 0, cury = 0; |
111 |
> |
int curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0; |
112 |
> |
int xsgn, ysgn; |
113 |
|
char *thislabel; |
114 |
|
int an; |
115 |
< |
|
115 |
> |
SET_DEFAULT_BINARY(); |
116 |
> |
SET_FILE_BINARY(stdin); |
117 |
> |
SET_FILE_BINARY(stdout); |
118 |
|
progname = argv[0]; |
119 |
|
|
120 |
|
for (an = 1; an < argc && argv[an][0] == '-'; an++) |
121 |
|
switch (argv[an][1]) { |
122 |
+ |
case 'h': |
123 |
+ |
echoheader = !echoheader; |
124 |
+ |
break; |
125 |
|
case 'x': |
126 |
< |
xmax = xsiz = atoi(argv[++an]); |
126 |
> |
xsiz = atoi(argv[++an]); |
127 |
|
break; |
128 |
|
case 'y': |
129 |
< |
ymax = ysiz = atoi(argv[++an]); |
129 |
> |
ysiz = atoi(argv[++an]); |
130 |
|
break; |
131 |
|
case 'b': |
132 |
|
setcolr(bgcolr, atof(argv[an+1]), |
137 |
|
case 'a': |
138 |
|
ncolumns = atoi(argv[++an]); |
139 |
|
break; |
140 |
+ |
case 's': |
141 |
+ |
spacing = atoi(argv[++an]); |
142 |
+ |
break; |
143 |
+ |
case 'o': |
144 |
+ |
curx = x0 = atoi(argv[++an]); |
145 |
+ |
cury = atoi(argv[++an]); |
146 |
+ |
break; |
147 |
|
case 'l': |
148 |
|
switch (argv[an][2]) { |
149 |
|
case 'a': |
165 |
|
goto userr; |
166 |
|
} |
167 |
|
dofiles: |
168 |
+ |
newheader("RADIANCE", stdout); |
169 |
+ |
fputnow(stdout); |
170 |
|
for (nfile = 0; an < argc; nfile++) { |
171 |
|
if (nfile >= MAXFILE) |
172 |
|
goto toomany; |
173 |
|
thislabel = NULL; |
174 |
< |
input[nfile].hasmin = input[nfile].hasmax = 0; |
175 |
< |
while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+')) |
174 |
> |
input[nfile].flags = 0; |
175 |
> |
xsgn = ysgn = '-'; |
176 |
> |
while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+' |
177 |
> |
|| argv[an][0] == '=')) { |
178 |
|
switch (argv[an][1]) { |
179 |
|
case 't': |
180 |
|
checkthresh = 1; |
181 |
|
if (argv[an][0] == '-') { |
182 |
< |
input[nfile].hasmin = 1; |
183 |
< |
setcolr(input[nfile].thmin, |
184 |
< |
atof(argv[an+1]), |
185 |
< |
atof(argv[an+1]), |
186 |
< |
atof(argv[an+1])); |
187 |
< |
} else { |
188 |
< |
input[nfile].hasmax = 1; |
189 |
< |
setcolr(input[nfile].thmax, |
137 |
< |
atof(argv[an+1]), |
138 |
< |
atof(argv[an+1]), |
139 |
< |
atof(argv[an+1])); |
140 |
< |
} |
141 |
< |
an += 2; |
182 |
> |
input[nfile].flags |= HASMIN; |
183 |
> |
input[nfile].thmin = atof(argv[an+1]); |
184 |
> |
} else if (argv[an][0] == '+') { |
185 |
> |
input[nfile].flags |= HASMAX; |
186 |
> |
input[nfile].thmax = atof(argv[an+1]); |
187 |
> |
} else |
188 |
> |
goto userr; |
189 |
> |
an++; |
190 |
|
break; |
191 |
|
case 'l': |
192 |
|
if (strcmp(argv[an], "-l")) |
193 |
|
goto userr; |
194 |
< |
thislabel = argv[an+1]; |
147 |
< |
an += 2; |
194 |
> |
thislabel = argv[++an]; |
195 |
|
break; |
196 |
+ |
case '+': |
197 |
+ |
case '-': |
198 |
+ |
case '0': |
199 |
+ |
if (argv[an][0] != '=') |
200 |
+ |
goto userr; |
201 |
+ |
xsgn = argv[an][1]; |
202 |
+ |
ysgn = argv[an][2]; |
203 |
+ |
if (ysgn != '+' && ysgn != '-' && ysgn != '0') |
204 |
+ |
goto userr; |
205 |
+ |
break; |
206 |
|
case '\0': |
207 |
|
if (argv[an][0] == '-') |
208 |
|
goto getfile; |
210 |
|
default: |
211 |
|
goto userr; |
212 |
|
} |
213 |
+ |
an++; |
214 |
+ |
} |
215 |
|
getfile: |
216 |
|
if (argc-an < (ncolumns ? 1 : 3)) |
217 |
|
goto userr; |
218 |
|
if (autolabel && thislabel == NULL) |
219 |
|
thislabel = argv[an]; |
220 |
|
if (!strcmp(argv[an], "-")) { |
221 |
< |
input[nfile].name = "<stdin>"; |
221 |
> |
input[nfile].name = StandardInput; |
222 |
|
input[nfile].fp = stdin; |
223 |
|
} else { |
224 |
|
if (argv[an][0] == '!') { |
225 |
< |
input[nfile].name = "<Command>"; |
225 |
> |
input[nfile].name = Command; |
226 |
|
input[nfile].fp = popen(argv[an]+1, "r"); |
227 |
|
} else { |
228 |
|
input[nfile].name = argv[an]; |
235 |
|
} |
236 |
|
an++; |
237 |
|
/* get header */ |
238 |
< |
printf("%s:\n", input[nfile].name); |
239 |
< |
getheader(input[nfile].fp, tabputs, NULL); |
238 |
> |
if (echoheader) |
239 |
> |
printf("%s:\n", input[nfile].name); |
240 |
> |
this_expos = 1; |
241 |
> |
getheader(input[nfile].fp, headline, NULL); |
242 |
> |
if (!nfile) |
243 |
> |
common_expos = this_expos; |
244 |
> |
else if (common_expos > 0 && |
245 |
> |
fabs(this_expos/common_expos - 1.) > 0.02) |
246 |
> |
common_expos = 0; |
247 |
|
if (wrongformat) { |
248 |
< |
fprintf(stderr, "%s: not a Radiance picture\n", |
248 |
> |
fprintf(stderr, "%s: incompatible input format\n", |
249 |
|
input[nfile].name); |
250 |
|
quit(1); |
251 |
|
} |
256 |
|
input[nfile].name); |
257 |
|
quit(1); |
258 |
|
} |
259 |
< |
if (ncolumns > 0) { |
260 |
< |
if (curcol >= ncolumns) { |
261 |
< |
cury = ymax; |
262 |
< |
curx = 0; |
259 |
> |
if (ncolumns) { |
260 |
> |
if (curcol >= abs(ncolumns)) { |
261 |
> |
cury = ymax + spacing; |
262 |
> |
curx = x0; |
263 |
|
curcol = 0; |
264 |
|
} |
265 |
|
input[nfile].xloc = curx; |
266 |
|
input[nfile].yloc = cury; |
267 |
< |
curx += input[nfile].xres; |
267 |
> |
curx += input[nfile].xres + spacing; |
268 |
|
curcol++; |
269 |
|
} else { |
270 |
|
input[nfile].xloc = atoi(argv[an++]); |
271 |
+ |
if (xsgn == '+') |
272 |
+ |
input[nfile].xloc -= input[nfile].xres; |
273 |
+ |
else if (xsgn == '0') |
274 |
+ |
input[nfile].xloc -= input[nfile].xres/2; |
275 |
|
input[nfile].yloc = atoi(argv[an++]); |
276 |
+ |
if (ysgn == '+') |
277 |
+ |
input[nfile].yloc -= input[nfile].yres; |
278 |
+ |
else if (ysgn == '0') |
279 |
+ |
input[nfile].yloc -= input[nfile].yres/2; |
280 |
|
} |
281 |
|
if (input[nfile].xloc < xmin) |
282 |
|
xmin = input[nfile].xloc; |
289 |
|
if (thislabel != NULL) { |
290 |
|
if (++nfile >= MAXFILE) |
291 |
|
goto toomany; |
292 |
< |
input[nfile].name = "<Label>"; |
293 |
< |
input[nfile].hasmin = input[nfile].hasmax = 0; |
292 |
> |
input[nfile].name = Label; |
293 |
> |
input[nfile].flags = 0; |
294 |
|
input[nfile].xres = input[nfile-1].xres; |
295 |
|
input[nfile].yres = labelht; |
296 |
|
if ((input[nfile].fp = lblopen(thislabel, |
304 |
|
} |
305 |
|
if (xsiz <= 0) |
306 |
|
xsiz = xmax; |
307 |
+ |
else if (xsiz > xmax) |
308 |
+ |
xmax = xsiz; |
309 |
|
if (ysiz <= 0) |
310 |
|
ysiz = ymax; |
311 |
+ |
else if (ysiz > ymax) |
312 |
+ |
ymax = ysiz; |
313 |
+ |
if (ncolumns < 0) { /* reverse rows if requested */ |
314 |
+ |
int i = nfile; |
315 |
+ |
while (i--) |
316 |
+ |
input[i].yloc = ymax - input[i].yres - input[i].yloc; |
317 |
+ |
} |
318 |
|
/* add new header info. */ |
319 |
|
printargs(argc, argv, stdout); |
320 |
< |
fputformat(COLRFMT, stdout); |
320 |
> |
if (common_expos > 0) /* print exposure if shared */ |
321 |
> |
fputexpos(common_expos, stdout); |
322 |
> |
if (strcmp(ourfmt, PICFMT)) |
323 |
> |
fputformat(ourfmt, stdout); /* print format if known */ |
324 |
|
putchar('\n'); |
325 |
|
fprtresolu(xsiz, ysiz, stdout); |
326 |
|
|
328 |
|
|
329 |
|
quit(0); |
330 |
|
userr: |
331 |
< |
fprintf(stderr, "Usage: %s [-x xr][-y yr][-b r g b][-a n][-la][-lh h] ", |
331 |
> |
fprintf(stderr, |
332 |
> |
"Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ", |
333 |
|
progname); |
334 |
< |
fprintf(stderr, "[-t min1][+t max1][-l lab] pic1 x1 y1 ..\n"); |
334 |
> |
fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n"); |
335 |
|
quit(1); |
336 |
|
toomany: |
337 |
|
fprintf(stderr, "%s: only %d files and labels allowed\n", |
340 |
|
labelerr: |
341 |
|
fprintf(stderr, "%s: error opening label\n", progname); |
342 |
|
quit(1); |
343 |
+ |
return 1; /* pro forma return */ |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
< |
compos() /* composite pictures */ |
347 |
> |
static void |
348 |
> |
compos(void) /* composite pictures */ |
349 |
|
{ |
350 |
|
COLR *scanin, *scanout; |
351 |
|
int y; |
382 |
|
if (x > xsiz) |
383 |
|
x = xsiz; |
384 |
|
for (x--; x >= 0 && x >= input[i].xloc; x--) { |
385 |
< |
if (input[i].hasmin && |
385 |
> |
if (input[i].flags & HASMIN && |
386 |
|
cmpcolr(scanin[x], input[i].thmin) <= 0) |
387 |
|
continue; |
388 |
< |
if (input[i].hasmax && |
388 |
> |
if (input[i].flags & HASMAX && |
389 |
|
cmpcolr(scanin[x], input[i].thmax) >= 0) |
390 |
|
continue; |
391 |
|
copycolr(scanout[x], scanin[x]); |
399 |
|
quit(1); |
400 |
|
} |
401 |
|
} |
402 |
+ |
/* read remainders from streams */ |
403 |
+ |
for (i = 0; i < nfile; i++) |
404 |
+ |
if (input[i].name[0] == '<') |
405 |
+ |
while (getc(input[i].fp) != EOF) |
406 |
+ |
; |
407 |
|
return; |
408 |
|
memerr: |
409 |
|
perror(progname); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
< |
int |
415 |
< |
cmpcolr(c1, c2) /* compare two colr's (improvisation) */ |
416 |
< |
register COLR c1, c2; |
414 |
> |
static int |
415 |
> |
cmpcolr( /* compare COLR to luminance */ |
416 |
> |
register COLR c1, |
417 |
> |
double lv2 |
418 |
> |
) |
419 |
|
{ |
420 |
< |
register int i, j; |
421 |
< |
|
422 |
< |
j = 4; /* check exponents first! */ |
423 |
< |
while (j--) |
424 |
< |
if (i = c1[j] - c2[j]) |
425 |
< |
return(i); |
420 |
> |
double lv1 = .0; |
421 |
> |
|
422 |
> |
if (c1[EXP]) |
423 |
> |
lv1 = ldexp((double)normbright(c1), (int)c1[EXP]-(COLXS+8)); |
424 |
> |
if (lv1 < lv2) return(-1); |
425 |
> |
if (lv1 > lv2) return(1); |
426 |
|
return(0); |
427 |
|
} |
428 |
|
|
429 |
|
|
430 |
< |
FILE * |
431 |
< |
lblopen(s, xp, yp) /* open pipe to label generator */ |
432 |
< |
char *s; |
433 |
< |
int *xp, *yp; |
430 |
> |
static FILE * |
431 |
> |
lblopen( /* open pipe to label generator */ |
432 |
> |
char *s, |
433 |
> |
int *xp, |
434 |
> |
int *yp |
435 |
> |
) |
436 |
|
{ |
437 |
< |
char com[128]; |
437 |
> |
char com[PATH_MAX]; |
438 |
|
FILE *fp; |
439 |
|
|
440 |
|
sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s); |
451 |
|
} |
452 |
|
|
453 |
|
|
454 |
+ |
void |
455 |
|
quit(code) /* exit gracefully */ |
456 |
|
int code; |
457 |
|
{ |
458 |
< |
int status; |
459 |
< |
|
460 |
< |
if (code == 0) /* reap any children */ |
461 |
< |
while (wait(&status) != -1) |
462 |
< |
if (code == 0) |
463 |
< |
code = status>>8 & 0xff; |
458 |
> |
register int i; |
459 |
> |
/* close input files */ |
460 |
> |
for (i = 0; i < nfile; i++) |
461 |
> |
if (input[i].name == Command || input[i].name == Label) |
462 |
> |
pclose(input[i].fp); |
463 |
> |
else |
464 |
> |
fclose(input[i].fp); |
465 |
|
exit(code); |
466 |
|
} |