Load Images with alpha

Hello. I am new here.
What is the best way to handle alpha in games?
Load a .png image with alpha channel or load a .jpg image with some background color and set alpha in game?
.png files are larger (most of the times), but what about performance?

Well what graphics library are you using?

I would assume that loading a .png file with alpha will be much quicker than loading a .jpg and changing the background b/c (depending on the library you are using) will take up more time than just loading it with the alpha already there.

I’m using Graphics2D from java.awt.

@ricardo
This function should load images with transparency.


public Image loadImage(String name)
{
    return new ImageIcon(name).getImage();
}

Hope it helps.

Thank you but I already know how to load images with / without transparency.
I just didn’t know what was the best way to load alpha.

I will use .png files. Thank you all.

Alpha is just another color channel in an image, there isn’t a better way or a worse way.

I imagine that not having an alpha channel can reduce the size of the texture on disk and in your memory cache (if you cache textures) and I can see that having some significance for very large games or games being run on limited hardware.

You can do the transparency (with for example, a colour Magenta) at the shader level with virtually zero performance hit. Preprocessing them and giving them an alpha channel defeats the purpose.

I wouldn’t worry about it and would just use your png with an alpha channel.

Thank you.
I will use .png for now, maybe if I need a little more fps I will try set alpha in-game.

You misunderstand; file format choice has zero impact upon potential frame rate.

You should choose a file format that suits your data, that is all.

Png is a good choice to start with, as it uses lossless compression, is flexible, and is widely supported by both APIs & editors.

PNG being lossless is also an important thing. Using JPG images may result in compression artefacts that could make your game ugly.