Skip to content

Commit

Permalink
Some bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed May 29, 2024
1 parent e164777 commit 381147a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 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.2.4"
version = "0.2.5"
edition = "2021"
description = "Hig performance utilities for color format handling and conversion."
readme = "README.md"
Expand Down
2 changes: 0 additions & 2 deletions src/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use colorutils_rs::*;
use image::io::Reader as ImageReader;
use image::{EncodableLayout, GenericImageView};
#[cfg(target_arch = "aarch64")]
use std::arch::aarch64::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
use std::mem::transmute;
Expand Down
12 changes: 6 additions & 6 deletions src/concat_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ pub fn append_alpha(
let mut a_offset = 0usize;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
let mut use_sse = false;
let mut _use_sse = false;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
let mut use_avx = false;
let mut _use_avx = false;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
{
if is_x86_feature_detected!("sse4.1") {
use_sse = true;
_use_sse = true;
}
if is_x86_feature_detected!("avx2") {
use_avx = true;
_use_avx = true;
}
}

Expand All @@ -57,7 +57,7 @@ pub fn append_alpha(

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
unsafe {
if use_avx {
if _use_avx {
while cx + 8 < width as usize {
let xyz_chan_ptr = src_ptr.add(cx * 3usize);
let a_chan_ptr = a_ptr.add(cx);
Expand All @@ -77,7 +77,7 @@ pub fn append_alpha(
cx += 8;
}
}
if use_sse {
if _use_sse {
while cx + 4 < width as usize {
let xyz_chan_ptr = src_ptr.add(cx * 3usize);
let a_chan_ptr = a_ptr.add(cx);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ 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;
pub use xyz_lab_to_image::laba_to_srgb;
pub use xyz_lab_to_image::xyza_to_rgba;
pub use image_to_linear::*;
pub use linear_to_image::*;
pub use concat_alpha::append_alpha;
Expand Down
77 changes: 75 additions & 2 deletions src/xyz_lab_to_image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::slice;

use crate::{Lab, Xyz, XYZ_TO_SRGB_D65};
use crate::gamma_curves::TransferFunction;
use crate::image::ImageConfiguration;
use crate::image_to_xyz_lab::XyzTarget;
Expand All @@ -10,6 +9,7 @@ use crate::image_to_xyz_lab::XyzTarget::{LAB, XYZ};
target_feature = "neon"
))]
use crate::neon_xyz_lab_to_image::neon_xyz_to_channels;
use crate::{Lab, Xyz, XYZ_TO_SRGB_D65};

fn xyz_to_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool, const TARGET: u8>(
src: &[f32],
Expand Down Expand Up @@ -105,7 +105,8 @@ fn xyz_to_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool, cons
unsafe { (a_channel.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 a_value = ((a_slice[x]) * 255f32).max(0f32);
dst_slice[x * channels + image_configuration.get_a_channel_offset()] = a_value as u8;
dst_slice[x * channels + image_configuration.get_a_channel_offset()] =
a_value as u8;
}
}

Expand Down Expand Up @@ -214,3 +215,75 @@ pub fn lab_to_srgb(
TransferFunction::Srgb,
);
}

/// This function converts LAB with separate alpha channel to RGBA. This is much more effective than naive direct transformation
///
/// # 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 RGB data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
/// * `height` - Image height
pub fn laba_to_srgb(
src: &[f32],
src_stride: u32,
a_plane: &[f32],
a_stride: u32,
dst: &mut [u8],
dst_stride: u32,
width: u32,
height: u32,
) {
xyz_to_channels::<{ ImageConfiguration::Rgba as u8 }, true, { LAB as u8 }>(
src,
src_stride,
&a_plane,
a_stride,
dst,
dst_stride,
width,
height,
&XYZ_TO_SRGB_D65,
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 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 RGB data
/// * `dst_stride` - Bytes per row for dst data
/// * `width` - Image width
/// * `height` - Image height
pub fn xyza_to_rgba(
src: &[f32],
src_stride: u32,
a_plane: &[f32],
a_stride: u32,
dst: &mut [u8],
dst_stride: u32,
width: u32,
height: u32,
matrix: &[[f32; 3]; 3],
transfer_function: TransferFunction,
) {
xyz_to_channels::<{ ImageConfiguration::Rgba as u8 }, true, { XYZ as u8 }>(
src,
src_stride,
&a_plane,
a_stride,
dst,
dst_stride,
width,
height,
matrix,
transfer_function,
);
}

0 comments on commit 381147a

Please sign in to comment.