6 releases

0.2.3 Jul 23, 2024
0.2.2 Jul 9, 2024
0.2.1 Feb 26, 2024
0.2.0 Sep 19, 2023
0.1.1 Jul 12, 2019

#115 in Algorithms

Download history 870/week @ 2024-08-09 1140/week @ 2024-08-16 1261/week @ 2024-08-23 647/week @ 2024-08-30 1624/week @ 2024-09-06 906/week @ 2024-09-13 1150/week @ 2024-09-20 1566/week @ 2024-09-27 1046/week @ 2024-10-04 1067/week @ 2024-10-11 816/week @ 2024-10-18 1293/week @ 2024-10-25 1107/week @ 2024-11-01 2286/week @ 2024-11-08 1430/week @ 2024-11-15 974/week @ 2024-11-22

5,880 downloads per month
Used in 6 crates (5 directly)

Apache-2.0/MIT

215KB
644 lines

blurhash-rs

CI Build Crates.io Crates.io

A pure Rust implementation of Blurhash.

Blurhash is an algorithm written by Dag Ågren for Wolt (woltapp/blurhash) that encodes an image into a short (~20-30 byte) ASCII string. When you decode the string back into an image, you get a gradient of colors that represent the original image. This can be useful for scenarios where you want an image placeholder before loading, or even to censor the contents of an image a la Mastodon.

🚴 Usage

Add blurhash to your Cargo.toml:

[dependencies]
blurhash = "0.2.3"

By default, the fast-linear-to-srgb is enabled. This improves decoding performance by about 60%, but has a memory overhead of 8KB. If this overhead is problematic, you can disable it by instead specifying the following to your Cargo.toml:

[dependencies]
blurhash = { version = "0.2.3", default-features = false }

Encoding

use blurhash::encode;
use image::GenericImageView;

fn main() {
  // Add image to your Cargo.toml
  let img = image::open("octocat.png").unwrap();
  let (width, height) = img.dimensions();
  let blurhash = encode(4, 3, width, height, &img.to_rgba().into_vec());
}

Decoding

use blurhash::decode;

let pixels = decode("LBAdAqof00WCqZj[PDay0.WB}pof", 50, 50, 1.0);

Licence

Licensed under either of

Dependencies

~0–8MB
~74K SLoC