blurhash_fast/lib.rs
1//! A pure Rust implementation of [woltapp/blurhash][1].
2//!
3//! ### Encoding
4//!
5//! ```
6//! use blurhash::encode;
7//! use image::{GenericImageView, EncodableLayout};
8//!
9//! let img = image::open("octocat.png").unwrap();
10//! let (width, height) = img.dimensions();
11//! let blurhash = encode(4, 3, width, height, img.to_rgba().as_bytes()).unwrap();
12//!
13//! assert_eq!(blurhash, "LBAdAqof00WCqZj[PDay0.WB}pof");
14//! ```
15//!
16//! ### Decoding
17//!
18//! ```no_run
19//! use blurhash::decode;
20//!
21//! let pixels = decode("LBAdAqof00WCqZj[PDay0.WB}pof", 50, 50, 1.0);
22//! ```
23//! [1]: https://github.com/woltapp/blurhash
24
25pub use decode::decode;
26pub use encode::encode;
27pub use error::Error;
28
29mod alternating_current;
30mod base83;
31mod direct_current;
32mod error;
33mod util;
34mod encode;
35mod decode;
36
37#[cfg(test)]
38mod tests {
39 use image::{ColorType::Rgba8, save_buffer};
40 use image::{EncodableLayout, GenericImageView};
41
42 use super::{decode, encode};
43
44 #[test]
45 fn decode_blurhash() {
46 let img = image::open("octocat.png").unwrap();
47 let (width, height) = img.dimensions();
48
49 let blurhash =
50 encode(
51 4,
52 3,
53 width,
54 height,
55 img
56 .to_rgba8()
57 .as_bytes(),
58 ).unwrap();
59
60 let img =
61 decode(
62 &blurhash,
63 width,
64 height,
65 1.0,
66 ).unwrap();
67
68 save_buffer(
69 "out.png",
70 &img,
71 width,
72 height,
73 Rgba8,
74 ).unwrap();
75
76 assert_eq!(img[0..5], [45, 1, 56, 255, 45]);
77 }
78}