Add example that reads from STDIN in chunks of 1024
Some checks failed
/ check (aarch64-unknown-linux-musl) (push) Waiting to run
/ check (armv7-unknown-linux-musleabihf) (push) Waiting to run
/ check (x86_64-unknown-linux-musl) (push) Waiting to run
/ clippy (push) Successful in 7s
/ tests (push) Has been cancelled

This commit is contained in:
asonix 2024-02-19 15:31:18 -06:00
parent f47c274df6
commit 835626d909
3 changed files with 45 additions and 0 deletions

View file

@ -15,6 +15,7 @@ opt-level = "z"
[dev-dependencies]
blurhash = "0.2.0"
clap = { version = "4.5.1", features = ["derive"] }
criterion = "0.5.1"
image = "0.24.8"

43
examples/stdin.rs Normal file
View file

@ -0,0 +1,43 @@
use std::io::Read;
use blurhash_update::{Components, Encoder, ImageBounds};
use clap::Parser;
#[derive(clap::Parser)]
struct Args {
/// Width of the provided image
#[clap(long)]
width: u32,
/// Height of the provided image
#[clap(long)]
height: u32,
}
// Example usage:
// ```bash
// magick convert /path/to/image RGBA:- | \
// cargo r --example --release -- --width blah --height blah
// ```
fn main() -> Result<(), Box<dyn std::error::Error>> {
let Args { width, height } = Args::parse();
let mut encoder = Encoder::new(Components { x: 4, y: 3 }, ImageBounds { width, height })?;
let mut stdin = std::io::stdin().lock();
let mut buf = [0u8; 1024];
loop {
let n = stdin.read(&mut buf)?;
if n == 0 {
break;
}
encoder.update(&buf[..n]);
}
println!("{}", encoder.finalize());
Ok(())
}

View file

@ -23,6 +23,7 @@
cargo-outdated
cargo-show-asm
clippy
imagemagick
rust-analyzer
rustc
rustfmt