Skip to content

Commit

Permalink
Merge pull request #7 from whisperfish/lookup-table
Browse files Browse the repository at this point in the history
Add lookup table for srgb_to_linear
  • Loading branch information
rubdos authored Aug 23, 2023
2 parents 2f3cacb + f441ebc commit 8d29f6a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ criterion = "0.5"
[[bench]]
name = "decode"
harness = false

[[bench]]
name = "encode"
harness = false
18 changes: 18 additions & 0 deletions benches/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use image::GenericImageView;

pub fn lenna(c: &mut Criterion) {
for case in ["data/SIPI_Jelly_Beans.tiff", "data/octocat.png"] {
let img = image::open(case).unwrap();

let (width, height) = img.dimensions();
let img = img.to_rgba8();

c.bench_function(&format!("encode {}", case), |b| {
b.iter(|| blurhash::encode(4, 3, width, height, black_box(&img)).unwrap());
});
}
}

criterion_group!(benches, lenna);
criterion_main!(benches);
28 changes: 28 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::io::Write;

/// srgb 0-255 integer to linear 0.0-1.0 floating point conversion.
pub fn srgb_to_linear(value: u32) -> f32 {
let v = value as f32 / 255.;
if v <= 0.04045 {
v / 12.92
} else {
f32::powf((v + 0.055) / 1.055, 2.4)
}
}

fn generate_srgb_lookup() -> [f32; 256] {
let mut table = [0f32; 256];
for (i, val) in table.iter_mut().enumerate() {
*val = srgb_to_linear(i as u32);
}
table
}

fn main() {
let table = generate_srgb_lookup();

let out_dir = std::env::var("OUT_DIR").unwrap();
let out_dir = std::path::PathBuf::from(out_dir);
let mut f = std::fs::File::create(out_dir.join("srgb_lookup.rs")).unwrap();
writeln!(f, "static SRGB_LOOKUP: [f32; 256] = {:?};", table).unwrap();
}
Binary file added data/SIPI_Jelly_Beans.tiff
Binary file not shown.
File renamed without changes
File renamed without changes
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! use blurhash::encode;
//! use image::{GenericImageView, EncodableLayout};
//!
//! let img = image::open("octocat.png").unwrap();
//! let img = image::open("data/octocat.png").unwrap();
//! let (width, height) = img.dimensions();
//! let blurhash = encode(4, 3, width, height, img.to_rgba().as_bytes()).unwrap();
//!
Expand Down Expand Up @@ -231,12 +231,12 @@ mod tests {

#[test]
fn decode_blurhash() {
let img = image::open("octocat.png").unwrap();
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();
let img = decode(&blurhash, width, height, 1.0).unwrap();
save_buffer("out.png", &img, width, height, Rgba8).unwrap();
save_buffer("data/out.png", &img, width, height, Rgba8).unwrap();

assert_eq!(img[0..5], [45, 1, 56, 255, 45]);
}
Expand Down
9 changes: 3 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include!(concat!(env!("OUT_DIR"), "/srgb_lookup.rs"));

/// linear 0.0-1.0 floating point to srgb 0-255 integer conversion.
pub fn linear_to_srgb(value: f32) -> u32 {
let v = f32::max(0., f32::min(1., value));
Expand All @@ -10,12 +12,7 @@ pub fn linear_to_srgb(value: f32) -> u32 {

/// srgb 0-255 integer to linear 0.0-1.0 floating point conversion.
pub fn srgb_to_linear(value: u32) -> f32 {
let v = value as f32 / 255.;
if v <= 0.04045 {
v / 12.92
} else {
f32::powf((v + 0.055) / 1.055, 2.4)
}
SRGB_LOOKUP[value as usize]
}

fn sign(n: f32) -> f32 {
Expand Down

0 comments on commit 8d29f6a

Please sign in to comment.