From 36260e999d542a1620534147abd3908337325660 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Fri, 3 Jun 2011 12:47:49 +0200 Subject: [PATCH] PNG: Only add alpha channel on surface if loaded image has an alpha channel. This fixes a bug where a loaded image could have an alpha of 0. Also it avoids expensive alpha blending where it is unnecessary. --- src/imageio.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/imageio.cpp b/src/imageio.cpp index b9b8921..177d073 100644 --- a/src/imageio.cpp +++ b/src/imageio.cpp @@ -14,6 +14,7 @@ SDL_Surface *loadPNG(const std::string &path) { SDL_SetAlpha(surface, SDL_SRCALPHA, 255); return surface; } else { + bool hasAlphaChannel = surface->format->Amask != 0; SDL_PixelFormat format32; memset(&format32, 0, sizeof(format32)); format32.BitsPerPixel = 32; @@ -23,13 +24,13 @@ SDL_Surface *loadPNG(const std::string &path) { format32.Gmask = 0x0000FF00; format32.Bmask = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000; - format32.Amask = 0xFF000000; + format32.Amask = hasAlphaChannel ? 0xFF000000 : 0; format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0; format32.Gshift = 8; format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16; - format32.Ashift = 24; - SDL_Surface *surface32 = - SDL_ConvertSurface(surface, &format32, SDL_SRCALPHA); + format32.Ashift = hasAlphaChannel ? 24 : 0; + SDL_Surface *surface32 = SDL_ConvertSurface( + surface, &format32, hasAlphaChannel ? SDL_SRCALPHA : 0); SDL_FreeSurface(surface); return surface32; }