--- ray/src/px/pf2.c 1989/02/02 10:49:24 1.1 +++ ray/src/px/pf2.c 2023/12/08 17:56:26 2.11 @@ -1,88 +1,71 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pf2.c,v 2.11 2023/12/08 17:56:26 greg Exp $"; #endif - /* * pf2.c - routines used by pfilt. - * - * 10/3/85 */ -#include - +#include "pfilt.h" #include "random.h" -#include "color.h" +#define AVGLVL 0.5 /* target mean brightness */ -#define PI 3.14159265359 +double avgbrt; /* average picture brightness */ +long npix; /* # pixels in average */ -#define FTINY (1e-6) - -extern int nrows, ncols; /* number of rows and columns for output */ - -extern int xres, yres; /* x and y resolution */ - -extern int avghot; /* true means average in avgbrt spots */ - -extern double hotlvl; /* brightness considered "hot" */ - -extern int npts; /* # of points for stars */ - -extern double spread; /* spread for star points */ - -extern char *progname; - -extern COLOR exposure; /* exposure for frame */ - -#define AVGLVL 0.5 /* target mean brightness */ - -double avgbrt; /* average picture brightness */ - -typedef struct hotpix { /* structure for avgbrt pixels */ +typedef struct hotpix { /* structure for avgbrt pixels */ struct hotpix *next; /* next in list */ COLOR val; /* pixel color */ short x, y; /* pixel position */ float slope; /* random slope for diffraction */ } HOTPIX; -HOTPIX *head; /* head of avgbrt pixel list */ +HOTPIX *head; /* head of avgbrt pixel list */ -double sprdfact; /* computed spread factor */ +double sprdfact; /* computed spread factor */ +static void starpoint(SCOLOR fcol, int x, int y, HOTPIX *hp); -pass1init() /* prepare for first pass */ + +void +pass1init(void) /* prepare for first pass */ { avgbrt = 0.0; + npix = 0; head = NULL; } -pass1default() /* for single pass */ +void +pass1default(void) /* for single pass */ { - avgbrt = AVGLVL * xres * yres; + avgbrt = AVGLVL; + npix = 1; head = NULL; } -pass1scan(scan, y) /* process first pass scanline */ -register COLOR *scan; -int y; +void +pass1scan( /* process first pass scanline */ + COLORV *scan, + int y +) { - extern char *malloc(); - extern double tan(), sqrt(); - double cbrt; - register int x; - register HOTPIX *hp; + double cbrt; + int x; + HOTPIX *hp; for (x = 0; x < xres; x++) { - cbrt = bright(scan[x]); + cbrt = (*ourbright)(scan+x*NCSAMP); - if (avghot || cbrt < hotlvl) - avgbrt += cbrt; + if (cbrt <= 0) + continue; + if (avghot || cbrt < hotlvl) { + avgbrt += cbrt; + npix++; + } if (npts && cbrt >= hotlvl) { hp = (HOTPIX *)malloc(sizeof(HOTPIX)); if (hp == NULL) { @@ -90,10 +73,10 @@ int y; progname); quit(1); } - copycolor(hp->val, scan[x]); + scolor_color(hp->val, scan+x*NCSAMP); hp->x = x; hp->y = y; - hp->slope = tan(PI*(0.5-(random()%npts+0.5)/npts)); + hp->slope = ttan(PI*(0.5-(irandom(npts)+0.5)/npts)); hp->next = head; head = hp; } @@ -101,14 +84,15 @@ int y; } -pass2init() /* prepare for final pass */ +void +pass2init(void) /* prepare for final pass */ { - avgbrt /= (double)xres * yres; - - if (avgbrt <= FTINY) { - fprintf(stderr, "%s: picture too dark\n", progname); + if (!npix) { + fprintf(stderr, "%s: picture too dark or too bright\n", + progname); quit(1); } + avgbrt /= (double)npix; scalecolor(exposure, AVGLVL/avgbrt); @@ -117,13 +101,15 @@ pass2init() /* prepare for final pass */ } -pass2scan(scan, y) /* process final pass scanline */ -register COLOR *scan; -int y; +void +pass2scan( /* process final pass scanline */ + COLORV *scan, + int y +) { int xmin, xmax; - register int x; - register HOTPIX *hp; + int x; + HOTPIX *hp; for (hp = head; hp != NULL; hp = hp->next) { if (hp->slope > FTINY) { @@ -144,20 +130,23 @@ int y; if (xmax >= xres) xmax = xres-1; for (x = xmin; x <= xmax; x++) - starpoint(scan[x], x, y, hp); + starpoint(scan+x*NCSAMP, x, y, hp); } for (x = 0; x < xres; x++) - multcolor(scan[x], exposure); + smultcolor(scan+x*NCSAMP, exposure); } -starpoint(fcol, x, y, hp) /* pixel is on the star's point */ -COLOR fcol; -int x, y; -register HOTPIX *hp; +static void +starpoint( /* pixel is on the star's point */ + SCOLOR fcol, + int x, + int y, + HOTPIX *hp +) { COLOR ctmp; - double d2; + double d2; d2 = (double)(x - hp->x)*(x - hp->x) + (double)(y - hp->y)*(y - hp->y); if (d2 > sprdfact) { @@ -166,8 +155,8 @@ register HOTPIX *hp; return; copycolor(ctmp, hp->val); scalecolor(ctmp, d2); - addcolor(fcol, ctmp); + saddcolor(fcol, ctmp); } else if (d2 > FTINY) { - addcolor(fcol, hp->val); + saddcolor(fcol, hp->val); } }