blurhash-update

Crates.ioblurhash-update
lib.rsblurhash-update
version0.1.0
sourcesrc
created_at2024-02-26 03:22:28.482181
updated_at2024-02-26 03:22:28.482181
descriptionA streaming blurhash encoder in rust
homepage
repositoryhttps://git.asonix.dog/asonix/blurhash-update
max_upload_size
id1152982
size1,182,478
asonix (asonix)

documentation

README

blurhash-update

A blurhash encoder for streaming bytes

Supports

  • Encoding
  • Decoding

Motivation

There exists already a blurhash crate, which is a good choice for creating blurhashes, however, it requires that all pixels for a given image exist in memory in order to compute it. For very large images, this might not be ideal.

blurhash-update provides an API for processing bytes from an image as they are made available. This isn't as performant as blurhash in like-for-like comparisons, but the benefit of a lower memory overhead can be useful in some scenarios.

blurhash-update also provides the ability to reduce accuracy by skipping processing of some of the input pixels. This greatly improves performance, but might lead to blurhashes that don't look quite right. Using blurhash-update's auto encoder configuration will target an extremely performant but very loose profile based on the image dimensions.

Usage

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,
}

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 }, 1)?;

    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(())
}

Example usage:

magick convert /path/to/image RGBA:- | \
    cargo r --example --release -- --width blah --height blah

License

blurhash-update is licensed under either of the following:

Commit count: 0

cargo fmt