20

I found two slightly conflicting blog posts on this matter, here I summarize:

Adding to confusion, this Reddit thread suggests that mozjpeg uses the same algorithm used in jpegcrush, and that jpegcrush is a wrapper for jpegtran... so we've come full circle? I can only assume that those Reddit comments are wrong, can anyone confirm that?

2
  • As you can read from JPGCrush description and from that article, JPGCrush is a Perl wrapper around jpegtran that exercises several JPEG compression options (listed in jpeg_scan_rgb.txt – each line is a jpeg_scan_info structure) and chooses the smallest output. And mozjpeg follows the same algorithm.
    – dma_k
    Commented Nov 21, 2016 at 9:50
  • 1
    Just found the answer to your question here: jpegcrush/jpegrescan trick: tweaks details of progressive JPEG for maximum compression (each scan gets its own Huffman table, and JPEG can arbitrarily divide data into scans). Also found scans file description.
    – dma_k
    Commented Nov 21, 2016 at 22:56

1 Answer 1

14

MozJPEG library uses one algorithm inspired by jpegcrush (optimized progressive scans), but technically it's a completely new implementation.

MozJPEG library is a drop-in replacement for the popular libjpeg, so almost every JPEG-related tool can be compiled in "regular" and "MozJPEG" flavors.

There exists a "regular" jpegtran and a MozJPEG version of jpegtran. That's the same program, but the MozJPEG version has different default settings and performs extra work to compress better.

Similarly, jpegoptim is usually built with stock libjpeg, but it's also possible to build it with MozJPEG's version of libjpeg (e.g. ImageOptim does it).


There are two ways to use MozJPEG:

  1. lossless (take an existing JPEG file and make it a bit smaller). That's what MozJPEG's jpegtran does.
  2. lossy (create a new JPEG file from uncompressed pixels, with higher quality/filesize ratio). That's what MozJPEG's cjpeg does, and other tools like jpegoptim can be made to do with MozJPEG.

Both modes of operation are slower than vanilla non-optimizing libjpeg. Lossless optimization does less work, but also achieves smaller gain.

More precise speed analysis is here: https://libjpeg-turbo.org/About/Mozjpeg

Note that "slow" here is relative. In absolute terms it compresses several megapixels per second, so it may be fast enough for most applications.

Not the answer you're looking for? Browse other questions tagged or ask your own question.