The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL texture scale down with 'linear' filter
JohnD


Joined: 18 Feb 2015
Posts: 4
Hi everyone,

I am new to SDL and i am developing a 2D game using SDL 2.0.3.
I use textures with power of 2 sizes.
I want to scale down textures but i have the following issue:
-When i render a texture by scaling it down to 50% of original size using linear filter ( SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); ) new image is fine (anti-aliased). So filter is applied.
-When i render the same texture by scaling it down to 25% of original size, image is not fine (aliased - hard edges). It looks like 'linear' filter is not applied or not effective enough.

I scale down a texture by using SDL_RenderCopy and modifying destination rect to new size.

Trying to figure out what is happening i made the following test:
Take original texture and render it to a new texture by scaling it down at 50% of original. Then take new texture and render it on screen by scaling down at 50% of new one.
So final image is 25% of original. Result is fine (anti-aliased). So there is a method to have a 25% of original texture anti-aliased, but this way seems to be a bad practice.

Other scaling methods like SDL_RenderSetScale or SDL_RenderSetLogicalSize have the same poor results.

Are all the above a normal behaviour? Is 'linear' filter only good for scale down up to 50% of original? Or am i missing something?
If everything is fine how can i scale down to 25% having anti-aliased results with smooth edges?

Thanks
MikeyPro


Joined: 14 Feb 2015
Posts: 15
I don't have an answer for you, but a suggestion: Don't scale down the original texture more than once. If you have to scale it down, use the original size and image and scale it down by the amount you need. Each time you scale, you lose valuable pixel information and it wont be able to maintain the 'essence' of the image no matter what you do. If you have to scale a 50% scaled image 50% more, scale the original by 75%, or down to 25%. There is no reason that I can think of no to do this, and it is essentially the correct way to do scaling. Maybe you have something else going on forcing you to do stacked scaling, but it is always a bad idea to do so because you will always lose pixel information each time you scale.
JohnD


Joined: 18 Feb 2015
Posts: 4
MikeyPro, thanks for your answer.

My intention is to scale down texture to 25% (1/4) of original size. When i do this with one step, as you suggest, result is not satisfying, as image become aliased with hard edges.
Two step scale down, gives fine results but as you say it is a bad idea and for sure not the proper way.
MikeyPro


Joined: 14 Feb 2015
Posts: 15
Okay, I didn't understand what you were saying in the op. So when you stack the scaling, the filtering is applied, but when you scale once by a large amount, it creates undesirable hard edges.

Hm. So that is interesting. I did a test myself using opengl hardware acceleration in windowed and fullscreen mode with the quality being set to best (linear) and was able to scale an image down by 75% of it's original size and it looked great.

Using simple tests, scaling with linear quality using RenderCopy works perfectly fine on my end. I could scale it down 92% before it starts looking slightly jagged on a 96 x 103 image. I even tried scaling the width and height differently and while the aspect was ruined, it was still butter smooth. Maybe something in your SDL setup? Video card settings forcing low texture quality?
JohnD


Joined: 18 Feb 2015
Posts: 4
MikeyPro,

I have one question. Is there a possibility that you had mipmaps creation enabled in your test?

It seems that i have to keep searching what is going on in my code or configuration.
I tested my app in another PC with same results.

Just to give you an idea of what i see download a small file of 78kb
http://wikisend.com/download/519750/LinearFilter.png

Thanks again for spending your time, trying to help me.[/url]
MikeyPro


Joined: 14 Feb 2015
Posts: 15
With fine grids like that you can't expect to scale it down and it be pixel perfect or smooth. Your problem with the grid is just the nature of the beast. If you want grids, I suggest you use line & rectangle rendering instead.
JohnD


Joined: 18 Feb 2015
Posts: 4
MikeyPro,

sorry for the misunderstanding. I am not using any grid in my textures.
Using grid it was just an example that can clearly show the differences between one step and two step scaling.

Anyway, from your answer i can understand that there nothing wrong in my code. What i see is what SDL and opengl underneath can do.
I also tried SDL_RENDERER_SOFTWARE flag to see what happens and things got worse.

Thanks a lot for your support Smile