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