2 stable releases
1.0.1 | Mar 5, 2023 |
---|---|
1.0.0 | Mar 4, 2023 |
#1 in #blurhash
26 downloads per month
41KB
621 lines
Fast(er) BlurHash
A faster implementation of the BlurHash algorithm used to generate better looking placeholder for websites and mobile apps. This crates encode and decode functions minimizes the number of allocated vectors to reduce the memory footprint. The base83 encode and decode are also both very fast!
Credits
The original implementation of the BlurHash algorithm can be found here.
Usage
Installation
fast_blurhash is available on crates.io
Use the cargo add command:
cargo add fast_blurhash
or add the crate in your Cargo.toml:
[dependencies]
fast_blurhash = "1"
Generating a blurhash from an image
Generating a blurhash from an image:
use fast_blurhash::compute_dct;
let (width, height) = todo!("Get image width and height");
let image: Vec<u32> = todo!("Load the image");
let blurhash = compute_dct(&image, width, height, 3, 4).into_blurhash();
You can also pass a long-enough iterator to avoid copying data:
use fast_blurhash::compute_dct;
let (width, height) = todo!("Get image width and height");
let image: Vec<u32> = todo!("Load the image");
let blurhash = compute_dct_iter(image.iter(), width, height, 3, 4).into_blurhash();
Supported types to be used with compute_dct:
Type | Alias | Disposition | Notes |
---|---|---|---|
[f32; 3] | Linear | [Red, Green, Blue] | Channels are in linear space |
[u8; 3], &[u8; 3] | Rgb | [Red, Green, Blue] | |
[u8; 4], &[u8; 4] | Rgba | [Red, Green, Blue, Alpha] | Alpha is ignored |
u32 | 0xAARRGGBB where A is alpha | Alpha is ignored |
This crate also supports using your custom types (see the trait AsLinear and examples in the documentation).
Generating a placeholder from a blurhash
Generating an image from a blurhash:
use fast_blurhash::decode;
let blurhash = "LlMF%n00%#MwS|WCWEM{R*bbWBbH";
let image: Vec<u32> = decode(&blurhash, 1.).unwrap().to_rgba(32, 32);
Available generation functions:
Function | Return type | Disposition | Notes |
---|---|---|---|
to_image<\T>(width, height, fn(Linear) -> T) | Vec<\T> | Linear: [Red, Green, Blue] | Linear is a builtin type that represents a color in linear space. |
to_rgb8(width, height) | Vec<[u8; 3]> | [Red, Green, Blue] | |
to_rgba8(width, height) | Vec<[u8; u4]> | [Red, Green, Blue, Alpha] | Alpha will always be 255 |
to_rgba(width, height) | Vec<\u32> | 0xAARRGGBB where A is alpha | Alpha will always be 255 |
Documentation
More documentation is available in rust docs.
TODO
- Add documentation
- Add decode
- Publish to crate.io
Contribution & Feedback
If you have any feedback, please open an issue. If you encounter any bugs or unwanted behaviour, please open an issue.
This projet is open to contributions, feel free to submit your pull requests!
License
fast-blurhash is available under the Apache License 2.0 license. See the LICENSE file for more info.