From 51d0d0482114c2d7d636314415f3aca09e2b31c2 Mon Sep 17 00:00:00 2001 From: Diaco Soltanpour Date: Wed, 11 Nov 2026 21:46:26 +0330 Subject: [PATCH] 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 --- Argyll_V3.5.0/profile/colprof.c | 27 ++++- Argyll_V3.5.0/profile/profout.c | 183 ++++++++++++++++++++++++++++---- Argyll_V3.5.0/xicc/Jamfile | 2 +- Argyll_V3.5.0/xicc/mchan_inv.c | 162 ++++++++++++++++++++++++++++ Argyll_V3.5.0/xicc/mchan_inv.h | 38 +++++++ Argyll_V3.5.0/xicc/xicc.c | 1 + Argyll_V3.5.0/xicc/xlut.c | 14 +++ 7 files changed, 401 insertions(+), 26 deletions(-) create mode 100644 Argyll_V3.5.0/xicc/mchan_inv.c create mode 100644 Argyll_V3.5.0/xicc/mchan_inv.h diff --git a/Argyll_V3.5.0/profile/colprof.c b/Argyll_V3.5.0/profile/colprof.c index 27c154c..0ea8b3c 100644 --- a/Argyll_V3.5.0/profile/colprof.c +++ b/Argyll_V3.5.0/profile/colprof.c @@ -122,7 +122,7 @@ void usage(char *diag, ...) { fprintf(stderr," enle: K level at Black 0.0 - 1.0\n"); fprintf(stderr," shape: 1.0 = straight, 0.0-1.0 concave, 1.0-2.0 convex\n"); fprintf(stderr," -K parameters Same as -k, but target is K locus rather than K value itself\n"); - fprintf(stderr," -l tlimit override total ink limit, 0 - 400%% (default from .ti3)\n"); + fprintf(stderr," -l tlimit override total ink limit, 0 - N*100%% (default from .ti3)\n"); fprintf(stderr," -L klimit override black ink limit, 0 - 100%% (default from .ti3)\n"); fprintf(stderr," -a lxXYgsmGS Algorithm type override\n"); fprintf(stderr," l = Lab cLUT (def.), x = XYZ cLUT,\n"); @@ -1117,6 +1117,7 @@ int main(int argc, char *argv[]) { int hascal = 0; /* nz if there is a .cal in the .ti3 */ double filimit = - 1.0; /* file ink limit */ icxInk ink; /* Ink parameters */ + double max_tac = 400.0; /* Max total ink limit based on channels */ /* Check if there is a .cal in the .ti3 or one provided in command line */ { @@ -1143,9 +1144,27 @@ int main(int argc, char *argv[]) { filimit = atoi(icg->t[0].kdata[ti]); } + /* Determine max possible TAC based on device channels */ + { + 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.0; + } else { + int nclr = 0; + if (sscanf(devpart, "%dCLR", &nclr) == 1 && nclr >= 1) + max_tac = nclr * 100.0; + } + } + } + } + /* Check against user set limit */ - if (filimit > 0.0 && filimit <= 400.0) { /* File has limit */ - if (tlimit > 0.0 && tlimit <= 400.0) { /* User has specified limit as option */ + if (filimit > 0.0 && filimit <= max_tac) { /* File has limit */ + if (tlimit > 0.0 && tlimit <= max_tac) { /* User has specified limit as option */ if (filimit < tlimit) { warning("Ink limit is greater than original chart! (%.0f%% > %.0f%%)",tlimit,filimit); } @@ -1173,7 +1192,7 @@ int main(int argc, char *argv[]) { } } - if (tlimit >= 0 && tlimit < 400.0) { + if (tlimit >= 0 && tlimit < max_tac) { if (verb) { if (hascal) printf("Final total ink limit being used is %.0f%%\n",tlimit); diff --git a/Argyll_V3.5.0/profile/profout.c b/Argyll_V3.5.0/profile/profout.c index 0d2e709..f4563a6 100644 --- a/Argyll_V3.5.0/profile/profout.c +++ b/Argyll_V3.5.0/profile/profout.c @@ -1060,13 +1060,116 @@ make_output_icc( isLab = 1; isAdditive = 1; #endif /* IMP_MONO */ - } else - error("Output device input file has unhandled color representation '%s'", - icg->t[0].kdata[ti]); + /* --- 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; + case 9: devspace = icSig9colorData; break; + case 10: devspace = icSig10colorData; break; + case 11: devspace = icSig11colorData; break; + case 12: devspace = icSig12colorData; break; + case 13: devspace = icSig13colorData; break; + case 14: devspace = icSig14colorData; break; + case 15: devspace = icSig15colorData; 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 --- */ /* Figure out some suitable table sizes */ /* A2B table: */ - if (devchan >= 4) { /* devchan == 4 or greater */ + if (devchan >= 7) { /* 7+ channels: very small CLUT to control memory */ + if (iquality >= 3) { + a2binres = 512; + a2bres = 5; + a2boutres = 512; + } else if (iquality == 2) { + a2binres = 512; + a2bres = 4; + a2boutres = 512; + } else if (iquality == 1) { + a2binres = 256; + a2bres = 4; + a2boutres = 256; + } else { + a2binres = 256; + a2bres = 3; + a2boutres = 256; + } + } else if (devchan >= 5) { /* 5-6 channels: moderate CLUT */ + if (iquality >= 3) { + a2binres = 1024; + a2bres = 9; + a2boutres = 1024; + } else if (iquality == 2) { + a2binres = 1024; + a2bres = 7; + a2boutres = 1024; + } else if (iquality == 1) { + a2binres = 512; + a2bres = 5; + a2boutres = 512; + } else { + a2binres = 256; + a2bres = 4; + a2boutres = 256; + } + } else if (devchan >= 4) { /* devchan == 4 (CMYK) */ if (iquality >= 3) { a2binres = 2048; a2bres = 23; @@ -1123,7 +1226,51 @@ make_output_icc( } /* B2A table: */ - if (devchan >= 2) { + if (devchan >= 7) { /* 7+ channels: small BtoA CLUT */ + if (oquality >= 3) { /* Ultra High */ + b2ainres = 512; + b2ares = 5; + b2aoutres = 512; + } else if (oquality == 2) { + b2ainres = 512; + b2ares = 4; /* High */ + b2aoutres = 512; + } else if (oquality == 1) { + b2ainres = 256; + b2ares = 4; /* Medium */ + b2aoutres = 256; + } else if (oquality >= 0) { + b2ainres = 256; + b2ares = 3; /* Low */ + b2aoutres = 256; + } else { /* Special, Extremely low quality */ + b2ainres = 64; + b2ares = 3; + b2aoutres = 64; + } + } else if (devchan >= 5) { /* 5-6 channels: moderate BtoA CLUT */ + if (oquality >= 3) { /* Ultra High */ + b2ainres = 1024; + b2ares = 9; + b2aoutres = 1024; + } else if (oquality == 2) { + b2ainres = 1024; + b2ares = 7; /* High */ + b2aoutres = 1024; + } else if (oquality == 1) { + b2ainres = 512; + b2ares = 5; /* Medium */ + b2aoutres = 512; + } else if (oquality >= 0) { + b2ainres = 256; + b2ares = 4; /* Low */ + b2aoutres = 256; + } else { /* Special, Extremely low quality */ + b2ainres = 64; + b2ares = 3; + b2aoutres = 64; + } + } else if (devchan >= 2) { if (oquality >= 3) { /* Ultra High */ b2ainres = 2048; b2ares = 45; @@ -1376,7 +1523,7 @@ make_output_icc( /* Colorant Table Tag: */ /* This is really ICC V4, but icclibv2 had support for it... */ - if (getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) + if (devchan >= 5 || getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) { unsigned int i; icmColorantTable *wo; @@ -3045,7 +3192,7 @@ make_output_icc( /* Set the ColorantTable PCS values */ /* This is really ICC V4, but icclibv2 had support for it... */ - if (getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) + if (devchan >= 5 || getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) { unsigned int i; icmColorantTable *wo; @@ -3224,7 +3371,7 @@ make_output_icc( /* Set the ColorantTable PCS values */ /* This is really ICC V4, but icclibv2 had suport for it... */ - if (getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL + if ((devchan >= 5 || getenv("ARGYLL_CREATE_V2COLORANT_TABLE") != NULL) && !isLut) { unsigned int i; @@ -3320,19 +3467,13 @@ make_output_icc( } if (verify && verb) { - if (devspace == icSigCmykData) { - printf("[%f] %f %f %f %f -> %f %f %f should be %f %f %f\n", - icmLabDE(ref, out), - tpat[i].p[0],tpat[i].p[1],tpat[i].p[2],tpat[i].p[3], - out[0],out[1],out[2], - ref[0],ref[1],ref[2]); - } else { - printf("[%f] %f %f %f -> %f %f %f should be %f %f %f\n", - icmLabDE(ref, out), - tpat[i].p[0],tpat[i].p[1],tpat[i].p[2], - out[0],out[1],out[2], - ref[0],ref[1],ref[2]); - } + int j; + printf("[%f]", icmLabDE(ref, out)); + for (j = 0; j < devchan; j++) + printf(" %f", tpat[i].p[j]); + printf(" -> %f %f %f should be %f %f %f\n", + out[0],out[1],out[2], + ref[0],ref[1],ref[2]); } /* Check the result */ diff --git a/Argyll_V3.5.0/xicc/Jamfile b/Argyll_V3.5.0/xicc/Jamfile index 9f7536a..f83cf7c 100755 --- a/Argyll_V3.5.0/xicc/Jamfile +++ b/Argyll_V3.5.0/xicc/Jamfile @@ -28,7 +28,7 @@ HDRS = ../h ../icc ../rspl ../cgats ../numlib ../gamut ../spectro ../profile # XICC library Library libxicc : xicc.c xspect.c xcolorants.c xutils.c iccjpeg.c xcam.c cam97s3.c cam02.c mpp.c ccmx.c ccss.c xfit.c moncurve.c xcal.c - bt1886.c tm3015.c ; + bt1886.c tm3015.c mchan_inv.c ; # colorant library. Use instead of libxicc Object xcolorants2 : xcolorants.c ; diff --git a/Argyll_V3.5.0/xicc/mchan_inv.c b/Argyll_V3.5.0/xicc/mchan_inv.c new file mode 100644 index 0000000..c5d8fe9 --- /dev/null +++ b/Argyll_V3.5.0/xicc/mchan_inv.c @@ -0,0 +1,162 @@ +/* + * 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 +#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 */ +} diff --git a/Argyll_V3.5.0/xicc/mchan_inv.h b/Argyll_V3.5.0/xicc/mchan_inv.h new file mode 100644 index 0000000..906de1e --- /dev/null +++ b/Argyll_V3.5.0/xicc/mchan_inv.h @@ -0,0 +1,38 @@ +#ifndef MCHAN_INV_H +#define MCHAN_INV_H + +/* + * Multichannel (>4 channel) BtoA inversion via constrained optimization. + * + * The rspl reverse lookup (rev.c) is hard-capped at MXRI=4 input dimensions. + * For device channels > 4, we use Powell's method to find device values that + * minimize deltaE to the target PCS while respecting ink limits. + * + * Author: LithiumCMS project + * Date: 2026/11/11 + * + * Copyright 2026 LithiumCMS contributors + * Licensed under the GNU GENERAL PUBLIC LICENSE Version 2 or later. + */ + +#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] + * clipd - If not NULL, return clip distance (0 = no clip) + * in - Target PCS values [3] (Lab or XYZ in PCS' space) + * + * Returns 0 on success, 1 if clipping occurred + */ +int icxLuLut_mchan_inv_clut( + icxLuLut *p, + double *out, + double *clipd, + double *in +); + +#endif /* MCHAN_INV_H */ diff --git a/Argyll_V3.5.0/xicc/xicc.c b/Argyll_V3.5.0/xicc/xicc.c index 9056571..c718484 100644 --- a/Argyll_V3.5.0/xicc/xicc.c +++ b/Argyll_V3.5.0/xicc/xicc.c @@ -78,6 +78,7 @@ static void icxLuEfv_wh_bk_points(icxLuBase *p, double *wht, double *blk, double /* and are included to keep their functions private. (static) */ #include "xmono.c" #include "xmatrix.c" +#include "mchan_inv.h" #include "xlut.c" /* New xfit3 in & out optimising based profiles */ //#include "xlut1.c" /* Old xfit1 device curve based profiles */ diff --git a/Argyll_V3.5.0/xicc/xlut.c b/Argyll_V3.5.0/xicc/xlut.c index f44e3d4..3c25d6d 100644 --- a/Argyll_V3.5.0/xicc/xlut.c +++ b/Argyll_V3.5.0/xicc/xlut.c @@ -992,6 +992,20 @@ double *in /* Function input values to invert (== clut output' values) */ /* Compute clip vector, if any */ cdir = icxClipVector(&p->clip, in, cdirv, 0); + /* 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; + } + if (p->clutTable->di > fdi) { /* ie. CMYK->Lab, there will be ambiguity */ double min[MXDI], max[MXDI]; /* Auxiliary locus range */