Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Dave Gilbert on Sat 21/08/2010 22:24:33

Title: Alpha channel issue (AGS 3.2)
Post by: Dave Gilbert on Sat 21/08/2010 22:24:33
Hello!  I am having a problem importing an image with an alpha channel.  I could swear that I was able to once do this, but for some reason it's beyond me now.

Here is how the background looks in Photoshop:

(http://www.davelgil.com/boe/dec/ent_BG.png)

The light coming from the window is on a separate layer.  I exported the light source as a PNG (which is here (http://www.davelgil.com/boe/dec/ent_highlight.png) for reference) which I imported into AGS with an alpha channel included.  The result?

(http://www.davelgil.com/boe/dec/game_BGb.png)

Holy blindness, Batman! The alpha channel/transparency is definitely there, as my character is able to stand behind the light.  But obviously the light itself is waaay too bright.  Is this a problem with the current version of AGS?  I used to be able to import light sources this way without any problems.  I'm a bit confuzzled!  Any tips are appreciated!

Thanks,

-Dave
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Dave Gilbert on Sat 21/08/2010 22:54:25
Right, duh. Nevermind.  Apparently the artist created that highlight layer as an "overlay", which wouldn't export the same the way it appears within Photoshop. So... go ahead and lock this.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: subspark on Mon 23/08/2010 16:10:43
Actually, before you do lock this thread, I just want to say that it would be nice if AGS could make use of blend modes like this.
It has been mentioned several times in the wish list and you've provided a good visual example of what to expect.

Cheers,
Sparky.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 16:13:29
providing I have the blending formula, the agsblend plugin has the framework all worked out.

If you have any specific requests, let me know.

(a link to the blending algorithm would be nice if you could find it)
Title: Re: Alpha channel issue (AGS 3.2)
Post by: subspark on Mon 23/08/2010 16:19:21
Sure I'll have a look Calin. Thanks.

Edit: Gotcha! http://www.nathanm.com/photoshop-blending-math/ (http://www.nathanm.com/photoshop-blending-math/)
Title: Re: Alpha channel issue (AGS 3.2)
Post by: GarageGothic on Mon 23/08/2010 16:25:17
There's a good list of blend modes that I've been using myself on Wikipedia (http://en.wikipedia.org/wiki/Blend_modes). This  other page (http://www.simpelfilter.de/en/basics/mixmods.html) has more algorithms, including overlay.

Edit: Sparky posted while I was looking through my bookmarks, but maybe this is still useful.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: subspark on Mon 23/08/2010 16:27:56
Some good examples there too, Garage. :)
Sparky.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 16:29:44
Quote from: subspark on Mon 23/08/2010 16:19:21
Edit: Gotcha! http://www.nathanm.com/photoshop-blending-math/ (http://www.nathanm.com/photoshop-blending-math/)

oooooOOOOoooo, I can stick those macros pretty much straight into the code as written
Title: Re: Alpha channel issue (AGS 3.2)
Post by: subspark on Mon 23/08/2010 16:30:31
SCHAWEEET!!!  ;D
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Dave Gilbert on Mon 23/08/2010 16:50:19
That's pretty awesome, Cailin.  Can you update this thread when you update the other one?
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 18:17:26
http://www.thethoughtradar.com/AGS/agsblend0.5.zip

Try this.

Usage:

DrawSprite(dest.Graphic, src.Graphic, x, y, mode);

the function is a little cleverer now and shouldnt crash if you pass it a sprite that is too big or your x,y coordinates dont make sense.
It will still crash or behave oddly if both sprites arent 32-bit.

It's also now independent of allegro so the allegro dll is no longer needed.

mode is an int between 0 and 23. Heres an enum to make life easier.


enum eBlendModes{
  eBlendNormal = 0,
  eBlendDarken = 1,   
  eBlendLighten = 2,
  eBlendMultiply = 3,
  eBlendAdd = 4,
  eBlendSubtract = 5,
  eBlendDifference = 6,
  eBlendNegation = 7,
  eBlendScreen = 8,
  eBlendExclusion = 9,
  eBlendOverlay = 10,
  eBlendSoftlight = 11,
  eBlendHardlight = 12,
  eBlendColorDodge = 13,
  eBlendColorBurn = 14,
  eBlendLinearDodge = 15,
  eBlendLinearBurn = 16,
  eBlendLinearLight = 17,
  eBlendVividLight = 18,
  eBlendPinLight = 19,
  eBlendHardMix = 20,
  eBlendReflect = 21,
  eBlendGlow = 22,
  eBlendPhoenix = 23
};


lemme know how you get on.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Knox on Mon 23/08/2010 19:13:15
A "ResizeFilter" (with bilinear) for alpha channel would be great with AGSBlend  ;D  ;D
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 19:22:55
Ah shit, that upload wont work, I forgot to update the header prototype.

Will fix tomorrow when i get home from work.

As for a bilinear resize, I'm not sure if the API gives you any method of resizing a sprites surface and i'm not sure how to return a new sprite from a function.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Knox on Mon 23/08/2010 19:44:32
I dont know if this sounds stupid (I really dont know how it all works, really!) but is it somehow possible to "extract" the alpha channel from the png and convert it onto a dynamic sprite that is only a b/w (black+white) opaque image...apply Khris's resizeFilter to that opaque image, and  then reconvert/reapply that newly resized+ filtered b/w image to the png's alpha channel (?)

Again, I really dont know how it all works :-X

I can help test out the blendmodes + post various results if you wish
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 19:46:43
yea the way you describe is essentially the only way to do it. The GetAlpha() and PutAlpha() functions can do for the alpha what khris' module does for the RGB.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Wyz on Mon 23/08/2010 20:07:50
Wow, photoshop blending modes would be very nifty! I was dying to use that for a game a while back, will this would definitely be a welcome addition. :D
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 20:17:25
It does work, promise  ::)

I just changed the function before compile and forgot to update the prototype and now im at work so i cant fix it.. i'll fix it in the morning.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Knox on Mon 23/08/2010 20:22:59
Quoteyea the way you describe is essentially the only way to do it. The GetAlpha() and PutAlpha() functions can do for the alpha what khris' module does for the RGB

holy crap so you're telling me I can use those funtions in conjunction with khris' module and it will work (well once you update it tomorrow)?

if so, ahhhhhhhhh YEAH!
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Mon 23/08/2010 20:33:35
Yea, although I looked through khris' module and I'm not 100% sure how it works... I are not as clever as Khris.

But!

As a very poor work around you could make a copy of the sprite using the GetAlpha and then run khris resize on that and then once it has been resized, plug the values back into the sprite.. so something like this.

Make a copy of your sprite at full size
iterate through each pixel of the original using GetAlpha to get its value.
Draw on the copy a grey value representing the alpha.
like:

alpha = GetAlpha(orig.Graphic, x, y);
ds.DrawingColor = Game.GetColorFromRGB(alpha,alpha,alpha); //this is grey
ds.DrawPixel(x,y);


then resize both your original and copy(which contains the alphas)

then iterate through the pixels again putting the alpha back into the original in accordance with the grey values stored in the copy.

This method might not be as inefficient as it seems since I dont think you can keep the drawingsurface open whilst running GetAlpha or PutAlpha so this way you only have to get/release the drawing surfaces a handful of times.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Tue 24/08/2010 07:36:22
There!
NOW it should work:

http://www.thethoughtradar.com/AGS/agsblend051.zip
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Dualnames on Tue 24/08/2010 08:02:16
enum eBlendModes{
  eBlendNormal = 0,
  eBlendDarken = 1,   
  eBlendLighten = 2,
  eBlendMultiply = 3,
  eBlendAdd = 4,
  eBlendSubtract = 5,
  eBlendDifference = 6,
  eBlendNegation = 7,
  eBlendScreen = 8,
  eBlendExclusion = 9,
  eBlendOverlay = 10,
  eBlendSoftlight = 11,
  eBlendHardlight = 12,
  eBlendColorDodge = 13,
  eBlendColorBurn = 14,
  eBlendLinearDodge = 15,
  eBlendLinearBurn = 16,
  eBlendLinearLight = 17,
  eBlendVividLight = 18,
  eBlendPinLight = 19,
  eBlendHardMix = 20,
  eBlendReflect = 21,
  eBlendGlow = 22,
  eBlendPhoenix = 2



Does those modes work for realz?
Cause if they do, I'm marrying you Calin.
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Tue 24/08/2010 08:20:42
they should do, although I havent tested them all against the photoshop versions.

Also I had to make up the code that factors in the alpha channel so I cant guarantee that alpha blending is exactly the same as photoshop handles it.

I can't really see how people would really use these at runtime except for very niche effects but ya know.. I answered the call of justice!
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Dualnames on Tue 24/08/2010 08:22:23
Quote from: Calin Leafshade on Tue 24/08/2010 08:20:42
they should do, although I havent tested them all against the photoshop versions.

Also I had to make up the code that factors in the alpha channel so I cant guarantee that alpha blending is exactly the same as photoshop handles it.

I can't really see how people would really use these at runtime except for very niche effects but ya know.. I answered the call of justice!

I can use those at runtime, they'd be like WOW. Well, downloading before you realize your mistake.
*I'm just being so badass today!*
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Tue 24/08/2010 08:24:16
oh also, update to the usage:

DrawSprite(dest.Graphic, src.Graphic, x, y, mode, trans);

trans is in the same format as AGS (0 = opaque)

Edit: Fixed some issues with the coordinate system and normal blending:

http://www.thethoughtradar.com/AGS/agsblend055.zip
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Knox on Wed 25/08/2010 01:45:49
If its not too much to ask, is it possible to add something like this as a function to your plugin? (quicker and more compact then doing it old school, I think) :

greyValue = GetRGB(alphaSprite.Graphic, x, y);
PutAlpha(inv_curs.Graphic, x, y, greyValue);

We can then get gray values from an opaque black and white sprite and convert it into transparency with your PutAlpha :D

(unless of course Im retarded and this exists already) **but not GetColorFromRGB**
Title: Re: Alpha channel issue (AGS 3.2)
Post by: Calin Leafshade on Wed 25/08/2010 07:43:52
in a monochrome colour all the values of RGB are the same so you just need to grab one of them.

this function will do that


enum eRGBComponent {
  eRed,
  eGreen,
  eBlue,
  };

function GetRGBComponent(int color, eRGBComponent component) {
  if (component == eRed) return 8*(color/2048);
  if (component == eGreen) return 8*((color%2048)/64);
  int b = (8*((color%2048)%64));
  if (b > 255) return (8*(((color%2048)%64)-31))-1;
  return b;
  }


The red colour value is the least computationally expensive one so use that one.