Convert Figma logo to code with AI

mozilla logomozjpeg

Improved JPEG encoder.

5,428
417
5,428
98

Top Related Projects

Main libjpeg-turbo repository

🧙‍♂️ ImageMagick 7

12,898

Perceptual JPEG encoder

Lossy PNG compressor — pngquant command based on libimagequant library

29,063

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

9,518

A fast image processing library with low memory needs.

Quick Overview

MozJPEG is an improved JPEG encoder developed by Mozilla. It aims to produce smaller JPEG files while maintaining compatibility with existing decoders. MozJPEG focuses on optimizing image compression for web use, resulting in faster page loads and reduced bandwidth consumption.

Pros

  • Produces smaller file sizes compared to standard JPEG encoders
  • Maintains compatibility with existing JPEG decoders
  • Offers improved image quality at similar file sizes
  • Suitable for both web and desktop applications

Cons

  • Slower encoding speed compared to standard JPEG encoders
  • Limited support for advanced features like progressive scanning
  • May not be ideal for real-time encoding scenarios
  • Requires recompression of existing JPEG files to benefit from improvements

Code Examples

#include <turbojpeg.h>

// Initialize the TurboJPEG compressor
tjhandle compressor = tjInitCompress();

// Compress the image
unsigned char *jpegBuf = NULL;
unsigned long jpegSize = 0;
tjCompress2(compressor, srcBuf, width, pitch, height, TJPF_RGB,
            &jpegBuf, &jpegSize, TJSAMP_444, quality, flags);

// Clean up
tjDestroy(compressor);

This example demonstrates how to use the TurboJPEG API to compress an image using MozJPEG.

#include <jconfig.h>
#include <jerror.h>
#include <jmorecfg.h>
#include <jpeglib.h>

// Set up the JPEG compression parameters
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);

// Specify the destination for the compressed data
jpeg_stdio_dest(&cinfo, outfile);

// Set image parameters and start compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);

// Compress the image data
while (cinfo.next_scanline < cinfo.image_height) {
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
}

// Finish compression and clean up
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);

This example shows how to use the libjpeg API directly for more fine-grained control over the compression process.

Getting Started

To use MozJPEG in your project:

  1. Clone the repository:

    git clone https://github.com/mozilla/mozjpeg.git
    
  2. Build the project:

    cd mozjpeg
    cmake -G"Unix Makefiles" .
    make
    
  3. Install the library:

    sudo make install
    
  4. Include the necessary headers in your C/C++ project and link against the MozJPEG library.

  5. Use the TurboJPEG API or the standard libjpeg API to compress images in your application.

Competitor Comparisons

Main libjpeg-turbo repository

Pros of libjpeg-turbo

  • Faster performance, especially for encoding and decoding JPEG images
  • Better compatibility with existing JPEG applications and libraries
  • More actively maintained with frequent updates and bug fixes

Cons of libjpeg-turbo

  • Generally produces larger file sizes compared to mozjpeg
  • Lacks some of the advanced compression techniques found in mozjpeg
  • May not achieve the same level of image quality at very low bitrates

Code Comparison

Both libraries provide similar APIs, but there are some differences in usage:

libjpeg-turbo:

#include <turbojpeg.h>

tjhandle tjInstance = tjInitCompress();
tjCompress2(tjInstance, sourceBuffer, width, pitch, height, pixelFormat,
            &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags);

mozjpeg:

#include <mozjpeg/jpeglib.h>

struct jpeg_compress_struct cinfo;
jpeg_create_compress(&cinfo);
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);

While both libraries offer similar functionality, libjpeg-turbo focuses on speed and compatibility, whereas mozjpeg prioritizes compression efficiency and image quality at lower file sizes. The choice between them depends on specific project requirements, such as performance needs, file size constraints, and desired image quality.

🧙‍♂️ ImageMagick 7

Pros of ImageMagick

  • Supports a wide range of image formats and operations
  • Offers a comprehensive set of command-line tools and APIs
  • Provides extensive image manipulation capabilities beyond compression

Cons of ImageMagick

  • Larger codebase and more complex to use for simple tasks
  • May not achieve the same level of JPEG compression as MozJPEG
  • Higher resource usage due to its broad feature set

Code Comparison

MozJPEG (simplified usage):

cjpeg -quality 85 input.png > output.jpg

ImageMagick (equivalent operation):

convert input.png -quality 85 output.jpg

Key Differences

  • MozJPEG focuses specifically on JPEG compression, while ImageMagick is a multi-purpose image processing suite
  • MozJPEG typically achieves better JPEG compression results, especially at lower file sizes
  • ImageMagick offers more versatility in terms of supported formats and image manipulation options

Use Cases

MozJPEG is ideal for:

  • Optimizing JPEG images for web use
  • Achieving high-quality compression with smaller file sizes

ImageMagick is better suited for:

  • Batch processing of various image formats
  • Complex image manipulations and transformations
  • Projects requiring a wide range of image processing capabilities

Both tools have their strengths, and the choice between them depends on the specific requirements of your project and the level of compression or versatility needed.

12,898

Perceptual JPEG encoder

Pros of Guetzli

  • Achieves higher compression ratios, often producing smaller file sizes than MozJPEG
  • Designed to maintain perceptual quality, potentially resulting in better-looking images at lower file sizes
  • Utilizes more advanced psychovisual modeling techniques

Cons of Guetzli

  • Significantly slower encoding process compared to MozJPEG
  • Higher memory usage during compression
  • Limited to producing JPEG output only, while MozJPEG supports multiple formats

Code Comparison

MozJPEG:

