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