/* * FAST IMAGE HISTAGRAM EQUILIZATION ALGORITHM * * Copywrite 1998 * by Melinda Green * melinda@superliminal.com * all rights reserved * * Permission is granted for use in all non-commercial applications. * All commercial use of this code or algorithm requires written * permission. * * DESCRIPION: * * When given an array of integer values need to be converted * into an image, colors must be assigned for each value. This * module assumes that a color ramp will be selected, and * that the lowest integer values should be assigned color map * indices at one end of that ramp, and the higest values indices * at the other end. The problem is that there are any number of * ways that those indices can be assigned. The most obvious is to * partition the ramp linearly from the lowest value to the highest, * but that only gives reasonable results when the input values are * equally distributed. In the worst case, almost all pixels in * an image would have very similar integer values, but a few would * be very different. Using linear interpolation, all the similar * values would be assigned only one or two unique color indices and * would be indistinguishable in the final image. All the fine * detail would be lost. Histagram equalization attempts to allocate * ranges of the color ramp proportional to the image area ocupied * by various pixel value ranges so that more colors are allocated * to the pixel ranges that most need it, and only few colors are * allocated for those rare pixel values. * * A proper histagram equalization algorithm would require a careful * analysis of the input image in order to allocate colors in the * most ideal way. The technique used here does not perform an ideal * allocation of color ranges, but it has the advantages of being * very simple and extremely fast. * * This module contains the following public methods: * select_samples() * lookup_color() * dump_samples() * * select_samples() takes an array of integer values representing an * image to be colored and sets up the data structures needed to * perform subsequent coloring of pixel values. It must be called * before lookup_color(). It returns the number of colors lookup_colors * requires up to the maximum (currently hardcoded at 256 but easily * changed). * * lookup_color() takes a single raw pixel integer to be colored, * and returns the colormap index that should be used to color that * pixel. * * dump_samples() is simply a debugging aid which can be used to * print out the internal sample array after select_samples has run. */ #include #include /* for qsort */ /* * Largest number of colors allowed. */ #define MAX_MAP 256 /* * When choosing color samples, this is the maximum number of pixels * to randomly sample. */ #define MAX_COLOR_TRYS (MAX_MAP * 10) const unsigned long MAX_VAL = 65931; /* * A sampled table of exit values used to partition the color map * into color ranges. */ int Samples[MAX_MAP]; int NSamples; /* The number of samples in the 'Samples' array. */ int lookup_color(val) int val; { int i; for(i=0; i= goal) goto enough_colors; } /* * if here, we must not be enough colors to fill the whole map * so we'll take what we can get. */ for(i=0; i MAX_COLOR_TRYS) { fprintf(stderr, "select_samples: quitting random selection\n"); break; } /* choose a random pixel */ i = random() % nvals; if( ! in_set(vals[i], NSamples, Samples)) Samples[NSamples++] = vals[i]; } /* * fill any remaining slots which occure when random sampling quit. */ for(i=0; i