1 |
/* Copyright (c) 1987 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 16 |
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 checkthresh = 0; /* check threshold value */ |
32 |
|
33 |
char *progname; |
34 |
|
35 |
struct { |
36 |
char *name; /* file name */ |
37 |
FILE *fp; /* stream pointer */ |
38 |
int xres, yres; /* picture size */ |
39 |
int xloc, yloc; /* anchor point */ |
40 |
int hasmin, hasmax; /* has threshold values */ |
41 |
COLR thmin, thmax; /* thresholds */ |
42 |
} input[MAXFILE]; /* our input files */ |
43 |
|
44 |
int nfile; /* number of files */ |
45 |
|
46 |
|
47 |
tabputs(s) /* print line preceded by a tab */ |
48 |
char *s; |
49 |
{ |
50 |
putc('\t', stdout); |
51 |
fputs(s, stdout); |
52 |
} |
53 |
|
54 |
|
55 |
main(argc, argv) |
56 |
int argc; |
57 |
char *argv[]; |
58 |
{ |
59 |
double atof(); |
60 |
int an; |
61 |
|
62 |
progname = argv[0]; |
63 |
|
64 |
for (an = 1; an < argc && argv[an][0] == '-'; an++) |
65 |
switch (argv[an][1]) { |
66 |
case 'x': |
67 |
xmax = xsiz = atoi(argv[++an]); |
68 |
break; |
69 |
case 'y': |
70 |
ymax = ysiz = atoi(argv[++an]); |
71 |
break; |
72 |
case 'b': |
73 |
setcolr(bgcolr, atof(argv[an+1]), |
74 |
atof(argv[an+2]), |
75 |
atof(argv[an+3])); |
76 |
an += 3; |
77 |
break; |
78 |
case '\0': |
79 |
case 't': |
80 |
goto dofiles; |
81 |
default: |
82 |
goto userr; |
83 |
} |
84 |
dofiles: |
85 |
for (nfile = 0; an < argc; nfile++) { |
86 |
if (nfile >= MAXFILE) { |
87 |
fprintf(stderr, "%s: too many files\n", progname); |
88 |
quit(1); |
89 |
} |
90 |
input[nfile].hasmin = input[nfile].hasmax = 0; |
91 |
while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+')) |
92 |
switch (argv[an][1]) { |
93 |
case 't': |
94 |
checkthresh = 1; |
95 |
if (argv[an][0] == '-') { |
96 |
input[nfile].hasmin = 1; |
97 |
setcolr(input[nfile].thmin, |
98 |
atof(argv[an+1]), |
99 |
atof(argv[an+1]), |
100 |
atof(argv[an+1])); |
101 |
} else { |
102 |
input[nfile].hasmax = 1; |
103 |
setcolr(input[nfile].thmax, |
104 |
atof(argv[an+1]), |
105 |
atof(argv[an+1]), |
106 |
atof(argv[an+1])); |
107 |
} |
108 |
an += 2; |
109 |
break; |
110 |
case '\0': |
111 |
if (argv[an][0] == '-') |
112 |
goto getfile; |
113 |
/* fall through */ |
114 |
default: |
115 |
goto userr; |
116 |
} |
117 |
getfile: |
118 |
if (argc-an < 3) |
119 |
goto userr; |
120 |
if (!strcmp(argv[an], "-")) { |
121 |
input[nfile].name = "<stdin>"; |
122 |
input[nfile].fp = stdin; |
123 |
} else { |
124 |
input[nfile].name = argv[an]; |
125 |
if ((input[nfile].fp = fopen(argv[an], "r")) == NULL) { |
126 |
fprintf(stderr, "%s: cannot open\n", argv[an]); |
127 |
quit(1); |
128 |
} |
129 |
} |
130 |
an++; |
131 |
/* get header */ |
132 |
printf("%s:\n", input[nfile].name); |
133 |
getheader(input[nfile].fp, tabputs); |
134 |
/* get picture size */ |
135 |
if (fgetresolu(&input[nfile].xres, &input[nfile].yres, |
136 |
input[nfile].fp) != (YMAJOR|YDECR)) { |
137 |
fprintf(stderr, "%s: bad picture size\n", |
138 |
input[nfile].name); |
139 |
quit(1); |
140 |
} |
141 |
input[nfile].xloc = atoi(argv[an++]); |
142 |
input[nfile].yloc = atoi(argv[an++]); |
143 |
if (input[nfile].xloc < xmin) |
144 |
xmin = input[nfile].xloc; |
145 |
if (input[nfile].yloc < ymin) |
146 |
ymin = input[nfile].yloc; |
147 |
if (input[nfile].xloc+input[nfile].xres > xmax) |
148 |
xmax = input[nfile].xloc+input[nfile].xres; |
149 |
if (input[nfile].yloc+input[nfile].yres > ymax) |
150 |
ymax = input[nfile].yloc+input[nfile].yres; |
151 |
} |
152 |
if (xsiz <= 0) |
153 |
xsiz = xmax; |
154 |
if (ysiz <= 0) |
155 |
ysiz = ymax; |
156 |
/* add new header info. */ |
157 |
printargs(argc, argv, stdout); |
158 |
printf("\n-Y %d +X %d\n", ysiz, xsiz); |
159 |
|
160 |
compos(); |
161 |
|
162 |
quit(0); |
163 |
userr: |
164 |
fprintf(stderr, "Usage: %s [-x xres][-y yres][-b r g b] ", progname); |
165 |
fprintf(stderr, "[-t min1][+t max1] file1 x1 y1 ..\n"); |
166 |
quit(1); |
167 |
} |
168 |
|
169 |
|
170 |
compos() /* composite pictures */ |
171 |
{ |
172 |
COLR *scanin, *scanout; |
173 |
int y; |
174 |
register int x, i; |
175 |
|
176 |
scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)); |
177 |
if (scanin == NULL) |
178 |
goto memerr; |
179 |
scanin -= xmin; |
180 |
if (checkthresh) { |
181 |
scanout = (COLR *)malloc(xsiz*sizeof(COLR)); |
182 |
if (scanout == NULL) |
183 |
goto memerr; |
184 |
} else |
185 |
scanout = scanin; |
186 |
for (y = ymax-1; y >= 0; y--) { |
187 |
for (x = 0; x < xsiz; x++) |
188 |
copycolr(scanout[x], bgcolr); |
189 |
for (i = 0; i < nfile; i++) { |
190 |
if (input[i].yloc > y || |
191 |
input[i].yloc+input[i].yres <= y) |
192 |
continue; |
193 |
if (freadcolrs(scanin+input[i].xloc, |
194 |
input[i].xres, input[i].fp) < 0) { |
195 |
fprintf(stderr, "%s: read error (y==%d)\n", |
196 |
input[i].name, |
197 |
y-input[i].yloc); |
198 |
quit(1); |
199 |
} |
200 |
if (y >= ysiz) |
201 |
continue; |
202 |
if (checkthresh) { |
203 |
x = input[i].xloc+input[i].xres; |
204 |
if (x > xsiz) |
205 |
x = xsiz; |
206 |
for (x--; x >= 0 && x >= input[i].xloc; x--) { |
207 |
if (input[i].hasmin && |
208 |
cmpcolr(scanin[x], input[i].thmin) <= 0) |
209 |
continue; |
210 |
if (input[i].hasmax && |
211 |
cmpcolr(scanin[x], input[i].thmax) >= 0) |
212 |
continue; |
213 |
copycolr(scanout[x], scanin[x]); |
214 |
} |
215 |
} |
216 |
} |
217 |
if (y >= ysiz) |
218 |
continue; |
219 |
if (fwritecolrs(scanout, xsiz, stdout) < 0) { |
220 |
fprintf(stderr, "%s: write error\n", progname); |
221 |
quit(1); |
222 |
} |
223 |
} |
224 |
return; |
225 |
memerr: |
226 |
fprintf(stderr, "%s: out of memory\n", progname); |
227 |
quit(1); |
228 |
} |
229 |
|
230 |
|
231 |
int |
232 |
cmpcolr(c1, c2) /* compare two colr's (improvisation) */ |
233 |
register COLR c1, c2; |
234 |
{ |
235 |
register int i, j; |
236 |
|
237 |
j = 4; /* check exponents first! */ |
238 |
while (j--) |
239 |
if (i = c1[j] - c2[j]) |
240 |
return(i); |
241 |
return(0); |
242 |
} |
243 |
|
244 |
|
245 |
quit(code) /* exit gracefully */ |
246 |
int code; |
247 |
{ |
248 |
exit(code); |
249 |
} |