/work/workdir/UnpackedTarball/lcms2/src/cmslut.c
Line | Count | Source |
1 | | //--------------------------------------------------------------------------------- |
2 | | // |
3 | | // Little Color Management System |
4 | | // Copyright (c) 1998-2026 Marti Maria Saguer |
5 | | // |
6 | | // Permission is hereby granted, free of charge, to any person obtaining |
7 | | // a copy of this software and associated documentation files (the "Software"), |
8 | | // to deal in the Software without restriction, including without limitation |
9 | | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 | | // and/or sell copies of the Software, and to permit persons to whom the Software |
11 | | // is furnished to do so, subject to the following conditions: |
12 | | // |
13 | | // The above copyright notice and this permission notice shall be included in |
14 | | // all copies or substantial portions of the Software. |
15 | | // |
16 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
18 | | // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19 | | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
20 | | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21 | | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
22 | | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23 | | // |
24 | | //--------------------------------------------------------------------------------- |
25 | | // |
26 | | |
27 | | #include "lcms2_internal.h" |
28 | | |
29 | | |
30 | | // Allocates an empty multi profile element |
31 | | cmsStage* CMSEXPORT _cmsStageAllocPlaceholder(cmsContext ContextID, |
32 | | cmsStageSignature Type, |
33 | | cmsUInt32Number InputChannels, |
34 | | cmsUInt32Number OutputChannels, |
35 | | _cmsStageEvalFn EvalPtr, |
36 | | _cmsStageDupElemFn DupElemPtr, |
37 | | _cmsStageFreeElemFn FreePtr, |
38 | | void* Data) |
39 | 0 | { |
40 | 0 | cmsStage* ph = (cmsStage*) _cmsMallocZero(ContextID, sizeof(cmsStage)); |
41 | |
|
42 | 0 | if (ph == NULL) return NULL; |
43 | | |
44 | | |
45 | 0 | ph ->ContextID = ContextID; |
46 | |
|
47 | 0 | ph ->Type = Type; |
48 | 0 | ph ->Implements = Type; // By default, no clue on what is implementing |
49 | |
|
50 | 0 | ph ->InputChannels = InputChannels; |
51 | 0 | ph ->OutputChannels = OutputChannels; |
52 | 0 | ph ->EvalPtr = EvalPtr; |
53 | 0 | ph ->DupElemPtr = DupElemPtr; |
54 | 0 | ph ->FreePtr = FreePtr; |
55 | 0 | ph ->Data = Data; |
56 | |
|
57 | 0 | return ph; |
58 | 0 | } |
59 | | |
60 | | |
61 | | static |
62 | | void EvaluateIdentity(const cmsFloat32Number In[], |
63 | | cmsFloat32Number Out[], |
64 | | const cmsStage *mpe) |
65 | 0 | { |
66 | 0 | memmove(Out, In, mpe ->InputChannels * sizeof(cmsFloat32Number)); |
67 | 0 | } |
68 | | |
69 | | |
70 | | cmsStage* CMSEXPORT cmsStageAllocIdentity(cmsContext ContextID, cmsUInt32Number nChannels) |
71 | 0 | { |
72 | 0 | return _cmsStageAllocPlaceholder(ContextID, |
73 | 0 | cmsSigIdentityElemType, |
74 | 0 | nChannels, nChannels, |
75 | 0 | EvaluateIdentity, |
76 | 0 | NULL, |
77 | 0 | NULL, |
78 | 0 | NULL); |
79 | 0 | } |
80 | | |
81 | | // Conversion functions. From floating point to 16 bits |
82 | | static |
83 | | void FromFloatTo16(const cmsFloat32Number In[], cmsUInt16Number Out[], cmsUInt32Number n) |
84 | 0 | { |
85 | 0 | cmsUInt32Number i; |
86 | |
|
87 | 0 | for (i=0; i < n; i++) { |
88 | 0 | Out[i] = _cmsQuickSaturateWord(In[i] * 65535.0); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | // From 16 bits to floating point |
93 | | static |
94 | | void From16ToFloat(const cmsUInt16Number In[], cmsFloat32Number Out[], cmsUInt32Number n) |
95 | 0 | { |
96 | 0 | cmsUInt32Number i; |
97 | |
|
98 | 0 | for (i=0; i < n; i++) { |
99 | 0 | Out[i] = (cmsFloat32Number) In[i] / 65535.0F; |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | |
104 | | // This function is quite useful to analyze the structure of a LUT and retrieve the MPE elements |
105 | | // that conform the LUT. It should be called with the LUT, the number of expected elements and |
106 | | // then a list of expected types followed with a list of cmsFloat64Number pointers to MPE elements. If |
107 | | // the function founds a match with current pipeline, it fills the pointers and returns TRUE |
108 | | // if not, returns FALSE without touching anything. Setting pointers to NULL does bypass |
109 | | // the storage process. |
110 | | cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(const cmsPipeline* Lut, cmsUInt32Number n, ...) |
111 | 0 | { |
112 | 0 | va_list args; |
113 | 0 | cmsUInt32Number i; |
114 | 0 | cmsStage* mpe; |
115 | 0 | cmsStageSignature Type; |
116 | 0 | void** ElemPtr; |
117 | | |
118 | | // Make sure same number of elements |
119 | 0 | if (cmsPipelineStageCount(Lut) != n) return FALSE; |
120 | | |
121 | 0 | va_start(args, n); |
122 | | |
123 | | // Iterate across asked types |
124 | 0 | mpe = Lut ->Elements; |
125 | 0 | for (i=0; i < n; i++) { |
126 | | |
127 | | // Get asked type. cmsStageSignature is promoted to int by compiler |
128 | 0 | Type = (cmsStageSignature)va_arg(args, int); |
129 | 0 | if (mpe ->Type != Type) { |
130 | |
|
131 | 0 | va_end(args); // Mismatch. We are done. |
132 | 0 | return FALSE; |
133 | 0 | } |
134 | 0 | mpe = mpe ->Next; |
135 | 0 | } |
136 | | |
137 | | // Found a combination, fill pointers if not NULL |
138 | 0 | mpe = Lut ->Elements; |
139 | 0 | for (i=0; i < n; i++) { |
140 | |
|
141 | 0 | ElemPtr = va_arg(args, void**); |
142 | 0 | if (ElemPtr != NULL) |
143 | 0 | *ElemPtr = mpe; |
144 | |
|
145 | 0 | mpe = mpe ->Next; |
146 | 0 | } |
147 | |
|
148 | 0 | va_end(args); |
149 | 0 | return TRUE; |
150 | 0 | } |
151 | | |
152 | | // Below there are implementations for several types of elements. Each type may be implemented by a |
153 | | // evaluation function, a duplication function, a function to free resources and a constructor. |
154 | | |
155 | | // ************************************************************************************************* |
156 | | // Type cmsSigCurveSetElemType (curves) |
157 | | // ************************************************************************************************* |
158 | | |
159 | | cmsToneCurve** _cmsStageGetPtrToCurveSet(const cmsStage* mpe) |
160 | 0 | { |
161 | 0 | _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data; |
162 | |
|
163 | 0 | return Data ->TheCurves; |
164 | 0 | } |
165 | | |
166 | | static |
167 | | void EvaluateCurves(const cmsFloat32Number In[], |
168 | | cmsFloat32Number Out[], |
169 | | const cmsStage *mpe) |
170 | 0 | { |
171 | 0 | _cmsStageToneCurvesData* Data; |
172 | 0 | cmsUInt32Number i; |
173 | |
|
174 | 0 | _cmsAssert(mpe != NULL); |
175 | |
|
176 | 0 | Data = (_cmsStageToneCurvesData*) mpe ->Data; |
177 | 0 | if (Data == NULL) return; |
178 | | |
179 | 0 | if (Data ->TheCurves == NULL) return; |
180 | | |
181 | 0 | for (i=0; i < Data ->nCurves; i++) { |
182 | 0 | Out[i] = cmsEvalToneCurveFloat(Data ->TheCurves[i], In[i]); |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | static |
187 | | void CurveSetElemTypeFree(cmsStage* mpe) |
188 | 0 | { |
189 | 0 | _cmsStageToneCurvesData* Data; |
190 | 0 | cmsUInt32Number i; |
191 | |
|
192 | 0 | _cmsAssert(mpe != NULL); |
193 | |
|
194 | 0 | Data = (_cmsStageToneCurvesData*) mpe ->Data; |
195 | 0 | if (Data == NULL) return; |
196 | | |
197 | 0 | if (Data ->TheCurves != NULL) { |
198 | 0 | for (i=0; i < Data ->nCurves; i++) { |
199 | 0 | if (Data ->TheCurves[i] != NULL) |
200 | 0 | cmsFreeToneCurve(Data ->TheCurves[i]); |
201 | 0 | } |
202 | 0 | } |
203 | 0 | _cmsFree(mpe ->ContextID, Data ->TheCurves); |
204 | 0 | _cmsFree(mpe ->ContextID, Data); |
205 | 0 | } |
206 | | |
207 | | |
208 | | static |
209 | | void* CurveSetDup(cmsStage* mpe) |
210 | 0 | { |
211 | 0 | _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data; |
212 | 0 | _cmsStageToneCurvesData* NewElem; |
213 | 0 | cmsUInt32Number i; |
214 | |
|
215 | 0 | NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageToneCurvesData)); |
216 | 0 | if (NewElem == NULL) return NULL; |
217 | | |
218 | 0 | NewElem ->nCurves = Data ->nCurves; |
219 | 0 | NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(mpe ->ContextID, NewElem ->nCurves, sizeof(cmsToneCurve*)); |
220 | |
|
221 | 0 | if (NewElem ->TheCurves == NULL) goto Error; |
222 | | |
223 | 0 | for (i=0; i < NewElem ->nCurves; i++) { |
224 | | |
225 | | // Duplicate each curve. It may fail. |
226 | 0 | NewElem ->TheCurves[i] = cmsDupToneCurve(Data ->TheCurves[i]); |
227 | 0 | if (NewElem ->TheCurves[i] == NULL) goto Error; |
228 | | |
229 | |
|
230 | 0 | } |
231 | 0 | return (void*) NewElem; |
232 | | |
233 | 0 | Error: |
234 | |
|
235 | 0 | if (NewElem ->TheCurves != NULL) { |
236 | 0 | for (i=0; i < NewElem ->nCurves; i++) { |
237 | 0 | if (NewElem ->TheCurves[i]) |
238 | 0 | cmsFreeToneCurve(NewElem ->TheCurves[i]); |
239 | 0 | } |
240 | 0 | } |
241 | 0 | _cmsFree(mpe ->ContextID, NewElem ->TheCurves); |
242 | 0 | _cmsFree(mpe ->ContextID, NewElem); |
243 | 0 | return NULL; |
244 | 0 | } |
245 | | |
246 | | |
247 | | // Curves == NULL forces identity curves |
248 | | cmsStage* CMSEXPORT cmsStageAllocToneCurves(cmsContext ContextID, cmsUInt32Number nChannels, cmsToneCurve* const Curves[]) |
249 | 0 | { |
250 | 0 | cmsUInt32Number i; |
251 | 0 | _cmsStageToneCurvesData* NewElem; |
252 | 0 | cmsStage* NewMPE; |
253 | | |
254 | |
|
255 | 0 | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCurveSetElemType, nChannels, nChannels, |
256 | 0 | EvaluateCurves, CurveSetDup, CurveSetElemTypeFree, NULL ); |
257 | 0 | if (NewMPE == NULL) return NULL; |
258 | | |
259 | 0 | NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(ContextID, sizeof(_cmsStageToneCurvesData)); |
260 | 0 | if (NewElem == NULL) { |
261 | 0 | cmsStageFree(NewMPE); |
262 | 0 | return NULL; |
263 | 0 | } |
264 | | |
265 | 0 | NewMPE ->Data = (void*) NewElem; |
266 | |
|
267 | 0 | NewElem ->nCurves = nChannels; |
268 | 0 | NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(ContextID, nChannels, sizeof(cmsToneCurve*)); |
269 | 0 | if (NewElem ->TheCurves == NULL) { |
270 | 0 | cmsStageFree(NewMPE); |
271 | 0 | return NULL; |
272 | 0 | } |
273 | | |
274 | 0 | for (i=0; i < nChannels; i++) { |
275 | |
|
276 | 0 | if (Curves == NULL) { |
277 | 0 | NewElem ->TheCurves[i] = cmsBuildGamma(ContextID, 1.0); |
278 | 0 | } |
279 | 0 | else { |
280 | 0 | NewElem ->TheCurves[i] = cmsDupToneCurve(Curves[i]); |
281 | 0 | } |
282 | |
|
283 | 0 | if (NewElem ->TheCurves[i] == NULL) { |
284 | 0 | cmsStageFree(NewMPE); |
285 | 0 | return NULL; |
286 | 0 | } |
287 | |
|
288 | 0 | } |
289 | | |
290 | 0 | return NewMPE; |
291 | 0 | } |
292 | | |
293 | | |
294 | | // Create a bunch of identity curves |
295 | | cmsStage* CMSEXPORT _cmsStageAllocIdentityCurves(cmsContext ContextID, cmsUInt32Number nChannels) |
296 | 0 | { |
297 | 0 | cmsStage* mpe = cmsStageAllocToneCurves(ContextID, nChannels, NULL); |
298 | |
|
299 | 0 | if (mpe == NULL) return NULL; |
300 | 0 | mpe ->Implements = cmsSigIdentityElemType; |
301 | 0 | return mpe; |
302 | 0 | } |
303 | | |
304 | | |
305 | | // ************************************************************************************************* |
306 | | // Type cmsSigMatrixElemType (Matrices) |
307 | | // ************************************************************************************************* |
308 | | |
309 | | |
310 | | // Special care should be taken here because precision loss. A temporary cmsFloat64Number buffer is being used |
311 | | static |
312 | | void EvaluateMatrix(const cmsFloat32Number In[], |
313 | | cmsFloat32Number Out[], |
314 | | const cmsStage *mpe) |
315 | 0 | { |
316 | 0 | cmsUInt32Number i, j; |
317 | 0 | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
318 | 0 | cmsFloat64Number Tmp; |
319 | | |
320 | | // Input is already in 0..1.0 notation |
321 | 0 | for (i=0; i < mpe ->OutputChannels; i++) { |
322 | |
|
323 | 0 | Tmp = 0; |
324 | 0 | for (j=0; j < mpe->InputChannels; j++) { |
325 | 0 | Tmp += In[j] * Data->Double[i*mpe->InputChannels + j]; |
326 | 0 | } |
327 | |
|
328 | 0 | if (Data ->Offset != NULL) |
329 | 0 | Tmp += Data->Offset[i]; |
330 | |
|
331 | 0 | Out[i] = (cmsFloat32Number) Tmp; |
332 | 0 | } |
333 | | |
334 | | |
335 | | // Output in 0..1.0 domain |
336 | 0 | } |
337 | | |
338 | | |
339 | | // Duplicate a yet-existing matrix element |
340 | | static |
341 | | void* MatrixElemDup(cmsStage* mpe) |
342 | 0 | { |
343 | 0 | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
344 | 0 | _cmsStageMatrixData* NewElem; |
345 | 0 | cmsUInt32Number sz; |
346 | |
|
347 | 0 | NewElem = (_cmsStageMatrixData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageMatrixData)); |
348 | 0 | if (NewElem == NULL) return NULL; |
349 | | |
350 | 0 | sz = mpe ->InputChannels * mpe ->OutputChannels; |
351 | |
|
352 | 0 | NewElem ->Double = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID, Data ->Double, sz * sizeof(cmsFloat64Number)) ; |
353 | |
|
354 | 0 | if (Data ->Offset) |
355 | 0 | NewElem ->Offset = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID, |
356 | 0 | Data ->Offset, mpe -> OutputChannels * sizeof(cmsFloat64Number)) ; |
357 | |
|
358 | 0 | return (void*) NewElem; |
359 | 0 | } |
360 | | |
361 | | |
362 | | static |
363 | | void MatrixElemTypeFree(cmsStage* mpe) |
364 | 0 | { |
365 | 0 | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
366 | 0 | if (Data == NULL) |
367 | 0 | return; |
368 | 0 | if (Data ->Double) |
369 | 0 | _cmsFree(mpe ->ContextID, Data ->Double); |
370 | |
|
371 | 0 | if (Data ->Offset) |
372 | 0 | _cmsFree(mpe ->ContextID, Data ->Offset); |
373 | |
|
374 | 0 | _cmsFree(mpe ->ContextID, mpe ->Data); |
375 | 0 | } |
376 | | |
377 | | |
378 | | |
379 | | cmsStage* CMSEXPORT cmsStageAllocMatrix(cmsContext ContextID, cmsUInt32Number Rows, cmsUInt32Number Cols, |
380 | | const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset) |
381 | 0 | { |
382 | 0 | cmsUInt32Number i, n; |
383 | 0 | _cmsStageMatrixData* NewElem; |
384 | 0 | cmsStage* NewMPE; |
385 | |
|
386 | 0 | n = Rows * Cols; |
387 | | |
388 | | // Check for overflow |
389 | 0 | if (n == 0) return NULL; |
390 | 0 | if (n >= UINT_MAX / Cols) return NULL; |
391 | 0 | if (n >= UINT_MAX / Rows) return NULL; |
392 | 0 | if (n < Rows || n < Cols) return NULL; |
393 | | |
394 | 0 | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows, |
395 | 0 | EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL ); |
396 | 0 | if (NewMPE == NULL) return NULL; |
397 | | |
398 | | |
399 | 0 | NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData)); |
400 | 0 | if (NewElem == NULL) goto Error; |
401 | 0 | NewMPE->Data = (void*)NewElem; |
402 | |
|
403 | 0 | NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number)); |
404 | 0 | if (NewElem->Double == NULL) goto Error; |
405 | | |
406 | 0 | for (i=0; i < n; i++) { |
407 | 0 | NewElem ->Double[i] = Matrix[i]; |
408 | 0 | } |
409 | |
|
410 | 0 | if (Offset != NULL) { |
411 | |
|
412 | 0 | NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Rows, sizeof(cmsFloat64Number)); |
413 | 0 | if (NewElem->Offset == NULL) goto Error; |
414 | | |
415 | 0 | for (i=0; i < Rows; i++) { |
416 | 0 | NewElem ->Offset[i] = Offset[i]; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | 0 | return NewMPE; |
421 | | |
422 | 0 | Error: |
423 | 0 | cmsStageFree(NewMPE); |
424 | 0 | return NULL; |
425 | 0 | } |
426 | | |
427 | | |
428 | | // ************************************************************************************************* |
429 | | // Type cmsSigCLutElemType |
430 | | // ************************************************************************************************* |
431 | | |
432 | | |
433 | | // Evaluate in true floating point |
434 | | static |
435 | | void EvaluateCLUTfloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
436 | 0 | { |
437 | 0 | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
438 | |
|
439 | 0 | Data -> Params ->Interpolation.LerpFloat(In, Out, Data->Params); |
440 | 0 | } |
441 | | |
442 | | |
443 | | // Convert to 16 bits, evaluate, and back to floating point |
444 | | static |
445 | | void EvaluateCLUTfloatIn16(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
446 | 0 | { |
447 | 0 | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
448 | 0 | cmsUInt16Number In16[MAX_STAGE_CHANNELS], Out16[MAX_STAGE_CHANNELS]; |
449 | |
|
450 | 0 | _cmsAssert(mpe ->InputChannels <= MAX_STAGE_CHANNELS); |
451 | 0 | _cmsAssert(mpe ->OutputChannels <= MAX_STAGE_CHANNELS); |
452 | |
|
453 | 0 | FromFloatTo16(In, In16, mpe ->InputChannels); |
454 | 0 | Data -> Params ->Interpolation.Lerp16(In16, Out16, Data->Params); |
455 | 0 | From16ToFloat(Out16, Out, mpe ->OutputChannels); |
456 | 0 | } |
457 | | |
458 | | |
459 | | // Given an hypercube of b dimensions, with Dims[] number of nodes by dimension, calculate the total amount of nodes |
460 | | static |
461 | | cmsUInt32Number CubeSize(const cmsUInt32Number Dims[], cmsUInt32Number b) |
462 | 0 | { |
463 | 0 | cmsUInt32Number dim; |
464 | 0 | cmsUInt64Number rv; |
465 | |
|
466 | 0 | _cmsAssert(Dims != NULL); |
467 | |
|
468 | 0 | for (rv = 1; b > 0; b--) { |
469 | |
|
470 | 0 | dim = Dims[b-1]; |
471 | 0 | if (dim <= 1) return 0; |
472 | | |
473 | | // Check for overflow |
474 | 0 | if (rv > UINT_MAX / dim) return 0; |
475 | | |
476 | 0 | rv *= dim; |
477 | 0 | } |
478 | | |
479 | | // Again, prevent overflow |
480 | 0 | if (rv > UINT_MAX / 15) return 0; |
481 | | |
482 | 0 | return (cmsUInt32Number) rv; |
483 | 0 | } |
484 | | |
485 | | static |
486 | | void* CLUTElemDup(cmsStage* mpe) |
487 | 0 | { |
488 | 0 | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
489 | 0 | _cmsStageCLutData* NewElem; |
490 | | |
491 | |
|
492 | 0 | NewElem = (_cmsStageCLutData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageCLutData)); |
493 | 0 | if (NewElem == NULL) return NULL; |
494 | | |
495 | 0 | NewElem ->nEntries = Data ->nEntries; |
496 | 0 | NewElem ->HasFloatValues = Data ->HasFloatValues; |
497 | |
|
498 | 0 | if (Data ->Tab.T) { |
499 | |
|
500 | 0 | if (Data ->HasFloatValues) { |
501 | 0 | NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.TFloat, Data ->nEntries * sizeof (cmsFloat32Number)); |
502 | 0 | if (NewElem ->Tab.TFloat == NULL) |
503 | 0 | goto Error; |
504 | 0 | } else { |
505 | 0 | NewElem ->Tab.T = (cmsUInt16Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.T, Data ->nEntries * sizeof (cmsUInt16Number)); |
506 | 0 | if (NewElem ->Tab.T == NULL) |
507 | 0 | goto Error; |
508 | 0 | } |
509 | 0 | } |
510 | | |
511 | 0 | NewElem ->Params = _cmsComputeInterpParamsEx(mpe ->ContextID, |
512 | 0 | Data ->Params ->nSamples, |
513 | 0 | Data ->Params ->nInputs, |
514 | 0 | Data ->Params ->nOutputs, |
515 | 0 | NewElem ->Tab.T, |
516 | 0 | Data ->Params ->dwFlags); |
517 | 0 | if (NewElem->Params != NULL) |
518 | 0 | return (void*) NewElem; |
519 | 0 | Error: |
520 | 0 | if (NewElem->Tab.T) |
521 | | // This works for both types |
522 | 0 | _cmsFree(mpe ->ContextID, NewElem -> Tab.T); |
523 | 0 | _cmsFree(mpe ->ContextID, NewElem); |
524 | 0 | return NULL; |
525 | 0 | } |
526 | | |
527 | | |
528 | | static |
529 | | void CLutElemTypeFree(cmsStage* mpe) |
530 | 0 | { |
531 | |
|
532 | 0 | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
533 | | |
534 | | // Already empty |
535 | 0 | if (Data == NULL) return; |
536 | | |
537 | | // This works for both types |
538 | 0 | if (Data -> Tab.T) |
539 | 0 | _cmsFree(mpe ->ContextID, Data -> Tab.T); |
540 | |
|
541 | 0 | _cmsFreeInterpParams(Data ->Params); |
542 | 0 | _cmsFree(mpe ->ContextID, mpe ->Data); |
543 | 0 | } |
544 | | |
545 | | |
546 | | // Allocates a 16-bit multidimensional CLUT. This is evaluated at 16-bit precision. Table may have different |
547 | | // granularity on each dimension. |
548 | | cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID, |
549 | | const cmsUInt32Number clutPoints[], |
550 | | cmsUInt32Number inputChan, |
551 | | cmsUInt32Number outputChan, |
552 | | const cmsUInt16Number* Table) |
553 | 0 | { |
554 | 0 | cmsUInt32Number i, n; |
555 | 0 | _cmsStageCLutData* NewElem; |
556 | 0 | cmsStage* NewMPE; |
557 | |
|
558 | 0 | _cmsAssert(clutPoints != NULL); |
559 | |
|
560 | 0 | if (inputChan > MAX_INPUT_DIMENSIONS) { |
561 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS); |
562 | 0 | return NULL; |
563 | 0 | } |
564 | | |
565 | 0 | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan, |
566 | 0 | EvaluateCLUTfloatIn16, CLUTElemDup, CLutElemTypeFree, NULL ); |
567 | |
|
568 | 0 | if (NewMPE == NULL) return NULL; |
569 | | |
570 | 0 | NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData)); |
571 | 0 | if (NewElem == NULL) { |
572 | 0 | cmsStageFree(NewMPE); |
573 | 0 | return NULL; |
574 | 0 | } |
575 | | |
576 | 0 | NewMPE ->Data = (void*) NewElem; |
577 | |
|
578 | 0 | NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan); |
579 | 0 | NewElem -> HasFloatValues = FALSE; |
580 | |
|
581 | 0 | if (n == 0) { |
582 | 0 | cmsStageFree(NewMPE); |
583 | 0 | return NULL; |
584 | 0 | } |
585 | | |
586 | | |
587 | 0 | NewElem ->Tab.T = (cmsUInt16Number*) _cmsCalloc(ContextID, n, sizeof(cmsUInt16Number)); |
588 | 0 | if (NewElem ->Tab.T == NULL) { |
589 | 0 | cmsStageFree(NewMPE); |
590 | 0 | return NULL; |
591 | 0 | } |
592 | | |
593 | 0 | if (Table != NULL) { |
594 | 0 | for (i=0; i < n; i++) { |
595 | 0 | NewElem ->Tab.T[i] = Table[i]; |
596 | 0 | } |
597 | 0 | } |
598 | |
|
599 | 0 | NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.T, CMS_LERP_FLAGS_16BITS); |
600 | 0 | if (NewElem ->Params == NULL) { |
601 | 0 | cmsStageFree(NewMPE); |
602 | 0 | return NULL; |
603 | 0 | } |
604 | | |
605 | 0 | return NewMPE; |
606 | 0 | } |
607 | | |
608 | | cmsStage* CMSEXPORT cmsStageAllocCLut16bit(cmsContext ContextID, |
609 | | cmsUInt32Number nGridPoints, |
610 | | cmsUInt32Number inputChan, |
611 | | cmsUInt32Number outputChan, |
612 | | const cmsUInt16Number* Table) |
613 | 0 | { |
614 | 0 | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
615 | 0 | int i; |
616 | | |
617 | | // Our resulting LUT would be same gridpoints on all dimensions |
618 | 0 | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
619 | 0 | Dimensions[i] = nGridPoints; |
620 | |
|
621 | 0 | return cmsStageAllocCLut16bitGranular(ContextID, Dimensions, inputChan, outputChan, Table); |
622 | 0 | } |
623 | | |
624 | | |
625 | | cmsStage* CMSEXPORT cmsStageAllocCLutFloat(cmsContext ContextID, |
626 | | cmsUInt32Number nGridPoints, |
627 | | cmsUInt32Number inputChan, |
628 | | cmsUInt32Number outputChan, |
629 | | const cmsFloat32Number* Table) |
630 | 0 | { |
631 | 0 | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
632 | 0 | int i; |
633 | | |
634 | | // Our resulting LUT would be same gridpoints on all dimensions |
635 | 0 | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
636 | 0 | Dimensions[i] = nGridPoints; |
637 | |
|
638 | 0 | return cmsStageAllocCLutFloatGranular(ContextID, Dimensions, inputChan, outputChan, Table); |
639 | 0 | } |
640 | | |
641 | | |
642 | | |
643 | | cmsStage* CMSEXPORT cmsStageAllocCLutFloatGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table) |
644 | 0 | { |
645 | 0 | cmsUInt32Number i, n; |
646 | 0 | _cmsStageCLutData* NewElem; |
647 | 0 | cmsStage* NewMPE; |
648 | |
|
649 | 0 | _cmsAssert(clutPoints != NULL); |
650 | |
|
651 | 0 | if (inputChan > MAX_INPUT_DIMENSIONS) { |
652 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS); |
653 | 0 | return NULL; |
654 | 0 | } |
655 | | |
656 | 0 | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan, |
657 | 0 | EvaluateCLUTfloat, CLUTElemDup, CLutElemTypeFree, NULL); |
658 | 0 | if (NewMPE == NULL) return NULL; |
659 | | |
660 | | |
661 | 0 | NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData)); |
662 | 0 | if (NewElem == NULL) { |
663 | 0 | cmsStageFree(NewMPE); |
664 | 0 | return NULL; |
665 | 0 | } |
666 | | |
667 | 0 | NewMPE ->Data = (void*) NewElem; |
668 | | |
669 | | // There is a potential integer overflow on conputing n and nEntries. |
670 | 0 | NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan); |
671 | 0 | NewElem -> HasFloatValues = TRUE; |
672 | |
|
673 | 0 | if (n == 0) { |
674 | 0 | cmsStageFree(NewMPE); |
675 | 0 | return NULL; |
676 | 0 | } |
677 | | |
678 | 0 | NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat32Number)); |
679 | 0 | if (NewElem ->Tab.TFloat == NULL) { |
680 | 0 | cmsStageFree(NewMPE); |
681 | 0 | return NULL; |
682 | 0 | } |
683 | | |
684 | 0 | if (Table != NULL) { |
685 | 0 | for (i=0; i < n; i++) { |
686 | 0 | NewElem ->Tab.TFloat[i] = Table[i]; |
687 | 0 | } |
688 | 0 | } |
689 | |
|
690 | 0 | NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT); |
691 | 0 | if (NewElem ->Params == NULL) { |
692 | 0 | cmsStageFree(NewMPE); |
693 | 0 | return NULL; |
694 | 0 | } |
695 | | |
696 | 0 | return NewMPE; |
697 | 0 | } |
698 | | |
699 | | |
700 | | static |
701 | | int IdentitySampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo) |
702 | 0 | { |
703 | 0 | int nChan = *(int*) Cargo; |
704 | 0 | int i; |
705 | |
|
706 | 0 | for (i=0; i < nChan; i++) |
707 | 0 | Out[i] = In[i]; |
708 | |
|
709 | 0 | return 1; |
710 | 0 | } |
711 | | |
712 | | // Creates an MPE that just copies input to output |
713 | | cmsStage* CMSEXPORT _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan) |
714 | 0 | { |
715 | 0 | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
716 | 0 | cmsStage* mpe ; |
717 | 0 | int i; |
718 | |
|
719 | 0 | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
720 | 0 | Dimensions[i] = 2; |
721 | |
|
722 | 0 | mpe = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, nChan, nChan, NULL); |
723 | 0 | if (mpe == NULL) return NULL; |
724 | | |
725 | 0 | if (!cmsStageSampleCLut16bit(mpe, IdentitySampler, &nChan, 0)) { |
726 | 0 | cmsStageFree(mpe); |
727 | 0 | return NULL; |
728 | 0 | } |
729 | | |
730 | 0 | mpe ->Implements = cmsSigIdentityElemType; |
731 | 0 | return mpe; |
732 | 0 | } |
733 | | |
734 | | |
735 | | |
736 | | // Quantize a value 0 <= i < MaxSamples to 0..0xffff |
737 | | cmsUInt16Number CMSEXPORT _cmsQuantizeVal(cmsFloat64Number i, cmsUInt32Number MaxSamples) |
738 | 0 | { |
739 | 0 | cmsFloat64Number x; |
740 | |
|
741 | 0 | x = ((cmsFloat64Number) i * 65535.) / (cmsFloat64Number) (MaxSamples - 1); |
742 | 0 | return _cmsQuickSaturateWord(x); |
743 | 0 | } |
744 | | |
745 | | |
746 | | // This routine does a sweep on whole input space, and calls its callback |
747 | | // function on knots. returns TRUE if all ok, FALSE otherwise. |
748 | | cmsBool CMSEXPORT cmsStageSampleCLut16bit(cmsStage* mpe, cmsSAMPLER16 Sampler, void * Cargo, cmsUInt32Number dwFlags) |
749 | 0 | { |
750 | 0 | int i, t, index, rest; |
751 | 0 | cmsUInt32Number nTotalPoints; |
752 | 0 | cmsUInt32Number nInputs, nOutputs; |
753 | 0 | cmsUInt32Number* nSamples; |
754 | 0 | cmsUInt16Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS]; |
755 | 0 | _cmsStageCLutData* clut; |
756 | |
|
757 | 0 | if (mpe == NULL) return FALSE; |
758 | | |
759 | 0 | clut = (_cmsStageCLutData*) mpe->Data; |
760 | |
|
761 | 0 | if (clut == NULL) return FALSE; |
762 | | |
763 | 0 | nSamples = clut->Params ->nSamples; |
764 | 0 | nInputs = clut->Params ->nInputs; |
765 | 0 | nOutputs = clut->Params ->nOutputs; |
766 | |
|
767 | 0 | if (nInputs <= 0) return FALSE; |
768 | 0 | if (nOutputs <= 0) return FALSE; |
769 | 0 | if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE; |
770 | 0 | if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE; |
771 | | |
772 | 0 | memset(In, 0, sizeof(In)); |
773 | 0 | memset(Out, 0, sizeof(Out)); |
774 | |
|
775 | 0 | nTotalPoints = CubeSize(nSamples, nInputs); |
776 | 0 | if (nTotalPoints == 0) return FALSE; |
777 | | |
778 | 0 | index = 0; |
779 | 0 | for (i = 0; i < (int) nTotalPoints; i++) { |
780 | |
|
781 | 0 | rest = i; |
782 | 0 | for (t = (int)nInputs - 1; t >= 0; --t) { |
783 | |
|
784 | 0 | cmsUInt32Number Colorant = rest % nSamples[t]; |
785 | |
|
786 | 0 | rest /= nSamples[t]; |
787 | |
|
788 | 0 | In[t] = _cmsQuantizeVal(Colorant, nSamples[t]); |
789 | 0 | } |
790 | |
|
791 | 0 | if (clut ->Tab.T != NULL) { |
792 | 0 | for (t = 0; t < (int)nOutputs; t++) |
793 | 0 | Out[t] = clut->Tab.T[index + t]; |
794 | 0 | } |
795 | |
|
796 | 0 | if (!Sampler(In, Out, Cargo)) |
797 | 0 | return FALSE; |
798 | | |
799 | 0 | if (!(dwFlags & SAMPLER_INSPECT)) { |
800 | |
|
801 | 0 | if (clut ->Tab.T != NULL) { |
802 | 0 | for (t=0; t < (int) nOutputs; t++) |
803 | 0 | clut->Tab.T[index + t] = Out[t]; |
804 | 0 | } |
805 | 0 | } |
806 | |
|
807 | 0 | index += nOutputs; |
808 | 0 | } |
809 | | |
810 | 0 | return TRUE; |
811 | 0 | } |
812 | | |
813 | | // Same as anterior, but for floating point |
814 | | cmsBool CMSEXPORT cmsStageSampleCLutFloat(cmsStage* mpe, cmsSAMPLERFLOAT Sampler, void * Cargo, cmsUInt32Number dwFlags) |
815 | 0 | { |
816 | 0 | int i, t, index, rest; |
817 | 0 | cmsUInt32Number nTotalPoints; |
818 | 0 | cmsUInt32Number nInputs, nOutputs; |
819 | 0 | cmsUInt32Number* nSamples; |
820 | 0 | cmsFloat32Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS]; |
821 | 0 | _cmsStageCLutData* clut; |
822 | |
|
823 | 0 | if (mpe == NULL) return FALSE; |
824 | | |
825 | 0 | clut = (_cmsStageCLutData*)mpe->Data; |
826 | |
|
827 | 0 | if (clut == NULL) return FALSE; |
828 | | |
829 | 0 | nSamples = clut->Params ->nSamples; |
830 | 0 | nInputs = clut->Params ->nInputs; |
831 | 0 | nOutputs = clut->Params ->nOutputs; |
832 | |
|
833 | 0 | if (nInputs <= 0) return FALSE; |
834 | 0 | if (nOutputs <= 0) return FALSE; |
835 | 0 | if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE; |
836 | 0 | if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE; |
837 | | |
838 | 0 | nTotalPoints = CubeSize(nSamples, nInputs); |
839 | 0 | if (nTotalPoints == 0) return FALSE; |
840 | | |
841 | 0 | index = 0; |
842 | 0 | for (i = 0; i < (int)nTotalPoints; i++) { |
843 | |
|
844 | 0 | rest = i; |
845 | 0 | for (t = (int) nInputs-1; t >=0; --t) { |
846 | |
|
847 | 0 | cmsUInt32Number Colorant = rest % nSamples[t]; |
848 | |
|
849 | 0 | rest /= nSamples[t]; |
850 | |
|
851 | 0 | In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, nSamples[t]) / 65535.0); |
852 | 0 | } |
853 | |
|
854 | 0 | if (clut ->Tab.TFloat != NULL) { |
855 | 0 | for (t=0; t < (int) nOutputs; t++) |
856 | 0 | Out[t] = clut->Tab.TFloat[index + t]; |
857 | 0 | } |
858 | |
|
859 | 0 | if (!Sampler(In, Out, Cargo)) |
860 | 0 | return FALSE; |
861 | | |
862 | 0 | if (!(dwFlags & SAMPLER_INSPECT)) { |
863 | |
|
864 | 0 | if (clut ->Tab.TFloat != NULL) { |
865 | 0 | for (t=0; t < (int) nOutputs; t++) |
866 | 0 | clut->Tab.TFloat[index + t] = Out[t]; |
867 | 0 | } |
868 | 0 | } |
869 | |
|
870 | 0 | index += nOutputs; |
871 | 0 | } |
872 | | |
873 | 0 | return TRUE; |
874 | 0 | } |
875 | | |
876 | | |
877 | | |
878 | | // This routine does a sweep on whole input space, and calls its callback |
879 | | // function on knots. returns TRUE if all ok, FALSE otherwise. |
880 | | cmsBool CMSEXPORT cmsSliceSpace16(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[], |
881 | | cmsSAMPLER16 Sampler, void * Cargo) |
882 | 0 | { |
883 | 0 | int i, t, rest; |
884 | 0 | cmsUInt32Number nTotalPoints; |
885 | 0 | cmsUInt16Number In[cmsMAXCHANNELS]; |
886 | |
|
887 | 0 | if (nInputs >= cmsMAXCHANNELS) return FALSE; |
888 | | |
889 | 0 | nTotalPoints = CubeSize(clutPoints, nInputs); |
890 | 0 | if (nTotalPoints == 0) return FALSE; |
891 | | |
892 | 0 | for (i = 0; i < (int) nTotalPoints; i++) { |
893 | |
|
894 | 0 | rest = i; |
895 | 0 | for (t = (int) nInputs-1; t >=0; --t) { |
896 | |
|
897 | 0 | cmsUInt32Number Colorant = rest % clutPoints[t]; |
898 | |
|
899 | 0 | rest /= clutPoints[t]; |
900 | 0 | In[t] = _cmsQuantizeVal(Colorant, clutPoints[t]); |
901 | |
|
902 | 0 | } |
903 | |
|
904 | 0 | if (!Sampler(In, NULL, Cargo)) |
905 | 0 | return FALSE; |
906 | 0 | } |
907 | | |
908 | 0 | return TRUE; |
909 | 0 | } |
910 | | |
911 | | cmsInt32Number CMSEXPORT cmsSliceSpaceFloat(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[], |
912 | | cmsSAMPLERFLOAT Sampler, void * Cargo) |
913 | 0 | { |
914 | 0 | int i, t, rest; |
915 | 0 | cmsUInt32Number nTotalPoints; |
916 | 0 | cmsFloat32Number In[cmsMAXCHANNELS]; |
917 | |
|
918 | 0 | if (nInputs >= cmsMAXCHANNELS) return FALSE; |
919 | | |
920 | 0 | nTotalPoints = CubeSize(clutPoints, nInputs); |
921 | 0 | if (nTotalPoints == 0) return FALSE; |
922 | | |
923 | 0 | for (i = 0; i < (int) nTotalPoints; i++) { |
924 | |
|
925 | 0 | rest = i; |
926 | 0 | for (t = (int) nInputs-1; t >=0; --t) { |
927 | |
|
928 | 0 | cmsUInt32Number Colorant = rest % clutPoints[t]; |
929 | |
|
930 | 0 | rest /= clutPoints[t]; |
931 | 0 | In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, clutPoints[t]) / 65535.0); |
932 | |
|
933 | 0 | } |
934 | |
|
935 | 0 | if (!Sampler(In, NULL, Cargo)) |
936 | 0 | return FALSE; |
937 | 0 | } |
938 | | |
939 | 0 | return TRUE; |
940 | 0 | } |
941 | | |
942 | | // ******************************************************************************** |
943 | | // Type cmsSigLab2XYZElemType |
944 | | // ******************************************************************************** |
945 | | |
946 | | |
947 | | static |
948 | | void EvaluateLab2XYZ(const cmsFloat32Number In[], |
949 | | cmsFloat32Number Out[], |
950 | | const cmsStage *mpe) |
951 | 0 | { |
952 | 0 | cmsCIELab Lab; |
953 | 0 | cmsCIEXYZ XYZ; |
954 | 0 | const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ; |
955 | | |
956 | | // V4 rules |
957 | 0 | Lab.L = In[0] * 100.0; |
958 | 0 | Lab.a = In[1] * 255.0 - 128.0; |
959 | 0 | Lab.b = In[2] * 255.0 - 128.0; |
960 | |
|
961 | 0 | cmsLab2XYZ(NULL, &XYZ, &Lab); |
962 | | |
963 | | // From XYZ, range 0..19997 to 0..1.0, note that 1.99997 comes from 0xffff |
964 | | // encoded as 1.15 fixed point, so 1 + (32767.0 / 32768.0) |
965 | |
|
966 | 0 | Out[0] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.X / XYZadj); |
967 | 0 | Out[1] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Y / XYZadj); |
968 | 0 | Out[2] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Z / XYZadj); |
969 | 0 | return; |
970 | | |
971 | 0 | cmsUNUSED_PARAMETER(mpe); |
972 | 0 | } |
973 | | |
974 | | |
975 | | // No dup or free routines needed, as the structure has no pointers in it. |
976 | | cmsStage* CMSEXPORT _cmsStageAllocLab2XYZ(cmsContext ContextID) |
977 | 0 | { |
978 | 0 | return _cmsStageAllocPlaceholder(ContextID, cmsSigLab2XYZElemType, 3, 3, EvaluateLab2XYZ, NULL, NULL, NULL); |
979 | 0 | } |
980 | | |
981 | | // ******************************************************************************** |
982 | | |
983 | | // v2 L=100 is supposed to be placed on 0xFF00. There is no reasonable |
984 | | // number of gridpoints that would make exact match. However, a prelinearization |
985 | | // of 258 entries, would map 0xFF00 exactly on entry 257, and this is good to avoid scum dot. |
986 | | // Almost all what we need but unfortunately, the rest of entries should be scaled by |
987 | | // (255*257/256) and this is not exact. |
988 | | |
989 | | cmsStage* _cmsStageAllocLabV2ToV4curves(cmsContext ContextID) |
990 | 0 | { |
991 | 0 | cmsStage* mpe; |
992 | 0 | cmsToneCurve* LabTable[3]; |
993 | 0 | int i, j; |
994 | |
|
995 | 0 | LabTable[0] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL); |
996 | 0 | LabTable[1] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL); |
997 | 0 | LabTable[2] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL); |
998 | |
|
999 | 0 | for (j=0; j < 3; j++) { |
1000 | |
|
1001 | 0 | if (LabTable[j] == NULL) { |
1002 | 0 | cmsFreeToneCurveTriple(LabTable); |
1003 | 0 | return NULL; |
1004 | 0 | } |
1005 | | |
1006 | | // We need to map * (0xffff / 0xff00), that's same as (257 / 256) |
1007 | | // So we can use 258-entry tables to do the trick (i / 257) * (255 * 257) * (257 / 256); |
1008 | 0 | for (i=0; i < 257; i++) { |
1009 | |
|
1010 | 0 | LabTable[j]->Table16[i] = (cmsUInt16Number) ((i * 0xffff + 0x80) >> 8); |
1011 | 0 | } |
1012 | |
|
1013 | 0 | LabTable[j] ->Table16[257] = 0xffff; |
1014 | 0 | } |
1015 | | |
1016 | 0 | mpe = cmsStageAllocToneCurves(ContextID, 3, LabTable); |
1017 | 0 | cmsFreeToneCurveTriple(LabTable); |
1018 | |
|
1019 | 0 | if (mpe == NULL) return NULL; |
1020 | 0 | mpe ->Implements = cmsSigLabV2toV4; |
1021 | 0 | return mpe; |
1022 | 0 | } |
1023 | | |
1024 | | // ******************************************************************************** |
1025 | | |
1026 | | // Matrix-based conversion, which is more accurate, but slower and cannot properly be saved in devicelink profiles |
1027 | | cmsStage* CMSEXPORT _cmsStageAllocLabV2ToV4(cmsContext ContextID) |
1028 | 0 | { |
1029 | 0 | static const cmsFloat64Number V2ToV4[] = { 65535.0/65280.0, 0, 0, |
1030 | 0 | 0, 65535.0/65280.0, 0, |
1031 | 0 | 0, 0, 65535.0/65280.0 |
1032 | 0 | }; |
1033 | |
|
1034 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V2ToV4, NULL); |
1035 | |
|
1036 | 0 | if (mpe == NULL) return mpe; |
1037 | 0 | mpe ->Implements = cmsSigLabV2toV4; |
1038 | 0 | return mpe; |
1039 | 0 | } |
1040 | | |
1041 | | |
1042 | | // Reverse direction |
1043 | | cmsStage* CMSEXPORT _cmsStageAllocLabV4ToV2(cmsContext ContextID) |
1044 | 0 | { |
1045 | 0 | static const cmsFloat64Number V4ToV2[] = { 65280.0/65535.0, 0, 0, |
1046 | 0 | 0, 65280.0/65535.0, 0, |
1047 | 0 | 0, 0, 65280.0/65535.0 |
1048 | 0 | }; |
1049 | |
|
1050 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V4ToV2, NULL); |
1051 | |
|
1052 | 0 | if (mpe == NULL) return mpe; |
1053 | 0 | mpe ->Implements = cmsSigLabV4toV2; |
1054 | 0 | return mpe; |
1055 | 0 | } |
1056 | | |
1057 | | |
1058 | | // To Lab to float. Note that the MPE gives numbers in normal Lab range |
1059 | | // and we need 0..1.0 range for the formatters |
1060 | | // L* : 0...100 => 0...1.0 (L* / 100) |
1061 | | // ab* : -128..+127 to 0..1 ((ab* + 128) / 255) |
1062 | | |
1063 | | cmsStage* _cmsStageNormalizeFromLabFloat(cmsContext ContextID) |
1064 | 0 | { |
1065 | 0 | static const cmsFloat64Number a1[] = { |
1066 | 0 | 1.0/100.0, 0, 0, |
1067 | 0 | 0, 1.0/255.0, 0, |
1068 | 0 | 0, 0, 1.0/255.0 |
1069 | 0 | }; |
1070 | |
|
1071 | 0 | static const cmsFloat64Number o1[] = { |
1072 | 0 | 0, |
1073 | 0 | 128.0/255.0, |
1074 | 0 | 128.0/255.0 |
1075 | 0 | }; |
1076 | |
|
1077 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1); |
1078 | |
|
1079 | 0 | if (mpe == NULL) return mpe; |
1080 | 0 | mpe ->Implements = cmsSigLab2FloatPCS; |
1081 | 0 | return mpe; |
1082 | 0 | } |
1083 | | |
1084 | | // From XYZ to floating point PCS |
1085 | | cmsStage* _cmsStageNormalizeFromXyzFloat(cmsContext ContextID) |
1086 | 0 | { |
1087 | 0 | #define n (32768.0/65535.0) |
1088 | 0 | static const cmsFloat64Number a1[] = { |
1089 | 0 | n, 0, 0, |
1090 | 0 | 0, n, 0, |
1091 | 0 | 0, 0, n |
1092 | 0 | }; |
1093 | 0 | #undef n |
1094 | |
|
1095 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL); |
1096 | |
|
1097 | 0 | if (mpe == NULL) return mpe; |
1098 | 0 | mpe ->Implements = cmsSigXYZ2FloatPCS; |
1099 | 0 | return mpe; |
1100 | 0 | } |
1101 | | |
1102 | | cmsStage* _cmsStageNormalizeToLabFloat(cmsContext ContextID) |
1103 | 0 | { |
1104 | 0 | static const cmsFloat64Number a1[] = { |
1105 | 0 | 100.0, 0, 0, |
1106 | 0 | 0, 255.0, 0, |
1107 | 0 | 0, 0, 255.0 |
1108 | 0 | }; |
1109 | |
|
1110 | 0 | static const cmsFloat64Number o1[] = { |
1111 | 0 | 0, |
1112 | 0 | -128.0, |
1113 | 0 | -128.0 |
1114 | 0 | }; |
1115 | |
|
1116 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1); |
1117 | 0 | if (mpe == NULL) return mpe; |
1118 | 0 | mpe ->Implements = cmsSigFloatPCS2Lab; |
1119 | 0 | return mpe; |
1120 | 0 | } |
1121 | | |
1122 | | cmsStage* _cmsStageNormalizeToXyzFloat(cmsContext ContextID) |
1123 | 0 | { |
1124 | 0 | #define n (65535.0/32768.0) |
1125 | |
|
1126 | 0 | static const cmsFloat64Number a1[] = { |
1127 | 0 | n, 0, 0, |
1128 | 0 | 0, n, 0, |
1129 | 0 | 0, 0, n |
1130 | 0 | }; |
1131 | 0 | #undef n |
1132 | |
|
1133 | 0 | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL); |
1134 | 0 | if (mpe == NULL) return mpe; |
1135 | 0 | mpe ->Implements = cmsSigFloatPCS2XYZ; |
1136 | 0 | return mpe; |
1137 | 0 | } |
1138 | | |
1139 | | // Clips values smaller than zero |
1140 | | static |
1141 | | void Clipper(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
1142 | 0 | { |
1143 | 0 | cmsUInt32Number i; |
1144 | 0 | for (i = 0; i < mpe->InputChannels; i++) { |
1145 | |
|
1146 | 0 | cmsFloat32Number n = In[i]; |
1147 | 0 | Out[i] = n < 0 ? 0 : n; |
1148 | 0 | } |
1149 | 0 | } |
1150 | | |
1151 | | cmsStage* _cmsStageClipNegatives(cmsContext ContextID, cmsUInt32Number nChannels) |
1152 | 0 | { |
1153 | 0 | return _cmsStageAllocPlaceholder(ContextID, cmsSigClipNegativesElemType, |
1154 | 0 | nChannels, nChannels, Clipper, NULL, NULL, NULL); |
1155 | 0 | } |
1156 | | |
1157 | | // ******************************************************************************** |
1158 | | // Type cmsSigXYZ2LabElemType |
1159 | | // ******************************************************************************** |
1160 | | |
1161 | | static |
1162 | | void EvaluateXYZ2Lab(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
1163 | 0 | { |
1164 | 0 | cmsCIELab Lab; |
1165 | 0 | cmsCIEXYZ XYZ; |
1166 | 0 | const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ; |
1167 | | |
1168 | | // From 0..1.0 to XYZ |
1169 | |
|
1170 | 0 | XYZ.X = In[0] * XYZadj; |
1171 | 0 | XYZ.Y = In[1] * XYZadj; |
1172 | 0 | XYZ.Z = In[2] * XYZadj; |
1173 | |
|
1174 | 0 | cmsXYZ2Lab(NULL, &Lab, &XYZ); |
1175 | | |
1176 | | // From V4 Lab to 0..1.0 |
1177 | |
|
1178 | 0 | Out[0] = (cmsFloat32Number) (Lab.L / 100.0); |
1179 | 0 | Out[1] = (cmsFloat32Number) ((Lab.a + 128.0) / 255.0); |
1180 | 0 | Out[2] = (cmsFloat32Number) ((Lab.b + 128.0) / 255.0); |
1181 | 0 | return; |
1182 | | |
1183 | 0 | cmsUNUSED_PARAMETER(mpe); |
1184 | 0 | } |
1185 | | |
1186 | | cmsStage* CMSEXPORT _cmsStageAllocXYZ2Lab(cmsContext ContextID) |
1187 | 0 | { |
1188 | 0 | return _cmsStageAllocPlaceholder(ContextID, cmsSigXYZ2LabElemType, 3, 3, EvaluateXYZ2Lab, NULL, NULL, NULL); |
1189 | |
|
1190 | 0 | } |
1191 | | |
1192 | | // ******************************************************************************** |
1193 | | |
1194 | | // For v4, S-Shaped curves are placed in a/b axis to increase resolution near gray |
1195 | | |
1196 | | cmsStage* _cmsStageAllocLabPrelin(cmsContext ContextID) |
1197 | 0 | { |
1198 | 0 | cmsToneCurve* LabTable[3]; |
1199 | 0 | cmsFloat64Number Params[1] = {2.4} ; |
1200 | |
|
1201 | 0 | LabTable[0] = cmsBuildGamma(ContextID, 1.0); |
1202 | 0 | LabTable[1] = cmsBuildParametricToneCurve(ContextID, 108, Params); |
1203 | 0 | LabTable[2] = cmsBuildParametricToneCurve(ContextID, 108, Params); |
1204 | |
|
1205 | 0 | return cmsStageAllocToneCurves(ContextID, 3, LabTable); |
1206 | 0 | } |
1207 | | |
1208 | | |
1209 | | // Free a single MPE |
1210 | | void CMSEXPORT cmsStageFree(cmsStage* mpe) |
1211 | 0 | { |
1212 | 0 | if (mpe ->FreePtr) |
1213 | 0 | mpe ->FreePtr(mpe); |
1214 | |
|
1215 | 0 | _cmsFree(mpe ->ContextID, mpe); |
1216 | 0 | } |
1217 | | |
1218 | | |
1219 | | cmsUInt32Number CMSEXPORT cmsStageInputChannels(const cmsStage* mpe) |
1220 | 0 | { |
1221 | 0 | return mpe ->InputChannels; |
1222 | 0 | } |
1223 | | |
1224 | | cmsUInt32Number CMSEXPORT cmsStageOutputChannels(const cmsStage* mpe) |
1225 | 0 | { |
1226 | 0 | return mpe ->OutputChannels; |
1227 | 0 | } |
1228 | | |
1229 | | cmsStageSignature CMSEXPORT cmsStageType(const cmsStage* mpe) |
1230 | 0 | { |
1231 | 0 | return mpe -> Type; |
1232 | 0 | } |
1233 | | |
1234 | | void* CMSEXPORT cmsStageData(const cmsStage* mpe) |
1235 | 0 | { |
1236 | 0 | return mpe -> Data; |
1237 | 0 | } |
1238 | | |
1239 | | cmsContext CMSEXPORT cmsGetStageContextID(const cmsStage* mpe) |
1240 | 0 | { |
1241 | 0 | return mpe -> ContextID; |
1242 | 0 | } |
1243 | | |
1244 | | cmsStage* CMSEXPORT cmsStageNext(const cmsStage* mpe) |
1245 | 0 | { |
1246 | 0 | return mpe -> Next; |
1247 | 0 | } |
1248 | | |
1249 | | |
1250 | | // Duplicates an MPE |
1251 | | cmsStage* CMSEXPORT cmsStageDup(cmsStage* mpe) |
1252 | 0 | { |
1253 | 0 | cmsStage* NewMPE; |
1254 | |
|
1255 | 0 | if (mpe == NULL) return NULL; |
1256 | 0 | NewMPE = _cmsStageAllocPlaceholder(mpe ->ContextID, |
1257 | 0 | mpe ->Type, |
1258 | 0 | mpe ->InputChannels, |
1259 | 0 | mpe ->OutputChannels, |
1260 | 0 | mpe ->EvalPtr, |
1261 | 0 | mpe ->DupElemPtr, |
1262 | 0 | mpe ->FreePtr, |
1263 | 0 | NULL); |
1264 | 0 | if (NewMPE == NULL) return NULL; |
1265 | | |
1266 | 0 | NewMPE ->Implements = mpe ->Implements; |
1267 | |
|
1268 | 0 | if (mpe ->DupElemPtr) { |
1269 | |
|
1270 | 0 | NewMPE ->Data = mpe ->DupElemPtr(mpe); |
1271 | |
|
1272 | 0 | if (NewMPE->Data == NULL) { |
1273 | |
|
1274 | 0 | cmsStageFree(NewMPE); |
1275 | 0 | return NULL; |
1276 | 0 | } |
1277 | |
|
1278 | 0 | } else { |
1279 | |
|
1280 | 0 | NewMPE ->Data = NULL; |
1281 | 0 | } |
1282 | | |
1283 | 0 | return NewMPE; |
1284 | 0 | } |
1285 | | |
1286 | | |
1287 | | // *********************************************************************************************************** |
1288 | | |
1289 | | // This function sets up the channel count |
1290 | | static |
1291 | | cmsBool BlessLUT(cmsPipeline* lut) |
1292 | 0 | { |
1293 | | // We can set the input/output channels only if we have elements. |
1294 | 0 | if (lut ->Elements != NULL) { |
1295 | |
|
1296 | 0 | cmsStage* prev; |
1297 | 0 | cmsStage* next; |
1298 | 0 | cmsStage* First; |
1299 | 0 | cmsStage* Last; |
1300 | |
|
1301 | 0 | First = cmsPipelineGetPtrToFirstStage(lut); |
1302 | 0 | Last = cmsPipelineGetPtrToLastStage(lut); |
1303 | |
|
1304 | 0 | if (First == NULL || Last == NULL) return FALSE; |
1305 | | |
1306 | 0 | lut->InputChannels = First->InputChannels; |
1307 | 0 | lut->OutputChannels = Last->OutputChannels; |
1308 | | |
1309 | | // Check chain consistency |
1310 | 0 | prev = First; |
1311 | 0 | next = prev->Next; |
1312 | |
|
1313 | 0 | while (next != NULL) |
1314 | 0 | { |
1315 | 0 | if (next->InputChannels != prev->OutputChannels) |
1316 | 0 | return FALSE; |
1317 | | |
1318 | 0 | next = next->Next; |
1319 | 0 | prev = prev->Next; |
1320 | 0 | } |
1321 | 0 | } |
1322 | | |
1323 | 0 | return TRUE; |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | | // Default to evaluate the LUT on 16 bit-basis. Precision is retained. |
1328 | | static |
1329 | | void _LUTeval16(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER const void* D) |
1330 | 0 | { |
1331 | 0 | cmsPipeline* lut = (cmsPipeline*) D; |
1332 | 0 | cmsStage *mpe; |
1333 | 0 | cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS]; |
1334 | 0 | int Phase = 0, NextPhase; |
1335 | |
|
1336 | 0 | From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels); |
1337 | |
|
1338 | 0 | for (mpe = lut ->Elements; |
1339 | 0 | mpe != NULL; |
1340 | 0 | mpe = mpe ->Next) { |
1341 | |
|
1342 | 0 | NextPhase = Phase ^ 1; |
1343 | 0 | mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe); |
1344 | 0 | Phase = NextPhase; |
1345 | 0 | } |
1346 | | |
1347 | |
|
1348 | 0 | FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels); |
1349 | 0 | } |
1350 | | |
1351 | | |
1352 | | |
1353 | | // Does evaluate the LUT on cmsFloat32Number-basis. |
1354 | | static |
1355 | | void _LUTevalFloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const void* D) |
1356 | 0 | { |
1357 | 0 | cmsPipeline* lut = (cmsPipeline*) D; |
1358 | 0 | cmsStage *mpe; |
1359 | 0 | cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS]; |
1360 | 0 | int Phase = 0, NextPhase; |
1361 | |
|
1362 | 0 | memmove(&Storage[Phase][0], In, lut ->InputChannels * sizeof(cmsFloat32Number)); |
1363 | |
|
1364 | 0 | for (mpe = lut ->Elements; |
1365 | 0 | mpe != NULL; |
1366 | 0 | mpe = mpe ->Next) { |
1367 | |
|
1368 | 0 | NextPhase = Phase ^ 1; |
1369 | 0 | mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe); |
1370 | 0 | Phase = NextPhase; |
1371 | 0 | } |
1372 | |
|
1373 | 0 | memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number)); |
1374 | 0 | } |
1375 | | |
1376 | | |
1377 | | // LUT Creation & Destruction |
1378 | | cmsPipeline* CMSEXPORT cmsPipelineAlloc(cmsContext ContextID, cmsUInt32Number InputChannels, cmsUInt32Number OutputChannels) |
1379 | 0 | { |
1380 | 0 | cmsPipeline* NewLUT; |
1381 | | |
1382 | | // A value of zero in channels is allowed as placeholder |
1383 | 0 | if (InputChannels >= cmsMAXCHANNELS || |
1384 | 0 | OutputChannels >= cmsMAXCHANNELS) return NULL; |
1385 | | |
1386 | 0 | NewLUT = (cmsPipeline*) _cmsMallocZero(ContextID, sizeof(cmsPipeline)); |
1387 | 0 | if (NewLUT == NULL) return NULL; |
1388 | | |
1389 | 0 | NewLUT -> InputChannels = InputChannels; |
1390 | 0 | NewLUT -> OutputChannels = OutputChannels; |
1391 | |
|
1392 | 0 | NewLUT ->Eval16Fn = _LUTeval16; |
1393 | 0 | NewLUT ->EvalFloatFn = _LUTevalFloat; |
1394 | 0 | NewLUT ->DupDataFn = NULL; |
1395 | 0 | NewLUT ->FreeDataFn = NULL; |
1396 | 0 | NewLUT ->Data = NewLUT; |
1397 | 0 | NewLUT ->ContextID = ContextID; |
1398 | |
|
1399 | 0 | if (!BlessLUT(NewLUT)) |
1400 | 0 | { |
1401 | 0 | _cmsFree(ContextID, NewLUT); |
1402 | 0 | return NULL; |
1403 | 0 | } |
1404 | | |
1405 | 0 | return NewLUT; |
1406 | 0 | } |
1407 | | |
1408 | | cmsContext CMSEXPORT cmsGetPipelineContextID(const cmsPipeline* lut) |
1409 | 0 | { |
1410 | 0 | _cmsAssert(lut != NULL); |
1411 | 0 | return lut ->ContextID; |
1412 | 0 | } |
1413 | | |
1414 | | cmsUInt32Number CMSEXPORT cmsPipelineInputChannels(const cmsPipeline* lut) |
1415 | 0 | { |
1416 | 0 | _cmsAssert(lut != NULL); |
1417 | 0 | return lut ->InputChannels; |
1418 | 0 | } |
1419 | | |
1420 | | cmsUInt32Number CMSEXPORT cmsPipelineOutputChannels(const cmsPipeline* lut) |
1421 | 0 | { |
1422 | 0 | _cmsAssert(lut != NULL); |
1423 | 0 | return lut ->OutputChannels; |
1424 | 0 | } |
1425 | | |
1426 | | // Free a profile elements LUT |
1427 | | void CMSEXPORT cmsPipelineFree(cmsPipeline* lut) |
1428 | 0 | { |
1429 | 0 | cmsStage *mpe, *Next; |
1430 | |
|
1431 | 0 | if (lut == NULL) return; |
1432 | | |
1433 | 0 | for (mpe = lut ->Elements; |
1434 | 0 | mpe != NULL; |
1435 | 0 | mpe = Next) { |
1436 | |
|
1437 | 0 | Next = mpe ->Next; |
1438 | 0 | cmsStageFree(mpe); |
1439 | 0 | } |
1440 | |
|
1441 | 0 | if (lut ->FreeDataFn) lut ->FreeDataFn(lut ->ContextID, lut ->Data); |
1442 | |
|
1443 | 0 | _cmsFree(lut ->ContextID, lut); |
1444 | 0 | } |
1445 | | |
1446 | | |
1447 | | // Default to evaluate the LUT on 16 bit-basis. |
1448 | | void CMSEXPORT cmsPipelineEval16(const cmsUInt16Number In[], cmsUInt16Number Out[], const cmsPipeline* lut) |
1449 | 0 | { |
1450 | 0 | _cmsAssert(lut != NULL); |
1451 | 0 | lut ->Eval16Fn(In, Out, lut->Data); |
1452 | 0 | } |
1453 | | |
1454 | | |
1455 | | // Does evaluate the LUT on cmsFloat32Number-basis. |
1456 | | void CMSEXPORT cmsPipelineEvalFloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsPipeline* lut) |
1457 | 0 | { |
1458 | 0 | _cmsAssert(lut != NULL); |
1459 | 0 | lut ->EvalFloatFn(In, Out, lut); |
1460 | 0 | } |
1461 | | |
1462 | | |
1463 | | |
1464 | | // Duplicates a LUT |
1465 | | cmsPipeline* CMSEXPORT cmsPipelineDup(const cmsPipeline* lut) |
1466 | 0 | { |
1467 | 0 | cmsPipeline* NewLUT; |
1468 | 0 | cmsStage *NewMPE, *Anterior = NULL, *mpe; |
1469 | 0 | cmsBool First = TRUE; |
1470 | |
|
1471 | 0 | if (lut == NULL) return NULL; |
1472 | | |
1473 | 0 | NewLUT = cmsPipelineAlloc(lut ->ContextID, lut ->InputChannels, lut ->OutputChannels); |
1474 | 0 | if (NewLUT == NULL) return NULL; |
1475 | | |
1476 | 0 | for (mpe = lut ->Elements; |
1477 | 0 | mpe != NULL; |
1478 | 0 | mpe = mpe ->Next) { |
1479 | |
|
1480 | 0 | NewMPE = cmsStageDup(mpe); |
1481 | |
|
1482 | 0 | if (NewMPE == NULL) { |
1483 | 0 | cmsPipelineFree(NewLUT); |
1484 | 0 | return NULL; |
1485 | 0 | } |
1486 | | |
1487 | 0 | if (First) { |
1488 | 0 | NewLUT ->Elements = NewMPE; |
1489 | 0 | First = FALSE; |
1490 | 0 | } |
1491 | 0 | else { |
1492 | 0 | if (Anterior != NULL) |
1493 | 0 | Anterior ->Next = NewMPE; |
1494 | 0 | } |
1495 | |
|
1496 | 0 | Anterior = NewMPE; |
1497 | 0 | } |
1498 | | |
1499 | 0 | NewLUT ->Eval16Fn = lut ->Eval16Fn; |
1500 | 0 | NewLUT ->EvalFloatFn = lut ->EvalFloatFn; |
1501 | 0 | NewLUT ->DupDataFn = lut ->DupDataFn; |
1502 | 0 | NewLUT ->FreeDataFn = lut ->FreeDataFn; |
1503 | |
|
1504 | 0 | if (NewLUT ->DupDataFn != NULL) |
1505 | 0 | NewLUT ->Data = NewLUT ->DupDataFn(lut ->ContextID, lut->Data); |
1506 | | |
1507 | |
|
1508 | 0 | NewLUT ->SaveAs8Bits = lut ->SaveAs8Bits; |
1509 | |
|
1510 | 0 | if (!BlessLUT(NewLUT)) |
1511 | 0 | { |
1512 | 0 | _cmsFree(lut->ContextID, NewLUT); |
1513 | 0 | return NULL; |
1514 | 0 | } |
1515 | | |
1516 | 0 | return NewLUT; |
1517 | 0 | } |
1518 | | |
1519 | | |
1520 | | int CMSEXPORT cmsPipelineInsertStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage* mpe) |
1521 | 0 | { |
1522 | 0 | cmsStage* Anterior = NULL, *pt; |
1523 | |
|
1524 | 0 | if (lut == NULL || mpe == NULL) |
1525 | 0 | return FALSE; |
1526 | | |
1527 | 0 | switch (loc) { |
1528 | | |
1529 | 0 | case cmsAT_BEGIN: |
1530 | 0 | mpe ->Next = lut ->Elements; |
1531 | 0 | lut ->Elements = mpe; |
1532 | 0 | break; |
1533 | | |
1534 | 0 | case cmsAT_END: |
1535 | |
|
1536 | 0 | if (lut ->Elements == NULL) |
1537 | 0 | lut ->Elements = mpe; |
1538 | 0 | else { |
1539 | |
|
1540 | 0 | for (pt = lut ->Elements; |
1541 | 0 | pt != NULL; |
1542 | 0 | pt = pt -> Next) Anterior = pt; |
1543 | | |
1544 | 0 | Anterior ->Next = mpe; |
1545 | 0 | mpe ->Next = NULL; |
1546 | 0 | } |
1547 | 0 | break; |
1548 | 0 | default:; |
1549 | 0 | return FALSE; |
1550 | 0 | } |
1551 | | |
1552 | 0 | return BlessLUT(lut); |
1553 | 0 | } |
1554 | | |
1555 | | // Unlink an element and return the pointer to it |
1556 | | void CMSEXPORT cmsPipelineUnlinkStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe) |
1557 | 0 | { |
1558 | 0 | cmsStage *Anterior, *pt, *Last; |
1559 | 0 | cmsStage *Unlinked = NULL; |
1560 | | |
1561 | | |
1562 | | // If empty LUT, there is nothing to remove |
1563 | 0 | if (lut ->Elements == NULL) { |
1564 | 0 | if (mpe) *mpe = NULL; |
1565 | 0 | return; |
1566 | 0 | } |
1567 | | |
1568 | | // On depending on the strategy... |
1569 | 0 | switch (loc) { |
1570 | | |
1571 | 0 | case cmsAT_BEGIN: |
1572 | 0 | { |
1573 | 0 | cmsStage* elem = lut ->Elements; |
1574 | |
|
1575 | 0 | lut ->Elements = elem -> Next; |
1576 | 0 | elem ->Next = NULL; |
1577 | 0 | Unlinked = elem; |
1578 | |
|
1579 | 0 | } |
1580 | 0 | break; |
1581 | | |
1582 | 0 | case cmsAT_END: |
1583 | 0 | Anterior = Last = NULL; |
1584 | 0 | for (pt = lut ->Elements; |
1585 | 0 | pt != NULL; |
1586 | 0 | pt = pt -> Next) { |
1587 | 0 | Anterior = Last; |
1588 | 0 | Last = pt; |
1589 | 0 | } |
1590 | |
|
1591 | 0 | Unlinked = Last; // Next already points to NULL |
1592 | | |
1593 | | // Truncate the chain |
1594 | 0 | if (Anterior) |
1595 | 0 | Anterior ->Next = NULL; |
1596 | 0 | else |
1597 | 0 | lut ->Elements = NULL; |
1598 | 0 | break; |
1599 | 0 | default:; |
1600 | 0 | } |
1601 | | |
1602 | 0 | if (mpe) |
1603 | 0 | *mpe = Unlinked; |
1604 | 0 | else |
1605 | 0 | cmsStageFree(Unlinked); |
1606 | | |
1607 | | // May fail, but we ignore it |
1608 | 0 | BlessLUT(lut); |
1609 | 0 | } |
1610 | | |
1611 | | |
1612 | | // Concatenate two LUT into a new single one |
1613 | | cmsBool CMSEXPORT cmsPipelineCat(cmsPipeline* l1, const cmsPipeline* l2) |
1614 | 0 | { |
1615 | 0 | cmsStage* mpe; |
1616 | | |
1617 | | // If both LUTS does not have elements, we need to inherit |
1618 | | // the number of channels |
1619 | 0 | if (l1 ->Elements == NULL && l2 ->Elements == NULL) { |
1620 | 0 | l1 ->InputChannels = l2 ->InputChannels; |
1621 | 0 | l1 ->OutputChannels = l2 ->OutputChannels; |
1622 | 0 | } |
1623 | | |
1624 | | // Cat second |
1625 | 0 | for (mpe = l2 ->Elements; |
1626 | 0 | mpe != NULL; |
1627 | 0 | mpe = mpe ->Next) { |
1628 | | |
1629 | | // We have to dup each element |
1630 | 0 | if (!cmsPipelineInsertStage(l1, cmsAT_END, cmsStageDup(mpe))) |
1631 | 0 | return FALSE; |
1632 | 0 | } |
1633 | | |
1634 | 0 | return BlessLUT(l1); |
1635 | 0 | } |
1636 | | |
1637 | | |
1638 | | cmsBool CMSEXPORT cmsPipelineSetSaveAs8bitsFlag(cmsPipeline* lut, cmsBool On) |
1639 | 0 | { |
1640 | 0 | cmsBool Anterior = lut ->SaveAs8Bits; |
1641 | |
|
1642 | 0 | lut ->SaveAs8Bits = On; |
1643 | 0 | return Anterior; |
1644 | 0 | } |
1645 | | |
1646 | | |
1647 | | cmsStage* CMSEXPORT cmsPipelineGetPtrToFirstStage(const cmsPipeline* lut) |
1648 | 0 | { |
1649 | 0 | return lut ->Elements; |
1650 | 0 | } |
1651 | | |
1652 | | cmsStage* CMSEXPORT cmsPipelineGetPtrToLastStage(const cmsPipeline* lut) |
1653 | 0 | { |
1654 | 0 | cmsStage *mpe, *Anterior = NULL; |
1655 | |
|
1656 | 0 | for (mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next) |
1657 | 0 | Anterior = mpe; |
1658 | |
|
1659 | 0 | return Anterior; |
1660 | 0 | } |
1661 | | |
1662 | | cmsUInt32Number CMSEXPORT cmsPipelineStageCount(const cmsPipeline* lut) |
1663 | 0 | { |
1664 | 0 | cmsStage *mpe; |
1665 | 0 | cmsUInt32Number n; |
1666 | |
|
1667 | 0 | for (n=0, mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next) |
1668 | 0 | n++; |
1669 | |
|
1670 | 0 | return n; |
1671 | 0 | } |
1672 | | |
1673 | | // This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional |
1674 | | // duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality. |
1675 | | void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsPipeline* Lut, |
1676 | | _cmsPipelineEval16Fn Eval16, |
1677 | | void* PrivateData, |
1678 | | _cmsFreeUserDataFn FreePrivateDataFn, |
1679 | | _cmsDupUserDataFn DupPrivateDataFn) |
1680 | 0 | { |
1681 | |
|
1682 | 0 | Lut ->Eval16Fn = Eval16; |
1683 | 0 | Lut ->DupDataFn = DupPrivateDataFn; |
1684 | 0 | Lut ->FreeDataFn = FreePrivateDataFn; |
1685 | 0 | Lut ->Data = PrivateData; |
1686 | 0 | } |
1687 | | |
1688 | | |
1689 | | // ----------------------------------------------------------- Reverse interpolation |
1690 | | // Here's how it goes. The derivative Df(x) of the function f is the linear |
1691 | | // transformation that best approximates f near the point x. It can be represented |
1692 | | // by a matrix A whose entries are the partial derivatives of the components of f |
1693 | | // with respect to all the coordinates. This is know as the Jacobian |
1694 | | // |
1695 | | // The best linear approximation to f is given by the matrix equation: |
1696 | | // |
1697 | | // y-y0 = A (x-x0) |
1698 | | // |
1699 | | // So, if x0 is a good "guess" for the zero of f, then solving for the zero of this |
1700 | | // linear approximation will give a "better guess" for the zero of f. Thus let y=0, |
1701 | | // and since y0=f(x0) one can solve the above equation for x. This leads to the |
1702 | | // Newton's method formula: |
1703 | | // |
1704 | | // xn+1 = xn - A-1 f(xn) |
1705 | | // |
1706 | | // where xn+1 denotes the (n+1)-st guess, obtained from the n-th guess xn in the |
1707 | | // fashion described above. Iterating this will give better and better approximations |
1708 | | // if you have a "good enough" initial guess. |
1709 | | |
1710 | | |
1711 | 0 | #define JACOBIAN_EPSILON 0.001f |
1712 | 0 | #define INVERSION_MAX_ITERATIONS 30 |
1713 | | |
1714 | | // Increment with reflexion on boundary |
1715 | | static |
1716 | | void IncDelta(cmsFloat32Number *Val) |
1717 | 0 | { |
1718 | 0 | if (*Val < (1.0 - JACOBIAN_EPSILON)) |
1719 | | |
1720 | 0 | *Val += JACOBIAN_EPSILON; |
1721 | | |
1722 | 0 | else |
1723 | 0 | *Val -= JACOBIAN_EPSILON; |
1724 | |
|
1725 | 0 | } |
1726 | | |
1727 | | |
1728 | | |
1729 | | // Euclidean distance between two vectors of n elements each one |
1730 | | static |
1731 | | cmsFloat32Number EuclideanDistance(cmsFloat32Number a[], cmsFloat32Number b[], int n) |
1732 | 0 | { |
1733 | 0 | cmsFloat32Number sum = 0; |
1734 | 0 | int i; |
1735 | |
|
1736 | 0 | for (i=0; i < n; i++) { |
1737 | 0 | cmsFloat32Number dif = b[i] - a[i]; |
1738 | 0 | sum += dif * dif; |
1739 | 0 | } |
1740 | |
|
1741 | 0 | return sqrtf(sum); |
1742 | 0 | } |
1743 | | |
1744 | | |
1745 | | // Evaluate a LUT in reverse direction. It only searches on 3->3 LUT. Uses Newton method |
1746 | | // |
1747 | | // x1 <- x - [J(x)]^-1 * f(x) |
1748 | | // |
1749 | | // lut: The LUT on where to do the search |
1750 | | // Target: LabK, 3 values of Lab plus destination K which is fixed |
1751 | | // Result: The obtained CMYK |
1752 | | // Hint: Location where begin the search |
1753 | | |
1754 | | cmsBool CMSEXPORT cmsPipelineEvalReverseFloat(cmsFloat32Number Target[], |
1755 | | cmsFloat32Number Result[], |
1756 | | cmsFloat32Number Hint[], |
1757 | | const cmsPipeline* lut) |
1758 | 0 | { |
1759 | 0 | cmsUInt32Number i, j; |
1760 | 0 | cmsFloat64Number error, LastError = 1E20; |
1761 | 0 | cmsFloat32Number fx[4], x[4], xd[4], fxd[4]; |
1762 | 0 | cmsVEC3 tmp, tmp2; |
1763 | 0 | cmsMAT3 Jacobian; |
1764 | | |
1765 | | // Only 3->3 and 4->3 are supported |
1766 | 0 | if (lut ->InputChannels != 3 && lut ->InputChannels != 4) return FALSE; |
1767 | 0 | if (lut ->OutputChannels != 3) return FALSE; |
1768 | | |
1769 | | // Take the hint as starting point if specified |
1770 | 0 | if (Hint == NULL) { |
1771 | | |
1772 | | // Begin at any point, we choose 1/3 of CMY axis |
1773 | 0 | x[0] = x[1] = x[2] = 0.3f; |
1774 | 0 | } |
1775 | 0 | else { |
1776 | | |
1777 | | // Only copy 3 channels from hint... |
1778 | 0 | for (j=0; j < 3; j++) |
1779 | 0 | x[j] = Hint[j]; |
1780 | 0 | } |
1781 | | |
1782 | | // If Lut is 4-dimensions, then grab target[3], which is fixed |
1783 | 0 | if (lut ->InputChannels == 4) { |
1784 | 0 | x[3] = Target[3]; |
1785 | 0 | } |
1786 | 0 | else x[3] = 0; // To keep lint happy |
1787 | | |
1788 | | |
1789 | | // Iterate |
1790 | 0 | for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) { |
1791 | | |
1792 | | // Get beginning fx |
1793 | 0 | cmsPipelineEvalFloat(x, fx, lut); |
1794 | | |
1795 | | // Compute error |
1796 | 0 | error = EuclideanDistance(fx, Target, 3); |
1797 | | |
1798 | | // If not convergent, return last safe value |
1799 | 0 | if (error >= LastError) |
1800 | 0 | break; |
1801 | | |
1802 | | // Keep latest values |
1803 | 0 | LastError = error; |
1804 | 0 | for (j=0; j < lut ->InputChannels; j++) |
1805 | 0 | Result[j] = x[j]; |
1806 | | |
1807 | | // Found an exact match? |
1808 | 0 | if (error <= 0) |
1809 | 0 | break; |
1810 | | |
1811 | | // Obtain slope (the Jacobian) |
1812 | 0 | for (j = 0; j < 3; j++) { |
1813 | |
|
1814 | 0 | xd[0] = x[0]; |
1815 | 0 | xd[1] = x[1]; |
1816 | 0 | xd[2] = x[2]; |
1817 | 0 | xd[3] = x[3]; // Keep fixed channel |
1818 | |
|
1819 | 0 | IncDelta(&xd[j]); |
1820 | |
|
1821 | 0 | cmsPipelineEvalFloat(xd, fxd, lut); |
1822 | |
|
1823 | 0 | Jacobian.v[0].n[j] = ((fxd[0] - fx[0]) / JACOBIAN_EPSILON); |
1824 | 0 | Jacobian.v[1].n[j] = ((fxd[1] - fx[1]) / JACOBIAN_EPSILON); |
1825 | 0 | Jacobian.v[2].n[j] = ((fxd[2] - fx[2]) / JACOBIAN_EPSILON); |
1826 | 0 | } |
1827 | | |
1828 | | // Solve system |
1829 | 0 | tmp2.n[0] = fx[0] - Target[0]; |
1830 | 0 | tmp2.n[1] = fx[1] - Target[1]; |
1831 | 0 | tmp2.n[2] = fx[2] - Target[2]; |
1832 | |
|
1833 | 0 | if (!_cmsMAT3solve(&tmp, &Jacobian, &tmp2)) |
1834 | 0 | return FALSE; |
1835 | | |
1836 | | // Move our guess |
1837 | 0 | x[0] -= (cmsFloat32Number) tmp.n[0]; |
1838 | 0 | x[1] -= (cmsFloat32Number) tmp.n[1]; |
1839 | 0 | x[2] -= (cmsFloat32Number) tmp.n[2]; |
1840 | | |
1841 | | // Some clipping.... |
1842 | 0 | for (j=0; j < 3; j++) { |
1843 | 0 | if (x[j] < 0) x[j] = 0; |
1844 | 0 | else |
1845 | 0 | if (x[j] > 1.0) x[j] = 1.0; |
1846 | 0 | } |
1847 | 0 | } |
1848 | | |
1849 | 0 | return TRUE; |
1850 | 0 | } |
1851 | | |
1852 | | |