Code Monkey home page Code Monkey logo

blurhash-rs's Introduction

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

blurhash-rs's People

Contributors

ardyfeb avatar follpvosten avatar git4unrealnondev avatar raincal avatar rubdos avatar schmiddiii avatar spazziale avatar thvdveld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

blurhash-rs's Issues

Encoding algorithm uses incorrect values

The AC values range from -1.0 to 1.0, and when computing actual_maximum_value, the original implementation takes the absolute value:
https://github.com/woltapp/blurhash/blob/f41a23cc50fba7af5650920d3e7ed4315a21d4c3/Swift/BlurHashEncode.swift#L57

However, this implementation does not use the absolute value:
https://github.com/Raincal/blurhash-rs/blob/ef38509dda21e52a38e5e37636377d8aed753edd/src/lib.rs#L58-L62

This results in a different blurhash than expected.

New release

It seems there have been a few fixes to this project at the beginning of this year, I was wondering if you're planning to make a new release soon.

I was also wondering if this supports WASM?

Incorrect encoding results

When cross-checking with https://blurha.sh/, we seem to encode to the wrong values.

#[test]
fn test_jelly_beans() {
    use image::{EncodableLayout, GenericImageView};

    let img = image::open("data/octocat.png").unwrap();
    let (width, height) = img.dimensions();
    let blurhash = encode(4, 3, width, height, img.to_rgba8().as_bytes()).unwrap();

    // assert_eq!(blurhash, "LFL4=pI8%foujXofRkWC.loyH?V{");
    assert_eq!(blurhash, "LNAdAqj[00aymkj[TKay9}ay-Sj[");
}

The actual result according to blurha.sh is the former assertion, but we encode to the latter. Undoing d3b0132 gives yet another result ("LBAdAqof00WCqZj[PDay0.WB}pof"), and 1b07c4a does not seem to have changed the behaviour.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.