...

Package icctransform

Overview ▾

Package icctransform interprets matrix/TRC RGB ICC profiles and converts device colour to sRGB. It implements only the common matrix/TRC display profile model (rXYZ/gXYZ/bXYZ colorant matrix plus per-channel tone reproduction curves, XYZ profile connection space); profiles outside that model - CMYK, grayscale, Lab PCS, LUT-based (mft1/mft2) and v4 mAB/mBA pipelines - are reported as unsupported so callers fall back to their existing colour handling.

The package is a pure leaf: it deliberately imports nothing from model (and must not import model/internal/colorprofile, which itself imports model), so model can depend on it without an import cycle. It therefore re-reads the 128-byte ICC header locally rather than reusing colorprofile.ParseHeader.

Like the iccextract package, the parser is defensive: malformed or truncated input yields an error rather than a panic, and any error means "fall back", never "abort".

type Transform

Transform converts device RGB from a matrix/TRC profile to sRGB. It holds the per-channel linearisation curves and the single precomputed matrix that maps linear device RGB straight to linear sRGB.

type Transform struct {
    // contains filtered or unexported fields
}

func NewTransform

func NewTransform(profile []byte) (*Transform, error)

NewTransform parses an ICC profile and builds a matrix/TRC -> sRGB transform. It returns an error when the profile is corrupt or is not a supported matrix/TRC RGB profile; in either case the caller falls back to its existing colour handling. The returned Transform is immutable and safe for concurrent use.

func (*Transform) Convert

func (t *Transform) Convert(r, g, b float64) (sr, sg, sb float64)

Convert maps one normalised device-RGB triple (each component in [0,1]) to normalised sRGB (each component in [0,1]). It linearises each channel through its TRC, applies the combined matrix to reach linear sRGB, then applies the sRGB encoding transfer function. Out-of-gamut and non-finite intermediate values are clamped, so the result is always within [0,1].

func (*Transform) EncodeLinear

func (t *Transform) EncodeLinear(lr, lg, lb float64) (sr, sg, sb float64)

EncodeLinear applies the combined colorant/adaptation matrix and the sRGB encoding transfer function to a TRC-linearised device-RGB triple, returning normalised sRGB. It is the second half of Convert.

func (*Transform) LinearizeChannel

func (t *Transform) LinearizeChannel(channel int, x float64) float64

LinearizeChannel applies the per-channel TRC linearisation to a single normalised device value, where channel is 0=red, 1=green, 2=blue. It is the linearisation half of Convert, exposed so callers can precompute per-channel lookup tables.