1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#ifndef lint
static char SccsId[] = "%W% %G%";
#endif
/* Module: histdist.c (Histogram Equalize Distribute)
* Subroutine: distribute_levels() returns: int
* Copyright: 1989 Smithsonian Astrophysical Observatory
* You may do anything you like with this file except remove
* this copyright. The Smithsonian Astrophysical Observatory
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
* Modified: {0} Michael VanHilst initial version 30 May 1989
* {1} MVH fixed bug in range_zgroup 7 Dec 1989
* {n} <who> -- <does what> -- <when>
*/
#include <stdio.h>
#include "hfiles/histeq.h" /* define SubrangeLink */
/*
* Subroutine: distribute_levels
* Purpose: Distribute the levels among histogram sub-groups
* Returns: number of groups with no assigned color levels
*/
int distribute_levels ( linklist, pixel_area, color_levels,
pmin, pmax, ncolor )
SubrangeLink *linklist;
int pixel_area, color_levels;
int pmin, pmax, ncolor;
{
int average;
int threshold;
int excess;
int levels, zeroes;
int max_z_excess, max_nz_excess, max_z_range;
SubrangeLink *subrange;
#ifdef DEBUG
int census = 0;
#endif
static int excess_zgroup(), excess_nzgroup(), range_zgroup();
/* if all one group (no strong peaks), allocation is simple */
if( linklist->next == 0 ) {
linklist->color_levels = ncolor;
return( 0 );
}
/* average is rounded strongly upward to be stingy with levels */
average = (pixel_area / color_levels) + 1;
subrange = linklist;
zeroes = 0;
max_z_excess = max_nz_excess = 0;
max_z_range = 0;
/* first pass, simple assignment and some note taking */
while( subrange != 0 ) {
if( subrange->range > 0 ) {
levels = subrange->pixel_area / average;
excess = subrange->pixel_area - (levels * average);
if( levels >= subrange->range ) {
levels = subrange->range;
subrange->range = -subrange->range;
} else {
if( levels == 0 ) {
zeroes++;
if( excess > max_z_excess )
max_z_excess = excess;
if( subrange->range > max_z_range )
max_z_range = subrange->range;
} else {
if( excess > max_nz_excess )
max_nz_excess = excess;
}
}
subrange->color_levels = levels;
subrange->excess_pixels = excess;
color_levels -= levels;
#ifndef DEBUG
}
#else
census += levels;
} else
++census;
#endif
subrange = subrange->next;
}
/* second pass groups with no levels and vals or ranges above limits */
if( zeroes > 0 ) {
/* groups with counts above 1/4 of average */
threshold = average / 4;
while( (zeroes > 0) && (color_levels > 0) && (max_z_excess > threshold) ) {
if( excess_zgroup(linklist, &max_z_excess, &max_z_range, average) ) {
zeroes--;
color_levels--;
#ifndef DEBUG
}
#else
census++;
} else
(void)fprintf(stderr, "Failed to find excess zero.\n");
#endif
}
/* groups with range above 2 * unequalized distribution */
threshold = MAX(((pmax - pmin) / 8) , 4);
while( (zeroes > 0) && (color_levels > 0) && (max_z_range > threshold) ) {
if( range_zgroup(linklist, &max_z_excess, &max_z_range, average) ) {
zeroes--;
color_levels--;
#ifndef DEBUG
}
#else
census++;
} else
(void)fprintf(stderr, "Failed to find range zero.\n");
#endif
}
}
/* third pass, give away any remaining levels by highest excess */
#ifdef DEBUG
if( color_levels != (ncolor - census) ) {
(void)fprintf(stderr, "Allocation waste: %d level(s)\n",
(ncolor - census) - color_levels);
}
#endif
while( color_levels > 0 ) {
if( (zeroes > 0) && (max_z_excess > max_nz_excess) ) {
if( excess_zgroup(linklist, &max_z_excess, &max_z_range, average) ) {
zeroes--;
color_levels--;
}
#ifdef DEBUG
else (void)fprintf(stderr, "Failed to find excess zero.\n");
#endif
} else {
if( excess_nzgroup(linklist, &max_nz_excess, average) )
color_levels--;
#ifdef DEBUG
else (void)fprintf(stderr, "Failed to find excess.\n");
#endif
}
}
return( zeroes );
}
/*
* Subroutine: excess_zgroup
* Purpose: Find subrange with zero allotted levels and specified excess.
* Assign it one level
*/
static int excess_zgroup ( subrange, excess, range, average )
SubrangeLink *subrange;
int *excess, *range;
int average;
{
int max_excess, looking;
max_excess = -32700;
looking = 1;
while( subrange != 0 ) {
if( (subrange->color_levels == 0) && (subrange->range > 0) ) {
if( looking && (subrange->excess_pixels == *excess) ) {
if( subrange->range > 1 ) {
subrange->color_levels = 1;
} else {
subrange->color_levels = 1;
subrange->range = -1;
}
subrange->excess_pixels -= average;
looking = 0;
} else {
if( subrange->excess_pixels > max_excess )
max_excess = subrange->excess_pixels;
if( subrange->range > *range )
*range = subrange->range;
}
}
subrange = subrange->next;
}
*excess = max_excess;
return( !looking );
}
/*
* Subroutine: range_zgroup
* Purpose: Find group with zero allotted levels and specified range.
* Assign it one level.
*/
static int range_zgroup (subrange, excess, range, average)
SubrangeLink *subrange;
int *excess, *range;
int average;
{
int max_range, looking;
max_range = 0;
looking = 1;
while( subrange != 0 ) {
if( (subrange->color_levels == 0) && (subrange->range > 0) ) {
if( looking && (subrange->range == *range) ) {
if( subrange->range > 1 ) {
subrange->color_levels = 1;
} else {
subrange->color_levels = 1;
subrange->range = -1;
}
subrange->excess_pixels -= average;
looking = 0;
} else {
if( subrange->excess_pixels > *excess )
*excess = subrange->excess_pixels;
if( subrange->range > max_range )
max_range = subrange->range;
}
}
subrange = subrange->next;
}
*range = max_range;
return( !looking );
}
/*
* Subroutine: excess_nzgroup
* Purpose: Find group with non-zero allotted levels and specified
* excess value. Assign it one additional level.
*/
static int excess_nzgroup ( subrange, excess, average )
SubrangeLink *subrange;
int *excess;
int average;
{
int looking, max_excess;
looking = 1;
max_excess = -32767;
while( subrange != 0 ) {
if( (subrange->color_levels > 0) && (subrange->range > 1) ) {
if( looking && (subrange->excess_pixels == *excess) &&
(subrange->color_levels < subrange->range) ) {
subrange->color_levels += 1;
subrange->excess_pixels -= average;
if( subrange->color_levels == subrange->range ) {
subrange->color_levels = subrange->range;
subrange->range = -subrange->range;
} else {
if( subrange->excess_pixels > max_excess )
max_excess = subrange->excess_pixels;
}
looking = 0;
} else {
if( subrange->excess_pixels > max_excess )
max_excess = subrange->excess_pixels;
}
}
subrange = subrange->next;
}
*excess = max_excess;
return( !looking );
}
|