Image downscaling

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
JensRex

Image downscaling

Unread post by JensRex » 2014-08-25, 08:19

In 24.7 the image downscaling was changed to what looks like a nearest neighbor algorithm... because reasons. Is it going to be changed back to a high quality algorithm? Soon? It looks really really bad, and it's a constant annoyance. I know MC didn't do this to offend me personally of course, but I just hope I don't have to deal with this much longer.

New Tobin Paradigm

Re: Image downscaling

Unread post by New Tobin Paradigm » 2014-08-25, 08:21

According to the release notes for v24.7.0:
Disabled HQ image downscaling. This is a workaround for the broken Mozilla HQ downscaling back-end causing constant invalidations and redrawing if 2 downscaled images with the same source were in view.
I am sure once a better solution is devised and/or the Mozilla HQ downscaling can be repaired it shall be re-enabled. If you can find a related bug on bugzilla that addresses this issue or want to submit a patch it is always welcome ;)

EDIT: Since it IS a pref you could flip it back on but I can't be held responsible for the performance loss. Also since this is not recommended you will have to find the pref your self. Though if you look at the commit log on the relbranch you should be able to find the change ;)

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-08-25, 09:12

The related bug (filed but not worked on) is bug #977459. Considering it depends on "DrawAPIRefactor", this will be yet another bug that won't be possible to just take from the Mozilla code base (when it's done) and the main reason it's not being fixed right now (they want to refactor first, then actually make improvements). Gone are the days where massive performance regression was top priority. And trust me, rapid-fire image invalidations and constant re-scaling will push your CPU pretty hard.

I'll have to look into finding a way around this without relying on the refactoring work. This will take time.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-08-25, 12:05

As a follow-up: I couldn't imagine that nearest-neighbor would be used for any sort of downscaling, and it's not.
The default algorithm is bilinear, which is pretty decent at its job and might only start causing issues if you downscale by a (very) large amount.

If you encounter nearest-neighbor scaling, then something else might be going on. Can you please post the output of Help -> Troubleshooting information (as text), and an example of a problematic downscaled image (and link to the original)?
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

JensRex

Re: Image downscaling

Unread post by JensRex » 2014-08-25, 12:39

Support text spam: Pardon the image example, but it has features that makes it good for illustrating the scaling issues. The "line burst" graphic in the background speaks for itself. The shadow on her left leg looks extremely jagged, as do the red carpet background against her legs. Her hair looks all pixel-ly.

Example of crappy downscaling: Original image:

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-08-25, 18:57

Yup, as said if you downscale by a (very) large amount, it would cause issues. the photo in question is 3600px high, so you'd be downscaling it by a factor 5 or so. You can expect issues with that with bilinear because it won't have the samples needed to properly smooth it out.
I'll be re-enabling the hq downscaling and making the downscaling more selective in the next version to mitigate the invalidation performance issue (reducing the number of cases where it would occur) while still doing a better job at super-scaling very large images. It'll still be a workaround until a better solution is found.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

New Tobin Paradigm

Re: Image downscaling

Unread post by New Tobin Paradigm » 2014-08-25, 19:20

The source image that was used to test downscailing is also heavily jpg compressed. Which could be somewhat contributing to the inaccurate downsampling.

User avatar
__NM64__
Lunatic
Lunatic
Posts: 359
Joined: 2013-10-17, 05:29
Location: Northeast Ohio

Re: Image downscaling

Unread post by __NM64__ » 2014-08-29, 22:34

Wait a minute, you sure it's just bilinear? When I do a plain bilinear downscale in something like XnView the results looks way better.


EDIT:
Here's the resized image: http://i.minus.com/i1z3KAN95zzOJ.png

I resized it in XnView via bilinear to exactly 1000 pixels tall, which is around the same size as the screenshot previously posted.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-08-30, 10:26

I think Mozilla's implementation of "bilinear" is a very limited and naive one (only taking next-pixel into account).
True bilinear should average all pixels in straight lines even if it covers more than 2 pixels. Mozilla's algorithm doesn't seem to do that at all. Tisk-tisk.

This is based on the fact that scaling is proper down to 50%, and gets bad anywhere beyond that. I'll have a closer look at the scaling algorithm and see if I can improve it. If I can make an acceptable improvement I may even completely forego their "HQ scaling" since it seems to cause more trouble than it actually solves. For example, the "HQ" scaling is only done after-the-fact. If you pay attention with it enabled, you get a grainy image first, then it gets replaced with a smooth image. Not only is that wasting resources (CPU+memory), it's also rather hack-y IMHO.

For the time being I'll limit HQ downscaling to 50% factor and below to mitigate the issues with it, until a better solution is implemented.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-08-31, 17:22

