Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Loading PNG files with transparency

This thread is locked; no one can reply to it. rss feed Print
Loading PNG files with transparency
guru
Member #2,010
March 2002
avatar

I would like to load PNG files with transparency. But I seem to be confused about the actual capabilities of the PNG file format.

Is it possible to have a paletted 256 color PNG file with transparency? I know its possible to have PNG translucency (with an alpha channel) but for now I only want to load a png as a 256 color BITMAP sprite. If I were to draw all BITMAPs with a translucency alpha channel - thats just too much cpu for nothing. (And no - no GL for this render path).

Any suggestions would be greatly appreciated.

GullRaDriel
Member #3,861
September 2003
avatar

I think you'll only have it using 32b mode.
256 is only 8 bit mode. Not enough free space for adding an alpha channel value !

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

A J
Member #3,025
December 2002
avatar

suggestion: use 32bit PNGs, then after they are loaded, convert to 256+alpha+whatever the hell it is your trying to do with alpha channels with 256 colour images.

___________________________
The more you talk, the more AJ is right. - ML

Arthur Kalliokoski
Second in Command
February 2005
avatar

8 bit transparency usually involves reserving one color to ignore, like for a sprite, right? Or are you talking about translucency? (blended stuff like for a glass window in the scene)

They all watch too much MSNBC... they get ideas.

Kitty Cat
Member #2,815
October 2002
avatar

PNG transparency is indeed possible, but it's up to your graphics editor to implement it properly. Translucency (aka alpha transparency) isn't possible with 8-bit, though. At least, not in any way loadpng supports.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

GullRaDriel
Member #3,861
September 2003
avatar

At least, you could have this: a 8bit color png for the color, another one for the alpha value.

BUT you'll need to set_color_depth(32) to draw them correctly

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Andrei Ellman
Member #3,434
April 2003

If you can load your 8-bit PNG into an 8-bit Allegro Bitmap, and use draw_sprite() to draw it, the colour at colour-index #0 will become the transparent colour, just like with PCX files.

AE.

--
Don't let the illegitimates turn you into carbon.

GullRaDriel
Member #3,861
September 2003
avatar

transparent != translucent.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Andrei Ellman
Member #3,434
April 2003

I know that. But guru asked about PNG transparency. AFAIK, 8-bit PNG files do not store an alpha-channel (tranaslucency), but they may store information about which colour-index is to be used as the transparent colour (only one colour can be transparent). Allegro renders sprites using colour-index #0 as the transparent colour, so if the colour-index is a value other than 0, either the program will have to swap colour-index #0 with the transparent colour-index, or the program will have to use custom sprite-drawing code that can take any colour as the transparent colour. 32-bit PNGs store an alpha-channel for each pixel which determines the translucency. A transparent pixel can be implemented by a pixel with full translucency, and an opaque pixel would have it's translucency set to 0.

AE.

--
Don't let the illegitimates turn you into carbon.

Peter Wang
Member #23
April 2000

Quote:

Allegro renders sprites using colour-index #0 as the transparent colour, so if the colour-index is a value other than 0, either the program will have to swap colour-index #0 with the transparent colour-index, or the program will have to use custom sprite-drawing code that can take any colour as the transparent colour.

In fact, Kitty Cat sent me a patch a while ago for loadpng to swap index 0 with the transparent index automatically, but I wasn't sure if I should apply it (and eventually I forgot about it). At least I would make such a feature optional. What do loadpng users think of it?

Ceagon Xylas
Member #5,495
February 2005
avatar

I use 32b and 16b pngs, so I have no problem. Just made the transparent part pink
makecol(255,0,255);

If I were using 8-bit... Yeah I'd like this.

Andrei Ellman
Member #3,434
April 2003

I would like to see this too, but perhaps, this could be optional (a flag would be passed into the PNG loading function, or an additional function could be used to swap 0 with the transparent colour), because otherwise, people will be wondering why their colour-indices were swapped around all of a sudden if the PNG has a transparent colour other than 0.

AE.

--
Don't let the illegitimates turn you into carbon.

Ceagon Xylas
Member #5,495
February 2005
avatar

Like I said, I dont know much about 8-bit pngs, but could you pass a flag that lets you set any of the 256 colors to transparent?

m c
Member #5,337
December 2004
avatar

PNG is quite a cool format.

It supports many different packed pixel formats... one of them being 32bit RGBA....

Now since it supports RGBA, pallete entries can be in the RGBA format instead of RGB.... thus effectively allowing indexed transluceny.

It is possible to have a n-bit PNG have full translucency... but the values are NOT separated from their color counterparts (ie it is all tied to togeather not a separate channel, or else it wouldn't be indexed would it? So #ff000011 and #ff000099 will take two different pallete entries, severly limiting what you can do compared to full color PNGs).

Personally i think that a better approach is to do everything in GIMP or Photoshop, and to save all the PNGs as transparent-index (RGB) images and have a separate grayscale indexed PNG to handle translucency (using allegro's appropriate functions to use the two togeather). You'll end up with better results for things like pretty smoke textures or something...

But if all you want is a simple sprite with AA'd edges.... a RGBA format indexed PNG may have enough entries to accommodate what you need (especially if the sprite has either a black border or vast border regions of the same color).

I use 32bit PNGs though... and i have no clue about how to get loadpng to set you up appropriately... (maybe it should either create a full RGBA 32bit memory bitmap if that environment has been set up, otherwise it'll take in a **BITMAP and return BITMAP[0] as the color image and BITMAP[1] as the grayscale alpha-mask for you to use allegro's functions in that manner).

Besides what i suggested i don't see any other way (but then again i'm not a library aurthor now am i?)

(\ /)
(O.o)
(> <)

BAF
Member #2,981
December 2002
avatar

If you just want transparency with allegro, why dont you just use magic pink? When you load the pngs they are loaded as normal bitmaps, and magic pink will work as transparent with masked_blit and such.

Go to: