PNG image has transparency color issue

Hello, I use Windows XP, and Codeblocks/MingW I have quite a few images loaded into my program with the SDL_image library, loaded as PNG files and I use the colorkey function to set the transparency color. All of the images have worked fine until now, when my latest one ends up not having a transparent color though I set it as with the others. As a side note, this same problem happened before in another program with some smaller images; that problem was solved by switching them to bitmap files. However, with this image I have, switching to a .bmp file didn’t work.

So, here are the steps I take to load the file: 1) I load with IMG_Load(), and 2) I set the color key. That’s it; perhaps someone could shed some light on this, thx in advance. -B

:slight_smile:

It sounds a little like you’re trying (perhaps inadvertently) to combine
colorkeys and an alpha channel. Here’s a read for you:
http://code.bluedinosaurs.com/tutorials/blending.html

http://code.bluedinosaurs.com/tutorials/blending.htmlJonny DOn Mon, Jul 19, 2010 at 9:32 PM, bala_48225 <bala_48225 at yahoo.com> wrote:

Hello, I use Windows XP, and Codeblocks/MingW I have quite a few images
loaded into my program with the SDL_image library, loaded as PNG files and I
use the colorkey function to set the transparency color. All of the images
have worked fine until now, when my latest one ends up not having a
transparent color though I set it as with the others. As a side note, this
same problem happened before in another program with some smaller images;
that problem was solved by switching them to bitmap files. However, with
this image I have, switching to a .bmp file didn’t work.

So, here are the steps I take to load the file: 1) I load with IMG_Load(),
and 2) I set the color key. That’s it; perhaps someone could shed some light
on this, thx in advance. -B

[image: Smile]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

no, nothing that complex, just too many opengl colors and shading overloaded the file format; my new image is better and uses less colors

The problem was caused by things like this:

Code:

SDL_Surface *image = IMG_Load(“somesuch.png”);
if(!image)
{
printf(“it’s an error %s\n”,SDL_GetError());
return(1);
}

And the solution was found on Tim Jones’s SDL Tutorials, involving a temporary surface and the use of the function SDL_DisplayFormat(). Perhaps other newbies who come across the same issue will thus find solace in the aforementioned tutorial, rather than relying on MingW32’s “new SDL project” auto-code!

So, what was the problem, exactly? It still sounds like you had an alpha
channel that you didn’t know about. Using SDL_DisplayFormat would remove
the alpha channel and make it work, of course.

Jonny DOn Sat, Sep 18, 2010 at 7:59 PM, bala_48225 <bala_48225 at yahoo.com> wrote:

The problem was caused by things like this:

Code:

SDL_Surface *image = IMG_Load(“somesuch.png”);
if(!image)
{
printf(“it’s an error %s\n”,SDL_GetError());
return(1);
}

And the solution was found on Tim Jones’s SDL Tutorials, involving a
temporary surface and the use of the function SDL_DisplayFormat(). Perhaps
other newbies who come across the same issue will thus find solace in the
aforementioned tutorial, rather than relying on MingW32’s "new SDL project"
auto-code!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

So, what was the problem, exactly? ?It still sounds like you had an alpha channel that you didn’t know about. ?Using SDL_DisplayFormat would remove the alpha channel and make it work, of course.

Jonny D

I have no doubt that’s true about the alpha channel that I don’t know about, or completely understand how to use it, despite reading your tutorial a few times, since you yourself don’t just totally “give it” to the people quite like your colleague Mr. Jones, with his “here’s a working program” kind of thing. From what I can tell in your Blue Dino tutorial, a person should set the alpha channel instead of setting the normal transparency, but without everything being spelled out, or, “set the alpha channel to the same colors as your transparency color would be”, then I could do it. Just, which code would be replaced with which code, for example? I’ll figure it out eventually if I don’t get that level of info right here from you. I was actually thinking the other day about how alpha blending would be good to darken the screen after the game is over. At any rate, I did feel like a conquering hero when I fixed that transparency and runny screens issue, for sure! Because of u
sing 0 .BMPs, All my images total less than 1MB, which is nice! Being classy is important, no need for excess space usage–but for CPU usage, that’s another story. 800 FPS is a good speed, ja? -B

P.S. I didn’t know you responded till just now.

Yeah, it’s perhaps not spelled out terribly explicitly, but that’s a matter
of style. What I’m saying is that there are two types of PNGs for your
issue. There are PNGs with no alpha channel (RGB), and those with an alpha
channel (RGBA). When you load an RGB PNG, it acts just like you loaded a
BMP. You can apply colorkey transparency because there’s no alpha channel
(the extra byte or so of transparency info in each pixel). If you load an
RGBA PNG, then there is an alpha channel. This alpha info automatically
overrides any other form of transparency (such as colorkeying). The
different forms of transparency cannot be used simultaneously. If you try
to use colorkeying on an RGBA surface, it won’t have any effect. My guess
was that this was your issue.

The extra alpha channel info is added to a PNG when it is created, say, by
the GIMP or Photoshop. Hence, if one of your PNGs is RGBA (like if you
didn’t notice that you used a little bit of transparency in the graphics
editor or you got the image from someone else who did this), then you get
two types of PNGs that you’re trying to treat the same way. One can use
colorkeying, one will not. Some possible (independent) solutions are:

  1. Use an alpha channel for all of your transparency and do not use
    colorkey.
  2. Make sure that your graphics editor saves the files as RGB (with no alpha
    channel). Use colorkey.
  3. Convert your SDL_Surfaces to RGB to strip out unintentional alpha
    channels. Use colorkey.

Numbers 1 and 2 are done in the graphics editor. Number 3 is done with
SDL_DisplayFormat.

Jonny DOn Sat, Oct 2, 2010 at 11:53 PM, bala_48225 <bala_48225 at yahoo.com> wrote:

Quote:

So, what was the problem, exactly? It still sounds like you had an alpha
channel that you didn’t know about. Using SDL_DisplayFormat would remove
the alpha channel and make it work, of course.

Jonny D

I have no doubt that’s true about the alpha channel that I don’t know
about, or completely understand how to use it, despite reading your tutorial
a few times, since you yourself don’t just totally “give it” to the people
quite like your colleague Mr. Jones, with his "here’s a working program"
kind of thing. From what I can tell in your Blue Dino tutorial, a person
should set the alpha channel instead of setting the normal transparency, but
without everything being spelled out, or, “set the alpha channel to the same
colors as your transparency color would be”, then I could do it. Just, which
code would be replaced with which code, for example? I’ll figure it out
eventually if I don’t get that level of info right here from you. I was
actually thinking the other day about how alpha blending would be good to
darken the screen after the game is over. At any rate, I did feel like a
conquering hero when I fixed that transparency and runny screens issue, for
sure! Because of using 0 .BMPs, All my images total less than 1MB, which is
nice! Being classy is important, no need for excess space usage–but for CPU
usage, that’s another story. 800 FPS is a good speed, ja? -B

P.S. I didn’t know you responded till just now.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Lazy Foo’s alpha-blending approach worked fine for me. He uses SDL_ScreenFormat() and then just goes ahead and uses SDL_SetAlhpa().