Skip to content

Little CMS 2 (C) ​

The reference open-source ICC colour management engine: profiles in, a compiled per-pixel transform out, with pixel layouts named by one bit-packed 32-bit descriptor and a plug-in API that can replace almost every stage of the engine.

FieldValue
LanguageC99 (C++17-compatible); plug-ins in C
LicenseMIT for the core; the fast_float and threaded plug-ins are GPL-3.0 (plugins/README.1ST) with a paid alternative (fast float, p. 3; threaded, p. 3)
Repositorymm2/Little-CMS
Revision read5546f1d68136f94281c9f6bfc027c6cdf4898da8 (October 27, 2026, "add a guard against too much stages"; LCMS_VERSION 2200, release 2.20)
DocumentationLittleCMS2.20 API.pdf · tutorial · Plugin API · fast float extensions (all in doc/ and plugins/ of the repository)
CategoryColour: colour management module (CMM)
TierA
Last reviewedNovember 7, 2026

NOTE

What was run. A scratch program, /tmp/lcms2-dive/probe/probe.c (outside this repository), compiled the core, fast_float and threaded from a git archive of the pinned revision with GCC 14.3 -O2 on an AMD Ryzen 9 7940HX (x86_64 Linux, 32 hardware threads). It decoded the TYPE_* descriptors, listed the stages of optimised pipelines, compared every one of the 2²⁴ 8-bit RGB inputs of an sRGB to Adobe-RGB-like transform against the float transform, tried malformed format and flag combinations, and timed each path (best of three runs). A second program, probe16.c, compared 16-bit paths against a TYPE_RGB_DBL reference. Results marked verified come from those runs; the timings are one machine, one profile pair and one compiler, and say nothing beyond that. The profile-parsing divergence against skcms is measured on the ICC page and not repeated here.


Overview ​

What it solves ​

An image that carries an ICC profile means nothing until a colour management module (CMM) links that profile to the destination's through the profile connection space and applies the result to every pixel. Little CMS (lcms2) is that module across much of the open-source desktop: its ChangeLog fixes bugs reported against colord and Krita, the CVE records name OpenJDK and GIMP as users of its 1.x predecessor, and libvips builds its icc_transform on it. Its README lists printer firmware, monitors, cameras and RIPs among its users (README.md). It parses profiles of every class (input, display, output, link, abstract, named colour), builds a transform for any pair or chain of them under any of four ICC intents plus six vendor intents, and runs it over pixel buffers in 8-bit, 16-bit, half, float or double layouts, interleaved or planar, with or without alpha.

The README states both the scope and the priority:

"Little CMS intends to be an OPEN SOURCE small-footprint color management engine, with special focus on accuracy and performance." (README.md)

"Little CMS is a FULL IMPLEMENTATION of ICC specification 4.4, it fully supports all kind of V2 and V4 profiles, including abstract, devicelink and named color profiles." (README.md)

The profile format itself (header, tag table, curve and LUT types, the v2/v4 Lab encodings) is covered on the ICC page; this page is about the engine.

Design philosophy ​

Four commitments shape the code.

  1. A transform is compiled once, then applied many times. cmsCreateTransform reads the tags, links the profiles into a cmsPipeline of stages, hands it to an optimiser that may replace the whole thing with a lookup table or a fixed-point matrix-shaper, and picks a formatter pair and a per-pixel loop. cmsDoTransform then does no allocation, takes no lock and logs nothing: "This function is re-entrant. It never fails and it never send error logs. You can re-use same transform handle in different threads calling this function" (API manual, cmsDoTransform).
  2. Errors are return values. "When a function fails, it just return the failure status. For example, if a color transform cannot be created, the function returns NULL instead of a valid handle. ... This approach is more robust and can deal with multithreaded environments" (tutorial). The log hook is a debugging aid that receives a code and English text.
  3. Everything is a plug-in. Thirteen plug-in types can replace the allocator, interpolators, curve types, formatters, tag types, tags, intents, multi-process elements, the optimiser, the whole transform, the mutex, the parallel scheduler and (from 2.20) the profile header reader. The Plugin API manual gives the reason plainly: "Licensing is another compelling reason; you can move all your intellectual property into plug-ins and be able to upgrade the core Little CMS library, keeping it in the open source side" (Plugin API, p. 4). The two plug-ins in the tree, fast_float and threaded, are exactly that split: GPL-3.0 in the repository, sold under another licence (fast float extensions, p. 3).
  4. State lives in a context. "Each context holds its owns globals and its own plug-ins. There is a global context with the id = 0 for lecacy compatibility though using the global context is not recommended" (lcms2.h). Most constructors and setters have a ...THR twin that takes a cmsContext.

The security policy adds a fifth, defensive stance: "This is a color library, not an arena for your war games", and "misusing the API does not by itself make it a vulnerability. ... C itself is unsafe" (SECURITY.md).


How it works ​

Profiles ​

