libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.38k stars 1.74k forks source link

New API request: `SDL_ConvertSurfaceToPixels` #10148

Open ankith26 opened 2 months ago

ankith26 commented 2 months ago

Here is my usecase:

Given an SDL_Surface instance, I want to

  1. Convert the pixelformat.
  2. Specify a custom pitch.
  3. Copy the resulting pixel data into an already available buffer.

My current approach is to use SDL_ConvertSurface to get a new surface in the desired format, and then manually copy the data into the pixel buffer. This works but the minor issues here are:

  1. I have to handle the custom pitch myself
  2. SDL does extra work setting up the dest surface and allocing an intermediate buffer.

Then I found out that SDL_ConvertPixels exists, which at a first glance seems to be exactly what I want, but now the problem is this function cannot handle any cases where my input surface has extra data apart from the raw pixel data (e.g indexed surfaces, surfaces with colorkey or surface-alpha, etc)

It would be very appreciated if I can use a variant of SDL_ConvertPixels that takes the src as a SDL_Surface, basically. The function signature could be something like

int SDL_ConvertSurfaceToPixels(SDL_Surface *src, void *dst, Uint32 dst_format, int dst_pitch);

It would also be nice if there was an additional boolean argument called dst_flip which controls if the resulting pixel data is vertically flipped or not, but now I realize at this point I may be asking for too much :)

The reason this argument would be useful is that some libraries (like PyOpenGL) apparently prefer their image data in a flipped format. Atleast this is what a comment in the pygame(-ce) codebase tells me, I've not verified this information myself.

slouken commented 2 months ago

How would your proposed function handle extra data apart from the raw pixel data?

ankith26 commented 2 months ago

I would like SDL_ConvertSurfaceToPixels to apply the pallete colour when converting from indexed -> non-indexed. I would like it to convert colorkey alphas and surface alphas to per pixel alphas when the dst format allows for a dedicated alpha channel.