Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust quantization tables for high-resolution displays #76

Closed
kornelski opened this issue Jul 21, 2014 · 11 comments
Closed

Adjust quantization tables for high-resolution displays #76

kornelski opened this issue Jul 21, 2014 · 11 comments

Comments

@kornelski
Copy link
Member

There is a trick used when compressing images for high-DPI ("Retina") displays: because details are less visible in higher resolution (or when resampled on lower-resolution displays), the image can tolerate higher level of compression.

http://filamentgroup.com/lab/compressive-images.html
http://gogrowstrategies.com/compressive-images/

Unfortunately in libjpeg the quantization tables are derived from default tables by proportionally scaling all coefficients.

The problem with that is that very low quality settings cause DC to be quantized too heavily, virtually guaranteeing that the image will look blocky and posterized. Giving bits to ACs can't help if the DC is awful.

My suggestion is:

  • Tweak the algorithm in jpeg_add_quant_table to quantize DC and the first few ACs less when the quality is very low.
  • Add a switch to cjpeg for optimization for high-dpi displays. It'd adjust quality scale (lower the quality), quantization tables (protect DC from getting too blocky) and perhaps adjust the lambda for trellis.

The implementation I've done ImageOptim@615652b just increases DC and the first few ACs. It does work, but maybe an even more elegant approach would be to devise quantization tables optimized for low and high qualities and crate final quantization table from interpolation between these tables.

@pdknsk
Copy link

pdknsk commented Jul 25, 2014

I'm not too familiar with how JPEG works, so just a general comment. This works, unless the image has gradients.

@kornelski
Copy link
Member Author

Mozjpeg:
kodim21-mozjpeg

With this change: ImageOptim@615652b

kodim21-pornel

@kornelski
Copy link
Member Author

Resized to simulate downsampling on "@1x" screens:
kodim-y
kodim-x

From dssim tool (lower is better):

  • 0.031151 standard DC
  • 0.023471 improved DC

@dwbuiten
Copy link
Contributor

@pornel On my high DPI display, the 2nd image (with your change) is different, but not better IMO. The sky gradients are less mangled, but the rocks and plants are much worse.

@kornelski
Copy link
Member Author

@dwbuiten in the posterized image rocks and plants look more interesting due to color shifts and increased saturation, but that is actually also an error in the image. The bland version is actually closer to the original: http://optipng.sourceforge.net/pngtech/corpus/kodak/kodim21.png

@danielgtaylor
Copy link

My subjective view is that the image after @pornel's change looks better. 👍

@dwbuiten
Copy link
Contributor

dwbuiten commented Aug 1, 2014

@pornel Looks like firefox was mucking with the image. Viewing in a proper image viewer indeed shows it looks better. Apologies.

@bdaehlie
Copy link
Contributor

bdaehlie commented Aug 1, 2014

What exactly does "firefox was mucking with the image" mean? If there is a bug in Firefox we should file it. The only thing I've noticed about how Firefox renders images that is sub-optimal is that most browsers assume sRGB for JPEGs without a colorspace, Firefox doesn't. This is bad for compatibility, and the sRGB guess is correct more often than not.

@dwbuiten
Copy link
Contributor

dwbuiten commented Aug 1, 2014

@bdaehlie On high DPI displays, firefox upsamples by default (as it should), but it doesn't do it so well with this set (it used to be bilinear, might be bicubic now -- there is a bug open or opened about changing it to lanczos... at least downsampling was fixed recently.)

The best way I can kind of explain it is that the discoloration in the original image near the rocks, after upsampling, looked to be a more similar complexity to the original image to my eye (psychovisual effect -- the eye tends to prefer similar complexity to the source image than blurred, but more similar). It could also just be that my taste is biased.

Sorry for the noise.

@bdaehlie
Copy link
Contributor

bdaehlie commented Aug 1, 2014

No worries, I just want to make sure any major issues with Firefox's rendering get reported and fixed.

@kornelski
Copy link
Member Author

cjpeg -quant-table 2 (#84) is now close enough, so I'm closing this issue.

kornelski pushed a commit to ImageOptim/mozjpeg-cocoa that referenced this issue May 21, 2016
GCC does support UAL syntax (strbeq) if the ".syntax unified" directive
is supplied.  This directive is supported by all versions of GCC and
clang going back to 2003, so it should not create any backward
compatibility issues.

Based on mattsarett/libjpeg-turbo@1264349

Closes mozilla#76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants