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