home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Utilities / wwwcount-2.3 / combine / alphaim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-02  |  3.1 KB  |  134 lines

  1. /*
  2.  *  AlphaImage () - creates an alpha channel
  3.  *
  4.  *  RCS:
  5.  *      $Revision: 2.3 $
  6.  *      $Date: 1996/05/03 02:21:34 $
  7.  *
  8.  *  Security:
  9.  *      Unclassified
  10.  *
  11.  *  Description:
  12.  *      From ImageMagick
  13.  *
  14.  *  Input Parameters:
  15.  *      type    identifier  description
  16.  *
  17.  *      text
  18.  *
  19.  *  Output Parameters:
  20.  *      type    identifier  description
  21.  *
  22.  *      text
  23.  *
  24.  *  Return Values:
  25.  *      value   description
  26.  *
  27.  *  Side Effects:
  28.  *      text
  29.  *
  30.  *  Limitations and Comments:
  31.  *      text
  32.  *
  33.  *  Development History:
  34.  *      who                 when        why
  35.  *      muquit@semcor.com   24-Aug-95   first cut
  36.  */
  37. #include "combine.h"
  38.  
  39. #if __STDC__ || defined(sgi) || defined(_AIX)
  40. void AlphaImage (Image *image,
  41.     unsigned char red,unsigned char green,unsigned char blue)
  42. #else
  43. void AlphaImage (image,red,green,blue)
  44. Image
  45.     *image;
  46. unsigned char
  47.     red,
  48.     green,
  49.     blue;
  50. #endif
  51. {
  52. #define DeltaX  16
  53.  
  54.     register Runlength
  55.         *p;
  56.  
  57.     register int
  58.         i;
  59.  
  60.     p=image->pixels;
  61.     switch(image->class)
  62.     {
  63.         case DirectClass:
  64.         {
  65.             if (!image->alpha)
  66.             {
  67.                 for (i=0; i < image->packets; i++)
  68.                 {
  69.                     p->index=Opaque;
  70.                     p++;
  71.                 }
  72.                 image->alpha=True;
  73.                 p=image->pixels;
  74.             }
  75.  
  76.             for (i=0; i < image->packets; i++)
  77.             {
  78.                 if (((int) p->red < (int) (red+DeltaX)) &&
  79.                     ((int) p->red > (int) (red-DeltaX)) &&
  80.                     ((int) p->green < (int) (green+DeltaX)) &&
  81.                     ((int) p->green > (int) (green-DeltaX)) &&
  82.                     ((int) p->blue < (int) (blue+DeltaX)) &&
  83.                     ((int) p->blue > (int) (blue-DeltaX)))
  84.                 p->index=Transparent;
  85.                p++;
  86.                 
  87.              }
  88.             break;
  89.         }
  90.         case PseudoClass:
  91.         {
  92.             double
  93.                 distance_squared,
  94.                 min_distance;
  95.  
  96.             int
  97.                 distance;
  98.  
  99.             register int
  100.                 index;
  101.  
  102.              min_distance=3.0*(MaxRGB+1)*(MaxRGB+1);
  103.              index=0;
  104.  
  105.              for (i=0; i < image->colors; i++)
  106.              {
  107.                 distance=(int) red-(int) image->colormap[i].red;
  108.                 distance_squared=(unsigned int) (distance*distance);
  109.                 distance=(int) green-(int) image->colormap[i].green;
  110.                 distance_squared+=(unsigned int) (distance*distance);
  111.                 distance=(int) blue-(int) image->colormap[i].blue;
  112.                 distance_squared+=(unsigned int) (distance*distance);
  113.                 if (distance_squared < min_distance)
  114.                 {
  115.                     min_distance=distance_squared;
  116.                     index=i;
  117.                 }
  118.              }
  119.  
  120.              image->class=DirectClass;
  121.              image->alpha=True;
  122.              for (i=0; i < image->packets; i++)
  123.              {
  124.                 if (p->index == index)
  125.                     p->index=Transparent;
  126.                 else
  127.                     p->index=Opaque;
  128.                 p++;
  129.              }
  130.              break;
  131.         }
  132.     }
  133. }
  134.