mozjpeg icon indicating copy to clipboard operation
mozjpeg copied to clipboard

mozjpeg -quality <=51 introduces artifacts

Open hyperknot opened this issue 4 years ago • 5 comments

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

hyperknot avatar Mar 01 '21 12:03 hyperknot

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.

kornelski avatar Mar 01 '21 16:03 kornelski

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.

hyperknot avatar Mar 02 '21 10:03 hyperknot

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.

kornelski avatar Mar 02 '21 12:03 kornelski

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

mattmischuk avatar Mar 09 '21 19:03 mattmischuk

@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.

hyperknot avatar Mar 09 '21 19:03 hyperknot