home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part01 / compress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  1.8 KB  |  71 lines

  1. /* compress.c:
  2.  *
  3.  * compress a colormap by removing unused RGB colors
  4.  *
  5.  * jim frost 10.05.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. void compress(image, verbose)
  15.      Image        *image;
  16.      unsigned int  verbose;
  17. { Pixel        *index;
  18.   unsigned int *used;  
  19.   RGBMap        rgb;
  20.   byte         *pixptr;
  21.   unsigned int  a, x, y;
  22.   Pixel         color;
  23.  
  24.   goodImage(image, "compress");
  25.   if (! RGBP(image)) /* we're AT&T */
  26.     return;
  27.  
  28.   if (verbose) {
  29.     printf("  Compressing colormap...");
  30.     fflush(stdout);
  31.   }
  32.  
  33.   newRGBMapData(&rgb, image->rgb.size);
  34.   index= (Pixel *)lmalloc(sizeof(Pixel) * image->rgb.used);
  35.   used= (unsigned int *)lmalloc(sizeof(unsigned int) * image->rgb.used);
  36.   for (x= 0; x < image->rgb.used; x++)
  37.     *(used + x)= 0;
  38.  
  39.   pixptr= image->data;
  40.   for (y= 0; y < image->height; y++)
  41.     for (x= 0; x < image->width; x++) {
  42.       color= memToVal(pixptr, image->pixlen);
  43.       if (*(used + color) == 0) {
  44.     for (a= 0; a < rgb.used; a++)
  45.       if ((*(rgb.red + a) == *(image->rgb.red + color)) &&
  46.           (*(rgb.green + a) == *(image->rgb.green + color)) &&
  47.           (*(rgb.blue + a) == *(image->rgb.blue + color)))
  48.         break;
  49.     *(index + color)= a;
  50.     *(used + color)= 1;
  51.     if (a == rgb.used) {
  52.       *(rgb.red + a)= *(image->rgb.red + color);
  53.       *(rgb.green + a)= *(image->rgb.green + color);
  54.       *(rgb.blue + a)= *(image->rgb.blue + color);
  55.       rgb.used++;
  56.     }
  57.       }
  58.       valToMem(*(index + color), pixptr, image->pixlen);
  59.       pixptr += image->pixlen;
  60.     }
  61.  
  62.   if (verbose)
  63.     if (rgb.used < image->rgb.used)
  64.       printf("%d unique colors of %d\n", rgb.used, image->rgb.used);
  65.     else
  66.       printf("no improvement\n");
  67.  
  68.   freeRGBMapData(&(image->rgb));
  69.   image->rgb= rgb;
  70. }
  71.