Skip to content

Commit

Permalink
Reorganize generated code and hand-write linear conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jan 7, 2025
1 parent b0aec2f commit 2279c6e
Show file tree
Hide file tree
Showing 8 changed files with 69,619 additions and 69,646 deletions.
312 changes: 119 additions & 193 deletions codegen/src/lut.rs

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions palette/src/encoding/adobe.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! The Adobe RGB (1998) standard.
use crate::{
encoding::{
lut::{self, adobe::*},
FromLinear, IntoLinear,
},
luma::LumaStandard,
num::{Powf, Real},
rgb::{Primaries, RgbSpace, RgbStandard},
white_point::{Any, D65},
Mat3, Yxy,
};

use super::{FromLinear, IntoLinear};

/// The Adobe RGB (1998) (a.k.a. opRGB) color space and standard.
///
/// This color space was designed to encompass most colors achievable by CMYK
Expand Down Expand Up @@ -115,6 +117,34 @@ where
}
}

impl IntoLinear<f32, u8> for AdobeRgb {
#[inline]
fn into_linear(encoded: u8) -> f32 {
ADOBE_RGB_U8_TO_F32[encoded as usize]
}
}

impl FromLinear<f32, u8> for AdobeRgb {
#[inline]
fn from_linear(linear: f32) -> u8 {
lut::linear_f32_to_encoded_u8(linear, ADOBE_RGB_MIN_FLOAT, &TO_ADOBE_RGB_U8)
}
}

impl IntoLinear<f64, u8> for AdobeRgb {
#[inline]
fn into_linear(encoded: u8) -> f64 {
ADOBE_RGB_U8_TO_F64[encoded as usize]
}
}

impl FromLinear<f64, u8> for AdobeRgb {
#[inline]
fn from_linear(linear: f64) -> u8 {
<AdobeRgb>::from_linear(linear as f32)
}
}

#[cfg(test)]
mod test {
#[cfg(feature = "approx")]
Expand Down
6 changes: 4 additions & 2 deletions palette/src/encoding/lut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod codegen;

pub use crate::encoding::lut::codegen::*;

const MAX_FLOAT_BITS: u32 = 0x3f7fffff; // 1.0 - f32::EPSILON

// SAFETY: Only use this macro if `input` is clamped between `min_float` and `max_float`.
Expand Down Expand Up @@ -33,7 +35,7 @@ macro_rules! unsafe_linear_float_to_encoded_uint {
}

#[inline]
fn linear_f32_to_encoded_u8(linear: f32, min_float_bits: u32, table: &[u32]) -> u8 {
pub fn linear_f32_to_encoded_u8(linear: f32, min_float_bits: u32, table: &[u32]) -> u8 {
let min_float = f32::from_bits(min_float_bits);
let max_float = f32::from_bits(MAX_FLOAT_BITS);

Expand All @@ -49,7 +51,7 @@ fn linear_f32_to_encoded_u8(linear: f32, min_float_bits: u32, table: &[u32]) ->

#[cfg(feature = "gamma_lut_u16")]
#[inline]
fn linear_f32_to_encoded_u16_with_linear_scale(
pub fn linear_f32_to_encoded_u16_with_linear_scale(
linear: f32,
linear_scale: f32,
min_float_bits: u32,
Expand Down
Loading

0 comments on commit 2279c6e

Please sign in to comment.