- 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
275 lines
8.4 KiB
C
275 lines
8.4 KiB
C
/*
|
|
* Multichannel (>4 channel) BtoA inversion via constrained optimization.
|
|
*
|
|
* Uses Powell's method from numlib to invert the forward (AtoB) CLUT for
|
|
* device channel counts exceeding 4, where the rspl rev.c simplicial
|
|
* inverter cannot operate (hard-capped at MXRI=4).
|
|
*
|
|
* Author: LithiumCMS project
|
|
* Date: 2026/11/11
|
|
*
|
|
* Copyright 2026 LithiumCMS contributors
|
|
* Licensed under the GNU GENERAL PUBLIC LICENSE Version 2 or later.
|
|
*/
|
|
|
|
#include "aconfig.h"
|
|
#include <math.h>
|
|
#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 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 */
|
|
} mchan_opt_cx;
|
|
|
|
/* Objective function for powell optimizer.
|
|
* Evaluates the forward model and returns a cost combining:
|
|
* - 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
|
|
* - Mild GCR-like regularization preferring CMYK over extra inks
|
|
*/
|
|
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 (true deltaE^2) */
|
|
double penalty = 0.0;
|
|
double sum = 0.0;
|
|
int i;
|
|
|
|
/* 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] * 50000.0;
|
|
clamped[i] = 0.0;
|
|
} else if (dv[i] > 1.0) {
|
|
penalty += (dv[i] - 1.0) * (dv[i] - 1.0) * 50000.0;
|
|
clamped[i] = 1.0;
|
|
} else {
|
|
clamped[i] = dv[i];
|
|
}
|
|
sum += clamped[i];
|
|
}
|
|
|
|
/* Total ink limit penalty */
|
|
if (cx->tlimit >= 0.0 && sum > cx->tlimit) {
|
|
double excess = sum - cx->tlimit;
|
|
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 * 20000.0;
|
|
}
|
|
|
|
/* Evaluate forward model: device -> PCS */
|
|
cx->p->clut(cx->p, fwd_pcs, clamped);
|
|
|
|
/* 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+).
|
|
* 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.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
|
|
* (MXRI=4 hard limit). Instead, we minimize the forward-model deltaE
|
|
* subject to ink limit constraints.
|
|
*
|
|
* Returns 0 on success, 1 if clipping occurred (large residual).
|
|
*/
|
|
int icxLuLut_mchan_inv_clut(
|
|
icxLuLut *p,
|
|
double *out,
|
|
double *clipd,
|
|
double *in
|
|
) {
|
|
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, k;
|
|
double L_norm, a_norm, b_norm, darkness;
|
|
double sat;
|
|
|
|
di = p->clutTable->di;
|
|
|
|
/* Sanity check */
|
|
if (di <= 4) {
|
|
/* Shouldn't be called for <= 4 channels; use rspl rev instead */
|
|
return 1;
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
/* 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++) {
|
|
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 the result */
|
|
double fwd[MXDO];
|
|
p->clut(p, fwd, out);
|
|
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);
|
|
}
|
|
}
|
|
|
|
return (clipd != NULL && *clipd > 4.0) ? 1 : 0;
|
|
}
|