Files
LithiumCMS/Argyll_V3.5.0/xicc/mchan_inv.c
Diaco Soltanpour 51d0d04821 feat: implement multichannel (5-7 color) ICC profiling support
- Phase 1 (profout.c, colprof.c):
  * Extend COLOR_REP parsing for named (e.g. CMYKOGV_LAB) and generic (NcCLR) spaces (5-15ch)
  * Set adaptive AtoB and BtoA grid resolutions for 5-6ch and 7+ch to avoid memory explosions
  * Automatically enable ICC v2/v4 Colorant Table tag for devchan >= 5
  * Generalize verification print loops to support N channels
  * Support dynamic TAC limits up to N*100% in colprof.c

- Phase 2 & 3 (xicc/):
  * Add mchan_inv.h and mchan_inv.c implementing constrained Powell optimization for BtoA inversion
  * Dispatch devchan > 4 in xlut.c (icxLuLut_inv_clut_aux) to bypass rspl rev.c MXRI=4 dimension limit
  * Register mchan_inv.c in xicc/Jamfile and include header in xicc.c
2026-11-11 21:46:26 +03:30

163 lines
4.5 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 */
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 (or 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 */
double penalty = 0.0;
double sum = 0.0;
int i;
/* Clamp device values to [0, 1] with soft penalty for out-of-range */
for (i = 0; i < cx->di; i++) {
if (dv[i] < 0.0) {
penalty += dv[i] * dv[i] * 10000.0;
clamped[i] = 0.0;
} else if (dv[i] > 1.0) {
penalty += (dv[i] - 1.0) * (dv[i] - 1.0) * 10000.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 * 5000.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;
}
/* 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;
}
/* GCR-like regularization: mildly prefer using fewer extra inks (channels 4+) */
/* This keeps solutions closer to CMYK when colorimetrically equivalent */
for (i = 4; i < cx->di; i++) {
penalty += clamped[i] * clamped[i] * 0.01;
}
return de + penalty;
}
/* 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 rv;
int di, i;
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;
/* 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 */
/* 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 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];
double de = 0.0;
p->clut(p, fwd, out);
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 residual error */
}