Mozilla has done a study of image formats and concluded that WebP and JPEG XR are not a big-enough improvement over well-optimized JPEG. In the study only HEVC (H.265) was significantly better, but it’s a patent-encumbered format, so it can’t be used freely (shhhh!)
It seems that Mozilla has a short-term and a long-term plan for image compression. They’re sponsoring development of the Daala codec, which is technically very interesting, but not production-ready yet.
For the short term Mozilla has developed MozJPEG — a modernized JPEG encoder that offers better compression while remaining fully standard-compliant, so it’s compatible with all browsers, operating systems and native apps, and you can use it today without waiting for the whole world to upgrade (BTW: if you need the same for images with alpha channel, try lossy PNG).
In addition to all standard libjpeg optimizations MozJPEG performs progressive scan optimization—a trick from the
jpegrescan tool. Parameters of progressive JPEG passes influence compression, so it’s possible to make smaller files by tuning them just right. It’s like having ImageOptim built-in.
MozJPEG also brings to JPEG a technique from modern video codecs called trellis quantization. Simple JPEG encoders discard a fixed amount of detail according to quality set, but MozJPEG looks how many bits it would cost to write each detail and discards details that compress the least. This makes files much smaller, but also tends to make images look softer (which, depending on an image, may be a good or a bad thing).
BTW: In this case JPEG’s file size is about the same as 32-color PNG’s! Image by xkcd.
JPEG files have quantization tables that allow fine-tuning of quality for various levels of detail. It’s like an audio equalizer with 64 knobs—e.g. you can choose whether you want sharper edges or less blocky gradients).
Modification of quantization tables used to be a hidden option reserved for the most advanced users. MozJPEG made it more accessible by adding a few good presets. It’s especially useful for high-resolution images. Standard JPEG tables have been tuned for low-DPI displays, and for compressive images tend to put too much emphasis on tiny details over accurate color reproduction.
To be fair, technically you can get a decent result from ImageMagick, but it takes a few obscure options and an XML file.
MozJPEG is binary-compatible with both libjpeg-turbo and the classic libjpeg. This makes it possible to use MozJPEG as a drop-in replacement for libjpeg, and even install it as a system-wide replacement.
The latest release has been made with help of libjpeg-turbo maintainer. MozJPEG and libjpeg-turbo adopted a common future-proof API that allows programs to be compatible with both libraries and still take advantage of each library’s specific features.
Ask makers of your favorite graphics programs to integrate MozJPEG! No, really. MozJPEG is a library for developers, and on its own is not useful to people who don’t like compiling C programs.
I’ve quickly hacked a web interface for you.
./configure && makein mozjpeg source directory (you’ll need a compiler installed.)
./cjpegin this directory (the
./prefix is important, otherwise you may invoke an old version bundled with your system.)
sudo make installif you want
cjpeg(and other mozjpeg libraries/tools) installed system-wide.
./cjpeg -quality 70 -outfile compressed-image.jpg source-image.png
-qualityoption accepts fractional numbers (necessary if you want to make a fair benchmark) and two numbers separated by commas to set quality of brightness and color separately, e.g.
-sample 1x1enables full-resolution color (e.g. red lines won’t be smudged as badly), but it makes files larger.
-quant-table 2(mentioned earlier) makes images softer and reduces posterization in low qualities.
-notrellismakes images sharper, but increases file size.
-outfiledefines path where result is (over)written to.
You can combine
cjpeg with ImageMagick like this:
convert -resize 50% source-image.png TGA:- | ./cjpeg -outfile compressed-image.jpg -targa
TGA:- | bit tells ImageMagick to quickly and cheaply pipe uncompressed data to
cjpeg, so you don’t waste CPU on generating a temporary PNG file.
Xiph.org’s researcher Tim Terriberry calls JPEG an “alien technology from the future”. JPEG, designed over 20 years ago, has got so many details right that it still remains competitive today, despite being much simpler and faster to decode than newer formats trying to dethrone it. And MozJPEG isn’t done yet!