* Create a crate for constant serialization of config structs for manganis * use SerializeConst for the image asset builder * switch to a declarative macro for assets * add serializable options for each asset type * implement the new manganis macro * optimize assets in the CLI * Fix assets with dioxus formattable * reduce fuzzing test limits * use the new syntax in examples * fix avif support * Fix manganis doc tests * cache asset optimization * Split out asset and bundled asset * make hash pointer width independent * Fix manganis macro docs * add constvec::is_empty method to fix clippy lint * remove nasm feature * simplify test_rsplit_once test so typos passes * revert example change from clippy fix * always accept unix paths * fix windows path seperator * fix folder asset hash * Optimize assets in a blocking task * Fix asset options docs * Document Asset and BundledAsset * simplify the linker macro a bit * add more docs to AssetParser expansion * add image format helpers * Split by both unix and windows path separators and take the smallest one
53 lines
1.7 KiB
Rust
53 lines
1.7 KiB
Rust
use std::{io::BufWriter, path::Path};
|
|
|
|
use image::DynamicImage;
|
|
|
|
pub(crate) fn compress_png(image: DynamicImage, output_location: &Path) {
|
|
// Image loading/saving is outside scope of this library
|
|
let width = image.width() as usize;
|
|
let height = image.height() as usize;
|
|
let bitmap: Vec<_> = image
|
|
.into_rgba8()
|
|
.pixels()
|
|
.map(|px| imagequant::RGBA::new(px[0], px[1], px[2], px[3]))
|
|
.collect();
|
|
|
|
// Configure the library
|
|
let mut liq = imagequant::new();
|
|
liq.set_speed(5).unwrap();
|
|
liq.set_quality(0, 99).unwrap();
|
|
|
|
// Describe the bitmap
|
|
let mut img = liq.new_image(&bitmap[..], width, height, 0.0).unwrap();
|
|
|
|
// The magic happens in quantize()
|
|
let mut res = match liq.quantize(&mut img) {
|
|
Ok(res) => res,
|
|
Err(err) => panic!("Quantization failed, because: {err:?}"),
|
|
};
|
|
|
|
let (palette, pixels) = res.remapped(&mut img).unwrap();
|
|
|
|
let file = std::fs::File::create(output_location).unwrap();
|
|
let w = &mut BufWriter::new(file);
|
|
|
|
let mut encoder = png::Encoder::new(w, width as u32, height as u32);
|
|
encoder.set_color(png::ColorType::Rgba);
|
|
let mut flattened_palette = Vec::new();
|
|
let mut alpha_palette = Vec::new();
|
|
for px in palette {
|
|
flattened_palette.push(px.r);
|
|
flattened_palette.push(px.g);
|
|
flattened_palette.push(px.b);
|
|
alpha_palette.push(px.a);
|
|
}
|
|
encoder.set_palette(flattened_palette);
|
|
encoder.set_trns(alpha_palette);
|
|
encoder.set_depth(png::BitDepth::Eight);
|
|
encoder.set_color(png::ColorType::Indexed);
|
|
encoder.set_compression(png::Compression::Best);
|
|
let mut writer = encoder.write_header().unwrap();
|
|
writer.write_image_data(&pixels).unwrap();
|
|
writer.finish().unwrap();
|
|
}
|