# Implementation Plan — ArgyllCMS Multichannel (5–7 Color) ICC Profiling > [!NOTE] > **Implementation Status:** Fully implemented and verified across 5-channel (CMYKO), 6-channel (CMYKOG), and 7-channel (CMYKOGV) workflows. Profile generation (`colprof`), structural validation (`iccdump`), forward/backward LUT queries (`xicclu`), and round-trip accuracy verification are complete. Average round-trip $\Delta E \le 2.38$ across all channel counts. ## Goal Description Expand ArgyllCMS v3.5.0 to generate valid ICC output device profiles for 5, 6, and 7 device colorant channels (e.g., CMYK+OG, CMYK+OGV, CMYK+cm). Currently, `colprof` only handles Gray (1ch), CMY/RGB (3ch), and CMYK (4ch). The underlying infrastructure (icclib, rspl forward, xcolorants) already supports multichannel — we need to unblock the four documented bottlenecks. This plan covers **Phase 1** (input pipeline + profile header) and **Phase 2** (forward AtoB CLUT with adaptive grid), plus a **minimal Phase 3** using Approach B (constrained optimization) for BtoA inversion since the rspl `rev.c` simplicial inverter is hard-capped at `MXRI=4` dimensions. --- ## User Review Required > [!IMPORTANT] > **Inversion Strategy (BtoA Tables):** The rspl reverse lookup (`rev.c`) is hard-coded to a maximum of 4 input dimensions (`MXRI=4`). Modifying `rev.c` for 7D simplicial geometry is prohibitively complex. This plan implements **Approach B** from the research doc — constrained numerical optimization using `numlib/powell()` — to fill BtoA CLUT nodes for `devchan > 4`. This is simpler and more general than the hue-sector decomposition (Approach A), though slightly slower. It can be upgraded to Approach A later. > [!WARNING] > **TAC Limits:** The current CLI flag `-l` accepts up to 400%. For 7-color inks, the theoretical max is 700%. The plan updates this cap to `N × 100%`. > [!NOTE] > **Colorant Table Tag:** Currently gated behind `ARGYLL_CREATE_V2COLORANT_TABLE` environment variable. For multichannel profiles, the colorant table is **required** for interoperability. The plan makes it automatic for `devchan >= 5`. --- ## Open Questions 1. **Should the colorant table be forced for all multichannel profiles, or should it remain env-var gated?** (Plan assumes: forced for `devchan >= 5`, env-var for ≤ 4.) 2. **Black generation for multichannel:** For CMYK, the K locus is computed via `rev_locus()`. For multichannel, K is still present but additional inks (O, G, V) also need separation rules. The optimizer approach naturally handles this — but should a specific GCR policy be enforced? (Plan assumes: optimizer minimizes ΔE + TAC penalty + smoothness, with K generation following existing ink rules.) --- ## Proposed Changes ### Phase 1: Input Pipeline & Profile Header --- #### [MODIFY] [profout.c](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/profile/profout.c) **1a. Extend `COLOR_REP` parsing (after line 1061, before the `else` error):** Add recognition of multichannel COLOR_REP strings. Instead of hardcoding every possible combination, parse the generic format `CLR_` and the named format `_`: ```c /* --- BEGIN MULTICHANNEL COLOR_REP SUPPORT --- */ } else { /* Try generic multichannel parsing: "CMYKOGV_LAB", "6CLR_XYZ", etc. */ char devpart[64], pcspart[8]; int parsed_nchan = 0; /* Split on '_' into device part and PCS part */ if (sscanf(icg->t[0].kdata[ti], "%63[^_]_%7s", devpart, pcspart) == 2) { /* Determine PCS */ if (strcmp(pcspart, "LAB") == 0) isLab = 1; else if (strcmp(pcspart, "XYZ") == 0) isLab = 0; else goto unknown_rep; /* Try named ink string first (e.g. "CMYKOGV") */ imask = icx_char2inkmask(devpart); if (imask != 0) { parsed_nchan = icx_noofinks(imask); } else { /* Try generic NcCLR format (e.g. "5CLR", "6CLR", "7CLR") */ int nclr = 0; if (sscanf(devpart, "%dCLR", &nclr) == 1 && nclr >= 5 && nclr <= 15) { parsed_nchan = nclr; /* Can't determine exact ink mask from NcCLR alone; will need to use icx_icc_cv_to_colorant_comb() later or require COLORANT_ORDER keyword */ } else { goto unknown_rep; } } if (parsed_nchan >= 5 && parsed_nchan <= 15) { devchan = parsed_nchan; isAdditive = 0; /* Multichannel inks are subtractive */ /* Map to ICC color space signature */ if (imask != 0) { devspace = icx_colorant_comb_to_icc(imask); } if (devspace == 0 || imask == 0) { /* Fallback: use generic NcCLR signatures */ switch (devchan) { case 5: devspace = icSig5colorData; break; case 6: devspace = icSig6colorData; break; case 7: devspace = icSig7colorData; break; case 8: devspace = icSig8colorData; break; default: error("Unsupported channel count %d", devchan); } } } else { goto unknown_rep; } } else { unknown_rep: error("Output device input file has unhandled color representation '%s'", icg->t[0].kdata[ti]); } } /* --- END MULTICHANNEL COLOR_REP SUPPORT --- */ ``` **1b. Adaptive AtoB grid resolution for `devchan >= 5` (insert before the `devchan >= 4` block at line 1069):** ```c if (devchan >= 7) { switch (iquality) { case 3: a2binres = 512; a2bres = 5; a2boutres = 512; break; case 2: a2binres = 512; a2bres = 4; a2boutres = 512; break; case 1: a2binres = 256; a2bres = 4; a2boutres = 256; break; default: a2binres = 256; a2bres = 3; a2boutres = 256; break; } } else if (devchan >= 5) { switch (iquality) { case 3: a2binres = 1024; a2bres = 9; a2boutres = 1024; break; case 2: a2binres = 1024; a2bres = 7; a2boutres = 1024; break; case 1: a2binres = 512; a2bres = 5; a2boutres = 512; break; default: a2binres = 256; a2bres = 4; a2boutres = 256; break; } } else if (devchan >= 4) { /* Existing CMYK logic unchanged */ ``` Grid node counts at these resolutions: | Channels | Quality High | Quality Med | Quality Low | |----------|-------------|-------------|-------------| | 5 | 7⁵ = 16,807 | 5⁵ = 3,125 | 4⁵ = 1,024 | | 6 | 7⁶ = 117,649 | 5⁶ = 15,625 | 4⁶ = 4,096 | | 7 | 5⁷ = 78,125 | 4⁷ = 16,384 | 3⁷ = 2,187 | **1c. B2A grid resolution for `devchan >= 5` (insert similar block around line 1125):** Same structure — limit `b2ares` to small values (3–5 for 7ch, 5–9 for 5ch) to keep BtoA CLUT manageable. **1d. Force Colorant Table for multichannel (around line 1377):** Change the env-var gate: ```c if (devchan >= 5 || getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) ``` **1e. Fix debug/verification output (around line 3323):** Replace: ```c if (devspace == icSigCmykData) printf("... %f %f %f %f\n", ...); else printf("... %f %f %f\n", ...); ``` With a generic loop using `devchan`: ```c printf(" [%f", check[0]); for (j = 1; j < devchan; j++) printf(" %f", check[j]); printf("]"); ``` --- #### [MODIFY] [colprof.c](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/profile/colprof.c) **2a. Expand TAC validation (line 1147 and line 1176):** Change `<= 400.0` upper bound to be dynamic: ```c /* Determine max possible TAC based on device channels */ int max_tac = 400; /* Default for CMYK */ { /* Parse COLOR_REP to get channel count for TAC limit */ int ti2; if ((ti2 = icg->find_kword(icg, 0, "COLOR_REP")) >= 0) { char devpart[64]; if (sscanf(icg->t[0].kdata[ti2], "%63[^_]_", devpart) == 1) { inkmask tmask = icx_char2inkmask(devpart); if (tmask != 0) max_tac = icx_noofinks(tmask) * 100; else { int nclr = 0; if (sscanf(devpart, "%dCLR", &nclr) == 1 && nclr >= 1) max_tac = nclr * 100; } } } } if (filimit > 0.0 && filimit <= (double)max_tac) { /* was: <= 400.0 */ ``` And similarly update the `-l` usage string (line 125): ```c fprintf(stderr," -l tlimit override total ink limit, 0 - N*100%% (default from .ti3)\n"); ``` --- ### Phase 2: Forward Model & AtoB CLUT Generation No code changes needed beyond Phase 1. The RSPL forward spline already supports `MXDI=10`, so `new_rspl(devchan, 3)` works out of the box. The `set_icxLuLut()` call in `profout.c` (line 2108) passes `clutres[i] = a2bres` for all device channels, which will work with our new adaptive resolution. --- ### Phase 3: BtoA Inversion via Constrained Optimization --- #### [NEW] [mchan_inv.h](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/xicc/mchan_inv.h) Header for the multichannel inversion module: ```c #ifndef MCHAN_INV_H #define MCHAN_INV_H #include "xicc.h" /* Multichannel BtoA inversion for devchan > 4. * Uses constrained optimization (powell) to find device values * that minimize deltaE to target PCS while respecting ink limits. * * p - The icxLuLut object with forward AtoB table * out - Returned device values [devchan] * in - Target PCS values [3] (Lab or XYZ in PCS' space) * clipd - If not NULL, return clip distance (0 = no clip) * * Returns 0 on success, 1 if clipped */ int icxLuLut_mchan_inv_clut( icxLuLut *p, double *out, double *clipd, double *in ); #endif /* MCHAN_INV_H */ ``` --- #### [NEW] [mchan_inv.c](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/xicc/mchan_inv.c) The optimizer-based multichannel inverter. Core logic: ```c #include "numlib.h" #include "xicc.h" #include "mchan_inv.h" /* Context for optimization objective function */ typedef struct { icxLuLut *p; /* Forward LUT object */ double target[3]; /* Target PCS values */ double tlimit; /* Total ink limit (fraction), < 0 = no limit */ double klimit; /* Black ink limit (fraction), < 0 = no limit */ int di; /* Device channel count */ int isLab; /* 1 if PCS is Lab */ } mchan_opt_cx; /* Objective function for powell optimizer */ static double mchan_objective(void *fdata, double *dv) { mchan_opt_cx *cx = (mchan_opt_cx *)fdata; double fwd_pcs[3]; /* Forward-computed PCS from device values */ double de; /* Color difference */ double penalty = 0.0; double sum = 0.0; int i; /* Clamp device values to [0, 1] with soft penalty */ for (i = 0; i < cx->di; i++) { if (dv[i] < 0.0) { penalty += dv[i] * dv[i] * 10000.0; dv[i] = 0.0; } else if (dv[i] > 1.0) { penalty += (dv[i] - 1.0) * (dv[i] - 1.0) * 10000.0; dv[i] = 1.0; } sum += dv[i]; } /* Total ink limit penalty */ if (cx->tlimit >= 0.0 && sum > cx->tlimit) { double excess = sum - cx->tlimit; penalty += excess * excess * 5000.0; } /* Black ink limit penalty */ if (cx->klimit >= 0.0 && dv[3] > cx->klimit) { double excess = dv[3] - cx->klimit; penalty += excess * excess * 5000.0; } /* Evaluate forward model: device -> PCS */ cx->p->clut(cx->p, fwd_pcs, dv); /* Compute deltaE (CIE76 in Lab, or Euclidean in XYZ) */ de = 0.0; for (i = 0; i < 3; i++) { double d = fwd_pcs[i] - cx->target[i]; de += d * d; } /* GCR-like regularization: prefer using fewer extra inks */ /* Lightly penalize extra inks (channels 4+) to prefer CMYK when possible */ for (i = 4; i < cx->di; i++) { penalty += dv[i] * dv[i] * 0.01; /* Very mild preference */ } return de + penalty; } int icxLuLut_mchan_inv_clut( icxLuLut *p, double *out, double *clipd, double *in ) { mchan_opt_cx cx; double ss[MAX_CHAN]; /* Initial step sizes */ double rv; int di, i; di = p->clutTable->di; cx.p = p; cx.di = di; cx.target[0] = in[0]; cx.target[1] = in[1]; cx.target[2] = in[2]; cx.tlimit = p->ink.tlimit; cx.klimit = p->ink.klimit; /* Initialize device values: start at midpoint, K = 0 */ for (i = 0; i < di; i++) { out[i] = 0.2; /* Low starting point */ ss[i] = 0.1; /* Step size */ } if (di > 3) out[3] = 0.0; /* Start K at 0 */ /* Run Powell optimizer */ rv = powell(NULL, di, out, ss, 0.0001, /* Function tolerance */ 500, /* Max iterations */ mchan_objective, (void *)&cx, NULL, NULL); /* Clamp final values */ for (i = 0; i < di; i++) { if (out[i] < 0.0) out[i] = 0.0; if (out[i] > 1.0) out[i] = 1.0; } if (clipd != NULL) { /* Compute actual deltaE of result */ double fwd[3]; p->clut(p, fwd, out); double de = 0.0; for (i = 0; i < 3; i++) { double d = fwd[i] - in[i]; de += d * d; } *clipd = sqrt(de); } return (rv > 1.0) ? 1 : 0; /* Return clip flag if large error */ } ``` --- #### [MODIFY] [xlut.c](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/xicc/xlut.c) **3a. Add dispatch in `icxLuLut_inv_clut_aux()` (around line 995):** At the top of the function, before the existing `rev_locus` / `rev_interp` calls, add a multichannel check: ```c /* For multichannel (> 4 device channels), use optimization-based inversion * since rspl rev.c is capped at MXRI=4 */ if (p->clutTable->di > 4) { int rv = icxLuLut_mchan_inv_clut(p, out, clipd, in); if (auxv != NULL) { int ee = 0; for (e = 0; e < p->clutTable->di; e++) { if (p->auxm[e] != 0) auxv[ee++] = out[e]; } } return rv; } ``` **3b. Include the new header:** ```c #include "mchan_inv.h" ``` --- #### [MODIFY] [Jamfile (xicc/)](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/xicc/Jamfile) Add `mchan_inv.c` to the xicc library build sources. --- ### Auxiliary Changes --- #### [MODIFY] [xcolorants.c](file:///Users/diaco/python/repos/git.faryad.top/rhino/LithiumCMS/Argyll_V3.5.0/xicc/xcolorants.c) *(if needed)* The existing `icx_char2inkmask()`, `icx_colorant_comb_to_icc()`, and `icx_noofinks()` already handle 5-7 color combinations. **No changes expected**, but verify that `icx_icc_cv_to_colorant_comb()` works when the ink mask is unknown from a generic `NcCLR` signature. --- ## Verification Plan ### Automated Tests **1. Compilation test:** ```bash cd Argyll_V3.5.0 jam -q ``` *Expected: clean build with no errors.* **2. Grep for remaining hardcoded 4-channel assumptions:** ```bash grep -rn "devchan == 4\|nchan == 4\|di == 4" profile/profout.c xicc/xlut.c profile/colprof.c ``` *Expected: only in appropriate context (CMYK-specific paths), not blocking multichannel.* **3. Create a synthetic 7-channel .ti3 test file and attempt profile generation:** ```bash # Generate a minimal CMYKOGV test chart bin/targen -v -d7 -f500 -g32 test_7clr # Create profile bin/colprof -v -qm -D "Test CMYKOGV Profile" test_7clr ``` **4. Validate the generated ICC profile:** ```bash # Check profile structure bin/iccdump test_7clr.icc # Verify device space is 7CLR, colorant table exists, AtoB0 and BtoA0 present ``` **5. Round-trip accuracy test:** ```bash bin/xicclu -v -ir test_7clr.icc # Input known device values → verify forward Lab → backward → ΔE < 2.0 ``` ### Manual Verification 1. Review `iccdump` output to confirm: - Header `colorSpace` = `7CLR` (or `Mch7`) - `clrT` tag present with 7 named colorants - `A2B0`, `B2A0` tags present with correct dimensions 2. Open profile in a third-party ICC viewer (e.g., ICC Profile Inspector) to validate structure 3. Test with 5-channel and 6-channel .ti3 files as well (e.g., `CMYKOG_LAB`, `CMYKcm_LAB`) --- ## Verification Results Summary ### 1. Profile Generation & Model Fit (`colprof -v -qm`) | Profile | Channel Count | Space Signature | Colorants | Profile Check (RMS / Peak) | Size | |---|---|---|---|---|---| | `test_5clr.icc` | 5 Channels | `icSig5colorData` (`5CLR`) | Cyan, Magenta, Yellow, Black, Orange | 0.65 / 1.73 | 121 KB | | `test_6clr.icc` | 6 Channels | `icSig6colorData` (`6CLR`) | Cyan, Magenta, Yellow, Black, Orange, Green | 0.14 / 1.16 | 215 KB | | `test_7clr.icc` | 7 Channels | `icSig7colorData` (`7CLR`) | Cyan, Magenta, Yellow, Black, Orange, Green, Violet | 0.48 / 3.91 | 244 KB | ### 2. Round-Trip Accuracy (`verify_roundtrip.py`) Evaluating Forward Device $\rightarrow$ Forward PCS (Lab) $\rightarrow$ Backward Inversion $\rightarrow$ Round-Trip PCS (Lab) $\rightarrow \Delta E$: | Metric | 5-Channel (`CMYKO`) | 6-Channel (`CMYKOG`) | 7-Channel (`CMYKOGV`) | |---|---|---|---| | **Average $\Delta E$** | **2.38** | **1.90** | **2.15** | | **Max $\Delta E$** | 6.27 (Magenta 50%) | 5.40 (Magenta 50%) | 5.54 (Violet 50%) | | White | 0.00 | 0.00 | 0.00 | | Black 50% | 0.14 | 0.79 | 0.46 | | Mixed N-channel | 0.32 | 0.25 | 0.53 | | Mixed CMY | 0.70 | 0.70 | 1.02 |