MozJPEG Documentation

repository·master·Indexed 26 days ago

https://github.com/mozilla/mozjpeg

MozJPEG is a high-performance JPEG encoder and a fork of libjpeg-turbo designed to reduce file sizes and web page load times. It acts as a drop-in replacement for libjpeg, maintaining API and ABI compatibility. Key features include trellis quantization, jpegrescan optimization, and new quantization table presets. While providing superior compression efficiency and visual quality, it is significantly slower than libjpeg-turbo and is intended for offline web encoding workflows.

Tokens
7K
Snippets
15
Records
48
Agent score
90%

What's inside MozJPEG

  1. Overview of mozjpeg

    master

    mozjpeg is a fork of libjpeg-turbo designed to reduce JPEG file sizes and web page load times. It achieves better compression through optimized Huffman trees, progressive entropy coding, separate DCT coefficient scans, and Trellis quantization.

    Note on Performance: mozjpeg is highly asymmetric. It is significantly slower at compressing images than libjpeg-turbo or libjpeg, making it unsuitable for real-time compression. It is best used as part of an offline web encoding workflow.

  2. Overview of MozJPEG

    master
    MozJPEG is a high-efficiency JPEG encoder that improves compression efficiency, achieving higher visual quality and smaller file sizes compared to standard encoders. It is a patch for libjpeg-turbo and is designed to be a drop-in replacement for libjpeg by maintaining compatibility with the libjpeg API and ABI. It is a strict superset of libjpeg-turbo functionality, and all MozJPEG-specific improvements can be disabled at runtime to behave exactly like libjpeg-turbo.
  3. Emulate libjpeg v7 or v8 ABI

    master

    If you have applications compiled against libjpeg v7 or v8 that need to run with libjpeg-turbo's accelerated performance, you can build a version that emulates these ABIs using cmake.

    Build Command: Pass -DWITH_JPEG7=1 or -DWITH_JPEG8=1 to cmake.

    Supported Features in Emulation:

    • libjpeg API: IDCT scaling extensions in decompressor (specific factors like 1/4 and 1/2 are SIMD-accelerated).
    • libjpeg API: Arithmetic coding.
    • libjpeg API: In-memory source and destination managers (jpeg_mem_src() and jpeg_mem_dest()).
    • cjpeg: Separate quality settings for luminance and chrominance.
    • cjpeg: 32-bit BMP support and -rgb option.
    • jpegtran: Lossless cropping, -perfect option, and forcing width/height.
    • rdjpgcom: -raw option and locale awareness.

    Unsupported Features:

    • libjpeg API: DCT scaling in compressor (cinfo.scale_num and cinfo.scale_denom are ignored).
    • libjpeg API: SmartScale (cinfo.block_size is ignored).
    • libjpeg API: Fancy downsampling in compressor (cinfo.do_fancy_downsampling is ignored).
    • jpegtran: Scaling.
    • Lossless RGB JPEG files.
  4. Integrate MozJPEG as a library

    master
    MozJPEG is intended to be used as a library in graphics programs and image processing tools. Because it is compatible with the industry-standard libjpeg API and ABI, you can integrate it into any program that uses libjpeg without writing MozJPEG-specific integration code. It is recommended to use the libjpeg C API and link against the MozJPEG library rather than using the provided cjpeg command-line tool for production use.
  5. Transform JPEG files losslessly with jpegtran

    master

    Use jpegtran to perform lossless transformations on DCT-based JPEG files (e.g., changing from baseline to progressive, or rotating). Because it rearranges compressed data without full decoding, there is no image degradation, though metadata may be removed.

    Standard usage:

    jpegtran [switches] [inputfile] > outputfile

    Available transformations:

    • -flip horizontal: Mirror image left-right.
    • -flip vertical: Mirror image top-bottom.
    • -rotate 90: Rotate 90 degrees clockwise.
    • -rotate 180: Rotate 180 degrees.
    • -rotate 270: Rotate 270 degrees clockwise.
    • -transpose: Transpose across UL-to-LR axis.
    • -transverse: Transpose across UR-to-LL axis.
    jpegtran [switches] [inputfile] >outputfile
  6. Decompress a JPEG using djpeg

    master

    Use the djpeg program to decompress a JPEG file back into a conventional image format (default is PBMPLUS).

    Standard usage:

    djpeg [switches] [jpegfile] > imagefile

    If your build supports TWO_FILE_COMMANDLINE:

    djpeg [switches] jpegfile imagefile

    For scripts, use the -outfile flag:

    djpeg [switches] -outfile imagefile jpegfile
    djpeg [switches] [jpegfile]  >imagefile
  7. Build libjpeg-turbo with MinGW on Windows

    master

    Assuming you are using the MSYS environment on Windows:

    cd {build_directory}
    cmake -G"MSYS Makefiles" [additional CMake flags] {source_directory}
    make
    cd {build_directory}
    cmake -G"MSYS Makefiles" [additional CMake flags] {source_directory}
    make
  8. Choose between TurboJPEG and libjpeg APIs

    master

    libjpeg-turbo provides two distinct APIs for JPEG compression and decompression:

    • TurboJPEG API: A straightforward, easy-to-use interface for in-memory compression and decompression. It is recommended for first-time users and includes specialized functionality like generating planar YUV images and performing multiple simultaneous lossless transforms. The Java interface is built on top of this API.
    • libjpeg API: The industry-standard API. It is more powerful but more complex to use. The implementation is API/ABI and mathematically compatible with libjpeg v6b, and can be configured to emulate v7 or v8.

    There is no significant performance difference between the two APIs when performing similar operations.

  9. Handle edge pixels in jpegtran transformations

    master

    When performing transformations like rotation or mirroring, jpegtran may leave untransformable edge pixels if image dimensions are not multiples of the iMCU size (usually 8 or 16 pixels).

    To avoid strange-looking strips at the edges, use the -trim switch to drop non-transformable edge blocks. Note that using -trim makes the transformation non-reversible and technically not lossless.

    If you require strictly lossless and mathematically consistent transformations, use the -perfect switch. This will cause jpegtran to fail with an error if a perfect transformation is not possible.

    jpegtran -rot 90 -perfect foo.jpg || djpeg foo.jpg | pnmflip -r90 | cjpeg
  10. Build libjpeg-turbo with Visual C++ (IDE)

    master
    1. Generate the Visual Studio project files using CMake:
      cd {build_directory}
      cmake -G"Visual Studio 10" [additional CMake flags] {source_directory}
      Note: Add "Win64" to the generator name (e.g., "Visual Studio 10 Win64") for 64-bit builds.
    2. Open ALL_BUILD.vcproj in Visual Studio.
    3. Build a configuration (e.g., "Debug" or "Release") to generate the libraries and DLLs.
    cd {build_directory}
    cmake -G"Visual Studio 10" [additional CMake flags] {source_directory}