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