From e164777f75c2cc8b23184a3a43d2c788fb1cf28b Mon Sep 17 00:00:00 2001 From: awxkee Date: Wed, 29 May 2024 08:43:15 +0100 Subject: [PATCH] Linearize bugfixes --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app/src/main.rs | 14 ++++++++++++++ src/concat_alpha.rs | 2 +- src/image_to_xyz_lab.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a5db915..df124fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -163,7 +163,7 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "colorutils-rs" -version = "0.2.3" +version = "0.2.4" dependencies = [ "half", ] diff --git a/Cargo.toml b/Cargo.toml index fa94949..ec7c0ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ workspace = { members = ["src/app"] } [package] name = "colorutils-rs" -version = "0.2.3" +version = "0.2.4" edition = "2021" description = "Hig performance utilities for color format handling and conversion." readme = "README.md" diff --git a/src/app/src/main.rs b/src/app/src/main.rs index 7eacd53..3089598 100644 --- a/src/app/src/main.rs +++ b/src/app/src/main.rs @@ -64,6 +64,20 @@ fn main() { ); src_bytes = &dst_rgba; + // { + // let mut store: Vec = vec![]; + // let store_stride = width as usize * 3usize * std::mem::size_of::(); + // store.resize(width as usize * 3usize * height as usize, 0f32); + // let mut alpha_store: Vec = vec![]; + // let alpha_stride = width as usize * std::mem::size_of::(); + // alpha_store.resize(width as usize * height as usize, 0f32); + // rgba_to_laba(src_bytes, 4u32 * width, &mut store, store_stride as u32, &mut alpha_store, alpha_stride as u32, width, height); + // let mut destination: Vec = vec![]; + // destination.resize(width as usize * height as usize * 4, 0f32); + // let dst_stride = width * 4 * std::mem::size_of::() as u32; + // append_alpha(&mut destination, dst_stride, &store, store_stride as u32, &alpha_store, alpha_stride as u32, width, height); + // } + let mut xyz: Vec = vec![]; xyz.resize(4 * width as usize * height as usize, 0f32); diff --git a/src/concat_alpha.rs b/src/concat_alpha.rs index 046ec1d..ab30eee 100644 --- a/src/concat_alpha.rs +++ b/src/concat_alpha.rs @@ -53,7 +53,7 @@ pub fn append_alpha( let a_ptr = unsafe { (a_plane.as_ptr() as *const u8).add(a_offset) as *const f32 }; let a_slice = unsafe { slice::from_raw_parts(a_ptr, width as usize) }; let dst_ptr = unsafe { (dst.as_mut_ptr() as *mut u8).add(dst_offset) as *mut f32 }; - let dst_slice = unsafe { slice::from_raw_parts_mut(dst_ptr, width as usize) }; + let dst_slice = unsafe { slice::from_raw_parts_mut(dst_ptr, width as usize * 4) }; #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] unsafe { diff --git a/src/image_to_xyz_lab.rs b/src/image_to_xyz_lab.rs index b55fb00..98a33d2 100644 --- a/src/image_to_xyz_lab.rs +++ b/src/image_to_xyz_lab.rs @@ -463,6 +463,41 @@ pub fn srgba_to_xyza( ); } +/// This function converts RGBA to CIE L*ab against D65 white point without alpha. 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 LAB 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_lab( + src: &[u8], + src_stride: u32, + dst: &mut [f32], + dst_stride: u32, + a_plane: &mut [f32], + a_stride: u32, + width: u32, + height: u32, +) { + channels_to_xyz::<{ ImageConfiguration::Rgba as u8 }, false, { LAB as u8 }>( + src, + src_stride, + dst, + dst_stride, + a_plane, + a_stride, + width, + height, + &SRGB_TO_XYZ_D65, + TransferFunction::Srgb, + ); +} + /// This function converts RGBA to CIE L*ab against D65 white point and preserving and normalizing alpha channels. This is much more effective than naive direct transformation /// /// # Arguments diff --git a/src/lib.rs b/src/lib.rs index 06ab2ca..315c268 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,7 @@ pub use image_to_xyz_lab::rgba_to_laba; pub use image_to_xyz_lab::bgra_to_laba; pub use image_to_xyz_lab::bgr_to_lab; pub use image_to_xyz_lab::srgb_to_xyz; +pub use image_to_xyz_lab::rgba_to_lab; pub use xyz_lab_to_image::xyz_to_rgb; pub use xyz_lab_to_image::lab_to_srgb; pub use xyz_lab_to_image::xyz_to_srgb;