feat: complete multichannel (5-7ch) ICC profile generation & verification

- Profile Creation & Inversion (profout.c, xlut.c, mchan_inv.c):
  * Unify BtoA CLUT resolutions for devchan >= 5 (9/17/25/33) to provide dense PCS inversion grids
  * Guard all reverse simplicial ops in xlut.c with within_restrictedsize() to prevent SIGSEGV when di > 4 (MXRI cap)
  * Enhance Powell optimizer in mchan_inv.c with multi-candidate starting strategy and scaled deltaE^2 Lab coordinates
  * Allow icSigColorantTableTag in V2 profiles for devchan >= 5 in icc.c

- Numerical & Geometry Stability (scat.c, gamut.c):
  * Fix SIGSEGV in rspl scat.c sparse matrix solver caused by assumption of sorted xcol in 4x unrolled loops
  * Add null pointer and tag checks in gamut.c radial_point_triang

- Test Targets & Verification (test_multichannel/):
  * Add synthetic .ti1 and .ti3 targets for 5-channel (CMYKO), 6-channel (CMYKOG), and 7-channel (CMYKOGV) workflows
  * Add ti1_to_ti3 conversion script and verify_roundtrip.py round-trip verification utility
  * Verified end-to-end:
    - 5-channel: avg dE = 2.38, max dE = 6.27
    - 6-channel: avg dE = 1.90, max dE = 5.40
    - 7-channel: avg dE = 2.15, max dE = 5.54

- Build & Housekeeping:
  * Add .gitignore for compiled executables, autotools outputs, and generated ICC profiles
  * Update MULTICHANNEL_IMPLEMENTATION_PLAN.md with verification results
This commit is contained in:
2026-11-18 16:49:43 +03:30
parent d909b331c2
commit eaab9e3dd3
17 changed files with 3336 additions and 247 deletions

View File

