1 |
greg |
1.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 (fscanf(input[nfile].fp, "-Y %d +X %d\n", |
136 |
|
|
&input[nfile].yres, &input[nfile].xres) != 2) { |
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)) - xmin; |
177 |
|
|
scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)) - xmin; |
178 |
|
|
if (scanin == NULL) |
179 |
|
|
goto memerr; |
180 |
|
|
scanin -= xmin; |
181 |
|
|
if (checkthresh) { |
182 |
|
|
scanout = (COLR *)malloc(xsiz*sizeof(COLR)); |
183 |
|
|
if (scanout == NULL) |
184 |
|
|
goto memerr; |
185 |
|
|
} else |
186 |
|
|
scanout = scanin; |
187 |
|
|
for (y = ymax-1; y >= 0; y--) { |
188 |
|
|
for (x = 0; x < xsiz; x++) |
189 |
|
|
copycolr(scanout[x], bgcolr); |
190 |
|
|
for (i = 0; i < nfile; i++) { |
191 |
|
|
if (input[i].yloc > y || |
192 |
|
|
input[i].yloc+input[i].yres <= y) |
193 |
|
|
continue; |
194 |
|
|
if (freadcolrs(scanin+input[i].xloc, |
195 |
|
|
input[i].xres, input[i].fp) < 0) { |
196 |
greg |
1.2 |
fprintf(stderr, "%s: read error (y==%d)\n", |
197 |
|
|
input[i].name, |
198 |
|
|
y-input[i].yloc); |
199 |
greg |
1.1 |
quit(1); |
200 |
|
|
} |
201 |
|
|
if (y >= ysiz) |
202 |
|
|
continue; |
203 |
|
|
if (checkthresh) { |
204 |
|
|
x = input[i].xloc+input[i].xres; |
205 |
|
|
if (x > xsiz) |
206 |
|
|
x = xsiz; |
207 |
|
|
for (x--; x >= 0 && x >= input[i].xloc; x--) { |
208 |
|
|
if (input[i].hasmin && |
209 |
|
|
cmpcolr(scanin[x], input[i].thmin) <= 0) |
210 |
|
|
continue; |
211 |
|
|
if (input[i].hasmax && |
212 |
|
|
cmpcolr(scanin[x], input[i].thmax) >= 0) |
213 |
|
|
continue; |
214 |
|
|
copycolr(scanout[x], scanin[x]); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
if (y >= ysiz) |
219 |
|
|
continue; |
220 |
|
|
if (fwritecolrs(scanout, xsiz, stdout) < 0) { |
221 |
|
|
fprintf(stderr, "%s: write error\n", progname); |
222 |
|
|
quit(1); |
223 |
|
|
} |
224 |
|
|
} |
225 |
|
|
return; |
226 |
|
|
memerr: |
227 |
|
|
fprintf(stderr, "%s: out of memory\n", progname); |
228 |
|
|
quit(1); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
|
232 |
|
|
int |
233 |
|
|
cmpcolr(c1, c2) /* compare two colr's (improvisation) */ |
234 |
|
|
register COLR c1, c2; |
235 |
|
|
{ |
236 |
|
|
register int i, j; |
237 |
|
|
|
238 |
|
|
j = 4; /* check exponents first! */ |
239 |
|
|
while (j--) |
240 |
|
|
if (i = c1[j] - c2[j]) |
241 |
|
|
return(i); |
242 |
|
|
return(0); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
|
246 |
|
|
quit(code) /* exit gracefully */ |
247 |
|
|
int code; |
248 |
|
|
{ |
249 |
|
|
exit(code); |
250 |
|
|
} |