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