home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / histzero.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  3KB  |  116 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    histzero.c (Histogram Zero)
  6.  * Subroutine:    resolve_zeroes()            returns: void
  7.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  8.  *        You may do anything you like with this file except remove
  9.  *        this copyright.  The Smithsonian Astrophysical Observatory
  10.  *        makes no representations about the suitability of this
  11.  *        software for any purpose.  It is provided "as is" without
  12.  *        express or implied warranty.
  13.  * Modified:    {0} Michael VanHilst    initial version          30 May 1989
  14.  *        {n} <who> -- <does what> -- <when>
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include "hfiles/histeq.h"        /* define SubrangeLink */
  19.  
  20. /*
  21.  * Subroutine:    resolve_zeroes
  22.  * Purpose:    Combine groups with zero alloted levels with adjoining groups
  23.  * Note:    Adjoining groups are large count single level groups
  24.  * Called by:    histrogram_equalize() above
  25.  */
  26. void resolve_zeroes ( PriorLink, zeroes )
  27.      SubrangeLink *PriorLink;
  28.      int zeroes;
  29. {
  30.   SubrangeLink *ThisLink, *NextLink;
  31.   int a_count, b_count, z1count, z2count;
  32.   static void merge_links();
  33.  
  34.   /* if very first entry is a zero allocated link */
  35.   if( PriorLink->color_levels == 0 ) {
  36.     /* merge this and next */
  37.     merge_links(PriorLink);
  38.     zeroes--;
  39.   }
  40.   /* scan for the zero allocated links */
  41.   while( zeroes > 0 ) {
  42.     ThisLink = PriorLink->next;
  43. #ifdef DEBUG
  44.     /* if reached the end of the list, we had an error */
  45.     if( ThisLink == 0 ) {
  46.       (void)fprintf(stderr,"Zero error\n");
  47.       return;
  48.     }
  49. #endif
  50.     /* if we are about to hit a zero */
  51.     if( ThisLink->color_levels == 0 ) {
  52.       /* if it is the last zero merge with prior */
  53.       NextLink = ThisLink->next;
  54.       if( NextLink == 0 ) {
  55.     merge_links(PriorLink);
  56.     return;
  57.       }
  58.       a_count = PriorLink->pixel_area;
  59.       b_count = NextLink->pixel_area;
  60.       /* if the preceding link is smaller than the trailing link */
  61.       if( a_count > b_count ) {
  62.     merge_links(PriorLink);
  63.     zeroes--;
  64.       } else {
  65.     /* probe beyond */
  66.     if( NextLink->next != 0 ) {
  67.       if( NextLink->next->color_levels != 0 ) {
  68.         /* if new competition, merge with next link */
  69.         merge_links(ThisLink);
  70.         zeroes--;
  71.       } else {
  72.         z1count = ThisLink->pixel_area;
  73.         z2count = NextLink->next->pixel_area;
  74.         /* where would the next one go? */
  75.         if( (NextLink->next->next == 0) ||
  76.         (NextLink->next->next->pixel_area > (b_count + z2count)) ) {
  77.           if( (b_count + z2count) > (a_count + z1count) ) {
  78.         merge_links(PriorLink);
  79.           } else {
  80.         merge_links(ThisLink);
  81.           }
  82.         } else
  83.           merge_links(ThisLink);
  84.         zeroes--;
  85.       }
  86.     } else {
  87.       merge_links(PriorLink);
  88.       zeroes--;
  89.     }
  90.       }
  91.     }
  92.     PriorLink = ThisLink;
  93.   }
  94. }
  95.  
  96. /*
  97.  * Subroutine:    merge_links
  98.  * Purpose:    Combine two links of histogram group list
  99.  */
  100. static void merge_links ( subrange )
  101.      SubrangeLink *subrange;
  102. {
  103.   SubrangeLink *lostlink;
  104.  
  105.   lostlink = subrange->next;
  106.   subrange->next = lostlink->next;
  107.   subrange->max_entry = MAX(subrange->max_entry, lostlink->max_entry);
  108.   subrange->pixel_area += lostlink->pixel_area;
  109.   subrange->high = lostlink->high;
  110.   subrange->range += lostlink->range;
  111.   subrange->nz_entries += lostlink->nz_entries;
  112.   subrange->excess_pixels += lostlink->excess_pixels;
  113.   subrange->color_levels += lostlink->color_levels;
  114.   free( (char *)lostlink );
  115. }
  116.