Skip to content

Commit

Permalink
Compute lookup table at runtime using once_cell
Browse files Browse the repository at this point in the history
So we don't bloat the binary size, which would be a problem especially
for WASM.
  • Loading branch information
YgorSouza committed Sep 8, 2024
1 parent 2a3c1e1 commit cdefc55
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ dependencies = [
"color-hex",
"document-features",
"emath",
"once_cell",
"serde",
]

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ home = "0.5.9"
image = { version = "0.25", default-features = false }
log = { version = "0.4", features = ["std"] }
nohash-hasher = "0.2"
once_cell = "1.19.0"
parking_lot = "0.12"
puffin = "0.19"
puffin_http = "0.16"
Expand Down
3 changes: 2 additions & 1 deletion crates/ecolor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
repository = "https://github.com/emilk/egui"
categories = ["mathematics", "encoding"]
keywords = ["gui", "color", "conversion", "gamedev", "images"]
include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "data/*", "Cargo.toml"]
include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"]

[lints]
workspace = true
Expand All @@ -32,6 +32,7 @@ default = []

[dependencies]
emath.workspace = true
once_cell.workspace = true

#! ### Optional dependencies

Expand Down
Binary file not shown.
34 changes: 11 additions & 23 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use once_cell::sync::Lazy;

use crate::{fast_round, linear_f32_from_linear_u8, Rgba};

static FROM_UNMULTIPLIED_LUT: &[u8; 256 * 256] =
include_bytes!("../data/color32_from_unmultiplied_lookup_table");
static FROM_UNMULTIPLIED_LUT: Lazy<[u8; 256 * 256]> = Lazy::new(|| {
use crate::{gamma_u8_from_linear_f32, linear_f32_from_gamma_u8};
core::array::from_fn(|i| {
let [value, alpha] = (i as u16).to_be_bytes();
let value_lin = linear_f32_from_gamma_u8(value);
let alpha_lin = linear_f32_from_linear_u8(alpha);
gamma_u8_from_linear_f32(value_lin * alpha_lin)
})
});

/// This format is used for space-efficient color representation (32 bits).
///
Expand Down Expand Up @@ -247,24 +256,3 @@ impl Color32 {
)
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_unmultiplied_lookup_table_check() {
use crate::{
gamma_u8_from_linear_f32, linear_f32_from_gamma_u8, linear_f32_from_linear_u8,
};
for value in 0..=255u8 {
for alpha in 0..=255u8 {
let value_lin = linear_f32_from_gamma_u8(value);
let alpha_lin = linear_f32_from_linear_u8(alpha);

let calculated = gamma_u8_from_linear_f32(value_lin * alpha_lin);
let lut = FROM_UNMULTIPLIED_LUT[usize::from(u16::from_be_bytes([value, alpha]))];
assert_eq!(calculated, lut);
}
}
}
}

0 comments on commit cdefc55

Please sign in to comment.