Skip to content

Commit

Permalink
Added l-alpha-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jul 30, 2024
1 parent 9d5820f commit df60780
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 46 deletions.
77 changes: 34 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ fn main() {
let g = 127;
let b = 255;
let rgb = Rgb::<u8>::new(r, g, b);
let jzazbz = Jzazbz::from_rgb(rgb, TransferFunction::Srgb);
println!("Jzczhz {:?}", jzazbz);
let lalphabeta = LAlphaBeta::from_rgb(rgb, TransferFunction::Srgb);
println!("LAlphaBeta {:?}", lalphabeta);
println!("Rgb {:?}", rgb);
let restored = jzazbz.to_rgb(TransferFunction::Srgb);
let restored = lalphabeta.to_rgb(TransferFunction::Srgb);
println!("Restored RGB {:?}", restored);

let img = ImageReader::open("./assets/beach_horizon.jpg")
Expand Down
71 changes: 71 additions & 0 deletions src/lalphabeta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* // Copyright 2024 (c) the Radzivon Bartoshyk. All rights reserved.
* //
* // Use of this source code is governed by a BSD-style
* // license that can be found in the LICENSE file.
*/
use crate::{Rgb, TransferFunction, Xyz, SRGB_TO_XYZ_D65, XYZ_TO_SRGB_D65};
use erydanos::{Exponential, Logarithmic};

/// Represents l-alpha-beta colorspace
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub struct LAlphaBeta {
pub l: f32,
pub alpha: f32,
pub beta: f32,
}

impl LAlphaBeta {
#[inline]
/// Creates new instance
pub fn new(l: f32, alpha: f32, beta: f32) -> LAlphaBeta {
LAlphaBeta { l, alpha, beta }
}

/// Converts RGB to l-alpha-beta
#[inline]
pub fn from_rgb(rgb: Rgb<u8>, transfer_function: TransferFunction) -> LAlphaBeta {
let xyz = rgb.to_xyz(&SRGB_TO_XYZ_D65, transfer_function);
LAlphaBeta::from_xyz(xyz)
}

/// Converts XYZ to l-alpha-beta
#[inline]
pub fn from_xyz(xyz: Xyz) -> LAlphaBeta {
let l_lms = 0.3897 * xyz.x + 0.6890 * xyz.y - 0.0787 * xyz.z;
let m = -0.2298 * xyz.x + 1.1834 * xyz.y + 0.0464 * xyz.z;
let s = xyz.z;
let lp = if l_lms > 0. { l_lms.eln() } else { 0. };
let mp = if m > 0. { m.eln() } else { 0. };
let sp = if s > 0. { s.eln() } else { 0. };
let l = 0.5774 * lp + 0.5774 * mp + 0.5774 * sp;
let alpha = 0.4082 * lp + 0.4082 * mp - 0.8165 * sp;
let beta = 1.4142 * lp - 1.4142 * mp;
LAlphaBeta::new(l, alpha, beta)
}

/// Converts l-alpha-beta to XYZ
#[inline]
pub fn to_xyz(&self) -> Xyz {
let l_a = self.l * 0.577324;
let s_a = 0.408263 * self.alpha;
let p_b = 0.353557 * self.beta;
let lp = l_a + s_a + p_b;
let mp = l_a + s_a - p_b;
let sp = self.l * 0.577253 - 0.816526 * self.alpha;
let l = lp.eexp();
let m = mp.eexp();
let s = sp.eexp();
let x = 1.91024 * l - 1.11218 * m + 0.201941 * s;
let y = 0.370942 * l + 0.62905 * m + 5.13315e-6 * s;
let z = s;
Xyz::new(x, y, z)
}

/// Converts l-alpha-beta to RGB
#[inline]
pub fn to_rgb(&self, transfer_function: TransferFunction) -> Rgb<u8> {
let xyz = self.to_xyz();
xyz.to_rgb(&XYZ_TO_SRGB_D65, transfer_function)
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod xyz_lab_to_image;
mod xyz_target;
mod xyz_transform;
mod xyza_laba_to_image;
mod lalphabeta;

pub use concat_alpha::append_alpha;
pub use gamma_curves::*;
Expand Down Expand Up @@ -172,3 +173,4 @@ pub use sigmoidal_to_image::sigmoidal_to_bgra;
pub use sigmoidal_to_image::sigmoidal_to_rgb;
pub use sigmoidal_to_image::sigmoidal_to_rgba;
pub use taxicab::TaxicabDistance;
pub use lalphabeta::LAlphaBeta;

0 comments on commit df60780

Please sign in to comment.