{"id":19010779,"url":"https://github.com/darthsim/quantizr","last_synced_at":"2025-04-22T23:23:35.669Z","repository":{"id":46032411,"uuid":"245803130","full_name":"DarthSim/quantizr","owner":"DarthSim","description":"Fast library for converting RGBA images to 8-bit palette images. Written in Rust; can be used in C programs","archived":false,"fork":false,"pushed_at":"2024-01-18T14:16:03.000Z","size":83,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-17T14:38:53.522Z","etag":null,"topics":["image","palette","quantization","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DarthSim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-08T11:37:46.000Z","updated_at":"2025-02-28T11:48:21.000Z","dependencies_parsed_at":"2024-11-15T00:02:01.126Z","dependency_job_id":null,"html_url":"https://github.com/DarthSim/quantizr","commit_stats":{"total_commits":47,"total_committers":1,"mean_commits":47.0,"dds":0.0,"last_synced_commit":"64d6c5ad81e3d2dcc8a27000b8828bd37ab43e74"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarthSim%2Fquantizr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarthSim%2Fquantizr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarthSim%2Fquantizr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DarthSim%2Fquantizr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DarthSim","download_url":"https://codeload.github.com/DarthSim/quantizr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250338700,"owners_count":21414231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image","palette","quantization","rust"],"created_at":"2024-11-08T19:12:29.211Z","updated_at":"2025-04-22T23:23:35.648Z","avatar_url":"https://github.com/DarthSim.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quantizr\n\n[![Crates.io](https://img.shields.io/crates/v/quantizr?style=for-the-badge)](https://crates.io/crates/quantizr) [![docs.rs](https://img.shields.io/docsrs/quantizr?style=for-the-badge)](https://docs.rs/quantizr) [![GH Actions](https://img.shields.io/github/actions/workflow/status/DarthSim/quantizr/ci.yml?branch=master\u0026label=CI\u0026style=for-the-badge)](https://github.com/DarthSim/quantizr/actions)\n\nFast library for converting RGBA images to 8-bit palette images. Written in Rust; can be used in C programs.\n\n## Using with Rust\n\nAdd to Cargo.toml:\n\n```toml\n[dependencies]\nquantizr = \"1.4.0\"\n```\n\nSee [docs.rs](https://docs.rs/quantizr) for the library API documentation. Also, check out [examples](examples) directory for example code.\n\n## Using with C\n\n### Installation\n\nQuantizr written in Rust, so you need to [install it](https://www.rust-lang.org/tools/install) first. Also, you need [cargo-c](https://github.com/lu-zero/cargo-c#installation). Then simply run:\n\n```bash\ncargo cinstall --release\n```\n\n### Usage\n\n```c\n#include \"quantizr.h\"\n\nuint8_t *image_data;\nint32_t image_width;\nint32_t image_height;\n\nQuantizrOptions *opts;\nQuantizrImage *img;\nQuantizrResult *res;\nQuantizrPalette *pal;\nQuantizrError err;\n\nsize_t out_buffer_length = width * height;\nuint8_t *out_buffer = (uint8_t*)malloc(out_buffer_length);\n\n// Load image data and get its dimensions. `load_image` is not a part of Quantizr\nload_image(\u0026data, \u0026width, \u0026height);\n\n// Create new image object.\n// You're responsible for freeing it when the work is done (see below).\n// image_data is unsigned char array with raw RGBA pixels.\nimg = quantizr_create_image_rgba(image_data, image_width, image_height);\n\n// Create new Quantizr options.\n// You're responsible for freeing it when the work is done (see below).\nopts = quantizr_new_options();\n\n// (optional) Set desired number of colors. The default number is 256.\n// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided number is less than 2 or\n// greater than 255.\nerr = quantizr_set_max_colors(opts, 128);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Quantize image.\n// This function returns quantization result, which you're responsible to free when\n// the work is done (see below).\nres = quantizr_quantize(img, opts);\n\n// Set dithering level for the future remapping. The default level is 1.0.\n// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided level is less than 0.0 or\n// greater than 1.0.\nerr = quantizr_set_dithering_level(res, 0.8);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Write quantized image in the provided buffer.\n// The buffer should be prealocated and be large enough to fit entire image (width*height bytes).\n// This function returns QUANTIZR_BUFFER_TOO_SMALL if the buffer is not large enough.\nerr = quantizr_remap(res, img, out_buffer, out_buffer_length);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Fetch palette from the quantization result.\n// Fetched pallette is read-only. You should not modify or free it.\n// pal-\u003ecount is a number of colors in the palette.\n// pal-\u003eentries is an array of colors.\n// pal-\u003eentries[i].r, pal-\u003eentries[i].g, pal-\u003eentries[i].b, and pal-\u003eentries[i].a are color channels\n// of palette colors.\npal = quantizr_get_palette(res);\n\n// Save the resulting image. `save_image` is not a part of Quantizr\nsave_image(pal, out_buffer, out_buffer_length, image_width, image_height);\n\n// Cleanup. Free quantization result, image, and options.\nquantizr_free_result(res);\nquantizr_free_image(img);\nquantizr_free_options(opts);\n```\n\n### Using histogram\n\nSometimes it's necessary to generate a single palette for multiple images like animation frames. In this case, you can use histogram API:\n\n```c\n#include \"quantizr.h\"\n\nuint8_t *image_data;\nint32_t image_width;\nint32_t image_height;\n\nQuantizrOptions *opts;\nQuantizrImage *img;\nQuantizrResult *res;\nQuantizrPalette *pal;\nQuantizrError err;\n\nsize_t out_buffer_length = width * height;\nuint8_t *out_buffer = (uint8_t*)malloc(out_buffer_length);\n\n// Create new histogram.\n// You're responsible for freeing it when the work is done (see below).\nQuantizrHistogram *hist = quantizr_create_histogram();\n\n// Load image data and get its dimensions. `load_image` is not a part of Quantizr\nload_image(\u0026data, \u0026width, \u0026height);\n\n// Create new image object.\n// You're responsible for freeing it when the work is done (see below).\n// image_data is unsigned char array with raw RGBA pixels.\nimg = quantizr_create_image_rgba(image_data, image_width, image_height);\n\n// Add the image to the histogram.\n// You can repeat these two steps multiple times to add multiple images to the histogram.\nquantizr_histogram_add_image(hist, image);\n\n// Create new Quantizr options.\n// You're responsible for freeing it when the work is done (see below).\nopts = quantizr_new_options();\n\n// (optional) Set desired number of colors. The default number is 256.\n// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided number is less than 2 or\n// greater than 255.\nerr = quantizr_set_max_colors(opts, 128);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Quantize histogram.\n// This function returns quantization result, which you're responsible to free when\n// the work is done (see below).\nQuantizrResult *res = quantizr_quantize_histogram(hist, opts);\n\n// Set dithering level for the future remapping. The default level is 1.0.\n// This function returns QUANTIZR_VALUE_OUT_OF_RANGE if provided level is less than 0.0 or\n// greater than 1.0.\nerr = quantizr_set_dithering_level(res, 0.8);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Write quantized image in the provided buffer.\n// The buffer should be prealocated and be large enough to fit entire image (width*height bytes).\n// This function returns QUANTIZR_BUFFER_TOO_SMALL if the buffer is not large enough.\nerr = quantizr_remap(res, img, out_buffer, out_buffer_length);\nif (err != QuantizrOk) {\n  // handle error...\n}\n\n// Fetch palette from the quantization result.\n// Fetched pallette is read-only. You should not modify or free it.\n// pal-\u003ecount is a number of colors in the palette.\n// pal-\u003eentries is an array of colors.\n// pal-\u003eentries[i].r, pal-\u003eentries[i].g, pal-\u003eentries[i].b, and pal-\u003eentries[i].a are color channels\n// of palette colors.\npal = quantizr_get_palette(res);\n\n// Save the resulting image. `save_image` is not a part of Quantizr\nsave_image(pal, out_buffer, out_buffer_length, image_width, image_height);\n\n// Cleanup. Free quantization result, image, and options.\nquantizr_free_result(res);\nquantizr_free_histogram(hist);\nquantizr_free_image(img);\nquantizr_free_options(opts);\n```\n\n## Using with [libvips](https://github.com/libvips/libvips)\n\nlibvips 8.13+ has first-class support of Quantizr.\n\nQuantizr 1.3+ can't be used with earlier versions of libvips. If you want to use Quantizr with an earlier version of libvips, use Quantizr 1.2 and follow [the instructions](https://github.com/DarthSim/quantizr/tree/v1.2.0#using-with-libvips).\n\n## Author\n\nSergey \"[DarthSim](https://github.com/DarthSim)\" Alexandrovich\n\n## License\n\nQuantizr is licensed under the MIT license.\n\nSee LICENSE for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarthsim%2Fquantizr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarthsim%2Fquantizr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarthsim%2Fquantizr/lists"}