2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*/
|
|
24 |
|
|
25 |
// This file is available under and governed by the GNU General Public
|
|
26 |
// License version 2 only, as published by the Free Software Foundation.
|
|
27 |
// However, the following notice accompanied the original version of this
|
|
28 |
// file:
|
|
29 |
//
|
|
30 |
//
|
|
31 |
// Little cms
|
2394
|
32 |
// Copyright (C) 1998-2007 Marti Maria
|
2
|
33 |
//
|
|
34 |
// Permission is hereby granted, free of charge, to any person obtaining
|
|
35 |
// a copy of this software and associated documentation files (the "Software"),
|
|
36 |
// to deal in the Software without restriction, including without limitation
|
|
37 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
38 |
// and/or sell copies of the Software, and to permit persons to whom the Software
|
|
39 |
// is furnished to do so, subject to the following conditions:
|
|
40 |
//
|
|
41 |
// The above copyright notice and this permission notice shall be included in
|
|
42 |
// all copies or substantial portions of the Software.
|
|
43 |
//
|
|
44 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
45 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
46 |
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
47 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
48 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
49 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
50 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
51 |
|
|
52 |
|
|
53 |
#include "lcms.h"
|
|
54 |
|
|
55 |
// Gamma handling.
|
|
56 |
|
|
57 |
LPGAMMATABLE LCMSEXPORT cmsAllocGamma(int nEntries);
|
|
58 |
void LCMSEXPORT cmsFreeGamma(LPGAMMATABLE Gamma);
|
|
59 |
void LCMSEXPORT cmsFreeGammaTriple(LPGAMMATABLE Gamma[3]);
|
|
60 |
LPGAMMATABLE LCMSEXPORT cmsBuildGamma(int nEntries, double Gamma);
|
|
61 |
LPGAMMATABLE LCMSEXPORT cmsDupGamma(LPGAMMATABLE Src);
|
|
62 |
LPGAMMATABLE LCMSEXPORT cmsReverseGamma(int nResultSamples, LPGAMMATABLE InGamma);
|
|
63 |
LPGAMMATABLE LCMSEXPORT cmsBuildParametricGamma(int nEntries, int Type, double Params[]);
|
|
64 |
LPGAMMATABLE LCMSEXPORT cmsJoinGamma(LPGAMMATABLE InGamma, LPGAMMATABLE OutGamma);
|
|
65 |
LPGAMMATABLE LCMSEXPORT cmsJoinGammaEx(LPGAMMATABLE InGamma, LPGAMMATABLE OutGamma, int nPoints);
|
2394
|
66 |
LCMSBOOL LCMSEXPORT cmsSmoothGamma(LPGAMMATABLE Tab, double lambda);
|
2
|
67 |
|
2394
|
68 |
LCMSBOOL cdecl _cmsSmoothEndpoints(LPWORD Table, int nPoints);
|
2
|
69 |
|
|
70 |
|
|
71 |
// Sampled curves
|
|
72 |
|
|
73 |
LPSAMPLEDCURVE cdecl cmsAllocSampledCurve(int nItems);
|
|
74 |
void cdecl cmsFreeSampledCurve(LPSAMPLEDCURVE p);
|
|
75 |
void cdecl cmsEndpointsOfSampledCurve(LPSAMPLEDCURVE p, double* Min, double* Max);
|
|
76 |
void cdecl cmsClampSampledCurve(LPSAMPLEDCURVE p, double Min, double Max);
|
2394
|
77 |
LCMSBOOL cdecl cmsSmoothSampledCurve(LPSAMPLEDCURVE Tab, double SmoothingLambda);
|
2
|
78 |
void cdecl cmsRescaleSampledCurve(LPSAMPLEDCURVE p, double Min, double Max, int nPoints);
|
|
79 |
|
|
80 |
LPSAMPLEDCURVE cdecl cmsJoinSampledCurves(LPSAMPLEDCURVE X, LPSAMPLEDCURVE Y, int nResultingPoints);
|
|
81 |
|
|
82 |
double LCMSEXPORT cmsEstimateGamma(LPGAMMATABLE t);
|
|
83 |
double LCMSEXPORT cmsEstimateGammaEx(LPWORD GammaTable, int nEntries, double Thereshold);
|
|
84 |
|
|
85 |
// ----------------------------------------------------------------------------------------
|
|
86 |
|
|
87 |
|
|
88 |
#define MAX_KNOTS 4096
|
|
89 |
typedef float vec[MAX_KNOTS+1];
|
|
90 |
|
|
91 |
|
|
92 |
// Ciclic-redundant-check for assuring table is a true representation of parametric curve
|
|
93 |
|
|
94 |
// The usual polynomial, which is used for AAL5, FDDI, and probably Ethernet
|
|
95 |
#define QUOTIENT 0x04c11db7
|
|
96 |
|
|
97 |
static
|
|
98 |
unsigned int Crc32(unsigned int result, LPVOID ptr, int len)
|
|
99 |
{
|
|
100 |
int i,j;
|
|
101 |
BYTE octet;
|
|
102 |
LPBYTE data = (LPBYTE) ptr;
|
|
103 |
|
|
104 |
for (i=0; i < len; i++) {
|
|
105 |
|
|
106 |
octet = *data++;
|
|
107 |
|
|
108 |
for (j=0; j < 8; j++) {
|
|
109 |
|
|
110 |
if (result & 0x80000000) {
|
|
111 |
|
|
112 |
result = (result << 1) ^ QUOTIENT ^ (octet >> 7);
|
|
113 |
}
|
|
114 |
else
|
|
115 |
{
|
|
116 |
result = (result << 1) ^ (octet >> 7);
|
|
117 |
}
|
|
118 |
octet <<= 1;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
return result;
|
|
123 |
}
|
|
124 |
|
|
125 |
// Get CRC of gamma table
|
|
126 |
|
|
127 |
unsigned int _cmsCrc32OfGammaTable(LPGAMMATABLE Table)
|
|
128 |
{
|
|
129 |
unsigned int crc = ~0U;
|
|
130 |
|
|
131 |
crc = Crc32(crc, &Table -> Seed.Type, sizeof(int));
|
|
132 |
crc = Crc32(crc, Table ->Seed.Params, sizeof(double)*10);
|
|
133 |
crc = Crc32(crc, &Table ->nEntries, sizeof(int));
|
|
134 |
crc = Crc32(crc, Table ->GammaTable, sizeof(WORD) * Table -> nEntries);
|
|
135 |
|
|
136 |
return ~crc;
|
|
137 |
|
|
138 |
}
|
|
139 |
|
|
140 |
|
|
141 |
LPGAMMATABLE LCMSEXPORT cmsAllocGamma(int nEntries)
|
|
142 |
{
|
|
143 |
LPGAMMATABLE p;
|
|
144 |
size_t size;
|
|
145 |
|
2394
|
146 |
if (nEntries > 65530 || nEntries <= 0) {
|
|
147 |
cmsSignalError(LCMS_ERRC_ABORTED, "Couldn't create gammatable of more than 65530 entries");
|
|
148 |
return NULL;
|
2
|
149 |
}
|
|
150 |
|
|
151 |
size = sizeof(GAMMATABLE) + (sizeof(WORD) * (nEntries-1));
|
|
152 |
|
2394
|
153 |
p = (LPGAMMATABLE) _cmsMalloc(size);
|
2
|
154 |
if (!p) return NULL;
|
|
155 |
|
|
156 |
ZeroMemory(p, size);
|
|
157 |
|
|
158 |
p -> Seed.Type = 0;
|
|
159 |
p -> nEntries = nEntries;
|
|
160 |
|
|
161 |
return p;
|
|
162 |
}
|
|
163 |
|
|
164 |
void LCMSEXPORT cmsFreeGamma(LPGAMMATABLE Gamma)
|
|
165 |
{
|
2394
|
166 |
if (Gamma) _cmsFree(Gamma);
|
2
|
167 |
}
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
void LCMSEXPORT cmsFreeGammaTriple(LPGAMMATABLE Gamma[3])
|
|
172 |
{
|
|
173 |
cmsFreeGamma(Gamma[0]);
|
|
174 |
cmsFreeGamma(Gamma[1]);
|
|
175 |
cmsFreeGamma(Gamma[2]);
|
|
176 |
Gamma[0] = Gamma[1] = Gamma[2] = NULL;
|
|
177 |
}
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
// Duplicate a gamma table
|
|
182 |
|
|
183 |
LPGAMMATABLE LCMSEXPORT cmsDupGamma(LPGAMMATABLE In)
|
|
184 |
{
|
|
185 |
LPGAMMATABLE Ptr;
|
|
186 |
size_t size;
|
|
187 |
|
|
188 |
Ptr = cmsAllocGamma(In -> nEntries);
|
|
189 |
if (Ptr == NULL) return NULL;
|
|
190 |
|
|
191 |
size = sizeof(GAMMATABLE) + (sizeof(WORD) * (In -> nEntries-1));
|
|
192 |
|
|
193 |
CopyMemory(Ptr, In, size);
|
|
194 |
return Ptr;
|
|
195 |
}
|
|
196 |
|
|
197 |
|
|
198 |
// Handle gamma using interpolation tables. The resulting curves can become
|
|
199 |
// very stange, but are pleasent to eye.
|
|
200 |
|
|
201 |
LPGAMMATABLE LCMSEXPORT cmsJoinGamma(LPGAMMATABLE InGamma,
|
|
202 |
LPGAMMATABLE OutGamma)
|
|
203 |
{
|
|
204 |
register int i;
|
|
205 |
L16PARAMS L16In, L16Out;
|
|
206 |
LPWORD InPtr, OutPtr;
|
|
207 |
LPGAMMATABLE p;
|
|
208 |
|
|
209 |
p = cmsAllocGamma(256);
|
|
210 |
if (!p) return NULL;
|
|
211 |
|
|
212 |
cmsCalcL16Params(InGamma -> nEntries, &L16In);
|
|
213 |
InPtr = InGamma -> GammaTable;
|
|
214 |
|
|
215 |
cmsCalcL16Params(OutGamma -> nEntries, &L16Out);
|
|
216 |
OutPtr = OutGamma-> GammaTable;
|
|
217 |
|
|
218 |
for (i=0; i < 256; i++)
|
|
219 |
{
|
|
220 |
WORD wValIn, wValOut;
|
|
221 |
|
|
222 |
wValIn = cmsLinearInterpLUT16(RGB_8_TO_16(i), InPtr, &L16In);
|
|
223 |
wValOut = cmsReverseLinearInterpLUT16(wValIn, OutPtr, &L16Out);
|
|
224 |
|
|
225 |
p -> GammaTable[i] = wValOut;
|
|
226 |
}
|
|
227 |
|
|
228 |
return p;
|
|
229 |
}
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
// New method, using smoothed parametric curves. This works FAR better.
|
|
234 |
// We want to get
|
|
235 |
//
|
|
236 |
// y = f(g^-1(x)) ; f = ingamma, g = outgamma
|
|
237 |
//
|
|
238 |
// And this can be parametrized as
|
|
239 |
//
|
|
240 |
// y = f(t)
|
|
241 |
// x = g(t)
|
|
242 |
|
|
243 |
|
|
244 |
LPGAMMATABLE LCMSEXPORT cmsJoinGammaEx(LPGAMMATABLE InGamma,
|
|
245 |
LPGAMMATABLE OutGamma, int nPoints)
|
|
246 |
{
|
|
247 |
|
|
248 |
LPSAMPLEDCURVE x, y, r;
|
|
249 |
LPGAMMATABLE res;
|
|
250 |
|
|
251 |
x = cmsConvertGammaToSampledCurve(InGamma, nPoints);
|
|
252 |
y = cmsConvertGammaToSampledCurve(OutGamma, nPoints);
|
|
253 |
r = cmsJoinSampledCurves(y, x, nPoints);
|
|
254 |
|
|
255 |
// Does clean "hair"
|
|
256 |
cmsSmoothSampledCurve(r, 0.001);
|
|
257 |
|
|
258 |
cmsClampSampledCurve(r, 0.0, 65535.0);
|
|
259 |
|
|
260 |
cmsFreeSampledCurve(x);
|
|
261 |
cmsFreeSampledCurve(y);
|
|
262 |
|
|
263 |
res = cmsConvertSampledCurveToGamma(r, 65535.0);
|
|
264 |
cmsFreeSampledCurve(r);
|
|
265 |
|
|
266 |
return res;
|
|
267 |
}
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
// Reverse a gamma table
|
|
272 |
|
|
273 |
LPGAMMATABLE LCMSEXPORT cmsReverseGamma(int nResultSamples, LPGAMMATABLE InGamma)
|
|
274 |
{
|
|
275 |
register int i;
|
|
276 |
L16PARAMS L16In;
|
|
277 |
LPWORD InPtr;
|
|
278 |
LPGAMMATABLE p;
|
|
279 |
|
2394
|
280 |
// Try to reverse it analytically whatever possible
|
|
281 |
if (InGamma -> Seed.Type > 0 && InGamma -> Seed.Type <= 5 &&
|
|
282 |
_cmsCrc32OfGammaTable(InGamma) == InGamma -> Seed.Crc32) {
|
|
283 |
|
|
284 |
return cmsBuildParametricGamma(nResultSamples, -(InGamma -> Seed.Type), InGamma ->Seed.Params);
|
|
285 |
}
|
|
286 |
|
|
287 |
|
|
288 |
// Nope, reverse the table
|
2
|
289 |
p = cmsAllocGamma(nResultSamples);
|
|
290 |
if (!p) return NULL;
|
|
291 |
|
|
292 |
cmsCalcL16Params(InGamma -> nEntries, &L16In);
|
|
293 |
InPtr = InGamma -> GammaTable;
|
|
294 |
|
|
295 |
for (i=0; i < nResultSamples; i++)
|
|
296 |
{
|
|
297 |
WORD wValIn, wValOut;
|
|
298 |
|
|
299 |
wValIn = _cmsQuantizeVal(i, nResultSamples);
|
|
300 |
wValOut = cmsReverseLinearInterpLUT16(wValIn, InPtr, &L16In);
|
|
301 |
p -> GammaTable[i] = wValOut;
|
|
302 |
}
|
|
303 |
|
|
304 |
|
|
305 |
return p;
|
|
306 |
}
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
// Parametric curves
|
|
311 |
//
|
|
312 |
// Parameters goes as: Gamma, a, b, c, d, e, f
|
|
313 |
// Type is the ICC type +1
|
|
314 |
// if type is negative, then the curve is analyticaly inverted
|
|
315 |
|
|
316 |
LPGAMMATABLE LCMSEXPORT cmsBuildParametricGamma(int nEntries, int Type, double Params[])
|
|
317 |
{
|
|
318 |
LPGAMMATABLE Table;
|
|
319 |
double R, Val, dval, e;
|
|
320 |
int i;
|
|
321 |
int ParamsByType[] = { 0, 1, 3, 4, 5, 7 };
|
|
322 |
|
|
323 |
Table = cmsAllocGamma(nEntries);
|
|
324 |
if (NULL == Table) return NULL;
|
|
325 |
|
|
326 |
Table -> Seed.Type = Type;
|
|
327 |
|
|
328 |
CopyMemory(Table ->Seed.Params, Params, ParamsByType[abs(Type)] * sizeof(double));
|
|
329 |
|
|
330 |
|
|
331 |
for (i=0; i < nEntries; i++) {
|
|
332 |
|
|
333 |
R = (double) i / (nEntries-1);
|
|
334 |
|
|
335 |
switch (Type) {
|
|
336 |
|
|
337 |
// X = Y ^ Gamma
|
|
338 |
case 1:
|
|
339 |
Val = pow(R, Params[0]);
|
|
340 |
break;
|
|
341 |
|
|
342 |
// Type 1 Reversed: X = Y ^1/gamma
|
|
343 |
case -1:
|
|
344 |
Val = pow(R, 1/Params[0]);
|
|
345 |
break;
|
|
346 |
|
|
347 |
// CIE 122-1966
|
|
348 |
// Y = (aX + b)^Gamma | X >= -b/a
|
|
349 |
// Y = 0 | else
|
|
350 |
case 2:
|
|
351 |
if (R >= -Params[2] / Params[1]) {
|
|
352 |
|
|
353 |
e = Params[1]*R + Params[2];
|
|
354 |
|
|
355 |
if (e > 0)
|
|
356 |
Val = pow(e, Params[0]);
|
|
357 |
else
|
|
358 |
Val = 0;
|
|
359 |
}
|
|
360 |
else
|
|
361 |
Val = 0;
|
|
362 |
break;
|
|
363 |
|
|
364 |
// Type 2 Reversed
|
|
365 |
// X = (Y ^1/g - b) / a
|
|
366 |
case -2:
|
|
367 |
|
|
368 |
Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
|
|
369 |
if (Val < 0)
|
|
370 |
Val = 0;
|
|
371 |
break;
|
|
372 |
|
|
373 |
|
|
374 |
// IEC 61966-3
|
|
375 |
// Y = (aX + b)^Gamma | X <= -b/a
|
|
376 |
// Y = c | else
|
|
377 |
case 3:
|
|
378 |
if (R >= -Params[2] / Params[1]) {
|
|
379 |
|
|
380 |
e = Params[1]*R + Params[2];
|
|
381 |
Val = pow(e, Params[0]) + Params[3];
|
|
382 |
}
|
|
383 |
else
|
|
384 |
Val = Params[3];
|
|
385 |
break;
|
|
386 |
|
|
387 |
|
|
388 |
// Type 3 reversed
|
|
389 |
// X=((Y-c)^1/g - b)/a | (Y>=c)
|
|
390 |
// X=-b/a | (Y<c)
|
|
391 |
|
|
392 |
case -3:
|
|
393 |
if (R >= Params[3]) {
|
|
394 |
e = R - Params[3];
|
|
395 |
Val = (pow(e, 1/Params[0]) - Params[2]) / Params[1];
|
|
396 |
if (Val < 0) Val = 0;
|
|
397 |
}
|
|
398 |
else {
|
|
399 |
Val = -Params[2] / Params[1];
|
|
400 |
}
|
|
401 |
break;
|
|
402 |
|
|
403 |
|
|
404 |
// IEC 61966-2.1 (sRGB)
|
|
405 |
// Y = (aX + b)^Gamma | X >= d
|
|
406 |
// Y = cX | X < d
|
|
407 |
case 4:
|
|
408 |
if (R >= Params[4]) {
|
|
409 |
|
|
410 |
e = Params[1]*R + Params[2];
|
|
411 |
if (e > 0)
|
|
412 |
Val = pow(e, Params[0]);
|
|
413 |
else
|
|
414 |
Val = 0;
|
|
415 |
}
|
|
416 |
else
|
|
417 |
Val = R * Params[3];
|
|
418 |
break;
|
|
419 |
|
|
420 |
// Type 4 reversed
|
|
421 |
// X=((Y^1/g-b)/a) | Y >= (ad+b)^g
|
|
422 |
// X=Y/c | Y< (ad+b)^g
|
|
423 |
|
|
424 |
case -4:
|
|
425 |
if (R >= pow(Params[1] * Params[4] + Params[2], Params[0])) {
|
|
426 |
|
|
427 |
Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
|
|
428 |
}
|
|
429 |
else {
|
|
430 |
Val = R / Params[3];
|
|
431 |
}
|
|
432 |
break;
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
// Y = (aX + b)^Gamma + e | X <= d
|
|
437 |
// Y = cX + f | else
|
|
438 |
case 5:
|
|
439 |
if (R >= Params[4]) {
|
|
440 |
|
|
441 |
e = Params[1]*R + Params[2];
|
|
442 |
Val = pow(e, Params[0]) + Params[5];
|
|
443 |
}
|
|
444 |
else
|
|
445 |
Val = R*Params[3] + Params[6];
|
|
446 |
break;
|
|
447 |
|
|
448 |
|
|
449 |
// Reversed type 5
|
|
450 |
// X=((Y-e)1/g-b)/a | Y >=(ad+b)^g+e)
|
|
451 |
// X=(Y-f)/c | else
|
|
452 |
case -5:
|
|
453 |
|
|
454 |
if (R >= pow(Params[1] * Params[4], Params[0]) + Params[5]) {
|
|
455 |
|
|
456 |
Val = pow(R - Params[5], 1/Params[0]) - Params[2] / Params[1];
|
|
457 |
}
|
|
458 |
else {
|
|
459 |
Val = (R - Params[6]) / Params[3];
|
|
460 |
}
|
|
461 |
break;
|
|
462 |
|
|
463 |
default:
|
|
464 |
cmsSignalError(LCMS_ERRC_ABORTED, "Unsupported parametric curve type=%d", abs(Type)-1);
|
|
465 |
cmsFreeGamma(Table);
|
|
466 |
return NULL;
|
|
467 |
}
|
|
468 |
|
|
469 |
|
|
470 |
// Saturate
|
|
471 |
|
|
472 |
dval = Val * 65535.0 + .5;
|
|
473 |
if (dval > 65535.) dval = 65535.0;
|
|
474 |
if (dval < 0) dval = 0;
|
|
475 |
|
|
476 |
Table->GammaTable[i] = (WORD) floor(dval);
|
|
477 |
}
|
|
478 |
|
|
479 |
Table -> Seed.Crc32 = _cmsCrc32OfGammaTable(Table);
|
|
480 |
|
|
481 |
return Table;
|
|
482 |
}
|
|
483 |
|
|
484 |
// Build a gamma table based on gamma constant
|
|
485 |
|
|
486 |
LPGAMMATABLE LCMSEXPORT cmsBuildGamma(int nEntries, double Gamma)
|
|
487 |
{
|
|
488 |
return cmsBuildParametricGamma(nEntries, 1, &Gamma);
|
|
489 |
}
|
|
490 |
|
|
491 |
|
|
492 |
|
|
493 |
// From: Eilers, P.H.C. (1994) Smoothing and interpolation with finite
|
|
494 |
// differences. in: Graphic Gems IV, Heckbert, P.S. (ed.), Academic press.
|
|
495 |
//
|
|
496 |
// Smoothing and interpolation with second differences.
|
|
497 |
//
|
|
498 |
// Input: weights (w), data (y): vector from 1 to m.
|
|
499 |
// Input: smoothing parameter (lambda), length (m).
|
|
500 |
// Output: smoothed vector (z): vector from 1 to m.
|
|
501 |
|
|
502 |
|
|
503 |
static
|
|
504 |
void smooth2(vec w, vec y, vec z, float lambda, int m)
|
|
505 |
{
|
|
506 |
int i, i1, i2;
|
|
507 |
vec c, d, e;
|
|
508 |
d[1] = w[1] + lambda;
|
|
509 |
c[1] = -2 * lambda / d[1];
|
|
510 |
e[1] = lambda /d[1];
|
|
511 |
z[1] = w[1] * y[1];
|
|
512 |
d[2] = w[2] + 5 * lambda - d[1] * c[1] * c[1];
|
|
513 |
c[2] = (-4 * lambda - d[1] * c[1] * e[1]) / d[2];
|
|
514 |
e[2] = lambda / d[2];
|
|
515 |
z[2] = w[2] * y[2] - c[1] * z[1];
|
|
516 |
for (i = 3; i < m - 1; i++) {
|
|
517 |
i1 = i - 1; i2 = i - 2;
|
|
518 |
d[i]= w[i] + 6 * lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
|
|
519 |
c[i] = (-4 * lambda -d[i1] * c[i1] * e[i1])/ d[i];
|
|
520 |
e[i] = lambda / d[i];
|
|
521 |
z[i] = w[i] * y[i] - c[i1] * z[i1] - e[i2] * z[i2];
|
|
522 |
}
|
|
523 |
i1 = m - 2; i2 = m - 3;
|
|
524 |
d[m - 1] = w[m - 1] + 5 * lambda -c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
|
|
525 |
c[m - 1] = (-2 * lambda - d[i1] * c[i1] * e[i1]) / d[m - 1];
|
|
526 |
z[m - 1] = w[m - 1] * y[m - 1] - c[i1] * z[i1] - e[i2] * z[i2];
|
|
527 |
i1 = m - 1; i2 = m - 2;
|
|
528 |
d[m] = w[m] + lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
|
|
529 |
z[m] = (w[m] * y[m] - c[i1] * z[i1] - e[i2] * z[i2]) / d[m];
|
|
530 |
z[m - 1] = z[m - 1] / d[m - 1] - c[m - 1] * z[m];
|
|
531 |
for (i = m - 2; 1<= i; i--)
|
|
532 |
z[i] = z[i] / d[i] - c[i] * z[i + 1] - e[i] * z[i + 2];
|
|
533 |
}
|
|
534 |
|
|
535 |
|
|
536 |
|
|
537 |
// Smooths a curve sampled at regular intervals
|
|
538 |
|
2394
|
539 |
LCMSBOOL LCMSEXPORT cmsSmoothGamma(LPGAMMATABLE Tab, double lambda)
|
2
|
540 |
|
|
541 |
{
|
|
542 |
vec w, y, z;
|
|
543 |
int i, nItems, Zeros, Poles;
|
|
544 |
|
|
545 |
|
|
546 |
if (cmsIsLinear(Tab->GammaTable, Tab->nEntries)) return FALSE; // Nothing to do
|
|
547 |
|
|
548 |
nItems = Tab -> nEntries;
|
|
549 |
|
|
550 |
if (nItems > MAX_KNOTS) {
|
|
551 |
cmsSignalError(LCMS_ERRC_ABORTED, "cmsSmoothGamma: too many points.");
|
|
552 |
return FALSE;
|
|
553 |
}
|
|
554 |
|
|
555 |
ZeroMemory(w, nItems * sizeof(float));
|
|
556 |
ZeroMemory(y, nItems * sizeof(float));
|
|
557 |
ZeroMemory(z, nItems * sizeof(float));
|
|
558 |
|
|
559 |
for (i=0; i < nItems; i++)
|
|
560 |
{
|
|
561 |
y[i+1] = (float) Tab -> GammaTable[i];
|
|
562 |
w[i+1] = 1.0;
|
|
563 |
}
|
|
564 |
|
|
565 |
smooth2(w, y, z, (float) lambda, nItems);
|
|
566 |
|
|
567 |
// Do some reality - checking...
|
|
568 |
Zeros = Poles = 0;
|
|
569 |
for (i=nItems; i > 1; --i) {
|
|
570 |
|
|
571 |
if (z[i] == 0.) Zeros++;
|
|
572 |
if (z[i] >= 65535.) Poles++;
|
|
573 |
if (z[i] < z[i-1]) return FALSE; // Non-Monotonic
|
|
574 |
}
|
|
575 |
|
|
576 |
if (Zeros > (nItems / 3)) return FALSE; // Degenerated, mostly zeros
|
|
577 |
if (Poles > (nItems / 3)) return FALSE; // Degenerated, mostly poles
|
|
578 |
|
|
579 |
// Seems ok
|
|
580 |
|
|
581 |
for (i=0; i < nItems; i++) {
|
|
582 |
|
|
583 |
// Clamp to WORD
|
|
584 |
|
|
585 |
float v = z[i+1];
|
|
586 |
|
|
587 |
if (v < 0) v = 0;
|
|
588 |
if (v > 65535.) v = 65535.;
|
|
589 |
|
|
590 |
Tab -> GammaTable[i] = (WORD) floor(v + .5);
|
|
591 |
}
|
|
592 |
|
|
593 |
return TRUE;
|
|
594 |
}
|
|
595 |
|
|
596 |
|
|
597 |
// Check if curve is exponential, return gamma if so.
|
|
598 |
|
|
599 |
double LCMSEXPORT cmsEstimateGammaEx(LPWORD GammaTable, int nEntries, double Thereshold)
|
|
600 |
{
|
|
601 |
double gamma, sum, sum2;
|
|
602 |
double n, x, y, Std;
|
|
603 |
int i;
|
|
604 |
|
|
605 |
sum = sum2 = n = 0;
|
|
606 |
|
|
607 |
// Does exclude endpoints
|
|
608 |
for (i=1; i < nEntries - 1; i++) {
|
|
609 |
|
|
610 |
x = (double) i / (nEntries - 1);
|
|
611 |
y = (double) GammaTable[i] / 65535.;
|
|
612 |
|
|
613 |
// Avoid 7% on lower part to prevent
|
|
614 |
// artifacts due to linear ramps
|
|
615 |
|
|
616 |
if (y > 0. && y < 1. && x > 0.07) {
|
|
617 |
|
|
618 |
gamma = log(y) / log(x);
|
|
619 |
sum += gamma;
|
|
620 |
sum2 += gamma * gamma;
|
|
621 |
n++;
|
|
622 |
}
|
|
623 |
|
|
624 |
}
|
|
625 |
|
|
626 |
// Take a look on SD to see if gamma isn't exponential at all
|
|
627 |
Std = sqrt((n * sum2 - sum * sum) / (n*(n-1)));
|
|
628 |
|
|
629 |
|
|
630 |
if (Std > Thereshold)
|
|
631 |
return -1.0;
|
|
632 |
|
|
633 |
return (sum / n); // The mean
|
|
634 |
}
|
|
635 |
|
|
636 |
|
|
637 |
double LCMSEXPORT cmsEstimateGamma(LPGAMMATABLE t)
|
|
638 |
{
|
|
639 |
return cmsEstimateGammaEx(t->GammaTable, t->nEntries, 0.7);
|
|
640 |
}
|
|
641 |
|
|
642 |
|
|
643 |
// -----------------------------------------------------------------Sampled curves
|
|
644 |
|
|
645 |
// Allocate a empty curve
|
|
646 |
|
|
647 |
LPSAMPLEDCURVE cmsAllocSampledCurve(int nItems)
|
|
648 |
{
|
|
649 |
LPSAMPLEDCURVE pOut;
|
|
650 |
|
2394
|
651 |
pOut = (LPSAMPLEDCURVE) _cmsMalloc(sizeof(SAMPLEDCURVE));
|
2
|
652 |
if (pOut == NULL)
|
|
653 |
return NULL;
|
|
654 |
|
2394
|
655 |
if((pOut->Values = (double *) _cmsMalloc(nItems * sizeof(double))) == NULL)
|
2
|
656 |
{
|
2394
|
657 |
_cmsFree(pOut);
|
2
|
658 |
return NULL;
|
|
659 |
}
|
|
660 |
|
|
661 |
pOut->nItems = nItems;
|
|
662 |
ZeroMemory(pOut->Values, nItems * sizeof(double));
|
|
663 |
|
|
664 |
return pOut;
|
|
665 |
}
|
|
666 |
|
|
667 |
|
|
668 |
void cmsFreeSampledCurve(LPSAMPLEDCURVE p)
|
|
669 |
{
|
2394
|
670 |
_cmsFree((LPVOID) p -> Values);
|
|
671 |
_cmsFree((LPVOID) p);
|
2
|
672 |
}
|
|
673 |
|
|
674 |
|
|
675 |
|
|
676 |
// Does duplicate a sampled curve
|
|
677 |
|
|
678 |
LPSAMPLEDCURVE cmsDupSampledCurve(LPSAMPLEDCURVE p)
|
|
679 |
{
|
|
680 |
LPSAMPLEDCURVE out;
|
|
681 |
|
|
682 |
out = cmsAllocSampledCurve(p -> nItems);
|
|
683 |
if (!out) return NULL;
|
|
684 |
|
|
685 |
CopyMemory(out ->Values, p ->Values, p->nItems * sizeof(double));
|
|
686 |
|
|
687 |
return out;
|
|
688 |
}
|
|
689 |
|
|
690 |
|
|
691 |
// Take min, max of curve
|
|
692 |
|
|
693 |
void cmsEndpointsOfSampledCurve(LPSAMPLEDCURVE p, double* Min, double* Max)
|
|
694 |
{
|
|
695 |
int i;
|
|
696 |
|
|
697 |
*Min = 65536.;
|
|
698 |
*Max = 0.;
|
|
699 |
|
|
700 |
for (i=0; i < p -> nItems; i++) {
|
|
701 |
|
|
702 |
double v = p -> Values[i];
|
|
703 |
|
|
704 |
if (v < *Min)
|
|
705 |
*Min = v;
|
|
706 |
|
|
707 |
if (v > *Max)
|
|
708 |
*Max = v;
|
|
709 |
}
|
|
710 |
|
|
711 |
if (*Min < 0) *Min = 0;
|
|
712 |
if (*Max > 65535.0) *Max = 65535.0;
|
|
713 |
}
|
|
714 |
|
|
715 |
// Clamps to Min, Max
|
|
716 |
|
|
717 |
void cmsClampSampledCurve(LPSAMPLEDCURVE p, double Min, double Max)
|
|
718 |
{
|
|
719 |
|
|
720 |
int i;
|
|
721 |
|
|
722 |
for (i=0; i < p -> nItems; i++) {
|
|
723 |
|
|
724 |
double v = p -> Values[i];
|
|
725 |
|
|
726 |
if (v < Min)
|
|
727 |
v = Min;
|
|
728 |
|
|
729 |
if (v > Max)
|
|
730 |
v = Max;
|
|
731 |
|
|
732 |
p -> Values[i] = v;
|
|
733 |
|
|
734 |
}
|
|
735 |
|
|
736 |
}
|
|
737 |
|
|
738 |
|
|
739 |
|
|
740 |
// Smooths a curve sampled at regular intervals
|
|
741 |
|
2394
|
742 |
LCMSBOOL cmsSmoothSampledCurve(LPSAMPLEDCURVE Tab, double lambda)
|
2
|
743 |
{
|
|
744 |
vec w, y, z;
|
|
745 |
int i, nItems;
|
|
746 |
|
|
747 |
nItems = Tab -> nItems;
|
|
748 |
|
|
749 |
if (nItems > MAX_KNOTS) {
|
|
750 |
cmsSignalError(LCMS_ERRC_ABORTED, "cmsSmoothSampledCurve: too many points.");
|
|
751 |
return FALSE;
|
|
752 |
}
|
|
753 |
|
|
754 |
ZeroMemory(w, nItems * sizeof(float));
|
|
755 |
ZeroMemory(y, nItems * sizeof(float));
|
|
756 |
ZeroMemory(z, nItems * sizeof(float));
|
|
757 |
|
|
758 |
for (i=0; i < nItems; i++)
|
|
759 |
{
|
|
760 |
float value = (float) Tab -> Values[i];
|
|
761 |
|
|
762 |
y[i+1] = value;
|
|
763 |
w[i+1] = (float) ((value < 0.0) ? 0 : 1);
|
|
764 |
}
|
|
765 |
|
|
766 |
|
|
767 |
smooth2(w, y, z, (float) lambda, nItems);
|
|
768 |
|
|
769 |
for (i=0; i < nItems; i++) {
|
|
770 |
|
|
771 |
Tab -> Values[i] = z[i+1];;
|
|
772 |
}
|
|
773 |
|
|
774 |
return TRUE;
|
|
775 |
|
|
776 |
}
|
|
777 |
|
|
778 |
|
|
779 |
// Scale a value v, within domain Min .. Max
|
|
780 |
// to a domain 0..(nPoints-1)
|
|
781 |
|
|
782 |
static
|
|
783 |
double ScaleVal(double v, double Min, double Max, int nPoints)
|
|
784 |
{
|
|
785 |
|
|
786 |
double a, b;
|
|
787 |
|
|
788 |
if (v <= Min) return 0;
|
|
789 |
if (v >= Max) return (nPoints-1);
|
|
790 |
|
|
791 |
a = (double) (nPoints - 1) / (Max - Min);
|
|
792 |
b = a * Min;
|
|
793 |
|
|
794 |
return (a * v) - b;
|
|
795 |
|
|
796 |
}
|
|
797 |
|
|
798 |
|
|
799 |
// Does rescale a sampled curve to fit in a 0..(nPoints-1) domain
|
|
800 |
|
|
801 |
void cmsRescaleSampledCurve(LPSAMPLEDCURVE p, double Min, double Max, int nPoints)
|
|
802 |
{
|
|
803 |
|
|
804 |
int i;
|
|
805 |
|
|
806 |
for (i=0; i < p -> nItems; i++) {
|
|
807 |
|
|
808 |
double v = p -> Values[i];
|
|
809 |
|
|
810 |
p -> Values[i] = ScaleVal(v, Min, Max, nPoints);
|
|
811 |
}
|
|
812 |
|
|
813 |
}
|
|
814 |
|
|
815 |
|
|
816 |
// Joins two sampled curves for X and Y. Curves should be sorted.
|
|
817 |
|
|
818 |
LPSAMPLEDCURVE cmsJoinSampledCurves(LPSAMPLEDCURVE X, LPSAMPLEDCURVE Y, int nResultingPoints)
|
|
819 |
{
|
|
820 |
int i, j;
|
|
821 |
LPSAMPLEDCURVE out;
|
|
822 |
double MinX, MinY, MaxX, MaxY;
|
|
823 |
double x, y, x1, y1, x2, y2, a, b;
|
|
824 |
|
|
825 |
out = cmsAllocSampledCurve(nResultingPoints);
|
|
826 |
if (out == NULL)
|
|
827 |
return NULL;
|
|
828 |
|
|
829 |
if (X -> nItems != Y -> nItems) {
|
|
830 |
|
|
831 |
cmsSignalError(LCMS_ERRC_ABORTED, "cmsJoinSampledCurves: invalid curve.");
|
|
832 |
cmsFreeSampledCurve(out);
|
|
833 |
return NULL;
|
|
834 |
}
|
|
835 |
|
|
836 |
// Get endpoints of sampled curves
|
|
837 |
cmsEndpointsOfSampledCurve(X, &MinX, &MaxX);
|
|
838 |
cmsEndpointsOfSampledCurve(Y, &MinY, &MaxY);
|
|
839 |
|
|
840 |
|
|
841 |
// Set our points
|
|
842 |
out ->Values[0] = MinY;
|
|
843 |
for (i=1; i < nResultingPoints; i++) {
|
|
844 |
|
|
845 |
// Scale t to x domain
|
|
846 |
x = (i * (MaxX - MinX) / (nResultingPoints-1)) + MinX;
|
|
847 |
|
|
848 |
// Find interval in which j is within (always up,
|
|
849 |
// since fn should be monotonic at all)
|
|
850 |
|
|
851 |
j = 1;
|
|
852 |
while ((j < X ->nItems - 1) && X ->Values[j] < x)
|
|
853 |
j++;
|
|
854 |
|
|
855 |
// Now x is within X[j-1], X[j]
|
|
856 |
x1 = X ->Values[j-1]; x2 = X ->Values[j];
|
|
857 |
y1 = Y ->Values[j-1]; y2 = Y ->Values[j];
|
|
858 |
|
|
859 |
// Interpolate the value
|
|
860 |
a = (y1 - y2) / (x1 - x2);
|
|
861 |
b = y1 - a * x1;
|
|
862 |
y = a* x + b;
|
|
863 |
|
|
864 |
out ->Values[i] = y;
|
|
865 |
}
|
|
866 |
|
|
867 |
|
|
868 |
cmsClampSampledCurve(out, MinY, MaxY);
|
|
869 |
return out;
|
|
870 |
}
|
|
871 |
|
|
872 |
|
|
873 |
|
|
874 |
// Convert between curve types
|
|
875 |
|
|
876 |
LPGAMMATABLE cmsConvertSampledCurveToGamma(LPSAMPLEDCURVE Sampled, double Max)
|
|
877 |
{
|
|
878 |
LPGAMMATABLE Gamma;
|
|
879 |
int i, nPoints;
|
|
880 |
|
|
881 |
|
|
882 |
nPoints = Sampled ->nItems;
|
|
883 |
|
|
884 |
Gamma = cmsAllocGamma(nPoints);
|
|
885 |
for (i=0; i < nPoints; i++) {
|
|
886 |
|
|
887 |
Gamma->GammaTable[i] = (WORD) floor(ScaleVal(Sampled ->Values[i], 0, Max, 65536) + .5);
|
|
888 |
}
|
|
889 |
|
|
890 |
return Gamma;
|
|
891 |
|
|
892 |
}
|
|
893 |
|
|
894 |
// Inverse of anterior
|
|
895 |
|
|
896 |
LPSAMPLEDCURVE cmsConvertGammaToSampledCurve(LPGAMMATABLE Gamma, int nPoints)
|
|
897 |
{
|
|
898 |
LPSAMPLEDCURVE Sampled;
|
|
899 |
L16PARAMS L16;
|
|
900 |
int i;
|
|
901 |
WORD wQuant, wValIn;
|
|
902 |
|
|
903 |
if (nPoints > 4096) {
|
|
904 |
|
|
905 |
cmsSignalError(LCMS_ERRC_ABORTED, "cmsConvertGammaToSampledCurve: too many points (max=4096)");
|
|
906 |
return NULL;
|
|
907 |
}
|
|
908 |
|
|
909 |
cmsCalcL16Params(Gamma -> nEntries, &L16);
|
|
910 |
|
|
911 |
Sampled = cmsAllocSampledCurve(nPoints);
|
|
912 |
for (i=0; i < nPoints; i++) {
|
|
913 |
wQuant = _cmsQuantizeVal(i, nPoints);
|
|
914 |
wValIn = cmsLinearInterpLUT16(wQuant, Gamma ->GammaTable, &L16);
|
|
915 |
Sampled ->Values[i] = (float) wValIn;
|
|
916 |
}
|
|
917 |
|
|
918 |
return Sampled;
|
|
919 |
}
|
|
920 |
|
|
921 |
|
|
922 |
|
|
923 |
|
|
924 |
// Smooth endpoints (used in Black/White compensation)
|
|
925 |
|
2394
|
926 |
LCMSBOOL _cmsSmoothEndpoints(LPWORD Table, int nEntries)
|
2
|
927 |
{
|
|
928 |
vec w, y, z;
|
|
929 |
int i, Zeros, Poles;
|
|
930 |
|
|
931 |
|
|
932 |
|
|
933 |
if (cmsIsLinear(Table, nEntries)) return FALSE; // Nothing to do
|
|
934 |
|
|
935 |
|
|
936 |
if (nEntries > MAX_KNOTS) {
|
|
937 |
cmsSignalError(LCMS_ERRC_ABORTED, "_cmsSmoothEndpoints: too many points.");
|
|
938 |
return FALSE;
|
|
939 |
}
|
|
940 |
|
|
941 |
ZeroMemory(w, nEntries * sizeof(float));
|
|
942 |
ZeroMemory(y, nEntries * sizeof(float));
|
|
943 |
ZeroMemory(z, nEntries * sizeof(float));
|
|
944 |
|
|
945 |
for (i=0; i < nEntries; i++)
|
|
946 |
{
|
|
947 |
y[i+1] = (float) Table[i];
|
|
948 |
w[i+1] = 1.0;
|
|
949 |
}
|
|
950 |
|
|
951 |
w[1] = 65535.0;
|
|
952 |
w[nEntries] = 65535.0;
|
|
953 |
|
|
954 |
smooth2(w, y, z, (float) nEntries, nEntries);
|
|
955 |
|
|
956 |
// Do some reality - checking...
|
|
957 |
Zeros = Poles = 0;
|
|
958 |
for (i=nEntries; i > 1; --i) {
|
|
959 |
|
|
960 |
if (z[i] == 0.) Zeros++;
|
|
961 |
if (z[i] >= 65535.) Poles++;
|
|
962 |
if (z[i] < z[i-1]) return FALSE; // Non-Monotonic
|
|
963 |
}
|
|
964 |
|
|
965 |
if (Zeros > (nEntries / 3)) return FALSE; // Degenerated, mostly zeros
|
|
966 |
if (Poles > (nEntries / 3)) return FALSE; // Degenerated, mostly poles
|
|
967 |
|
|
968 |
// Seems ok
|
|
969 |
|
|
970 |
for (i=0; i < nEntries; i++) {
|
|
971 |
|
|
972 |
// Clamp to WORD
|
|
973 |
|
|
974 |
float v = z[i+1];
|
|
975 |
|
|
976 |
if (v < 0) v = 0;
|
|
977 |
if (v > 65535.) v = 65535.;
|
|
978 |
|
|
979 |
Table[i] = (WORD) floor(v + .5);
|
|
980 |
}
|
|
981 |
|
|
982 |
return TRUE;
|
|
983 |
}
|