2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation. Oracle designates this
7
* particular file as subject to the "Classpath" exception as provided
8
* by Oracle in the LICENSE file that accompanied this code.
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
25
// This file is available under and governed by the GNU General Public
26
// License version 2 only, as published by the Free Software Foundation.
27
// However, the following notice accompanied the original version of this
30
//---------------------------------------------------------------------------------
32
// Little Color Management System
33
// Copyright (c) 1998-2023 Marti Maria Saguer
35
// Permission is hereby granted, free of charge, to any person obtaining
36
// a copy of this software and associated documentation files (the "Software"),
37
// to deal in the Software without restriction, including without limitation
38
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
39
// and/or sell copies of the Software, and to permit persons to whom the Software
40
// is furnished to do so, subject to the following conditions:
42
// The above copyright notice and this permission notice shall be included in
43
// all copies or substantial portions of the Software.
45
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
47
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
49
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
50
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
51
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53
//---------------------------------------------------------------------------------
56
#include "lcms2_internal.h"
59
//----------------------------------------------------------------------------------
61
// Optimization for 8 bits, Shaper-CLUT (3 inputs only)
66
const cmsInterpParams* p; // Tetrahedrical interpolation parameters. This is a not-owned pointer.
68
cmsUInt16Number rx[256], ry[256], rz[256];
69
cmsUInt32Number X0[256], Y0[256], Z0[256]; // Precomputed nodes and offsets for 8-bit input data
75
// Generic optimization for 16 bits Shaper-CLUT-Shaper (any inputs)
81
cmsUInt32Number nInputs;
82
cmsUInt32Number nOutputs;
84
_cmsInterpFn16 EvalCurveIn16[MAX_INPUT_DIMENSIONS]; // The maximum number of input channels is known in advance
85
cmsInterpParams* ParamsCurveIn16[MAX_INPUT_DIMENSIONS];
87
_cmsInterpFn16 EvalCLUT; // The evaluator for 3D grid
88
const cmsInterpParams* CLUTparams; // (not-owned pointer)
91
_cmsInterpFn16* EvalCurveOut16; // Points to an array of curve evaluators in 16 bits (not-owned pointer)
92
cmsInterpParams** ParamsCurveOut16; // Points to an array of references to interpolation params (not-owned pointer)
98
// Optimization for matrix-shaper in 8 bits. Numbers are operated in n.14 signed, tables are stored in 1.14 fixed
100
typedef cmsInt32Number cmsS1Fixed14Number; // Note that this may hold more than 16 bits!
102
#define DOUBLE_TO_1FIXED14(x) ((cmsS1Fixed14Number) floor((x) * 16384.0 + 0.5))
106
cmsContext ContextID;
108
cmsS1Fixed14Number Shaper1R[256]; // from 0..255 to 1.14 (0.0...1.0)
109
cmsS1Fixed14Number Shaper1G[256];
110
cmsS1Fixed14Number Shaper1B[256];
112
cmsS1Fixed14Number Mat[3][3]; // n.14 to n.14 (needs a saturation after that)
113
cmsS1Fixed14Number Off[3];
115
cmsUInt16Number Shaper2R[16385]; // 1.14 to 0..255
116
cmsUInt16Number Shaper2G[16385];
117
cmsUInt16Number Shaper2B[16385];
121
// Curves, optimization is shared between 8 and 16 bits
124
cmsContext ContextID;
126
cmsUInt32Number nCurves; // Number of curves
127
cmsUInt32Number nElements; // Elements in curves
128
cmsUInt16Number** Curves; // Points to a dynamically allocated array
133
// Simple optimizations ----------------------------------------------------------------------------------------------------------
136
// Remove an element in linked chain
138
void _RemoveElement(cmsStage** head)
140
cmsStage* mpe = *head;
141
cmsStage* next = mpe ->Next;
146
// Remove all identities in chain. Note that pt actually is a double pointer to the element that holds the pointer.
148
cmsBool _Remove1Op(cmsPipeline* Lut, cmsStageSignature UnaryOp)
150
cmsStage** pt = &Lut ->Elements;
151
cmsBool AnyOpt = FALSE;
153
while (*pt != NULL) {
155
if ((*pt) ->Implements == UnaryOp) {
160
pt = &((*pt) -> Next);
166
// Same, but only if two adjacent elements are found
168
cmsBool _Remove2Op(cmsPipeline* Lut, cmsStageSignature Op1, cmsStageSignature Op2)
172
cmsBool AnyOpt = FALSE;
174
pt1 = &Lut ->Elements;
175
if (*pt1 == NULL) return AnyOpt;
177
while (*pt1 != NULL) {
179
pt2 = &((*pt1) -> Next);
180
if (*pt2 == NULL) return AnyOpt;
182
if ((*pt1) ->Implements == Op1 && (*pt2) ->Implements == Op2) {
188
pt1 = &((*pt1) -> Next);
196
cmsBool CloseEnoughFloat(cmsFloat64Number a, cmsFloat64Number b)
198
return fabs(b - a) < 0.00001f;
202
cmsBool isFloatMatrixIdentity(const cmsMAT3* a)
207
_cmsMAT3identity(&Identity);
209
for (i = 0; i < 3; i++)
210
for (j = 0; j < 3; j++)
211
if (!CloseEnoughFloat(a->v[i].n[j], Identity.v[i].n[j])) return FALSE;
216
// if two adjacent matrices are found, multiply them.
218
cmsBool _MultiplyMatrix(cmsPipeline* Lut)
223
cmsBool AnyOpt = FALSE;
225
pt1 = &Lut->Elements;
226
if (*pt1 == NULL) return AnyOpt;
228
while (*pt1 != NULL) {
230
pt2 = &((*pt1)->Next);
231
if (*pt2 == NULL) return AnyOpt;
233
if ((*pt1)->Implements == cmsSigMatrixElemType && (*pt2)->Implements == cmsSigMatrixElemType) {
236
_cmsStageMatrixData* m1 = (_cmsStageMatrixData*) cmsStageData(*pt1);
237
_cmsStageMatrixData* m2 = (_cmsStageMatrixData*) cmsStageData(*pt2);
240
// Input offset and output offset should be zero to use this optimization
241
if (m1->Offset != NULL || m2 ->Offset != NULL ||
242
cmsStageInputChannels(*pt1) != 3 || cmsStageOutputChannels(*pt1) != 3 ||
243
cmsStageInputChannels(*pt2) != 3 || cmsStageOutputChannels(*pt2) != 3)
246
// Multiply both matrices to get the result
247
_cmsMAT3per(&res, (cmsMAT3*)m2->Double, (cmsMAT3*)m1->Double);
249
// Get the next in chain after the matrices
250
chain = (*pt2)->Next;
252
// Remove both matrices
256
// Now what if the result is a plain identity?
257
if (!isFloatMatrixIdentity(&res)) {
259
// We can not get rid of full matrix
260
cmsStage* Multmat = cmsStageAllocMatrix(Lut->ContextID, 3, 3, (const cmsFloat64Number*) &res, NULL);
261
if (Multmat == NULL) return FALSE; // Should never happen
264
Multmat->Next = chain;
271
pt1 = &((*pt1)->Next);
278
// Preoptimize just gets rif of no-ops coming paired. Conversion from v2 to v4 followed
279
// by a v4 to v2 and vice-versa. The elements are then discarded.
281
cmsBool PreOptimize(cmsPipeline* Lut)
283
cmsBool AnyOpt = FALSE, Opt;
289
// Remove all identities
290
Opt |= _Remove1Op(Lut, cmsSigIdentityElemType);
292
// Remove XYZ2Lab followed by Lab2XYZ
293
Opt |= _Remove2Op(Lut, cmsSigXYZ2LabElemType, cmsSigLab2XYZElemType);
295
// Remove Lab2XYZ followed by XYZ2Lab
296
Opt |= _Remove2Op(Lut, cmsSigLab2XYZElemType, cmsSigXYZ2LabElemType);
298
// Remove V4 to V2 followed by V2 to V4
299
Opt |= _Remove2Op(Lut, cmsSigLabV4toV2, cmsSigLabV2toV4);
301
// Remove V2 to V4 followed by V4 to V2
302
Opt |= _Remove2Op(Lut, cmsSigLabV2toV4, cmsSigLabV4toV2);
304
// Remove float pcs Lab conversions
305
Opt |= _Remove2Op(Lut, cmsSigLab2FloatPCS, cmsSigFloatPCS2Lab);
307
// Remove float pcs Lab conversions
308
Opt |= _Remove2Op(Lut, cmsSigXYZ2FloatPCS, cmsSigFloatPCS2XYZ);
311
Opt |= _MultiplyMatrix(Lut);
313
if (Opt) AnyOpt = TRUE;
321
void Eval16nop1D(CMSREGISTER const cmsUInt16Number Input[],
322
CMSREGISTER cmsUInt16Number Output[],
323
CMSREGISTER const struct _cms_interp_struc* p)
325
Output[0] = Input[0];
327
cmsUNUSED_PARAMETER(p);
331
void PrelinEval16(CMSREGISTER const cmsUInt16Number Input[],
332
CMSREGISTER cmsUInt16Number Output[],
333
CMSREGISTER const void* D)
335
Prelin16Data* p16 = (Prelin16Data*) D;
336
cmsUInt16Number StageABC[MAX_INPUT_DIMENSIONS];
337
cmsUInt16Number StageDEF[cmsMAXCHANNELS];
340
for (i=0; i < p16 ->nInputs; i++) {
342
p16 ->EvalCurveIn16[i](&Input[i], &StageABC[i], p16 ->ParamsCurveIn16[i]);
345
p16 ->EvalCLUT(StageABC, StageDEF, p16 ->CLUTparams);
347
for (i=0; i < p16 ->nOutputs; i++) {
349
p16 ->EvalCurveOut16[i](&StageDEF[i], &Output[i], p16 ->ParamsCurveOut16[i]);
355
void PrelinOpt16free(cmsContext ContextID, void* ptr)
357
Prelin16Data* p16 = (Prelin16Data*) ptr;
359
_cmsFree(ContextID, p16 ->EvalCurveOut16);
360
_cmsFree(ContextID, p16 ->ParamsCurveOut16);
362
_cmsFree(ContextID, p16);
366
void* Prelin16dup(cmsContext ContextID, const void* ptr)
368
Prelin16Data* p16 = (Prelin16Data*) ptr;
369
Prelin16Data* Duped = (Prelin16Data*) _cmsDupMem(ContextID, p16, sizeof(Prelin16Data));
371
if (Duped == NULL) return NULL;
373
Duped->EvalCurveOut16 = (_cmsInterpFn16*) _cmsDupMem(ContextID, p16->EvalCurveOut16, p16->nOutputs * sizeof(_cmsInterpFn16));
374
Duped->ParamsCurveOut16 = (cmsInterpParams**)_cmsDupMem(ContextID, p16->ParamsCurveOut16, p16->nOutputs * sizeof(cmsInterpParams*));
381
Prelin16Data* PrelinOpt16alloc(cmsContext ContextID,
382
const cmsInterpParams* ColorMap,
383
cmsUInt32Number nInputs, cmsToneCurve** In,
384
cmsUInt32Number nOutputs, cmsToneCurve** Out )
387
Prelin16Data* p16 = (Prelin16Data*)_cmsMallocZero(ContextID, sizeof(Prelin16Data));
388
if (p16 == NULL) return NULL;
390
p16 ->nInputs = nInputs;
391
p16 ->nOutputs = nOutputs;
394
for (i=0; i < nInputs; i++) {
397
p16 -> ParamsCurveIn16[i] = NULL;
398
p16 -> EvalCurveIn16[i] = Eval16nop1D;
402
p16 -> ParamsCurveIn16[i] = In[i] ->InterpParams;
403
p16 -> EvalCurveIn16[i] = p16 ->ParamsCurveIn16[i]->Interpolation.Lerp16;
407
p16 ->CLUTparams = ColorMap;
408
p16 ->EvalCLUT = ColorMap ->Interpolation.Lerp16;
411
p16 -> EvalCurveOut16 = (_cmsInterpFn16*) _cmsCalloc(ContextID, nOutputs, sizeof(_cmsInterpFn16));
412
if (p16->EvalCurveOut16 == NULL)
414
_cmsFree(ContextID, p16);
418
p16 -> ParamsCurveOut16 = (cmsInterpParams**) _cmsCalloc(ContextID, nOutputs, sizeof(cmsInterpParams* ));
419
if (p16->ParamsCurveOut16 == NULL)
422
_cmsFree(ContextID, p16->EvalCurveOut16);
423
_cmsFree(ContextID, p16);
427
for (i=0; i < nOutputs; i++) {
430
p16 ->ParamsCurveOut16[i] = NULL;
431
p16 -> EvalCurveOut16[i] = Eval16nop1D;
435
p16 ->ParamsCurveOut16[i] = Out[i] ->InterpParams;
436
p16 -> EvalCurveOut16[i] = p16 ->ParamsCurveOut16[i]->Interpolation.Lerp16;
445
// Resampling ---------------------------------------------------------------------------------
447
#define PRELINEARIZATION_POINTS 4096
449
// Sampler implemented by another LUT. This is a clean way to precalculate the devicelink 3D CLUT for
450
// almost any transform. We use floating point precision and then convert from floating point to 16 bits.
452
cmsInt32Number XFormSampler16(CMSREGISTER const cmsUInt16Number In[],
453
CMSREGISTER cmsUInt16Number Out[],
454
CMSREGISTER void* Cargo)
456
cmsPipeline* Lut = (cmsPipeline*) Cargo;
457
cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
460
_cmsAssert(Lut -> InputChannels < cmsMAXCHANNELS);
461
_cmsAssert(Lut -> OutputChannels < cmsMAXCHANNELS);
463
// From 16 bit to floating point
464
for (i=0; i < Lut ->InputChannels; i++)
465
InFloat[i] = (cmsFloat32Number) (In[i] / 65535.0);
467
// Evaluate in floating point
468
cmsPipelineEvalFloat(InFloat, OutFloat, Lut);
470
// Back to 16 bits representation
471
for (i=0; i < Lut ->OutputChannels; i++)
472
Out[i] = _cmsQuickSaturateWord(OutFloat[i] * 65535.0);
478
// Try to see if the curves of a given MPE are linear
480
cmsBool AllCurvesAreLinear(cmsStage* mpe)
482
cmsToneCurve** Curves;
483
cmsUInt32Number i, n;
485
Curves = _cmsStageGetPtrToCurveSet(mpe);
486
if (Curves == NULL) return FALSE;
488
n = cmsStageOutputChannels(mpe);
490
for (i=0; i < n; i++) {
491
if (!cmsIsToneCurveLinear(Curves[i])) return FALSE;
497
// This function replaces a specific node placed in "At" by the "Value" numbers. Its purpose
498
// is to fix scum dot on broken profiles/transforms. Works on 1, 3 and 4 channels
500
cmsBool PatchLUT(cmsStage* CLUT, cmsUInt16Number At[], cmsUInt16Number Value[],
501
cmsUInt32Number nChannelsOut, cmsUInt32Number nChannelsIn)
503
_cmsStageCLutData* Grid = (_cmsStageCLutData*) CLUT ->Data;
504
cmsInterpParams* p16 = Grid ->Params;
505
cmsFloat64Number px, py, pz, pw;
509
if (CLUT -> Type != cmsSigCLutElemType) {
510
cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) Attempt to PatchLUT on non-lut stage");
514
if (nChannelsIn == 4) {
516
px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
517
py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
518
pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
519
pw = ((cmsFloat64Number) At[3] * (p16->Domain[3])) / 65535.0;
521
x0 = (int) floor(px);
522
y0 = (int) floor(py);
523
z0 = (int) floor(pz);
524
w0 = (int) floor(pw);
526
if (((px - x0) != 0) ||
529
((pw - w0) != 0)) return FALSE; // Not on exact node
531
index = (int) p16 -> opta[3] * x0 +
532
(int) p16 -> opta[2] * y0 +
533
(int) p16 -> opta[1] * z0 +
534
(int) p16 -> opta[0] * w0;
537
if (nChannelsIn == 3) {
539
px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
540
py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
541
pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
543
x0 = (int) floor(px);
544
y0 = (int) floor(py);
545
z0 = (int) floor(pz);
547
if (((px - x0) != 0) ||
549
((pz - z0) != 0)) return FALSE; // Not on exact node
551
index = (int) p16 -> opta[2] * x0 +
552
(int) p16 -> opta[1] * y0 +
553
(int) p16 -> opta[0] * z0;
556
if (nChannelsIn == 1) {
558
px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
560
x0 = (int) floor(px);
562
if (((px - x0) != 0)) return FALSE; // Not on exact node
564
index = (int) p16 -> opta[0] * x0;
567
cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
571
for (i = 0; i < (int) nChannelsOut; i++)
572
Grid->Tab.T[index + i] = Value[i];
577
// Auxiliary, to see if two values are equal or very different
579
cmsBool WhitesAreEqual(cmsUInt32Number n, cmsUInt16Number White1[], cmsUInt16Number White2[] )
583
for (i=0; i < n; i++) {
585
if (abs(White1[i] - White2[i]) > 0xf000) return TRUE; // Values are so extremely different that the fixup should be avoided
586
if (White1[i] != White2[i]) return FALSE;
592
// Locate the node for the white point and fix it to pure white in order to avoid scum dot.
594
cmsBool FixWhiteMisalignment(cmsPipeline* Lut, cmsColorSpaceSignature EntryColorSpace, cmsColorSpaceSignature ExitColorSpace)
596
cmsUInt16Number *WhitePointIn, *WhitePointOut;
597
cmsUInt16Number WhiteIn[cmsMAXCHANNELS], WhiteOut[cmsMAXCHANNELS], ObtainedOut[cmsMAXCHANNELS];
598
cmsUInt32Number i, nOuts, nIns;
599
cmsStage *PreLin = NULL, *CLUT = NULL, *PostLin = NULL;
601
if (!_cmsEndPointsBySpace(EntryColorSpace,
602
&WhitePointIn, NULL, &nIns)) return FALSE;
604
if (!_cmsEndPointsBySpace(ExitColorSpace,
605
&WhitePointOut, NULL, &nOuts)) return FALSE;
607
// It needs to be fixed?
608
if (Lut ->InputChannels != nIns) return FALSE;
609
if (Lut ->OutputChannels != nOuts) return FALSE;
611
cmsPipelineEval16(WhitePointIn, ObtainedOut, Lut);
613
if (WhitesAreEqual(nOuts, WhitePointOut, ObtainedOut)) return TRUE; // whites already match
615
// Check if the LUT comes as Prelin, CLUT or Postlin. We allow all combinations
616
if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &PreLin, &CLUT, &PostLin))
617
if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCurveSetElemType, cmsSigCLutElemType, &PreLin, &CLUT))
618
if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCLutElemType, cmsSigCurveSetElemType, &CLUT, &PostLin))
619
if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCLutElemType, &CLUT))
622
// We need to interpolate white points of both, pre and post curves
625
cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PreLin);
627
for (i=0; i < nIns; i++) {
628
WhiteIn[i] = cmsEvalToneCurve16(Curves[i], WhitePointIn[i]);
632
for (i=0; i < nIns; i++)
633
WhiteIn[i] = WhitePointIn[i];
636
// If any post-linearization, we need to find how is represented white before the curve, do
637
// a reverse interpolation in this case.
640
cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PostLin);
642
for (i=0; i < nOuts; i++) {
644
cmsToneCurve* InversePostLin = cmsReverseToneCurve(Curves[i]);
645
if (InversePostLin == NULL) {
646
WhiteOut[i] = WhitePointOut[i];
650
WhiteOut[i] = cmsEvalToneCurve16(InversePostLin, WhitePointOut[i]);
651
cmsFreeToneCurve(InversePostLin);
656
for (i=0; i < nOuts; i++)
657
WhiteOut[i] = WhitePointOut[i];
660
// Ok, proceed with patching. May fail and we don't care if it fails
661
PatchLUT(CLUT, WhiteIn, WhiteOut, nOuts, nIns);
666
// -----------------------------------------------------------------------------------------------------------------------------------------------
667
// This function creates simple LUT from complex ones. The generated LUT has an optional set of
668
// prelinearization curves, a CLUT of nGridPoints and optional postlinearization tables.
669
// These curves have to exist in the original LUT in order to be used in the simplified output.
670
// Caller may also use the flags to allow this feature.
671
// LUTS with all curves will be simplified to a single curve. Parametric curves are lost.
672
// This function should be used on 16-bits LUTS only, as floating point losses precision when simplified
673
// -----------------------------------------------------------------------------------------------------------------------------------------------
676
cmsBool OptimizeByResampling(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
678
cmsPipeline* Src = NULL;
679
cmsPipeline* Dest = NULL;
681
cmsStage *KeepPreLin = NULL, *KeepPostLin = NULL;
682
cmsUInt32Number nGridPoints;
683
cmsColorSpaceSignature ColorSpace, OutputColorSpace;
684
cmsStage *NewPreLin = NULL;
685
cmsStage *NewPostLin = NULL;
686
_cmsStageCLutData* DataCLUT;
687
cmsToneCurve** DataSetIn;
688
cmsToneCurve** DataSetOut;
691
// This is a lossy optimization! does not apply in floating-point cases
692
if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
694
ColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*InputFormat));
695
OutputColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*OutputFormat));
697
// Color space must be specified
698
if (ColorSpace == (cmsColorSpaceSignature)0 ||
699
OutputColorSpace == (cmsColorSpaceSignature)0) return FALSE;
701
nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
703
// For empty LUTs, 2 points are enough
704
if (cmsPipelineStageCount(*Lut) == 0)
709
// Allocate an empty LUT
710
Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
711
if (!Dest) return FALSE;
713
// Prelinearization tables are kept unless indicated by flags
714
if (*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION) {
716
// Get a pointer to the prelinearization element
717
cmsStage* PreLin = cmsPipelineGetPtrToFirstStage(Src);
720
if (PreLin && PreLin ->Type == cmsSigCurveSetElemType) {
722
// Maybe this is a linear tram, so we can avoid the whole stuff
723
if (!AllCurvesAreLinear(PreLin)) {
725
// All seems ok, proceed.
726
NewPreLin = cmsStageDup(PreLin);
727
if(!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, NewPreLin))
730
// Remove prelinearization. Since we have duplicated the curve
731
// in destination LUT, the sampling should be applied after this stage.
732
cmsPipelineUnlinkStage(Src, cmsAT_BEGIN, &KeepPreLin);
738
CLUT = cmsStageAllocCLut16bit(Src ->ContextID, nGridPoints, Src ->InputChannels, Src->OutputChannels, NULL);
739
if (CLUT == NULL) goto Error;
741
// Add the CLUT to the destination LUT
742
if (!cmsPipelineInsertStage(Dest, cmsAT_END, CLUT)) {
746
// Postlinearization tables are kept unless indicated by flags
747
if (*dwFlags & cmsFLAGS_CLUT_POST_LINEARIZATION) {
749
// Get a pointer to the postlinearization if present
750
cmsStage* PostLin = cmsPipelineGetPtrToLastStage(Src);
753
if (PostLin && cmsStageType(PostLin) == cmsSigCurveSetElemType) {
755
// Maybe this is a linear tram, so we can avoid the whole stuff
756
if (!AllCurvesAreLinear(PostLin)) {
758
// All seems ok, proceed.
759
NewPostLin = cmsStageDup(PostLin);
760
if (!cmsPipelineInsertStage(Dest, cmsAT_END, NewPostLin))
763
// In destination LUT, the sampling should be applied after this stage.
764
cmsPipelineUnlinkStage(Src, cmsAT_END, &KeepPostLin);
769
// Now its time to do the sampling. We have to ignore pre/post linearization
770
// The source LUT without pre/post curves is passed as parameter.
771
if (!cmsStageSampleCLut16bit(CLUT, XFormSampler16, (void*) Src, 0)) {
773
// Ops, something went wrong, Restore stages
774
if (KeepPreLin != NULL) {
775
if (!cmsPipelineInsertStage(Src, cmsAT_BEGIN, KeepPreLin)) {
776
_cmsAssert(0); // This never happens
779
if (KeepPostLin != NULL) {
780
if (!cmsPipelineInsertStage(Src, cmsAT_END, KeepPostLin)) {
781
_cmsAssert(0); // This never happens
784
cmsPipelineFree(Dest);
790
if (KeepPreLin != NULL) cmsStageFree(KeepPreLin);
791
if (KeepPostLin != NULL) cmsStageFree(KeepPostLin);
792
cmsPipelineFree(Src);
794
DataCLUT = (_cmsStageCLutData*) CLUT ->Data;
796
if (NewPreLin == NULL) DataSetIn = NULL;
797
else DataSetIn = ((_cmsStageToneCurvesData*) NewPreLin ->Data) ->TheCurves;
799
if (NewPostLin == NULL) DataSetOut = NULL;
800
else DataSetOut = ((_cmsStageToneCurvesData*) NewPostLin ->Data) ->TheCurves;
803
if (DataSetIn == NULL && DataSetOut == NULL) {
805
_cmsPipelineSetOptimizationParameters(Dest, (_cmsPipelineEval16Fn) DataCLUT->Params->Interpolation.Lerp16, DataCLUT->Params, NULL, NULL);
809
p16 = PrelinOpt16alloc(Dest ->ContextID,
811
Dest ->InputChannels,
813
Dest ->OutputChannels,
816
_cmsPipelineSetOptimizationParameters(Dest, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
820
// Don't fix white on absolute colorimetric
821
if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
822
*dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
824
if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
826
FixWhiteMisalignment(Dest, ColorSpace, OutputColorSpace);
832
cmsUNUSED_PARAMETER(Intent);
836
// -----------------------------------------------------------------------------------------------------------------------------------------------
837
// Fixes the gamma balancing of transform. This is described in my paper "Prelinearization Stages on
838
// Color-Management Application-Specific Integrated Circuits (ASICs)" presented at NIP24. It only works
839
// for RGB transforms. See the paper for more details
840
// -----------------------------------------------------------------------------------------------------------------------------------------------
843
// Normalize endpoints by slope limiting max and min. This assures endpoints as well.
844
// Descending curves are handled as well.
846
void SlopeLimiting(cmsToneCurve* g)
848
int BeginVal, EndVal;
849
int AtBegin = (int) floor((cmsFloat64Number) g ->nEntries * 0.02 + 0.5); // Cutoff at 2%
850
int AtEnd = (int) g ->nEntries - AtBegin - 1; // And 98%
851
cmsFloat64Number Val, Slope, beta;
854
if (cmsIsToneCurveDescending(g)) {
855
BeginVal = 0xffff; EndVal = 0;
858
BeginVal = 0; EndVal = 0xffff;
861
// Compute slope and offset for begin of curve
862
Val = g ->Table16[AtBegin];
863
Slope = (Val - BeginVal) / AtBegin;
864
beta = Val - Slope * AtBegin;
866
for (i=0; i < AtBegin; i++)
867
g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
869
// Compute slope and offset for the end
870
Val = g ->Table16[AtEnd];
871
Slope = (EndVal - Val) / AtBegin; // AtBegin holds the X interval, which is same in both cases
872
beta = Val - Slope * AtEnd;
874
for (i = AtEnd; i < (int) g ->nEntries; i++)
875
g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
879
// Precomputes tables for 8-bit on input devicelink.
881
Prelin8Data* PrelinOpt8alloc(cmsContext ContextID, const cmsInterpParams* p, cmsToneCurve* G[3])
884
cmsUInt16Number Input[3];
885
cmsS15Fixed16Number v1, v2, v3;
888
p8 = (Prelin8Data*)_cmsMallocZero(ContextID, sizeof(Prelin8Data));
889
if (p8 == NULL) return NULL;
891
// Since this only works for 8 bit input, values comes always as x * 257,
892
// we can safely take msb byte (x << 8 + x)
894
for (i=0; i < 256; i++) {
898
// Get 16-bit representation
899
Input[0] = cmsEvalToneCurve16(G[0], FROM_8_TO_16(i));
900
Input[1] = cmsEvalToneCurve16(G[1], FROM_8_TO_16(i));
901
Input[2] = cmsEvalToneCurve16(G[2], FROM_8_TO_16(i));
904
Input[0] = FROM_8_TO_16(i);
905
Input[1] = FROM_8_TO_16(i);
906
Input[2] = FROM_8_TO_16(i);
910
// Move to 0..1.0 in fixed domain
911
v1 = _cmsToFixedDomain((int) (Input[0] * p -> Domain[0]));
912
v2 = _cmsToFixedDomain((int) (Input[1] * p -> Domain[1]));
913
v3 = _cmsToFixedDomain((int) (Input[2] * p -> Domain[2]));
915
// Store the precalculated table of nodes
916
p8 ->X0[i] = (p->opta[2] * FIXED_TO_INT(v1));
917
p8 ->Y0[i] = (p->opta[1] * FIXED_TO_INT(v2));
918
p8 ->Z0[i] = (p->opta[0] * FIXED_TO_INT(v3));
920
// Store the precalculated table of offsets
921
p8 ->rx[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v1);
922
p8 ->ry[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v2);
923
p8 ->rz[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v3);
926
p8 ->ContextID = ContextID;
933
void Prelin8free(cmsContext ContextID, void* ptr)
935
_cmsFree(ContextID, ptr);
939
void* Prelin8dup(cmsContext ContextID, const void* ptr)
941
return _cmsDupMem(ContextID, ptr, sizeof(Prelin8Data));
946
// A optimized interpolation for 8-bit input.
947
#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
948
static CMS_NO_SANITIZE
949
void PrelinEval8(CMSREGISTER const cmsUInt16Number Input[],
950
CMSREGISTER cmsUInt16Number Output[],
951
CMSREGISTER const void* D)
954
cmsUInt8Number r, g, b;
955
cmsS15Fixed16Number rx, ry, rz;
956
cmsS15Fixed16Number c0, c1, c2, c3, Rest;
958
CMSREGISTER cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
959
Prelin8Data* p8 = (Prelin8Data*) D;
960
CMSREGISTER const cmsInterpParams* p = p8 ->p;
961
int TotalOut = (int) p -> nOutputs;
962
const cmsUInt16Number* LutTable = (const cmsUInt16Number*) p->Table;
964
r = (cmsUInt8Number) (Input[0] >> 8);
965
g = (cmsUInt8Number) (Input[1] >> 8);
966
b = (cmsUInt8Number) (Input[2] >> 8);
968
X0 = (cmsS15Fixed16Number) p8->X0[r];
969
Y0 = (cmsS15Fixed16Number) p8->Y0[g];
970
Z0 = (cmsS15Fixed16Number) p8->Z0[b];
976
X1 = X0 + (cmsS15Fixed16Number)((rx == 0) ? 0 : p ->opta[2]);
977
Y1 = Y0 + (cmsS15Fixed16Number)((ry == 0) ? 0 : p ->opta[1]);
978
Z1 = Z0 + (cmsS15Fixed16Number)((rz == 0) ? 0 : p ->opta[0]);
981
// These are the 6 Tetrahedral
982
for (OutChan=0; OutChan < TotalOut; OutChan++) {
984
c0 = DENS(X0, Y0, Z0);
986
if (rx >= ry && ry >= rz)
988
c1 = DENS(X1, Y0, Z0) - c0;
989
c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
990
c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
993
if (rx >= rz && rz >= ry)
995
c1 = DENS(X1, Y0, Z0) - c0;
996
c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
997
c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
1000
if (rz >= rx && rx >= ry)
1002
c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
1003
c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
1004
c3 = DENS(X0, Y0, Z1) - c0;
1007
if (ry >= rx && rx >= rz)
1009
c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
1010
c2 = DENS(X0, Y1, Z0) - c0;
1011
c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
1014
if (ry >= rz && rz >= rx)
1016
c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
1017
c2 = DENS(X0, Y1, Z0) - c0;
1018
c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
1021
if (rz >= ry && ry >= rx)
1023
c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
1024
c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
1025
c3 = DENS(X0, Y0, Z1) - c0;
1031
Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
1032
Output[OutChan] = (cmsUInt16Number) (c0 + ((Rest + (Rest >> 16)) >> 16));
1040
// Curves that contain wide empty areas are not optimizeable
1042
cmsBool IsDegenerated(const cmsToneCurve* g)
1044
cmsUInt32Number i, Zeros = 0, Poles = 0;
1045
cmsUInt32Number nEntries = g ->nEntries;
1047
for (i=0; i < nEntries; i++) {
1049
if (g ->Table16[i] == 0x0000) Zeros++;
1050
if (g ->Table16[i] == 0xffff) Poles++;
1053
if (Zeros == 1 && Poles == 1) return FALSE; // For linear tables
1054
if (Zeros > (nEntries / 20)) return TRUE; // Degenerated, many zeros
1055
if (Poles > (nEntries / 20)) return TRUE; // Degenerated, many poles
1060
// --------------------------------------------------------------------------------------------------------------
1061
// We need xput over here
1064
cmsBool OptimizeByComputingLinearization(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1066
cmsPipeline* OriginalLut;
1067
cmsUInt32Number nGridPoints;
1068
cmsToneCurve *Trans[cmsMAXCHANNELS], *TransReverse[cmsMAXCHANNELS];
1069
cmsUInt32Number t, i;
1070
cmsFloat32Number v, In[cmsMAXCHANNELS], Out[cmsMAXCHANNELS];
1071
cmsBool lIsSuitable, lIsLinear;
1072
cmsPipeline* OptimizedLUT = NULL, *LutPlusCurves = NULL;
1073
cmsStage* OptimizedCLUTmpe;
1074
cmsColorSpaceSignature ColorSpace, OutputColorSpace;
1075
cmsStage* OptimizedPrelinMpe;
1076
cmsToneCurve** OptimizedPrelinCurves;
1077
_cmsStageCLutData* OptimizedPrelinCLUT;
1080
// This is a lossy optimization! does not apply in floating-point cases
1081
if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
1083
// Only on chunky RGB
1084
if (T_COLORSPACE(*InputFormat) != PT_RGB) return FALSE;
1085
if (T_PLANAR(*InputFormat)) return FALSE;
1087
if (T_COLORSPACE(*OutputFormat) != PT_RGB) return FALSE;
1088
if (T_PLANAR(*OutputFormat)) return FALSE;
1090
// On 16 bits, user has to specify the feature
1091
if (!_cmsFormatterIs8bit(*InputFormat)) {
1092
if (!(*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION)) return FALSE;
1097
ColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*InputFormat));
1098
OutputColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*OutputFormat));
1100
// Color space must be specified
1101
if (ColorSpace == (cmsColorSpaceSignature)0 ||
1102
OutputColorSpace == (cmsColorSpaceSignature)0) return FALSE;
1104
nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
1106
// Empty gamma containers
1107
memset(Trans, 0, sizeof(Trans));
1108
memset(TransReverse, 0, sizeof(TransReverse));
1110
// If the last stage of the original lut are curves, and those curves are
1111
// degenerated, it is likely the transform is squeezing and clipping
1112
// the output from previous CLUT. We cannot optimize this case
1114
cmsStage* last = cmsPipelineGetPtrToLastStage(OriginalLut);
1116
if (last == NULL) goto Error;
1117
if (cmsStageType(last) == cmsSigCurveSetElemType) {
1119
_cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*)cmsStageData(last);
1120
for (i = 0; i < Data->nCurves; i++) {
1121
if (IsDegenerated(Data->TheCurves[i]))
1127
for (t = 0; t < OriginalLut ->InputChannels; t++) {
1128
Trans[t] = cmsBuildTabulatedToneCurve16(OriginalLut ->ContextID, PRELINEARIZATION_POINTS, NULL);
1129
if (Trans[t] == NULL) goto Error;
1132
// Populate the curves
1133
for (i=0; i < PRELINEARIZATION_POINTS; i++) {
1135
v = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
1137
// Feed input with a gray ramp
1138
for (t=0; t < OriginalLut ->InputChannels; t++)
1141
// Evaluate the gray value
1142
cmsPipelineEvalFloat(In, Out, OriginalLut);
1144
// Store result in curve
1145
for (t=0; t < OriginalLut ->InputChannels; t++)
1147
if (Trans[t]->Table16 != NULL)
1148
Trans[t] ->Table16[i] = _cmsQuickSaturateWord(Out[t] * 65535.0);
1152
// Slope-limit the obtained curves
1153
for (t = 0; t < OriginalLut ->InputChannels; t++)
1154
SlopeLimiting(Trans[t]);
1156
// Check for validity. lIsLinear is here for debug purposes
1159
for (t=0; (lIsSuitable && (t < OriginalLut ->InputChannels)); t++) {
1161
// Exclude if already linear
1162
if (!cmsIsToneCurveLinear(Trans[t]))
1165
// Exclude if non-monotonic
1166
if (!cmsIsToneCurveMonotonic(Trans[t]))
1167
lIsSuitable = FALSE;
1169
if (IsDegenerated(Trans[t]))
1170
lIsSuitable = FALSE;
1173
// If it is not suitable, just quit
1174
if (!lIsSuitable) goto Error;
1176
// Invert curves if possible
1177
for (t = 0; t < OriginalLut ->InputChannels; t++) {
1178
TransReverse[t] = cmsReverseToneCurveEx(PRELINEARIZATION_POINTS, Trans[t]);
1179
if (TransReverse[t] == NULL) goto Error;
1182
// Now inset the reversed curves at the begin of transform
1183
LutPlusCurves = cmsPipelineDup(OriginalLut);
1184
if (LutPlusCurves == NULL) goto Error;
1186
if (!cmsPipelineInsertStage(LutPlusCurves, cmsAT_BEGIN, cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, TransReverse)))
1189
// Create the result LUT
1190
OptimizedLUT = cmsPipelineAlloc(OriginalLut ->ContextID, OriginalLut ->InputChannels, OriginalLut ->OutputChannels);
1191
if (OptimizedLUT == NULL) goto Error;
1193
OptimizedPrelinMpe = cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, Trans);
1195
// Create and insert the curves at the beginning
1196
if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_BEGIN, OptimizedPrelinMpe))
1199
// Allocate the CLUT for result
1200
OptimizedCLUTmpe = cmsStageAllocCLut16bit(OriginalLut ->ContextID, nGridPoints, OriginalLut ->InputChannels, OriginalLut ->OutputChannels, NULL);
1202
// Add the CLUT to the destination LUT
1203
if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_END, OptimizedCLUTmpe))
1207
if (!cmsStageSampleCLut16bit(OptimizedCLUTmpe, XFormSampler16, (void*) LutPlusCurves, 0)) goto Error;
1210
for (t = 0; t < OriginalLut ->InputChannels; t++) {
1212
if (Trans[t]) cmsFreeToneCurve(Trans[t]);
1213
if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
1216
cmsPipelineFree(LutPlusCurves);
1219
OptimizedPrelinCurves = _cmsStageGetPtrToCurveSet(OptimizedPrelinMpe);
1220
OptimizedPrelinCLUT = (_cmsStageCLutData*) OptimizedCLUTmpe ->Data;
1222
// Set the evaluator if 8-bit
1223
if (_cmsFormatterIs8bit(*InputFormat)) {
1225
Prelin8Data* p8 = PrelinOpt8alloc(OptimizedLUT ->ContextID,
1226
OptimizedPrelinCLUT ->Params,
1227
OptimizedPrelinCurves);
1228
if (p8 == NULL) return FALSE;
1230
_cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval8, (void*) p8, Prelin8free, Prelin8dup);
1235
Prelin16Data* p16 = PrelinOpt16alloc(OptimizedLUT ->ContextID,
1236
OptimizedPrelinCLUT ->Params,
1237
3, OptimizedPrelinCurves, 3, NULL);
1238
if (p16 == NULL) return FALSE;
1240
_cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
1244
// Don't fix white on absolute colorimetric
1245
if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
1246
*dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
1248
if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
1250
if (!FixWhiteMisalignment(OptimizedLUT, ColorSpace, OutputColorSpace)) {
1256
// And return the obtained LUT
1258
cmsPipelineFree(OriginalLut);
1259
*Lut = OptimizedLUT;
1264
for (t = 0; t < OriginalLut ->InputChannels; t++) {
1266
if (Trans[t]) cmsFreeToneCurve(Trans[t]);
1267
if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
1270
if (LutPlusCurves != NULL) cmsPipelineFree(LutPlusCurves);
1271
if (OptimizedLUT != NULL) cmsPipelineFree(OptimizedLUT);
1275
cmsUNUSED_PARAMETER(Intent);
1276
cmsUNUSED_PARAMETER(lIsLinear);
1280
// Curves optimizer ------------------------------------------------------------------------------------------------------------------
1283
void CurvesFree(cmsContext ContextID, void* ptr)
1285
Curves16Data* Data = (Curves16Data*) ptr;
1288
for (i=0; i < Data -> nCurves; i++) {
1290
_cmsFree(ContextID, Data ->Curves[i]);
1293
_cmsFree(ContextID, Data ->Curves);
1294
_cmsFree(ContextID, ptr);
1298
void* CurvesDup(cmsContext ContextID, const void* ptr)
1300
Curves16Data* Data = (Curves16Data*)_cmsDupMem(ContextID, ptr, sizeof(Curves16Data));
1303
if (Data == NULL) return NULL;
1305
Data->Curves = (cmsUInt16Number**) _cmsDupMem(ContextID, Data->Curves, Data->nCurves * sizeof(cmsUInt16Number*));
1307
for (i=0; i < Data -> nCurves; i++) {
1308
Data->Curves[i] = (cmsUInt16Number*) _cmsDupMem(ContextID, Data->Curves[i], Data->nElements * sizeof(cmsUInt16Number));
1311
return (void*) Data;
1314
// Precomputes tables for 8-bit on input devicelink.
1316
Curves16Data* CurvesAlloc(cmsContext ContextID, cmsUInt32Number nCurves, cmsUInt32Number nElements, cmsToneCurve** G)
1318
cmsUInt32Number i, j;
1321
c16 = (Curves16Data*)_cmsMallocZero(ContextID, sizeof(Curves16Data));
1322
if (c16 == NULL) return NULL;
1324
c16 ->nCurves = nCurves;
1325
c16 ->nElements = nElements;
1327
c16->Curves = (cmsUInt16Number**) _cmsCalloc(ContextID, nCurves, sizeof(cmsUInt16Number*));
1328
if (c16->Curves == NULL) {
1329
_cmsFree(ContextID, c16);
1333
for (i=0; i < nCurves; i++) {
1335
c16->Curves[i] = (cmsUInt16Number*) _cmsCalloc(ContextID, nElements, sizeof(cmsUInt16Number));
1337
if (c16->Curves[i] == NULL) {
1339
for (j=0; j < i; j++) {
1340
_cmsFree(ContextID, c16->Curves[j]);
1342
_cmsFree(ContextID, c16->Curves);
1343
_cmsFree(ContextID, c16);
1347
if (nElements == 256U) {
1349
for (j=0; j < nElements; j++) {
1351
c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], FROM_8_TO_16(j));
1356
for (j=0; j < nElements; j++) {
1357
c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], (cmsUInt16Number) j);
1366
void FastEvaluateCurves8(CMSREGISTER const cmsUInt16Number In[],
1367
CMSREGISTER cmsUInt16Number Out[],
1368
CMSREGISTER const void* D)
1370
Curves16Data* Data = (Curves16Data*) D;
1374
for (i=0; i < Data ->nCurves; i++) {
1377
Out[i] = Data -> Curves[i][x];
1383
void FastEvaluateCurves16(CMSREGISTER const cmsUInt16Number In[],
1384
CMSREGISTER cmsUInt16Number Out[],
1385
CMSREGISTER const void* D)
1387
Curves16Data* Data = (Curves16Data*) D;
1390
for (i=0; i < Data ->nCurves; i++) {
1391
Out[i] = Data -> Curves[i][In[i]];
1397
void FastIdentity16(CMSREGISTER const cmsUInt16Number In[],
1398
CMSREGISTER cmsUInt16Number Out[],
1399
CMSREGISTER const void* D)
1401
cmsPipeline* Lut = (cmsPipeline*) D;
1404
for (i=0; i < Lut ->InputChannels; i++) {
1410
// If the target LUT holds only curves, the optimization procedure is to join all those
1411
// curves together. That only works on curves and does not work on matrices.
1413
cmsBool OptimizeByJoiningCurves(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1415
cmsToneCurve** GammaTables = NULL;
1416
cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
1417
cmsUInt32Number i, j;
1418
cmsPipeline* Src = *Lut;
1419
cmsPipeline* Dest = NULL;
1421
cmsStage* ObtainedCurves = NULL;
1424
// This is a lossy optimization! does not apply in floating-point cases
1425
if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
1427
// Only curves in this LUT?
1428
for (mpe = cmsPipelineGetPtrToFirstStage(Src);
1430
mpe = cmsStageNext(mpe)) {
1431
if (cmsStageType(mpe) != cmsSigCurveSetElemType) return FALSE;
1434
// Allocate an empty LUT
1435
Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
1436
if (Dest == NULL) return FALSE;
1438
// Create target curves
1439
GammaTables = (cmsToneCurve**) _cmsCalloc(Src ->ContextID, Src ->InputChannels, sizeof(cmsToneCurve*));
1440
if (GammaTables == NULL) goto Error;
1442
for (i=0; i < Src ->InputChannels; i++) {
1443
GammaTables[i] = cmsBuildTabulatedToneCurve16(Src ->ContextID, PRELINEARIZATION_POINTS, NULL);
1444
if (GammaTables[i] == NULL) goto Error;
1447
// Compute 16 bit result by using floating point
1448
for (i=0; i < PRELINEARIZATION_POINTS; i++) {
1450
for (j=0; j < Src ->InputChannels; j++)
1451
InFloat[j] = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
1453
cmsPipelineEvalFloat(InFloat, OutFloat, Src);
1455
for (j=0; j < Src ->InputChannels; j++)
1456
GammaTables[j] -> Table16[i] = _cmsQuickSaturateWord(OutFloat[j] * 65535.0);
1459
ObtainedCurves = cmsStageAllocToneCurves(Src ->ContextID, Src ->InputChannels, GammaTables);
1460
if (ObtainedCurves == NULL) goto Error;
1462
for (i=0; i < Src ->InputChannels; i++) {
1463
cmsFreeToneCurve(GammaTables[i]);
1464
GammaTables[i] = NULL;
1467
if (GammaTables != NULL) {
1468
_cmsFree(Src->ContextID, GammaTables);
1472
// Maybe the curves are linear at the end
1473
if (!AllCurvesAreLinear(ObtainedCurves)) {
1474
_cmsStageToneCurvesData* Data;
1476
if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, ObtainedCurves))
1478
Data = (_cmsStageToneCurvesData*) cmsStageData(ObtainedCurves);
1479
ObtainedCurves = NULL;
1481
// If the curves are to be applied in 8 bits, we can save memory
1482
if (_cmsFormatterIs8bit(*InputFormat)) {
1483
Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 256, Data ->TheCurves);
1485
if (c16 == NULL) goto Error;
1486
*dwFlags |= cmsFLAGS_NOCACHE;
1487
_cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves8, c16, CurvesFree, CurvesDup);
1491
Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 65536, Data ->TheCurves);
1493
if (c16 == NULL) goto Error;
1494
*dwFlags |= cmsFLAGS_NOCACHE;
1495
_cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves16, c16, CurvesFree, CurvesDup);
1500
// LUT optimizes to nothing. Set the identity LUT
1501
cmsStageFree(ObtainedCurves);
1502
ObtainedCurves = NULL;
1504
if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageAllocIdentity(Dest ->ContextID, Src ->InputChannels)))
1507
*dwFlags |= cmsFLAGS_NOCACHE;
1508
_cmsPipelineSetOptimizationParameters(Dest, FastIdentity16, (void*) Dest, NULL, NULL);
1512
cmsPipelineFree(Src);
1518
if (ObtainedCurves != NULL) cmsStageFree(ObtainedCurves);
1519
if (GammaTables != NULL) {
1520
for (i=0; i < Src ->InputChannels; i++) {
1521
if (GammaTables[i] != NULL) cmsFreeToneCurve(GammaTables[i]);
1524
_cmsFree(Src ->ContextID, GammaTables);
1527
if (Dest != NULL) cmsPipelineFree(Dest);
1530
cmsUNUSED_PARAMETER(Intent);
1531
cmsUNUSED_PARAMETER(InputFormat);
1532
cmsUNUSED_PARAMETER(OutputFormat);
1533
cmsUNUSED_PARAMETER(dwFlags);
1536
// -------------------------------------------------------------------------------------------------------------------------------------
1537
// LUT is Shaper - Matrix - Matrix - Shaper, which is very frequent when combining two matrix-shaper profiles
1541
void FreeMatShaper(cmsContext ContextID, void* Data)
1543
if (Data != NULL) _cmsFree(ContextID, Data);
1547
void* DupMatShaper(cmsContext ContextID, const void* Data)
1549
return _cmsDupMem(ContextID, Data, sizeof(MatShaper8Data));
1553
// A fast matrix-shaper evaluator for 8 bits. This is a bit tricky since I'm using 1.14 signed fixed point
1554
// to accomplish some performance. Actually it takes 256x3 16 bits tables and 16385 x 3 tables of 8 bits,
1555
// in total about 50K, and the performance boost is huge!
1556
static CMS_NO_SANITIZE
1557
void MatShaperEval16(CMSREGISTER const cmsUInt16Number In[],
1558
CMSREGISTER cmsUInt16Number Out[],
1559
CMSREGISTER const void* D)
1561
MatShaper8Data* p = (MatShaper8Data*) D;
1562
cmsS1Fixed14Number l1, l2, l3, r, g, b;
1563
cmsUInt32Number ri, gi, bi;
1565
// In this case (and only in this case!) we can use this simplification since
1566
// In[] is assured to come from a 8 bit number. (a << 8 | a)
1571
// Across first shaper, which also converts to 1.14 fixed point
1572
r = p->Shaper1R[ri];
1573
g = p->Shaper1G[gi];
1574
b = p->Shaper1B[bi];
1576
// Evaluate the matrix in 1.14 fixed point
1577
l1 = (p->Mat[0][0] * r + p->Mat[0][1] * g + p->Mat[0][2] * b + p->Off[0] + 0x2000) >> 14;
1578
l2 = (p->Mat[1][0] * r + p->Mat[1][1] * g + p->Mat[1][2] * b + p->Off[1] + 0x2000) >> 14;
1579
l3 = (p->Mat[2][0] * r + p->Mat[2][1] * g + p->Mat[2][2] * b + p->Off[2] + 0x2000) >> 14;
1581
// Now we have to clip to 0..1.0 range
1582
ri = (l1 < 0) ? 0 : ((l1 > 16384) ? 16384U : (cmsUInt32Number) l1);
1583
gi = (l2 < 0) ? 0 : ((l2 > 16384) ? 16384U : (cmsUInt32Number) l2);
1584
bi = (l3 < 0) ? 0 : ((l3 > 16384) ? 16384U : (cmsUInt32Number) l3);
1586
// And across second shaper,
1587
Out[0] = p->Shaper2R[ri];
1588
Out[1] = p->Shaper2G[gi];
1589
Out[2] = p->Shaper2B[bi];
1593
// This table converts from 8 bits to 1.14 after applying the curve
1595
void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
1598
cmsFloat32Number R, y;
1600
for (i=0; i < 256; i++) {
1602
R = (cmsFloat32Number) (i / 255.0);
1603
y = cmsEvalToneCurveFloat(Curve, R);
1606
Table[i] = DOUBLE_TO_1FIXED14(y);
1608
Table[i] = 0x7fffffff;
1612
// This table converts form 1.14 (being 0x4000 the last entry) to 8 bits after applying the curve
1614
void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
1617
cmsFloat32Number R, Val;
1619
for (i=0; i < 16385; i++) {
1621
R = (cmsFloat32Number) (i / 16384.0);
1622
Val = cmsEvalToneCurveFloat(Curve, R); // Val comes 0..1.0
1630
if (Is8BitsOutput) {
1632
// If 8 bits output, we can optimize further by computing the / 257 part.
1633
// first we compute the resulting byte and then we store the byte times
1634
// 257. This quantization allows to round very quick by doing a >> 8, but
1635
// since the low byte is always equal to msb, we can do a & 0xff and this works!
1636
cmsUInt16Number w = _cmsQuickSaturateWord(Val * 65535.0);
1637
cmsUInt8Number b = FROM_16_TO_8(w);
1639
Table[i] = FROM_8_TO_16(b);
1641
else Table[i] = _cmsQuickSaturateWord(Val * 65535.0);
1645
// Compute the matrix-shaper structure
1647
cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, cmsVEC3* Off, cmsToneCurve* Curve2[3], cmsUInt32Number* OutputFormat)
1651
cmsBool Is8Bits = _cmsFormatterIs8bit(*OutputFormat);
1653
// Allocate a big chuck of memory to store precomputed tables
1654
p = (MatShaper8Data*) _cmsMalloc(Dest ->ContextID, sizeof(MatShaper8Data));
1655
if (p == NULL) return FALSE;
1657
p -> ContextID = Dest -> ContextID;
1659
// Precompute tables
1660
FillFirstShaper(p ->Shaper1R, Curve1[0]);
1661
FillFirstShaper(p ->Shaper1G, Curve1[1]);
1662
FillFirstShaper(p ->Shaper1B, Curve1[2]);
1664
FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits);
1665
FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits);
1666
FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits);
1668
// Convert matrix to nFixed14. Note that those values may take more than 16 bits
1669
for (i=0; i < 3; i++) {
1670
for (j=0; j < 3; j++) {
1671
p ->Mat[i][j] = DOUBLE_TO_1FIXED14(Mat->v[i].n[j]);
1675
for (i=0; i < 3; i++) {
1681
p ->Off[i] = DOUBLE_TO_1FIXED14(Off->n[i]);
1685
// Mark as optimized for faster formatter
1687
*OutputFormat |= OPTIMIZED_SH(1);
1689
// Fill function pointers
1690
_cmsPipelineSetOptimizationParameters(Dest, MatShaperEval16, (void*) p, FreeMatShaper, DupMatShaper);
1694
// 8 bits on input allows matrix-shaper boot up to 25 Mpixels per second on RGB. That's fast!
1696
cmsBool OptimizeMatrixShaper(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1698
cmsStage* Curve1, *Curve2;
1699
cmsStage* Matrix1, *Matrix2;
1701
cmsBool IdentityMat;
1702
cmsPipeline* Dest, *Src;
1703
cmsFloat64Number* Offset;
1705
// Only works on RGB to RGB
1706
if (T_CHANNELS(*InputFormat) != 3 || T_CHANNELS(*OutputFormat) != 3) return FALSE;
1708
// Only works on 8 bit input
1709
if (!_cmsFormatterIs8bit(*InputFormat)) return FALSE;
1711
// Seems suitable, proceed
1716
// shaper-matrix-matrix-shaper
1717
// shaper-matrix-shaper
1719
// Both of those constructs are possible (first because abs. colorimetric).
1720
// additionally, In the first case, the input matrix offset should be zero.
1722
IdentityMat = FALSE;
1723
if (cmsPipelineCheckAndRetreiveStages(Src, 4,
1724
cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
1725
&Curve1, &Matrix1, &Matrix2, &Curve2)) {
1727
// Get both matrices
1728
_cmsStageMatrixData* Data1 = (_cmsStageMatrixData*)cmsStageData(Matrix1);
1729
_cmsStageMatrixData* Data2 = (_cmsStageMatrixData*)cmsStageData(Matrix2);
1732
if (Matrix1->InputChannels != 3 || Matrix1->OutputChannels != 3 ||
1733
Matrix2->InputChannels != 3 || Matrix2->OutputChannels != 3) return FALSE;
1735
// Input offset should be zero
1736
if (Data1->Offset != NULL) return FALSE;
1738
// Multiply both matrices to get the result
1739
_cmsMAT3per(&res, (cmsMAT3*)Data2->Double, (cmsMAT3*)Data1->Double);
1741
// Only 2nd matrix has offset, or it is zero
1742
Offset = Data2->Offset;
1744
// Now the result is in res + Data2 -> Offset. Maybe is a plain identity?
1745
if (_cmsMAT3isIdentity(&res) && Offset == NULL) {
1747
// We can get rid of full matrix
1754
if (cmsPipelineCheckAndRetreiveStages(Src, 3,
1755
cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
1756
&Curve1, &Matrix1, &Curve2)) {
1758
_cmsStageMatrixData* Data = (_cmsStageMatrixData*)cmsStageData(Matrix1);
1760
if (Matrix1->InputChannels != 3 || Matrix1->OutputChannels != 3) return FALSE;
1762
// Copy the matrix to our result
1763
memcpy(&res, Data->Double, sizeof(res));
1765
// Preserve the Odffset (may be NULL as a zero offset)
1766
Offset = Data->Offset;
1768
if (_cmsMAT3isIdentity(&res) && Offset == NULL) {
1770
// We can get rid of full matrix
1775
return FALSE; // Not optimizeable this time
1779
// Allocate an empty LUT
1780
Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
1781
if (!Dest) return FALSE;
1783
// Assamble the new LUT
1784
if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageDup(Curve1)))
1789
if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageAllocMatrix(Dest->ContextID, 3, 3, (const cmsFloat64Number*)&res, Offset)))
1793
if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageDup(Curve2)))
1796
// If identity on matrix, we can further optimize the curves, so call the join curves routine
1799
OptimizeByJoiningCurves(&Dest, Intent, InputFormat, OutputFormat, dwFlags);
1802
_cmsStageToneCurvesData* mpeC1 = (_cmsStageToneCurvesData*) cmsStageData(Curve1);
1803
_cmsStageToneCurvesData* mpeC2 = (_cmsStageToneCurvesData*) cmsStageData(Curve2);
1805
// In this particular optimization, cache does not help as it takes more time to deal with
1806
// the cache than with the pixel handling
1807
*dwFlags |= cmsFLAGS_NOCACHE;
1809
// Setup the optimizarion routines
1810
SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Offset, mpeC2->TheCurves, OutputFormat);
1813
cmsPipelineFree(Src);
1817
// Leave Src unchanged
1818
cmsPipelineFree(Dest);
1823
// -------------------------------------------------------------------------------------------------------------------------------------
1824
// Optimization plug-ins
1826
// List of optimizations
1827
typedef struct _cmsOptimizationCollection_st {
1829
_cmsOPToptimizeFn OptimizePtr;
1831
struct _cmsOptimizationCollection_st *Next;
1833
} _cmsOptimizationCollection;
1836
// The built-in list. We currently implement 4 types of optimizations. Joining of curves, matrix-shaper, linearization and resampling
1837
static _cmsOptimizationCollection DefaultOptimization[] = {
1839
{ OptimizeByJoiningCurves, &DefaultOptimization[1] },
1840
{ OptimizeMatrixShaper, &DefaultOptimization[2] },
1841
{ OptimizeByComputingLinearization, &DefaultOptimization[3] },
1842
{ OptimizeByResampling, NULL }
1845
// The linked list head
1846
_cmsOptimizationPluginChunkType _cmsOptimizationPluginChunk = { NULL };
1849
// Duplicates the zone of memory used by the plug-in in the new context
1851
void DupPluginOptimizationList(struct _cmsContext_struct* ctx,
1852
const struct _cmsContext_struct* src)
1854
_cmsOptimizationPluginChunkType newHead = { NULL };
1855
_cmsOptimizationCollection* entry;
1856
_cmsOptimizationCollection* Anterior = NULL;
1857
_cmsOptimizationPluginChunkType* head = (_cmsOptimizationPluginChunkType*) src->chunks[OptimizationPlugin];
1859
_cmsAssert(ctx != NULL);
1860
_cmsAssert(head != NULL);
1862
// Walk the list copying all nodes
1863
for (entry = head->OptimizationCollection;
1865
entry = entry ->Next) {
1867
_cmsOptimizationCollection *newEntry = ( _cmsOptimizationCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsOptimizationCollection));
1869
if (newEntry == NULL)
1872
// We want to keep the linked list order, so this is a little bit tricky
1873
newEntry -> Next = NULL;
1875
Anterior -> Next = newEntry;
1877
Anterior = newEntry;
1879
if (newHead.OptimizationCollection == NULL)
1880
newHead.OptimizationCollection = newEntry;
1883
ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsOptimizationPluginChunkType));
1886
void _cmsAllocOptimizationPluginChunk(struct _cmsContext_struct* ctx,
1887
const struct _cmsContext_struct* src)
1891
// Copy all linked list
1892
DupPluginOptimizationList(ctx, src);
1895
static _cmsOptimizationPluginChunkType OptimizationPluginChunkType = { NULL };
1896
ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx ->MemPool, &OptimizationPluginChunkType, sizeof(_cmsOptimizationPluginChunkType));
1901
// Register new ways to optimize
1902
cmsBool _cmsRegisterOptimizationPlugin(cmsContext ContextID, cmsPluginBase* Data)
1904
cmsPluginOptimization* Plugin = (cmsPluginOptimization*) Data;
1905
_cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
1906
_cmsOptimizationCollection* fl;
1910
ctx->OptimizationCollection = NULL;
1914
// Optimizer callback is required
1915
if (Plugin ->OptimizePtr == NULL) return FALSE;
1917
fl = (_cmsOptimizationCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsOptimizationCollection));
1918
if (fl == NULL) return FALSE;
1920
// Copy the parameters
1921
fl ->OptimizePtr = Plugin ->OptimizePtr;
1924
fl ->Next = ctx->OptimizationCollection;
1927
ctx ->OptimizationCollection = fl;
1933
// The entry point for LUT optimization
1934
cmsBool CMSEXPORT _cmsOptimizePipeline(cmsContext ContextID,
1935
cmsPipeline** PtrLut,
1936
cmsUInt32Number Intent,
1937
cmsUInt32Number* InputFormat,
1938
cmsUInt32Number* OutputFormat,
1939
cmsUInt32Number* dwFlags)
1941
_cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
1942
_cmsOptimizationCollection* Opts;
1943
cmsBool AnySuccess = FALSE;
1946
// A CLUT is being asked, so force this specific optimization
1947
if (*dwFlags & cmsFLAGS_FORCE_CLUT) {
1949
PreOptimize(*PtrLut);
1950
return OptimizeByResampling(PtrLut, Intent, InputFormat, OutputFormat, dwFlags);
1953
// Anything to optimize?
1954
if ((*PtrLut) ->Elements == NULL) {
1955
_cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
1959
// Named color pipelines cannot be optimized
1960
for (mpe = cmsPipelineGetPtrToFirstStage(*PtrLut);
1962
mpe = cmsStageNext(mpe)) {
1963
if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
1966
// Try to get rid of identities and trivial conversions.
1967
AnySuccess = PreOptimize(*PtrLut);
1969
// After removal do we end with an identity?
1970
if ((*PtrLut) ->Elements == NULL) {
1971
_cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
1975
// Do not optimize, keep all precision
1976
if (*dwFlags & cmsFLAGS_NOOPTIMIZE)
1979
// Try plug-in optimizations
1980
for (Opts = ctx->OptimizationCollection;
1982
Opts = Opts ->Next) {
1984
// If one schema succeeded, we are done
1985
if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
1987
return TRUE; // Optimized!
1991
// Try built-in optimizations
1992
for (Opts = DefaultOptimization;
1994
Opts = Opts ->Next) {
1996
if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
2002
// Only simple optimizations succeeded