As a follow-up: The scaling used (Actually Google's code, not Mozilla's :!: ) seems to be a simple single-box, non-averaged Hamming-1. Although Hamming is extremely fast, there shouldn't be a noticeable difference if switched to a slower algorithm on even marginally modern hardware.
I'm switching it to a Lanczos algorithm for the next version, which will give a slightly slower but better quality "quick" downscaling in situations where there is a request for "Good" downscaling, used for scaling down to 33% of the original. After that, a switch to HQ downscaling will be made since the implementation remains limited and not filtering more than a small window of pixels resulting in pixels being skipped with particularly large scaling factors (causing the pixelation seen in this thread).
Downscaling will not be pixel-perfect as you may expect from an actual graphics application even with the switch to Lanczos, because it will remain a trade-off between speed and quality and the scaling is kept simple on purpose; Pale Moon is, after all, a web browser, and not a paint or photo-manipulation program.

This is a balanced compromise based on visual tests and comparisons.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Instant

Re: Image downscaling

Unread post by Instant » 2014-09-06, 21:17

Just wondering, isn't this sort of things what GPU excel at. Can Pale Moon use the GPU for scaling?

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2014-09-06, 21:55

Instant wrote:Just wondering, isn't this sort of things what GPU excel at. Can Pale Moon use the GPU for scaling?
It does for a few things that require very fast scaling (if available), but using the GPU has a lot of overhead and in the end for image scaling you can get unpredictable results if you use the GPU. It's often more economical and faster to use the CPU in-memory than to create a texture, send it to the GPU, tell the GPU to scale, get it back from the GPU only to send it right back out to it afterwards to composite the page.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Instant

Re: Image downscaling

Unread post by Instant » 2014-09-07, 11:34

Wow, didn't expect such a fast response. And thanks for the info.

User avatar
__NM64__
Lunatic
Lunatic
Posts: 359
Joined: 2013-10-17, 05:29
Location: Northeast Ohio

Re: Image downscaling

Unread post by __NM64__ » 2015-07-24, 07:22

Apologies for the bump, but I've run into an image that's giving some nearest neighbor-like artifacts when scaled. Before I jump to any conclusions, I'd like to ask for clarification on what Pale Moon does regarding the zooming of images...


From what it sounds like, any zoom level between 33%-100% should be resized with Lanczos (presumably Lanczos3) - is this accurate?

If that's the case, would it not be accurate to say that, if I resize an image via Lanczos3 to, say, 44% in a program like XnView and then zoom the same image in Pale Moon to 44%, the image in Pale Moon and XnView should look identical, correct?

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2015-07-24, 07:47

No, it's not using Lanczos3 because that would be much too slow for on-the-fly resizing. It uses Lanczos2, which uses a smaller box and to keep scaling speedily, at the trade-off of some aliasing artifacts. It's a browser, not an image viewer, so trade-offs must be made for web-embedded scaled images to keep things speedy.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
__NM64__
Lunatic
Lunatic
Posts: 359
Joined: 2013-10-17, 05:29
Location: Northeast Ohio

Re: Image downscaling

Unread post by __NM64__ » 2015-07-24, 07:51

Moonchild wrote:It uses Lanczos2
That's all you had to say, and is why I asked before I jumped to any conclusion - no need to say anything about it not being an image viewer and such.

So that's all then, no problem here - carry on with your day.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35481
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Image downscaling

Unread post by Moonchild » 2015-07-24, 08:06

Nintendo Maniac 64 wrote:That's all you had to say
For you maybe, but not for the casual reader who may not know the difference between the two who stumbles across this thread ;)
Then again, I realize I just repeated myself :P So.. sorry!
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
__NM64__
Lunatic
Lunatic
Posts: 359
Joined: 2013-10-17, 05:29
Location: Northeast Ohio

Re: Image downscaling

Unread post by __NM64__ » 2015-07-31, 05:42

I'm not sure if I should make a new thread, but I figured it was moderately related and I had already recently bumped this thread...

What does Pale Moon do for upscaling? Does it use the same algorithm for all upscaling?

User avatar
__NM64__
Lunatic
Lunatic
Posts: 359
Joined: 2013-10-17, 05:29
Location: Northeast Ohio

Re: Image downscaling

Unread post by __NM64__ » 2015-11-15, 04:44

My last question regarding upscaling never got answered, but I did just find out something interesting...


You know how some large images can give aliasing artifacts when resized to fit the screen due to the use of Lanczos2? Well it turns out that these artifacts don't happen when the image's native resolution is an exact multiple of the downscaled size. Therefore, I did a test and it turns out that if Pale Moon first upscales an image to an exact multiple of the target size and then downscales the image, the resulting quality is much better.

In other words, if the target height is 749px (which is what it is for me), you get a better result taking a 2000px high image, upscaling it to 2247px high, and then downscaling it to 749px high rather than trying to downscale from 2000px directly to 749px.


However, this completely ignores the possibility that doing an upscale followed by a downscale may in fact take more processing time than simply using a higher-quality downscaling algorithm such as Laczos3 or the like.

petrus

Re: Image downscaling

Unread post by petrus » 2017-01-04, 15:18

To not open a new thread, is there a config line which disables all interpolation to get crisp images at all sizes?
I googled around and it should be image.high_quality_downscaling, but with this disabled they are still blurry.

Unlike when you put into userContent.css: img { image-rendering: -moz-crisp-edges; } which does make them pixel perfect.

Locked