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