📄 cmsintrp.c
字号:
//// Little cms// Copyright (C) 1998-2005 Marti Maria//// Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the Software // is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.// Interpolation#include "lcms.h"void cmsCalcL16Params(int nSamples, LPL16PARAMS p){ p -> nSamples = nSamples; p -> Domain = (WORD) (nSamples - 1); p -> nInputs = p -> nOutputs = 1; }// Eval gray LUT having only one input channel staticvoid Eval1Input(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, k1, rk, K0, K1; int OutChan; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = (WORD) FIXED_REST_TO_INT(fk); k1 = k0 + (StageABC[0] != 0xFFFFU ? 1 : 0); K0 = p16 -> opta1 * k0; K1 = p16 -> opta1 * k1; for (OutChan=0; OutChan < p16->nOutputs; OutChan++) { StageLMN[OutChan] = (WORD) FixedLERP(rk, LutTable[K0+OutChan], LutTable[K1+OutChan]); }}// For more that 3 inputs (i.e., CMYK)// evaluate two 3-dimensional interpolations and then linearly interpolate between them.staticvoid Eval4Inputs(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, rk; int K0, K1; LPWORD T; int i; WORD Tmp1[MAXCHANNELS], Tmp2[MAXCHANNELS]; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = FIXED_REST_TO_INT(fk); K0 = p16 -> opta4 * k0; K1 = p16 -> opta4 * (k0 + (StageABC[0] != 0xFFFFU ? 1 : 0)); p16 -> nInputs = 3; T = LutTable + K0; cmsTetrahedralInterp16(StageABC + 1, Tmp1, T, p16); T = LutTable + K1; cmsTetrahedralInterp16(StageABC + 1, Tmp2, T, p16); p16 -> nInputs = 4; for (i=0; i < p16 -> nOutputs; i++) { StageLMN[i] = (WORD) FixedLERP(rk, Tmp1[i], Tmp2[i]); }}staticvoid Eval5Inputs(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, rk; int K0, K1; LPWORD T; int i; WORD Tmp1[MAXCHANNELS], Tmp2[MAXCHANNELS]; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = FIXED_REST_TO_INT(fk); K0 = p16 -> opta5 * k0; K1 = p16 -> opta5 * (k0 + (StageABC[0] != 0xFFFFU ? 1 : 0)); p16 -> nInputs = 4; T = LutTable + K0; Eval4Inputs(StageABC + 1, Tmp1, T, p16); T = LutTable + K1; Eval4Inputs(StageABC + 1, Tmp2, T, p16); p16 -> nInputs = 5; for (i=0; i < p16 -> nOutputs; i++) { StageLMN[i] = (WORD) FixedLERP(rk, Tmp1[i], Tmp2[i]); }}staticvoid Eval6Inputs(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, rk; int K0, K1; LPWORD T; int i; WORD Tmp1[MAXCHANNELS], Tmp2[MAXCHANNELS]; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = FIXED_REST_TO_INT(fk); K0 = p16 -> opta6 * k0; K1 = p16 -> opta6 * (k0 + (StageABC[0] != 0xFFFFU ? 1 : 0)); p16 -> nInputs = 5; T = LutTable + K0; Eval5Inputs(StageABC + 1, Tmp1, T, p16); T = LutTable + K1; Eval5Inputs(StageABC + 1, Tmp2, T, p16); p16 -> nInputs = 6; for (i=0; i < p16 -> nOutputs; i++) { StageLMN[i] = (WORD) FixedLERP(rk, Tmp1[i], Tmp2[i]); }}staticvoid Eval7Inputs(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, rk; int K0, K1; LPWORD T; int i; WORD Tmp1[MAXCHANNELS], Tmp2[MAXCHANNELS]; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = FIXED_REST_TO_INT(fk); K0 = p16 -> opta7 * k0; K1 = p16 -> opta7 * (k0 + (StageABC[0] != 0xFFFFU ? 1 : 0)); p16 -> nInputs = 6; T = LutTable + K0; Eval6Inputs(StageABC + 1, Tmp1, T, p16); T = LutTable + K1; Eval6Inputs(StageABC + 1, Tmp2, T, p16); p16 -> nInputs = 7; for (i=0; i < p16 -> nOutputs; i++) { StageLMN[i] = (WORD) FixedLERP(rk, Tmp1[i], Tmp2[i]); }}staticvoid Eval8Inputs(WORD StageABC[], WORD StageLMN[], WORD LutTable[], LPL16PARAMS p16){ Fixed32 fk; Fixed32 k0, rk; int K0, K1; LPWORD T; int i; WORD Tmp1[MAXCHANNELS], Tmp2[MAXCHANNELS]; fk = ToFixedDomain((Fixed32) StageABC[0] * p16 -> Domain); k0 = FIXED_TO_INT(fk); rk = FIXED_REST_TO_INT(fk); K0 = p16 -> opta8 * k0; K1 = p16 -> opta8 * (k0 + (StageABC[0] != 0xFFFFU ? 1 : 0)); p16 -> nInputs = 7; T = LutTable + K0; Eval7Inputs(StageABC + 1, Tmp1, T, p16); T = LutTable + K1; Eval7Inputs(StageABC + 1, Tmp2, T, p16); p16 -> nInputs = 8; for (i=0; i < p16 -> nOutputs; i++) { StageLMN[i] = (WORD) FixedLERP(rk, Tmp1[i], Tmp2[i]); }}// Fills optimization parametersvoid cmsCalcCLUT16ParamsEx(int nSamples, int InputChan, int OutputChan, BOOL lUseTetrahedral, LPL16PARAMS p){ int clutPoints; cmsCalcL16Params(nSamples, p); p -> nInputs = InputChan; p -> nOutputs = OutputChan; clutPoints = p -> Domain + 1; p -> opta1 = p -> nOutputs; // Z p -> opta2 = p -> opta1 * clutPoints; // Y p -> opta3 = p -> opta2 * clutPoints; // X p -> opta4 = p -> opta3 * clutPoints; // Used only in 4 inputs LUT p -> opta5 = p -> opta4 * clutPoints; // Used only in 5 inputs LUT p -> opta6 = p -> opta5 * clutPoints; // Used only on 6 inputs LUT p -> opta7 = p -> opta6 * clutPoints; // Used only on 7 inputs LUT p -> opta8 = p -> opta7 * clutPoints; // Used only on 8 inputs LUT switch (InputChan) { case 1: // Gray LUT p ->Interp3D = Eval1Input; break; case 3: // RGB et al if (lUseTetrahedral) { p ->Interp3D = cmsTetrahedralInterp16; } else p ->Interp3D = cmsTrilinearInterp16; break; case 4: // CMYK LUT p ->Interp3D = Eval4Inputs; break; case 5: // 5 Inks p ->Interp3D = Eval5Inputs; break; case 6: // 6 Inks p -> Interp3D = Eval6Inputs; break; case 7: // 7 inks p ->Interp3D = Eval7Inputs; break; case 8: // 8 inks p ->Interp3D = Eval8Inputs; break; default: cmsSignalError(LCMS_ERRC_ABORTED, "Unsupported restoration (%d channels)", InputChan); } }void cmsCalcCLUT16Params(int nSamples, int InputChan, int OutputChan, LPL16PARAMS p){ cmsCalcCLUT16ParamsEx(nSamples, InputChan, OutputChan, FALSE, p);}#ifdef USE_FLOAT// Floating-point versionWORD cmsLinearInterpLUT16(WORD Value, WORD LutTable[], LPL16PARAMS p){ double y1, y0; double y; double val2, rest; int cell0, cell1; // if last value... if (Value == 0xffff) return LutTable[p -> Domain]; val2 = p -> Domain * ((double) Value / 65535.0); cell0 = (int) floor(val2); cell1 = (int) ceil(val2); // Rest is 16 LSB bits rest = val2 - cell0; y0 = LutTable[cell0] ; y1 = LutTable[cell1] ; y = y0 + (y1 - y0) * rest; return (WORD) floor(y+.5);}#endif//// Linear interpolation (Fixed-point optimized, but C source)//#ifdef USE_CWORD cmsLinearInterpLUT16(WORD Value1, WORD LutTable[], LPL16PARAMS p){ WORD y1, y0; WORD y; int dif, a1; int cell0, rest; int val3, Value; // if last value... Value = Value1; if (Value == 0xffff) return LutTable[p -> Domain]; val3 = p -> Domain * Value; val3 = ToFixedDomain(val3); // To fixed 15.16 cell0 = FIXED_TO_INT(val3); // Cell is 16 MSB bits rest = FIXED_REST_TO_INT(val3); // Rest is 16 LSB bits y0 = LutTable[cell0] ; y1 = LutTable[cell0+1] ; dif = (int) y1 - y0; // dif is in domain -ffff ... ffff if (dif >= 0) { a1 = ToFixedDomain(dif * rest); a1 += 0x8000; } else { a1 = ToFixedDomain((- dif) * rest); a1 -= 0x8000; a1 = -a1; } y = (WORD) (y0 + FIXED_TO_INT(a1)); return y;}#endif// Linear interpolation (asm by hand optimized)#ifdef USE_ASSEMBLER#ifdef _MSC_VER#pragma warning(disable : 4033)#endifWORD cmsLinearInterpLUT16(WORD Value, WORD LutTable[], LPL16PARAMS p){ int xDomain = p -> Domain; if (Value == 0xffff) return LutTable[p -> Domain]; else ASM { xor eax, eax mov ax, word ptr ss:Value mov edx, ss:xDomain mul edx // val3 = p -> Domain * Value; shld edx, eax, 16 // Convert it to fixed 15.16 shl eax, 16 // * 65536 / 65535 mov ebx, 0x0000ffff div ebx mov ecx, eax sar ecx, 16 // ecx = cell0 mov edx, eax // rest = (val2 & 0xFFFFU) and edx, 0x0000ffff // edx = rest mov ebx, ss:LutTable lea eax, dword ptr [ebx+2*ecx] // Ptr to LUT xor ebx, ebx mov bx, word ptr [eax] // EBX = y0 movzx eax, word ptr [eax+2] // EAX = y1 sub eax, ebx // EAX = y1-y0 js IsNegative mul edx // EAX = EAX * rest shld edx, eax, 16 // Pass it to fixed sal eax, 16 // * 65536 / 65535 mov ecx, 0x0000ffff div ecx add eax, 0x8000 // Rounding sar eax, 16 add eax, ebx // Done! } RET((WORD) _EAX); IsNegative: ASM { neg eax mul edx // EAX = EAX * rest shld edx, eax, 16 // Pass it to fixed sal eax, 16 // * 65536 / 65535 mov ecx, 0x0000ffff div ecx sub eax, 0x8000 neg eax sar eax, 16 add eax, ebx // Done! } RET((WORD) _EAX);}#ifndef __BORLANDC__#pragma warning(default : 4033)#endif#endifFixed32 cmsLinearInterpFixed(WORD Value1, WORD LutTable[], LPL16PARAMS p){ Fixed32 y1, y0; int cell0; int val3, Value; // if last value... Value = Value1; if (Value == 0xffffU) return LutTable[p -> Domain]; val3 = p -> Domain * Value; val3 = ToFixedDomain(val3); // To fixed 15.16 cell0 = FIXED_TO_INT(val3); // Cell is 16 MSB bits y0 = LutTable[cell0] ; y1 = LutTable[cell0+1] ; return y0 + FixedMul((y1 - y0), (val3 & 0xFFFFL));}// Reverse Lineal interpolation (16 bits)// Im using a sort of binary search here, this is not a time-critical functionWORD cmsReverseLinearInterpLUT16(WORD Value, WORD LutTable[], LPL16PARAMS p){ register int l = 1; register int r = 0x10000; register int x = 0, res; // 'int' Give spacing for negative values int NumZeroes, NumPoles; int cell0, cell1; double val2; double y0, y1, x0, x1; double a, b, f; // July/27 2001 - Expanded to handle degenerated curves with an arbitrary // number of elements containing 0 at the begining of the table (Zeroes) // and another arbitrary number of poles (FFFFh) at the end. // First the zero and pole extents are computed, then value is compared. NumZeroes = 0; while (LutTable[NumZeroes] == 0 && NumZeroes < p -> Domain) NumZeroes++; // There are no zeros at the beginning and we are trying to find a zero, so // return anything. It seems zero would be the less destructive choice if (NumZeroes == 0 && Value == 0) return 0; NumPoles = 0; while (LutTable[p -> Domain - NumPoles] == 0xFFFF && NumPoles < p -> Domain) NumPoles++; // Does the curve belong to this case? if (NumZeroes > 1 || NumPoles > 1) { int a, b; // Identify if value fall downto 0 or FFFF zone if (Value == 0) return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -