home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / ImageLib / Image_lib / lib_source / RGBtoHAM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-28  |  1.7 KB  |  100 lines

  1. /*
  2. Kod úródîowy pochodzi z CyberXXXsrc
  3. i ksiâûki "Programowanie plików graficznych
  4. w C/C++" wydawinictwo Translator s.c.
  5. z moimi drobnymi zmianami.
  6. */
  7. #include <exec/types.h>
  8. #define abs(x) ((x)<0?-(x):(x))
  9. #define DELTA(x1,x2) abs((x1)-(x2))
  10. #define NUM_GUNS 3
  11.  
  12. void RGBtoHAM( UBYTE *rgb,
  13.                UBYTE *ham,
  14.                ULONG width,
  15.                ULONG height,
  16.                BYTE mode)
  17. {
  18.   UBYTE oRed, oGreen, oBlue;
  19.   UBYTE dRed, dGreen, dBlue;
  20.   UBYTE red, green, blue;
  21.   ULONG xp, yp;
  22.   UBYTE mod_r, mod_g, mod_b, mod;
  23.  
  24.   if(mode)
  25.   {
  26.     mod_r=0x80;
  27.     mod_g=0xC0;
  28.     mod_b=0x40;
  29.     mod=2;
  30.   }
  31.   else
  32.   {
  33.     mod_r=0x20;
  34.     mod_g=0x30;
  35.     mod_b=0x10;
  36.     mod=4;
  37.   }
  38.  
  39.   for (yp=height; yp>0; yp--)
  40.   {
  41.     oRed=0;
  42.     oGreen=0;
  43.     oBlue=0;
  44.  
  45.     for (xp=width; xp>0; xp--)
  46.     {
  47.       red=rgb[0] >> mod;
  48.       green=rgb[1] >> mod;
  49.       blue=rgb[2] >> mod;
  50.       rgb+=3;
  51.  
  52.       if ((red==oRed) && (green==oGreen) && (blue==oBlue))
  53.       {
  54.         switch (xp%NUM_GUNS)
  55.         {
  56.           case 0:
  57.            *ham++=mod_r | red;
  58.            break;
  59.           case 1:
  60.            *ham++=mod_g | green;
  61.            break;
  62.           case 2:
  63.            *ham++=mod_b | blue;
  64.         }
  65.         continue;
  66.       }
  67.  
  68.       dRed=DELTA(red,oRed);
  69.       dGreen=DELTA(green,oGreen);
  70.       dBlue=DELTA(blue,oBlue);
  71.       if (dRed>dGreen)
  72.       {
  73.         if (dRed>dBlue)
  74.         {
  75.           *ham++=mod_r | red;
  76.           oRed=red;
  77.         }
  78.         else
  79.         {
  80.           *ham++=mod_b | blue;
  81.           oBlue=blue;
  82.         }
  83.       }
  84.       else
  85.       {
  86.         if (dGreen>dBlue)
  87.         {
  88.           *ham++=mod_g | green;
  89.           oGreen=green;
  90.         }
  91.         else
  92.         {
  93.           *ham++=mod_b | blue;
  94.           oBlue=blue;
  95.         }
  96.       }
  97.     }
  98.   }
  99. }
  100.