Hello,
I’ve been struggling with the following problem for some days and I’m hoping for someone to give me a hand.
I recently discovered a problem with SDL 2.0.10 that does not occur with SDL 2.0.9 regarding displaying PNG images with latest SDL_image 2.0.5. Some PNG images with transparency look incorrect in 2.0.10 while they used to work in 2.0.9.
I prepared the following very simple test C program for MSYS2 MinGW (Windows but confirmed it also happens in Linux) to showcase the problem with a sample PNG file attached to this topic:
// Build: x86_64-w64-mingw32-gcc sdl-show.c -o sdl-show -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
// Run: ./sdl-show.exe <IMAGE_FILE>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
int main(int argc, char* args[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("SDL Image Test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *image = IMG_Load(args[1]);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
SDL_Rect pos = {
.x=(SCREEN_WIDTH - image->w) / 2,
.y=(SCREEN_HEIGHT - image->h) / 2,
.w=image->w, .h=image->h
};
SDL_SetRenderDrawColor(renderer, 0x40, 0x40, 0x40, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_Delay(5000);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
For the text below, see this Imgur album: https://imgur.com/a/Jg2EXc0
When you run this sample program with the attached PNG file using SDL 2.0.9 or below and SDL_image 2.0.5 you get the expected output (image 1 in album)
However when the same program is run with the same image under SDL 2.0.10 with the same SDL_image 2.0.5, you get an incorrect output (image 2 in album)
As you can observe, the transparency data of the PNG file gets messed up. This only started to happen since I upgraded to SDL 2.0.10 on my machine.
I’m kindda stuck with this at the moment. Anyone has a clue of what can be the problem? Is this a bug in SDL 2.0.10 or maybe SDL_image needs some updates to work with SDL 2.0.10 ? Is there an undocumented change in behaviour? If that is the case, any suggestion to programatically restore the old behaviour?
Thanks a lot for your help!
Hugo
The testing PNG file is attached below (your browser probably renders the transparency correctly too):