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