Add example that reads from STDIN in chunks of 1024
Some checks failed
Some checks failed
This commit is contained in:
parent
f47c274df6
commit
835626d909
|
@ -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
43
examples/stdin.rs
Normal 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(())
|
||||
}
|
Loading…
Reference in a new issue