Downsizing Large Images with SDL2 and SDL_Image

Hi everyone,

I’m currently facing an issue with downscaling very large images using SDL2 and SDL_Image. I’ve set SDL_HINT_RENDER_SCALE_QUALITY to linear, but the results are not satisfactory. I’m wondering if it’s possible to achieve better downscaling purely with SDL_Image and SDL2. Could someone please guide me on the correct approach and provide some code snippets if possible? Your assistance is highly appreciated!

The image rendered using SDL appears excessively sharp and contains noticeable noise.

I think this is an inherent problem with bilinear interpolation. It’s simply not good at downscaling to much smaller sizes because there are a lot of pixels that gets ignored and doesn’t contribute to the result. You might find this discussion interesting.

2 Likes

To address this issue, I’ve implemented a solution using the stb_image_resize. Prior to creating the texture, I utilized the stbir_resize_uint8_linear function. This involved passing the original surface pixels as the source and the resized surface as the destination, resulting in a more refined image quality.

pseudo code:

SDL_Surface *surface = IMG_Load(path);
SDL_Surface *resized_surface = SDL_CreateRGBSurface(0, {{width}}, {height}},
                                                      surface->format->BitsPerPixel,
                                                      surface->format->Rmask,
                                                      surface->format->Gmask,
                                                      surface->format->Bmask,
                                                      surface->format->Amask);

stbir_resize_uint8_linear(surface->pixels, surface->w, surface->h, 0,
                          resized_surface->pixels, {{width}}, {{height}}, 0,
                          surface->format->BytesPerPixel);

SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, resized_surface);