1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
8 |
|
* pfilt.c - program to post-process picture file. |
9 |
|
* |
10 |
|
* 9/26/85 |
11 |
+ |
* 6/23/93 Added additional buffers for value spreading |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include <stdio.h> |
14 |
> |
#include "standard.h" |
15 |
|
|
16 |
|
#include <signal.h> |
17 |
|
|
18 |
|
#include "color.h" |
19 |
|
|
20 |
< |
extern char *malloc(); |
20 |
> |
#include "resolu.h" |
21 |
> |
|
22 |
> |
#include "paths.h" |
23 |
> |
|
24 |
|
extern float *matchlamp(); |
25 |
|
|
26 |
< |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
26 |
> |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
27 |
|
|
28 |
< |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
28 |
> |
double CHECKRAD = 1.5; /* radius to check for filtering */ |
29 |
|
|
30 |
+ |
#define THRESHRAD 5.0 /* maximum sample spread in output */ |
31 |
+ |
|
32 |
|
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
33 |
|
|
34 |
< |
double rad = 0.0; /* output pixel radius for filtering */ |
34 |
> |
double rad = 0.0; /* output pixel radius for filtering */ |
35 |
|
|
36 |
+ |
double thresh = 0.0; /* maximum contribution for subpixel */ |
37 |
+ |
|
38 |
|
int nrows = 0; /* number of rows for output */ |
39 |
|
int ncols = 0; /* number of columns for output */ |
40 |
|
|
41 |
< |
double x_c = 1.0; /* ratio of output x size to input */ |
42 |
< |
double y_r = 1.0; /* ratio of output y size to input */ |
41 |
> |
double x_c = 1.0; /* ratio of output x size to input */ |
42 |
> |
double y_r = 1.0; /* ratio of output y size to input */ |
43 |
|
|
44 |
|
int singlepass = 0; /* true means skip first pass */ |
45 |
|
|
46 |
|
int avghot = 0; /* true means average in bright spots */ |
47 |
|
|
48 |
< |
double hotlvl = 1000.0; /* level considered "hot" */ |
48 |
> |
double hotlvl = 100.0; /* level considered "hot" */ |
49 |
|
|
50 |
|
int npts = 0; /* (half) number of points for stars */ |
51 |
|
|
52 |
< |
double spread = 1e-4; /* spread for star points */ |
52 |
> |
double spread = 1e-4; /* spread for star points */ |
53 |
|
|
46 |
– |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
47 |
– |
|
54 |
|
char *tfname = NULL; |
55 |
|
|
56 |
|
char *lampdat = "lamp.tab"; /* lamp data file */ |
57 |
|
|
58 |
+ |
int order; /* scanline ordering of input */ |
59 |
|
int xres, yres; /* resolution of input */ |
60 |
< |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
60 |
> |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
61 |
|
int correctaspect = 0; /* aspect ratio correction? */ |
62 |
|
|
63 |
|
int wrongformat = 0; |
64 |
|
|
65 |
< |
int xrad; /* x window size */ |
66 |
< |
int yrad; /* y window size */ |
65 |
> |
int xrad; /* x search radius */ |
66 |
> |
int yrad; /* y search radius */ |
67 |
> |
int xbrad; /* x box size */ |
68 |
> |
int ybrad; /* y box size */ |
69 |
|
|
70 |
|
int barsize; /* size of input scan bar */ |
71 |
|
COLOR **scanin; /* input scan bar */ |
72 |
|
COLOR *scanout; /* output scan line */ |
73 |
+ |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
74 |
+ |
float **greybar; /* grey-averaged input values */ |
75 |
+ |
int obarsize = 0; /* size of output scan bar */ |
76 |
+ |
int orad = 0; /* output window radius */ |
77 |
|
|
78 |
|
char *progname; |
79 |
|
|
82 |
|
int argc; |
83 |
|
char **argv; |
84 |
|
{ |
72 |
– |
extern char *mktemp(); |
73 |
– |
extern double atof(), pow(); |
85 |
|
extern long ftell(); |
86 |
|
extern int quit(), headline(); |
87 |
|
FILE *fin; |
88 |
|
float *lampcolor; |
89 |
|
char *lamptype = NULL; |
90 |
|
long fpos; |
91 |
< |
double outaspect = 0.0; |
92 |
< |
double d; |
93 |
< |
int i; |
94 |
< |
|
91 |
> |
double outaspect = 0.0; |
92 |
> |
double d; |
93 |
> |
int i, j; |
94 |
> |
#ifdef MSDOS |
95 |
> |
extern int _fmode; |
96 |
> |
_fmode = O_BINARY; |
97 |
> |
setmode(fileno(stdin), O_BINARY); |
98 |
> |
setmode(fileno(stdout), O_BINARY); |
99 |
> |
#endif |
100 |
|
if (signal(SIGINT, quit) == SIG_IGN) |
101 |
|
signal(SIGINT, SIG_IGN); |
102 |
|
if (signal(SIGHUP, quit) == SIG_IGN) |
103 |
|
signal(SIGINT, SIG_IGN); |
104 |
|
signal(SIGTERM, quit); |
105 |
|
signal(SIGPIPE, quit); |
106 |
< |
#ifdef SIGXCPU |
106 |
> |
#ifdef SIGXCPU |
107 |
|
signal(SIGXCPU, quit); |
108 |
|
signal(SIGXFSZ, quit); |
109 |
|
#endif |
110 |
|
|
111 |
< |
progname = argv[0]; |
111 |
> |
progname = argv[0] = fixargv0(argv[0]); |
112 |
|
|
113 |
|
for (i = 1; i < argc; i++) |
114 |
|
if (argv[i][0] == '-') |
192 |
|
case 'r': |
193 |
|
rad = atof(argv[++i]); |
194 |
|
break; |
195 |
+ |
case 'm': |
196 |
+ |
thresh = atof(argv[++i]); |
197 |
+ |
if (rad <= FTINY) |
198 |
+ |
rad = 1.0; |
199 |
+ |
break; |
200 |
|
case 'b': |
201 |
< |
rad = 0.0; |
201 |
> |
rad = thresh = 0.0; |
202 |
|
break; |
203 |
|
default:; |
204 |
|
badopt: |
217 |
|
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
218 |
|
quit(1); |
219 |
|
} |
220 |
< |
colval(exposure,RED) /= lampcolor[0]; |
221 |
< |
colval(exposure,GRN) /= lampcolor[1]; |
222 |
< |
colval(exposure,BLU) /= lampcolor[2]; |
220 |
> |
for (i = 0; i < 3; i++) |
221 |
> |
if (lampcolor[i] > 1e-4) |
222 |
> |
colval(exposure,i) /= lampcolor[i]; |
223 |
|
freelamps(); |
224 |
|
} |
225 |
|
/* open input file */ |
259 |
|
/* add new header info. */ |
260 |
|
printargs(i, argv, stdout); |
261 |
|
/* get picture size */ |
262 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
262 |
> |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
263 |
|
fprintf(stderr, "%s: bad picture size\n", progname); |
264 |
|
quit(1); |
265 |
|
} |
266 |
+ |
if (!(order & YMAJOR)) |
267 |
+ |
inpaspect = 1.0/inpaspect; |
268 |
|
/* compute output resolution */ |
269 |
|
if (ncols <= 0) |
270 |
|
ncols = x_c*xres + .5; |
308 |
|
fputs(s, stdout); /* copy to output */ |
309 |
|
if (isaspect(s)) /* get aspect ratio */ |
310 |
|
inpaspect *= aspectval(s); |
311 |
+ |
else if (isexpos(s)) |
312 |
+ |
hotlvl *= exposval(s); |
313 |
|
else if (isformat(s)) { |
314 |
|
formatval(fmt, s); |
315 |
|
wrongformat = strcmp(fmt, COLRFMT); |
347 |
|
} |
348 |
|
for (i = 0; i < yres; i++) { |
349 |
|
if (freadscan(scan, xres, in) < 0) { |
350 |
< |
nrows = nrows * i / yres; /* adjust frame */ |
350 |
> |
nrows = (long)nrows * i / yres; /* adjust frame */ |
351 |
|
if (nrows <= 0) { |
352 |
|
fprintf(stderr, "%s: empty frame\n", progname); |
353 |
|
quit(1); |
354 |
|
} |
355 |
|
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
356 |
< |
progname, 100*i/yres); |
356 |
> |
progname, (int)(100L*i/yres)); |
357 |
|
yres = i; |
358 |
|
y_r = (double)nrows/yres; |
359 |
|
break; |
370 |
|
int yread; |
371 |
|
int ycent, xcent; |
372 |
|
int r, c; |
373 |
< |
|
373 |
> |
|
374 |
|
pass2init(); |
375 |
|
scan2init(); |
376 |
|
yread = 0; |
389 |
|
} |
390 |
|
yread++; |
391 |
|
} |
392 |
+ |
if (obarsize > 0) |
393 |
+ |
scan2sync(r); |
394 |
|
for (c = 0; c < ncols; c++) { |
395 |
|
xcent = (long)c*xres/ncols; |
396 |
< |
if (rad <= 0.0) |
397 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
398 |
< |
else |
396 |
> |
if (thresh > FTINY) |
397 |
> |
dothresh(xcent, ycent, c, r); |
398 |
> |
else if (rad > FTINY) |
399 |
|
dogauss(scanout[c], xcent, ycent, c, r); |
400 |
+ |
else |
401 |
+ |
dobox(scanout[c], xcent, ycent, c, r); |
402 |
|
} |
403 |
< |
if (fwritescan(scanout, ncols, stdout) < 0) { |
403 |
> |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
404 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
405 |
|
quit(1); |
406 |
|
} |
407 |
|
} |
408 |
< |
/* skip leftovers */ |
408 |
> |
/* skip leftover input */ |
409 |
|
while (yread < yres) { |
410 |
|
if (freadscan(scanin[0], xres, in) < 0) |
411 |
|
break; |
412 |
|
yread++; |
413 |
|
} |
414 |
+ |
scan2flush(); /* flush output */ |
415 |
|
} |
416 |
|
|
417 |
|
|
418 |
|
scan2init() /* prepare scanline arrays */ |
419 |
|
{ |
420 |
|
COLOR ctmp; |
421 |
< |
double d; |
421 |
> |
double d; |
422 |
|
register int i; |
423 |
|
|
424 |
< |
if (rad <= 0.0) { |
425 |
< |
xrad = xres/ncols/2 + 1; |
426 |
< |
yrad = yres/nrows/2 + 1; |
397 |
< |
} else { |
424 |
> |
xbrad = xres/ncols/2 + 1; |
425 |
> |
ybrad = yres/nrows/2 + 1; |
426 |
> |
if (rad > FTINY) { |
427 |
|
if (nrows >= yres && ncols >= xres) |
428 |
|
rad *= (y_r + x_c)/2.0; |
429 |
|
|
430 |
< |
xrad = CHECKRAD*rad/x_c + 1; |
431 |
< |
yrad = CHECKRAD*rad/y_r + 1; |
432 |
< |
|
430 |
> |
if (thresh > FTINY) { |
431 |
> |
xrad = CHECKRAD*THRESHRAD*rad/x_c + xbrad; |
432 |
> |
yrad = CHECKRAD*THRESHRAD*rad/y_r + ybrad; |
433 |
> |
orad = CHECKRAD*THRESHRAD*rad + 1; |
434 |
> |
obarsize = 2*orad + 1; |
435 |
> |
} else { |
436 |
> |
xrad = CHECKRAD*rad/x_c + 1; |
437 |
> |
yrad = CHECKRAD*rad/y_r + 1; |
438 |
> |
} |
439 |
|
initmask(); /* initialize filter table */ |
440 |
+ |
} else { |
441 |
+ |
xrad = xbrad; |
442 |
+ |
yrad = ybrad; |
443 |
|
} |
444 |
|
barsize = 2*yrad + 1; |
445 |
|
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
446 |
+ |
if (scanin == NULL) |
447 |
+ |
goto memerr; |
448 |
|
for (i = 0; i < barsize; i++) { |
449 |
|
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
450 |
< |
if (scanin[i] == NULL) { |
451 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
452 |
< |
quit(1); |
450 |
> |
if (scanin[i] == NULL) |
451 |
> |
goto memerr; |
452 |
> |
} |
453 |
> |
if (obarsize > 0) { |
454 |
> |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
455 |
> |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
456 |
> |
if (scoutbar == NULL | greybar == NULL) |
457 |
> |
goto memerr; |
458 |
> |
for (i = 0; i < obarsize; i++) { |
459 |
> |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
460 |
> |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
461 |
> |
if (scoutbar[i] == NULL | greybar[i] == NULL) |
462 |
> |
goto memerr; |
463 |
|
} |
464 |
+ |
} else { |
465 |
+ |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
466 |
+ |
if (scanout == NULL) |
467 |
+ |
goto memerr; |
468 |
|
} |
415 |
– |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
416 |
– |
if (scanout == NULL) { |
417 |
– |
fprintf(stderr, "%s: out of memory\n", progname); |
418 |
– |
quit(1); |
419 |
– |
} |
469 |
|
/* record pixel aspect ratio */ |
470 |
|
if (!correctaspect) { |
471 |
< |
d = x_c / y_r; |
471 |
> |
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
472 |
|
if (!FEQ(d,1.0)) |
473 |
|
fputaspect(d, stdout); |
474 |
|
} |
484 |
|
fputcolcor(ctmp, stdout); |
485 |
|
printf("\n"); |
486 |
|
/* write out resolution */ |
487 |
< |
fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); |
487 |
> |
fputresolu(order, ncols, nrows, stdout); |
488 |
> |
return; |
489 |
> |
memerr: |
490 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
491 |
> |
quit(1); |
492 |
> |
} |
493 |
> |
|
494 |
> |
|
495 |
> |
scan2sync(r) /* synchronize grey averages and output scan */ |
496 |
> |
int r; |
497 |
> |
{ |
498 |
> |
static int nextrow = 0; |
499 |
> |
COLOR ctmp; |
500 |
> |
int ybot; |
501 |
> |
register int c; |
502 |
> |
/* average input scanlines */ |
503 |
> |
while (nextrow < r+orad && nextrow < nrows) { |
504 |
> |
ybot = (long)nextrow*yres/nrows; |
505 |
> |
for (c = 0; c < ncols; c++) { |
506 |
> |
dobox(ctmp, (int)((long)c*xres/ncols),ybot, c,nextrow); |
507 |
> |
greybar[nextrow%obarsize][c] = bright(ctmp); |
508 |
> |
} |
509 |
> |
/* and zero output scanline */ |
510 |
> |
bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); |
511 |
> |
nextrow++; |
512 |
> |
} |
513 |
> |
/* point to top scanline for output */ |
514 |
> |
if (r-orad >= 0) |
515 |
> |
scanout = scoutbar[(r-orad)%obarsize]; |
516 |
> |
else |
517 |
> |
scanout = NULL; |
518 |
> |
} |
519 |
> |
|
520 |
> |
|
521 |
> |
scan2flush() /* flush output buffer */ |
522 |
> |
{ |
523 |
> |
register int r; |
524 |
> |
|
525 |
> |
for (r = nrows-orad; r < nrows; r++) |
526 |
> |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
527 |
> |
break; |
528 |
> |
if (fflush(stdout) < 0) { |
529 |
> |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
530 |
> |
exit(1); |
531 |
> |
} |
532 |
|
} |
533 |
|
|
534 |
|
|