Libdither is a library for black-and-white and color image dithering, written in C (ANSI C99 standard). Libdither has no external dependencies and should compile easily on most current systems (e.g. Windows, Linux, MacOS). All you need is a C compiler.
This version of libdither is a Release Candidate: it has been tested and is working, but minor code cleanup is still required.
The black-and-white only libdither version can now be found in the libdither_mono branch.
- Color ditherers: error diffusion, ordered dithering
- Mono ditherers: grid dithering, ordered dithering, error diffusion, variable error diffusion (Ostromoukhov, Zhou Fang), pattern dithering, Direct Binary Search (DBS), dot diffusion, Kacker and Allebach dithering, Riemersma dithering, thresholding
- Support for color comparison modes: LAB76, LAB94, LAB2000, sRGB, linear, HSV, luminance, Tetrapal
- Support for color reduction: Median-Cut, Wu, KD-Tree
- Support for images with transparent background
- Libdither works in linear color space
- Dither matrices and inputs can be easily extended without having to change the dither code itself
- no external dependencies on other libraries
- works with C and C++ projects
- supports Windows, Linux, MacOS (universal binary support Intel / Apple silicon), and possible others
Libdither MIT licensed, but the following code parts have their own permissive license:
- kdtree license: requires inclusion of license terms in source and binary distributions (more)
- uthash license: requires inclusion of license terms in source distributions (more)
Note: Palettes with a good range of hues and brightness, which matches the source image work best. If the overall palette is too dark, or too bright, the image, too, will be darker or brighte.
| Median-Cut: 16 colors error diffusion |
Wu: 16 colors error diffusion |
KD-Tree: 16 colors error diffusion |
Note: Median-Cut and Wu are fairly fast, compared to KD-Tree. However, KD-Tree often gives the best results.
| Original | Median-Cut: 2 colors error diffusion |
Median-Cut: 4 colors error diffusion |
| Median-Cut: 8 colors error diffusion |
Median-Cut: 16 colors error diffusion |
Median-Cut: 32 colors error diffusion |
Note: With a palette that matches the original palette well, 32 colors are often sufficient.
Notes:
- Different color distance measuring methods are supported for finding the closest color in the target palette.
- sRGB distance is a good, universally applicable choice.
- LAB2000 distance is the most accurate method. It works best with well balanced palettes, where it can pick up even subtle nuances, but it is slow.
- Luminance distance works best for gradients.
- CCIR distance methods take human perceptions into account and add a slight saturation boost.
- Tetrapal: works best for ordered dithering and pre-composed palettes (i.e. non quantified), such as EGA, C64, MS Paint palettes.
- Other distance methods may work better than others in certain cases - make sure to try them.
You need an ANSI C compiler and the make utility. Run make to display all build options.
By default, libdither is built for the current architecture.
Once compiled, you can find the finished library in the dist directory.
MacOS notes:
- Installing the XCode command line tools is all you need for building libdither
- You can choose if you want to build a x64, arm64 or universal library. The demo, however, only builds against the current machine's architecture.
Linux notes:
gccandmakeis all you need to build libdither. E.g. on Ubuntu you should installbuild-essentialviaaptto get these tools.
Windows notes - MinGW:
- Install
makevia Chocolatey package manager from chocolatey.org (https://chocolatey.org/, https://chocolatey.org/packages/make) - Open the Makefile and ensure the path (on top of the file) points to your MingW installation directory.
- Run
make libditherto build using MinGW.
Windows notes - Microsoft Visual Studio Solution:
- Open the solution file (```.sln``) in Visual Studio 2022 or newer.
- There are 3 projects you can build: libdither (dynamic ```.dll`` library) and the demo.
Windows notes - Microsoft Visual Studio (make):
- To build using make, install
makevia Chocolatey package manager from chocolatey.org (https://chocolatey.org/, https://chocolatey.org/packages/make) - Run
make libdither_msvcto build (make sure to run Visual Studio'svcvars64.batfirst!) - OR use the solution file (
.sln) to build libdither from the Visual Studio IDE (2022 or newer).
The src/demo example shows how to load an image (we use .bmp as it's an easy format to work with),
convert it to linear color space, dither it, and write it back to an output .bmp file. The demo was used
to create all the dithering examples you can see below.
You can also look at libdither.h, which includes commentary on how to use libdither.
In your own code, you only need to #include "libdither.h", which includes all public functions
and data structures, and link the libdither library, either statically or dynamically.