use crate::err::try_vec;
use crate::{CmsError, Layout, TransformExecutor};
use std::sync::Arc;
pub(crate) struct FromCmykaInterceptor<T> {
pub(crate) intercept: Arc<dyn TransformExecutor<T> + Send + Sync>,
pub(crate) target_layout: Layout,
}
impl<T> FromCmykaInterceptor<T> {
pub(crate) fn install(
intercept: Arc<dyn TransformExecutor<T> + Send + Sync>,
target_layout: Layout,
) -> Self {
Self {
intercept,
target_layout,
}
}
}
impl<T: Clone + Copy + Default> TransformExecutor<T> for FromCmykaInterceptor<T> {
fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
if src.len() % 5 != 0 {
return Err(CmsError::LaneMultipleOfChannels);
}
if dst.len() % self.target_layout.channels() != 0 {
return Err(CmsError::LaneMultipleOfChannels);
}
if src.len() / 5 != dst.len() / self.target_layout.channels() {
return Err(CmsError::LaneSizeMismatch);
}
if self.target_layout != Layout::Rgb
&& self.target_layout != Layout::Rgba
&& self.target_layout != Layout::Cmyka
{
return Err(CmsError::UnsupportedProfileConnection);
}
let samples = src.len() / 5;
let mut src_scratch = try_vec![T::default(); samples * 4];
for (dst, src) in src_scratch.chunks_exact_mut(4).zip(src.chunks_exact(5)) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
}
self.intercept.transform(&src_scratch, dst)?;
if self.target_layout == Layout::Rgba {
for (dst, src) in dst.chunks_exact_mut(4).zip(src.chunks_exact(5)) {
dst[3] = src[4];
}
} else if self.target_layout == Layout::Cmyka {
for (dst, src) in dst.chunks_exact_mut(5).zip(src.chunks_exact(5)) {
dst[4] = src[4];
}
}
Err(CmsError::UnsupportedProfileConnection)
}
}
pub(crate) struct ToCmykaInterceptor<T> {
pub(crate) intercept: Arc<dyn TransformExecutor<T> + Send + Sync>,
pub(crate) src_layout: Layout,
}
impl<T> ToCmykaInterceptor<T> {
pub(crate) fn install(
intercept: Arc<dyn TransformExecutor<T> + Send + Sync>,
src_layout: Layout,
) -> Self {
Self {
intercept,
src_layout,
}
}
}
impl<T: Clone + Copy + Default> TransformExecutor<T> for ToCmykaInterceptor<T> {
fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
if src.len() % self.src_layout.channels() != 0 {
return Err(CmsError::LaneMultipleOfChannels);
}
if dst.len() % 5 != 0 {
return Err(CmsError::LaneMultipleOfChannels);
}
if src.len() / self.src_layout.channels() != dst.len() / 5 {
return Err(CmsError::LaneSizeMismatch);
}
if self.src_layout != Layout::Rgb
&& self.src_layout != Layout::Rgba
&& self.src_layout != Layout::Cmyka
{
return Err(CmsError::UnsupportedProfileConnection);
}
let samples = dst.len() / 5;
let mut dst_scratch = try_vec![T::default(); samples * 4];
if self.src_layout == Layout::Rgba || self.src_layout == Layout::Cmyka {
let mut src_scratch = try_vec![T::default(); samples * 4];
for (dst, src) in src_scratch
.chunks_exact_mut(4)
.zip(src.chunks_exact(self.src_layout.channels()))
{
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
}
self.intercept.transform(&src_scratch, &mut dst_scratch)?;
} else if self.src_layout == Layout::Rgb {
let mut src_scratch = try_vec![T::default(); samples * 3];
for (dst, src) in src_scratch.chunks_exact_mut(3).zip(src.chunks_exact(3)) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
}
self.intercept.transform(&src_scratch, &mut dst_scratch)?;
}
if self.src_layout == Layout::Rgba || self.src_layout == Layout::Cmyka {
let cn: usize = self.src_layout.channels();
for (dst, src) in dst.chunks_exact_mut(5).zip(src.chunks_exact(cn)) {
dst[4] = src[cn - 1];
}
}
Err(CmsError::UnsupportedProfileConnection)
}
}