pic_scale/colors/
xyz_scaler.rsuse colorutils_rs::{
rgba_to_xyz_with_alpha, srgb_to_xyz, xyz_to_srgb, xyz_with_alpha_to_rgba, TransferFunction,
SRGB_TO_XYZ_D65, XYZ_TO_SRGB_D65,
};
use crate::scaler::{Scaling, ScalingF32};
use crate::{ImageSize, ImageStore, ResamplingFunction, Scaler, ThreadingPolicy};
#[derive(Debug, Copy, Clone)]
pub struct XYZScaler {
pub(crate) scaler: Scaler,
}
impl XYZScaler {
pub fn new(filter: ResamplingFunction) -> Self {
XYZScaler {
scaler: Scaler::new(filter),
}
}
fn rgba_to_xyz(store: ImageStore<u8, 4>) -> ImageStore<f32, 4> {
let mut new_store = ImageStore::<f32, 4>::alloc(store.width, store.height);
let lab_stride = store.width as u32 * 4u32 * std::mem::size_of::<f32>() as u32;
rgba_to_xyz_with_alpha(
store.buffer.borrow(),
store.width as u32 * 4u32,
new_store.buffer.borrow_mut(),
lab_stride,
store.width as u32,
store.height as u32,
&SRGB_TO_XYZ_D65,
TransferFunction::Srgb,
);
new_store
}
fn xyz_to_srgba(store: ImageStore<f32, 4>) -> ImageStore<u8, 4> {
let mut new_store = ImageStore::<u8, 4>::alloc(store.width, store.height);
xyz_with_alpha_to_rgba(
store.buffer.borrow(),
store.width as u32 * 4u32 * std::mem::size_of::<f32>() as u32,
new_store.buffer.borrow_mut(),
store.width as u32 * 4u32,
store.width as u32,
store.height as u32,
&XYZ_TO_SRGB_D65,
TransferFunction::Srgb,
);
new_store
}
}
impl Scaling for XYZScaler {
fn set_threading_policy(&mut self, threading_policy: ThreadingPolicy) {
self.scaler.threading_policy = threading_policy;
}
fn resize_rgb(&self, new_size: ImageSize, store: ImageStore<u8, 3>) -> ImageStore<u8, 3> {
const COMPONENTS: usize = 3;
let mut lab_store = ImageStore::<f32, COMPONENTS>::alloc(store.width, store.height);
let lab_stride =
lab_store.width as u32 * COMPONENTS as u32 * std::mem::size_of::<f32>() as u32;
srgb_to_xyz(
store.buffer.borrow(),
store.width as u32 * COMPONENTS as u32,
lab_store.buffer.borrow_mut(),
lab_stride,
lab_store.width as u32,
lab_store.height as u32,
);
let new_store = self.scaler.resize_rgb_f32(new_size, lab_store);
let mut new_u8_store = ImageStore::<u8, COMPONENTS>::alloc(new_size.width, new_size.height);
let new_lab_stride =
new_store.width as u32 * COMPONENTS as u32 * std::mem::size_of::<f32>() as u32;
xyz_to_srgb(
new_store.buffer.borrow(),
new_lab_stride,
new_u8_store.buffer.borrow_mut(),
new_u8_store.width as u32 * COMPONENTS as u32,
new_store.width as u32,
new_store.height as u32,
);
new_u8_store
}
fn resize_rgba(
&self,
new_size: ImageSize,
store: ImageStore<u8, 4>,
is_alpha_premultiplied: bool,
) -> ImageStore<u8, 4> {
let mut src_store = store;
let pool = self
.scaler
.threading_policy
.get_pool(ImageSize::new(new_size.width, new_size.height));
if is_alpha_premultiplied {
let mut premultiplied_store =
ImageStore::<u8, 4>::alloc(src_store.width, src_store.height);
src_store.unpremultiply_alpha(&mut premultiplied_store, &pool);
src_store = premultiplied_store;
}
let lab_store = Self::rgba_to_xyz(src_store);
let new_store = self
.scaler
.resize_rgba_f32_impl(new_size, lab_store, false, &pool);
let rgba_store = Self::xyz_to_srgba(new_store);
if is_alpha_premultiplied {
let mut premultiplied_store =
ImageStore::<u8, 4>::alloc(rgba_store.width, rgba_store.height);
rgba_store.premultiply_alpha(&mut premultiplied_store, &pool);
return premultiplied_store;
}
rgba_store
}
}