Zopfli Documentation

repository·master·Indexed 25 days ago

https://github.com/google/zopfli

A C-based compression library for high-quality deflate, gzip, and zlib compression. It includes ZopfliPNG, a high-compression PNG optimizer that achieves better compression ratios than standard encoders like libpng. The library provides the ZopfliCompress function and specialized functions such as ZopfliDeflate, ZopfliZlibCompress, and ZopfliGzipCompress. Note that Zopfli is strictly for compression and does not support decompression.

Tokens
941
Snippets
2
Records
7
Agent score
36%

What's inside zopfli

  1. Overview of ZopfliPNG

    master

    ZopfliPNG is a command-line program designed to optimize Portable Network Graphics (PNG) images. It achieves better compression by using Zopfli compression for the Deflate stage, comparing various scanline filter strategies, and choosing suitable color types for lossless encoding.

    Key features include:

    • Removal of unimportant ancillary chunks (metadata, text, etc.) to reduce file size.
    • Optional alteration of hidden colors in fully transparent pixels for improved compression.
    • Optional conversion of 16-bit color channels to 8-bit.

    Note: ZopfliPNG uses significantly more CPU time (2-3 orders of magnitude more) than libpng to achieve these optimizations.

  2. Overview of Zopfli Compression Algorithm

    master
    Zopfli is a C-based compression library designed to perform high-quality, albeit slower, deflate or zlib compression. It is strictly a compression library; it does not support decompression. However, any standard zlib or deflate library can be used to decompress the data produced by Zopfli.
  3. Build Zopfli from source

    master

    You can build Zopfli by compiling all .c source files located under src/zopfli into a single binary. You must link against the standard C math library (-lm).

    Manual Compilation (Cross-platform):

    gcc src/zopfli/*.c -O2 -W -Wall -Wextra -Wno-unused-function -ansi -pedantic -lm -o zopfli

    Using Makefile (Linux only):

    • To build the binary: make
    • To build as a shared library: make libzopfli
  4. Build ZopfliPNG from source

    master

    To build ZopfliPNG, you must compile all .c, .cc, and .cpp files from the following directories into a single binary using a C++ compiler (e.g., g++).

    Directories to include:

    • src/zopfli (excluding src/zopfli/zopfli_bin.c)
    • src/zopflipng
    • src/zopflipng/lodepng

    Linux Users: You can use the provided Makefile by running make zopflipng.

    Other Platforms: Use the manual compilation command provided below.

    g++ src/zopfli/{blocksplitter,cache,deflate,gzip_container,hash,katajainen,lz77,squeeze,tree,util,zlib_container,zopfli_lib}.c src/zopflipng/*.cc src/zopflipng/lodepng/*.cpp -O2 -W -Wall -Wextra -Wno-unused-function -ansi -pedantic -o zopflipng
  5. Use specialized compression functions for specific formats

    master

    If you only need to support a single specific format, use these specialized functions instead of the general ZopfliCompress:

    • Deflate: Use ZopfliDeflate in deflate.h to create a valid deflate stream in memory (RFC 1951).
    • Zlib: Use ZopfliZlibCompress in zlib_container.h to create a valid zlib stream in memory (RFC 1950).
    • Gzip: Use ZopfliGzipCompress in gzip_container.h to create a valid gzip stream in memory (RFC 1952).
  6. Compress data using ZopfliCompress

    master

    The primary function for data compression is ZopfliCompress (defined in zopfli.h). This function supports deflate, gzip, and zlib output formats via parameters.

    To ensure correct configuration, you should first initialize the ZopfliOptions object using the ZopfliInitOptions function to populate it with default values. You can then modify the ZopfliOptions to tune the balance between compression speed and ratio.

  7. Preserve specific PNG chunks with --keepchunks

    master

    By default, ZopfliPNG removes ancillary chunks (metadata) that do not affect rendering to save space. If your application requires specific metadata (such as custom gamma correction or DPI information), use the --keepchunks flag to prevent them from being removed.

    Warning: Using this flag will increase the resulting file size. Always visually verify the output in your target renderer to ensure no visual regressions occurred.