home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmigaPlus / Tools / Development / renderlib40 / src / rnd_alpha.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-02-12  |  6.3 KB  |  275 lines

  1.  
  2. #include "lib_init.h"
  3. #include "lib_debug.h"
  4. #include <render/render.h>
  5. #include <proto/utility.h>
  6. #include <proto/exec.h>
  7.  
  8.  
  9. LIBAPI void InsertAlphaChannelA(UBYTE *chunky, UWORD width, UWORD height, ULONG *rgb, struct TagItem *tags)
  10. {
  11.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  12.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  13.     LONG x;
  14.     ULONG col;
  15.     if (!chunky || !width || !height || !rgb) return;
  16.  
  17.     while (height--)
  18.     {
  19.         for (x = 0; x < width; ++x)
  20.         {
  21.             col = *rgb;
  22.             col &= 0x00ffffff;
  23.             col |= *chunky++ << 24;
  24.             *rgb++ = col;
  25.         }
  26.         rgb += tsw - width;
  27.         chunky += tdw - width;
  28.     }
  29. }
  30.  
  31. LIBAPI void ExtractAlphaChannelA(ULONG *rgb, UWORD width, UWORD height, UBYTE *chunky, struct TagItem *tags)
  32. {
  33.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  34.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  35.     LONG x;
  36.     if (!chunky || !width || !height || !rgb) return;
  37.  
  38.     while (height--)
  39.     {
  40.         for (x = 0; x < width; ++x)
  41.         {
  42.             *chunky++ = *rgb++ >> 24;
  43.         }
  44.         rgb += tsw - width;
  45.         chunky += tdw - width;
  46.     }
  47. }
  48.  
  49. LIBAPI void ApplyAlphaChannelA(ULONG *src, UWORD width, UWORD height, ULONG *dst, struct TagItem *tags)
  50. {
  51.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  52.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  53.     LONG taw = GetTagData(RND_AlphaWidth, width, tags);
  54.     UBYTE *alpha = (UBYTE *) GetTagData(RND_AlphaChannel, (ULONG) src, tags);
  55.     LONG alphaoffs = GetTagData(RND_AlphaModulo, 4, tags);
  56.     LONG x, s, d, a;
  57.     ULONG col1, col2, rgb;
  58.     if (!src || !width || !height || !dst || !alpha) return;
  59.     
  60.     while (height--)
  61.     {
  62.         for (x = 0; x < width; ++x)
  63.         {
  64.             a = *alpha;
  65.             col1 = *src++;
  66.             col2 = *dst;
  67.  
  68.             s = (col1 & 0xff0000) >> 16;
  69.             d = (col2 & 0xff0000) >> 16;
  70.             s += (((d - s) * a) >> 8);
  71.             rgb = s << 8;
  72.  
  73.             s = (col1 & 0x00ff00) >> 8;
  74.             d = (col2 & 0x00ff00) >> 8;
  75.             rgb |= s + (((d - s) * a) >> 8);
  76.             rgb <<= 8;
  77.  
  78.             s = (col1 & 0x0000ff);
  79.             d = (col2 & 0x0000ff);
  80.             rgb |= s + (((d - s) * a) >> 8);
  81.             
  82.             rgb |= (col2 & 0xff000000);        /* keep alpha channel in dest */
  83.             
  84.             *dst++ = rgb;
  85.             alpha += alphaoffs;
  86.         }
  87.  
  88.         alpha += (taw - width) * alphaoffs;
  89.         src += tsw - width;
  90.         dst += tdw - width;
  91.     }
  92. }
  93.  
  94. LIBAPI void TintRGBArrayA(ULONG *src, UWORD width, UWORD height, ULONG rgb, UWORD ratio, ULONG *dst, struct TagItem *tags)
  95. {
  96.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  97.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  98.     LONG r = (rgb & 0xff0000) >> 16;
  99.     LONG g = (rgb & 0x00ff00) >> 8;
  100.     LONG b = (rgb & 0x0000ff);
  101.     LONG s, x;
  102.     ULONG col;
  103.  
  104.     if (!src || !width || !height || !dst) return;
  105.  
  106.     while (height--)
  107.     {
  108.         for (x = 0; x < width; ++x)
  109.         {
  110.             col = *src++;
  111.  
  112.             s = (col & 0xff0000) >> 16;
  113.             s += (((r - s) * ratio) >> 8);
  114.             rgb = s << 8;
  115.  
  116.             s = (col & 0x00ff00) >> 8;
  117.             rgb |= s + (((g - s) * ratio) >> 8);
  118.             rgb <<= 8;
  119.  
  120.             s = (col & 0x0000ff);
  121.             rgb |= s + (((b - s) * ratio) >> 8);
  122.             
  123.             rgb |= (col & 0xff000000);        /* keep alpha channel in dest */
  124.             
  125.             *dst++ = rgb;
  126.         }
  127.         src += tsw - width;
  128.         dst += tdw - width;
  129.     }
  130. }
  131.  
  132. LIBAPI void MixRGBArrayA(ULONG *src, UWORD width, UWORD height, ULONG *dst, UWORD ratio, struct TagItem *tags)
  133. {
  134.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  135.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  136.     LONG x, s, d;
  137.     ULONG col1, col2, rgb;
  138.     if (!src || !width || !height || !dst) return;
  139.     
  140.     ratio = 255 - ratio;        /* ! */
  141.     
  142.     while (height--)
  143.     {
  144.         for (x = 0; x < width; ++x)
  145.         {
  146.             col1 = *src++;
  147.             col2 = *dst;
  148.  
  149.             s = (col1 & 0xff0000) >> 16;
  150.             d = (col2 & 0xff0000) >> 16;
  151.             s += (((d - s) * ratio) >> 8);
  152.             rgb = s << 8;
  153.  
  154.             s = (col1 & 0x00ff00) >> 8;
  155.             d = (col2 & 0x00ff00) >> 8;
  156.             rgb |= s + (((d - s) * ratio) >> 8);
  157.             rgb <<= 8;
  158.  
  159.             s = (col1 & 0x0000ff);
  160.             d = (col2 & 0x0000ff);
  161.             rgb |= s + (((d - s) * ratio) >> 8);
  162.             
  163.             rgb |= (col2 & 0xff000000);        /* keep alpha channel in dest */
  164.             
  165.             *dst++ = rgb;
  166.         }
  167.         src += tsw - width;
  168.         dst += tdw - width;
  169.     }
  170. }
  171.  
  172. LIBAPI void MixAlphaChannelA(ULONG *src1, ULONG *src2, UWORD width, UWORD height, ULONG *dst, struct TagItem *tags)
  173. {
  174.     LONG tsw1 = GetTagData(RND_SourceWidth, width, tags);
  175.     LONG tsw2 = GetTagData(RND_SourceWidth2, width, tags);
  176.     LONG tdw = GetTagData(RND_DestWidth, width, tags);
  177.     UBYTE *alpha1 = (UBYTE *) GetTagData(RND_AlphaChannel, (ULONG) src1, tags);
  178.     UBYTE *alpha2 = (UBYTE *) GetTagData(RND_AlphaChannel2, (ULONG) src2, tags);
  179.     LONG taw1 = GetTagData(RND_AlphaWidth, width, tags);
  180.     LONG taw2 = GetTagData(RND_AlphaWidth2, width, tags);
  181.     LONG aoffs1 = GetTagData(RND_AlphaModulo, 4, tags);
  182.     LONG aoffs2 = GetTagData(RND_AlphaModulo2, 4, tags);
  183.     LONG x, a, s, d;
  184.     ULONG col1, col2, rgb;
  185.  
  186.     if (!src1 || !src2 || !width || !height || !dst) return;
  187.  
  188.     while (height--)
  189.     {
  190.         for (x = 0; x < width; ++x)
  191.         {
  192.             a = 0;
  193.  
  194.             if (alpha2)
  195.             {
  196.                 a = 255 - *alpha2;
  197.                 alpha2 += aoffs2;
  198.             }
  199.  
  200.             if (alpha1)
  201.             {
  202.                 a += *alpha1;
  203.                 alpha1 += aoffs1;
  204.  
  205.                 if (alpha2) a >>= 1;
  206.             }
  207.             
  208.             col1 = *src1++;
  209.             col2 = *src2++;
  210.             
  211.             s = (col1 & 0xff0000) >> 16;
  212.             d = (col2 & 0xff0000) >> 16;
  213.             d += (((s - d) * a) >> 8);
  214.             rgb = d << 8;
  215.  
  216.             s = (col1 & 0x00ff00) >> 8;
  217.             d = (col2 & 0x00ff00) >> 8;
  218.             rgb |= d + (((s - d) * a) >> 8);
  219.             rgb <<= 8;
  220.  
  221.             s = (col1 & 0x0000ff);
  222.             d = (col2 & 0x0000ff);
  223.             rgb |= d + (((s - d) * a) >> 8);
  224.             
  225.             rgb |= (*dst & 0xff000000);        /* keep alpha channel in dest */
  226.             
  227.             *dst++ = rgb;
  228.         }
  229.         
  230.         if (alpha1) alpha1 += (taw1 - width) * aoffs1;
  231.         if (alpha2) alpha2 += (taw2 - width) * aoffs2;
  232.         
  233.         src1 += tsw1 - width;
  234.         src2 += tsw2 - width;
  235.         dst += tdw - width;
  236.     }
  237. }
  238.  
  239.  
  240.  
  241. #define ABS(x) ((x) >= 0 ? (x) : -(x))
  242.  
  243. LIBAPI void CreateAlphaArrayA(ULONG *rgb, UWORD width, UWORD height, struct TagItem *tags)
  244. {
  245.     LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  246.     LONG taw = GetTagData(RND_AlphaWidth, width, tags);
  247.     UBYTE *dst = (UBYTE *) GetTagData(RND_AlphaChannel, (ULONG) rgb, tags);
  248.     LONG aoffs = GetTagData(RND_AlphaModulo, 4, tags);
  249.     ULONG maskrgb = GetTagData(RND_MaskRGB, 0x000000, tags);
  250.     LONG x;
  251.     ULONG col;
  252.     LONG r, g, b;
  253.  
  254.     if (!width || !height || !rgb || !dst) return;
  255.  
  256.     while (height--)
  257.     {
  258.         for (x = 0; x < width; ++x)
  259.         {
  260.             col = *rgb++;
  261.             r = (maskrgb & 0xff0000) >> 16;
  262.             r -= (col & 0xff0000) >> 16;
  263.             g = (maskrgb & 0x00ff00) >> 8;
  264.             g -= (col & 0x00ff00) >> 8;
  265.             b = (maskrgb & 0x0000ff);
  266.             b -= (col & 0x0000ff);
  267.             *dst = 0.299f * ABS(r) + 0.587f * ABS(g) + 0.114f * ABS(b);
  268.             dst += aoffs;
  269.         }
  270.         
  271.         rgb += tsw - width;
  272.         dst += (taw - width) * aoffs;
  273.     }
  274. }
  275.