The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_LoadBMP with alpha produces corrupt image
Rena


Joined: 22 Nov 2015
Posts: 2
Location: Kitchener, Ontario, Canadia
With this test program:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

int main(int argc, char **argv) {
   SDL_Init(SDL_INIT_VIDEO);

   SDL_Surface *surf = SDL_LoadBMP("test.bmp");
   printf("Format is %s, %dbpp\n",
      SDL_GetPixelFormatName(surf->format->format),
      surf->format->BitsPerPixel);

   SDL_FreeSurface(surf);
   SDL_Quit();
   return 0;
}


When loading a bitmap saved as A8 R8 G8 B8 in Gimp, the output is paradoxical:
Format is SDL_PIXELFORMAT_RGB888, 32bpp

32 bits per pixel, but only 3 channels?

Examining the surface, it shows Amask = 0, Aloss = 8, as if the alpha channel is being discarded. However when I then load this in OpenGL, I get a garbled image. I have to manually correct the surface format:

Code:
if(surf->format->format == SDL_PIXELFORMAT_RGB888
&& surf->format->BitsPerPixel == 32) {
   surf->format->format = SDL_PIXELFORMAT_ARGB8888;
   surf->format->Amask  = 0xFF000000;
   surf->format->Aloss  = 0;
   surf->format->Ashift = 24;
}


Then the image loads correctly (since my texture loader trusts surf->format->format to be correct). So seems like more than just discarding the alpha, but actually generating an incorrect format?

The header of my test image:
Code:
00000000  42 4d 8a 60 00 00 00 00  00 00 8a 00 00 00 7c 00  |BM.`..........|.|
00000010  00 00 80 00 00 00 30 00  00 00 01 00 20 00 03 00  |......0..... ...|
00000020  00 00 00 60 00 00 12 0b  00 00 12 0b 00 00 00 00  |...`............|
00000030  00 00 00 00 00 00 00 00  00 ff 00 00 ff 00 00 ff  |................|
00000040  00 00 ff 00 00 00 42 47  52 73 00 00 00 00 00 00  |......BGRs......|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|


Using SDL 2.0.3 on Arch Linux AMD64.