sdl-gpu icon indicating copy to clipboard operation
sdl-gpu copied to clipboard

Blit with linear filtering bleeds adjacent pixels from outside the source region

Open mutability opened this issue 7 years ago • 1 comments

Simplest way to explain this is with a demo:

sdlgpu-filtering

Source: https://gist.github.com/mutability/82eea1c8f0b092cd7710a74922caa001

Left side is the source GPU_Image, 16x16, scaled up so you can see it.

Right side is the results of GPU_BlitRect with a source rectangle that covers the red square only:

  • Top: GPU_FILTER_NEAREST
  • Middle: GPU_FILTER_LINEAR
  • Bottom: GPU_FILTER_LINEAR with snap disabled and adding in a half-pixel correction

As I understand it, the problem is that the texture coordinates that BlitRect computes lie on the texel boundaries, not texel centers. Probably GPU_Blit* should do the necessary corrections itself rather than requiring the caller to juggle this.

mutability avatar Jan 02 '17 00:01 mutability

I have a similar problem if the image is big enough GPU_FILTER_LINEAR is used and the image becomes blurry. Using GPU_FILTER_NEAREST fix the problem.

GPU_Target *screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, 256, 256, GPU_DEFAULT_INIT_FLAGS);
GPU_Image *img = GPU_LoadImage("test2.png");
GPU_Image* sheet = GPU_CreateImage(256, 256, GPU_FORMAT_RGBA);
GPU_Target* target = GPU_LoadTarget(sheet);

GPU_BlitTransformX(img, NULL, target, img->base_h, 0, 0, 0, 90, 1, 1);
GPU_SaveImage(sheet, "result.png", GPU_FILE_PNG);

Original image : test2 Result : result

NicolasPerdu avatar Jan 02 '17 20:01 NicolasPerdu