Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1943 - Wrong handling of legacy 32bpp BMP files
Browse files Browse the repository at this point in the history
Kang Seonghoon

While BMP format supports alpha channel, it is enabled only when the header is at least 56 bytes long (BITMAPV3INFOHEADER and later). For very common 40-byte-long header (BITMAPINFOHEADER) 32bpp format should be interpreted as BGRX format, but currently SDL interprets them as BGRA format and causes a significant compatibility problem as many 32bpp files use a padding byte of 0 ("transparent" in BGRA interpretation).

---

I fixed this by checking to see if the alpha channel is all 0, and if so, setting it opaque.
  • Loading branch information
slouken committed Jul 8, 2013
1 parent 30e9ad0 commit 3c19ce0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/video/SDL_bmp.c
Expand Up @@ -47,6 +47,35 @@
#endif


static void CorrectAlphaChannel(SDL_Surface *surface)
{
/* Check to see if there is any alpha channel data */
SDL_bool hasAlpha = SDL_FALSE;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int alphaChannelOffset = 0;
#else
int alphaChannelOffset = 3;
#endif
Uint8 *alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
Uint8 *end = alpha + surface->h * surface->pitch;

while (alpha < end) {
if (*alpha != 0) {
hasAlpha = SDL_TRUE;
break;
}
alpha += 4;
}

if (!hasAlpha) {
alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
while (alpha < end) {
*alpha = SDL_ALPHA_OPAQUE;
alpha += 4;
}
}
}

SDL_Surface *
SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
{
Expand All @@ -64,6 +93,7 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
Uint8 *top, *end;
SDL_bool topDown;
int ExpandBMP;
SDL_bool correctAlpha = SDL_FALSE;

/* The Win32 BMP file header (14 bytes) */
char magic[2];
Expand Down Expand Up @@ -182,6 +212,8 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
#endif
break;
case 32:
/* We don't know if this has alpha channel or not */
correctAlpha = SDL_TRUE;
Amask = 0xFF000000;
Rmask = 0x00FF0000;
Gmask = 0x0000FF00;
Expand Down Expand Up @@ -358,6 +390,9 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
bits -= surface->pitch;
}
}
if (correctAlpha) {
CorrectAlphaChannel(surface);
}
done:
if (was_error) {
if (src) {
Expand Down

0 comments on commit 3c19ce0

Please sign in to comment.