1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* pf2.c - routines used by pfilt. |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include <math.h> |
14 |
|
15 |
#include "random.h" |
16 |
|
17 |
#include "color.h" |
18 |
|
19 |
#define PI 3.14159265359 |
20 |
|
21 |
#define FTINY (1e-6) |
22 |
|
23 |
extern int nrows, ncols; /* number of rows and columns for output */ |
24 |
|
25 |
extern int xres, yres; /* x and y resolution */ |
26 |
|
27 |
extern int avghot; /* true means average in avgbrt spots */ |
28 |
|
29 |
extern double hotlvl; /* brightness considered "hot" */ |
30 |
|
31 |
extern int npts; /* # of points for stars */ |
32 |
|
33 |
extern double spread; /* spread for star points */ |
34 |
|
35 |
extern char *progname; |
36 |
|
37 |
extern COLOR exposure; /* exposure for frame */ |
38 |
|
39 |
extern double (*ourbright)(); /* brightness calculation function */ |
40 |
|
41 |
#define AVGLVL 0.5 /* target mean brightness */ |
42 |
|
43 |
double avgbrt; /* average picture brightness */ |
44 |
long npix; /* # pixels in average */ |
45 |
|
46 |
typedef struct hotpix { /* structure for avgbrt pixels */ |
47 |
struct hotpix *next; /* next in list */ |
48 |
COLOR val; /* pixel color */ |
49 |
short x, y; /* pixel position */ |
50 |
float slope; /* random slope for diffraction */ |
51 |
} HOTPIX; |
52 |
|
53 |
HOTPIX *head; /* head of avgbrt pixel list */ |
54 |
|
55 |
double sprdfact; /* computed spread factor */ |
56 |
|
57 |
|
58 |
pass1init() /* prepare for first pass */ |
59 |
{ |
60 |
avgbrt = 0.0; |
61 |
npix = 0; |
62 |
head = NULL; |
63 |
} |
64 |
|
65 |
|
66 |
pass1default() /* for single pass */ |
67 |
{ |
68 |
avgbrt = AVGLVL; |
69 |
npix = 1; |
70 |
head = NULL; |
71 |
} |
72 |
|
73 |
|
74 |
pass1scan(scan, y) /* process first pass scanline */ |
75 |
register COLOR *scan; |
76 |
int y; |
77 |
{ |
78 |
extern char *malloc(); |
79 |
double cbrt; |
80 |
register int x; |
81 |
register HOTPIX *hp; |
82 |
|
83 |
for (x = 0; x < xres; x++) { |
84 |
|
85 |
cbrt = (*ourbright)(scan[x]); |
86 |
|
87 |
if (cbrt <= 0) |
88 |
continue; |
89 |
|
90 |
if (avghot || cbrt < hotlvl) { |
91 |
avgbrt += cbrt; |
92 |
npix++; |
93 |
} |
94 |
if (npts && cbrt >= hotlvl) { |
95 |
hp = (HOTPIX *)malloc(sizeof(HOTPIX)); |
96 |
if (hp == NULL) { |
97 |
fprintf(stderr, "%s: out of memory\n", |
98 |
progname); |
99 |
quit(1); |
100 |
} |
101 |
copycolor(hp->val, scan[x]); |
102 |
hp->x = x; |
103 |
hp->y = y; |
104 |
hp->slope = tan(PI*(0.5-(random()%npts+0.5)/npts)); |
105 |
hp->next = head; |
106 |
head = hp; |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
|
112 |
pass2init() /* prepare for final pass */ |
113 |
{ |
114 |
if (!npix) { |
115 |
fprintf(stderr, "%s: picture too dark or too bright\n", |
116 |
progname); |
117 |
quit(1); |
118 |
} |
119 |
avgbrt /= (double)npix; |
120 |
|
121 |
scalecolor(exposure, AVGLVL/avgbrt); |
122 |
|
123 |
sprdfact = spread / (hotlvl * (*ourbright)(exposure)) |
124 |
* ((double)xres*xres + (double)yres*yres) / 4.0; |
125 |
} |
126 |
|
127 |
|
128 |
pass2scan(scan, y) /* process final pass scanline */ |
129 |
register COLOR *scan; |
130 |
int y; |
131 |
{ |
132 |
int xmin, xmax; |
133 |
register int x; |
134 |
register HOTPIX *hp; |
135 |
|
136 |
for (hp = head; hp != NULL; hp = hp->next) { |
137 |
if (hp->slope > FTINY) { |
138 |
xmin = (y - hp->y - 0.5)/hp->slope + hp->x; |
139 |
xmax = (y - hp->y + 0.5)/hp->slope + hp->x; |
140 |
} else if (hp->slope < -FTINY) { |
141 |
xmin = (y - hp->y + 0.5)/hp->slope + hp->x; |
142 |
xmax = (y - hp->y - 0.5)/hp->slope + hp->x; |
143 |
} else if (y == hp->y) { |
144 |
xmin = 0; |
145 |
xmax = xres-1; |
146 |
} else { |
147 |
xmin = 1; |
148 |
xmax = 0; |
149 |
} |
150 |
if (xmin < 0) |
151 |
xmin = 0; |
152 |
if (xmax >= xres) |
153 |
xmax = xres-1; |
154 |
for (x = xmin; x <= xmax; x++) |
155 |
starpoint(scan[x], x, y, hp); |
156 |
} |
157 |
for (x = 0; x < xres; x++) |
158 |
multcolor(scan[x], exposure); |
159 |
} |
160 |
|
161 |
|
162 |
starpoint(fcol, x, y, hp) /* pixel is on the star's point */ |
163 |
COLOR fcol; |
164 |
int x, y; |
165 |
register HOTPIX *hp; |
166 |
{ |
167 |
COLOR ctmp; |
168 |
double d2; |
169 |
|
170 |
d2 = (double)(x - hp->x)*(x - hp->x) + (double)(y - hp->y)*(y - hp->y); |
171 |
if (d2 > sprdfact) { |
172 |
d2 = sprdfact / d2; |
173 |
if (d2 < FTINY) |
174 |
return; |
175 |
copycolor(ctmp, hp->val); |
176 |
scalecolor(ctmp, d2); |
177 |
addcolor(fcol, ctmp); |
178 |
} else if (d2 > FTINY) { |
179 |
addcolor(fcol, hp->val); |
180 |
} |
181 |
} |