Skip to content

Commit

Permalink
Improve linear and u8
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jun 4, 2024
1 parent b3c2089 commit 6a53466
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 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.8"
version = "0.2.9"
edition = "2021"
description = "Hig performance utilities for color format handling and conversion."
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions src/image_to_linear_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
let rgb_f32 = rgb.to_rgb_f32();

unsafe {
let t_r = transfer(rgb_f32.r).min(255f32).max(0f32) as u8;
let t_g = transfer(rgb_f32.g).min(255f32).max(0f32) as u8;
let t_b = transfer(rgb_f32.b).min(255f32).max(0f32) as u8;
let t_r = (transfer(rgb_f32.r) * 255f32).min(255f32).max(0f32) as u8;
let t_g = (transfer(rgb_f32.g) * 255f32).min(255f32).max(0f32) as u8;
let t_b = (transfer(rgb_f32.b) * 255f32).min(255f32).max(0f32) as u8;
*dst_slice.get_unchecked_mut(px) = t_r;
*dst_slice.get_unchecked_mut(px + 1) = t_g;
*dst_slice.get_unchecked_mut(px + 2) = t_b;
Expand Down
10 changes: 5 additions & 5 deletions src/linear_to_image_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use crate::image::ImageConfiguration;
any(target_arch = "aarch64", target_arch = "arm"),
target_feature = "neon"
))]
use crate::neon_to_linear::get_neon_linear_transfer;
use crate::neon_linear_to_image::get_neon_gamma_transfer;
#[cfg(all(
any(target_arch = "aarch64", target_arch = "arm"),
target_feature = "neon"
))]
use crate::neon_to_linear_u8::neon_image_linear_to_u8;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::sse_image_to_linear_u8::sse_image_to_linear_unsigned;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::sse_to_linear::get_sse_linear_transfer;
use crate::Rgb;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::sse_linear_to_image::get_sse_gamma_transfer;

#[inline]
fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
Expand Down Expand Up @@ -56,7 +56,7 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
unsafe {
if _has_sse {
let transfer = get_sse_linear_transfer(transfer_function);
let transfer = get_sse_gamma_transfer(transfer_function);
cx = sse_image_to_linear_unsigned::sse_channels_to_linear::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
Expand All @@ -77,7 +77,7 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b
target_feature = "neon"
))]
unsafe {
let transfer = get_neon_linear_transfer(transfer_function);
let transfer = get_neon_gamma_transfer(transfer_function);
cx = neon_image_linear_to_u8::neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
Expand Down
12 changes: 6 additions & 6 deletions src/neon_to_linear_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ pub mod neon_image_linear_to_u8 {
let r_f = vmulq_n_f32(vcvtq_f32_u32(r), 1f32 / 255f32);
let g_f = vmulq_n_f32(vcvtq_f32_u32(g), 1f32 / 255f32);
let b_f = vmulq_n_f32(vcvtq_f32_u32(b), 1f32 / 255f32);
let r_linear = transfer(r_f);
let g_linear = transfer(g_f);
let b_linear = transfer(b_f);
let r_linear = vmulq_n_f32(transfer(r_f), 255f32);
let g_linear = vmulq_n_f32(transfer(g_f), 255f32);
let b_linear = vmulq_n_f32(transfer(b_f), 255f32);

(
vcvtaq_u32_f32(r_linear),
vcvtaq_u32_f32(g_linear),
vcvtaq_u32_f32(b_linear),
)
}

#[inline(always)]
#[inline]
pub(crate) unsafe fn neon_channels_to_linear_u8<
const CHANNELS_CONFIGURATION: u8,
const USE_ALPHA: bool,
Expand Down Expand Up @@ -142,5 +143,4 @@ pub mod neon_image_linear_to_u8 {

cx
}

}
}
7 changes: 4 additions & 3 deletions src/sse_image_to_linear_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ pub mod sse_image_to_linear_unsigned {
let r_f = _mm_mul_ps(_mm_cvtepi32_ps(r), u8_scale);
let g_f = _mm_mul_ps(_mm_cvtepi32_ps(g), u8_scale);
let b_f = _mm_mul_ps(_mm_cvtepi32_ps(b), u8_scale);
let r_linear = transfer(r_f);
let g_linear = transfer(g_f);
let b_linear = transfer(b_f);
let u8_backwards = _mm_set1_ps(255f32);
let r_linear = _mm_mul_ps(transfer(r_f), u8_backwards);
let g_linear = _mm_mul_ps(transfer(g_f), u8_backwards);
let b_linear = _mm_mul_ps(transfer(b_f), u8_backwards);
(
_mm_cvtps_epi32(r_linear),
_mm_cvtps_epi32(g_linear),
Expand Down

0 comments on commit 6a53466

Please sign in to comment.