home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Utilities / wwwcount-2.3 / combine / compcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-19  |  2.5 KB  |  101 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %                                                                             %
  5. %                                                                             %
  6. %   C o m p r e s s C o l o r m a p                                           %
  7. %                                                                             %
  8. %                                                                             %
  9. %                                                                             %
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. %
  12. %  Function CompressColormap compresses an image colormap removing any
  13. %  unused color entries.
  14. %
  15. %  The format of the CompressColormap routine is:
  16. %
  17. %      CompressColormap(image)
  18. %
  19. %  A description of each parameter follows:
  20. %
  21. %    o image: The address of a structure of type Image.
  22. %
  23. %
  24. */
  25.  
  26. #include "combine.h"
  27. #include "defines.h"
  28.  
  29. void CompressColormap(image)
  30. Image
  31.   *image;
  32. {
  33.   RGB
  34.     *colormap;
  35.  
  36.   int
  37.     number_colors;
  38.  
  39.   register int
  40.     i;
  41.  
  42.   register Runlength
  43.     *p;
  44.  
  45.   register unsigned short
  46.     index;
  47.  
  48.   /*
  49.     Determine if colormap can be compressed.
  50.   */
  51.   if (image->class != PseudoClass)
  52.     return;
  53.   number_colors=image->colors;
  54.   for (i=0; i < image->colors; i++)
  55.     image->colormap[i].flags=False;
  56.   image->colors=0;
  57.   p=image->pixels;
  58.   for (i=0; i < image->packets; i++)
  59.   {
  60.     if (!image->colormap[p->index].flags)
  61.       {
  62.         image->colormap[p->index].index=image->colors;
  63.         image->colormap[p->index].flags=True;
  64.         image->colors++;
  65.       }
  66.     p++;
  67.   }
  68.   if (image->colors == number_colors)
  69.     return;  /* no unused entries */
  70.   /*
  71.     Compress colormap.
  72.   */
  73.   colormap=(RGB *) malloc(image->colors*sizeof(RGB));
  74.   if (colormap == (RGB *) NULL)
  75.     {
  76.       (void) fprintf (stderr,
  77.         "Unable to compress colormap,Memory allocation error\n");
  78.       image->colors=number_colors;
  79.       return;
  80.     }
  81.   for (i=0; i < number_colors; i++)
  82.     if (image->colormap[i].flags)
  83.       {
  84.         index=image->colormap[i].index;
  85.         colormap[index].red=image->colormap[i].red;
  86.         colormap[index].green=image->colormap[i].green;
  87.         colormap[index].blue=image->colormap[i].blue;
  88.       }
  89.   /*
  90.     Remap pixels.
  91.   */
  92.   p=image->pixels;
  93.   for (i=0; i < image->packets; i++)
  94.   {
  95.     p->index=image->colormap[p->index].index;
  96.     p++;
  97.   }
  98.   (void) free((char *) image->colormap);
  99.   image->colormap=colormap;
  100. }
  101.