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

  1. #include "combine.h"
  2. #include "defines.h"
  3.  
  4. void CompressImage(image)
  5. Image
  6.   *image;
  7. {
  8.   register int
  9.     i;
  10.  
  11.   register Runlength
  12.     *p,
  13.     *q;
  14.  
  15.   /*
  16.     Compress image.
  17.   */
  18.   p=image->pixels;
  19.   image->runlength=p->length+1;
  20.   image->packets=0;
  21.   q=image->pixels;
  22.   q->length=MaxRunlength;
  23.  
  24.   if (image->alpha)
  25.     for (i=0; i < (image->columns*image->rows); i++)
  26.     {
  27.       if (image->runlength != 0)
  28.         image->runlength--;
  29.       else
  30.         {
  31.           p++;
  32.           image->runlength=p->length;
  33.         }
  34.       if ((p->red == q->red) && (p->green == q->green) &&
  35.           (p->blue == q->blue) && (p->index == q->index) &&
  36.           (q->length < MaxRunlength))
  37.         q->length++;
  38.       else
  39.         {
  40.           if (image->packets != 0)
  41.             q++;
  42.           image->packets++;
  43.           *q=(*p);
  44.           q->length=0;
  45.         }
  46.     }
  47.   else
  48.     for (i=0; i < (image->columns*image->rows); i++)
  49.     {
  50.       if (image->runlength != 0)
  51.         image->runlength--;
  52.       else
  53.         {
  54.           p++;
  55.           image->runlength=p->length;
  56.         }
  57.       if ((p->red == q->red) && (p->green == q->green) &&
  58.           (p->blue == q->blue) && (q->length < MaxRunlength))
  59.         q->length++;
  60.       else
  61.         {
  62.           if (image->packets != 0)
  63.             q++;
  64.           image->packets++;
  65.           *q=(*p);
  66.           q->length=0;
  67.         }
  68.     }
  69.   image->pixels=(Runlength *) realloc((char *) image->pixels,
  70.     image->packets*sizeof(Runlength));
  71.   /*
  72.     Runlength-encode only if it consumes less memory than no compression.
  73.   */
  74.   if (image->compression == RunlengthEncodedCompression)
  75.     if (image->class == DirectClass)
  76.       {
  77.         if (image->packets >= ((image->columns*image->rows*3) >> 2))
  78.           image->compression=NoCompression;
  79.       }
  80.     else
  81.       if (image->packets >= ((image->columns*image->rows) >> 1))
  82.         image->compression=NoCompression;
  83.  
  84. }
  85.