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