-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from whisperfish/lookup-table
Add lookup table for srgb_to_linear
- Loading branch information
Showing
8 changed files
with
56 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,7 @@ criterion = "0.5" | |
[[bench]] | ||
name = "decode" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "encode" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters