mozilla / mozjpeg

Improved JPEG encoder.
Other
5.48k stars 415 forks source link

mozjpeg -quality <=51 introduces artifacts #393

Open hyperknot opened 3 years ago

hyperknot commented 3 years ago

On the following image, any quality value under <= 51 introduces bad artifacts. They are not present with libjpeg-turbo on any quality settings.

mozjpeg-bug

I don't think it's working as it should as the size saving from 51 vs 52 is minimal. On the sample image: 76058 vs. 76859 bytes.

q51: q51

q52: q52

On a color corrected monitor the black lines are very visible, on my laptop it's barely noticable. But here is how they look with a levels adjustment.

q51 q51levels

q52 q52levels

kornelski commented 3 years ago

The horizontal lines are caused by trellis quantization of DC coefficients.

This is sort-of intentional, as it usually helps without introducing visible distortion. This image is just an unfortunate case that makes this compression more visible.

So I can only recommend using higher qualities here, because the algorithm decides the lines are an acceptable flaw for lower-quality images.

A more advanced option, if you need to compress harder, is to set a custom quantization table that compresses DC coefficient less, and AC coefficients more.

hyperknot commented 3 years ago

And can I be sure that it's not used over q52+? I find this optimisation strange to be honest, it provides almost no benefits to file size and produces visual artifacts which might or might not be visible based on the image, all that for 1.1% of savings.

kornelski commented 3 years ago

There are 100 quality levels, if each one saves 1% that's pretty good :)

This algorithm is more effective on gradients and visually busy images. Your image is an edge case where it's not beneficial.

The DC trellis option is used for all quality levels, even 99 and 100. It's just that the distortion it causes is more visible in lower quality levels, because these levels preserve color of flat areas less precisely, so a "smallest" change of background color is still big in absolute terms.

I think it should be safe to assume that quality >52 won't cause such distortion unexpectedly. The distortion depends on DC quantization level, and that depends on quality setting and quantization table chosen.

mattmischuk commented 3 years ago

@hyperknot using mssim-tuned kodak seems to fix your artifacts for similar savings. ~10kb more in this case.

hyperknot commented 3 years ago

@hyperknot using mssim-tuned kodak seems to fix your artifacts for similar savings. ~10kb more in this case.

But the funny thing is that using q52 barely adds to the file size.