diff --git a/Cargo.toml b/Cargo.toml index 355c3e1..debe007 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/stdin.rs b/examples/stdin.rs new file mode 100644 index 0000000..9aaf30f --- /dev/null +++ b/examples/stdin.rs @@ -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> { + 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(()) +} diff --git a/flake.nix b/flake.nix index fb4940a..14c7328 100644 --- a/flake.nix +++ b/flake.nix @@ -23,6 +23,7 @@ cargo-outdated cargo-show-asm clippy + imagemagick rust-analyzer rustc rustfmt