A profile is an opaque cmsHPROFILE. It comes from bytes (cmsOpenProfileFromMem, ...FromFile, ...FromIOhandler2) or is synthesised in memory: cmsCreate_sRGBProfile, cmsCreateRGBProfile(whitePoint, primaries, curves), cmsCreateGrayProfile, cmsCreateLab4Profile, cmsCreateXYZProfile, cmsCreateLinearizationDeviceLink, cmsCreateBCHSWabstractProfile and others (lcms2.h). The built-in sRGB is itself a cmsCreateRGBProfileTHR call with D65 and the Rec. 709 primaries, and its curve is the parametric type 4 that encodes the sRGB piecewise function:

c
// src/cmsvirt.c (abridged)
Parameters[0] = 2.4;
Parameters[1] = 1. / 1.055;
Parameters[2] = 0.055 / 1.055;
Parameters[3] = 1. / 12.92;
Parameters[4] = 0.04045;
return cmsBuildParametricToneCurve(ContextID, 4, Parameters);

(cmsvirt.c)

cmsOpenProfileFromMem copies the caller's bytes into a block owned by a memory cmsIOHANDLER (_cmsMalloc then memmove, cmsio0.c), reads and checks the header and tag directory eagerly, and leaves every tag unparsed. cmsReadTag parses a tag on first access under the profile's mutex, caches the decoded object in Icc->TagPtrs[n] and returns the cached pointer afterwards (cmsio0.c).

The pixel-format descriptor ​

lcms2 names a buffer layout with one cmsUInt32Number whose bit fields are documented in the public header:

c
// include/lcms2.h
//                               2                1          0
//                        4 3 2 10987 6 5 4 3 2 1 098 7654 321
//                        M A O TTTTT U Y F P X S EEE CCCC BBB
//
//            M: Premultiplied alpha (only works when extra samples is 1)
//            A: Floating point -- With this flag we can differentiate 16 bits as float and as int
//            O: Optimized -- previous optimization already returns the final 8-bit value
//            T: Pixeltype
//            F: Flavor  0=MinIsBlack(Chocolate) 1=MinIsWhite(Vanilla)
//            P: Planar? 0=Chunky, 1=Planar
//            X: swap 16 bps endianness?
//            S: Do swap? ie, BGR, KYMC
//            E: Extra samples
//            C: Channels (Samples per pixel)
//            B: bytes per sample
//            Y: Swap first - changes ABGR to BGRA and KCMY to CMYK

(lcms2.h)

Constructor macros (COLORSPACE_SH, CHANNELS_SH, EXTRA_SH, BYTES_SH, DOSWAP_SH, SWAPFIRST_SH, PLANAR_SH, ENDIAN16_SH, FLAVOR_SH, FLOAT_SH, PREMUL_SH) build a descriptor, and T_* macros take it apart. The named TYPE_* constants are just combinations (lcms2.h). Decoding a few (verified):

ConstantValueFields set
TYPE_RGB_80x00040019space PT_RGB, 3 channels, 1 byte
TYPE_RGBA_80x00040099as above, 1 extra sample
TYPE_BGRA_80x00044499as above, DOSWAP and SWAPFIRST
TYPE_ARGB_80x00044099as above, SWAPFIRST only
TYPE_RGBA_8_PREMUL0x00840099as TYPE_RGBA_8, PREMUL
TYPE_RGB_160x0004001A2 bytes
TYPE_RGB_HALF_FLT0x0044001A2 bytes and FLOAT: half precision
TYPE_RGBA_FLT0x0044009C4 bytes and FLOAT
TYPE_RGB_DBL0x004400180 bytes and FLOAT: double, because the field is 3 bits
TYPE_RGB_150x0404001Afast_float's bit 26 (BIT15_SH) on top of RGB_16

Channel order is not stored as a permutation. It is the colour space's canonical order (RGB, CMYK) modified by two booleans: DOSWAP reverses the whole pixel (RGB to BGR, RGBA to ABGR), and SWAPFIRST rotates the first sample to the end or back (ARGB, and with DOSWAP, BGRA). Names read in memory order, so TYPE_BGRA_8 is blue at the lowest address, which matches Vulkan's non-packed names and Wuffs' convention (pixel format names). The descriptor carries the interleaved or planar choice but no stride: strides are call arguments.

Formatters: descriptor to 16-bit or float ​

The descriptor is not interpreted per pixel. At transform creation _cmsGetFormatter asks each formatter plug-in's factory, then falls back to stock tables that pair a Type with a Mask of ignored bits and a function; the first entry with (dwInput & ~f->Mask) == f->Type wins (cmspack.c, cmspack.c):

c
// src/cmspack.c (abridged)
{ CHANNELS_SH(3)|BYTES_SH(1),                              ANYSPACE,  Unroll3Bytes},
{ CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1),                 ANYSPACE,  Unroll3BytesSwap},
{ CHANNELS_SH(3)|EXTRA_SH(1)|BYTES_SH(1)|DOSWAP_SH(1),     ANYSPACE,  Unroll3BytesSkip1Swap},
{ CHANNELS_SH(4)|BYTES_SH(1),                              ANYSPACE,  Unroll4Bytes},

