Skip to content

Commit 51fc891

Browse files
committed
cvtColor: fixed tables init, moved some tables to heap
1 parent c3cced0 commit 51fc891

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

modules/imgproc/src/color.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,6 @@ const int CB2GI = -5636;
140140
const int CR2GI = -11698;
141141
const int CR2RI = 22987;
142142

143-
// computes cubic spline coefficients for a function: (xi=i, yi=f[i]), i=0..n
144-
template<typename _Tp> static void splineBuild(const _Tp* f, int n, _Tp* tab)
145-
{
146-
_Tp cn = 0;
147-
int i;
148-
tab[0] = tab[1] = (_Tp)0;
149-
150-
for(i = 1; i < n-1; i++)
151-
{
152-
_Tp t = 3*(f[i+1] - 2*f[i] + f[i-1]);
153-
_Tp l = 1/(4 - tab[(i-1)*4]);
154-
tab[i*4] = l; tab[i*4+1] = (t - tab[(i-1)*4+1])*l;
155-
}
156-
157-
for(i = n-1; i >= 0; i--)
158-
{
159-
_Tp c = tab[i*4+1] - tab[i*4]*cn;
160-
_Tp b = f[i+1] - f[i] - (cn + c*2)*(_Tp)0.3333333333333333;
161-
_Tp d = (cn - c)*(_Tp)0.3333333333333333;
162-
tab[i*4] = f[i]; tab[i*4+1] = b;
163-
tab[i*4+2] = c; tab[i*4+3] = d;
164-
cn = c;
165-
}
166-
}
167-
168143
static void splineBuild(const softfloat* f, int n, float* tab)
169144
{
170145
const softfloat f2(2), f3(3), f4(4);
@@ -173,7 +148,7 @@ static void splineBuild(const softfloat* f, int n, float* tab)
173148
int i;
174149
tab[0] = tab[1] = 0.0f;
175150

176-
for(i = 1; i < n-1; i++)
151+
for(i = 1; i <= n-1; i++)
177152
{
178153
softfloat t = (f[i+1] - f[i]*f2 + f[i-1])*f3;
179154
softfloat l = softfloat::one()/(f4 - sftab[(i-1)*4]);
@@ -5845,10 +5820,11 @@ static const softdouble D65[] = {softdouble::fromRaw(0x3fee6a22b3892ee8),
58455820
softdouble::fromRaw(0x3ff16b8950763a19)};
58465821

58475822
enum { LAB_CBRT_TAB_SIZE = 1024, GAMMA_TAB_SIZE = 1024 };
5848-
static float LabCbrtTab[LAB_CBRT_TAB_SIZE*4];
5823+
static float *LabCbrtTab;
58495824
static const float LabCbrtTabScale = softfloat(LAB_CBRT_TAB_SIZE*2)/softfloat(3);
58505825

5851-
static float sRGBGammaTab[GAMMA_TAB_SIZE*4], sRGBInvGammaTab[GAMMA_TAB_SIZE*4];
5826+
static float *sRGBGammaTab;
5827+
static float *sRGBInvGammaTab;
58525828
static const float GammaTabScale((int)GAMMA_TAB_SIZE);
58535829

58545830
static ushort sRGBGammaTab_b[256], linearGammaTab_b[256];
@@ -5873,21 +5849,21 @@ enum
58735849
trilinear_shift = 8 - lab_lut_shift + 1,
58745850
TRILINEAR_BASE = (1 << trilinear_shift)
58755851
};
5876-
static int16_t RGB2LabLUT_s16[LAB_LUT_DIM*LAB_LUT_DIM*LAB_LUT_DIM*3*8];
5852+
static int16_t *RGB2LabLUT_s16;
58775853
static int16_t trilinearLUT[TRILINEAR_BASE*TRILINEAR_BASE*TRILINEAR_BASE*8];
58785854
static ushort LabToYF_b[256*2];
58795855
static const int minABvalue = -8145;
5880-
static int abToXZ_b[LAB_BASE*9/4];
5856+
static int *abToXZ_b;
58815857
// Luv constants
58825858
static const bool enableRGB2LuvInterpolation = true;
58835859
static const bool enablePackedRGB2Luv = true;
58845860
static const bool enablePackedLuv2RGB = true;
5885-
static int16_t RGB2LuvLUT_s16[LAB_LUT_DIM*LAB_LUT_DIM*LAB_LUT_DIM*3*8];
5861+
static int16_t *RGB2LuvLUT_s16;
58865862
static const softfloat uLow(-134), uHigh(220), uRange(uHigh-uLow);
58875863
static const softfloat vLow(-140), vHigh(122), vRange(vHigh-vLow);
5888-
static int LuToUp_b[256*256];
5889-
static int LvToVp_b[256*256];
5890-
static long long int LvToVpl_b[256*256];
5864+
static int *LuToUp_b;
5865+
static int *LvToVp_b;
5866+
static long long int *LvToVpl_b;
58915867

58925868
#define clip(value) \
58935869
value < 0.0f ? 0.0f : value > 1.0f ? 1.0f : value;
@@ -5935,6 +5911,7 @@ static void initLabTabs()
59355911
softfloat x = scale*softfloat(i);
59365912
f[i] = x < lthresh ? mulAdd(x, lscale, lbias) : cbrt(x);
59375913
}
5914+
LabCbrtTab = new float[LAB_CBRT_TAB_SIZE*4];
59385915
splineBuild(f, LAB_CBRT_TAB_SIZE, LabCbrtTab);
59395916

