libsdl-org / SDL_image

Image decoding for many popular formats for Simple Directmedia Layer.
zlib License
570 stars 185 forks source link

generate alpha for non alpha image types with IMG_LoadTexture #372

Closed thrive4 closed 1 year ago

thrive4 commented 1 year ago

Maybe I am overlooking some part of the library but IMG_LoadTexture does not promote alpha to the created texture unless the image file already has alpha.

Quote: 'SDL_image will attempt to create an SDL_Texture in the most format that most reasonably represents the image data (but in many cases, this will just end up being 32-bit RGB or 32-bit RGBA).' https://wiki.libsdl.org/SDL2_image/IMG_LoadTexture

As a very crude work around I am currently utilizing this: So it's read > write > read a) read a file type without alpha support into a surface b) write the surface to a file type that supports alpha c) read the converted file into a texture d) manipulate alpha where needed

pro forma code:

temp_surface = IMG_Load(image) IMG_SavePNG(temp_surface, exepath + "\image.png") SDL_FreeSurface(temp_surface) temp_surface = IMG_LoadTexture(renderer, exepath + "\image.png")

crossfade loop: case "crossfade" SDL_SetTextureAlphaMod(temp_surface, 256 - fade)

Would it be possible / desirable to modify IMG_LoadTexture to: SDL_Texture IMG_LoadTexture(SDL_Renderer renderer, const char *file, bool forcealpha ); or perhaps create a hint? : SDL_HINT_IMAGE_FORCEALPHA https://wiki.libsdl.org/SDL3/CategoryHints

Or maybe there is a another solution I have overlooked that does not involve excessive disk i/o?

Just for reference I have implemented the read>write>read approach in this small app: https://github.com/thrive4/app.fb.slideshow @687

slouken commented 1 year ago

The easier way is probably to load to a surface, convert the surface to RGBA and create a texture from that. I'm closing this since it's not a bug, and feel free to ask questions on the SDL discussion group: https://discourse.libsdl.org/c/sdl-development/6