annotate src/share/native/sun/java2d/cmm/lcms/cmsopt.c @ 6295:2c71b4f2104b

8007929: Improve CurvesAlloc Reviewed-by: bae, prr Contributed-by: jia-hong.chen@oracle.com
author bae
date Thu, 21 Mar 2013 12:02:04 +0400
parents 6e0721fb94e1
children 7fa6d3ba2cc7
rev   line source
bae@2693 1 /*
bae@2693 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bae@2693 3 *
bae@2693 4 * This code is free software; you can redistribute it and/or modify it
bae@2693 5 * under the terms of the GNU General Public License version 2 only, as
bae@2693 6 * published by the Free Software Foundation. Oracle designates this
bae@2693 7 * particular file as subject to the "Classpath" exception as provided
bae@2693 8 * by Oracle in the LICENSE file that accompanied this code.
bae@2693 9 *
bae@2693 10 * This code is distributed in the hope that it will be useful, but WITHOUT
bae@2693 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bae@2693 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bae@2693 13 * version 2 for more details (a copy is included in the LICENSE file that
bae@2693 14 * accompanied this code).
bae@2693 15 *
bae@2693 16 * You should have received a copy of the GNU General Public License version
bae@2693 17 * 2 along with this work; if not, write to the Free Software Foundation,
bae@2693 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bae@2693 19 *
bae@2693 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bae@2693 21 * or visit www.oracle.com if you need additional information or have any
bae@2693 22 * questions.
bae@2693 23 */
bae@2693 24
bae@2693 25 // This file is available under and governed by the GNU General Public
bae@2693 26 // License version 2 only, as published by the Free Software Foundation.
bae@2693 27 // However, the following notice accompanied the original version of this
bae@2693 28 // file:
bae@2693 29 //
bae@2693 30 //---------------------------------------------------------------------------------
bae@2693 31 //
bae@2693 32 // Little Color Management System
bae@6274 33 // Copyright (c) 1998-2011 Marti Maria Saguer
bae@2693 34 //
bae@2693 35 // Permission is hereby granted, free of charge, to any person obtaining
bae@2693 36 // a copy of this software and associated documentation files (the "Software"),
bae@2693 37 // to deal in the Software without restriction, including without limitation
bae@2693 38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
bae@2693 39 // and/or sell copies of the Software, and to permit persons to whom the Software
bae@2693 40 // is furnished to do so, subject to the following conditions:
bae@2693 41 //
bae@2693 42 // The above copyright notice and this permission notice shall be included in
bae@2693 43 // all copies or substantial portions of the Software.
bae@2693 44 //
bae@2693 45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
bae@2693 46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
bae@2693 47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
bae@2693 48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
bae@2693 49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
bae@2693 50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
bae@2693 51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bae@2693 52 //
bae@2693 53 //---------------------------------------------------------------------------------
bae@2693 54 //
bae@2693 55
bae@2693 56 #include "lcms2_internal.h"
bae@2693 57
bae@2693 58
bae@2693 59 //----------------------------------------------------------------------------------
bae@2693 60
bae@2693 61 // Optimization for 8 bits, Shaper-CLUT (3 inputs only)
bae@2693 62 typedef struct {
bae@2693 63
bae@2693 64 cmsContext ContextID;
bae@2693 65
bae@2693 66 const cmsInterpParams* p; // Tetrahedrical interpolation parameters. This is a not-owned pointer.
bae@2693 67
bae@2693 68 cmsUInt16Number rx[256], ry[256], rz[256];
bae@2693 69 cmsUInt32Number X0[256], Y0[256], Z0[256]; // Precomputed nodes and offsets for 8-bit input data
bae@2693 70
bae@2693 71
bae@2693 72 } Prelin8Data;
bae@2693 73
bae@2693 74
bae@2693 75 // Generic optimization for 16 bits Shaper-CLUT-Shaper (any inputs)
bae@2693 76 typedef struct {
bae@2693 77
bae@2693 78 cmsContext ContextID;
bae@2693 79
bae@2693 80 // Number of channels
bae@2693 81 int nInputs;
bae@2693 82 int nOutputs;
bae@2693 83
bae@2693 84 // Since there is no limitation of the output number of channels, this buffer holding the connexion CLUT-shaper
bae@2693 85 // has to be dynamically allocated. This is not the case of first step shaper-CLUT, which is limited to max inputs
bae@2693 86 cmsUInt16Number* StageDEF;
bae@2693 87
bae@2693 88 _cmsInterpFn16 EvalCurveIn16[MAX_INPUT_DIMENSIONS]; // The maximum number of input channels is known in advance
bae@2693 89 cmsInterpParams* ParamsCurveIn16[MAX_INPUT_DIMENSIONS];
bae@2693 90
bae@2693 91 _cmsInterpFn16 EvalCLUT; // The evaluator for 3D grid
bae@2693 92 const cmsInterpParams* CLUTparams; // (not-owned pointer)
bae@2693 93
bae@2693 94
bae@2693 95 _cmsInterpFn16* EvalCurveOut16; // Points to an array of curve evaluators in 16 bits (not-owned pointer)
bae@2693 96 cmsInterpParams** ParamsCurveOut16; // Points to an array of references to interpolation params (not-owned pointer)
bae@2693 97
bae@2693 98
bae@2693 99 } Prelin16Data;
bae@2693 100
bae@2693 101
bae@2693 102 // Optimization for matrix-shaper in 8 bits. Numbers are operated in n.14 signed, tables are stored in 1.14 fixed
bae@2693 103
bae@2693 104 typedef cmsInt32Number cmsS1Fixed14Number; // Note that this may hold more than 16 bits!
bae@2693 105
bae@2693 106 #define DOUBLE_TO_1FIXED14(x) ((cmsS1Fixed14Number) floor((x) * 16384.0 + 0.5))
bae@2693 107
bae@2693 108 typedef struct {
bae@2693 109
bae@2693 110 cmsContext ContextID;
bae@2693 111
bae@2693 112 cmsS1Fixed14Number Shaper1R[256]; // from 0..255 to 1.14 (0.0...1.0)
bae@2693 113 cmsS1Fixed14Number Shaper1G[256];
bae@2693 114 cmsS1Fixed14Number Shaper1B[256];
bae@2693 115
bae@2693 116 cmsS1Fixed14Number Mat[3][3]; // n.14 to n.14 (needs a saturation after that)
bae@2693 117 cmsS1Fixed14Number Off[3];
bae@2693 118
bae@2693 119 cmsUInt16Number Shaper2R[16385]; // 1.14 to 0..255
bae@2693 120 cmsUInt16Number Shaper2G[16385];
bae@2693 121 cmsUInt16Number Shaper2B[16385];
bae@2693 122
bae@2693 123 } MatShaper8Data;
bae@2693 124
bae@2693 125 // Curves, optimization is shared between 8 and 16 bits
bae@2693 126 typedef struct {
bae@2693 127
bae@2693 128 cmsContext ContextID;
bae@2693 129
bae@2693 130 int nCurves; // Number of curves
bae@2693 131 int nElements; // Elements in curves
bae@2693 132 cmsUInt16Number** Curves; // Points to a dynamically allocated array
bae@2693 133
bae@2693 134 } Curves16Data;
bae@2693 135
bae@2693 136
bae@2693 137 // Simple optimizations ----------------------------------------------------------------------------------------------------------
bae@2693 138
bae@2693 139
bae@2693 140 // Remove an element in linked chain
bae@2693 141 static
bae@2693 142 void _RemoveElement(cmsStage** head)
bae@2693 143 {
bae@2693 144 cmsStage* mpe = *head;
bae@2693 145 cmsStage* next = mpe ->Next;
bae@2693 146 *head = next;
bae@2693 147 cmsStageFree(mpe);
bae@2693 148 }
bae@2693 149
bae@2693 150 // Remove all identities in chain. Note that pt actually is a double pointer to the element that holds the pointer.
bae@2693 151 static
bae@2693 152 cmsBool _Remove1Op(cmsPipeline* Lut, cmsStageSignature UnaryOp)
bae@2693 153 {
bae@2693 154 cmsStage** pt = &Lut ->Elements;
bae@2693 155 cmsBool AnyOpt = FALSE;
bae@2693 156
bae@2693 157 while (*pt != NULL) {
bae@2693 158
bae@2693 159 if ((*pt) ->Implements == UnaryOp) {
bae@2693 160 _RemoveElement(pt);
bae@2693 161 AnyOpt = TRUE;
bae@2693 162 }
bae@2693 163 else
bae@2693 164 pt = &((*pt) -> Next);
bae@2693 165 }
bae@2693 166
bae@2693 167 return AnyOpt;
bae@2693 168 }
bae@2693 169
bae@2693 170 // Same, but only if two adjacent elements are found
bae@2693 171 static
bae@2693 172 cmsBool _Remove2Op(cmsPipeline* Lut, cmsStageSignature Op1, cmsStageSignature Op2)
bae@2693 173 {
bae@2693 174 cmsStage** pt1;
bae@2693 175 cmsStage** pt2;
bae@2693 176 cmsBool AnyOpt = FALSE;
bae@2693 177
bae@2693 178 pt1 = &Lut ->Elements;
bae@2693 179 if (*pt1 == NULL) return AnyOpt;
bae@2693 180
bae@2693 181 while (*pt1 != NULL) {
bae@2693 182
bae@2693 183 pt2 = &((*pt1) -> Next);
bae@2693 184 if (*pt2 == NULL) return AnyOpt;
bae@2693 185
bae@2693 186 if ((*pt1) ->Implements == Op1 && (*pt2) ->Implements == Op2) {
bae@2693 187 _RemoveElement(pt2);
bae@2693 188 _RemoveElement(pt1);
bae@2693 189 AnyOpt = TRUE;
bae@2693 190 }
bae@2693 191 else
bae@2693 192 pt1 = &((*pt1) -> Next);
bae@2693 193 }
bae@2693 194
bae@2693 195 return AnyOpt;
bae@2693 196 }
bae@2693 197
bae@2693 198 // Preoptimize just gets rif of no-ops coming paired. Conversion from v2 to v4 followed
bae@2693 199 // by a v4 to v2 and vice-versa. The elements are then discarded.
bae@2693 200 static
bae@2693 201 cmsBool PreOptimize(cmsPipeline* Lut)
bae@2693 202 {
bae@2693 203 cmsBool AnyOpt = FALSE, Opt;
bae@2693 204
bae@2693 205 AnyOpt = FALSE;
bae@2693 206
bae@2693 207 do {
bae@2693 208
bae@2693 209 Opt = FALSE;
bae@2693 210
bae@2693 211 // Remove all identities
bae@2693 212 Opt |= _Remove1Op(Lut, cmsSigIdentityElemType);
bae@2693 213
bae@2693 214 // Remove XYZ2Lab followed by Lab2XYZ
bae@2693 215 Opt |= _Remove2Op(Lut, cmsSigXYZ2LabElemType, cmsSigLab2XYZElemType);
bae@2693 216
bae@2693 217 // Remove Lab2XYZ followed by XYZ2Lab
bae@2693 218 Opt |= _Remove2Op(Lut, cmsSigLab2XYZElemType, cmsSigXYZ2LabElemType);
bae@2693 219
bae@2693 220 // Remove V4 to V2 followed by V2 to V4
bae@2693 221 Opt |= _Remove2Op(Lut, cmsSigLabV4toV2, cmsSigLabV2toV4);
bae@2693 222
bae@2693 223 // Remove V2 to V4 followed by V4 to V2
bae@2693 224 Opt |= _Remove2Op(Lut, cmsSigLabV2toV4, cmsSigLabV4toV2);
bae@2693 225
bae@6274 226 // Remove float pcs Lab conversions
bae@6274 227 Opt |= _Remove2Op(Lut, cmsSigLab2FloatPCS, cmsSigFloatPCS2Lab);
bae@6274 228
bae@6274 229 // Remove float pcs Lab conversions
bae@6274 230 Opt |= _Remove2Op(Lut, cmsSigXYZ2FloatPCS, cmsSigFloatPCS2XYZ);
bae@6274 231
bae@2693 232 if (Opt) AnyOpt = TRUE;
bae@2693 233
bae@2693 234 } while (Opt);
bae@2693 235
bae@2693 236 return AnyOpt;
bae@2693 237 }
bae@2693 238
bae@2693 239 static
bae@2693 240 void Eval16nop1D(register const cmsUInt16Number Input[],
bae@2693 241 register cmsUInt16Number Output[],
bae@2693 242 register const struct _cms_interp_struc* p)
bae@2693 243 {
bae@2693 244 Output[0] = Input[0];
bae@2693 245
bae@2693 246 cmsUNUSED_PARAMETER(p);
bae@2693 247 }
bae@2693 248
bae@2693 249 static
bae@2693 250 void PrelinEval16(register const cmsUInt16Number Input[],
bae@2693 251 register cmsUInt16Number Output[],
bae@2693 252 register const void* D)
bae@2693 253 {
bae@2693 254 Prelin16Data* p16 = (Prelin16Data*) D;
bae@2693 255 cmsUInt16Number StageABC[MAX_INPUT_DIMENSIONS];
bae@2693 256 int i;
bae@2693 257
bae@2693 258 for (i=0; i < p16 ->nInputs; i++) {
bae@2693 259
bae@2693 260 p16 ->EvalCurveIn16[i](&Input[i], &StageABC[i], p16 ->ParamsCurveIn16[i]);
bae@2693 261 }
bae@2693 262
bae@2693 263 p16 ->EvalCLUT(StageABC, p16 ->StageDEF, p16 ->CLUTparams);
bae@2693 264
bae@2693 265 for (i=0; i < p16 ->nOutputs; i++) {
bae@2693 266
bae@2693 267 p16 ->EvalCurveOut16[i](&p16->StageDEF[i], &Output[i], p16 ->ParamsCurveOut16[i]);
bae@2693 268 }
bae@2693 269 }
bae@2693 270
bae@2693 271
bae@2693 272 static
bae@2693 273 void PrelinOpt16free(cmsContext ContextID, void* ptr)
bae@2693 274 {
bae@2693 275 Prelin16Data* p16 = (Prelin16Data*) ptr;
bae@2693 276
bae@2693 277 _cmsFree(ContextID, p16 ->StageDEF);
bae@2693 278 _cmsFree(ContextID, p16 ->EvalCurveOut16);
bae@2693 279 _cmsFree(ContextID, p16 ->ParamsCurveOut16);
bae@2693 280
bae@2693 281 _cmsFree(ContextID, p16);
bae@2693 282 }
bae@2693 283
bae@2693 284 static
bae@2693 285 void* Prelin16dup(cmsContext ContextID, const void* ptr)
bae@2693 286 {
bae@2693 287 Prelin16Data* p16 = (Prelin16Data*) ptr;
bae@2693 288 Prelin16Data* Duped = _cmsDupMem(ContextID, p16, sizeof(Prelin16Data));
bae@2693 289
bae@2693 290 if (Duped == NULL) return NULL;
bae@2693 291
bae@2693 292 Duped ->StageDEF = _cmsCalloc(ContextID, p16 ->nOutputs, sizeof(cmsUInt16Number));
bae@2693 293 Duped ->EvalCurveOut16 = _cmsDupMem(ContextID, p16 ->EvalCurveOut16, p16 ->nOutputs * sizeof(_cmsInterpFn16));
bae@2693 294 Duped ->ParamsCurveOut16 = _cmsDupMem(ContextID, p16 ->ParamsCurveOut16, p16 ->nOutputs * sizeof(cmsInterpParams* ));
bae@2693 295
bae@2693 296 return Duped;
bae@2693 297 }
bae@2693 298
bae@2693 299
bae@2693 300 static
bae@2693 301 Prelin16Data* PrelinOpt16alloc(cmsContext ContextID,
bae@2693 302 const cmsInterpParams* ColorMap,
bae@2693 303 int nInputs, cmsToneCurve** In,
bae@2693 304 int nOutputs, cmsToneCurve** Out )
bae@2693 305 {
bae@2693 306 int i;
bae@6274 307 Prelin16Data* p16 = _cmsMallocZero(ContextID, sizeof(Prelin16Data));
bae@2693 308 if (p16 == NULL) return NULL;
bae@2693 309
bae@2693 310 p16 ->nInputs = nInputs;
bae@2693 311 p16 -> nOutputs = nOutputs;
bae@2693 312
bae@2693 313
bae@2693 314 for (i=0; i < nInputs; i++) {
bae@2693 315
bae@2693 316 if (In == NULL) {
bae@2693 317 p16 -> ParamsCurveIn16[i] = NULL;
bae@2693 318 p16 -> EvalCurveIn16[i] = Eval16nop1D;
bae@2693 319
bae@2693 320 }
bae@2693 321 else {
bae@2693 322 p16 -> ParamsCurveIn16[i] = In[i] ->InterpParams;
bae@2693 323 p16 -> EvalCurveIn16[i] = p16 ->ParamsCurveIn16[i]->Interpolation.Lerp16;
bae@2693 324 }
bae@2693 325 }
bae@2693 326
bae@2693 327 p16 ->CLUTparams = ColorMap;
bae@2693 328 p16 ->EvalCLUT = ColorMap ->Interpolation.Lerp16;
bae@2693 329
bae@2693 330
bae@2693 331 p16 -> StageDEF = _cmsCalloc(ContextID, p16 ->nOutputs, sizeof(cmsUInt16Number));
bae@2693 332 p16 -> EvalCurveOut16 = (_cmsInterpFn16*) _cmsCalloc(ContextID, nOutputs, sizeof(_cmsInterpFn16));
bae@2693 333 p16 -> ParamsCurveOut16 = (cmsInterpParams**) _cmsCalloc(ContextID, nOutputs, sizeof(cmsInterpParams* ));
bae@2693 334
bae@2693 335 for (i=0; i < nOutputs; i++) {
bae@2693 336
bae@2693 337 if (Out == NULL) {
bae@2693 338 p16 ->ParamsCurveOut16[i] = NULL;
bae@2693 339 p16 -> EvalCurveOut16[i] = Eval16nop1D;
bae@2693 340 }
bae@2693 341 else {
bae@2693 342
bae@2693 343 p16 ->ParamsCurveOut16[i] = Out[i] ->InterpParams;
bae@2693 344 p16 -> EvalCurveOut16[i] = p16 ->ParamsCurveOut16[i]->Interpolation.Lerp16;
bae@2693 345 }
bae@2693 346 }
bae@2693 347
bae@2693 348 return p16;
bae@2693 349 }
bae@2693 350
bae@2693 351
bae@2693 352
bae@2693 353 // Resampling ---------------------------------------------------------------------------------
bae@2693 354
bae@2693 355 #define PRELINEARIZATION_POINTS 4096
bae@2693 356
bae@2693 357 // Sampler implemented by another LUT. This is a clean way to precalculate the devicelink 3D CLUT for
bae@2693 358 // almost any transform. We use floating point precision and then convert from floating point to 16 bits.
bae@2693 359 static
bae@2693 360 int XFormSampler16(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
bae@2693 361 {
bae@2693 362 cmsPipeline* Lut = (cmsPipeline*) Cargo;
bae@2693 363 cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
bae@2693 364 cmsUInt32Number i;
bae@2693 365
bae@2693 366 _cmsAssert(Lut -> InputChannels < cmsMAXCHANNELS);
bae@2693 367 _cmsAssert(Lut -> OutputChannels < cmsMAXCHANNELS);
bae@2693 368
bae@2693 369 // From 16 bit to floating point
bae@2693 370 for (i=0; i < Lut ->InputChannels; i++)
bae@2693 371 InFloat[i] = (cmsFloat32Number) (In[i] / 65535.0);
bae@2693 372
bae@2693 373 // Evaluate in floating point
bae@2693 374 cmsPipelineEvalFloat(InFloat, OutFloat, Lut);
bae@2693 375
bae@2693 376 // Back to 16 bits representation
bae@2693 377 for (i=0; i < Lut ->OutputChannels; i++)
bae@2693 378 Out[i] = _cmsQuickSaturateWord(OutFloat[i] * 65535.0);
bae@2693 379
bae@2693 380 // Always succeed
bae@2693 381 return TRUE;
bae@2693 382 }
bae@2693 383
bae@2693 384 // Try to see if the curves of a given MPE are linear
bae@2693 385 static
bae@2693 386 cmsBool AllCurvesAreLinear(cmsStage* mpe)
bae@2693 387 {
bae@2693 388 cmsToneCurve** Curves;
bae@2693 389 cmsUInt32Number i, n;
bae@2693 390
bae@2693 391 Curves = _cmsStageGetPtrToCurveSet(mpe);
bae@2693 392 if (Curves == NULL) return FALSE;
bae@2693 393
bae@2693 394 n = cmsStageOutputChannels(mpe);
bae@2693 395
bae@2693 396 for (i=0; i < n; i++) {
bae@2693 397 if (!cmsIsToneCurveLinear(Curves[i])) return FALSE;
bae@2693 398 }
bae@2693 399
bae@2693 400 return TRUE;
bae@2693 401 }
bae@2693 402
bae@2693 403 // This function replaces a specific node placed in "At" by the "Value" numbers. Its purpose
bae@2693 404 // is to fix scum dot on broken profiles/transforms. Works on 1, 3 and 4 channels
bae@2693 405 static
bae@2693 406 cmsBool PatchLUT(cmsStage* CLUT, cmsUInt16Number At[], cmsUInt16Number Value[],
bae@2693 407 int nChannelsOut, int nChannelsIn)
bae@2693 408 {
bae@2693 409 _cmsStageCLutData* Grid = (_cmsStageCLutData*) CLUT ->Data;
bae@2693 410 cmsInterpParams* p16 = Grid ->Params;
bae@2693 411 cmsFloat64Number px, py, pz, pw;
bae@2693 412 int x0, y0, z0, w0;
bae@2693 413 int i, index;
bae@2693 414
bae@2693 415 if (CLUT -> Type != cmsSigCLutElemType) {
bae@2693 416 cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) Attempt to PatchLUT on non-lut MPE");
bae@2693 417 return FALSE;
bae@2693 418 }
bae@2693 419
bae@6274 420 if (nChannelsIn == 4) {
bae@2693 421
bae@6274 422 px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
bae@6274 423 py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
bae@6274 424 pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
bae@6274 425 pw = ((cmsFloat64Number) At[3] * (p16->Domain[3])) / 65535.0;
bae@2693 426
bae@6274 427 x0 = (int) floor(px);
bae@6274 428 y0 = (int) floor(py);
bae@6274 429 z0 = (int) floor(pz);
bae@6274 430 w0 = (int) floor(pw);
bae@2693 431
bae@2693 432 if (((px - x0) != 0) ||
bae@2693 433 ((py - y0) != 0) ||
bae@2693 434 ((pz - z0) != 0) ||
bae@2693 435 ((pw - w0) != 0)) return FALSE; // Not on exact node
bae@2693 436
bae@2693 437 index = p16 -> opta[3] * x0 +
bae@6274 438 p16 -> opta[2] * y0 +
bae@6274 439 p16 -> opta[1] * z0 +
bae@6274 440 p16 -> opta[0] * w0;
bae@2693 441 }
bae@2693 442 else
bae@2693 443 if (nChannelsIn == 3) {
bae@2693 444
bae@6274 445 px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
bae@6274 446 py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
bae@6274 447 pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
bae@6274 448
bae@6274 449 x0 = (int) floor(px);
bae@6274 450 y0 = (int) floor(py);
bae@6274 451 z0 = (int) floor(pz);
bae@6274 452
bae@2693 453 if (((px - x0) != 0) ||
bae@2693 454 ((py - y0) != 0) ||
bae@2693 455 ((pz - z0) != 0)) return FALSE; // Not on exact node
bae@2693 456
bae@2693 457 index = p16 -> opta[2] * x0 +
bae@6274 458 p16 -> opta[1] * y0 +
bae@6274 459 p16 -> opta[0] * z0;
bae@2693 460 }
bae@2693 461 else
bae@2693 462 if (nChannelsIn == 1) {
bae@2693 463
bae@6274 464 px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
bae@6274 465
bae@6274 466 x0 = (int) floor(px);
bae@6274 467
bae@2693 468 if (((px - x0) != 0)) return FALSE; // Not on exact node
bae@2693 469
bae@2693 470 index = p16 -> opta[0] * x0;
bae@2693 471 }
bae@2693 472 else {
bae@2693 473 cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
bae@2693 474 return FALSE;
bae@2693 475 }
bae@2693 476
bae@2693 477 for (i=0; i < nChannelsOut; i++)
bae@2693 478 Grid -> Tab.T[index + i] = Value[i];
bae@2693 479
bae@2693 480 return TRUE;
bae@2693 481 }
bae@2693 482
bae@6274 483 // Auxiliar, to see if two values are equal or very different
bae@2693 484 static
bae@2693 485 cmsBool WhitesAreEqual(int n, cmsUInt16Number White1[], cmsUInt16Number White2[] )
bae@2693 486 {
bae@2693 487 int i;
bae@2693 488
bae@2693 489 for (i=0; i < n; i++) {
bae@6274 490
bae@6274 491 if (abs(White1[i] - White2[i]) > 0xf000) return TRUE; // Values are so extremly different that the fixup should be avoided
bae@2693 492 if (White1[i] != White2[i]) return FALSE;
bae@2693 493 }
bae@2693 494 return TRUE;
bae@2693 495 }
bae@2693 496
bae@2693 497
bae@2693 498 // Locate the node for the white point and fix it to pure white in order to avoid scum dot.
bae@2693 499 static
bae@2693 500 cmsBool FixWhiteMisalignment(cmsPipeline* Lut, cmsColorSpaceSignature EntryColorSpace, cmsColorSpaceSignature ExitColorSpace)
bae@2693 501 {
bae@2693 502 cmsUInt16Number *WhitePointIn, *WhitePointOut;
bae@2693 503 cmsUInt16Number WhiteIn[cmsMAXCHANNELS], WhiteOut[cmsMAXCHANNELS], ObtainedOut[cmsMAXCHANNELS];
bae@2693 504 cmsUInt32Number i, nOuts, nIns;
bae@2693 505 cmsStage *PreLin = NULL, *CLUT = NULL, *PostLin = NULL;
bae@2693 506
bae@2693 507 if (!_cmsEndPointsBySpace(EntryColorSpace,
bae@2693 508 &WhitePointIn, NULL, &nIns)) return FALSE;
bae@2693 509
bae@2693 510 if (!_cmsEndPointsBySpace(ExitColorSpace,
bae@2693 511 &WhitePointOut, NULL, &nOuts)) return FALSE;
bae@2693 512
bae@2693 513 // It needs to be fixed?
bae@6274 514 if (Lut ->InputChannels != nIns) return FALSE;
bae@6274 515 if (Lut ->OutputChannels != nOuts) return FALSE;
bae@2693 516
bae@2693 517 cmsPipelineEval16(WhitePointIn, ObtainedOut, Lut);
bae@2693 518
bae@2693 519 if (WhitesAreEqual(nOuts, WhitePointOut, ObtainedOut)) return TRUE; // whites already match
bae@2693 520
bae@2693 521 // Check if the LUT comes as Prelin, CLUT or Postlin. We allow all combinations
bae@2693 522 if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &PreLin, &CLUT, &PostLin))
bae@2693 523 if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCurveSetElemType, cmsSigCLutElemType, &PreLin, &CLUT))
bae@2693 524 if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCLutElemType, cmsSigCurveSetElemType, &CLUT, &PostLin))
bae@2693 525 if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCLutElemType, &CLUT))
bae@2693 526 return FALSE;
bae@2693 527
bae@2693 528 // We need to interpolate white points of both, pre and post curves
bae@2693 529 if (PreLin) {
bae@2693 530
bae@2693 531 cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PreLin);
bae@2693 532
bae@2693 533 for (i=0; i < nIns; i++) {
bae@2693 534 WhiteIn[i] = cmsEvalToneCurve16(Curves[i], WhitePointIn[i]);
bae@2693 535 }
bae@2693 536 }
bae@2693 537 else {
bae@2693 538 for (i=0; i < nIns; i++)
bae@2693 539 WhiteIn[i] = WhitePointIn[i];
bae@2693 540 }
bae@2693 541
bae@2693 542 // If any post-linearization, we need to find how is represented white before the curve, do
bae@2693 543 // a reverse interpolation in this case.
bae@2693 544 if (PostLin) {
bae@2693 545
bae@2693 546 cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PostLin);
bae@2693 547
bae@2693 548 for (i=0; i < nOuts; i++) {
bae@2693 549
bae@2693 550 cmsToneCurve* InversePostLin = cmsReverseToneCurve(Curves[i]);
bae@2693 551 WhiteOut[i] = cmsEvalToneCurve16(InversePostLin, WhitePointOut[i]);
bae@2693 552 cmsFreeToneCurve(InversePostLin);
bae@2693 553 }
bae@2693 554 }
bae@2693 555 else {
bae@2693 556 for (i=0; i < nOuts; i++)
bae@2693 557 WhiteOut[i] = WhitePointOut[i];
bae@2693 558 }
bae@2693 559
bae@2693 560 // Ok, proceed with patching. May fail and we don't care if it fails
bae@2693 561 PatchLUT(CLUT, WhiteIn, WhiteOut, nOuts, nIns);
bae@2693 562
bae@2693 563 return TRUE;
bae@2693 564 }
bae@2693 565
bae@2693 566 // -----------------------------------------------------------------------------------------------------------------------------------------------
bae@2693 567 // This function creates simple LUT from complex ones. The generated LUT has an optional set of
bae@2693 568 // prelinearization curves, a CLUT of nGridPoints and optional postlinearization tables.
bae@2693 569 // These curves have to exist in the original LUT in order to be used in the simplified output.
bae@2693 570 // Caller may also use the flags to allow this feature.
bae@2693 571 // LUTS with all curves will be simplified to a single curve. Parametric curves are lost.
bae@2693 572 // This function should be used on 16-bits LUTS only, as floating point losses precision when simplified
bae@2693 573 // -----------------------------------------------------------------------------------------------------------------------------------------------
bae@2693 574
bae@2693 575 static
bae@2693 576 cmsBool OptimizeByResampling(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
bae@2693 577 {
bae@2693 578 cmsPipeline* Src;
bae@2693 579 cmsPipeline* Dest;
bae@6274 580 cmsStage* mpe;
bae@2693 581 cmsStage* CLUT;
bae@2693 582 cmsStage *KeepPreLin = NULL, *KeepPostLin = NULL;
bae@2693 583 int nGridPoints;
bae@2693 584 cmsColorSpaceSignature ColorSpace, OutputColorSpace;
bae@2693 585 cmsStage *NewPreLin = NULL;
bae@2693 586 cmsStage *NewPostLin = NULL;
bae@2693 587 _cmsStageCLutData* DataCLUT;
bae@2693 588 cmsToneCurve** DataSetIn;
bae@2693 589 cmsToneCurve** DataSetOut;
bae@2693 590 Prelin16Data* p16;
bae@2693 591
bae@2693 592
bae@2693 593 // This is a loosy optimization! does not apply in floating-point cases
bae@2693 594 if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
bae@2693 595
bae@2693 596 ColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*InputFormat));
bae@2693 597 OutputColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*OutputFormat));
bae@2693 598 nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
bae@2693 599
bae@2693 600 // For empty LUTs, 2 points are enough
bae@2693 601 if (cmsPipelineStageCount(*Lut) == 0)
bae@2693 602 nGridPoints = 2;
bae@2693 603
bae@2693 604 Src = *Lut;
bae@2693 605
bae@6274 606 // Named color pipelines cannot be optimized either
bae@6274 607 for (mpe = cmsPipelineGetPtrToFirstStage(Src);
bae@6274 608 mpe != NULL;
bae@6274 609 mpe = cmsStageNext(mpe)) {
bae@6274 610 if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
bae@6274 611 }
bae@6274 612
bae@2693 613 // Allocate an empty LUT
bae@2693 614 Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
bae@2693 615 if (!Dest) return FALSE;
bae@2693 616
bae@2693 617 // Prelinearization tables are kept unless indicated by flags
bae@2693 618 if (*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION) {
bae@2693 619
bae@2693 620 // Get a pointer to the prelinearization element
bae@2693 621 cmsStage* PreLin = cmsPipelineGetPtrToFirstStage(Src);
bae@2693 622
bae@2693 623 // Check if suitable
bae@2693 624 if (PreLin ->Type == cmsSigCurveSetElemType) {
bae@2693 625
bae@2693 626 // Maybe this is a linear tram, so we can avoid the whole stuff
bae@2693 627 if (!AllCurvesAreLinear(PreLin)) {
bae@2693 628
bae@2693 629 // All seems ok, proceed.
bae@2693 630 NewPreLin = cmsStageDup(PreLin);
bae@2693 631 cmsPipelineInsertStage(Dest, cmsAT_BEGIN, NewPreLin);
bae@2693 632
bae@2693 633 // Remove prelinearization. Since we have duplicated the curve
bae@2693 634 // in destination LUT, the sampling shoud be applied after this stage.
bae@2693 635 cmsPipelineUnlinkStage(Src, cmsAT_BEGIN, &KeepPreLin);
bae@2693 636 }
bae@2693 637 }
bae@2693 638 }
bae@2693 639
bae@2693 640 // Allocate the CLUT
bae@2693 641 CLUT = cmsStageAllocCLut16bit(Src ->ContextID, nGridPoints, Src ->InputChannels, Src->OutputChannels, NULL);
bae@2693 642 if (CLUT == NULL) return FALSE;
bae@2693 643
bae@2693 644 // Add the CLUT to the destination LUT
bae@2693 645 cmsPipelineInsertStage(Dest, cmsAT_END, CLUT);
bae@2693 646
bae@2693 647 // Postlinearization tables are kept unless indicated by flags
bae@2693 648 if (*dwFlags & cmsFLAGS_CLUT_POST_LINEARIZATION) {
bae@2693 649
bae@2693 650 // Get a pointer to the postlinearization if present
bae@2693 651 cmsStage* PostLin = cmsPipelineGetPtrToLastStage(Src);
bae@2693 652
bae@2693 653 // Check if suitable
bae@2693 654 if (cmsStageType(PostLin) == cmsSigCurveSetElemType) {
bae@2693 655
bae@2693 656 // Maybe this is a linear tram, so we can avoid the whole stuff
bae@2693 657 if (!AllCurvesAreLinear(PostLin)) {
bae@2693 658
bae@2693 659 // All seems ok, proceed.
bae@2693 660 NewPostLin = cmsStageDup(PostLin);
bae@2693 661 cmsPipelineInsertStage(Dest, cmsAT_END, NewPostLin);
bae@2693 662
bae@2693 663 // In destination LUT, the sampling shoud be applied after this stage.
bae@2693 664 cmsPipelineUnlinkStage(Src, cmsAT_END, &KeepPostLin);
bae@2693 665 }
bae@2693 666 }
bae@2693 667 }
bae@2693 668
bae@2693 669 // Now its time to do the sampling. We have to ignore pre/post linearization
bae@2693 670 // The source LUT whithout pre/post curves is passed as parameter.
bae@2693 671 if (!cmsStageSampleCLut16bit(CLUT, XFormSampler16, (void*) Src, 0)) {
bae@2693 672
bae@2693 673 // Ops, something went wrong, Restore stages
bae@2693 674 if (KeepPreLin != NULL) cmsPipelineInsertStage(Src, cmsAT_BEGIN, KeepPreLin);
bae@2693 675 if (KeepPostLin != NULL) cmsPipelineInsertStage(Src, cmsAT_END, KeepPostLin);
bae@2693 676 cmsPipelineFree(Dest);
bae@2693 677 return FALSE;
bae@2693 678 }
bae@2693 679
bae@2693 680 // Done.
bae@2693 681
bae@2693 682 if (KeepPreLin != NULL) cmsStageFree(KeepPreLin);
bae@2693 683 if (KeepPostLin != NULL) cmsStageFree(KeepPostLin);
bae@2693 684 cmsPipelineFree(Src);
bae@2693 685
bae@2693 686 DataCLUT = (_cmsStageCLutData*) CLUT ->Data;
bae@2693 687
bae@2693 688 if (NewPreLin == NULL) DataSetIn = NULL;
bae@2693 689 else DataSetIn = ((_cmsStageToneCurvesData*) NewPreLin ->Data) ->TheCurves;
bae@2693 690
bae@2693 691 if (NewPostLin == NULL) DataSetOut = NULL;
bae@2693 692 else DataSetOut = ((_cmsStageToneCurvesData*) NewPostLin ->Data) ->TheCurves;
bae@2693 693
bae@2693 694
bae@2693 695 if (DataSetIn == NULL && DataSetOut == NULL) {
bae@2693 696
bae@2693 697 _cmsPipelineSetOptimizationParameters(Dest, (_cmsOPTeval16Fn) DataCLUT->Params->Interpolation.Lerp16, DataCLUT->Params, NULL, NULL);
bae@2693 698 }
bae@2693 699 else {
bae@2693 700
bae@2693 701 p16 = PrelinOpt16alloc(Dest ->ContextID,
bae@2693 702 DataCLUT ->Params,
bae@2693 703 Dest ->InputChannels,
bae@2693 704 DataSetIn,
bae@2693 705 Dest ->OutputChannels,
bae@2693 706 DataSetOut);
bae@2693 707
bae@2693 708
bae@2693 709 _cmsPipelineSetOptimizationParameters(Dest, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
bae@2693 710 }
bae@2693 711
bae@2693 712
bae@2693 713 // Don't fix white on absolute colorimetric
bae@2693 714 if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
bae@2693 715 *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
bae@2693 716
bae@2693 717 if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
bae@2693 718
bae@2693 719 FixWhiteMisalignment(Dest, ColorSpace, OutputColorSpace);
bae@2693 720 }
bae@2693 721
bae@2693 722 *Lut = Dest;
bae@2693 723 return TRUE;
bae@2693 724
bae@2693 725 cmsUNUSED_PARAMETER(Intent);
bae@2693 726 }
bae@2693 727
bae@2693 728
bae@2693 729 // -----------------------------------------------------------------------------------------------------------------------------------------------
bae@2693 730 // Fixes the gamma balancing of transform. This is described in my paper "Prelinearization Stages on
bae@2693 731 // Color-Management Application-Specific Integrated Circuits (ASICs)" presented at NIP24. It only works
bae@2693 732 // for RGB transforms. See the paper for more details
bae@2693 733 // -----------------------------------------------------------------------------------------------------------------------------------------------
bae@2693 734
bae@2693 735
bae@2693 736 // Normalize endpoints by slope limiting max and min. This assures endpoints as well.
bae@2693 737 // Descending curves are handled as well.
bae@2693 738 static
bae@2693 739 void SlopeLimiting(cmsToneCurve* g)
bae@2693 740 {
bae@2693 741 int BeginVal, EndVal;
bae@2693 742 int AtBegin = (int) floor((cmsFloat64Number) g ->nEntries * 0.02 + 0.5); // Cutoff at 2%
bae@2693 743 int AtEnd = g ->nEntries - AtBegin - 1; // And 98%
bae@2693 744 cmsFloat64Number Val, Slope, beta;
bae@2693 745 int i;
bae@2693 746
bae@2693 747 if (cmsIsToneCurveDescending(g)) {
bae@2693 748 BeginVal = 0xffff; EndVal = 0;
bae@2693 749 }
bae@2693 750 else {
bae@2693 751 BeginVal = 0; EndVal = 0xffff;
bae@2693 752 }
bae@2693 753
bae@2693 754 // Compute slope and offset for begin of curve
bae@2693 755 Val = g ->Table16[AtBegin];
bae@2693 756 Slope = (Val - BeginVal) / AtBegin;
bae@2693 757 beta = Val - Slope * AtBegin;
bae@2693 758
bae@2693 759 for (i=0; i < AtBegin; i++)
bae@2693 760 g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
bae@2693 761
bae@2693 762 // Compute slope and offset for the end
bae@2693 763 Val = g ->Table16[AtEnd];
bae@2693 764 Slope = (EndVal - Val) / AtBegin; // AtBegin holds the X interval, which is same in both cases
bae@2693 765 beta = Val - Slope * AtEnd;
bae@2693 766
bae@2693 767 for (i = AtEnd; i < (int) g ->nEntries; i++)
bae@2693 768 g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
bae@2693 769 }
bae@2693 770
bae@2693 771
bae@2693 772 // Precomputes tables for 8-bit on input devicelink.
bae@2693 773 static
bae@2693 774 Prelin8Data* PrelinOpt8alloc(cmsContext ContextID, const cmsInterpParams* p, cmsToneCurve* G[3])
bae@2693 775 {
bae@2693 776 int i;
bae@2693 777 cmsUInt16Number Input[3];
bae@2693 778 cmsS15Fixed16Number v1, v2, v3;
bae@2693 779 Prelin8Data* p8;
bae@2693 780
bae@2693 781 p8 = _cmsMallocZero(ContextID, sizeof(Prelin8Data));
bae@2693 782 if (p8 == NULL) return NULL;
bae@2693 783
bae@2693 784 // Since this only works for 8 bit input, values comes always as x * 257,
bae@2693 785 // we can safely take msb byte (x << 8 + x)
bae@2693 786
bae@2693 787 for (i=0; i < 256; i++) {
bae@2693 788
bae@2693 789 if (G != NULL) {
bae@2693 790
bae@2693 791 // Get 16-bit representation
bae@2693 792 Input[0] = cmsEvalToneCurve16(G[0], FROM_8_TO_16(i));
bae@2693 793 Input[1] = cmsEvalToneCurve16(G[1], FROM_8_TO_16(i));
bae@2693 794 Input[2] = cmsEvalToneCurve16(G[2], FROM_8_TO_16(i));
bae@2693 795 }
bae@2693 796 else {
bae@2693 797 Input[0] = FROM_8_TO_16(i);
bae@2693 798 Input[1] = FROM_8_TO_16(i);
bae@2693 799 Input[2] = FROM_8_TO_16(i);
bae@2693 800 }
bae@2693 801
bae@2693 802
bae@2693 803 // Move to 0..1.0 in fixed domain
bae@2693 804 v1 = _cmsToFixedDomain(Input[0] * p -> Domain[0]);
bae@2693 805 v2 = _cmsToFixedDomain(Input[1] * p -> Domain[1]);
bae@2693 806 v3 = _cmsToFixedDomain(Input[2] * p -> Domain[2]);
bae@2693 807
bae@2693 808 // Store the precalculated table of nodes
bae@2693 809 p8 ->X0[i] = (p->opta[2] * FIXED_TO_INT(v1));
bae@2693 810 p8 ->Y0[i] = (p->opta[1] * FIXED_TO_INT(v2));
bae@2693 811 p8 ->Z0[i] = (p->opta[0] * FIXED_TO_INT(v3));
bae@2693 812
bae@2693 813 // Store the precalculated table of offsets
bae@2693 814 p8 ->rx[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v1);
bae@2693 815 p8 ->ry[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v2);
bae@2693 816 p8 ->rz[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v3);
bae@2693 817 }
bae@2693 818
bae@2693 819 p8 ->ContextID = ContextID;
bae@2693 820 p8 ->p = p;
bae@2693 821
bae@2693 822 return p8;
bae@2693 823 }
bae@2693 824
bae@2693 825 static
bae@2693 826 void Prelin8free(cmsContext ContextID, void* ptr)
bae@2693 827 {
bae@2693 828 _cmsFree(ContextID, ptr);
bae@2693 829 }
bae@2693 830
bae@2693 831 static
bae@2693 832 void* Prelin8dup(cmsContext ContextID, const void* ptr)
bae@2693 833 {
bae@2693 834 return _cmsDupMem(ContextID, ptr, sizeof(Prelin8Data));
bae@2693 835 }
bae@2693 836
bae@2693 837
bae@2693 838
bae@2693 839 // A optimized interpolation for 8-bit input.
bae@2693 840 #define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
bae@2693 841 static
bae@2693 842 void PrelinEval8(register const cmsUInt16Number Input[],
bae@2693 843 register cmsUInt16Number Output[],
bae@2693 844 register const void* D)
bae@2693 845 {
bae@2693 846
bae@2693 847 cmsUInt8Number r, g, b;
bae@2693 848 cmsS15Fixed16Number rx, ry, rz;
bae@2693 849 cmsS15Fixed16Number c0, c1, c2, c3, Rest;
bae@6274 850 int OutChan;
bae@6274 851 register cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
bae@2693 852 Prelin8Data* p8 = (Prelin8Data*) D;
bae@2693 853 register const cmsInterpParams* p = p8 ->p;
bae@2693 854 int TotalOut = p -> nOutputs;
bae@2693 855 const cmsUInt16Number* LutTable = p -> Table;
bae@2693 856
bae@2693 857 r = Input[0] >> 8;
bae@2693 858 g = Input[1] >> 8;
bae@2693 859 b = Input[2] >> 8;
bae@2693 860
bae@2693 861 X0 = X1 = p8->X0[r];
bae@2693 862 Y0 = Y1 = p8->Y0[g];
bae@2693 863 Z0 = Z1 = p8->Z0[b];
bae@2693 864
bae@2693 865 rx = p8 ->rx[r];
bae@2693 866 ry = p8 ->ry[g];
bae@2693 867 rz = p8 ->rz[b];
bae@2693 868
bae@2693 869 X1 = X0 + ((rx == 0) ? 0 : p ->opta[2]);
bae@2693 870 Y1 = Y0 + ((ry == 0) ? 0 : p ->opta[1]);
bae@2693 871 Z1 = Z0 + ((rz == 0) ? 0 : p ->opta[0]);
bae@2693 872
bae@2693 873
bae@2693 874 // These are the 6 Tetrahedral
bae@2693 875 for (OutChan=0; OutChan < TotalOut; OutChan++) {
bae@2693 876
bae@2693 877 c0 = DENS(X0, Y0, Z0);
bae@2693 878
bae@2693 879 if (rx >= ry && ry >= rz)
bae@2693 880 {
bae@2693 881 c1 = DENS(X1, Y0, Z0) - c0;
bae@2693 882 c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
bae@2693 883 c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
bae@2693 884 }
bae@2693 885 else
bae@2693 886 if (rx >= rz && rz >= ry)
bae@2693 887 {
bae@2693 888 c1 = DENS(X1, Y0, Z0) - c0;
bae@2693 889 c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
bae@2693 890 c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
bae@2693 891 }
bae@2693 892 else
bae@2693 893 if (rz >= rx && rx >= ry)
bae@2693 894 {
bae@2693 895 c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
bae@2693 896 c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
bae@2693 897 c3 = DENS(X0, Y0, Z1) - c0;
bae@2693 898 }
bae@2693 899 else
bae@2693 900 if (ry >= rx && rx >= rz)
bae@2693 901 {
bae@2693 902 c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
bae@2693 903 c2 = DENS(X0, Y1, Z0) - c0;
bae@2693 904 c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
bae@2693 905 }
bae@2693 906 else
bae@2693 907 if (ry >= rz && rz >= rx)
bae@2693 908 {
bae@2693 909 c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
bae@2693 910 c2 = DENS(X0, Y1, Z0) - c0;
bae@2693 911 c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
bae@2693 912 }
bae@2693 913 else
bae@2693 914 if (rz >= ry && ry >= rx)
bae@2693 915 {
bae@2693 916 c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
bae@2693 917 c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
bae@2693 918 c3 = DENS(X0, Y0, Z1) - c0;
bae@2693 919 }
bae@2693 920 else {
bae@2693 921 c1 = c2 = c3 = 0;
bae@2693 922 }
bae@2693 923
bae@2693 924
bae@6274 925 Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
bae@6274 926 Output[OutChan] = (cmsUInt16Number)c0 + ((Rest + (Rest>>16))>>16);
bae@2693 927
bae@2693 928 }
bae@2693 929 }
bae@2693 930
bae@2693 931 #undef DENS
bae@2693 932
bae@6274 933
bae@6274 934 // Curves that contain wide empty areas are not optimizeable
bae@6274 935 static
bae@6274 936 cmsBool IsDegenerated(const cmsToneCurve* g)
bae@6274 937 {
bae@6274 938 int i, Zeros = 0, Poles = 0;
bae@6274 939 int nEntries = g ->nEntries;
bae@6274 940
bae@6274 941 for (i=0; i < nEntries; i++) {
bae@6274 942
bae@6274 943 if (g ->Table16[i] == 0x0000) Zeros++;
bae@6274 944 if (g ->Table16[i] == 0xffff) Poles++;
bae@6274 945 }
bae@6274 946
bae@6274 947 if (Zeros == 1 && Poles == 1) return FALSE; // For linear tables
bae@6274 948 if (Zeros > (nEntries / 4)) return TRUE; // Degenerated, mostly zeros
bae@6274 949 if (Poles > (nEntries / 4)) return TRUE; // Degenerated, mostly poles
bae@6274 950
bae@6274 951 return FALSE;
bae@6274 952 }
bae@6274 953
bae@2693 954 // --------------------------------------------------------------------------------------------------------------
bae@2693 955 // We need xput over here
bae@2693 956
bae@2693 957 static
bae@2693 958 cmsBool OptimizeByComputingLinearization(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
bae@2693 959 {
bae@2693 960 cmsPipeline* OriginalLut;
bae@2693 961 int nGridPoints;
bae@2693 962 cmsToneCurve *Trans[cmsMAXCHANNELS], *TransReverse[cmsMAXCHANNELS];
bae@2693 963 cmsUInt32Number t, i;
bae@2693 964 cmsFloat32Number v, In[cmsMAXCHANNELS], Out[cmsMAXCHANNELS];
bae@2693 965 cmsBool lIsSuitable, lIsLinear;
bae@2693 966 cmsPipeline* OptimizedLUT = NULL, *LutPlusCurves = NULL;
bae@2693 967 cmsStage* OptimizedCLUTmpe;
bae@2693 968 cmsColorSpaceSignature ColorSpace, OutputColorSpace;
bae@2693 969 cmsStage* OptimizedPrelinMpe;
bae@6274 970 cmsStage* mpe;
bae@2693 971 cmsToneCurve** OptimizedPrelinCurves;
bae@2693 972 _cmsStageCLutData* OptimizedPrelinCLUT;
bae@2693 973
bae@2693 974
bae@2693 975 // This is a loosy optimization! does not apply in floating-point cases
bae@2693 976 if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
bae@2693 977
bae@2693 978 // Only on RGB
bae@2693 979 if (T_COLORSPACE(*InputFormat) != PT_RGB) return FALSE;
bae@2693 980 if (T_COLORSPACE(*OutputFormat) != PT_RGB) return FALSE;
bae@2693 981
bae@2693 982
bae@2693 983 // On 16 bits, user has to specify the feature
bae@2693 984 if (!_cmsFormatterIs8bit(*InputFormat)) {
bae@2693 985 if (!(*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION)) return FALSE;
bae@2693 986 }
bae@2693 987
bae@2693 988 OriginalLut = *Lut;
bae@6274 989
bae@6274 990 // Named color pipelines cannot be optimized either
bae@6274 991 for (mpe = cmsPipelineGetPtrToFirstStage(OriginalLut);
bae@6274 992 mpe != NULL;
bae@6274 993 mpe = cmsStageNext(mpe)) {
bae@6274 994 if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
bae@6274 995 }
bae@6274 996
bae@2693 997 ColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*InputFormat));
bae@2693 998 OutputColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*OutputFormat));
bae@2693 999 nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
bae@2693 1000
bae@2693 1001 // Empty gamma containers
bae@2693 1002 memset(Trans, 0, sizeof(Trans));
bae@2693 1003 memset(TransReverse, 0, sizeof(TransReverse));
bae@2693 1004
bae@2693 1005 for (t = 0; t < OriginalLut ->InputChannels; t++) {
bae@2693 1006 Trans[t] = cmsBuildTabulatedToneCurve16(OriginalLut ->ContextID, PRELINEARIZATION_POINTS, NULL);
bae@2693 1007 if (Trans[t] == NULL) goto Error;
bae@2693 1008 }
bae@2693 1009
bae@2693 1010 // Populate the curves
bae@2693 1011 for (i=0; i < PRELINEARIZATION_POINTS; i++) {
bae@2693 1012
bae@2693 1013 v = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
bae@2693 1014
bae@2693 1015 // Feed input with a gray ramp
bae@2693 1016 for (t=0; t < OriginalLut ->InputChannels; t++)
bae@2693 1017 In[t] = v;
bae@2693 1018
bae@2693 1019 // Evaluate the gray value
bae@2693 1020 cmsPipelineEvalFloat(In, Out, OriginalLut);
bae@2693 1021
bae@2693 1022 // Store result in curve
bae@2693 1023 for (t=0; t < OriginalLut ->InputChannels; t++)
bae@2693 1024 Trans[t] ->Table16[i] = _cmsQuickSaturateWord(Out[t] * 65535.0);
bae@2693 1025 }
bae@2693 1026
bae@2693 1027 // Slope-limit the obtained curves
bae@2693 1028 for (t = 0; t < OriginalLut ->InputChannels; t++)
bae@2693 1029 SlopeLimiting(Trans[t]);
bae@2693 1030
bae@2693 1031 // Check for validity
bae@2693 1032 lIsSuitable = TRUE;
bae@2693 1033 lIsLinear = TRUE;
bae@2693 1034 for (t=0; (lIsSuitable && (t < OriginalLut ->InputChannels)); t++) {
bae@2693 1035
bae@2693 1036 // Exclude if already linear
bae@2693 1037 if (!cmsIsToneCurveLinear(Trans[t]))
bae@2693 1038 lIsLinear = FALSE;
bae@2693 1039
bae@2693 1040 // Exclude if non-monotonic
bae@2693 1041 if (!cmsIsToneCurveMonotonic(Trans[t]))
bae@2693 1042 lIsSuitable = FALSE;
bae@6274 1043
bae@6274 1044 if (IsDegenerated(Trans[t]))
bae@6274 1045 lIsSuitable = FALSE;
bae@2693 1046 }
bae@2693 1047
bae@2693 1048 // If it is not suitable, just quit
bae@2693 1049 if (!lIsSuitable) goto Error;
bae@2693 1050
bae@2693 1051 // Invert curves if possible
bae@2693 1052 for (t = 0; t < OriginalLut ->InputChannels; t++) {
bae@2693 1053 TransReverse[t] = cmsReverseToneCurveEx(PRELINEARIZATION_POINTS, Trans[t]);
bae@2693 1054 if (TransReverse[t] == NULL) goto Error;
bae@2693 1055 }
bae@2693 1056
bae@2693 1057 // Now inset the reversed curves at the begin of transform
bae@2693 1058 LutPlusCurves = cmsPipelineDup(OriginalLut);
bae@2693 1059 if (LutPlusCurves == NULL) goto Error;
bae@2693 1060
bae@2693 1061 cmsPipelineInsertStage(LutPlusCurves, cmsAT_BEGIN, cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, TransReverse));
bae@2693 1062
bae@2693 1063 // Create the result LUT
bae@2693 1064 OptimizedLUT = cmsPipelineAlloc(OriginalLut ->ContextID, OriginalLut ->InputChannels, OriginalLut ->OutputChannels);
bae@2693 1065 if (OptimizedLUT == NULL) goto Error;
bae@2693 1066
bae@2693 1067 OptimizedPrelinMpe = cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, Trans);
bae@2693 1068
bae@2693 1069 // Create and insert the curves at the beginning
bae@2693 1070 cmsPipelineInsertStage(OptimizedLUT, cmsAT_BEGIN, OptimizedPrelinMpe);
bae@2693 1071
bae@2693 1072 // Allocate the CLUT for result
bae@2693 1073 OptimizedCLUTmpe = cmsStageAllocCLut16bit(OriginalLut ->ContextID, nGridPoints, OriginalLut ->InputChannels, OriginalLut ->OutputChannels, NULL);
bae@2693 1074
bae@2693 1075 // Add the CLUT to the destination LUT
bae@2693 1076 cmsPipelineInsertStage(OptimizedLUT, cmsAT_END, OptimizedCLUTmpe);
bae@2693 1077
bae@2693 1078 // Resample the LUT
bae@2693 1079 if (!cmsStageSampleCLut16bit(OptimizedCLUTmpe, XFormSampler16, (void*) LutPlusCurves, 0)) goto Error;
bae@2693 1080
bae@2693 1081 // Free resources
bae@2693 1082 for (t = 0; t < OriginalLut ->InputChannels; t++) {
bae@2693 1083
bae@2693 1084 if (Trans[t]) cmsFreeToneCurve(Trans[t]);
bae@2693 1085 if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
bae@2693 1086 }
bae@2693 1087
bae@2693 1088 cmsPipelineFree(LutPlusCurves);
bae@2693 1089
bae@2693 1090
bae@2693 1091 OptimizedPrelinCurves = _cmsStageGetPtrToCurveSet(OptimizedPrelinMpe);
bae@2693 1092 OptimizedPrelinCLUT = (_cmsStageCLutData*) OptimizedCLUTmpe ->Data;
bae@2693 1093
bae@2693 1094 // Set the evaluator if 8-bit
bae@2693 1095 if (_cmsFormatterIs8bit(*InputFormat)) {
bae@2693 1096
bae@2693 1097 Prelin8Data* p8 = PrelinOpt8alloc(OptimizedLUT ->ContextID,
bae@2693 1098 OptimizedPrelinCLUT ->Params,
bae@2693 1099 OptimizedPrelinCurves);
bae@2693 1100 if (p8 == NULL) return FALSE;
bae@2693 1101
bae@2693 1102 _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval8, (void*) p8, Prelin8free, Prelin8dup);
bae@2693 1103
bae@2693 1104 }
bae@2693 1105 else
bae@2693 1106 {
bae@2693 1107 Prelin16Data* p16 = PrelinOpt16alloc(OptimizedLUT ->ContextID,
bae@2693 1108 OptimizedPrelinCLUT ->Params,
bae@2693 1109 3, OptimizedPrelinCurves, 3, NULL);
bae@2693 1110 if (p16 == NULL) return FALSE;
bae@2693 1111
bae@2693 1112 _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
bae@2693 1113
bae@2693 1114 }
bae@2693 1115
bae@2693 1116 // Don't fix white on absolute colorimetric
bae@2693 1117 if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
bae@2693 1118 *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
bae@2693 1119
bae@2693 1120 if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
bae@2693 1121
bae@2693 1122 if (!FixWhiteMisalignment(OptimizedLUT, ColorSpace, OutputColorSpace)) {
bae@2693 1123
bae@2693 1124 return FALSE;
bae@2693 1125 }
bae@2693 1126 }
bae@2693 1127
bae@2693 1128 // And return the obtained LUT
bae@2693 1129
bae@2693 1130 cmsPipelineFree(OriginalLut);
bae@2693 1131 *Lut = OptimizedLUT;
bae@2693 1132 return TRUE;
bae@2693 1133
bae@2693 1134 Error:
bae@2693 1135
bae@2693 1136 for (t = 0; t < OriginalLut ->InputChannels; t++) {
bae@2693 1137
bae@2693 1138 if (Trans[t]) cmsFreeToneCurve(Trans[t]);
bae@2693 1139 if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
bae@2693 1140 }
bae@2693 1141
bae@2693 1142 if (LutPlusCurves != NULL) cmsPipelineFree(LutPlusCurves);
bae@2693 1143 if (OptimizedLUT != NULL) cmsPipelineFree(OptimizedLUT);
bae@2693 1144
bae@2693 1145 return FALSE;
bae@2693 1146
bae@2693 1147 cmsUNUSED_PARAMETER(Intent);
bae@2693 1148 }
bae@2693 1149
bae@2693 1150
bae@2693 1151 // Curves optimizer ------------------------------------------------------------------------------------------------------------------
bae@2693 1152
bae@2693 1153 static
bae@2693 1154 void CurvesFree(cmsContext ContextID, void* ptr)
bae@2693 1155 {
bae@2693 1156 Curves16Data* Data = (Curves16Data*) ptr;
bae@2693 1157 int i;
bae@2693 1158
bae@2693 1159 for (i=0; i < Data -> nCurves; i++) {
bae@2693 1160
bae@2693 1161 _cmsFree(ContextID, Data ->Curves[i]);
bae@2693 1162 }
bae@2693 1163
bae@2693 1164 _cmsFree(ContextID, Data ->Curves);
bae@2693 1165 _cmsFree(ContextID, ptr);
bae@2693 1166 }
bae@2693 1167
bae@2693 1168 static
bae@2693 1169 void* CurvesDup(cmsContext ContextID, const void* ptr)
bae@2693 1170 {
bae@2693 1171 Curves16Data* Data = _cmsDupMem(ContextID, ptr, sizeof(Curves16Data));
bae@2693 1172 int i;
bae@2693 1173
bae@2693 1174 if (Data == NULL) return NULL;
bae@2693 1175
bae@2693 1176 Data ->Curves = _cmsDupMem(ContextID, Data ->Curves, Data ->nCurves * sizeof(cmsUInt16Number*));
bae@2693 1177
bae@2693 1178 for (i=0; i < Data -> nCurves; i++) {
bae@2693 1179 Data ->Curves[i] = _cmsDupMem(ContextID, Data ->Curves[i], Data -> nElements * sizeof(cmsUInt16Number));
bae@2693 1180 }
bae@2693 1181
bae@2693 1182 return (void*) Data;
bae@2693 1183 }
bae@2693 1184
bae@2693 1185 // Precomputes tables for 8-bit on input devicelink.
bae@2693 1186 static
bae@2693 1187 Curves16Data* CurvesAlloc(cmsContext ContextID, int nCurves, int nElements, cmsToneCurve** G)
bae@2693 1188 {
bae@2693 1189 int i, j;
bae@2693 1190 Curves16Data* c16;
bae@2693 1191
bae@2693 1192 c16 = _cmsMallocZero(ContextID, sizeof(Curves16Data));
bae@2693 1193 if (c16 == NULL) return NULL;
bae@2693 1194
bae@2693 1195 c16 ->nCurves = nCurves;
bae@2693 1196 c16 ->nElements = nElements;
bae@2693 1197
bae@2693 1198 c16 ->Curves = _cmsCalloc(ContextID, nCurves, sizeof(cmsUInt16Number*));
bae@2693 1199 if (c16 ->Curves == NULL) return NULL;
bae@2693 1200
bae@2693 1201 for (i=0; i < nCurves; i++) {
bae@2693 1202
bae@2693 1203 c16->Curves[i] = _cmsCalloc(ContextID, nElements, sizeof(cmsUInt16Number));
bae@6295 1204 if (c16->Curves[i] == NULL) {
bae@6295 1205 for (j=0; j < i; j++) {
bae@6295 1206 _cmsFree(ContextID, c16->Curves[j]);
bae@6295 1207 }
bae@6295 1208 _cmsFree(ContextID, c16->Curves);
bae@6295 1209 _cmsFree(ContextID, c16);
bae@6295 1210
bae@6295 1211 return NULL;
bae@6295 1212 }
bae@2693 1213
bae@2693 1214 if (nElements == 256) {
bae@2693 1215
bae@2693 1216 for (j=0; j < nElements; j++) {
bae@2693 1217
bae@2693 1218 c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], FROM_8_TO_16(j));
bae@2693 1219 }
bae@2693 1220 }
bae@2693 1221 else {
bae@2693 1222
bae@2693 1223 for (j=0; j < nElements; j++) {
bae@2693 1224 c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], (cmsUInt16Number) j);
bae@2693 1225 }
bae@2693 1226 }
bae@2693 1227 }
bae@2693 1228
bae@2693 1229 return c16;
bae@2693 1230 }
bae@2693 1231
bae@2693 1232 static
bae@2693 1233 void FastEvaluateCurves8(register const cmsUInt16Number In[],
bae@2693 1234 register cmsUInt16Number Out[],
bae@2693 1235 register const void* D)
bae@2693 1236 {
bae@2693 1237 Curves16Data* Data = (Curves16Data*) D;
bae@2693 1238 cmsUInt8Number x;
bae@2693 1239 int i;
bae@2693 1240
bae@2693 1241 for (i=0; i < Data ->nCurves; i++) {
bae@2693 1242
bae@2693 1243 x = (In[i] >> 8);
bae@2693 1244 Out[i] = Data -> Curves[i][x];
bae@2693 1245 }
bae@2693 1246 }
bae@2693 1247
bae@2693 1248
bae@2693 1249 static
bae@2693 1250 void FastEvaluateCurves16(register const cmsUInt16Number In[],
bae@2693 1251 register cmsUInt16Number Out[],
bae@2693 1252 register const void* D)
bae@2693 1253 {
bae@2693 1254 Curves16Data* Data = (Curves16Data*) D;
bae@2693 1255 int i;
bae@2693 1256
bae@2693 1257 for (i=0; i < Data ->nCurves; i++) {
bae@2693 1258 Out[i] = Data -> Curves[i][In[i]];
bae@2693 1259 }
bae@2693 1260 }
bae@2693 1261
bae@2693 1262
bae@2693 1263 static
bae@2693 1264 void FastIdentity16(register const cmsUInt16Number In[],
bae@2693 1265 register cmsUInt16Number Out[],
bae@2693 1266 register const void* D)
bae@2693 1267 {
bae@2693 1268 cmsPipeline* Lut = (cmsPipeline*) D;
bae@2693 1269 cmsUInt32Number i;
bae@2693 1270
bae@2693 1271 for (i=0; i < Lut ->InputChannels; i++) {
bae@2693 1272 Out[i] = In[i];
bae@2693 1273 }
bae@2693 1274 }
bae@2693 1275
bae@2693 1276
bae@2693 1277 // If the target LUT holds only curves, the optimization procedure is to join all those
bae@2693 1278 // curves together. That only works on curves and does not work on matrices.
bae@2693 1279 static
bae@2693 1280 cmsBool OptimizeByJoiningCurves(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
bae@2693 1281 {
bae@2693 1282 cmsToneCurve** GammaTables = NULL;
bae@2693 1283 cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
bae@2693 1284 cmsUInt32Number i, j;
bae@2693 1285 cmsPipeline* Src = *Lut;
bae@2693 1286 cmsPipeline* Dest = NULL;
bae@2693 1287 cmsStage* mpe;
bae@2693 1288 cmsStage* ObtainedCurves = NULL;
bae@2693 1289
bae@2693 1290
bae@2693 1291 // This is a loosy optimization! does not apply in floating-point cases
bae@2693 1292 if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
bae@2693 1293
bae@2693 1294 // Only curves in this LUT?
bae@2693 1295 for (mpe = cmsPipelineGetPtrToFirstStage(Src);
bae@2693 1296 mpe != NULL;
bae@2693 1297 mpe = cmsStageNext(mpe)) {
bae@2693 1298 if (cmsStageType(mpe) != cmsSigCurveSetElemType) return FALSE;
bae@2693 1299 }
bae@2693 1300
bae@2693 1301 // Allocate an empty LUT
bae@2693 1302 Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
bae@2693 1303 if (Dest == NULL) return FALSE;
bae@2693 1304
bae@2693 1305 // Create target curves
bae@2693 1306 GammaTables = (cmsToneCurve**) _cmsCalloc(Src ->ContextID, Src ->InputChannels, sizeof(cmsToneCurve*));
bae@2693 1307 if (GammaTables == NULL) goto Error;
bae@2693 1308
bae@2693 1309 for (i=0; i < Src ->InputChannels; i++) {
bae@2693 1310 GammaTables[i] = cmsBuildTabulatedToneCurve16(Src ->ContextID, PRELINEARIZATION_POINTS, NULL);
bae@2693 1311 if (GammaTables[i] == NULL) goto Error;
bae@2693 1312 }
bae@2693 1313
bae@2693 1314 // Compute 16 bit result by using floating point
bae@2693 1315 for (i=0; i < PRELINEARIZATION_POINTS; i++) {
bae@2693 1316
bae@2693 1317 for (j=0; j < Src ->InputChannels; j++)
bae@2693 1318 InFloat[j] = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
bae@2693 1319
bae@2693 1320 cmsPipelineEvalFloat(InFloat, OutFloat, Src);
bae@2693 1321
bae@2693 1322 for (j=0; j < Src ->InputChannels; j++)
bae@2693 1323 GammaTables[j] -> Table16[i] = _cmsQuickSaturateWord(OutFloat[j] * 65535.0);
bae@2693 1324 }
bae@2693 1325
bae@2693 1326 ObtainedCurves = cmsStageAllocToneCurves(Src ->ContextID, Src ->InputChannels, GammaTables);
bae@2693 1327 if (ObtainedCurves == NULL) goto Error;
bae@2693 1328
bae@2693 1329 for (i=0; i < Src ->InputChannels; i++) {
bae@2693 1330 cmsFreeToneCurve(GammaTables[i]);
bae@2693 1331 GammaTables[i] = NULL;
bae@2693 1332 }
bae@2693 1333
bae@2693 1334 if (GammaTables != NULL) _cmsFree(Src ->ContextID, GammaTables);
bae@2693 1335
bae@2693 1336 // Maybe the curves are linear at the end
bae@2693 1337 if (!AllCurvesAreLinear(ObtainedCurves)) {
bae@2693 1338
bae@2693 1339 cmsPipelineInsertStage(Dest, cmsAT_BEGIN, ObtainedCurves);
bae@2693 1340
bae@2693 1341 // If the curves are to be applied in 8 bits, we can save memory
bae@2693 1342 if (_cmsFormatterIs8bit(*InputFormat)) {
bae@2693 1343
bae@2693 1344 _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) ObtainedCurves ->Data;
bae@2693 1345 Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 256, Data ->TheCurves);
bae@2693 1346
bae@2693 1347 *dwFlags |= cmsFLAGS_NOCACHE;
bae@2693 1348 _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves8, c16, CurvesFree, CurvesDup);
bae@2693 1349
bae@2693 1350 }
bae@2693 1351 else {
bae@2693 1352
bae@2693 1353 _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) cmsStageData(ObtainedCurves);
bae@2693 1354 Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 65536, Data ->TheCurves);
bae@2693 1355
bae@2693 1356 *dwFlags |= cmsFLAGS_NOCACHE;
bae@2693 1357 _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves16, c16, CurvesFree, CurvesDup);
bae@2693 1358 }
bae@2693 1359 }
bae@2693 1360 else {
bae@2693 1361
bae@2693 1362 // LUT optimizes to nothing. Set the identity LUT
bae@2693 1363 cmsStageFree(ObtainedCurves);
bae@2693 1364
bae@2693 1365 cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageAllocIdentity(Dest ->ContextID, Src ->InputChannels));
bae@2693 1366
bae@2693 1367 *dwFlags |= cmsFLAGS_NOCACHE;
bae@2693 1368 _cmsPipelineSetOptimizationParameters(Dest, FastIdentity16, (void*) Dest, NULL, NULL);
bae@2693 1369 }
bae@2693 1370
bae@2693 1371 // We are done.
bae@2693 1372 cmsPipelineFree(Src);
bae@2693 1373 *Lut = Dest;
bae@2693 1374 return TRUE;
bae@2693 1375
bae@2693 1376 Error:
bae@2693 1377
bae@2693 1378 if (ObtainedCurves != NULL) cmsStageFree(ObtainedCurves);
bae@2693 1379 if (GammaTables != NULL) {
bae@2693 1380 for (i=0; i < Src ->InputChannels; i++) {
bae@2693 1381 if (GammaTables[i] != NULL) cmsFreeToneCurve(GammaTables[i]);
bae@2693 1382 }
bae@2693 1383
bae@2693 1384 _cmsFree(Src ->ContextID, GammaTables);
bae@2693 1385 }
bae@2693 1386
bae@2693 1387 if (Dest != NULL) cmsPipelineFree(Dest);
bae@2693 1388 return FALSE;
bae@2693 1389
bae@2693 1390 cmsUNUSED_PARAMETER(Intent);
bae@2693 1391 cmsUNUSED_PARAMETER(InputFormat);
bae@2693 1392 cmsUNUSED_PARAMETER(OutputFormat);
bae@2693 1393 cmsUNUSED_PARAMETER(dwFlags);
bae@2693 1394 }
bae@2693 1395
bae@2693 1396 // -------------------------------------------------------------------------------------------------------------------------------------
bae@2693 1397 // LUT is Shaper - Matrix - Matrix - Shaper, which is very frequent when combining two matrix-shaper profiles
bae@2693 1398
bae@2693 1399
bae@2693 1400 static
bae@2693 1401 void FreeMatShaper(cmsContext ContextID, void* Data)
bae@2693 1402 {
bae@2693 1403 if (Data != NULL) _cmsFree(ContextID, Data);
bae@2693 1404 }
bae@2693 1405
bae@2693 1406 static
bae@2693 1407 void* DupMatShaper(cmsContext ContextID, const void* Data)
bae@2693 1408 {
bae@2693 1409 return _cmsDupMem(ContextID, Data, sizeof(MatShaper8Data));
bae@2693 1410 }
bae@2693 1411
bae@2693 1412
bae@2693 1413 // A fast matrix-shaper evaluator for 8 bits. This is a bit ticky since I'm using 1.14 signed fixed point
bae@2693 1414 // to accomplish some performance. Actually it takes 256x3 16 bits tables and 16385 x 3 tables of 8 bits,
bae@2693 1415 // in total about 50K, and the performance boost is huge!
bae@2693 1416 static
bae@2693 1417 void MatShaperEval16(register const cmsUInt16Number In[],
bae@2693 1418 register cmsUInt16Number Out[],
bae@2693 1419 register const void* D)
bae@2693 1420 {
bae@2693 1421 MatShaper8Data* p = (MatShaper8Data*) D;
bae@2693 1422 cmsS1Fixed14Number l1, l2, l3, r, g, b;
bae@2693 1423 cmsUInt32Number ri, gi, bi;
bae@2693 1424
bae@2693 1425 // In this case (and only in this case!) we can use this simplification since
bae@2693 1426 // In[] is assured to come from a 8 bit number. (a << 8 | a)
bae@2693 1427 ri = In[0] & 0xFF;
bae@2693 1428 gi = In[1] & 0xFF;
bae@2693 1429 bi = In[2] & 0xFF;
bae@2693 1430
bae@2693 1431 // Across first shaper, which also converts to 1.14 fixed point
bae@2693 1432 r = p->Shaper1R[ri];
bae@2693 1433 g = p->Shaper1G[gi];
bae@2693 1434 b = p->Shaper1B[bi];
bae@2693 1435
bae@2693 1436 // Evaluate the matrix in 1.14 fixed point
bae@2693 1437 l1 = (p->Mat[0][0] * r + p->Mat[0][1] * g + p->Mat[0][2] * b + p->Off[0] + 0x2000) >> 14;
bae@2693 1438 l2 = (p->Mat[1][0] * r + p->Mat[1][1] * g + p->Mat[1][2] * b + p->Off[1] + 0x2000) >> 14;
bae@2693 1439 l3 = (p->Mat[2][0] * r + p->Mat[2][1] * g + p->Mat[2][2] * b + p->Off[2] + 0x2000) >> 14;
bae@2693 1440
bae@2693 1441 // Now we have to clip to 0..1.0 range
bae@2693 1442 ri = (l1 < 0) ? 0 : ((l1 > 16384) ? 16384 : l1);
bae@2693 1443 gi = (l2 < 0) ? 0 : ((l2 > 16384) ? 16384 : l2);
bae@2693 1444 bi = (l3 < 0) ? 0 : ((l3 > 16384) ? 16384 : l3);
bae@2693 1445
bae@2693 1446 // And across second shaper,
bae@2693 1447 Out[0] = p->Shaper2R[ri];
bae@2693 1448 Out[1] = p->Shaper2G[gi];
bae@2693 1449 Out[2] = p->Shaper2B[bi];
bae@2693 1450
bae@2693 1451 }
bae@2693 1452
bae@2693 1453 // This table converts from 8 bits to 1.14 after applying the curve
bae@2693 1454 static
bae@2693 1455 void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
bae@2693 1456 {
bae@2693 1457 int i;
bae@2693 1458 cmsFloat32Number R, y;
bae@2693 1459
bae@2693 1460 for (i=0; i < 256; i++) {
bae@2693 1461
bae@2693 1462 R = (cmsFloat32Number) (i / 255.0);
bae@2693 1463 y = cmsEvalToneCurveFloat(Curve, R);
bae@2693 1464
bae@2693 1465 Table[i] = DOUBLE_TO_1FIXED14(y);
bae@2693 1466 }
bae@2693 1467 }
bae@2693 1468
bae@2693 1469 // This table converts form 1.14 (being 0x4000 the last entry) to 8 bits after applying the curve
bae@2693 1470 static
bae@2693 1471 void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
bae@2693 1472 {
bae@2693 1473 int i;
bae@2693 1474 cmsFloat32Number R, Val;
bae@2693 1475
bae@2693 1476 for (i=0; i < 16385; i++) {
bae@2693 1477
bae@2693 1478 R = (cmsFloat32Number) (i / 16384.0);
bae@2693 1479 Val = cmsEvalToneCurveFloat(Curve, R); // Val comes 0..1.0
bae@2693 1480
bae@2693 1481 if (Is8BitsOutput) {
bae@2693 1482
bae@2693 1483 // If 8 bits output, we can optimize further by computing the / 257 part.
bae@2693 1484 // first we compute the resulting byte and then we store the byte times
bae@2693 1485 // 257. This quantization allows to round very quick by doing a >> 8, but
bae@2693 1486 // since the low byte is always equal to msb, we can do a & 0xff and this works!
bae@6274 1487 cmsUInt16Number w = _cmsQuickSaturateWord(Val * 65535.0);
bae@2693 1488 cmsUInt8Number b = FROM_16_TO_8(w);
bae@2693 1489
bae@2693 1490 Table[i] = FROM_8_TO_16(b);
bae@2693 1491 }
bae@6274 1492 else Table[i] = _cmsQuickSaturateWord(Val * 65535.0);
bae@2693 1493 }
bae@2693 1494 }
bae@2693 1495
bae@2693 1496 // Compute the matrix-shaper structure
bae@2693 1497 static
bae@2693 1498 cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, cmsVEC3* Off, cmsToneCurve* Curve2[3], cmsUInt32Number* OutputFormat)
bae@2693 1499 {
bae@2693 1500 MatShaper8Data* p;
bae@2693 1501 int i, j;
bae@2693 1502 cmsBool Is8Bits = _cmsFormatterIs8bit(*OutputFormat);
bae@2693 1503
bae@2693 1504 // Allocate a big chuck of memory to store precomputed tables
bae@2693 1505 p = (MatShaper8Data*) _cmsMalloc(Dest ->ContextID, sizeof(MatShaper8Data));
bae@2693 1506 if (p == NULL) return FALSE;
bae@2693 1507
bae@2693 1508 p -> ContextID = Dest -> ContextID;
bae@2693 1509
bae@2693 1510 // Precompute tables
bae@2693 1511 FillFirstShaper(p ->Shaper1R, Curve1[0]);
bae@2693 1512 FillFirstShaper(p ->Shaper1G, Curve1[1]);
bae@2693 1513 FillFirstShaper(p ->Shaper1B, Curve1[2]);
bae@2693 1514
bae@2693 1515 FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits);
bae@2693 1516 FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits);
bae@2693 1517 FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits);
bae@2693 1518
bae@2693 1519 // Convert matrix to nFixed14. Note that those values may take more than 16 bits as
bae@2693 1520 for (i=0; i < 3; i++) {
bae@2693 1521 for (j=0; j < 3; j++) {
bae@2693 1522 p ->Mat[i][j] = DOUBLE_TO_1FIXED14(Mat->v[i].n[j]);
bae@2693 1523 }
bae@2693 1524 }
bae@2693 1525
bae@2693 1526 for (i=0; i < 3; i++) {
bae@2693 1527
bae@2693 1528 if (Off == NULL) {
bae@2693 1529 p ->Off[i] = 0;
bae@2693 1530 }
bae@2693 1531 else {
bae@2693 1532 p ->Off[i] = DOUBLE_TO_1FIXED14(Off->n[i]);
bae@2693 1533 }
bae@2693 1534 }
bae@2693 1535
bae@2693 1536 // Mark as optimized for faster formatter
bae@2693 1537 if (Is8Bits)
bae@2693 1538 *OutputFormat |= OPTIMIZED_SH(1);
bae@2693 1539
bae@2693 1540 // Fill function pointers
bae@2693 1541 _cmsPipelineSetOptimizationParameters(Dest, MatShaperEval16, (void*) p, FreeMatShaper, DupMatShaper);
bae@2693 1542 return TRUE;
bae@2693 1543 }
bae@2693 1544
bae@2693 1545 // 8 bits on input allows matrix-shaper boot up to 25 Mpixels per second on RGB. That's fast!
bae@2693 1546 // TODO: Allow a third matrix for abs. colorimetric
bae@2693 1547 static
bae@2693 1548 cmsBool OptimizeMatrixShaper(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
bae@2693 1549 {
bae@2693 1550 cmsStage* Curve1, *Curve2;
bae@2693 1551 cmsStage* Matrix1, *Matrix2;
bae@2693 1552 _cmsStageMatrixData* Data1;
bae@2693 1553 _cmsStageMatrixData* Data2;
bae@2693 1554 cmsMAT3 res;
bae@2693 1555 cmsBool IdentityMat;
bae@2693 1556 cmsPipeline* Dest, *Src;
bae@2693 1557
bae@2693 1558 // Only works on RGB to RGB
bae@2693 1559 if (T_CHANNELS(*InputFormat) != 3 || T_CHANNELS(*OutputFormat) != 3) return FALSE;
bae@2693 1560
bae@2693 1561 // Only works on 8 bit input
bae@2693 1562 if (!_cmsFormatterIs8bit(*InputFormat)) return FALSE;
bae@2693 1563
bae@2693 1564 // Seems suitable, proceed
bae@2693 1565 Src = *Lut;
bae@2693 1566
bae@2693 1567 // Check for shaper-matrix-matrix-shaper structure, that is what this optimizer stands for
bae@2693 1568 if (!cmsPipelineCheckAndRetreiveStages(Src, 4,
bae@2693 1569 cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
bae@2693 1570 &Curve1, &Matrix1, &Matrix2, &Curve2)) return FALSE;
bae@2693 1571
bae@2693 1572 // Get both matrices
bae@2693 1573 Data1 = (_cmsStageMatrixData*) cmsStageData(Matrix1);
bae@2693 1574 Data2 = (_cmsStageMatrixData*) cmsStageData(Matrix2);
bae@2693 1575
bae@2693 1576 // Input offset should be zero
bae@2693 1577 if (Data1 ->Offset != NULL) return FALSE;
bae@2693 1578
bae@2693 1579 // Multiply both matrices to get the result
bae@2693 1580 _cmsMAT3per(&res, (cmsMAT3*) Data2 ->Double, (cmsMAT3*) Data1 ->Double);
bae@2693 1581
bae@2693 1582 // Now the result is in res + Data2 -> Offset. Maybe is a plain identity?
bae@2693 1583 IdentityMat = FALSE;
bae@2693 1584 if (_cmsMAT3isIdentity(&res) && Data2 ->Offset == NULL) {
bae@2693 1585
bae@2693 1586 // We can get rid of full matrix
bae@2693 1587 IdentityMat = TRUE;
bae@2693 1588 }
bae@2693 1589
bae@2693 1590 // Allocate an empty LUT
bae@2693 1591 Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
bae@2693 1592 if (!Dest) return FALSE;
bae@2693 1593
bae@2693 1594 // Assamble the new LUT
bae@2693 1595 cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageDup(Curve1));
bae@2693 1596 if (!IdentityMat)
bae@2693 1597 cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageAllocMatrix(Dest ->ContextID, 3, 3, (const cmsFloat64Number*) &res, Data2 ->Offset));
bae@2693 1598 cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageDup(Curve2));
bae@2693 1599
bae@2693 1600 // If identity on matrix, we can further optimize the curves, so call the join curves routine
bae@2693 1601 if (IdentityMat) {
bae@2693 1602
bae@2693 1603 OptimizeByJoiningCurves(&Dest, Intent, InputFormat, OutputFormat, dwFlags);
bae@2693 1604 }
bae@2693 1605 else {
bae@2693 1606 _cmsStageToneCurvesData* mpeC1 = (_cmsStageToneCurvesData*) cmsStageData(Curve1);
bae@2693 1607 _cmsStageToneCurvesData* mpeC2 = (_cmsStageToneCurvesData*) cmsStageData(Curve2);
bae@2693 1608
bae@2693 1609 // In this particular optimization, caché does not help as it takes more time to deal with
bae@2693 1610 // the caché that with the pixel handling
bae@2693 1611 *dwFlags |= cmsFLAGS_NOCACHE;
bae@2693 1612
bae@2693 1613 // Setup the optimizarion routines
bae@2693 1614 SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Data2 ->Offset, mpeC2->TheCurves, OutputFormat);
bae@2693 1615 }
bae@2693 1616
bae@2693 1617 cmsPipelineFree(Src);
bae@2693 1618 *Lut = Dest;
bae@2693 1619 return TRUE;
bae@2693 1620 }
bae@2693 1621
bae@2693 1622
bae@2693 1623 // -------------------------------------------------------------------------------------------------------------------------------------
bae@2693 1624 // Optimization plug-ins
bae@2693 1625
bae@2693 1626 // List of optimizations
bae@2693 1627 typedef struct _cmsOptimizationCollection_st {
bae@2693 1628
bae@2693 1629 _cmsOPToptimizeFn OptimizePtr;
bae@2693 1630
bae@2693 1631 struct _cmsOptimizationCollection_st *Next;
bae@2693 1632
bae@2693 1633 } _cmsOptimizationCollection;
bae@2693 1634
bae@2693 1635
bae@2693 1636 // The built-in list. We currently implement 4 types of optimizations. Joining of curves, matrix-shaper, linearization and resampling
bae@2693 1637 static _cmsOptimizationCollection DefaultOptimization[] = {
bae@2693 1638
bae@2693 1639 { OptimizeByJoiningCurves, &DefaultOptimization[1] },
bae@2693 1640 { OptimizeMatrixShaper, &DefaultOptimization[2] },
bae@2693 1641 { OptimizeByComputingLinearization, &DefaultOptimization[3] },
bae@2693 1642 { OptimizeByResampling, NULL }
bae@2693 1643 };
bae@2693 1644
bae@2693 1645 // The linked list head
bae@2693 1646 static _cmsOptimizationCollection* OptimizationCollection = DefaultOptimization;
bae@2693 1647
bae@2693 1648 // Register new ways to optimize
bae@2693 1649 cmsBool _cmsRegisterOptimizationPlugin(cmsPluginBase* Data)
bae@2693 1650 {
bae@2693 1651 cmsPluginOptimization* Plugin = (cmsPluginOptimization*) Data;
bae@2693 1652 _cmsOptimizationCollection* fl;
bae@2693 1653
bae@2693 1654 if (Data == NULL) {
bae@2693 1655
bae@2693 1656 OptimizationCollection = DefaultOptimization;
bae@2693 1657 return TRUE;
bae@2693 1658 }
bae@2693 1659
bae@2693 1660 // Optimizer callback is required
bae@2693 1661 if (Plugin ->OptimizePtr == NULL) return FALSE;
bae@2693 1662
bae@2693 1663 fl = (_cmsOptimizationCollection*) _cmsPluginMalloc(sizeof(_cmsOptimizationCollection));
bae@2693 1664 if (fl == NULL) return FALSE;
bae@2693 1665
bae@2693 1666 // Copy the parameters
bae@2693 1667 fl ->OptimizePtr = Plugin ->OptimizePtr;
bae@2693 1668
bae@2693 1669 // Keep linked list
bae@2693 1670 fl ->Next = OptimizationCollection;
bae@2693 1671 OptimizationCollection = fl;
bae@2693 1672
bae@2693 1673 // All is ok
bae@2693 1674 return TRUE;
bae@2693 1675 }
bae@2693 1676
bae@2693 1677 // The entry point for LUT optimization
bae@2693 1678 cmsBool _cmsOptimizePipeline(cmsPipeline** PtrLut,
bae@2693 1679 int Intent,
bae@2693 1680 cmsUInt32Number* InputFormat,
bae@2693 1681 cmsUInt32Number* OutputFormat,
bae@2693 1682 cmsUInt32Number* dwFlags)
bae@2693 1683 {
bae@2693 1684 _cmsOptimizationCollection* Opts;
bae@2693 1685 cmsBool AnySuccess = FALSE;
bae@2693 1686
bae@2693 1687 // A CLUT is being asked, so force this specific optimization
bae@2693 1688 if (*dwFlags & cmsFLAGS_FORCE_CLUT) {
bae@2693 1689
bae@2693 1690 PreOptimize(*PtrLut);
bae@2693 1691 return OptimizeByResampling(PtrLut, Intent, InputFormat, OutputFormat, dwFlags);
bae@2693 1692 }
bae@2693 1693
bae@2693 1694 // Anything to optimize?
bae@2693 1695 if ((*PtrLut) ->Elements == NULL) {
bae@2693 1696 _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
bae@2693 1697 return TRUE;
bae@2693 1698 }
bae@2693 1699
bae@2693 1700 // Try to get rid of identities and trivial conversions.
bae@2693 1701 AnySuccess = PreOptimize(*PtrLut);
bae@2693 1702
bae@2693 1703 // After removal do we end with an identity?
bae@2693 1704 if ((*PtrLut) ->Elements == NULL) {
bae@2693 1705 _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
bae@2693 1706 return TRUE;
bae@2693 1707 }
bae@2693 1708
bae@2693 1709 // Do not optimize, keep all precision
bae@2693 1710 if (*dwFlags & cmsFLAGS_NOOPTIMIZE)
bae@2693 1711 return FALSE;
bae@2693 1712
bae@2693 1713 // Try built-in optimizations and plug-in
bae@2693 1714 for (Opts = OptimizationCollection;
bae@2693 1715 Opts != NULL;
bae@2693 1716 Opts = Opts ->Next) {
bae@2693 1717
bae@2693 1718 // If one schema succeeded, we are done
bae@2693 1719 if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
bae@2693 1720
bae@2693 1721 return TRUE; // Optimized!
bae@2693 1722 }
bae@2693 1723 }
bae@2693 1724
bae@2693 1725 // Only simple optimizations succeeded
bae@2693 1726 return AnySuccess;
bae@2693 1727 }
bae@2693 1728
bae@6274 1729
bae@6274 1730