@@ -21,7 +21,7 @@
/* Context for optimization objective function */
typedef struct {
icxLuLut *p; /* Forward LUT object */
double target[3]; /* Target PCS values */
double target[3]; /* Target PCS values in normalized [0, 1] range */
double tlimit; /* Total ink limit (fraction), < 0 = no limit */
double klimit; /* Black ink limit (fraction), < 0 = no limit */
int di; /* Device channel count */
@@ -29,7 +29,7 @@ typedef struct {
/* Objective function for powell optimizer.
* Evaluates the forward model and returns a cost combining:
* - CIE76 deltaE^2 (or Euclidean in XYZ)
* - CIE76 deltaE^2 in Lab (or scaled Euclidean in XYZ)
* - Soft boundary penalties for [0,1] clamping
* - Total ink limit penalty
* - Black ink limit penalty
@@ -39,18 +39,18 @@ static double mchan_objective(void *fdata, double *dv) {
mchan_opt_cx *cx = (mchan_opt_cx *)fdata;
double fwd_pcs[MXDO]; /* Forward-computed PCS from device values */
double clamped[MXDI]; /* Clamped copy of device values for forward eval */
double de; /* Color difference squared */
double de; /* Color difference squared (true deltaE^2) */
double penalty = 0.0;
double sum = 0.0;
int i;
/* Clamp device values to [0, 1] with soft penalty for out-of-range */
/* Clamp device values to [0, 1] with soft quadratic penalty for out-of-range */
for (i = 0; i < cx->di; i++) {
if (dv[i] < 0.0) {
penalty += dv[i] * dv[i] * 10000.0;
penalty += dv[i] * dv[i] * 50000.0;
clamped[i] = 0.0;
} else if (dv[i] > 1.0) {
penalty += (dv[i] - 1.0) * (dv[i] - 1.0) * 10000.0;
penalty += (dv[i] - 1.0) * (dv[i] - 1.0) * 50000.0;
clamped[i] = 1.0;
} else {
clamped[i] = dv[i];
@@ -61,34 +61,63 @@ static double mchan_objective(void *fdata, double *dv) {
/* Total ink limit penalty */
if (cx->tlimit >= 0.0 && sum > cx->tlimit) {
double excess = sum - cx->tlimit;
penalty += excess * excess * 5000.0;
penalty += excess * excess * 20000.0;
}
/* Black ink limit penalty (K is channel index 3 in CMYK+extras) */
if (cx->klimit >= 0.0 && cx->di > 3 && clamped[3] > cx->klimit) {
double excess = clamped[3] - cx->klimit;
penalty += excess * excess * 5000.0;
penalty += excess * excess * 20000.0;
}
/* Evaluate forward model: device -> PCS */
cx->p->clut(cx->p, fwd_pcs, clamped);
/* Compute deltaE^2 (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;
/* Compute true deltaE^2 */
if (cx->p->outs == icSigXYZData) {
double d0 = (fwd_pcs[0] - cx->target[0]) * 100.0;
double d1 = (fwd_pcs[1] - cx->target[1]) * 100.0;
double d2 = (fwd_pcs[2] - cx->target[2]) * 100.0;
de = d0 * d0 + d1 * d1 + d2 * d2;
} else {
/* Standard Lab: L is 0..1 -> 0..100, a and b are 0..1 -> 255 range */
double dL = (fwd_pcs[0] - cx->target[0]) * 100.0;
double da = (fwd_pcs[1] - cx->target[1]) * 255.0;
double db = (fwd_pcs[2] - cx->target[2]) * 255.0;
de = dL * dL + da * da + db * db;
}
/* GCR-like regularization: mildly prefer using fewer extra inks (channels 4+) */
/* This keeps solutions closer to CMYK when colorimetrically equivalent */
/* GCR-like regularization: mildly prefer using fewer extra inks (channels 4+).
* Scaled so that max penalty for an extra ink is ~0.05 (sub-0.25 dE),
* breaking ties without sacrificing color fidelity. */
for (i = 4; i < cx->di; i++) {
penalty += clamped[i] * clamped[i] * 0.01;
penalty += clamped[i] * clamped[i] * 0.05;
}
return de + penalty;
}
/* Helper to evaluate a candidate starting point and keep the best */
static void try_candidate(mchan_opt_cx *cx, double *cand, double *best, double *best_cost) {
int i;
double c = mchan_objective(cx, cand);
if (c < *best_cost) {
*best_cost = c;
for (i = 0; i < cx->di; i++)
best[i] = cand[i];
}
}
/* Helper to test a pair of inks */
static void try_pair(mchan_opt_cx *cx, int ch1, double val1, int ch2, double val2,
double *cand, double *best, double *best_cost) {
int i;
for (i = 0; i < cx->di; i++) cand[i] = 0.0;
if (ch1 >= 0 && ch1 < cx->di) cand[ch1] = (val1 > 1.0) ? 1.0 : (val1 < 0.0 ? 0.0 : val1);
if (ch2 >= 0 && ch2 < cx->di) cand[ch2] = (val2 > 1.0) ? 1.0 : (val2 < 0.0 ? 0.0 : val2);
try_candidate(cx, cand, best, best_cost);
}
/* Perform multichannel BtoA inversion using constrained Powell optimization.
*
* For device channels > 4, the rspl rev.c reverse lookup cannot be used
@@ -105,8 +134,13 @@ int icxLuLut_mchan_inv_clut(
) {
mchan_opt_cx cx;
double ss[MXDI]; /* Initial step sizes for Powell */
double cand[MXDI]; /* Candidate starting point */
double best[MXDI]; /* Best starting point found */
double best_cost = 1e30;
double rv;
int di, i;
int di, i, k;
double L_norm, a_norm, b_norm, darkness;
double sat;
di = p->clutTable->di;
@@ -124,21 +158,94 @@ int icxLuLut_mchan_inv_clut(
cx.tlimit = p->ink.tlimit;
cx.klimit = p->ink.klimit;
/* Initialize device values: start at a low uniform value, K = 0.
* Starting low avoids over-inking and gives the optimizer room. */
for (i = 0; i < di; i++) {
out[i] = 0.2;
ss[i] = 0.1; /* Step size */
}
if (di > 3)
out[3] = 0.0; /* Start K at 0 */
L_norm = in[0];
a_norm = in[1];
b_norm = in[2];
darkness = 1.0 - L_norm;
if (darkness < 0.0) darkness = 0.0;
if (darkness > 1.0) darkness = 1.0;
/* Run Powell optimizer */
rv = powell(NULL, di, out, ss,
0.0001, /* Function tolerance */
500, /* Max iterations */
mchan_objective, (void *)&cx,
NULL, NULL);
/* Approximate chroma / saturation */
sat = sqrt((a_norm - 0.502) * (a_norm - 0.502) + (b_norm - 0.502) * (b_norm - 0.502));
/* Candidate 0: All zeros (White / clear) */
for (i = 0; i < di; i++) cand[i] = 0.0;
try_candidate(&cx, cand, best, &best_cost);
/* Candidate 1: Neutral K only */
for (i = 0; i < di; i++) cand[i] = 0.0;
if (di > 3) cand[3] = darkness;
try_candidate(&cx, cand, best, &best_cost);
/* Candidate 2: Uniform low ink */
for (i = 0; i < di; i++) cand[i] = 0.05;
if (di > 3) cand[3] = 0.0;
try_candidate(&cx, cand, best, &best_cost);
/* Candidates 3..N: Pure single ink for each channel */
{
double single_val = darkness * 1.5;
if (single_val > 1.0) single_val = 1.0;
if (single_val < 0.2) single_val = 0.2;
for (k = 0; k < di; k++) {
for (i = 0; i < di; i++) cand[i] = 0.0;
cand[k] = single_val;
try_candidate(&cx, cand, best, &best_cost);
/* Also try at full 1.0 for primaries */
cand[k] = 1.0;
try_candidate(&cx, cand, best, &best_cost);
}
}
/* Pair combinations of dominant inks */
{
double pval = darkness * 1.2;
if (pval > 1.0) pval = 1.0;
if (pval < 0.2) pval = 0.2;
/* CMYK primary pairs */
try_pair(&cx, 0, pval, 1, pval, cand, best, &best_cost); /* C + M */
try_pair(&cx, 0, pval, 2, pval, cand, best, &best_cost); /* C + Y */
try_pair(&cx, 1, pval, 2, pval, cand, best, &best_cost); /* M + Y */
/* Multichannel pairs if available */
if (di >= 6) {
try_pair(&cx, 4, pval, 2, pval, cand, best, &best_cost); /* O + Y */
try_pair(&cx, 4, pval, 1, pval, cand, best, &best_cost); /* O + M */
try_pair(&cx, 5, pval, 0, pval, cand, best, &best_cost); /* G + C */
try_pair(&cx, 5, pval, 2, pval, cand, best, &best_cost); /* G + Y */
}
if (di >= 7) {
try_pair(&cx, 6, pval, 0, pval, cand, best, &best_cost); /* V + C */
try_pair(&cx, 6, pval, 1, pval, cand, best, &best_cost); /* V + M */
}
}
/* Candidate: Dark composite ink */
if (darkness > 0.4) {
for (i = 0; i < di; i++) cand[i] = 0.0;
cand[0] = 0.2;
cand[1] = 0.2;
cand[2] = 0.2;
if (di > 3) cand[3] = darkness;
try_candidate(&cx, cand, best, &best_cost);
}
/* Initialize Powell from the best candidate */
for (i = 0; i < di; i++) {
out[i] = best[i];
ss[i] = 0.05; /* Step size */
}
/* If best candidate is already extremely close (cost < 0.05, approx 0.2 dE),
* we can skip Powell or do a quick polish */
if (best_cost > 0.04) {
rv = powell(NULL, di, out, ss,
0.0005, /* Function tolerance on deltaE^2 */
250, /* Max iterations */
mchan_objective, (void *)&cx,
NULL, NULL);
}
/* Clamp final values to valid device range */
for (i = 0; i < di; i++) {
@@ -149,14 +256,19 @@ int icxLuLut_mchan_inv_clut(
if (clipd != NULL) {
/* Compute actual deltaE of the result */
double fwd[MXDO];
double de = 0.0;
p->clut(p, fwd, out);
for (i = 0; i < 3; i++) {
double d = fwd[i] - in[i];
de += d * d;
if (p->outs == icSigXYZData) {
double d0 = (fwd[0] - in[0]) * 100.0;
double d1 = (fwd[1] - in[1]) * 100.0;
double d2 = (fwd[2] - in[2]) * 100.0;
*clipd = sqrt(d0 * d0 + d1 * d1 + d2 * d2);
} else {
double dL = (fwd[0] - in[0]) * 100.0;
double da = (fwd[1] - in[1]) * 255.0;
double db = (fwd[2] - in[2]) * 255.0;
*clipd = sqrt(dL * dL + da * da + db * db);
}
*clipd = sqrt(de);
}
return (rv > 1.0) ? 1 : 0; /* Return clip flag if large residual error */
return (clipd != NULL && *clipd > 4.0) ? 1 : 0;
}

View File

@@ -1592,7 +1592,8 @@ int icxLuLut_clut_aux_locus(icxLuLut *p, double *locus, double *out, double *in)
int nsoln; /* Number of solutions found */
int e,f;
if (p->clutTable->di > p->clutTable->fdi) { /* ie. CMYK->Lab, there will be ambiguity */
if (p->clutTable->di > p->clutTable->fdi
&& p->clutTable->within_restrictedsize(p->clutTable)) { /* ie. CMYK->Lab, there will be ambiguity */
double min[MXDI], max[MXDI]; /* Auxiliary locus range */
/* Setup for auxiliary locus lookup */
@@ -1914,7 +1915,7 @@ int setLminmax /* Figure the L locus for inking rule */
if (ink) {
p->ink = *ink; /* Copy the structure */
} else {
p->ink.tlimit = 3.0; /* default ink limit of 300% */
p->ink.tlimit = devchan > 4 ? (double)devchan : 3.0; /* default ink limit */
p->ink.klimit = -1.0; /* default no black limit */
p->ink.KonlyLmin = 0; /* default don't use K only black as Locus Lmin */
p->ink.k_rule = icxKluma5; /* default K generation rule */
@@ -1935,18 +1936,20 @@ int setLminmax /* Figure the L locus for inking rule */
/* Set the ink limit information for any reverse interpolation. */
/* Calling this will clear the reverse interpolaton cache. */
p->clutTable->rev_set_limit(
p->clutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
if (p->clutTable->within_restrictedsize(p->clutTable)) {
p->clutTable->rev_set_limit(
p->clutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
}
/* Duplicate in the CAM clip rspl if it exists */
if (p->cclutTable != NULL) {
if (p->cclutTable != NULL && p->cclutTable->within_restrictedsize(p->cclutTable)) {
p->cclutTable->rev_set_limit(
p->cclutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
@@ -2370,7 +2373,8 @@ fprintf(stderr,"~1 Internal optimised 4D separations not yet implemented!\n");
#ifdef USELCHWEIGHT
/* If we are not doing camclip, but our output is an Lab like space, */
/* then apply lchw weighting anyway. */
if (!p->camclip && (p->outs == icSigLabData || p->outs == icxSigJabData)) {
if (!p->camclip && (p->outs == icSigLabData || p->outs == icxSigJabData)
&& p->clutTable->within_restrictedsize(p->clutTable)) {
double lchw[MXRO] = { JCCWEIGHT, CCCWEIGHT, HCCWEIGHT };
/* Set the Nearest Neighbor clipping Weighting */
@@ -2463,7 +2467,8 @@ icxLuLut *p) {
#ifdef USELCHWEIGHT
/* Set the Nearest Neighbor clipping Weighting */
p->cclutTable->rev_set_lchw(p->cclutTable, lchw);
if (p->cclutTable->within_restrictedsize(p->cclutTable))
p->cclutTable->rev_set_lchw(p->cclutTable, lchw);
#endif /* USELCHWEIGHT */
if (p->plu->max_clut_res(p->plu, gres) == 0) { /* Hmm. */
@@ -2484,15 +2489,17 @@ icxLuLut *p) {
p->ninmin, p->ninmax, gres, cmin, cmax);
/* Duplicate the ink limit information for any reverse interpolation. */
p->cclutTable->rev_set_limit(
p->cclutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
if (p->cclutTable->within_restrictedsize(p->cclutTable)) {
p->cclutTable->rev_set_limit(
p->cclutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
}
return 0;
}
@@ -3094,6 +3101,13 @@ int ores /* Num of out-table entries */
break;
}
default:
if (inputChan >= 5) {
for (e = 0; e < inputChan; e++) {
dwhite[e] = 0.0;
dblack[e] = 1.0;
}
break;
}
return icm_err(icco,ICX_ERR_BAD_DEV,
"set_icxLuLut: can't handle color space %s",
icm2str(icmColorSpaceSig, h->colorSpace));
@@ -3278,6 +3292,24 @@ int ores /* Num of out-table entries */
}
default:
if (inputChan >= 5) {
for (i = 0; i < nodpbw; i++) {
int all_zero = 1;
for (e = 0; e < inputChan; e++) {
if (ipoints[i].p[e] >= 0.001) {
all_zero = 0;
break;
}
}
if (all_zero) {
wp[0] += ipoints[i].v[0];
wp[1] += ipoints[i].v[1];
wp[2] += ipoints[i].v[2];
nw++;
}
}
break;
}
return icm_err(icco,ICX_ERR_BAD_DEV,
"set_icxLuLut: can't handle color space %s",
icm2str(icmColorSpaceSig, h->colorSpace));