1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* pfilt.c - program to post-process picture file. |
9 |
* |
10 |
* 9/26/85 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include <signal.h> |
16 |
|
17 |
#include "color.h" |
18 |
|
19 |
|
20 |
extern char *malloc(); |
21 |
|
22 |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
23 |
|
24 |
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
25 |
|
26 |
double rad = 0.0; /* output pixel radius for filtering */ |
27 |
|
28 |
int nrows = 0; /* number of rows for output */ |
29 |
int ncols = 0; /* number of columns for output */ |
30 |
|
31 |
int singlepass = 0; /* true means skip first pass */ |
32 |
|
33 |
int avghot = 0; /* true means average in bright spots */ |
34 |
|
35 |
double hotlvl = 1000.0; /* level considered "hot" */ |
36 |
|
37 |
int npts = 0; /* (half) number of points for stars */ |
38 |
|
39 |
double spread = 1e-4; /* spread for star points */ |
40 |
|
41 |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
42 |
|
43 |
char *tfname = NULL; |
44 |
|
45 |
int xres, yres; /* resolution of input */ |
46 |
|
47 |
double x_c, y_r; /* conversion factors */ |
48 |
|
49 |
int xrad; /* x window size */ |
50 |
int yrad; /* y window size */ |
51 |
|
52 |
int barsize; /* size of input scan bar */ |
53 |
COLOR **scanin; /* input scan bar */ |
54 |
COLOR *scanout; /* output scan line */ |
55 |
|
56 |
char *progname; |
57 |
|
58 |
|
59 |
main(argc, argv) |
60 |
int argc; |
61 |
char **argv; |
62 |
{ |
63 |
extern char *mktemp(); |
64 |
extern double atof(), pow(); |
65 |
extern long ftell(); |
66 |
extern int quit(); |
67 |
FILE *fin; |
68 |
long fpos; |
69 |
double d; |
70 |
int i; |
71 |
|
72 |
if (signal(SIGINT, quit) == SIG_IGN) |
73 |
signal(SIGINT, SIG_IGN); |
74 |
if (signal(SIGHUP, quit) == SIG_IGN) |
75 |
signal(SIGINT, SIG_IGN); |
76 |
signal(SIGTERM, quit); |
77 |
signal(SIGPIPE, quit); |
78 |
#ifdef SIGXCPU |
79 |
signal(SIGXCPU, quit); |
80 |
signal(SIGXFSZ, quit); |
81 |
#endif |
82 |
|
83 |
progname = argv[0]; |
84 |
|
85 |
for (i = 1; i < argc; i++) |
86 |
if (argv[i][0] == '-') |
87 |
switch (argv[i][1]) { |
88 |
case 'x': |
89 |
ncols = atoi(argv[++i]); |
90 |
break; |
91 |
case 'y': |
92 |
nrows = atoi(argv[++i]); |
93 |
break; |
94 |
case 'e': |
95 |
if (argv[i+1][0] == '+' || argv[i+1][0] == '-') |
96 |
d = pow(2.0, atof(argv[i+1])); |
97 |
else |
98 |
d = atof(argv[i+1]); |
99 |
switch (argv[i][2]) { |
100 |
case '\0': |
101 |
scalecolor(exposure, d); |
102 |
break; |
103 |
case 'r': |
104 |
colval(exposure,RED) *= d; |
105 |
break; |
106 |
case 'g': |
107 |
colval(exposure,GRN) *= d; |
108 |
break; |
109 |
case 'b': |
110 |
colval(exposure,BLU) *= d; |
111 |
break; |
112 |
default: |
113 |
goto badopt; |
114 |
} |
115 |
i++; |
116 |
break; |
117 |
case '1': |
118 |
singlepass = 1; |
119 |
break; |
120 |
case 'p': |
121 |
npts = atoi(argv[++i]) / 2; |
122 |
break; |
123 |
case 's': |
124 |
spread = atof(argv[++i]); |
125 |
break; |
126 |
case 'a': |
127 |
avghot = !avghot; |
128 |
break; |
129 |
case 'h': |
130 |
hotlvl = atof(argv[++i]); |
131 |
break; |
132 |
case 'r': |
133 |
rad = atof(argv[++i]); |
134 |
break; |
135 |
case 'b': |
136 |
rad = 0.0; |
137 |
break; |
138 |
default:; |
139 |
badopt: |
140 |
fprintf(stderr, "%s: unknown option: %s\n", |
141 |
progname, argv[i]); |
142 |
quit(1); |
143 |
break; |
144 |
} |
145 |
else |
146 |
break; |
147 |
|
148 |
if (i == argc) { |
149 |
if (singlepass) |
150 |
fin = stdin; |
151 |
else { |
152 |
tfname = mktemp(TEMPLATE); |
153 |
if ((fin = fopen(tfname, "w+")) == NULL) { |
154 |
fprintf(stderr, "%s: can't create ", progname); |
155 |
fprintf(stderr, "temp file \"%s\"\n", tfname); |
156 |
quit(1); |
157 |
} |
158 |
copyfile(stdin, fin); |
159 |
if (fseek(fin, 0L, 0) == -1) { |
160 |
fprintf(stderr, "%s: seek fail\n", progname); |
161 |
quit(1); |
162 |
} |
163 |
} |
164 |
} else if (i == argc-1) { |
165 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
166 |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
167 |
progname, argv[i]); |
168 |
quit(1); |
169 |
} |
170 |
} else { |
171 |
fprintf(stderr, "%s: bad # file arguments\n", progname); |
172 |
quit(1); |
173 |
} |
174 |
/* copy header */ |
175 |
copyheader(fin, stdout); |
176 |
/* add new header info. */ |
177 |
printargs(i, argv, stdout); |
178 |
/* get picture size */ |
179 |
if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) { |
180 |
fprintf(stderr, "%s: bad picture size\n", progname); |
181 |
quit(1); |
182 |
} |
183 |
if (ncols <= 0) |
184 |
ncols = xres; |
185 |
if (nrows <= 0) |
186 |
nrows = yres; |
187 |
|
188 |
if (singlepass) { |
189 |
/* skip exposure, etc. */ |
190 |
pass1default(); |
191 |
pass2(fin); |
192 |
quit(0); |
193 |
} |
194 |
|
195 |
fpos = ftell(fin); /* save input file position */ |
196 |
|
197 |
pass1(fin); |
198 |
|
199 |
if (fseek(fin, fpos, 0) == -1) { |
200 |
fprintf(stderr, "%s: seek fail\n", progname); |
201 |
quit(1); |
202 |
} |
203 |
pass2(fin); |
204 |
|
205 |
quit(0); |
206 |
} |
207 |
|
208 |
|
209 |
copyfile(in, out) /* copy a file */ |
210 |
register FILE *in, *out; |
211 |
{ |
212 |
register int c; |
213 |
|
214 |
while ((c = getc(in)) != EOF) |
215 |
putc(c, out); |
216 |
|
217 |
if (ferror(out)) { |
218 |
fprintf(stderr, "%s: write error in copyfile\n", progname); |
219 |
quit(1); |
220 |
} |
221 |
} |
222 |
|
223 |
|
224 |
pass1(in) /* first pass of picture file */ |
225 |
FILE *in; |
226 |
{ |
227 |
int i; |
228 |
COLOR *scan; |
229 |
|
230 |
pass1init(); |
231 |
|
232 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
233 |
if (scan == NULL) { |
234 |
fprintf(stderr, "%s: out of memory\n", progname); |
235 |
quit(1); |
236 |
} |
237 |
for (i = 0; i < yres; i++) { |
238 |
if (freadscan(scan, xres, in) < 0) { |
239 |
nrows = nrows * i / yres; /* adjust frame */ |
240 |
if (nrows <= 0) { |
241 |
fprintf(stderr, "%s: empty frame\n", progname); |
242 |
quit(1); |
243 |
} |
244 |
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
245 |
progname, 100*i/yres); |
246 |
yres = i; |
247 |
break; |
248 |
} |
249 |
pass1scan(scan, i); |
250 |
} |
251 |
free((char *)scan); |
252 |
} |
253 |
|
254 |
|
255 |
pass2(in) /* last pass on file, write to stdout */ |
256 |
FILE *in; |
257 |
{ |
258 |
int yread; |
259 |
int ycent, xcent; |
260 |
int r, c; |
261 |
|
262 |
pass2init(); |
263 |
scan2init(); |
264 |
yread = 0; |
265 |
for (r = 0; r < nrows; r++) { |
266 |
ycent = (long)r*yres/nrows; |
267 |
while (yread <= ycent+yrad) { |
268 |
if (yread < yres) { |
269 |
if (freadscan(scanin[yread%barsize], |
270 |
xres, in) < 0) { |
271 |
fprintf(stderr, |
272 |
"%s: bad read (y=%d)\n", |
273 |
progname, yres-1-yread); |
274 |
quit(1); |
275 |
} |
276 |
pass2scan(scanin[yread%barsize], yread); |
277 |
} |
278 |
yread++; |
279 |
} |
280 |
for (c = 0; c < ncols; c++) { |
281 |
xcent = (long)c*xres/ncols; |
282 |
if (rad <= 0.0) |
283 |
dobox(scanout[c], xcent, ycent, c, r); |
284 |
else |
285 |
dogauss(scanout[c], xcent, ycent, c, r); |
286 |
} |
287 |
if (fwritescan(scanout, ncols, stdout) < 0) { |
288 |
fprintf(stderr, "%s: write error in pass2\n", progname); |
289 |
quit(1); |
290 |
} |
291 |
} |
292 |
} |
293 |
|
294 |
|
295 |
scan2init() /* prepare scanline arrays */ |
296 |
{ |
297 |
double e; |
298 |
register int i; |
299 |
|
300 |
x_c = (double)ncols/xres; |
301 |
y_r = (double)nrows/yres; |
302 |
|
303 |
if (rad <= 0.0) { |
304 |
xrad = xres/ncols/2 + 1; |
305 |
yrad = yres/nrows/2 + 1; |
306 |
} else { |
307 |
if (nrows >= yres && ncols >= xres) |
308 |
rad *= (y_r + x_c)/2.0; |
309 |
|
310 |
xrad = CHECKRAD*rad/x_c + 1; |
311 |
yrad = CHECKRAD*rad/y_r + 1; |
312 |
|
313 |
initmask(); /* initialize filter table */ |
314 |
} |
315 |
barsize = 2 * yrad; |
316 |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
317 |
for (i = 0; i < barsize; i++) { |
318 |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
319 |
if (scanin[i] == NULL) { |
320 |
fprintf(stderr, "%s: out of memory\n", progname); |
321 |
quit(1); |
322 |
} |
323 |
} |
324 |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
325 |
if (scanout == NULL) { |
326 |
fprintf(stderr, "%s: out of memory\n", progname); |
327 |
quit(1); |
328 |
} |
329 |
e = bright(exposure); |
330 |
if (e < 1-1e-7 || e > 1+1e-7) /* record exposure */ |
331 |
printf("EXPOSURE=%e\n", e); |
332 |
printf("\n"); |
333 |
printf("-Y %d +X %d\n", nrows, ncols); /* write picture size */ |
334 |
} |
335 |
|
336 |
|
337 |
quit(code) /* remove temporary file and exit */ |
338 |
int code; |
339 |
{ |
340 |
if (tfname != NULL) |
341 |
unlink(tfname); |
342 |
exit(code); |
343 |
} |