void jpeg_simple_progression(j_compress_ptr cinfo)
{
  int nscans;
  jpeg_simple_progression_sof(cinfo);
  nscans = jpeg_simple_progression_Ah_Al_Ss_Se(cinfo);
  jpeg_simple_progression_scan_sequence(cinfo, nscans);
}

Guetzli:

bool Processor::ProcessJpegData(const Params& params,
                                const JPEGData& jpg_in,
                                Comparator* comparator,
                                JPEGData* jpg_out,
                                ProcessStats* stats) {
  // Implementation details
}

Both projects aim to improve JPEG compression, but their approaches and trade-offs differ. Guetzli focuses on achieving higher compression ratios at the cost of slower encoding, while MozJPEG offers a balance between compression and speed. The code snippets showcase different implementation languages and approaches to JPEG processing.

Lossy PNG compressor — pngquant command based on libimagequant library

Pros of pngquant

  • Specializes in PNG optimization, offering excellent compression for PNG files
  • Supports alpha channel transparency, crucial for web graphics
  • Provides a command-line interface and library for easy integration

Cons of pngquant

  • Limited to PNG format, while mozjpeg handles JPEG compression
  • May not achieve the same level of compression as mozjpeg for photographic images
  • Requires additional tools for full web image optimization workflow

Code Comparison

mozjpeg (JPEG compression):

int main(int argc, char **argv)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
}

pngquant (PNG optimization):

int main(int argc, char *argv[])
{
    pngquant_config config;
    pngquant_init(&config, argc, argv);
    pngquant_image *input_image = pngquant_read_image(&config, stdin, 0);
    pngquant_image *output_image = pngquant_compress(&config, input_image);
}

Both projects offer powerful image compression capabilities, with mozjpeg focusing on JPEG optimization and pngquant specializing in PNG compression. While mozjpeg may provide better results for photographic images, pngquant excels in preserving transparency and optimizing PNG files for web use. The choice between the two depends on the specific image format and use case requirements.

29,063

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

Pros of Sharp

  • Supports multiple image formats (JPEG, PNG, WebP, AVIF, TIFF, GIF)
  • Provides a high-level API for image processing tasks
  • Faster processing speed for many operations

Cons of Sharp

  • Larger package size and more dependencies
  • May require more setup and configuration
  • Less specialized in JPEG optimization compared to MozJPEG

Code Comparison

Sharp:

import sharp from 'sharp';

sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg');

MozJPEG:

const mozjpeg = require('mozjpeg');
const fs = require('fs');

fs.createReadStream('input.jpg')
  .pipe(mozjpeg(['-quality', '85']))
  .pipe(fs.createWriteStream('output.jpg'));

Summary

Sharp is a versatile image processing library that supports multiple formats and provides a high-level API for various tasks. It offers faster processing for many operations but comes with a larger package size and more dependencies. MozJPEG, on the other hand, is specialized in JPEG optimization and may be more suitable for projects focused solely on JPEG compression. The choice between the two depends on the specific requirements of your project, such as the need for multi-format support or specialized JPEG optimization.

9,518

A fast image processing library with low memory needs.

Pros of libvips

  • Supports a wide range of image formats beyond JPEG
  • Offers advanced image processing capabilities (resizing, filtering, color management)
  • Designed for high-performance processing of large images

Cons of libvips

  • More complex to use and integrate compared to mozjpeg
  • Larger library size and potentially higher resource usage
  • Less specialized for JPEG optimization

Code Comparison

mozjpeg (focusing on JPEG compression):

cinfo.optimize_coding = TRUE;
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_simple_progression(&cinfo);

libvips (general image processing, including JPEG handling):

VipsImage *out;
vips_thumbnail(input, &out, width,
               "height", height,
               "size", VIPS_SIZE_BOTH,
               NULL);

mozjpeg is specifically designed for JPEG compression and optimization, while libvips is a more comprehensive image processing library. mozjpeg excels in producing highly optimized JPEG files, whereas libvips offers a broader range of image manipulation features across multiple formats. The choice between the two depends on the specific requirements of the project, with mozjpeg being ideal for JPEG-focused tasks and libvips better suited for diverse image processing needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Mozilla JPEG Encoder Project Build Status

MozJPEG improves JPEG compression efficiency achieving higher visual quality and smaller file sizes at the same time. It is compatible with the JPEG standard, and the vast majority of the world's deployed JPEG decoders.

MozJPEG is a patch for libjpeg-turbo. Please send pull requests to libjpeg-turbo if the changes aren't specific to newly-added MozJPEG-only compression code. This project aims to keep differences with libjpeg-turbo minimal, so whenever possible, improvements and bug fixes should go there first.

MozJPEG is compatible with the libjpeg API and ABI. It is intended to be a drop-in replacement for libjpeg. MozJPEG is a strict superset of libjpeg-turbo's functionality. All MozJPEG's improvements can be disabled at run time, and in that case it behaves exactly like libjpeg-turbo.

MozJPEG is meant to be used as a library in graphics programs and image processing tools. We include a demo cjpeg command-line tool, but it's not intended for serious use. We encourage authors of graphics programs to use libjpeg's C API and link with MozJPEG library instead.

Features

  • Progressive encoding with "jpegrescan" optimization. It can be applied to any JPEG file (with jpegtran) to losslessly reduce file size.
  • Trellis quantization. When converting other formats to JPEG it maximizes quality/filesize ratio.
  • Comes with new quantization table presets, e.g. tuned for high-resolution displays.
  • Fully compatible with all web browsers.
  • Can be seamlessly integrated into any program that uses the industry-standard libjpeg API. There's no need to write any MozJPEG-specific integration code.

Releases

Compiling

See BUILDING. MozJPEG is built exactly the same way as libjpeg-turbo, so if you need additional help please consult libjpeg-turbo documentation.