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