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 |
#define SIGNHT 24 |
21 |
#define setpscom(b,n) sprintf(b, "psign -h %d '%.30s'|pfilt -1 -x /2 -y /2",\ |
22 |
2*SIGNHT, n) |
23 |
|
24 |
/* output picture size */ |
25 |
int xsiz = 0; |
26 |
int ysiz = 0; |
27 |
/* input dimensions */ |
28 |
int xmin = 0; |
29 |
int ymin = 0; |
30 |
int xmax = 0; |
31 |
int ymax = 0; |
32 |
|
33 |
COLR bgcolr = BLKCOLR; /* background color */ |
34 |
|
35 |
int checkthresh = 0; /* check threshold value */ |
36 |
|
37 |
char *progname; |
38 |
|
39 |
struct { |
40 |
char *name; /* file or command name */ |
41 |
FILE *fp; /* stream pointer */ |
42 |
int xres, yres; /* picture size */ |
43 |
int xloc, yloc; /* anchor point */ |
44 |
int hasmin, hasmax; /* has threshold values */ |
45 |
COLR thmin, thmax; /* thresholds */ |
46 |
} input[MAXFILE]; /* our input files */ |
47 |
|
48 |
int nfile; /* number of files */ |
49 |
|
50 |
int wrongformat = 0; |
51 |
|
52 |
char tmpbuf[128]; |
53 |
|
54 |
FILE *popen(); |
55 |
|
56 |
|
57 |
tabputs(s) /* print line preceded by a tab */ |
58 |
char *s; |
59 |
{ |
60 |
char fmt[32]; |
61 |
|
62 |
if (isformat(s)) { |
63 |
formatval(fmt, s); |
64 |
wrongformat = strcmp(fmt, COLRFMT); |
65 |
} else { |
66 |
putc('\t', stdout); |
67 |
fputs(s, stdout); |
68 |
} |
69 |
} |
70 |
|
71 |
|
72 |
main(argc, argv) |
73 |
int argc; |
74 |
char *argv[]; |
75 |
{ |
76 |
double atof(); |
77 |
int ncolumns = 0; |
78 |
int dolabels = 0; |
79 |
int curcol = 0, curx = 0, cury = 0; |
80 |
int an; |
81 |
|
82 |
progname = argv[0]; |
83 |
|
84 |
for (an = 1; an < argc && argv[an][0] == '-'; an++) |
85 |
switch (argv[an][1]) { |
86 |
case 'x': |
87 |
xmax = xsiz = atoi(argv[++an]); |
88 |
break; |
89 |
case 'y': |
90 |
ymax = ysiz = atoi(argv[++an]); |
91 |
break; |
92 |
case 'b': |
93 |
setcolr(bgcolr, atof(argv[an+1]), |
94 |
atof(argv[an+2]), |
95 |
atof(argv[an+3])); |
96 |
an += 3; |
97 |
break; |
98 |
case 'a': |
99 |
ncolumns = atoi(argv[++an]); |
100 |
break; |
101 |
case 'l': |
102 |
dolabels++; |
103 |
break; |
104 |
case '\0': |
105 |
case 't': |
106 |
goto dofiles; |
107 |
default: |
108 |
goto userr; |
109 |
} |
110 |
dofiles: |
111 |
for (nfile = 0; an < argc; nfile++) { |
112 |
if (dolabels) { |
113 |
if (nfile >= MAXFILE-1) { |
114 |
fprintf(stderr, |
115 |
"%s: only %d files allowed with labels\n", |
116 |
progname, MAXFILE/2); |
117 |
} |
118 |
} else { |
119 |
if (nfile >= MAXFILE) { |
120 |
fprintf(stderr, "%s: only %d files allowed\n", |
121 |
progname, MAXFILE); |
122 |
quit(1); |
123 |
} |
124 |
} |
125 |
if (nfile >= (dolabels ? MAXFILE-1 : MAXFILE)) { |
126 |
fprintf(stderr, "%s: too many files\n", progname); |
127 |
quit(1); |
128 |
} |
129 |
input[nfile].hasmin = input[nfile].hasmax = 0; |
130 |
while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+')) |
131 |
switch (argv[an][1]) { |
132 |
case 't': |
133 |
checkthresh = 1; |
134 |
if (argv[an][0] == '-') { |
135 |
input[nfile].hasmin = 1; |
136 |
setcolr(input[nfile].thmin, |
137 |
atof(argv[an+1]), |
138 |
atof(argv[an+1]), |
139 |
atof(argv[an+1])); |
140 |
} else { |
141 |
input[nfile].hasmax = 1; |
142 |
setcolr(input[nfile].thmax, |
143 |
atof(argv[an+1]), |
144 |
atof(argv[an+1]), |
145 |
atof(argv[an+1])); |
146 |
} |
147 |
an += 2; |
148 |
break; |
149 |
case '\0': |
150 |
if (argv[an][0] == '-') |
151 |
goto getfile; |
152 |
/* fall through */ |
153 |
default: |
154 |
goto userr; |
155 |
} |
156 |
getfile: |
157 |
if (argc-an < (ncolumns ? 1 : 3)) |
158 |
goto userr; |
159 |
if (!strcmp(argv[an], "-")) { |
160 |
input[nfile].name = "<stdin>"; |
161 |
input[nfile].fp = stdin; |
162 |
} else { |
163 |
input[nfile].name = argv[an]; |
164 |
if ((input[nfile].fp = argv[an][0] == '!' ? |
165 |
popen(argv[an]+1, "r") : |
166 |
fopen(argv[an], "r")) == NULL) { |
167 |
perror(argv[an]); |
168 |
quit(1); |
169 |
} |
170 |
} |
171 |
an++; |
172 |
/* get header */ |
173 |
printf("%s:\n", input[nfile].name); |
174 |
getheader(input[nfile].fp, tabputs, NULL); |
175 |
if (wrongformat) { |
176 |
fprintf(stderr, "%s: not a Radiance picture\n", |
177 |
input[nfile].name); |
178 |
quit(1); |
179 |
} |
180 |
/* get picture size */ |
181 |
if (fgetresolu(&input[nfile].xres, &input[nfile].yres, |
182 |
input[nfile].fp) != (YMAJOR|YDECR)) { |
183 |
fprintf(stderr, "%s: bad picture size\n", |
184 |
input[nfile].name); |
185 |
quit(1); |
186 |
} |
187 |
if (ncolumns > 0) { |
188 |
if (curcol >= ncolumns) { |
189 |
cury = ymax; |
190 |
curx = 0; |
191 |
curcol = 0; |
192 |
} |
193 |
input[nfile].xloc = curx; |
194 |
input[nfile].yloc = cury; |
195 |
curx += input[nfile].xres; |
196 |
curcol++; |
197 |
} else { |
198 |
input[nfile].xloc = atoi(argv[an++]); |
199 |
input[nfile].yloc = atoi(argv[an++]); |
200 |
} |
201 |
if (input[nfile].xloc < xmin) |
202 |
xmin = input[nfile].xloc; |
203 |
if (input[nfile].yloc < ymin) |
204 |
ymin = input[nfile].yloc; |
205 |
if (input[nfile].xloc+input[nfile].xres > xmax) |
206 |
xmax = input[nfile].xloc+input[nfile].xres; |
207 |
if (input[nfile].yloc+input[nfile].yres > ymax) |
208 |
ymax = input[nfile].yloc+input[nfile].yres; |
209 |
if (dolabels) { |
210 |
input[++nfile].name = "<Label>"; |
211 |
setpscom(tmpbuf, input[nfile-1].name); |
212 |
if ((input[nfile].fp = popen(tmpbuf, "r")) == NULL) |
213 |
goto labelerr; |
214 |
if (checkheader(input[nfile].fp, COLRFMT, NULL) < 0) |
215 |
goto labelerr; |
216 |
if (fgetresolu(&input[nfile].xres, &input[nfile].yres, |
217 |
input[nfile].fp) != (YMAJOR|YDECR)) |
218 |
goto labelerr; |
219 |
input[nfile].xloc = input[nfile-1].xloc; |
220 |
input[nfile].yloc = input[nfile-1].yloc + |
221 |
input[nfile-1].yres - SIGNHT; |
222 |
input[nfile].hasmin = input[nfile].hasmax = 0; |
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 xres][-y yres][-b r g b] ", progname); |
239 |
fprintf(stderr, "[-t min1][+t max1] file1 x1 y1 ..\n"); |
240 |
quit(1); |
241 |
labelerr: |
242 |
fprintf(stderr, "%s: error opening label\n", progname); |
243 |
quit(1); |
244 |
} |
245 |
|
246 |
|
247 |
compos() /* composite pictures */ |
248 |
{ |
249 |
COLR *scanin, *scanout; |
250 |
int y; |
251 |
register int x, i; |
252 |
|
253 |
scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)); |
254 |
if (scanin == NULL) |
255 |
goto memerr; |
256 |
scanin -= xmin; |
257 |
if (checkthresh) { |
258 |
scanout = (COLR *)malloc(xsiz*sizeof(COLR)); |
259 |
if (scanout == NULL) |
260 |
goto memerr; |
261 |
} else |
262 |
scanout = scanin; |
263 |
for (y = ymax-1; y >= 0; y--) { |
264 |
for (x = 0; x < xsiz; x++) |
265 |
copycolr(scanout[x], bgcolr); |
266 |
for (i = 0; i < nfile; i++) { |
267 |
if (input[i].yloc > y || |
268 |
input[i].yloc+input[i].yres <= y) |
269 |
continue; |
270 |
if (freadcolrs(scanin+input[i].xloc, |
271 |
input[i].xres, input[i].fp) < 0) { |
272 |
fprintf(stderr, "%s: read error (y==%d)\n", |
273 |
input[i].name, |
274 |
y-input[i].yloc); |
275 |
quit(1); |
276 |
} |
277 |
if (y >= ysiz) |
278 |
continue; |
279 |
if (checkthresh) { |
280 |
x = input[i].xloc+input[i].xres; |
281 |
if (x > xsiz) |
282 |
x = xsiz; |
283 |
for (x--; x >= 0 && x >= input[i].xloc; x--) { |
284 |
if (input[i].hasmin && |
285 |
cmpcolr(scanin[x], input[i].thmin) <= 0) |
286 |
continue; |
287 |
if (input[i].hasmax && |
288 |
cmpcolr(scanin[x], input[i].thmax) >= 0) |
289 |
continue; |
290 |
copycolr(scanout[x], scanin[x]); |
291 |
} |
292 |
} |
293 |
} |
294 |
if (y >= ysiz) |
295 |
continue; |
296 |
if (fwritecolrs(scanout, xsiz, stdout) < 0) { |
297 |
perror(progname); |
298 |
quit(1); |
299 |
} |
300 |
} |
301 |
return; |
302 |
memerr: |
303 |
perror(progname); |
304 |
quit(1); |
305 |
} |
306 |
|
307 |
|
308 |
int |
309 |
cmpcolr(c1, c2) /* compare two colr's (improvisation) */ |
310 |
register COLR c1, c2; |
311 |
{ |
312 |
register int i, j; |
313 |
|
314 |
j = 4; /* check exponents first! */ |
315 |
while (j--) |
316 |
if (i = c1[j] - c2[j]) |
317 |
return(i); |
318 |
return(0); |
319 |
} |
320 |
|
321 |
|
322 |
quit(code) /* exit gracefully */ |
323 |
int code; |
324 |
{ |
325 |
exit(code); |
326 |
} |