3 // Copyright (C) 1998-2007 Marti Maria
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the Software
10 // is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Shaper/Matrix handling
28 // This routines handles the matrix-shaper method. A note about domain
29 // is here required. If the shaper-matrix is invoked on INPUT profiles,
30 // after the shaper process, we have a value between 0 and 0xFFFF. Thus,
31 // for proper matrix handling, we must convert it to 15fix16, so
32 // ToFixedDomain might be called. But cmsLinearInterpFixed() returns
33 // data yet in fixed point, so no additional process is required.
34 // Then, we obtain data on 15.16, so we need to shift >> by 1 to
35 // obtain 1.15 PCS format.
37 // On OUTPUT profiles, things are inverse, we must first expand 1 bit
38 // by shifting left, and then convert result between 0 and 1.000 to
39 // RGB, so FromFixedDomain() must be called before pass values to
40 // shaper. Trickly, there is a situation where this shifts works
41 // little different. Sometimes, lcms smelts input/output
42 // matrices into a single, one shaper, process. In such cases, since
43 // input is encoded from 0 to 0xffff, we must first use the shaper and
44 // then the matrix, an additional FromFixedDomain() must be used to
45 // accomodate output values.
47 // For a sake of simplicity, I will handle this three behaviours
48 // with different routines, so the flags MATSHAPER_INPUT and MATSHAPER_OUTPUT
49 // can be conbined to signal smelted matrix-shapers
54 int ComputeTables(LPGAMMATABLE Table
[3], LPWORD Out
[3], LPL16PARAMS p16
)
58 cmsCalcL16Params(Table
[0] -> nEntries
, p16
);
65 PtrW
= (LPWORD
) _cmsMalloc(sizeof(WORD
) * p16
-> nSamples
);
67 if (PtrW
== NULL
) return -1; // Signal error
69 CopyMemory(PtrW
, Table
[i
] -> GammaTable
, sizeof(WORD
) * Table
[i
] -> nEntries
);
71 Out
[i
] = PtrW
; // Set table pointer
75 AllLinear
+= cmsIsLinear(PtrW
, p16
-> nSamples
);
78 // If is all linear, then supress table interpolation (this
79 // will speed greately some trivial operations.
80 // Return 1 if present, 0 if all linear
83 if (AllLinear
!= 3) return 1;
90 LPMATSHAPER
cmsAllocMatShaper2(LPMAT3 Matrix
, LPGAMMATABLE In
[], LPLCMSPRECACHE InPrecache
,
91 LPGAMMATABLE Out
[], LPLCMSPRECACHE OutPrecache
,
94 LPMATSHAPER NewMatShaper
;
97 NewMatShaper
= (LPMATSHAPER
) _cmsMalloc(sizeof(MATSHAPER
));
99 ZeroMemory(NewMatShaper
, sizeof(MATSHAPER
));
101 NewMatShaper
->dwFlags
= Behaviour
;
104 if (Behaviour
& MATSHAPER_FLOATMAT
) {
105 FMAT3ASetup(&NewMatShaper
->Matrix
.FA
);
106 MAT3toFloatTranspose(NewMatShaper
-> Matrix
.FA
.F
, Matrix
);
107 if (!FMAT3isIdentity(NewMatShaper
-> Matrix
.FA
.F
, 0.00001f
))
108 NewMatShaper
-> dwFlags
|= MATSHAPER_HASMATRIX
;
110 // This needs to be calculated by the CPU or a very precise
111 // compiler. If it's too big (like 1.0), values are clamped
112 // to 65536 instead 65535, and we either have an overflow of
113 // the precache bounds or scary downcasting.
114 NewMatShaper
-> clampMax
= ((FLOAT
) (65536 - 1)) / 65536.0f
;
117 MAT3toFix(&NewMatShaper
-> Matrix
.W
, Matrix
);
118 if (!MAT3isIdentity(&NewMatShaper
-> Matrix
.W
, 0.00001))
119 NewMatShaper
-> dwFlags
|= MATSHAPER_HASMATRIX
;
122 // Now, on the table characteristics
124 // If we have an output precache, use that
125 if (OutPrecache
!= NULL
) {
126 PRECACHE_ADDREF(OutPrecache
);
127 NewMatShaper
->L_Precache
= OutPrecache
;
128 NewMatShaper
-> dwFlags
|= MATSHAPER_HASSHAPER
;
132 rc
= ComputeTables(Out
, NewMatShaper
->L
, &NewMatShaper
->p16
);
134 cmsFreeMatShaper(NewMatShaper
);
137 if (rc
== 1) NewMatShaper
-> dwFlags
|= MATSHAPER_HASSHAPER
;
140 // If we have an input precache, use that
141 if (InPrecache
!= NULL
) {
142 PRECACHE_ADDREF(InPrecache
);
143 NewMatShaper
->L2_Precache
= InPrecache
;
144 NewMatShaper
-> dwFlags
|= MATSHAPER_HASINPSHAPER
;
149 rc
= ComputeTables(In
, NewMatShaper
->L2
, &NewMatShaper
->p2_16
);
151 cmsFreeMatShaper(NewMatShaper
);
154 if (rc
== 1) NewMatShaper
-> dwFlags
|= MATSHAPER_HASINPSHAPER
;
161 // Creation & Destruction
163 LPMATSHAPER
cmsAllocMatShaper(LPMAT3 Matrix
, LPGAMMATABLE Tables
[], DWORD Behaviour
)
165 LPMATSHAPER NewMatShaper
;
168 NewMatShaper
= (LPMATSHAPER
) _cmsMalloc(sizeof(MATSHAPER
));
170 ZeroMemory(NewMatShaper
, sizeof(MATSHAPER
));
172 NewMatShaper
->dwFlags
= Behaviour
& (MATSHAPER_ALLSMELTED
);
176 MAT3toFix(&NewMatShaper
-> Matrix
.W
, Matrix
);
180 if (!MAT3isIdentity(&NewMatShaper
-> Matrix
.W
, 0.00001))
181 NewMatShaper
-> dwFlags
|= MATSHAPER_HASMATRIX
;
183 // Now, on the table characteristics
185 cmsCalcL16Params(Tables
[0] -> nEntries
, &NewMatShaper
-> p16
);
190 for (i
=0; i
< 3; i
++)
194 PtrW
= (LPWORD
) _cmsMalloc(sizeof(WORD
) * NewMatShaper
-> p16
.nSamples
);
197 cmsFreeMatShaper(NewMatShaper
);
201 CopyMemory(PtrW
, Tables
[i
] -> GammaTable
,
202 sizeof(WORD
) * Tables
[i
] -> nEntries
);
204 NewMatShaper
-> L
[i
] = PtrW
; // Set table pointer
208 AllLinear
+= cmsIsLinear(PtrW
, NewMatShaper
-> p16
.nSamples
);
211 // If is all linear, then supress table interpolation (this
212 // will speed greately some trivial operations
215 NewMatShaper
-> dwFlags
|= MATSHAPER_HASSHAPER
;
222 // Free associated memory
224 void cmsFreeMatShaper(LPMATSHAPER MatShaper
)
228 if (!MatShaper
) return;
230 // Release references to the precaches if we have them
231 if (MatShaper
->L_Precache
!= NULL
)
232 PRECACHE_RELEASE(MatShaper
->L_Precache
);
233 if (MatShaper
->L2_Precache
!= NULL
)
234 PRECACHE_RELEASE(MatShaper
->L2_Precache
);
236 for (i
=0; i
< 3; i
++)
238 // These are never initialized from their zeroed state if we
239 // were using a cache
240 if (MatShaper
-> L
[i
]) _cmsFree(MatShaper
->L
[i
]);
241 if (MatShaper
-> L2
[i
]) _cmsFree(MatShaper
->L2
[i
]);
248 // All smelted must postpose gamma to last stage.
251 void AllSmeltedBehaviour(LPMATSHAPER MatShaper
, WORD In
[], WORD Out
[])
255 WVEC3 InVect
, OutVect
;
257 if (MatShaper
-> dwFlags
& MATSHAPER_HASINPSHAPER
)
259 if (MatShaper
->L2_Precache
!= NULL
)
261 InVect
.n
[VX
] = MatShaper
->L2_Precache
->Impl
.LI16W_FORWARD
.Cache
[0][In
[0]];
262 InVect
.n
[VY
] = MatShaper
->L2_Precache
->Impl
.LI16W_FORWARD
.Cache
[1][In
[1]];
263 InVect
.n
[VZ
] = MatShaper
->L2_Precache
->Impl
.LI16W_FORWARD
.Cache
[2][In
[2]];
267 InVect
.n
[VX
] = cmsLinearInterpFixed(In
[0], MatShaper
-> L2
[0], &MatShaper
-> p2_16
);
268 InVect
.n
[VY
] = cmsLinearInterpFixed(In
[1], MatShaper
-> L2
[1], &MatShaper
-> p2_16
);
269 InVect
.n
[VZ
] = cmsLinearInterpFixed(In
[2], MatShaper
-> L2
[2], &MatShaper
-> p2_16
);
274 InVect
.n
[VX
] = ToFixedDomain(In
[0]);
275 InVect
.n
[VY
] = ToFixedDomain(In
[1]);
276 InVect
.n
[VZ
] = ToFixedDomain(In
[2]);
280 if (MatShaper
-> dwFlags
& MATSHAPER_HASMATRIX
)
283 MAT3evalW(&OutVect
, &MatShaper
-> Matrix
.W
, &InVect
);
287 OutVect
.n
[VX
] = InVect
.n
[VX
];
288 OutVect
.n
[VY
] = InVect
.n
[VY
];
289 OutVect
.n
[VZ
] = InVect
.n
[VZ
];
293 tmp
[0] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VX
]));
294 tmp
[1] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VY
]));
295 tmp
[2] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VZ
]));
299 if (MatShaper
-> dwFlags
& MATSHAPER_HASSHAPER
)
301 if (MatShaper
->L_Precache
!= NULL
)
303 Out
[0] = MatShaper
->L_Precache
->Impl
.LI1616_REVERSE
.Cache
[0][tmp
[0]];
304 Out
[1] = MatShaper
->L_Precache
->Impl
.LI1616_REVERSE
.Cache
[1][tmp
[1]];
305 Out
[2] = MatShaper
->L_Precache
->Impl
.LI1616_REVERSE
.Cache
[2][tmp
[2]];
309 Out
[0] = cmsLinearInterpLUT16(tmp
[0], MatShaper
-> L
[0], &MatShaper
-> p16
);
310 Out
[1] = cmsLinearInterpLUT16(tmp
[1], MatShaper
-> L
[1], &MatShaper
-> p16
);
311 Out
[2] = cmsLinearInterpLUT16(tmp
[2], MatShaper
-> L
[2], &MatShaper
-> p16
);
325 void InputBehaviour(LPMATSHAPER MatShaper
, WORD In
[], WORD Out
[])
327 WVEC3 InVect
, OutVect
;
330 if (MatShaper
-> dwFlags
& MATSHAPER_HASSHAPER
)
332 InVect
.n
[VX
] = cmsLinearInterpFixed(In
[0], MatShaper
-> L
[0], &MatShaper
-> p16
);
333 InVect
.n
[VY
] = cmsLinearInterpFixed(In
[1], MatShaper
-> L
[1], &MatShaper
-> p16
);
334 InVect
.n
[VZ
] = cmsLinearInterpFixed(In
[2], MatShaper
-> L
[2], &MatShaper
-> p16
);
338 InVect
.n
[VX
] = ToFixedDomain(In
[0]);
339 InVect
.n
[VY
] = ToFixedDomain(In
[1]);
340 InVect
.n
[VZ
] = ToFixedDomain(In
[2]);
343 if (MatShaper
-> dwFlags
& MATSHAPER_HASMATRIX
)
345 MAT3evalW(&OutVect
, &MatShaper
-> Matrix
.W
, &InVect
);
352 // PCS in 1Fixed15 format, adjusting
354 Out
[0] = _cmsClampWord((OutVect
.n
[VX
]) >> 1);
355 Out
[1] = _cmsClampWord((OutVect
.n
[VY
]) >> 1);
356 Out
[2] = _cmsClampWord((OutVect
.n
[VZ
]) >> 1);
362 void OutputBehaviour(LPMATSHAPER MatShaper
, WORD In
[], WORD Out
[])
364 WVEC3 InVect
, OutVect
;
367 // We need to convert from XYZ to RGB, here we must
368 // shift << 1 to pass between 1.15 to 15.16 formats
370 InVect
.n
[VX
] = (Fixed32
) In
[0] << 1;
371 InVect
.n
[VY
] = (Fixed32
) In
[1] << 1;
372 InVect
.n
[VZ
] = (Fixed32
) In
[2] << 1;
374 if (MatShaper
-> dwFlags
& MATSHAPER_HASMATRIX
)
376 MAT3evalW(&OutVect
, &MatShaper
-> Matrix
.W
, &InVect
);
384 if (MatShaper
-> dwFlags
& MATSHAPER_HASSHAPER
)
386 for (i
=0; i
< 3; i
++)
389 Out
[i
] = cmsLinearInterpLUT16(
390 _cmsClampWord(FromFixedDomain(OutVect
.n
[i
])),
397 // Result from fixed domain to RGB
399 Out
[0] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VX
]));
400 Out
[1] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VY
]));
401 Out
[2] = _cmsClampWord(FromFixedDomain(OutVect
.n
[VZ
]));
407 // Master on evaluating shapers, 3 different behaviours
409 void cmsEvalMatShaper(LPMATSHAPER MatShaper
, WORD In
[], WORD Out
[])
411 if ((MatShaper
-> dwFlags
& MATSHAPER_ALLSMELTED
) == MATSHAPER_ALLSMELTED
)
413 AllSmeltedBehaviour(MatShaper
, In
, Out
);
416 if (MatShaper
-> dwFlags
& MATSHAPER_INPUT
)
418 InputBehaviour(MatShaper
, In
, Out
);
422 OutputBehaviour(MatShaper
, In
, Out
);