(cmspack.c)

An input formatter ("unroll") reads one pixel into cmsUInt16Number wIn[cmsMAXCHANNELS] or cmsFloat32Number[] and returns the advanced pointer; an output formatter ("pack") does the reverse. Specialised entries exist for common shapes and generic ones (UnrollChunkyBytes, UnrollPlanarBytes) catch the rest. 8-bit values widen to 16 bits by byte replication, FROM_8_TO_16(rgb) = (rgb << 8) | rgb, and narrow by a rounding multiply (lcms2_internal.h). An unsupported combination fails at creation with "Unsupported raster format" (verified for BYTES_SH(3)).

Linking: intents, black point compensation, adaptation ​

cmsCreateTransform is a thin wrapper over cmsCreateExtendedTransform, which takes up to 255 profiles, a per-profile intent, black point compensation (BPC) flag and chromatic-adaptation state, an optional gamut-check profile, two descriptors and flags (lcms2.h). It checks the colour space of each end against the descriptor (IsProperColorSpace; PT_ANY skips the check), then calls _cmsLinkProfiles (cmsxform.c). Linking applies two policy rules before dispatching on the first intent: absolute colorimetric never uses BPC, and "Force BPC for V4 profiles in perceptual and saturation" (cmscnvrt.c). Intents are a linked list of {code, description, link function} entries; the default list maps the four ICC intents to DefaultICCintents and codes 10 to 15 to the two black-preserving CMYK handlers (cmscnvrt.c). An intent plug-in prepends entries to that list.

The result is a cmsPipeline: a doubly evaluable list of cmsStages (curve sets cvst, matrices matf, CLUTs clut, Lab/XYZ conversions, named colours), each with a 16-bit and a float evaluator.

Optimisation ​

_cmsOptimizePipeline first runs PreOptimize (drop identities, join adjacent matrices, remove Lab/XYZ round trips), then tries the optimisation plug-ins, then the built-in list in order, stopping at the first that claims the pipeline (cmsopt.c):

c
// src/cmsopt.c
static _cmsOptimizationCollection DefaultOptimization[] = {

    { OptimizeByJoiningCurves,            &DefaultOptimization[1] },
    { OptimizeMatrixShaper,               &DefaultOptimization[2] },
    { OptimizeByComputingLinearization,   &DefaultOptimization[3] },
    { OptimizeByResampling,               NULL }
};

(cmsopt.c)

  • Joining curves: a pipeline of curves only becomes one table per channel.
  • Matrix-shaper: RGB to RGB with 8-bit input and the shape curve, matrix (or two), curve becomes MatShaperEval16, which looks up a first shaper into 1.14 signed fixed point, multiplies by the matrix in integers and looks up a second shaper over 16 385 entries. Its comment: "Actually it takes 256x3 16 bits tables and 16385 x 3 tables of 8 bits, in total about 50K, and the performance boost is huge!" (cmsopt.c).
  • Linearisation: pre-linearisation curves plus a CLUT, for chunky RGB to RGB when the derived curves are monotonic, not degenerate and invertible; on 16-bit input only if the caller sets cmsFLAGS_CLUT_PRE_LINEARIZATION (cmsopt.c).
  • Resampling: sample the whole pipeline into a CLUT whose grid size comes from _cmsReasonableGridpointsByColorspace (33 points for RGB by default, 49 with cmsFLAGS_HIGHRESPRECALC, 17 with LOWRESPRECALC, or cmsFLAGS_GRIDPOINTS(n)), with white-point fix-up; it states "This is a lossy optimization! does not apply in floating-point cases" (cmsopt.c, cmspcs.c).

cmsFLAGS_NOOPTIMIZE keeps the full pipeline; cmsFLAGS_FORCE_CLUT forces resampling. Float descriptors on either side skip the lossy optimisations, and a 16-bit transform whose first profile is RGB with a detected gamma below 1.6 disables optimisation, since a CLUT on linear data loses the shadows (cmsxform.c).

The per-pixel loop ​

AllocEmptyTransform lets a transform plug-in take the whole job first; otherwise it selects formatters and one of seven loops: FloatXFORM, NullFloatXFORM, NullXFORM, PrecalculatedXFORM, CachedXFORM and the gamut-check variants of the last two (cmsxform.c). The 16-bit loop is three indirect calls per pixel:

c
// src/cmsxform.c, PrecalculatedXFORM
accum = p->FromInput(p, wIn, accum, Stride->BytesPerPlaneIn);
p->Lut->Eval16Fn(wIn, wOut, p->Lut->Data);
output = p->ToOutput(p, wOut, output, Stride->BytesPerPlaneOut);

(cmsxform.c)

