Skip to content

Commit

Permalink
XYZ with alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jun 15, 2024
1 parent e84a311 commit 95d0c5a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["src/app"] }

[package]
name = "colorutils-rs"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
description = "High performance utilities for color format handling and conversion."
readme = "README.md"
Expand Down
67 changes: 61 additions & 6 deletions src/image_xyza_laba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ pub fn rgba_to_lab_with_alpha(
/// * `height` - Image height
/// * `dst` - A mutable slice to receive LAB(a) data
/// * `dst_stride` - Bytes per row for dst data
/// * `a_plane` - A mutable slice to receive XYZ data
/// * `a_stride` - Bytes per row for dst data
pub fn bgra_to_lab_with_alpha(
src: &[u8],
src_stride: u32,
Expand Down Expand Up @@ -226,8 +224,6 @@ pub fn bgra_to_lab_with_alpha(
/// * `height` - Image height
/// * `dst` - A mutable slice to receive LAB(a) data
/// * `dst_stride` - Bytes per row for dst data
/// * `a_plane` - A mutable slice to receive XYZ data
/// * `a_stride` - Bytes per row for dst data
pub fn rgba_to_luv_with_alpha(
src: &[u8],
src_stride: u32,
Expand Down Expand Up @@ -257,8 +253,6 @@ pub fn rgba_to_luv_with_alpha(
/// * `height` - Image height
/// * `dst` - A mutable slice to receive LAB(a) data
/// * `dst_stride` - Bytes per row for dst data
/// * `a_plane` - A mutable slice to receive XYZ data
/// * `a_stride` - Bytes per row for dst data
pub fn bgra_to_luv_with_alpha(
src: &[u8],
src_stride: u32,
Expand All @@ -278,3 +272,64 @@ pub fn bgra_to_luv_with_alpha(
TransferFunction::Srgb,
);
}


/// This function converts RGBA to CIE XYZ against D65 white point and preserving and normalizing alpha channels keeping it at last positions. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains RGBA data
/// * `src_stride` - Bytes per row for src data.
/// * `width` - Image width
/// * `height` - Image height
/// * `dst` - A mutable slice to receive XYZ(a) data
/// * `dst_stride` - Bytes per row for dst data
pub fn rgba_to_xyz_with_alpha(
src: &[u8],
src_stride: u32,
dst: &mut [f32],
dst_stride: u32,
width: u32,
height: u32,
) {
channels_to_xyz_with_alpha::<{ ImageConfiguration::Rgba as u8 }, { XYZ as u8 }>(
src,
src_stride,
dst,
dst_stride,
width,
height,
&SRGB_TO_XYZ_D65,
TransferFunction::Srgb,
);
}

/// This function converts BGRA to CIE XYZ against D65 white point and preserving and normalizing alpha channels keeping it at last positions. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains BGRA data
/// * `src_stride` - Bytes per row for src data.
/// * `width` - Image width
/// * `height` - Image height
/// * `dst` - A mutable slice to receive XYZ data
/// * `dst_stride` - Bytes per row for dst data
/// * `a_plane` - A mutable slice to receive XYZ data
/// * `a_stride` - Bytes per row for dst data
pub fn bgra_to_xyz_with_alpha(
src: &[u8],
src_stride: u32,
dst: &mut [f32],
dst_stride: u32,
width: u32,
height: u32,
) {
channels_to_xyz_with_alpha::<{ ImageConfiguration::Bgra as u8 }, { XYZ as u8 }>(
src,
src_stride,
dst,
dst_stride,
width,
height,
&SRGB_TO_XYZ_D65,
TransferFunction::Srgb,
);
}
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub use hsv::Hsv;
pub use hsv_to_image::*;
pub use image_to_hsv::*;
pub use image_to_linear::*;
pub use image_to_linear_u8::*;
pub use image_to_xyz_lab::bgr_to_lab;
pub use image_to_xyz_lab::bgr_to_luv;
pub use image_to_xyz_lab::bgra_to_laba;
Expand All @@ -62,10 +63,13 @@ pub use image_to_xyz_lab::srgba_to_xyz;
pub use image_to_xyz_lab::srgba_to_xyza;
pub use image_xyza_laba::bgra_to_lab_with_alpha;
pub use image_xyza_laba::bgra_to_luv_with_alpha;
pub use image_xyza_laba::bgra_to_xyz_with_alpha;
pub use image_xyza_laba::rgba_to_lab_with_alpha;
pub use image_xyza_laba::rgba_to_luv_with_alpha;
pub use image_xyza_laba::rgba_to_xyz_with_alpha;
pub use lab::Lab;
pub use linear_to_image::*;
pub use linear_to_image_u8::*;
pub use luv::LCh;
pub use luv::Luv;
pub use rgb::Rgb;
Expand All @@ -90,9 +94,8 @@ pub use xyza_laba_to_image::lab_with_alpha_to_bgra;
pub use xyza_laba_to_image::lab_with_alpha_to_rgba;
pub use xyza_laba_to_image::luv_with_alpha_to_bgra;
pub use xyza_laba_to_image::luv_with_alpha_to_rgba;

pub use image_to_linear_u8::*;
pub use linear_to_image_u8::*;
pub use xyza_laba_to_image::xyz_with_alpha_to_bgra;
pub use xyza_laba_to_image::xyz_with_alpha_to_rgba;

pub use image_to_sigmoidal::bgra_to_sigmoidal;
pub use image_to_sigmoidal::rgb_to_sigmoidal;
Expand Down
4 changes: 0 additions & 4 deletions src/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ pub struct Xyz {

impl Xyz {
#[inline]
#[allow(dead_code)]
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}

#[inline]
#[allow(dead_code)]
pub fn saturate_x(x: f32) -> f32 {
x.max(0f32).min(95.047f32)
}

#[inline]
#[allow(dead_code)]
pub fn saturate_y(y: f32) -> f32 {
y.max(0f32).min(100f32)
}

#[inline]
#[allow(dead_code)]
pub fn saturate_z(z: f32) -> f32 {
z.max(0f32).min(108.883f32)
}
Expand Down
68 changes: 60 additions & 8 deletions src/xyza_laba_to_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ fn xyz_with_alpha_to_channels<const CHANNELS_CONFIGURATION: u8, const TARGET: u8
/// # Arguments
/// * `src` - A slice contains LAB data
/// * `src_stride` - Bytes per row for src data.
/// * `a_plane` - A slice contains Alpha data
/// * `a_stride` - Bytes per row for alpha plane data
/// * `dst` - A mutable slice to receive RGBA data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
Expand Down Expand Up @@ -214,8 +212,6 @@ pub fn lab_with_alpha_to_rgba(
/// # Arguments
/// * `src` - A slice contains LAB data
/// * `src_stride` - Bytes per row for src data.
/// * `a_plane` - A slice contains Alpha data
/// * `a_stride` - Bytes per row for alpha plane data
/// * `dst` - A mutable slice to receive BGRA data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
Expand Down Expand Up @@ -243,10 +239,8 @@ pub fn lab_with_alpha_to_bgra(
/// This function converts LUV with separate alpha channel to RGBA. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains LAB data
/// * `src` - A slice contains LUV data
/// * `src_stride` - Bytes per row for src data.
/// * `a_plane` - A slice contains Alpha data
/// * `a_stride` - Bytes per row for alpha plane data
/// * `dst` - A mutable slice to receive RGBA data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
Expand Down Expand Up @@ -274,7 +268,7 @@ pub fn luv_with_alpha_to_rgba(
/// This function converts LUV with separate alpha channel to BGRA. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains LAB data
/// * `src` - A slice contains LUV data
/// * `src_stride` - Bytes per row for src data.
/// * `a_plane` - A slice contains Alpha data
/// * `a_stride` - Bytes per row for alpha plane data
Expand All @@ -301,3 +295,61 @@ pub fn luv_with_alpha_to_bgra(
TransferFunction::Srgb,
);
}

/// This function converts XYZ with separate alpha channel to RGBA. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains XYZa data
/// * `src_stride` - Bytes per row for src data.
/// * `dst` - A mutable slice to receive RGBA data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
/// * `height` - Image height
pub fn xyz_with_alpha_to_rgba(
src: &[f32],
src_stride: u32,
dst: &mut [u8],
dst_stride: u32,
width: u32,
height: u32,
) {
xyz_with_alpha_to_channels::<{ ImageConfiguration::Rgba as u8 }, { XYZ as u8 }>(
src,
src_stride,
dst,
dst_stride,
width,
height,
&XYZ_TO_SRGB_D65,
TransferFunction::Srgb,
);
}

/// This function converts XYZ with separate alpha channel to BGRA. This is much more effective than naive direct transformation
///
/// # Arguments
/// * `src` - A slice contains XYZ data
/// * `src_stride` - Bytes per row for src data.
/// * `dst` - A mutable slice to receive BGRA data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
/// * `height` - Image height
pub fn xyz_with_alpha_to_bgra(
src: &[f32],
src_stride: u32,
dst: &mut [u8],
dst_stride: u32,
width: u32,
height: u32,
) {
xyz_with_alpha_to_channels::<{ ImageConfiguration::Bgra as u8 }, { XYZ as u8 }>(
src,
src_stride,
dst,
dst_stride,
width,
height,
&XYZ_TO_SRGB_D65,
TransferFunction::Srgb,
);
}

0 comments on commit 95d0c5a

Please sign in to comment.