Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Dec 19, 2021
1 parent 04b3abe commit 05cb440
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ cargo cinstall --release

// Create new Quantizr options.
// You're responsible for freeing it when the work is done (see below).
quantizr_options *opts = quantizr_new_options();
QuantizrOptions *opts = quantizr_new_options();
// (optional) Set desired number of colors. The default number is 256.
// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided number is less than 2 or
// greater than 255.
quantizr_set_max_colors(128);
// Create new image object.
// You're responsible for freeing it when the work is done (see below).
// image_data is unsigned char array with raw RGBA pixels.
quantizr_image *img = quantizr_create_image_rgba(image_data, image_width, image_height);
QuantizrImage *img = quantizr_create_image_rgba(image_data, image_width, image_height);
// Quantize image.
// This function returns quantization result, which you're responsible to free when
// the work is done (see below).
quantizr_result *res = quantizr_quantize(img, opts);
QuantizrResult *res = quantizr_quantize(img, opts);
// Set dithering level for the future remapping. The default level is 1.0.
// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided level is less than 0.0 or
// greater than 1.0.
Expand All @@ -42,7 +42,7 @@ quantizr_set_dithering_level(res, 0.8);
// pal->entries is an array of colors.
// pal->entries[i].r, pal->entries[i].g, pal->entries[i].b, and pal->entries[i].a are color channels
// of palette colors.
quantizr_palette *pal = quantizr_get_palette(res);
QuantizrPalette *pal = quantizr_get_palette(res);
// Write quantized image in the provided buffer.
// The buffer should be prealocated and be large enough to fit entire image (width*height bytes).
// This function returns QUANTIZR_BUFFER_TOO_SMALL if the buffer is not large enough.
Expand All @@ -53,6 +53,57 @@ quantizr_free_image(img);
quantizr_free_options(opts);
```
### Using histogram
Sometimes it's necessary to generate a single palette for multiple images like animation frames. In this case, you can use histogram API:
```c
#include "quantizr.h"
// Create new Quantizr options.
// You're responsible for freeing it when the work is done (see below).
QuantizrOptions *opts = quantizr_new_options();
// (optional) Set desired number of colors. The default number is 256.
// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided number is less than 2 or
// greater than 255.
quantizr_set_max_colors(128);
// Create new histogram.
// You're responsible for freeing it when the work is done (see below).
QuantizrHistogram *hist = quantizr_create_histogram();
// Create new image object.
// You're responsible for freeing it when the work is done (see below).
// image_data is unsigned char array with raw RGBA pixels.
QuantizrImage *img = quantizr_create_image_rgba(image_data, image_width, image_height);
// Add the image to the histogram.
// You can repeat these two steps multople times to add multiple images to the histogram.
quantizr_histogram_add_image(hist, image);
// Quantize histogram.
// This function returns quantization result, which you're responsible to free when
// the work is done (see below).
QuantizrResult *res = quantizr_quantize_histogram(hist, opts);
// Set dithering level for the future remapping. The default level is 1.0.
// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided level is less than 0.0 or
// greater than 1.0.
quantizr_set_dithering_level(res, 0.8);
// Fetch palette from the quantization result.
// Fetched pallette is read-only. You should not modify or free it.
// pal->count is a number of colors in the palette.
// pal->entries is an array of colors.
// pal->entries[i].r, pal->entries[i].g, pal->entries[i].b, and pal->entries[i].a are color channels
// of palette colors.
QuantizrPalette *pal = quantizr_get_palette(res);
// Write quantized image in the provided buffer.
// The buffer should be prealocated and be large enough to fit entire image (width*height bytes).
// This function returns QUANTIZR_BUFFER_TOO_SMALL if the buffer is not large enough.
// You can repeat this step to remap multiple images.
quantizr_remap(res, img, out_buffer, out_buffer_length);
// Cleanup. Free quantization result, image, and options.
quantizr_free_result(res);
quantizr_free_histogram(hist);
quantizr_free_image(img);
quantizr_free_options(opts);
```

## Using with [libvips](https://github.com/libvips/libvips)

libvips currently doesn't have Quantizr support. That's why Quantizr partly implements libimagequant API enough to be used with libvips.
Expand Down

0 comments on commit 05cb440

Please sign in to comment.