59405917
scale = softfloat::one()/softfloat(GammaTabScale);
@@ -5944,6 +5921,9 @@ static void initLabTabs()
59445921
g[i] = applyGamma(x);
59455922
ig[i] = applyInvGamma(x);
59465923
}
5924+
5925+
sRGBGammaTab = new float[GAMMA_TAB_SIZE*4];
5926+
sRGBInvGammaTab = new float[GAMMA_TAB_SIZE*4];
59475927
splineBuild(g, GAMMA_TAB_SIZE, sRGBGammaTab);
59485928
splineBuild(ig, GAMMA_TAB_SIZE, sRGBInvGammaTab);
59495929

@@ -5999,6 +5979,7 @@ static void initLabTabs()
59995979
}
60005980

60015981
//Lookup table for a,b to x,z conversion
5982+
abToXZ_b = new int[LAB_BASE*9/4];
60025983
for(i = minABvalue; i < LAB_BASE*9/4+minABvalue; i++)
60035984
{
60045985
int v;
@@ -6032,6 +6013,9 @@ static void initLabTabs()
60326013
*/
60336014

60346015
//Luv LUT
6016+
LuToUp_b = new int[256*256];
6017+
LvToVp_b = new int[256*256];
6018+
LvToVpl_b = new long long int[256*256];
60356019
for(int LL = 0; LL < 256; LL++)
60366020
{
60376021
softfloat L = softfloat(LL*100)/f255;
@@ -6145,6 +6129,8 @@ static void initLabTabs()
61456129
}
61466130
}
61476131
}
6132+
RGB2LabLUT_s16 = new int16_t[LAB_LUT_DIM*LAB_LUT_DIM*LAB_LUT_DIM*3*8];
6133+
RGB2LuvLUT_s16 = new int16_t[LAB_LUT_DIM*LAB_LUT_DIM*LAB_LUT_DIM*3*8];
61486134
for(int p = 0; p < LAB_LUT_DIM; p++)
61496135
{
61506136
for(int q = 0; q < LAB_LUT_DIM; q++)
@@ -6199,15 +6185,15 @@ static void initLabTabs()
61996185

62006186

62016187
// cx, cy, cz are in [0; LAB_BASE]
6202-
static inline void trilinearInterpolate(int cx, int cy, int cz, int16_t* LUT,
6188+
static inline void trilinearInterpolate(int cx, int cy, int cz, const int16_t* LUT,
62036189
int& a, int& b, int& c)
62046190
{
62056191
//LUT idx of origin pt of cube
62066192
int tx = cx >> (lab_base_shift - lab_lut_shift);
62076193
int ty = cy >> (lab_base_shift - lab_lut_shift);
62086194
int tz = cz >> (lab_base_shift - lab_lut_shift);
62096195

6210-
int16_t* baseLUT = &LUT[3*8*tx + (3*8*LAB_LUT_DIM)*ty + (3*8*LAB_LUT_DIM*LAB_LUT_DIM)*tz];
6196+
const int16_t* baseLUT = &LUT[3*8*tx + (3*8*LAB_LUT_DIM)*ty + (3*8*LAB_LUT_DIM*LAB_LUT_DIM)*tz];
62116197
int aa[8], bb[8], cc[8];
62126198
for(int i = 0; i < 8; i++)
62136199
{

0 commit comments

Comments
 (0)