/* * // Copyright (c) Radzivon Bartoshyk 4/2025. All rights reserved. * // * // Redistribution and use in source and binary forms, with or without modification, * // are permitted provided that the following conditions are met: * // * // 1. Redistributions of source code must retain the above copyright notice, this * // list of conditions and the following disclaimer. * // * // 2. Redistributions in binary form must reproduce the above copyright notice, * // this list of conditions and the following disclaimer in the documentation * // and/or other materials provided with the distribution. * // * // 3. Neither the name of the copyright holder nor the names of its * // contributors may be used to endorse or promote products derived from * // this software without specific prior written permission. * // * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ use crate::conversions::TransformMatrixShaper; use crate::conversions::rgbxyz::{ TransformMatrixShaperOptimized, make_rgb_xyz_rgb_transform, make_rgb_xyz_rgb_transform_opt, }; use crate::conversions::rgbxyz_fixed::{make_rgb_xyz_q2_13, make_rgb_xyz_q2_13_opt}; use crate::{CmsError, Layout, TransformExecutor, TransformOptions}; use num_traits::AsPrimitive; const FIXED_POINT_SCALE: i32 = 13; // Q2.13; pub(crate) trait RgbXyzFactory + Default> { fn make_transform( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaper, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError>; } pub(crate) trait RgbXyzFactoryOpt + Default> { fn make_optimized_transform< const LINEAR_CAP: usize, const GAMMA_LUT: usize, const BIT_DEPTH: usize, >( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaperOptimized, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError>; } impl RgbXyzFactory for u16 { fn make_transform( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaper, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if BIT_DEPTH < 16 && transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { return make_rgb_xyz_q2_13::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } make_rgb_xyz_rgb_transform::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactory for f32 { fn make_transform( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaper, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { return make_rgb_xyz_q2_13::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } make_rgb_xyz_rgb_transform::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactory for f64 { fn make_transform( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaper, _: TransformOptions, ) -> Result + Send + Sync>, CmsError> { make_rgb_xyz_rgb_transform::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactory for u8 { fn make_transform( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaper, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ); } } make_rgb_xyz_q2_13::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ) } else { make_rgb_xyz_rgb_transform::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ) } } } // Optimized factories impl RgbXyzFactoryOpt for u16 { fn make_optimized_transform< const LINEAR_CAP: usize, const GAMMA_LUT: usize, const BIT_DEPTH: usize, >( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaperOptimized, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if BIT_DEPTH >= 12 && transform_options.prefer_fixed_point { #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { if std::arch::is_aarch64_feature_detected!("rdm") { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q1_30_opt; return make_rgb_xyz_q1_30_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } } if BIT_DEPTH < 16 && transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2_opt; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2_opt::< u16, LINEAR_CAP, FIXED_POINT_SCALE, >( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH ); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41_opt; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41_opt::< u16, LINEAR_CAP, FIXED_POINT_SCALE, >( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH ); } } #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { return make_rgb_xyz_q2_13_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } make_rgb_xyz_rgb_transform_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactoryOpt for f32 { fn make_optimized_transform< const LINEAR_CAP: usize, const GAMMA_LUT: usize, const BIT_DEPTH: usize, >( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaperOptimized, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2_opt; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2_opt::< f32, LINEAR_CAP, FIXED_POINT_SCALE, >( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH ); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41_opt; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41_opt::< f32, LINEAR_CAP, FIXED_POINT_SCALE, >( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH ); } } #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { return if std::arch::is_aarch64_feature_detected!("rdm") { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q1_30_opt; make_rgb_xyz_q1_30_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } else { make_rgb_xyz_q2_13_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) }; } } make_rgb_xyz_rgb_transform_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactoryOpt for f64 { fn make_optimized_transform< const LINEAR_CAP: usize, const GAMMA_LUT: usize, const BIT_DEPTH: usize, >( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaperOptimized, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if transform_options.prefer_fixed_point { #[cfg(all(target_arch = "aarch64", target_feature = "neon", feature = "neon"))] { if std::arch::is_aarch64_feature_detected!("rdm") { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q1_30_opt; return make_rgb_xyz_q1_30_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ); } } } make_rgb_xyz_rgb_transform_opt::( src_layout, dst_layout, profile, GAMMA_LUT, BIT_DEPTH, ) } } impl RgbXyzFactoryOpt for u8 { fn make_optimized_transform< const LINEAR_CAP: usize, const GAMMA_LUT: usize, const BIT_DEPTH: usize, >( src_layout: Layout, dst_layout: Layout, profile: TransformMatrixShaperOptimized, transform_options: TransformOptions, ) -> Result + Send + Sync>, CmsError> { if transform_options.prefer_fixed_point { #[cfg(all(target_arch = "x86_64", feature = "avx512"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx512_opt; if std::arch::is_x86_feature_detected!("avx512bw") && std::arch::is_x86_feature_detected!("avx512vl") { return make_rgb_xyz_q2_13_transform_avx512_opt::< u8, LINEAR_CAP, FIXED_POINT_SCALE, >(src_layout, dst_layout, profile, GAMMA_LUT, 8); } } #[cfg(all(target_arch = "x86_64", feature = "avx"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_avx2_opt; if std::arch::is_x86_feature_detected!("avx2") { return make_rgb_xyz_q2_13_transform_avx2_opt::< u8, LINEAR_CAP, FIXED_POINT_SCALE, >(src_layout, dst_layout, profile, GAMMA_LUT, 8); } } #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))] { use crate::conversions::rgbxyz_fixed::make_rgb_xyz_q2_13_transform_sse_41_opt; if std::arch::is_x86_feature_detected!("sse4.1") { return make_rgb_xyz_q2_13_transform_sse_41_opt::< u8, LINEAR_CAP, FIXED_POINT_SCALE, >(src_layout, dst_layout, profile, GAMMA_LUT, 8); } } make_rgb_xyz_q2_13_opt::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ) } else { make_rgb_xyz_rgb_transform_opt::( src_layout, dst_layout, profile, GAMMA_LUT, 8, ) } } }