ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pf2.c
Revision: 2.6
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.5: +3 -5 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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