mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 17:27:19 +00:00
The crate denies panics, but asserts and slice indexing were reachable from arguments: pixel strides, buffers that are not whole pixels or a destination shorter than the source, single-colour slices shorter than the channel count, CLUT and pipeline dimensions, and LUT sizes. Every such check now returns CmsError::InvalidArgument. Buffer conversions validate once per call, before any pixel is written, so the per-pixel loops carry no extra checks; internal callers that already guarantee lengths use crate-private unchecked evaluation. Pipeline, Clut and Lut3d keep their fields private (read through accessors) so a validated value cannot be changed into one that panics. Pipeline::new checks matrix shapes and channel counts; Clut::new and Clut::sample check grid, outputs and the table size limit. API changes (unreleased crate): - Transform::eval, eval_fast, convert_u8/u16/f32, apply, convert_bytes_many and from_pipeline return Result. - Pipeline::new, extend and eval, Clut::new, sample, eval, eval_trilinear and eval_multilinear return Result. - GamutCheck::delta_e and out_of_gamut, Lut3d::from_transform, LutFile::identity and from_fn, synth::cmyk_profile and coated_cmyk return Result. - Pipeline, Clut and Lut3d fields are read through methods.
80 lines
3.1 KiB
Rust
80 lines
3.1 KiB
Rust
//! Gamut checks against a proof (output) profile: Photoshop's View › Gamut Warning.
|
||
|
||
use crate::clut::Clut;
|
||
use crate::pipeline::{Pipeline, Stage};
|
||
use crate::profile::Profile;
|
||
use crate::transform::link_stages;
|
||
use crate::{CmsError, Intent, check_len};
|
||
|
||
/// Default ΔE76 threshold above which a colour counts as out of gamut.
|
||
pub const DEFAULT_THRESHOLD: f32 = 4.0;
|
||
|
||
/// Precomputed out-of-gamut test for colours of `src` against `proof`.
|
||
///
|
||
/// A colour is out of gamut when sending it to the proof device (relative colorimetric,
|
||
/// clipping) and back to Lab moves it by more than the threshold (ΔE76). The ΔE is tabulated
|
||
/// over the source device space (33³ grid for 3 channels, 17⁴ for 4, exact otherwise).
|
||
#[derive(Debug)]
|
||
pub struct GamutCheck {
|
||
inputs: usize,
|
||
grid: Option<Clut>,
|
||
exact: (Pipeline, Pipeline),
|
||
pub threshold: f32,
|
||
}
|
||
|
||
impl GamutCheck {
|
||
pub fn new(src: &Profile, proof: &Profile, threshold: f32) -> Result<GamutCheck, CmsError> {
|
||
let lab = crate::Builtin::LabD50.profile();
|
||
let direct = Pipeline::new(src.channels(), link_stages(src, lab, Intent::RelativeColorimetric, false)?)?;
|
||
let mut via = link_stages(src, proof, Intent::RelativeColorimetric, false)?;
|
||
via.push(Stage::Clamp01);
|
||
via.extend(link_stages(proof, lab, Intent::RelativeColorimetric, false)?);
|
||
let via = Pipeline::new(src.channels(), via)?;
|
||
let de = |p: &[f32], o: &mut [f32]| {
|
||
let (mut a, mut b) = ([0.0f32; 16], [0.0f32; 16]);
|
||
direct.run(p, &mut a);
|
||
via.run(p, &mut b);
|
||
let d = |i: usize, s: f32| (a[i] - b[i]) * s;
|
||
o[0] = (d(0, 100.0).powi(2) + d(1, 255.0).powi(2) + d(2, 255.0).powi(2)).sqrt();
|
||
};
|
||
let n = src.channels();
|
||
let grid = match n {
|
||
1 => Some(Clut::sample(vec![256], 1, de)?),
|
||
3 => Some(Clut::sample(vec![33; 3], 1, de)?),
|
||
4 => Some(Clut::sample(vec![17; 4], 1, de)?),
|
||
_ => None,
|
||
};
|
||
Ok(GamutCheck { inputs: n, grid, exact: (direct, via), threshold })
|
||
}
|
||
|
||
pub fn inputs(&self) -> usize {
|
||
self.inputs
|
||
}
|
||
|
||
/// ΔE76 between the colour (at least `inputs` normalised values) and its proof round trip.
|
||
pub fn delta_e(&self, px: &[f32]) -> Result<f32, CmsError> {
|
||
check_len("gamut check input", px.len(), self.inputs)?;
|
||
let mut o = [0.0f32; 1];
|
||
match &self.grid {
|
||
Some(g) => {
|
||
let mut c = [0.0f32; 16];
|
||
for (d, s) in c.iter_mut().zip(&px[..self.inputs]) {
|
||
*d = s.clamp(0.0, 1.0);
|
||
}
|
||
g.interp(&c[..self.inputs], &mut o);
|
||
}
|
||
None => {
|
||
let (mut a, mut b) = ([0.0f32; 16], [0.0f32; 16]);
|
||
self.exact.0.run(px, &mut a);
|
||
self.exact.1.run(px, &mut b);
|
||
o[0] = ((a[0] - b[0]) * 100.0).hypot((a[1] - b[1]) * 255.0).hypot((a[2] - b[2]) * 255.0);
|
||
}
|
||
}
|
||
Ok(o[0])
|
||
}
|
||
|
||
pub fn out_of_gamut(&self, px: &[f32]) -> Result<bool, CmsError> {
|
||
Ok(self.delta_e(px)? > self.threshold)
|
||
}
|
||
}
|