CachedXFORM adds a one-pixel cache: if the unrolled input equals the previous pixel's, it reuses the output. The cache is copied from the transform into a stack local at the start of each call, memcpy(&Cache, &p->Cache, sizeof(Cache)), so concurrent calls on one transform never write shared state (cmsxform.c). Alpha and other extra channels are untouched unless cmsFLAGS_COPY_ALPHA is set, in which case _cmsHandleExtraChannels copies them and the descriptors must agree on the extra count (verified: without the flag the output alpha byte kept its previous value; with it, the input alpha was copied; with TYPE_RGBA_8 in and TYPE_RGB_8 out, creation failed with "Mismatched alpha channels").

cmsDoTransform(xform, in, out, n) processes one line; cmsDoTransformLineStride takes pixels per line, line count, bytes per line and bytes per plane for both sides, through a cmsStride record (cmsxform.c, lcms2_plugin.h).

Plug-ins and contexts ​

A plug-in is a static struct that begins with cmsPluginBase: magic 'acpp', expected library version, a type signature and a Next pointer, so one entry point can register a chain of plug-ins of different types (lcms2_plugin.h). A context (struct _cmsContext_struct) is a sub-allocator plus an array of per-subsystem "chunks", one per _cmsMemoryClient value: user data, logger, alarm codes, adaptation state and one slot per plug-in type; a NULL chunk falls back to the global context (lcms2_internal.h). cmsCreateContext(plugin, userData) installs plug-ins at creation; cmsDupContext clones one. Profiles and transforms remember their context and allocate through its memory handler.

fast_float registers a formatter factory (the 15-bit Photoshop and dithered 8-bit formats, on descriptor bits 26 and 27 that the core leaves unused) and a full-transform factory, Floating_Point_Transforms_Dispatcher, which tries eleven specialised optimisers in order: 15-bit matrix-shaper, 8-bit joined curves, an SSE2 8-bit matrix-shaper, a scalar 8-bit matrix-shaper, float curves, float matrix-shaper, 8-bit and 16-bit prelinearisation plus tetrahedral, and float RGB, CMYK and Lab CLUTs (fast_float_sup.c, lcms2_fast_float.h). threaded registers a parallelisation plug-in whose scheduler wraps whatever loop was chosen (threaded_scheduler.c).


Analysis ​

1. Pixel and memory model ​

A transform sees a buffer as a base pointer, a pixel count per line, a line count, and byte strides between lines and between planes, with the per-sample layout fixed in the 32-bit descriptor at creation. Interleaved ("chunky") and planar are both supported; there is no tiling, no subsampled chroma (PT_YCbCr is a colour space; the descriptor has no subsampling field, so every pixel carries all its samples, which is 4:4:4), no N-D extent and no bit depth other than 8, 16, half, float, double and fast_float's 15. Up to 15 colour channels plus up to 7 extra samples fit in the fields (cmsMAXCHANNELS is 16). The descriptor packs colour model, component order, sample type, alpha association and plane arrangement into one integer that two programs can exchange, which is the same idea as Wuffs' pixel format (a uint32_t with colour model, order, transparency, float and four 4-bit channel depths). The differences are telling: Wuffs stores per-channel depths and therefore packed formats such as RGB 565; lcms2 stores one byte count for all samples and has no packed formats at all, but it stores the colour space, which Wuffs leaves to metadata. skcms goes the other way again: a closed enum of 42 concrete formats, skcms_PixelFormat_RGBA_8888, ..._RGBA_1010102, ..._RGB_hhh_Norm, ..._BGRA_10101010_XR, which includes packed and extended-range formats but no planar or CMYK ones (skcms_public.h).

2. Genericity mechanism ​

