/src/ghostpdl/lcms2mt/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 | 20.4M | { |
40 | 20.4M | cmsStage* ph = (cmsStage*) _cmsMallocZero(ContextID, sizeof(cmsStage)); |
41 | | |
42 | 20.4M | if (ph == NULL) return NULL; |
43 | | |
44 | 20.4M | ph ->Type = Type; |
45 | 20.4M | ph ->Implements = Type; // By default, no clue on what is implementing |
46 | | |
47 | 20.4M | ph ->InputChannels = InputChannels; |
48 | 20.4M | ph ->OutputChannels = OutputChannels; |
49 | 20.4M | ph ->EvalPtr = EvalPtr; |
50 | 20.4M | ph ->DupElemPtr = DupElemPtr; |
51 | 20.4M | ph ->FreePtr = FreePtr; |
52 | 20.4M | ph ->Data = Data; |
53 | | |
54 | 20.4M | return ph; |
55 | 20.4M | } |
56 | | |
57 | | |
58 | | static |
59 | | void EvaluateIdentity(cmsContext ContextID, const cmsFloat32Number In[], |
60 | | cmsFloat32Number Out[], |
61 | | const cmsStage *mpe) |
62 | 0 | { |
63 | 0 | cmsUNUSED_PARAMETER(ContextID); |
64 | 0 | memmove(Out, In, mpe ->InputChannels * sizeof(cmsFloat32Number)); |
65 | 0 | } |
66 | | |
67 | | |
68 | | cmsStage* CMSEXPORT cmsStageAllocIdentity(cmsContext ContextID, cmsUInt32Number nChannels) |
69 | 27.1k | { |
70 | 27.1k | return _cmsStageAllocPlaceholder(ContextID, |
71 | 27.1k | cmsSigIdentityElemType, |
72 | 27.1k | nChannels, nChannels, |
73 | 27.1k | EvaluateIdentity, |
74 | 27.1k | NULL, |
75 | 27.1k | NULL, |
76 | 27.1k | NULL); |
77 | 27.1k | } |
78 | | |
79 | | // Conversion functions. From floating point to 16 bits |
80 | | static |
81 | | void FromFloatTo16(const cmsFloat32Number In[], cmsUInt16Number Out[], cmsUInt32Number n) |
82 | 3.55G | { |
83 | 3.55G | cmsUInt32Number i; |
84 | | |
85 | 16.9G | for (i=0; i < n; i++) { |
86 | 13.3G | Out[i] = _cmsQuickSaturateWord(In[i] * 65535.0); |
87 | 13.3G | } |
88 | 3.55G | } |
89 | | |
90 | | // From 16 bits to floating point |
91 | | static |
92 | | void From16ToFloat(const cmsUInt16Number In[], cmsFloat32Number Out[], cmsUInt32Number n) |
93 | 3.55G | { |
94 | 3.55G | cmsUInt32Number i; |
95 | | |
96 | 15.0G | for (i=0; i < n; i++) { |
97 | 11.5G | Out[i] = (cmsFloat32Number) In[i] / 65535.0F; |
98 | 11.5G | } |
99 | 3.55G | } |
100 | | |
101 | | |
102 | | // This function is quite useful to analyze the structure of a LUT and retrieve the MPE elements |
103 | | // that conform the LUT. It should be called with the LUT, the number of expected elements and |
104 | | // then a list of expected types followed with a list of cmsFloat64Number pointers to MPE elements. If |
105 | | // the function founds a match with current pipeline, it fills the pointers and returns TRUE |
106 | | // if not, returns FALSE without touching anything. Setting pointers to NULL does bypass |
107 | | // the storage process. |
108 | | cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(cmsContext ContextID, const cmsPipeline* Lut, cmsUInt32Number n, ...) |
109 | 506k | { |
110 | 506k | va_list args; |
111 | 506k | cmsUInt32Number i; |
112 | 506k | cmsStage* mpe; |
113 | 506k | cmsStageSignature Type; |
114 | 506k | void** ElemPtr; |
115 | | |
116 | | // Make sure same number of elements |
117 | 506k | if (cmsPipelineStageCount(ContextID, Lut) != n) return FALSE; |
118 | | |
119 | 506k | va_start(args, n); |
120 | | |
121 | | // Iterate across asked types |
122 | 126k | mpe = Lut ->Elements; |
123 | 253k | for (i=0; i < n; i++) { |
124 | | |
125 | | // Get asked type. cmsStageSignature is promoted to int by compiler |
126 | 126k | Type = (cmsStageSignature)va_arg(args, int); |
127 | 126k | if (mpe ->Type != Type) { |
128 | |
|
129 | 0 | va_end(args); // Mismatch. We are done. |
130 | 0 | return FALSE; |
131 | 0 | } |
132 | 126k | mpe = mpe ->Next; |
133 | 126k | } |
134 | | |
135 | | // Found a combination, fill pointers if not NULL |
136 | 126k | mpe = Lut ->Elements; |
137 | 253k | for (i=0; i < n; i++) { |
138 | | |
139 | 126k | ElemPtr = va_arg(args, void**); |
140 | 126k | if (ElemPtr != NULL) |
141 | 126k | *ElemPtr = mpe; |
142 | | |
143 | 126k | mpe = mpe ->Next; |
144 | 126k | } |
145 | | |
146 | 126k | va_end(args); |
147 | 126k | return TRUE; |
148 | 126k | } |
149 | | |
150 | | // Below there are implementations for several types of elements. Each type may be implemented by a |
151 | | // evaluation function, a duplication function, a function to free resources and a constructor. |
152 | | |
153 | | // ************************************************************************************************* |
154 | | // Type cmsSigCurveSetElemType (curves) |
155 | | // ************************************************************************************************* |
156 | | |
157 | | cmsToneCurve** _cmsStageGetPtrToCurveSet(const cmsStage* mpe) |
158 | 28.6k | { |
159 | 28.6k | _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data; |
160 | | |
161 | 28.6k | return Data ->TheCurves; |
162 | 28.6k | } |
163 | | |
164 | | static |
165 | | void EvaluateCurves(cmsContext ContextID, const cmsFloat32Number In[], |
166 | | cmsFloat32Number Out[], |
167 | | const cmsStage *mpe) |
168 | 16.0G | { |
169 | 16.0G | _cmsStageToneCurvesData* Data; |
170 | 16.0G | cmsUInt32Number i; |
171 | | |
172 | 16.0G | _cmsAssert(mpe != NULL); |
173 | | |
174 | 16.0G | Data = (_cmsStageToneCurvesData*) mpe ->Data; |
175 | 16.0G | if (Data == NULL) return; |
176 | | |
177 | 16.0G | if (Data ->TheCurves == NULL) return; |
178 | | |
179 | 60.7G | for (i=0; i < Data ->nCurves; i++) { |
180 | 44.7G | Out[i] = cmsEvalToneCurveFloat(ContextID, Data ->TheCurves[i], In[i]); |
181 | 44.7G | } |
182 | 16.0G | } |
183 | | |
184 | | static |
185 | | void CurveSetElemTypeFree(cmsContext ContextID, cmsStage* mpe) |
186 | 6.07M | { |
187 | 6.07M | _cmsStageToneCurvesData* Data; |
188 | 6.07M | cmsUInt32Number i; |
189 | | |
190 | 6.07M | _cmsAssert(mpe != NULL); |
191 | | |
192 | 6.07M | Data = (_cmsStageToneCurvesData*) mpe ->Data; |
193 | 6.07M | if (Data == NULL) return; |
194 | | |
195 | 6.07M | if (Data ->TheCurves != NULL) { |
196 | 17.7M | for (i=0; i < Data ->nCurves; i++) { |
197 | 11.6M | if (Data ->TheCurves[i] != NULL) |
198 | 11.6M | cmsFreeToneCurve(ContextID, Data ->TheCurves[i]); |
199 | 11.6M | } |
200 | 6.07M | } |
201 | 6.07M | _cmsFree(ContextID, Data ->TheCurves); |
202 | 6.07M | _cmsFree(ContextID, Data); |
203 | 6.07M | } |
204 | | |
205 | | |
206 | | static |
207 | | void* CurveSetDup(cmsContext ContextID, cmsStage* mpe) |
208 | 3.23M | { |
209 | 3.23M | _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data; |
210 | 3.23M | _cmsStageToneCurvesData* NewElem; |
211 | 3.23M | cmsUInt32Number i; |
212 | | |
213 | 3.23M | NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(ContextID, sizeof(_cmsStageToneCurvesData)); |
214 | 3.23M | if (NewElem == NULL) return NULL; |
215 | | |
216 | 3.23M | NewElem ->nCurves = Data ->nCurves; |
217 | 3.23M | NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(ContextID, NewElem ->nCurves, sizeof(cmsToneCurve*)); |
218 | | |
219 | 3.23M | if (NewElem ->TheCurves == NULL) goto Error; |
220 | | |
221 | 9.71M | for (i=0; i < NewElem ->nCurves; i++) { |
222 | | |
223 | | // Duplicate each curve. It may fail. |
224 | 6.48M | NewElem ->TheCurves[i] = cmsDupToneCurve(ContextID, Data ->TheCurves[i]); |
225 | 6.48M | if (NewElem ->TheCurves[i] == NULL) goto Error; |
226 | | |
227 | | |
228 | 6.48M | } |
229 | 3.23M | return (void*) NewElem; |
230 | | |
231 | 0 | Error: |
232 | |
|
233 | 0 | if (NewElem ->TheCurves != NULL) { |
234 | 0 | for (i=0; i < NewElem ->nCurves; i++) { |
235 | 0 | if (NewElem ->TheCurves[i]) |
236 | 0 | cmsFreeToneCurve(ContextID, NewElem ->TheCurves[i]); |
237 | 0 | } |
238 | 0 | } |
239 | 0 | _cmsFree(ContextID, NewElem ->TheCurves); |
240 | 0 | _cmsFree(ContextID, NewElem); |
241 | 0 | return NULL; |
242 | 3.23M | } |
243 | | |
244 | | |
245 | | // Curves == NULL forces identity curves |
246 | | cmsStage* CMSEXPORT cmsStageAllocToneCurves(cmsContext ContextID, cmsUInt32Number nChannels, cmsToneCurve* const Curves[]) |
247 | 2.84M | { |
248 | 2.84M | cmsUInt32Number i; |
249 | 2.84M | _cmsStageToneCurvesData* NewElem; |
250 | 2.84M | cmsStage* NewMPE; |
251 | | |
252 | | |
253 | 2.84M | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCurveSetElemType, nChannels, nChannels, |
254 | 2.84M | EvaluateCurves, CurveSetDup, CurveSetElemTypeFree, NULL ); |
255 | 2.84M | if (NewMPE == NULL) return NULL; |
256 | | |
257 | 2.84M | NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(ContextID, sizeof(_cmsStageToneCurvesData)); |
258 | 2.84M | if (NewElem == NULL) { |
259 | 0 | cmsStageFree(ContextID, NewMPE); |
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | 2.84M | NewMPE ->Data = (void*) NewElem; |
264 | | |
265 | 2.84M | NewElem ->nCurves = nChannels; |
266 | 2.84M | NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(ContextID, nChannels, sizeof(cmsToneCurve*)); |
267 | 2.84M | if (NewElem ->TheCurves == NULL) { |
268 | 0 | cmsStageFree(ContextID, NewMPE); |
269 | 0 | return NULL; |
270 | 0 | } |
271 | | |
272 | 8.04M | for (i=0; i < nChannels; i++) { |
273 | | |
274 | 5.20M | if (Curves == NULL) { |
275 | 1.75M | NewElem ->TheCurves[i] = cmsBuildGamma(ContextID, 1.0); |
276 | 1.75M | } |
277 | 3.45M | else { |
278 | 3.45M | NewElem ->TheCurves[i] = cmsDupToneCurve(ContextID, Curves[i]); |
279 | 3.45M | } |
280 | | |
281 | 5.20M | if (NewElem ->TheCurves[i] == NULL) { |
282 | 0 | cmsStageFree(ContextID, NewMPE); |
283 | 0 | return NULL; |
284 | 0 | } |
285 | | |
286 | 5.20M | } |
287 | | |
288 | 2.84M | return NewMPE; |
289 | 2.84M | } |
290 | | |
291 | | |
292 | | // Create a bunch of identity curves |
293 | | cmsStage* CMSEXPORT _cmsStageAllocIdentityCurves(cmsContext ContextID, cmsUInt32Number nChannels) |
294 | 583k | { |
295 | 583k | cmsStage* mpe = cmsStageAllocToneCurves(ContextID, nChannels, NULL); |
296 | | |
297 | 583k | if (mpe == NULL) return NULL; |
298 | 583k | mpe ->Implements = cmsSigIdentityElemType; |
299 | 583k | return mpe; |
300 | 583k | } |
301 | | |
302 | | |
303 | | // ************************************************************************************************* |
304 | | // Type cmsSigMatrixElemType (Matrices) |
305 | | // ************************************************************************************************* |
306 | | |
307 | | |
308 | | // Special care should be taken here because precision loss. A temporary cmsFloat64Number buffer is being used |
309 | | static |
310 | | void EvaluateMatrix(cmsContext ContextID, const cmsFloat32Number In[], |
311 | | cmsFloat32Number Out[], |
312 | | const cmsStage *mpe) |
313 | 13.6G | { |
314 | 13.6G | cmsUInt32Number i, j; |
315 | 13.6G | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
316 | 13.6G | cmsFloat64Number Tmp; |
317 | 13.6G | cmsUNUSED_PARAMETER(ContextID); |
318 | | |
319 | | // Input is already in 0..1.0 notation |
320 | 47.8G | for (i=0; i < mpe ->OutputChannels; i++) { |
321 | | |
322 | 34.1G | Tmp = 0; |
323 | 136G | for (j=0; j < mpe->InputChannels; j++) { |
324 | 102G | Tmp += In[j] * Data->Double[i*mpe->InputChannels + j]; |
325 | 102G | } |
326 | | |
327 | 34.1G | if (Data ->Offset != NULL) |
328 | 8.28G | Tmp += Data->Offset[i]; |
329 | | |
330 | 34.1G | Out[i] = (cmsFloat32Number) Tmp; |
331 | 34.1G | } |
332 | | |
333 | | |
334 | | // Output in 0..1.0 domain |
335 | 13.6G | } |
336 | | |
337 | | |
338 | | // Duplicate a yet-existing matrix element |
339 | | static |
340 | | void* MatrixElemDup(cmsContext ContextID, cmsStage* mpe) |
341 | 4.26M | { |
342 | 4.26M | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
343 | 4.26M | _cmsStageMatrixData* NewElem; |
344 | 4.26M | cmsUInt32Number sz; |
345 | | |
346 | 4.26M | NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData)); |
347 | 4.26M | if (NewElem == NULL) return NULL; |
348 | | |
349 | 4.26M | sz = mpe ->InputChannels * mpe ->OutputChannels; |
350 | | |
351 | 4.26M | NewElem ->Double = (cmsFloat64Number*) _cmsDupMem(ContextID, Data ->Double, sz * sizeof(cmsFloat64Number)) ; |
352 | | |
353 | 4.26M | if (Data ->Offset) |
354 | 332 | NewElem ->Offset = (cmsFloat64Number*) _cmsDupMem(ContextID, |
355 | 332 | Data ->Offset, mpe -> OutputChannels * sizeof(cmsFloat64Number)) ; |
356 | | |
357 | 4.26M | return (void*) NewElem; |
358 | 4.26M | } |
359 | | |
360 | | |
361 | | static |
362 | | void MatrixElemTypeFree(cmsContext ContextID, cmsStage* mpe) |
363 | 8.52M | { |
364 | 8.52M | _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data; |
365 | 8.52M | if (Data == NULL) |
366 | 0 | return; |
367 | 8.52M | if (Data ->Double) |
368 | 8.52M | _cmsFree(ContextID, Data ->Double); |
369 | | |
370 | 8.52M | if (Data ->Offset) |
371 | 13.4k | _cmsFree(ContextID, Data ->Offset); |
372 | | |
373 | 8.52M | _cmsFree(ContextID, mpe ->Data); |
374 | 8.52M | } |
375 | | |
376 | | |
377 | | |
378 | | cmsStage* CMSEXPORT cmsStageAllocMatrix(cmsContext ContextID, cmsUInt32Number Rows, cmsUInt32Number Cols, |
379 | | const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset) |
380 | 4.25M | { |
381 | 4.25M | cmsUInt32Number i, n; |
382 | 4.25M | _cmsStageMatrixData* NewElem; |
383 | 4.25M | cmsStage* NewMPE; |
384 | | |
385 | 4.25M | n = Rows * Cols; |
386 | | |
387 | | // Check for overflow |
388 | 4.25M | if (n == 0) return NULL; |
389 | 4.25M | if (n >= UINT_MAX / Cols) return NULL; |
390 | 4.25M | if (n >= UINT_MAX / Rows) return NULL; |
391 | 4.25M | if (n < Rows || n < Cols) return NULL; |
392 | | |
393 | 4.25M | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows, |
394 | 4.25M | EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL ); |
395 | 4.25M | if (NewMPE == NULL) return NULL; |
396 | | |
397 | | |
398 | 4.25M | NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData)); |
399 | 4.25M | if (NewElem == NULL) goto Error; |
400 | 4.25M | NewMPE->Data = (void*)NewElem; |
401 | | |
402 | 4.25M | NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number)); |
403 | 4.25M | if (NewElem->Double == NULL) goto Error; |
404 | | |
405 | 32.4M | for (i=0; i < n; i++) { |
406 | 28.2M | NewElem ->Double[i] = Matrix[i]; |
407 | 28.2M | } |
408 | | |
409 | 4.25M | if (Offset != NULL) { |
410 | | |
411 | 13.1k | NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Rows, sizeof(cmsFloat64Number)); |
412 | 13.1k | if (NewElem->Offset == NULL) goto Error; |
413 | | |
414 | 52.6k | for (i=0; i < Rows; i++) { |
415 | 39.4k | NewElem ->Offset[i] = Offset[i]; |
416 | 39.4k | } |
417 | 13.1k | } |
418 | | |
419 | 4.25M | return NewMPE; |
420 | | |
421 | 0 | Error: |
422 | 0 | cmsStageFree(ContextID, NewMPE); |
423 | 0 | return NULL; |
424 | 4.25M | } |
425 | | |
426 | | |
427 | | // ************************************************************************************************* |
428 | | // Type cmsSigCLutElemType |
429 | | // ************************************************************************************************* |
430 | | |
431 | | |
432 | | // Evaluate in true floating point |
433 | | static |
434 | | void EvaluateCLUTfloat(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
435 | 0 | { |
436 | 0 | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
437 | |
|
438 | 0 | Data -> Params ->Interpolation.LerpFloat(ContextID, In, Out, Data->Params); |
439 | 0 | } |
440 | | |
441 | | |
442 | | // Convert to 16 bits, evaluate, and back to floating point |
443 | | static |
444 | | void EvaluateCLUTfloatIn16(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
445 | 3.55G | { |
446 | 3.55G | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
447 | 3.55G | cmsUInt16Number In16[MAX_STAGE_CHANNELS], Out16[MAX_STAGE_CHANNELS]; |
448 | | |
449 | 3.55G | _cmsAssert(mpe ->InputChannels <= MAX_STAGE_CHANNELS); |
450 | 3.55G | _cmsAssert(mpe ->OutputChannels <= MAX_STAGE_CHANNELS); |
451 | | |
452 | 3.55G | FromFloatTo16(In, In16, mpe ->InputChannels); |
453 | 3.55G | Data -> Params ->Interpolation.Lerp16(ContextID, In16, Out16, Data->Params); |
454 | 3.55G | From16ToFloat(Out16, Out, mpe ->OutputChannels); |
455 | 3.55G | } |
456 | | |
457 | | |
458 | | // Given an hypercube of b dimensions, with Dims[] number of nodes by dimension, calculate the total amount of nodes |
459 | | static |
460 | | cmsUInt32Number CubeSize(const cmsUInt32Number Dims[], cmsUInt32Number b) |
461 | 3.12M | { |
462 | 3.12M | cmsUInt32Number dim; |
463 | 3.12M | cmsUInt64Number rv; |
464 | | |
465 | 3.12M | _cmsAssert(Dims != NULL); |
466 | | |
467 | 10.6M | for (rv = 1; b > 0; b--) { |
468 | | |
469 | 7.52M | dim = Dims[b-1]; |
470 | 7.52M | if (dim <= 1) return 0; |
471 | | |
472 | | // Check for overflow |
473 | 7.52M | if (rv > UINT_MAX / dim) return 0; |
474 | | |
475 | 7.52M | rv *= dim; |
476 | 7.52M | } |
477 | | |
478 | | // Again, prevent overflow |
479 | 3.12M | if (rv > UINT_MAX / 15) return 0; |
480 | | |
481 | 3.12M | return (cmsUInt32Number) rv; |
482 | 3.12M | } |
483 | | |
484 | | static |
485 | | void* CLUTElemDup(cmsContext ContextID, cmsStage* mpe) |
486 | 3.24M | { |
487 | 3.24M | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
488 | 3.24M | _cmsStageCLutData* NewElem; |
489 | | |
490 | | |
491 | 3.24M | NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData)); |
492 | 3.24M | if (NewElem == NULL) return NULL; |
493 | | |
494 | 3.24M | NewElem ->nEntries = Data ->nEntries; |
495 | 3.24M | NewElem ->HasFloatValues = Data ->HasFloatValues; |
496 | | |
497 | 3.24M | if (Data ->Tab.T) { |
498 | | |
499 | 3.24M | if (Data ->HasFloatValues) { |
500 | 0 | NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsDupMem(ContextID, Data ->Tab.TFloat, Data ->nEntries * sizeof (cmsFloat32Number)); |
501 | 0 | if (NewElem ->Tab.TFloat == NULL) |
502 | 0 | goto Error; |
503 | 3.24M | } else { |
504 | 3.24M | NewElem ->Tab.T = (cmsUInt16Number*) _cmsDupMem(ContextID, Data ->Tab.T, Data ->nEntries * sizeof (cmsUInt16Number)); |
505 | 3.24M | if (NewElem ->Tab.T == NULL) |
506 | 0 | goto Error; |
507 | 3.24M | } |
508 | 3.24M | } |
509 | | |
510 | 3.24M | NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, |
511 | 3.24M | Data ->Params ->nSamples, |
512 | 3.24M | Data ->Params ->nInputs, |
513 | 3.24M | Data ->Params ->nOutputs, |
514 | 3.24M | NewElem ->Tab.T, |
515 | 3.24M | Data ->Params ->dwFlags); |
516 | 3.24M | if (NewElem->Params != NULL) |
517 | 3.24M | return (void*) NewElem; |
518 | 0 | Error: |
519 | 0 | if (NewElem->Tab.T) |
520 | | // This works for both types |
521 | 0 | _cmsFree(ContextID, NewElem -> Tab.T); |
522 | 0 | _cmsFree(ContextID, NewElem); |
523 | 0 | return NULL; |
524 | 3.24M | } |
525 | | |
526 | | |
527 | | static |
528 | | void CLutElemTypeFree(cmsContext ContextID, cmsStage* mpe) |
529 | 4.82M | { |
530 | | |
531 | 4.82M | _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data; |
532 | | |
533 | | // Already empty |
534 | 4.82M | if (Data == NULL) return; |
535 | | |
536 | | // This works for both types |
537 | 4.82M | if (Data -> Tab.T) |
538 | 4.82M | _cmsFree(ContextID, Data -> Tab.T); |
539 | | |
540 | 4.82M | _cmsFreeInterpParams(ContextID, Data ->Params); |
541 | 4.82M | _cmsFree(ContextID, mpe ->Data); |
542 | 4.82M | } |
543 | | |
544 | | |
545 | | // Allocates a 16-bit multidimensional CLUT. This is evaluated at 16-bit precision. Table may have different |
546 | | // granularity on each dimension. |
547 | | cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID, |
548 | | const cmsUInt32Number clutPoints[], |
549 | | cmsUInt32Number inputChan, |
550 | | cmsUInt32Number outputChan, |
551 | | const cmsUInt16Number* Table) |
552 | 1.58M | { |
553 | 1.58M | cmsUInt32Number i, n; |
554 | 1.58M | _cmsStageCLutData* NewElem; |
555 | 1.58M | cmsStage* NewMPE; |
556 | | |
557 | 1.58M | _cmsAssert(clutPoints != NULL); |
558 | | |
559 | 1.58M | if (inputChan > MAX_INPUT_DIMENSIONS) { |
560 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS); |
561 | 0 | return NULL; |
562 | 0 | } |
563 | | |
564 | 1.58M | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan, |
565 | 1.58M | EvaluateCLUTfloatIn16, CLUTElemDup, CLutElemTypeFree, NULL ); |
566 | | |
567 | 1.58M | if (NewMPE == NULL) return NULL; |
568 | | |
569 | 1.58M | NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData)); |
570 | 1.58M | if (NewElem == NULL) { |
571 | 0 | cmsStageFree(ContextID, NewMPE); |
572 | 0 | return NULL; |
573 | 0 | } |
574 | | |
575 | 1.58M | NewMPE ->Data = (void*) NewElem; |
576 | | |
577 | 1.58M | NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan); |
578 | 1.58M | NewElem -> HasFloatValues = FALSE; |
579 | | |
580 | 1.58M | if (n == 0) { |
581 | 196 | cmsStageFree(ContextID, NewMPE); |
582 | 196 | return NULL; |
583 | 196 | } |
584 | | |
585 | | |
586 | 1.58M | NewElem ->Tab.T = (cmsUInt16Number*) _cmsCalloc(ContextID, n, sizeof(cmsUInt16Number)); |
587 | 1.58M | if (NewElem ->Tab.T == NULL) { |
588 | 10 | cmsStageFree(ContextID, NewMPE); |
589 | 10 | return NULL; |
590 | 10 | } |
591 | | |
592 | 1.58M | if (Table != NULL) { |
593 | 2.17G | for (i=0; i < n; i++) { |
594 | 2.17G | NewElem ->Tab.T[i] = Table[i]; |
595 | 2.17G | } |
596 | 46.7k | } |
597 | | |
598 | 1.58M | NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.T, CMS_LERP_FLAGS_16BITS); |
599 | 1.58M | if (NewElem ->Params == NULL) { |
600 | 0 | cmsStageFree(ContextID, NewMPE); |
601 | 0 | return NULL; |
602 | 0 | } |
603 | | |
604 | 1.58M | return NewMPE; |
605 | 1.58M | } |
606 | | |
607 | | cmsStage* CMSEXPORT cmsStageAllocCLut16bit(cmsContext ContextID, |
608 | | cmsUInt32Number nGridPoints, |
609 | | cmsUInt32Number inputChan, |
610 | | cmsUInt32Number outputChan, |
611 | | const cmsUInt16Number* Table) |
612 | 558k | { |
613 | 558k | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
614 | 558k | int i; |
615 | | |
616 | | // Our resulting LUT would be same gridpoints on all dimensions |
617 | 8.93M | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
618 | 8.37M | Dimensions[i] = nGridPoints; |
619 | | |
620 | 558k | return cmsStageAllocCLut16bitGranular(ContextID, Dimensions, inputChan, outputChan, Table); |
621 | 558k | } |
622 | | |
623 | | |
624 | | cmsStage* CMSEXPORT cmsStageAllocCLutFloat(cmsContext ContextID, |
625 | | cmsUInt32Number nGridPoints, |
626 | | cmsUInt32Number inputChan, |
627 | | cmsUInt32Number outputChan, |
628 | | const cmsFloat32Number* Table) |
629 | 0 | { |
630 | 0 | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
631 | 0 | int i; |
632 | | |
633 | | // Our resulting LUT would be same gridpoints on all dimensions |
634 | 0 | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
635 | 0 | Dimensions[i] = nGridPoints; |
636 | |
|
637 | 0 | return cmsStageAllocCLutFloatGranular(ContextID, Dimensions, inputChan, outputChan, Table); |
638 | 0 | } |
639 | | |
640 | | |
641 | | |
642 | | cmsStage* CMSEXPORT cmsStageAllocCLutFloatGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table) |
643 | 0 | { |
644 | 0 | cmsUInt32Number i, n; |
645 | 0 | _cmsStageCLutData* NewElem; |
646 | 0 | cmsStage* NewMPE; |
647 | |
|
648 | 0 | _cmsAssert(clutPoints != NULL); |
649 | |
|
650 | 0 | if (inputChan > MAX_INPUT_DIMENSIONS) { |
651 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS); |
652 | 0 | return NULL; |
653 | 0 | } |
654 | | |
655 | 0 | NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan, |
656 | 0 | EvaluateCLUTfloat, CLUTElemDup, CLutElemTypeFree, NULL); |
657 | 0 | if (NewMPE == NULL) return NULL; |
658 | | |
659 | | |
660 | 0 | NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData)); |
661 | 0 | if (NewElem == NULL) { |
662 | 0 | cmsStageFree(ContextID, NewMPE); |
663 | 0 | return NULL; |
664 | 0 | } |
665 | | |
666 | 0 | NewMPE ->Data = (void*) NewElem; |
667 | | |
668 | | // There is a potential integer overflow on conputing n and nEntries. |
669 | 0 | NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan); |
670 | 0 | NewElem -> HasFloatValues = TRUE; |
671 | |
|
672 | 0 | if (n == 0) { |
673 | 0 | cmsStageFree(ContextID, NewMPE); |
674 | 0 | return NULL; |
675 | 0 | } |
676 | | |
677 | 0 | NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat32Number)); |
678 | 0 | if (NewElem ->Tab.TFloat == NULL) { |
679 | 0 | cmsStageFree(ContextID, NewMPE); |
680 | 0 | return NULL; |
681 | 0 | } |
682 | | |
683 | 0 | if (Table != NULL) { |
684 | 0 | for (i=0; i < n; i++) { |
685 | 0 | NewElem ->Tab.TFloat[i] = Table[i]; |
686 | 0 | } |
687 | 0 | } |
688 | |
|
689 | 0 | NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT); |
690 | 0 | if (NewElem ->Params == NULL) { |
691 | 0 | cmsStageFree(ContextID, NewMPE); |
692 | 0 | return NULL; |
693 | 0 | } |
694 | | |
695 | 0 | return NewMPE; |
696 | 0 | } |
697 | | |
698 | | |
699 | | static |
700 | | int IdentitySampler(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo) |
701 | 8.22M | { |
702 | 8.22M | int nChan = *(int*) Cargo; |
703 | 8.22M | int i; |
704 | 8.22M | cmsUNUSED_PARAMETER(ContextID); |
705 | | |
706 | 32.9M | for (i=0; i < nChan; i++) |
707 | 24.6M | Out[i] = In[i]; |
708 | | |
709 | 8.22M | return 1; |
710 | 8.22M | } |
711 | | |
712 | | // Creates an MPE that just copies input to output |
713 | | cmsStage* CMSEXPORT _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan) |
714 | 1.02M | { |
715 | 1.02M | cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS]; |
716 | 1.02M | cmsStage* mpe ; |
717 | 1.02M | int i; |
718 | | |
719 | 16.4M | for (i=0; i < MAX_INPUT_DIMENSIONS; i++) |
720 | 15.4M | Dimensions[i] = 2; |
721 | | |
722 | 1.02M | mpe = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, nChan, nChan, NULL); |
723 | 1.02M | if (mpe == NULL) return NULL; |
724 | | |
725 | 1.02M | if (!cmsStageSampleCLut16bit(ContextID, mpe, IdentitySampler, &nChan, 0)) { |
726 | 0 | cmsStageFree(ContextID, mpe); |
727 | 0 | return NULL; |
728 | 0 | } |
729 | | |
730 | 1.02M | mpe ->Implements = cmsSigIdentityElemType; |
731 | 1.02M | return mpe; |
732 | 1.02M | } |
733 | | |
734 | | |
735 | | |
736 | | // Quantize a value 0 <= i < MaxSamples to 0..0xffff |
737 | | cmsUInt16Number CMSEXPORT _cmsQuantizeVal(cmsFloat64Number i, cmsUInt32Number MaxSamples) |
738 | 21.3G | { |
739 | 21.3G | cmsFloat64Number x; |
740 | | |
741 | 21.3G | x = ((cmsFloat64Number) i * 65535.) / (cmsFloat64Number) (MaxSamples - 1); |
742 | 21.3G | return _cmsQuickSaturateWord(x); |
743 | 21.3G | } |
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(cmsContext ContextID, cmsStage* mpe, cmsSAMPLER16 Sampler, void * Cargo, cmsUInt32Number dwFlags) |
749 | 1.53M | { |
750 | 1.53M | int i, t, index, rest; |
751 | 1.53M | cmsUInt32Number nTotalPoints; |
752 | 1.53M | cmsUInt32Number nInputs, nOutputs; |
753 | 1.53M | cmsUInt32Number* nSamples; |
754 | 1.53M | cmsUInt16Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS]; |
755 | 1.53M | _cmsStageCLutData* clut; |
756 | | |
757 | 1.53M | if (mpe == NULL) return FALSE; |
758 | | |
759 | 1.53M | clut = (_cmsStageCLutData*) mpe->Data; |
760 | | |
761 | 1.53M | if (clut == NULL) return FALSE; |
762 | | |
763 | 1.53M | nSamples = clut->Params ->nSamples; |
764 | 1.53M | nInputs = clut->Params ->nInputs; |
765 | 1.53M | nOutputs = clut->Params ->nOutputs; |
766 | | |
767 | 1.53M | if (nInputs <= 0) return FALSE; |
768 | 1.53M | if (nOutputs <= 0) return FALSE; |
769 | 1.53M | if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE; |
770 | 1.53M | if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE; |
771 | | |
772 | 1.53M | memset(In, 0, sizeof(In)); |
773 | 1.53M | memset(Out, 0, sizeof(Out)); |
774 | | |
775 | 1.53M | nTotalPoints = CubeSize(nSamples, nInputs); |
776 | 1.53M | if (nTotalPoints == 0) return FALSE; |
777 | | |
778 | 1.53M | index = 0; |
779 | 6.11G | for (i = 0; i < (int) nTotalPoints; i++) { |
780 | | |
781 | 6.11G | rest = i; |
782 | 27.0G | for (t = (int)nInputs - 1; t >= 0; --t) { |
783 | | |
784 | 20.9G | cmsUInt32Number Colorant = rest % nSamples[t]; |
785 | | |
786 | 20.9G | rest /= nSamples[t]; |
787 | | |
788 | 20.9G | In[t] = _cmsQuantizeVal(Colorant, nSamples[t]); |
789 | 20.9G | } |
790 | | |
791 | 6.11G | if (clut ->Tab.T != NULL) { |
792 | 18.4G | for (t = 0; t < (int)nOutputs; t++) |
793 | 12.3G | Out[t] = clut->Tab.T[index + t]; |
794 | 6.11G | } |
795 | | |
796 | 6.11G | if (!Sampler(ContextID, In, Out, Cargo)) |
797 | 0 | return FALSE; |
798 | | |
799 | 6.11G | if (!(dwFlags & SAMPLER_INSPECT)) { |
800 | | |
801 | 6.11G | if (clut ->Tab.T != NULL) { |
802 | 18.4G | for (t=0; t < (int) nOutputs; t++) |
803 | 12.3G | clut->Tab.T[index + t] = Out[t]; |
804 | 6.11G | } |
805 | 6.11G | } |
806 | | |
807 | 6.11G | index += nOutputs; |
808 | 6.11G | } |
809 | | |
810 | 1.53M | return TRUE; |
811 | 1.53M | } |
812 | | |
813 | | // Same as anterior, but for floating point |
814 | | cmsBool CMSEXPORT cmsStageSampleCLutFloat(cmsContext ContextID, 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(ContextID, 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(cmsContext ContextID, 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(ContextID, In, NULL, Cargo)) |
905 | 0 | return FALSE; |
906 | 0 | } |
907 | | |
908 | 0 | return TRUE; |
909 | 0 | } |
910 | | |
911 | | cmsInt32Number CMSEXPORT cmsSliceSpaceFloat(cmsContext ContextID, 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(ContextID, 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(cmsContext ContextID, const cmsFloat32Number In[], |
949 | | cmsFloat32Number Out[], |
950 | | const cmsStage *mpe) |
951 | 2.31G | { |
952 | 2.31G | cmsCIELab Lab; |
953 | 2.31G | cmsCIEXYZ XYZ; |
954 | 2.31G | const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ; |
955 | | |
956 | | // V4 rules |
957 | 2.31G | Lab.L = In[0] * 100.0; |
958 | 2.31G | Lab.a = In[1] * 255.0 - 128.0; |
959 | 2.31G | Lab.b = In[2] * 255.0 - 128.0; |
960 | | |
961 | 2.31G | cmsLab2XYZ(ContextID, 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 | 2.31G | Out[0] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.X / XYZadj); |
967 | 2.31G | Out[1] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Y / XYZadj); |
968 | 2.31G | Out[2] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Z / XYZadj); |
969 | 2.31G | 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 | 9.78k | { |
978 | 9.78k | return _cmsStageAllocPlaceholder(ContextID, cmsSigLab2XYZElemType, 3, 3, EvaluateLab2XYZ, NULL, NULL, NULL); |
979 | 9.78k | } |
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(ContextID, 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(ContextID, 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 | 1.05M | { |
1029 | 1.05M | static const cmsFloat64Number V2ToV4[] = { 65535.0/65280.0, 0, 0, |
1030 | 1.05M | 0, 65535.0/65280.0, 0, |
1031 | 1.05M | 0, 0, 65535.0/65280.0 |
1032 | 1.05M | }; |
1033 | | |
1034 | 1.05M | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V2ToV4, NULL); |
1035 | | |
1036 | 1.05M | if (mpe == NULL) return mpe; |
1037 | 1.05M | mpe ->Implements = cmsSigLabV2toV4; |
1038 | 1.05M | return mpe; |
1039 | 1.05M | } |
1040 | | |
1041 | | |
1042 | | // Reverse direction |
1043 | | cmsStage* CMSEXPORT _cmsStageAllocLabV4ToV2(cmsContext ContextID) |
1044 | 1.02M | { |
1045 | 1.02M | static const cmsFloat64Number V4ToV2[] = { 65280.0/65535.0, 0, 0, |
1046 | 1.02M | 0, 65280.0/65535.0, 0, |
1047 | 1.02M | 0, 0, 65280.0/65535.0 |
1048 | 1.02M | }; |
1049 | | |
1050 | 1.02M | cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V4ToV2, NULL); |
1051 | | |
1052 | 1.02M | if (mpe == NULL) return mpe; |
1053 | 1.02M | mpe ->Implements = cmsSigLabV4toV2; |
1054 | 1.02M | return mpe; |
1055 | 1.02M | } |
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(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
1142 | 0 | { |
1143 | 0 | cmsUInt32Number i; |
1144 | 0 | cmsUNUSED_PARAMETER(ContextID); |
1145 | 0 | for (i = 0; i < mpe->InputChannels; i++) { |
1146 | |
|
1147 | 0 | cmsFloat32Number n = In[i]; |
1148 | 0 | Out[i] = n < 0 ? 0 : n; |
1149 | 0 | } |
1150 | 0 | } |
1151 | | |
1152 | | cmsStage* _cmsStageClipNegatives(cmsContext ContextID, cmsUInt32Number nChannels) |
1153 | 0 | { |
1154 | 0 | return _cmsStageAllocPlaceholder(ContextID, cmsSigClipNegativesElemType, |
1155 | 0 | nChannels, nChannels, Clipper, NULL, NULL, NULL); |
1156 | 0 | } |
1157 | | |
1158 | | // ******************************************************************************** |
1159 | | // Type cmsSigXYZ2LabElemType |
1160 | | // ******************************************************************************** |
1161 | | |
1162 | | static |
1163 | | void EvaluateXYZ2Lab(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe) |
1164 | 448M | { |
1165 | 448M | cmsCIELab Lab; |
1166 | 448M | cmsCIEXYZ XYZ; |
1167 | 448M | const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ; |
1168 | | |
1169 | | // From 0..1.0 to XYZ |
1170 | | |
1171 | 448M | XYZ.X = In[0] * XYZadj; |
1172 | 448M | XYZ.Y = In[1] * XYZadj; |
1173 | 448M | XYZ.Z = In[2] * XYZadj; |
1174 | | |
1175 | 448M | cmsXYZ2Lab(ContextID, NULL, &Lab, &XYZ); |
1176 | | |
1177 | | // From V4 Lab to 0..1.0 |
1178 | | |
1179 | 448M | Out[0] = (cmsFloat32Number) (Lab.L / 100.0); |
1180 | 448M | Out[1] = (cmsFloat32Number) ((Lab.a + 128.0) / 255.0); |
1181 | 448M | Out[2] = (cmsFloat32Number) ((Lab.b + 128.0) / 255.0); |
1182 | 448M | return; |
1183 | | |
1184 | 0 | cmsUNUSED_PARAMETER(mpe); |
1185 | 0 | } |
1186 | | |
1187 | | cmsStage* CMSEXPORT _cmsStageAllocXYZ2Lab(cmsContext ContextID) |
1188 | 1.03M | { |
1189 | 1.03M | return _cmsStageAllocPlaceholder(ContextID, cmsSigXYZ2LabElemType, 3, 3, EvaluateXYZ2Lab, NULL, NULL, NULL); |
1190 | | |
1191 | 1.03M | } |
1192 | | |
1193 | | // ******************************************************************************** |
1194 | | |
1195 | | // For v4, S-Shaped curves are placed in a/b axis to increase resolution near gray |
1196 | | |
1197 | | cmsStage* _cmsStageAllocLabPrelin(cmsContext ContextID) |
1198 | 0 | { |
1199 | 0 | cmsToneCurve* LabTable[3]; |
1200 | 0 | cmsFloat64Number Params[1] = {2.4} ; |
1201 | |
|
1202 | 0 | LabTable[0] = cmsBuildGamma(ContextID, 1.0); |
1203 | 0 | LabTable[1] = cmsBuildParametricToneCurve(ContextID, 108, Params); |
1204 | 0 | LabTable[2] = cmsBuildParametricToneCurve(ContextID, 108, Params); |
1205 | |
|
1206 | 0 | return cmsStageAllocToneCurves(ContextID, 3, LabTable); |
1207 | 0 | } |
1208 | | |
1209 | | |
1210 | | // Free a single MPE |
1211 | | void CMSEXPORT cmsStageFree(cmsContext ContextID, cmsStage* mpe) |
1212 | 20.4M | { |
1213 | 20.4M | if (mpe ->FreePtr) |
1214 | 19.4M | mpe ->FreePtr(ContextID, mpe); |
1215 | | |
1216 | 20.4M | _cmsFree(ContextID, mpe); |
1217 | 20.4M | } |
1218 | | |
1219 | | |
1220 | | cmsUInt32Number CMSEXPORT cmsStageInputChannels(cmsContext ContextID, const cmsStage* mpe) |
1221 | 579k | { |
1222 | 579k | cmsUNUSED_PARAMETER(ContextID); |
1223 | 579k | return mpe ->InputChannels; |
1224 | 579k | } |
1225 | | |
1226 | | cmsUInt32Number CMSEXPORT cmsStageOutputChannels(cmsContext ContextID, const cmsStage* mpe) |
1227 | 135k | { |
1228 | 135k | cmsUNUSED_PARAMETER(ContextID); |
1229 | 135k | return mpe ->OutputChannels; |
1230 | 135k | } |
1231 | | |
1232 | | cmsStageSignature CMSEXPORT cmsStageType(cmsContext ContextID, const cmsStage* mpe) |
1233 | 11.0M | { |
1234 | 11.0M | cmsUNUSED_PARAMETER(ContextID); |
1235 | 11.0M | return mpe -> Type; |
1236 | 11.0M | } |
1237 | | |
1238 | | void* CMSEXPORT cmsStageData(cmsContext ContextID, const cmsStage* mpe) |
1239 | 1.07M | { |
1240 | 1.07M | cmsUNUSED_PARAMETER(ContextID); |
1241 | 1.07M | return mpe -> Data; |
1242 | 1.07M | } |
1243 | | |
1244 | | cmsStage* CMSEXPORT cmsStageNext(cmsContext ContextID, const cmsStage* mpe) |
1245 | 10.5M | { |
1246 | 10.5M | cmsUNUSED_PARAMETER(ContextID); |
1247 | 10.5M | return mpe -> Next; |
1248 | 10.5M | } |
1249 | | |
1250 | | |
1251 | | // Duplicates an MPE |
1252 | | cmsStage* CMSEXPORT cmsStageDup(cmsContext ContextID, cmsStage* mpe) |
1253 | 10.7M | { |
1254 | 10.7M | cmsStage* NewMPE; |
1255 | | |
1256 | 10.7M | if (mpe == NULL) return NULL; |
1257 | 10.7M | NewMPE = _cmsStageAllocPlaceholder(ContextID, |
1258 | 10.7M | mpe ->Type, |
1259 | 10.7M | mpe ->InputChannels, |
1260 | 10.7M | mpe ->OutputChannels, |
1261 | 10.7M | mpe ->EvalPtr, |
1262 | 10.7M | mpe ->DupElemPtr, |
1263 | 10.7M | mpe ->FreePtr, |
1264 | 10.7M | NULL); |
1265 | 10.7M | if (NewMPE == NULL) return NULL; |
1266 | | |
1267 | 10.7M | NewMPE ->Implements = mpe ->Implements; |
1268 | | |
1269 | 10.7M | if (mpe ->DupElemPtr) { |
1270 | | |
1271 | 10.7M | NewMPE ->Data = mpe ->DupElemPtr(ContextID, mpe); |
1272 | | |
1273 | 10.7M | if (NewMPE->Data == NULL) { |
1274 | |
|
1275 | 0 | cmsStageFree(ContextID, NewMPE); |
1276 | 0 | return NULL; |
1277 | 0 | } |
1278 | | |
1279 | 10.7M | } else { |
1280 | | |
1281 | 21 | NewMPE ->Data = NULL; |
1282 | 21 | } |
1283 | | |
1284 | 10.7M | return NewMPE; |
1285 | 10.7M | } |
1286 | | |
1287 | | |
1288 | | // *********************************************************************************************************** |
1289 | | |
1290 | | // This function sets up the channel count |
1291 | | static |
1292 | | cmsBool BlessLUT(cmsContext ContextID, cmsPipeline* lut) |
1293 | 33.5M | { |
1294 | | // We can set the input/output channels only if we have elements. |
1295 | 33.5M | if (lut ->Elements != NULL) { |
1296 | | |
1297 | 23.6M | cmsStage* prev; |
1298 | 23.6M | cmsStage* next; |
1299 | 23.6M | cmsStage* First; |
1300 | 23.6M | cmsStage* Last; |
1301 | | |
1302 | 23.6M | First = cmsPipelineGetPtrToFirstStage(ContextID, lut); |
1303 | 23.6M | Last = cmsPipelineGetPtrToLastStage(ContextID, lut); |
1304 | | |
1305 | 23.6M | if (First == NULL || Last == NULL) return FALSE; |
1306 | | |
1307 | 23.6M | lut->InputChannels = First->InputChannels; |
1308 | 23.6M | lut->OutputChannels = Last->OutputChannels; |
1309 | | |
1310 | | // Check chain consistency |
1311 | 23.6M | prev = First; |
1312 | 23.6M | next = prev->Next; |
1313 | | |
1314 | 58.3M | while (next != NULL) |
1315 | 34.7M | { |
1316 | 34.7M | if (next->InputChannels != prev->OutputChannels) |
1317 | 344 | return FALSE; |
1318 | | |
1319 | 34.7M | next = next->Next; |
1320 | 34.7M | prev = prev->Next; |
1321 | 34.7M | } |
1322 | 23.6M | } |
1323 | | |
1324 | 33.5M | return TRUE; |
1325 | 33.5M | } |
1326 | | |
1327 | | |
1328 | | // Default to evaluate the LUT on 16 bit-basis. Precision is retained. |
1329 | | static |
1330 | | void _LUTeval16(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER const void* D) |
1331 | 1.03M | { |
1332 | 1.03M | cmsPipeline* lut = (cmsPipeline*) D; |
1333 | 1.03M | cmsStage *mpe; |
1334 | 1.03M | cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS]; |
1335 | 1.03M | int Phase = 0, NextPhase; |
1336 | | |
1337 | 1.03M | From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels); |
1338 | | |
1339 | 1.03M | for (mpe = lut ->Elements; |
1340 | 7.22M | mpe != NULL; |
1341 | 6.18M | mpe = mpe ->Next) { |
1342 | | |
1343 | 6.18M | NextPhase = Phase ^ 1; |
1344 | 6.18M | mpe ->EvalPtr(ContextID, &Storage[Phase][0], &Storage[NextPhase][0], mpe); |
1345 | 6.18M | Phase = NextPhase; |
1346 | 6.18M | } |
1347 | | |
1348 | | |
1349 | 1.03M | FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels); |
1350 | 1.03M | } |
1351 | | |
1352 | | |
1353 | | |
1354 | | // Does evaluate the LUT on cmsFloat32Number-basis. |
1355 | | static |
1356 | | void _LUTevalFloat(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const void* D) |
1357 | 6.23G | { |
1358 | 6.23G | cmsPipeline* lut = (cmsPipeline*) D; |
1359 | 6.23G | cmsStage *mpe; |
1360 | 6.23G | cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS]; |
1361 | 6.23G | int Phase = 0, NextPhase; |
1362 | | |
1363 | 6.23G | memmove(&Storage[Phase][0], In, lut ->InputChannels * sizeof(cmsFloat32Number)); |
1364 | | |
1365 | 6.23G | for (mpe = lut ->Elements; |
1366 | 42.2G | mpe != NULL; |
1367 | 36.0G | mpe = mpe ->Next) { |
1368 | | |
1369 | 36.0G | NextPhase = Phase ^ 1; |
1370 | 36.0G | mpe ->EvalPtr(ContextID, &Storage[Phase][0], &Storage[NextPhase][0], mpe); |
1371 | 36.0G | Phase = NextPhase; |
1372 | 36.0G | } |
1373 | | |
1374 | 6.23G | memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number)); |
1375 | 6.23G | } |
1376 | | |
1377 | | |
1378 | | // LUT Creation & Destruction |
1379 | | cmsPipeline* CMSEXPORT cmsPipelineAlloc(cmsContext ContextID, cmsUInt32Number InputChannels, cmsUInt32Number OutputChannels) |
1380 | 9.94M | { |
1381 | 9.94M | cmsPipeline* NewLUT; |
1382 | | |
1383 | | // A value of zero in channels is allowed as placeholder |
1384 | 9.94M | if (InputChannels >= cmsMAXCHANNELS || |
1385 | 9.94M | OutputChannels >= cmsMAXCHANNELS) return NULL; |
1386 | | |
1387 | 9.94M | NewLUT = (cmsPipeline*) _cmsMallocZero(ContextID, sizeof(cmsPipeline)); |
1388 | 9.94M | if (NewLUT == NULL) return NULL; |
1389 | | |
1390 | 9.94M | NewLUT -> InputChannels = InputChannels; |
1391 | 9.94M | NewLUT -> OutputChannels = OutputChannels; |
1392 | | |
1393 | 9.94M | NewLUT ->Eval16Fn = _LUTeval16; |
1394 | 9.94M | NewLUT ->EvalFloatFn = _LUTevalFloat; |
1395 | 9.94M | NewLUT ->DupDataFn = NULL; |
1396 | 9.94M | NewLUT ->FreeDataFn = NULL; |
1397 | 9.94M | NewLUT ->Data = NewLUT; |
1398 | | |
1399 | 9.94M | if (!BlessLUT(ContextID, NewLUT)) |
1400 | 0 | { |
1401 | 0 | _cmsFree(ContextID, NewLUT); |
1402 | 0 | return NULL; |
1403 | 0 | } |
1404 | | |
1405 | 9.94M | return NewLUT; |
1406 | 9.94M | } |
1407 | | |
1408 | | cmsUInt32Number CMSEXPORT cmsPipelineInputChannels(cmsContext ContextID, const cmsPipeline* lut) |
1409 | 1.64M | { |
1410 | 1.64M | cmsUNUSED_PARAMETER(ContextID); |
1411 | 1.64M | _cmsAssert(lut != NULL); |
1412 | 1.64M | return lut ->InputChannels; |
1413 | 1.64M | } |
1414 | | |
1415 | | cmsUInt32Number CMSEXPORT cmsPipelineOutputChannels(cmsContext ContextID, const cmsPipeline* lut) |
1416 | 3.29M | { |
1417 | 3.29M | cmsUNUSED_PARAMETER(ContextID); |
1418 | 3.29M | _cmsAssert(lut != NULL); |
1419 | 3.29M | return lut ->OutputChannels; |
1420 | 3.29M | } |
1421 | | |
1422 | | // Free a profile elements LUT |
1423 | | void CMSEXPORT cmsPipelineFree(cmsContext ContextID, cmsPipeline* lut) |
1424 | 9.94M | { |
1425 | 9.94M | cmsStage *mpe, *Next; |
1426 | | |
1427 | 9.94M | if (lut == NULL) return; |
1428 | | |
1429 | 9.94M | for (mpe = lut ->Elements; |
1430 | 27.1M | mpe != NULL; |
1431 | 17.2M | mpe = Next) { |
1432 | | |
1433 | 17.2M | Next = mpe ->Next; |
1434 | 17.2M | cmsStageFree(ContextID, mpe); |
1435 | 17.2M | } |
1436 | | |
1437 | 9.94M | if (lut ->FreeDataFn) lut ->FreeDataFn(ContextID, lut ->Data); |
1438 | | |
1439 | 9.94M | _cmsFree(ContextID, lut); |
1440 | 9.94M | } |
1441 | | |
1442 | | |
1443 | | // Default to evaluate the LUT on 16 bit-basis. |
1444 | | void CMSEXPORT cmsPipelineEval16(cmsContext ContextID, const cmsUInt16Number In[], cmsUInt16Number Out[], const cmsPipeline* lut) |
1445 | 511k | { |
1446 | 511k | _cmsAssert(lut != NULL); |
1447 | 511k | lut ->Eval16Fn(ContextID, In, Out, lut->Data); |
1448 | 511k | } |
1449 | | |
1450 | | |
1451 | | // Does evaluate the LUT on cmsFloat32Number-basis. |
1452 | | void CMSEXPORT cmsPipelineEvalFloat(cmsContext ContextID, const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsPipeline* lut) |
1453 | 6.23G | { |
1454 | 6.23G | _cmsAssert(lut != NULL); |
1455 | 6.23G | lut ->EvalFloatFn(ContextID, In, Out, lut); |
1456 | 6.23G | } |
1457 | | |
1458 | | // Duplicates a LUT |
1459 | | cmsPipeline* CMSEXPORT cmsPipelineDup(cmsContext ContextID, const cmsPipeline* lut) |
1460 | 2.81M | { |
1461 | 2.81M | cmsPipeline* NewLUT; |
1462 | 2.81M | cmsStage *NewMPE, *Anterior = NULL, *mpe; |
1463 | 2.81M | cmsBool First = TRUE; |
1464 | | |
1465 | 2.81M | if (lut == NULL) return NULL; |
1466 | | |
1467 | 2.81M | NewLUT = cmsPipelineAlloc(ContextID, lut ->InputChannels, lut ->OutputChannels); |
1468 | 2.81M | if (NewLUT == NULL) return NULL; |
1469 | | |
1470 | 2.81M | for (mpe = lut ->Elements; |
1471 | 5.81M | mpe != NULL; |
1472 | 2.99M | mpe = mpe ->Next) { |
1473 | | |
1474 | 2.99M | NewMPE = cmsStageDup(ContextID, mpe); |
1475 | | |
1476 | 2.99M | if (NewMPE == NULL) { |
1477 | 0 | cmsPipelineFree(ContextID, NewLUT); |
1478 | 0 | return NULL; |
1479 | 0 | } |
1480 | | |
1481 | 2.99M | if (First) { |
1482 | 2.81M | NewLUT ->Elements = NewMPE; |
1483 | 2.81M | First = FALSE; |
1484 | 2.81M | } |
1485 | 176k | else { |
1486 | 176k | if (Anterior != NULL) |
1487 | 176k | Anterior ->Next = NewMPE; |
1488 | 176k | } |
1489 | | |
1490 | 2.99M | Anterior = NewMPE; |
1491 | 2.99M | } |
1492 | | |
1493 | 2.81M | NewLUT ->Eval16Fn = lut ->Eval16Fn; |
1494 | 2.81M | NewLUT ->EvalFloatFn = lut ->EvalFloatFn; |
1495 | 2.81M | NewLUT ->DupDataFn = lut ->DupDataFn; |
1496 | 2.81M | NewLUT ->FreeDataFn = lut ->FreeDataFn; |
1497 | | |
1498 | 2.81M | if (NewLUT ->DupDataFn != NULL) |
1499 | 0 | NewLUT ->Data = NewLUT ->DupDataFn(ContextID, lut->Data); |
1500 | | |
1501 | | |
1502 | 2.81M | NewLUT ->SaveAs8Bits = lut ->SaveAs8Bits; |
1503 | | |
1504 | 2.81M | if (!BlessLUT(ContextID, NewLUT)) |
1505 | 0 | { |
1506 | 0 | cmsPipelineFree(ContextID, NewLUT); |
1507 | 0 | return NULL; |
1508 | 0 | } |
1509 | | |
1510 | 2.81M | return NewLUT; |
1511 | 2.81M | } |
1512 | | |
1513 | | |
1514 | | int CMSEXPORT cmsPipelineInsertStage(cmsContext ContextID, cmsPipeline* lut, cmsStageLoc loc, cmsStage* mpe) |
1515 | 17.4M | { |
1516 | 17.4M | cmsStage* Anterior = NULL, *pt; |
1517 | | |
1518 | 17.4M | if (lut == NULL || mpe == NULL) |
1519 | 2.56k | return FALSE; |
1520 | | |
1521 | 17.4M | switch (loc) { |
1522 | | |
1523 | 2.66M | case cmsAT_BEGIN: |
1524 | 2.66M | mpe ->Next = lut ->Elements; |
1525 | 2.66M | lut ->Elements = mpe; |
1526 | 2.66M | break; |
1527 | | |
1528 | 14.8M | case cmsAT_END: |
1529 | | |
1530 | 14.8M | if (lut ->Elements == NULL) |
1531 | 4.34M | lut ->Elements = mpe; |
1532 | 10.4M | else { |
1533 | | |
1534 | 10.4M | for (pt = lut ->Elements; |
1535 | 34.9M | pt != NULL; |
1536 | 24.5M | pt = pt -> Next) Anterior = pt; |
1537 | | |
1538 | 10.4M | Anterior ->Next = mpe; |
1539 | 10.4M | mpe ->Next = NULL; |
1540 | 10.4M | } |
1541 | 14.8M | break; |
1542 | 0 | default:; |
1543 | 0 | return FALSE; |
1544 | 17.4M | } |
1545 | | |
1546 | 17.4M | return BlessLUT(ContextID, lut); |
1547 | 17.4M | } |
1548 | | |
1549 | | // Unlink an element and return the pointer to it |
1550 | | void CMSEXPORT cmsPipelineUnlinkStage(cmsContext ContextID, cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe) |
1551 | 0 | { |
1552 | 0 | cmsStage *Anterior, *pt, *Last; |
1553 | 0 | cmsStage *Unlinked = NULL; |
1554 | | |
1555 | | |
1556 | | // If empty LUT, there is nothing to remove |
1557 | 0 | if (lut ->Elements == NULL) { |
1558 | 0 | if (mpe) *mpe = NULL; |
1559 | 0 | return; |
1560 | 0 | } |
1561 | | |
1562 | | // On depending on the strategy... |
1563 | 0 | switch (loc) { |
1564 | | |
1565 | 0 | case cmsAT_BEGIN: |
1566 | 0 | { |
1567 | 0 | cmsStage* elem = lut ->Elements; |
1568 | |
|
1569 | 0 | lut ->Elements = elem -> Next; |
1570 | 0 | elem ->Next = NULL; |
1571 | 0 | Unlinked = elem; |
1572 | |
|
1573 | 0 | } |
1574 | 0 | break; |
1575 | | |
1576 | 0 | case cmsAT_END: |
1577 | 0 | Anterior = Last = NULL; |
1578 | 0 | for (pt = lut ->Elements; |
1579 | 0 | pt != NULL; |
1580 | 0 | pt = pt -> Next) { |
1581 | 0 | Anterior = Last; |
1582 | 0 | Last = pt; |
1583 | 0 | } |
1584 | |
|
1585 | 0 | Unlinked = Last; // Next already points to NULL |
1586 | | |
1587 | | // Truncate the chain |
1588 | 0 | if (Anterior) |
1589 | 0 | Anterior ->Next = NULL; |
1590 | 0 | else |
1591 | 0 | lut ->Elements = NULL; |
1592 | 0 | break; |
1593 | 0 | default:; |
1594 | 0 | } |
1595 | | |
1596 | 0 | if (mpe) |
1597 | 0 | *mpe = Unlinked; |
1598 | 0 | else |
1599 | 0 | cmsStageFree(ContextID, Unlinked); |
1600 | | |
1601 | | // May fail, but we ignore it |
1602 | 0 | BlessLUT(ContextID, lut); |
1603 | 0 | } |
1604 | | |
1605 | | |
1606 | | // Concatenate two LUT into a new single one |
1607 | | cmsBool CMSEXPORT cmsPipelineCat(cmsContext ContextID, cmsPipeline* l1, const cmsPipeline* l2) |
1608 | 3.34M | { |
1609 | 3.34M | cmsStage* mpe; |
1610 | | |
1611 | | // If both LUTS does not have elements, we need to inherit |
1612 | | // the number of channels |
1613 | 3.34M | if (l1 ->Elements == NULL && l2 ->Elements == NULL) { |
1614 | 3 | l1 ->InputChannels = l2 ->InputChannels; |
1615 | 3 | l1 ->OutputChannels = l2 ->OutputChannels; |
1616 | 3 | } |
1617 | | |
1618 | | // Cat second |
1619 | 3.34M | for (mpe = l2 ->Elements; |
1620 | 11.0M | mpe != NULL; |
1621 | 7.74M | mpe = mpe ->Next) { |
1622 | | |
1623 | | // We have to dup each element |
1624 | 7.74M | if (!cmsPipelineInsertStage(ContextID, l1, cmsAT_END, cmsStageDup(ContextID, mpe))) |
1625 | 30 | return FALSE; |
1626 | 7.74M | } |
1627 | | |
1628 | 3.34M | return BlessLUT(ContextID, l1); |
1629 | 3.34M | } |
1630 | | |
1631 | | |
1632 | | cmsBool CMSEXPORT cmsPipelineSetSaveAs8bitsFlag(cmsContext ContextID, cmsPipeline* lut, cmsBool On) |
1633 | 0 | { |
1634 | 0 | cmsBool Anterior = lut ->SaveAs8Bits; |
1635 | 0 | cmsUNUSED_PARAMETER(ContextID); |
1636 | |
|
1637 | 0 | lut ->SaveAs8Bits = On; |
1638 | 0 | return Anterior; |
1639 | 0 | } |
1640 | | |
1641 | | |
1642 | | cmsStage* CMSEXPORT cmsPipelineGetPtrToFirstStage(cmsContext ContextID, const cmsPipeline* lut) |
1643 | 26.9M | { |
1644 | 26.9M | cmsUNUSED_PARAMETER(ContextID); |
1645 | 26.9M | return lut ->Elements; |
1646 | 26.9M | } |
1647 | | |
1648 | | cmsStage* CMSEXPORT cmsPipelineGetPtrToLastStage(cmsContext ContextID, const cmsPipeline* lut) |
1649 | 23.6M | { |
1650 | 23.6M | cmsStage *mpe, *Anterior = NULL; |
1651 | 23.6M | cmsUNUSED_PARAMETER(ContextID); |
1652 | | |
1653 | 82.0M | for (mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next) |
1654 | 58.3M | Anterior = mpe; |
1655 | | |
1656 | 23.6M | return Anterior; |
1657 | 23.6M | } |
1658 | | |
1659 | | cmsUInt32Number CMSEXPORT cmsPipelineStageCount(cmsContext ContextID, const cmsPipeline* lut) |
1660 | 1.01M | { |
1661 | 1.01M | cmsStage *mpe; |
1662 | 1.01M | cmsUInt32Number n; |
1663 | 1.01M | cmsUNUSED_PARAMETER(ContextID); |
1664 | | |
1665 | 3.66M | for (n=0, mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next) |
1666 | 2.64M | n++; |
1667 | | |
1668 | 1.01M | return n; |
1669 | 1.01M | } |
1670 | | |
1671 | | // This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional |
1672 | | // duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality. |
1673 | | void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsContext ContextID, |
1674 | | cmsPipeline* Lut, |
1675 | | _cmsPipelineEval16Fn Eval16, |
1676 | | void* PrivateData, |
1677 | | _cmsFreeUserDataFn FreePrivateDataFn, |
1678 | | _cmsDupUserDataFn DupPrivateDataFn) |
1679 | 540k | { |
1680 | 540k | cmsUNUSED_PARAMETER(ContextID); |
1681 | | |
1682 | 540k | Lut ->Eval16Fn = Eval16; |
1683 | 540k | Lut ->DupDataFn = DupPrivateDataFn; |
1684 | 540k | Lut ->FreeDataFn = FreePrivateDataFn; |
1685 | 540k | Lut ->Data = PrivateData; |
1686 | 540k | } |
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(cmsContext ContextID, |
1755 | | cmsFloat32Number Target[], |
1756 | | cmsFloat32Number Result[], |
1757 | | cmsFloat32Number Hint[], |
1758 | | const cmsPipeline* lut) |
1759 | 0 | { |
1760 | 0 | cmsUInt32Number i, j; |
1761 | 0 | cmsFloat64Number error, LastError = 1E20; |
1762 | 0 | cmsFloat32Number fx[4], x[4], xd[4], fxd[4]; |
1763 | 0 | cmsVEC3 tmp, tmp2; |
1764 | 0 | cmsMAT3 Jacobian; |
1765 | | |
1766 | | // Only 3->3 and 4->3 are supported |
1767 | 0 | if (lut ->InputChannels != 3 && lut ->InputChannels != 4) return FALSE; |
1768 | 0 | if (lut ->OutputChannels != 3) return FALSE; |
1769 | | |
1770 | | // Take the hint as starting point if specified |
1771 | 0 | if (Hint == NULL) { |
1772 | | |
1773 | | // Begin at any point, we choose 1/3 of CMY axis |
1774 | 0 | x[0] = x[1] = x[2] = 0.3f; |
1775 | 0 | } |
1776 | 0 | else { |
1777 | | |
1778 | | // Only copy 3 channels from hint... |
1779 | 0 | for (j=0; j < 3; j++) |
1780 | 0 | x[j] = Hint[j]; |
1781 | 0 | } |
1782 | | |
1783 | | // If Lut is 4-dimensions, then grab target[3], which is fixed |
1784 | 0 | if (lut ->InputChannels == 4) { |
1785 | 0 | x[3] = Target[3]; |
1786 | 0 | } |
1787 | 0 | else x[3] = 0; // To keep lint happy |
1788 | | |
1789 | | |
1790 | | // Iterate |
1791 | 0 | for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) { |
1792 | | |
1793 | | // Get beginning fx |
1794 | 0 | cmsPipelineEvalFloat(ContextID, x, fx, lut); |
1795 | | |
1796 | | // Compute error |
1797 | 0 | error = EuclideanDistance(fx, Target, 3); |
1798 | | |
1799 | | // If not convergent, return last safe value |
1800 | 0 | if (error >= LastError) |
1801 | 0 | break; |
1802 | | |
1803 | | // Keep latest values |
1804 | 0 | LastError = error; |
1805 | 0 | for (j=0; j < lut ->InputChannels; j++) |
1806 | 0 | Result[j] = x[j]; |
1807 | | |
1808 | | // Found an exact match? |
1809 | 0 | if (error <= 0) |
1810 | 0 | break; |
1811 | | |
1812 | | // Obtain slope (the Jacobian) |
1813 | 0 | for (j = 0; j < 3; j++) { |
1814 | |
|
1815 | 0 | xd[0] = x[0]; |
1816 | 0 | xd[1] = x[1]; |
1817 | 0 | xd[2] = x[2]; |
1818 | 0 | xd[3] = x[3]; // Keep fixed channel |
1819 | |
|
1820 | 0 | IncDelta(&xd[j]); |
1821 | |
|
1822 | 0 | cmsPipelineEvalFloat(ContextID, xd, fxd, lut); |
1823 | |
|
1824 | 0 | Jacobian.v[0].n[j] = ((fxd[0] - fx[0]) / JACOBIAN_EPSILON); |
1825 | 0 | Jacobian.v[1].n[j] = ((fxd[1] - fx[1]) / JACOBIAN_EPSILON); |
1826 | 0 | Jacobian.v[2].n[j] = ((fxd[2] - fx[2]) / JACOBIAN_EPSILON); |
1827 | 0 | } |
1828 | | |
1829 | | // Solve system |
1830 | 0 | tmp2.n[0] = fx[0] - Target[0]; |
1831 | 0 | tmp2.n[1] = fx[1] - Target[1]; |
1832 | 0 | tmp2.n[2] = fx[2] - Target[2]; |
1833 | |
|
1834 | 0 | if (!_cmsMAT3solve(ContextID, &tmp, &Jacobian, &tmp2)) |
1835 | 0 | return FALSE; |
1836 | | |
1837 | | // Move our guess |
1838 | 0 | x[0] -= (cmsFloat32Number) tmp.n[0]; |
1839 | 0 | x[1] -= (cmsFloat32Number) tmp.n[1]; |
1840 | 0 | x[2] -= (cmsFloat32Number) tmp.n[2]; |
1841 | | |
1842 | | // Some clipping.... |
1843 | 0 | for (j=0; j < 3; j++) { |
1844 | 0 | if (x[j] < 0) x[j] = 0; |
1845 | 0 | else |
1846 | 0 | if (x[j] > 1.0) x[j] = 1.0; |
1847 | 0 | } |
1848 | 0 | } |
1849 | | |
1850 | 0 | return TRUE; |
1851 | 0 | } |