Listing 4 find cutoff_point - looks through a histogram to find the threshold point

    /*******************************************
    *
    *   find_cutoff_point(..
    *
    *   This function looks at a histogram
    *   and sets a cuttoff point at a given
    *   percentage of pixels.
    *   For example, if percent=0.6, you
    *   start at 0 in the histogram and count
    *   up until you've hit 60% of the pixels.
    *   Then you stop and return that pixel
    *   value.
    *
    ********************************************/

find_cutoff_point(histogram, percent, cutoff}
   unsigned long histogram[];
   float    percent;
   short    *cutoff;
{
   float  fd, fsum, sum_div;
   int    i, looking;
   long   lc, lr, num=0, sum=0;

   sum     = 0;
   i       = 0;
   lr      = (long)(ROWS);
   lc      = (long)(COLS);
   num     = lr*lc;
   fd      = (float)(num);
   while(looking){
      fsum    = (float)(sum);
      sum_div = fsum/fd;
      if(sum_div >= percent)
         looking = 0;
      else
         sum = sum + histogram[i++];
   }  /* ends while looking */

   if(i >= 256) i = 255;
   *cutoff = i;
   printf("\nCutoff is %d sum=%ld", *cutoff, sum);
}  /* ends find_cutoff_point */

/* End of File */