home *** CD-ROM | disk | FTP | other *** search
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C o m p r e s s C o l o r m a p %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function CompressColormap compresses an image colormap removing any
- % unused color entries.
- %
- % The format of the CompressColormap routine is:
- %
- % CompressColormap(image)
- %
- % A description of each parameter follows:
- %
- % o image: The address of a structure of type Image.
- %
- %
- */
-
- #include "combine.h"
- #include "defines.h"
-
- void CompressColormap(image)
- Image
- *image;
- {
- RGB
- *colormap;
-
- int
- number_colors;
-
- register int
- i;
-
- register Runlength
- *p;
-
- register unsigned short
- index;
-
- /*
- Determine if colormap can be compressed.
- */
- if (image->class != PseudoClass)
- return;
- number_colors=image->colors;
- for (i=0; i < image->colors; i++)
- image->colormap[i].flags=False;
- image->colors=0;
- p=image->pixels;
- for (i=0; i < image->packets; i++)
- {
- if (!image->colormap[p->index].flags)
- {
- image->colormap[p->index].index=image->colors;
- image->colormap[p->index].flags=True;
- image->colors++;
- }
- p++;
- }
- if (image->colors == number_colors)
- return; /* no unused entries */
- /*
- Compress colormap.
- */
- colormap=(RGB *) malloc(image->colors*sizeof(RGB));
- if (colormap == (RGB *) NULL)
- {
- (void) fprintf (stderr,
- "Unable to compress colormap,Memory allocation error\n");
- image->colors=number_colors;
- return;
- }
- for (i=0; i < number_colors; i++)
- if (image->colormap[i].flags)
- {
- index=image->colormap[i].index;
- colormap[index].red=image->colormap[i].red;
- colormap[index].green=image->colormap[i].green;
- colormap[index].blue=image->colormap[i].blue;
- }
- /*
- Remap pixels.
- */
- p=image->pixels;
- for (i=0; i < image->packets; i++)
- {
- p->index=image->colormap[p->index].index;
- p++;
- }
- (void) free((char *) image->colormap);
- image->colormap=colormap;
- }
-