how do you get the alpha of a pixel?

Started by Monsieur OUXX, Mon 23/10/2017 21:58:08

Previous topic - Next topic

Monsieur OUXX

how do you get the alpha of a pixel in a drawingSurface? By parsing the highest bits of the value obtained through GetPixel?
 

Crimson Wizard

#1
You cannot, AGS explicitly removes alpha in returned value.
To be precise, it returns result of Game.GetColorFromRGB with the rgb values from pixel, which also reduces component precision in returned value, meaning, it is not real 24-bit or 32-bit RGB, but some kind of 16-bit packed RGB.

Raw color manipulations in AGS are still 16-bit, to think of it, even in 32-bit games.

Snarky

You can extract it by creating a white sprite of the same size, using DynamicSprite.CopyTransparencyMask() to copy the alpha mask, and drawing the masked sprite onto a black background, then reading the pixel value. Obviously this will be quite slow.

Gurok

#3
You can speed it up a bit by just working with the pixel you're interested in. Here's a proof of (Snarky's) concept:

Code: ags
int GetPixelAlpha(this DrawingSurface *, int x, int y)
{
  DrawingSurface *surface;
  DynamicSprite *test;
  DynamicSprite *white;
  int alpha;

  if(this.GetPixel(x, y) == COLOR_TRANSPARENT)
    alpha = 0;
  else
  {
    // Create a sprite from the pixel we're interested in
    test = DynamicSprite.CreateFromDrawingSurface(this, x, y, 1, 1);
    white = DynamicSprite.Create(1, 1, true);
    surface = white.GetDrawingSurface();
    surface.Clear(15);
    surface.Release();
    white.CopyTransparencyMask(test.Graphic);
    surface = test.GetDrawingSurface();
    surface.Clear(0);
    surface.DrawImage(0, 0, white.Graphic);
    white.Delete();
    // Obtain the alpha from the green channel
    // From the AGS source:
    // agscolor += ((grn >> 2) & 0x3f) << 5;
    // Green has the most precision
    alpha = (surface.GetPixel(0, 0) >> 5 & 63) << 2 | 3;
    surface.Release();
    test.Delete();
  }

  return(alpha);
}


This returns alpha, so 0 is transparent, 255 is opaque.
[img]http://7d4iqnx.gif;rWRLUuw.gi

SMF spam blocked by CleanTalk