From f75a235c907724b633248a11c63908594ffe8619 Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:42:47 +0200 Subject: [PATCH] Use boxed slice for lookup table to avoid stack overflow (#5212) * Closes * [x] I have followed the instructions in the PR template --- crates/ecolor/src/color32.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index 80c876d192d..827314c0787 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -108,15 +108,17 @@ impl Color32 { // common-case optimization 255 => Self::from_rgb(r, g, b), a => { - static LOOKUP_TABLE: OnceLock<[u8; 256 * 256]> = OnceLock::new(); + static LOOKUP_TABLE: OnceLock> = OnceLock::new(); let lut = LOOKUP_TABLE.get_or_init(|| { use crate::{gamma_u8_from_linear_f32, linear_f32_from_gamma_u8}; - core::array::from_fn(|i| { - let [value, alpha] = (i as u16).to_ne_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) - }) + (0..=u16::MAX) + .map(|i| { + let [value, alpha] = i.to_ne_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) + }) + .collect() }); let [r, g, b] =