# libimagequant—Image Quantization Library
Small, portable C library for high-quality conversion of RGBA images to 8-bit indexed-color (palette) images.
It's powering [pngquant2](https://pngquant.org).
## License
Libimagequant is dual-licensed:
* For Free/Libre Open Source Software it's available under [GPL v3 or later](https://raw.github.com/ImageOptim/libimagequant/master/COPYRIGHT) with additional copyright notices for older parts of the code.
* For use in non-GPL software (e.g. closed-source or App Store distribution) please ask kornel@pngquant.org for a commercial license.
## Download
The [library](https://pngquant.org/lib) is currently a part of the [pngquant2 project](https://pngquant.org). [Repository](https://github.com/ImageOptim/libimagequant).
## Compiling and Linking
The library can be linked with ANSI C, C++, [Rust](https://github.com/pornel/libimagequant-rust) and [Java](https://github.com/ImageOptim/libimagequant/tree/master/org/pngquant) programs. It has no external dependencies.
To build on Unix-like systems run:
make static
it will create `libimagequant.a` which you can link with your program.
gcc yourprogram.c /path/to/libimagequant.a
On BSD, use `gmake` (GNU make) rather than the native `make`.
Alternatively you can compile the library with your program simply by including all `.c` files (and define `NDEBUG` to get a fast version):
gcc -std=c99 -O3 -DNDEBUG libimagequant/*.c yourprogram.c
### Rust
In [Rust](https://www.rust-lang.org/) you can use Cargo to build the library. Add [`imagequant`](https://crates.io/crates/imagequant) to dependencies of Rust programs, or `cargo build` [`imagequant-sys`](https://crates.io/crates/imagequant-sys) to build `libimagequant.a` for any language.
### Java JNI
To build Java JNI interface, ensure `JAVA_HOME` is set to your JDK directory, and run:
# export JAVA_HOME=$(locate include/jni.h) # you may need to set JAVA_HOME first
make java
It will create `libimagequant.jnilib` and classes in `org/pngquant/`.
On Windows run `make java-dll` and it'll create `libimagequant.dll` instead.
### Compiling on Windows/Visual Studio
The library can be compiled with any C compiler that has at least basic support for C99 (GCC, clang, ICC, C++ Builder, even Tiny C Compiler), but Visual Studio 2012 and older are not up to date with the 1999 C standard. There are 2 options for using `libimagequant` on Windows:
* Use Visual Studio **2015** and an [MSVC-compatible branch of the library](https://github.com/ImageOptim/libimagequant/tree/msvc)
* Or use GCC from [MinGW](http://www.mingw.org) or [MSYS2](http://www.msys2.org/). Use GCC to build `libimagequant.a` (using the instructions above for Unix) and add it along with `libgcc.a` (shipped with the MinGW compiler) to your VC project.
## Overview
The basic flow is:
1. Create attributes object and configure the library.
2. Create image object from RGBA pixels or data source.
3. Perform quantization (generate palette).
4. Store remapped image and final palette.
5. Free memory.
Please note that libimagequant only handles raw uncompressed arrays of pixels in memory and is completely independent of any file format.
<p>
#include "libimagequant.h"
liq_attr *attr = liq_attr_create();
liq_image *image = liq_image_create_rgba(attr, example_bitmap_rgba, width, height, 0);
liq_result *res;
liq_image_quantize(image, attr, &res);
liq_write_remapped_image(res, image, example_bitmap_8bpp, example_bitmap_size);
const liq_palette *pal = liq_get_palette(res);
// Save the image and the palette now.
for(int i=0; i < pal->count; i++) {
example_copy_palette_entry(pal->entries[i]);
}
// You'll need a PNG library to write to a file.
example_write_image(example_bitmap_8bpp);
liq_result_destroy(res);
liq_image_destroy(image);
liq_attr_destroy(attr);
Functions returning `liq_error` return `LIQ_OK` (`0`) on success and non-zero on error.
It's safe to pass `NULL` to any function accepting `liq_attr`, `liq_image`, `liq_result` (in that case the error code `LIQ_INVALID_POINTER` will be returned). These objects can be reused multiple times.
There are 3 ways to create image object for quantization:
* `liq_image_create_rgba()` for simple, contiguous RGBA pixel arrays (width×height×4 bytes large bitmap).
* `liq_image_create_rgba_rows()` for non-contiguous RGBA pixel arrays (that have padding between rows or reverse order, e.g. BMP).
* `liq_image_create_custom()` for RGB, ABGR, YUV and all other formats that can be converted on-the-fly to RGBA (you have to supply the conversion function).
Note that "image" here means raw uncompressed pixels. If you have a compressed image file, such as PNG, you must use another library (e.g. libpng or lodepng) to decode it first.
## Functions
----
liq_attr* liq_attr_create(void);
Returns object that will hold initial settings (attributes) for the library. The object should be freed using `liq_attr_destroy()` after it's no longer needed.
Returns `NULL` in the unlikely case that the library cannot run on the current machine (e.g. the library has been compiled for SSE-capable x86 CPU and run on VIA C3 CPU).
----
liq_error liq_set_max_colors(liq_attr* attr, int colors);
Specifies maximum number of colors to use. The default is 256. Instead of setting a fixed limit it's better to use `liq_set_quality()`.
The first argument is attributes object from `liq_attr_create()`.
Returns `LIQ_VALUE_OUT_OF_RANGE` if number of colors is outside the range 2-256.
----
int liq_get_max_colors(liq_attr* attr);
Returns the value set by `liq_set_max_colors()`.
----
liq_error liq_set_quality(liq_attr* attr, int minimum, int maximum);
Quality is in range `0` (worst) to `100` (best) and values are analoguous to JPEG quality (i.e. `80` is usually good enough).
Quantization will attempt to use the lowest number of colors needed to achieve `maximum` quality. `maximum` value of `100` is the default and means conversion as good as possible.
If it's not possible to convert the image with at least `minimum` quality (i.e. 256 colors is not enough to meet the minimum quality), then `liq_image_quantize()` will fail. The default minumum is `0` (proceeds regardless of quality).
Quality measures how well the generated palette fits image given to `liq_image_quantize()`. If a different image is remapped with `liq_write_remapped_image()` then actual quality may be different.
Regardless of the quality settings the number of colors won't exceed the maximum (see `liq_set_max_colors()`).
The first argument is attributes object from `liq_attr_create()`.
Returns `LIQ_VALUE_OUT_OF_RANGE` if target is lower than minimum or any of them is outside the 0-100 range.
Returns `LIQ_INVALID_POINTER` if `attr` appears to be invalid.
liq_attr *attr = liq_attr_create();
liq_set_quality(attr, 50, 80); // use quality 80 if possible. Give up if quality drops below 50.
----
int liq_get_min_quality(liq_attr* attr);
Returns the lower bound set by `liq_set_quality()`.
----
int liq_get_max_quality(liq_attr* attr);
Returns the upper bound set by `liq_set_quality()`.
----
liq_image *liq_image_create_rgba(liq_attr *attr, void* pixels, int width, int height, double gamma);
Creates an object that represents the image pixels to be used for quantization and remapping. The pixel array must be contiguous run of RGBA pixels (alpha is the last component, 0 = transparent, 255 = opaque).
The first argument is attributes object from `liq_attr_create()`. The same `attr` object should be used for the entire process, from creation of images to quantization.
The `pixels` array must not be modified or freed until this object is freed with `liq_image_destroy()`. See also `liq_image_set_memory_ownership()`.
`width` and `height` are dimensions in pixels. An image 10x10 pixel large will need a 400-byte array.
`gamma` can be `0` for images with the typical 1