img_core/icc_cms.rs#
Source: lib/img_core/icc_cms.rs
icc_cms -- ICC colour transforms for every device space: one colour in f64, many pixels
through a grid (icc_xform is the fast path of 8-bit RGB and grey rows)
ColorTransform::new(src, dst, intent, flags) -> ColorTransform | ERR_*
src / dst = profile bytes of class scnr / mntr / prtr / spac with a device space of 1
to 15 channels (GRAY, RGB, CMY, CMYK, 2CLR..FCLR, MCH1..MCHF, Lab, XYZ, YCbr, Luv,
Yxy, HSV, HLS) and PCS XYZ or Lab; dst None = the built-in sRGB (icc_builtin).
intent ICC_* (icc_xform), flags ICF_BPC. ERR_TRUNC / ERR_HDR (a broken profile or
tag, channel counts that do not fit the spaces), ERR_UNSUPPORTED (abstract, link and
named colour profiles, other tag types such as mpet, a side without its LUT or
matrix/TRC), ERR_LIMIT (a curve of more than 2^20 entries, a CLUT of more than 2^25
values)
xf.inputs() / xf.outputs() the channels of src / dst
xf.eval(input, out) one colour: device values 0..1 -> destination values 0..1
(Lab: L / 100, (a + 128) / 255, (b + 128) / 255; XYZ: the
PCS encoding, X * 32768 / 65535); missing inputs count 0
ColorGrid::new(&xf) -> Some for 1 to 4 inputs: the transform sampled at 256, 65^2, 33^3
or 17^4 nodes (f32); grid.eval(input, out), grid.rows8(src, dst)
for interleaved 8-bit pixels
profile_info(data) -> ProfileInfo | ERR_* class, space, PCS, channels, version
The maths are LittleCMS 2's (the reference of tools/cms_check.py, and the CMM of Poppler and
Ghostscript), in f64:
* tags: the intent's A2B / B2A (absolute: *1), else *0, else matrix/TRC (RGB) or kTRC
(grey); lut8 / lut16 (their matrix on 3 inputs when it is not the identity, input
tables, CLUT, output tables), mAB (A, CLUT, M, matrix, B) and mBA (B, matrix, M, CLUT, A)
with 1 to 15 channels. CLUTs: 1 input linear, 2 bilinear, 3 tetrahedral (the B2A of a
Lab PCS trilinear, as lcms reads it), 4 and more linear in the first input between
the CLUTs of the others. lut16 with a Lab PCS has the v2 encoding (65280 = 100), every
other Lab the v4 one; a lut16 of a Lab device space too.
* matrix/TRC destinations: the inverse matrix, linear values clamped to 0..1, the inverse
curves (para analytically, curv through a 4096-sample inverse as cmsReverseToneCurve).
* the absolute intent scales XYZ by the media whites (wtpt; D50 for v2 display profiles).
* black point compensation (ICF_BPC, and always for perceptual and saturation into a v4
destination) maps the source's black point to the destination's in XYZ; the black
points as cmsDetectBlackPoint / cmsDetectDestinationBlackPoint find them: v4 perceptual
and saturation the fixed perceptual black (matrix/TRC: the darkest colorant); a v2 CMYK
output profile under relative colorimetric the perceptual round trip of L* = 0; else the
darkest colorant made neutral with L* <= 50; LUT destinations of grey, RGB and CMYK by
Adobe's round trip of an L* ramp and its quadratic fit.
A grid prelinearises as lcms does: the source's separable stages (TRCs, input tables or A
curves before; after the CLUT a lut8 / lut16's output tables or an mAB's B curves) go per
pixel through tables of 4097 values, an mAB's M curves and matrix with its B curves exactly;
the nodes hold what lies between (the CLUT's outputs, a matrix/TRC's XYZ), so a CLUT on a grid
of a multiple of its density is interpolated exactly (one of other densities, 65 points to a
grid of 33, only approximately). Each pixel goes on to XYZ, the intent's adjustment and the
destination, whose grey and RGB curves end through a table at linear (j / 4096)^2.
tools/cms_check.py measures: eval within 0.0012 of LittleCMS on 71 profiles, the grids of
those within one 8-bit step of eval. Curves are clipped to 0..1 (ICC.1).
Every length and offset of a profile is checked before it is read (icc_check validated the
tag table); a transform owns its tables, the profile bytes may go.
/// the most channels of a space (15CLR)
MAX_CHANNELS: usize = 15
struct ProfileInfowhat the header of a profile saysclass: [u8; 4]-- the profile class (scnr, mntr, prtr, spac, link, abst, nmcl)space: [u8; 4]-- the data colour space (GRAY, RGB, CMYK, Lab, 4CLR ...)pcs: [u8; 4]-- the profile connection space (XYZ or Lab)channels: usize-- the channels of the data colour space (0 for a space this module does not know)version: u32-- the version field of the header (0x04300000 = 4.3)
fn profile_info(data: &[u8]) -> Result<ProfileInfo, i32>the header of a profile (icc_check: size, 'acsp', version 2..5, the tag table)struct ColorTransforma colour transform from one profile to another, every device space
impl ColorTransform
fn new(src: &[u8], dst: Option<&[u8]>, intent: u32, flags: u32) -> Result<ColorTransform, i32>a transform from profile src to profile dst (None = the built-in sRGB)fn inputs(&self) -> usizethe channels of the source profile's spacefn outputs(&self) -> usizethe channels of the destination profile's spacefn eval(&self, input: &[f64], out: &mut [f64])one colour: inputs 0..1 in the source's encoding -> outputs 0..1 (as many as out holds, at most outputs())struct ColorGrida transform of 1 to 4 inputs sampled on a grid, for many pixels, as LittleCMS prelinearises: the separable first stage of the source (its TRCs, input tables or A curves) goes per pixel through tables, the grid holds the rest in the source's own PCS (Lab or XYZ, the intent's adjustment applied). A LUT's CLUT is piecewise linear there, and on a grid of a multiple of its density the interpolation gives it exactly. Each pixel then goes to XYZ and through the destination; grey and RGB end through a table of the inverse curve.
impl ColorGrid
fn new(xf: &ColorTransform) -> Option<ColorGrid>the grid of xf: 256 nodes (1 input), 65^2, 33^3, 17^4; None for more inputsfn inputs(&self) -> usizethe channels of the source's spacefn outputs(&self) -> usizethe channels of the destination's spacefn eval(&self, input: &[f32], out: &mut [f32])one colour: inputs 0..1 -> outputs 0..1 (as many as out holds, at most outputs())fn rows8(&self, src: &[u8], dst: &mut [u8])interleaved 8-bit pixels: src inputs() bytes each -> dst outputs() bytes each (as many pixels as both hold)