Runtime dispatch on data, everywhere. The descriptor is matched against mask tables to pick formatter function pointers; pipelines are linked lists of stages with function pointers for 16-bit and float evaluation; optimisers, intents, interpolators and tag types are linked lists searched in order, plug-ins first. There is no compile-time specialisation beyond the hand-written fast paths (Unroll3Bytes, MatShaperEval16, fast_float's kernels). The cost is one indirect call per pixel for each of unroll, evaluate and pack in the generic loop, which the optimisers mitigate by making Eval16Fn itself cheap and fast_float removes by replacing the loop. The design fits a C library whose consumers choose formats at run time from file headers. It is the opposite of the D approach the catalog's generic-programming thread examines, where a format would be a template parameter and the loop specialised per instantiation; the descriptor is still useful in D as a runtime value that a static foreach over supported formats can switch on.

3. Colour and metadata ​

This is the library's whole purpose. Intents follow ICC (perceptual, relative and absolute colorimetric, saturation) plus Adobe-style black point compensation and six K-preserving intents; absolute colorimetric honours an adaptation state (cmsSetAdaptationState, 0 for no adaptation to 1 for full); proofing transforms add a third profile and an out-of-gamut alarm colour (lcms2.h, lcms2.h). Profiles can be written as well as read, including device links from a transform (cmsTransform2DeviceLink), and CGATS/IT8 measurement files are parsed by cmscgats.c. Transfer functions are ICC curves: parametric types 1 to 5 and sampled tables, with segmented float curves for v4 multi-process elements; lcms2 also registers its own parametric types 6 to 8, 108 and 109 (cmsgamma.c). lcms2 knows the cicp tag (the 2.18 ChangeLog says "Fix CICp tag size") and the Microsoft MHC2 tag, but a CICP code point is metadata to it: there is no built-in PQ or HLG profile, and HDR headroom, gain maps and EXIF are outside its scope. Alpha is opaque payload unless a descriptor says PREMUL, in which case the formatters divide on input and multiply on output (straight and premultiplied alpha). See ICC for the profile model and CICP for the enumerated alternative.

4. I/O and codec architecture ​

Not a codec; it decodes one format, ICC, from a cmsIOHANDLER that wraps memory, a FILE*, a stream or a user callback table, with Read, Seek, Tell, Write, Close (lcms2_plugin.h, cmsio0.c). Reading is pull-based and random-access (tags are seeked to by offset), so it is not incremental or suspendable: the whole profile must be in memory or seekable. That is cheap in practice because profiles are kilobytes to a few megabytes, and the image codec that found the profile already holds the bytes. Profile writing (cmsSaveProfileToMem, cmsMD5computeID) uses the same handler. The 2.20 header plug-in lets a context read or write private header fields and set the accepted ICC version (cmsio0.c, lcms2_plugin.h).

5. Hostile input and safety ​

The parser is defensive at the byte level and tolerant at the structure level.

  • Reads: MemoryRead checks size * count for overflow, rejects reads past the block and returns 0 with "Read from memory error" (cmsio0.c).
  • Header: _cmsReadHeader clamps the header's size field to the real buffer size, rejects more than MAX_TABLE_TAG (100) tags, and skips (with cmsERROR_CORRUPTION_DETECTED) any tag whose offset + size exceeds the size or wraps (cmsio0.c).
  • Tags: cmsReadTag rejects tags under 8 bytes, checks the tag type against the tag's allowed types, and fails if a reader returned fewer elements than the tag descriptor requires (cmsio0.c).
  • LUTs: CubeSize computes CLUT entry counts in 64 bits and rejects anything over UINT_MAX / 15 (cmslut.c); channel counts are capped at cmsMAXCHANNELS; a multi-process element tag is capped at 50 stages, if (ElementCount > 50) goto Error; (cmstypes.c). That line is the whole of the pinned commit, "add a guard against too much stages" (a two-line diff to Type_MPE_Read, read by fetching the commit with its parent), so the cap is new at this revision.
  • Transforms: channel counts from the linked pipeline must equal the colour spaces' ("Channel count doesn't match. Profile is corrupted", cmsxform.c).

In-tree fuzzing arrived late. The 2.17 ChangeLog records "Add fuzzers foundation. Many thanks to Amir Montazery and Open Source Technology Improvement Fund (ostif.org), Google, for funding that" (ChangeLog). The in-tree harness opens a profile, reads every tag cooked and raw, every intent's input, output and device-link LUT, black points, TAC, and PostScript CSA and CRD (fuzzers.c). It calls no transform API itself; black-point detection and the PostScript generators build small internal transforms over fixed Lab or colorant values (cmssamp.c), so caller-chosen descriptors, buffers and strides are never exercised. OSS-Fuzz is older: its oldest commit touching projects/lcms is dated November 29, 2016 (a rename from targets/, a143b9b3), and transform fuzzing lives there (fifteen harnesses, including cms_transform_fuzzer.c from PDFium, which links a fuzzed profile to sRGB and transforms one pixel) under ASan, UBSan and MSan with libFuzzer, AFL and honggfuzz (project.yaml, cms_transform_fuzzer.c). The test suite does not exercise these.

The CVE record shows the shape of the risk. Older records: integer overflows leading to heap overflow in lcms 1.x (CVE-2009-0723), NULL pointer dereferences in lcms2 before 2.5 (CVE-2013-4160), an out-of-bounds read in Type_MLU_Read (CVE-2016-10165) and an integer overflow in the CGATS parser's AllocateDataSet (CVE-2018-16435) (CVE records, 2013, 2016, 2018). Two 2025 heap-overflow reports, in UnrollChunkyBytes and in the smooth2 helper, are marked disputed by the supplier: the first as "a bug in a third-party calling program", the second because the function "is never called on normal color management" (CVE-2025-29069, CVE-2025-29070). The first dispute is the API's real hazard: a formatter trusts that the buffer holds n pixels of the declared descriptor and that strides are right. Nothing in the API can check that, which is what the security policy means by "C itself is unsafe". The ChangeLog's 2.9 entry records "Several fixes related with security, and therefore not detailed here" (ChangeLog), so the record is incomplete by design.

How tolerant the parser is, compared with skcms, is tabulated on the ICC page (lcms2 opens profiles with a bad size field, a D65 PCS illuminant or a negative gamma that skcms rejects).

6. Allocation and ownership ​

Every object is heap-allocated through the context's memory handler (_cmsMalloc, _cmsMallocZero, _cmsDupMem), which a memory plug-in can replace; the context itself is allocated by the plug-in if one is given (Plugin API, p. 5). Opening a profile from memory copies the bytes (cmsio0.c); tags are allocated on first read and freed with the profile. A transform owns its optimised pipeline (copied from the profiles' stages), its formatters' private data and its cache, so profiles can be closed as soon as the transform exists, as every example does. Ownership is manual: cmsCloseProfile, cmsDeleteTransform, cmsDeleteContext. After creation, the 16-bit and float loops do not allocate; the threaded scheduler does (_cmsCalloc of slices and handles per call, threaded_scheduler.c). Compare skcms, which borrows the caller's profile bytes and builds its op program in a 32-entry array on the stack on every skcms_Transform call, so neither parsing nor transforming allocates (skcms.cc).

7. CPU parallelism and SIMD ​

The core is scalar C: integer fixed-point for 8 and 16 bits, tetrahedral interpolation (Sakamoto's algorithm) for 3-input CLUTs and trilinear on request (cmsintrp.c). Speed comes from choosing a cheaper evaluator, not vectors. fast_float adds SSE2 for the 8-bit matrix-shaper, detected at run time (CMS_DONT_USE_SSE2 disables it), and scalar fast paths elsewhere (lcms2_fast_float.h).

Thread safety is by construction for transforms: cmsDoTransform reads only immutable transform state and the per-call stack copy of the cache (cmsxform.c). Profiles are shared mutable state (lazy tag cache) and are serialised by a per-profile mutex from the mutex plug-in, pthreads or Win32 by default (cmsio0.c). Parallelism is a plug-in: when one is registered, ParalellizeIfSuitable saves the chosen loop as p->Worker and installs the plug-in's scheduler as p->xform (cmsxform.c). The threaded scheduler gives each worker at least 128 KiB of input plus output, splits by lines (or by pixels for one long line), spawns a thread per slice with pthread_create (CreateThread on Windows) on every call, runs slice 0 itself and joins (threaded_split.c, threaded_scheduler.c). There is no pool. Libvips, which already parallelises by region, sets cmsFLAGS_NOCACHE on a shared transform (libvips), with the comment that this makes "calling cmsDoTransform() from multiple threads safe" (icc_transform.c). At this lcms2 revision the cache is a per-call copy, so the flag trades the cache's benefit for nothing in thread safety.

Measured throughput for 2²⁴ 8-bit RGB pixels, sRGB to an Adobe-RGB-like matrix-shaper profile (verified, one machine, see the note at the top):

PathTimeThroughput
core 8-bit, default (matrix-shaper)242.7 ms69 Mpixel/s
core 8-bit, cmsFLAGS_FORCE_CLUT397.6 ms42 Mpixel/s
core 8-bit, cmsFLAGS_NOOPTIMIZE3 697 ms5 Mpixel/s
core float (TYPE_RGB_FLT)3 507 ms5 Mpixel/s
fast_float 8-bit (SSE2 matrix-shaper)76.9 ms218 Mpixel/s
fast_float float595.4 ms28 Mpixel/s
threaded, core 8-bit, all 32 threads22.0 ms761 Mpixel/s

A verification rerun of the same probe on the same CPU model (GCC 16.2 -O2, three invocations, each best of three) put core 8-bit default between 115.8 and 201.8 ms, NOOPTIMIZE between 1 848 and 3 300 ms and threaded between 10.2 and 51.3 ms, so the absolute times above vary by a factor of two or more with machine state. The ratios held: NOOPTIMIZE and core float 14 to 17 times the default, fast_float 8-bit 1.7 to 2.7 times faster than the default.

skcms takes the opposite route: one op-list interpreter compiled three times, baseline, Haswell (AVX2) and Skylake-X (AVX-512), selected by cpuid once (skcms.cc). Its CLUT size limit exists because the gather instructions take 32-bit indices (ICC).

8. GPU offload and interop ​

Not applicable: lcms2 has no GPU path and no shader generation. The closest artefacts are the ones a GPU consumer builds from it: a device link sampled into a 3D CLUT (cmsFLAGS_FORCE_CLUT with cmsFLAGS_GRIDPOINTS(n), then reading the clut stage via cmsGetTransformPipeline) or a .cube file, which lcms2 2.16 can import as an RGB device link (ChangeLog). OpenColorIO is the colour library in this catalog that generates shader text for its transforms.

9. Temporal, animation and containers ​

Not applicable: lcms2 transforms buffers of pixels and knows no frames, sequences or containers. The one temporal concern it affects is reuse: a transform is meant to be created once per profile pair and descriptor pair and applied to every frame, and the fast_float manual advises against cmsChangeBuffersFormat because "Transforms that are polymorphic regarding formats are not optimizable" (fast float extensions, p. 8).

10. Testing and conformance ​

testbed/testcms2.c is a 10 055-line self-checking program with 167 Check(...) calls, from fixed-point helpers and interpolation through curve inversion, every formatter, tag round trips and transforms, plus testplugin.c for the plug-in types, alpha_test.c and a C++ threading test (testcms2.c). Precision checks are tolerances, not exact oracles: an 8-bit transform of a profile onto itself may deviate by "2 contone of difference on 8 bits" (testcms2.c), and an sRGB to Lab to sRGB round trip repeated 50 times may drift by a 16-bit distance of 20 (testcms2.c). fast_float and threaded carry their own testbeds. The CI matrix runs ./configure --with-fastfloat --with-threaded, make and make check on Ubuntu and macOS, builds and runs the Visual Studio testbed on Windows, and repeats the tests under Meson and CMake (build.yml). There is no ICC conformance corpus in the tree beyond the test profiles in testbed/.

What the defaults cost in precision, against the float transform rounded to the output depth (verified):

Transform (sRGB to Adobe-RGB-like)Evaluator chosenMax errorSamples off
8-bit defaultfixed-point matrix-shaper2 of 2551.03% of 2²⁴ × 3
8-bit cmsFLAGS_NOOPTIMIZEfull pipeline, 16-bit00
8-bit cmsFLAGS_FORCE_CLUT33³ CLUT, tetrahedral5 of 2552.42%
fast_float 8-bitSSE2 matrix-shaper2 of 2550.91%
16-bit default33³ CLUT (resampled)964 of 65 535 (1.5%)mean 8.2, worst near black
16-bit cmsFLAGS_HIGHRESPRECALC49³ CLUT625mean 2.3
16-bit cmsFLAGS_GRIDPOINTS(65)65³ CLUT334mean 1.2
16-bit cmsFLAGS_NOOPTIMIZEfull pipeline00

The 16-bit rows were sampled on an 86 × 52 × 37 grid of inputs (steps of 771, 1 285 and 1 799 code values) against a TYPE_RGB_DBL cmsFLAGS_NOOPTIMIZE transform; the worst input in every CLUT row was a dark red (771, 0, 0), where the sRGB toe meets a gamma-2.2 output through a grid that is uniform in encoded values. The matrix-shaper optimisation applies only to 8-bit input and linearisation needs an explicit flag on 16-bit input, so a 16-bit RGB to RGB transform falls to resampling. The fast float manual states the general rule: "unless both formats are float, the internal lcms2 math being used is 16 bits". One more verified observation: cmsGetTransformPipeline on the default 8-bit transform lists the same three stages (cvst, matf, cvst) as the unoptimised one; the fixed-point evaluator is installed with _cmsPipelineSetOptimizationParameters, so the stage list does not reveal which evaluator will run.


Lessons for Sparkles ​

  • Q5 Colour management is a separate engine with its own lifecycle (parse, link, optimise, apply) and its own state (contexts, caches). No decoder in this catalog embeds one; libvips, libjxl (JxlCmsInterface) and Skia consume one through an interface. The Sparkles baseline has no colour management at all (existing code, G4). Binding lcms2 through ImportC, porting a subset, or adopting skcms's matrix/TRC-plus-A2B scope are the three options this evidence bears on; the MIT core makes binding legally simple, the GPL-3.0 fast paths do not.
  • Q1 lcms2's descriptor shows that a 32-bit runtime value can name colour space, channel count, extra samples, sample type, order and alpha association, and that this value can travel through a C ABI. It also shows the cost of a fixed field layout: BYTES_SH(0) must mean double because the field has three bits, packed formats cannot be expressed, and fast_float claims bits 26 and 27 for its own formats. A D pixel format that is a compile-time type (design by introspection) can still expose such a value for dispatch and FFI; Wuffs and lcms2 together give two concrete bit layouts to compare, and both name channels in memory order.
  • Q3 The optimiser is the transferable idea: a general pipeline of stages, rewritten at creation into the cheapest evaluator that meets the precision the caller asked for (NOOPTIMIZE, HIGHRESPRECALC, float). In D that rewrite can happen at compile time when the profiles are known (sRGB to linear) and at run time otherwise; the measured 15-fold gap between NOOPTIMIZE and the matrix-shaper on 8-bit data (about 50-fold against fast_float) is the price of not doing it. The 16-bit CLUT error near black is the price of doing it blindly.
  • Q3, Q7 The per-pixel loop with three indirect calls is what fast_float exists to replace. A Sparkles kernel templated on source and destination formats and evaluated over a whole row (or a Slice) avoids the indirection; the @target dispatch pattern the SIMD baseline describes (G4, G5) is what skcms does with three compilations of one interpreter. lcms2's parallelism, a thread spawned per slice per call, would not suit sparkles:event-horizon's pools; the 128 KiB minimum slice is a data point for a grain size.
  • Q5, Q8 A transform should be immutable after creation, so it can be shared across threads without locks; lcms2's stack copy of its cache is the detail that makes this true. In @safe pure nothrow @nogc D the same property falls out of a const transform and a scratch buffer per call, or a UniqueBuffer owned by the worker.
  • Q4, Q5 The profile parser needs the opposite property: lazy, cached tag decoding under a mutex is a mutable shared object. Parsing everything once into an immutable value (as skcms does into skcms_ICCProfile) suits @nogc and dip1000 better, with the profile bytes owned by the image's metadata buffer.
  • Q4, Q8 lcms2's hardening is a list of scattered checks, some added after reports: overflow-checked reads, tag bounds, UINT_MAX / 15 on CLUT sizes, 100 tags, 50 MPE stages. A Sparkles parser described in sparkles:wired (wired baseline) would state these as limits in the description rather than as scattered ifs, and the in-tree fuzzer's gap (no caller-built transforms, no pixel buffers) shows that the transform construction path needs fuzzing too. The hostile-input papers argue the same.
  • Q8 lcms2 is a ready differential oracle for a D implementation: NOOPTIMIZE and TYPE_*_DBL give a reference path, the testbed's tolerances (2 levels at 8 bits) give a starting threshold, and the optimised paths give known, measurable error to stay within. Such an oracle would sit beside the existing stb_image PNG oracle (existing code) as a dev-shell dependency, not a runtime one.
  • Q9 The plug-in split, MIT core plus GPL fast paths, and the separation of profile parsing, linking and pixel evaluation into separable modules (cmsio*, cmscnvrt, cmsopt, cmsxform, cmspack) are evidence that colour management is its own package boundary, consumed by formats and algorithms rather than living inside either.
  • Q2 Contexts as a struct of per-subsystem slots, inherited from a global default, are a C answer to "configure the library per user". The D answer the guidelines prefer is a policy passed as a template or a value at the call site; lcms2's experience that the global context is "not recommended" (lcms2.h) argues against a global.

Strengths ​

  • Complete ICC v2 and v4 support, including device links, abstract and named-colour profiles, proofing, BPC and K-preserving intents; a de facto reference.
  • One compact runtime descriptor covers interleaved and planar layouts, five sample types, alpha, premultiplication, byte order and channel order.
  • Transforms are immutable and thread-safe to apply; cmsDoTransform cannot fail.
  • A real optimiser with selectable precision; the default 8-bit matrix-shaper path is within 2 levels of the float reference.
  • Nearly every subsystem is replaceable per context, including the whole transform.
  • MIT core, no dependencies beyond libc and optional pthreads.

Weaknesses ​

  • Generic loops pay three indirect calls per pixel; the fast paths are in GPL plug-ins.
  • No SIMD in the core and no GPU path; the parallel plug-in spawns threads per call.
  • 16-bit integer transforms default to a resampled CLUT whose error near black (964 of 65 535 in the measured case) is far larger than the 8-bit path's.
  • Buffers are trusted: descriptor, pixel count and strides cannot be checked, and the disputed 2025 CVE shows callers getting this wrong.
  • The fuzz harness in the tree covers profile parsing and the internal transforms it reaches, not caller-built transforms over pixel buffers; those are fuzzed in OSS-Fuzz.
  • The parser tolerates malformed profiles that a strict reader rejects, so two CMMs can disagree on the same file (ICC).
  • No HDR model: no PQ or HLG built-ins, no headroom, no gain maps.

Key design decisions and trade-offs ​

DecisionRationaleTrade-off
Pixel layout as one bit-packed cmsUInt32NumberRuntime-selectable, ABI-stable, cheap to compare and to match against masksFixed field widths: no packed formats, double encoded as 0 bytes, plug-ins borrow high bits
Compile a transform once; formatters and evaluator chosen thenPer-pixel work is fixed; transforms are immutable and shareableCreation is costly; format-polymorphic transforms (cmsChangeBuffersFormat) lose optimisation
16-bit integer internal precision unless both ends are floatFast fixed-point arithmetic and compact tablesFloat paths are an order of magnitude slower; 16-bit CLUT resampling loses shadow precision
Optimiser chain: curves, matrix-shaper, linearisation, resamplingFirst applicable rewrite wins; flags trade memory and accuracyLossy by default; the stage list no longer tells which evaluator runs
Thirteen plug-in types, registered per contextExtensibility and a licensing seam for proprietary fast pathsFast paths are GPL; plug-in ABI is tied to ExpectedVersion
Lazy tag parsing behind a per-profile mutexOpening is cheap; only tags a transform needs are decodedProfiles are mutable shared objects; a mutex plug-in is needed without pthreads
Tolerate malformed profiles where repairableReal-world profiles are often slightly wrongDivergence from strict readers; hostile inputs get further into the code
Return NULL on failure, log text to a hookThread-friendly, no global error stateError codes are coarse families; the reason is only in English text

Sources ​