Patching dual tree complex wavelet code. It works now...

This commit is contained in:
Emil Martinec
2012-01-27 20:57:47 -06:00
parent 325d8d1620
commit 163098df71
5 changed files with 264 additions and 489 deletions

View File

@@ -124,7 +124,7 @@ void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage
}*/
//cplx_wavelet_decomposition Ldecomp(dst->data, dst->W, dst->H, 2 /*maxlvl*/);
//Ldecomp.reconstruct(dst->data);
impulse_nr (dst, 50.0f/20.0f);
//PF_correct_RT(dst, dst, defringe.radius, defringe.threshold);
@@ -180,7 +180,7 @@ void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage
for (int j=0; j<src->width; j++) {
//float wt = expf(-100.0f*fabs(dst->L[i][j]-blur->L[i][j])/((float)dnparams.luma));
//blur->L[i][j] = wt*dst->L[i][j] + (1-wt)*blur->L[i][j];
//blur->L[i][j] = dst->L[i][j];
blur->a[i][j] /*= 32768.0f*0.2f*(blur->a[i][j]-blur->L[i][j]);/*/*= 32768.0f;
blur->b[i][j] /*= 32768.0f*0.2f*(blur->L[i][j]-blur->b[i][j]);/*/*= 32768.0f;
blur->L[i][j] *= 32768.0f;//= gamcurve[32768.0f*blur->L[i][j]];
@@ -191,7 +191,7 @@ void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage
}
}
//dirpyr_ab(blur, blur, dnparams);//use dirpyr here if using it to blur ab channels only
//dirpyrLab_denoise(blur, blur, dnparams);//use dirpyr here if using it to blur ab channels only
@@ -705,7 +705,7 @@ void ImProcFunctions::RGBtile_denoise (fftwf_complex ** fLblox, fftwf_complex **
//float RLfactor = 1;//(2*RLblockvar)/(eps+RLwsq+RLblockvar);
//float BLfactor = 1;//(2*BLblockvar)/(eps+BLwsq+BLblockvar);
Lwsq = MAX(0.0f, Lwsq-0.25*Lblockvar);
float Lshrinkfactor = Lwsq/( Lwsq+noisevar_L * Lfactor);
float Lshrinkfactor = Lwsq/(Lwsq + noisevar_L * Lfactor);
//float RLshrinkfactor = RLwsq/(RLwsq+noisevar_ab*RLfactor);
//float BLshrinkfactor = BLwsq/(BLwsq+noisevar_ab*BLfactor);

View File

@@ -24,10 +24,15 @@ namespace rtengine {
cplx_wavelet_decomposition::~cplx_wavelet_decomposition()
{
for(int i = 0; i < lvltot; i++)
{
delete dual_tree_coeffs[i];
for(int i = 0; i < lvltot; i++) {
for (int j=0; j<4; j++) {
delete dual_tree_coeffs[i][j];
}
}
delete[] first_lev_anal;
delete[] first_lev_synth;
delete[] wavfilt_anal;
delete[] wavfilt_synth;
}
};

View File

@@ -37,7 +37,7 @@ template <typename A, typename B>
void copy_out(A ** a, B * b, size_t datalen)
{
for (size_t j=0; j<datalen; j++) {
b[j] = static_cast<B> (0.25f*(a[0][j]+a[1][j]+a[2][j]+a[3][j]));
b[j] = static_cast<B> (0.25*(a[0][j]+a[1][j]+a[2][j]+a[3][j]));
}
}
@@ -59,16 +59,16 @@ private:
size_t m_w1, m_h1;
int first_lev_len, first_lev_offset;
//multi_array2D<float,2> first_lev_anal;
//multi_array2D<float,2> first_lev_synth;
float *first_lev_anal;
float *first_lev_synth;
int wavfilt_len, wavfilt_offset;
//multi_array2D<float,2> wavfilt_anal;
//multi_array2D<float,2> wavfilt_synth;
float *wavfilt_anal;
float *wavfilt_synth;
int testfilt_len, testfilt_offset;
float *testfilt_anal;
float *testfilt_synth;
cplx_wavelet_level<internal_type> * dual_tree_coeffs[maxlevels][4];//m_c in old code
@@ -84,43 +84,6 @@ public:
};
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*template<typename E>
cplx_wavelet_decomposition::cplx_wavelet_decomposition(E * src, int width, int height, int maxlvl)
: lvltot(0), m_w(w), m_h(h), m_w1(0), m_h1(0)
{
m_w1 = w;
m_h1 = h;
m_c[0] = new cplx_wavelet_level<internal_type>(src, m_w1, m_h1, FSFarras);
lvltot = 1;
while(lvltot < maxlevels)
{
m_c[level] = new cplx_wavelet_level<internal_type>(m_c[lvltot-1]->data[0], m_c[lvltot-1]->width(),
m_c[lvltot-1]->height(), Kingsbury);
lvltot ++;
}
}*/
/*template<typename E, typename L>
void cplx_wavelet_decomposition::reconstruct(E * dst)
{
noop<internal_type> n;
for(int level = lvltot - 1; level > 0; level--)
{
int alpha = 1024 + 10 * c[level];
m_c[level]->reconstruct(m_c[level-1]->lowfreq(), alpha, n);
}
int alpha = 1024 + 10 * c[0];
m_c[0]->reconstruct(dst, alpha, l);
}*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -135,153 +98,48 @@ void cplx_wavelet_decomposition::reconstruct(E * dst)
//initialize wavelet filters
first_lev_len = FSFarras_len;
first_lev_offset = FSFarras_offset;
//multi_array2D<float,2> first_lev_anal(2,first_lev_len);
//multi_array2D<float,2> first_lev_synth(2,first_lev_len);
float *first_level_anal = new float[4*first_lev_len];
float *first_level_synth = new float[4*first_lev_len];
first_lev_len = Kingsbury_len;
first_lev_offset = Kingsbury_offset;
first_lev_anal = new float[4*first_lev_len];
first_lev_synth = new float[4*first_lev_len];
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int i=0; i<first_lev_len; i++) {
//first_lev_anal[n][m][i] = FSFarras_anal[n][m][i];
//first_lev_synth[n][m][i] = FSFarras_anal[n][m][first_lev_len-1-i];
first_lev_anal[first_lev_len*(2*n+m)+i] = FSFarras_anal[n][m][i];
first_lev_synth[first_lev_len*(2*n+m)+i] = FSFarras_anal[n][m][first_lev_len-1-i];
first_lev_anal[first_lev_len*(2*n+m)+i] = Kingsbury_anal[n][m][i];
first_lev_synth[first_lev_len*(2*n+m)+i] = Kingsbury_anal[n][m][first_lev_len-1-i];
}
}
}
wavfilt_len = Kingsbury_len;
wavfilt_offset = Kingsbury_offset;
//multi_array2D<float,2> wavfilt_anal(2,Kingsbury_len);
//multi_array2D<float,2> wavfilt_synth(2,Kingsbury_len);
float *wavfilt_anal = new float[4*wavfilt_len];
float *wavfilt_synth = new float[4*wavfilt_len];
wavfilt_anal = new float[4*wavfilt_len];
wavfilt_synth = new float[4*wavfilt_len];
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int i=0; i<wavfilt_len; i++) {
//wavfilt_anal[n][m][i] = Kingsbury_anal[n][m][i];
//wavfilt_synth[n][m][i] = Kingsbury_anal[n][m][wavfilt_len-1-i];
wavfilt_anal[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][i];
wavfilt_synth[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][first_lev_len-1-i];
}
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Initialize wavelet coeffs
/*CplxWavelet AntonB = {
12,//length of filter
6,//offset
{//analysis filter
{{0, -0.08838834764832, 0.08838834764832, 0.69587998903400, 0.69587998903400,
0.08838834764832, -0.08838834764832, 0.01122679215254, 0.01122679215254, 0},
{0, 0, 0, 0.04563588155712, -0.02877176311425, -0.29563588155712 ,
0.55754352622850, -0.29563588155713, -0.02877176311425, 0.04563588155712, 0, 0}},
{{0 , 0 , 0.02674875741081, -0.01686411844287, -0.07822326652899, 0.26686411844288,
0.60294901823636, 0.26686411844287, -0.07822326652899, -0.01686411844287, 0.02674875741081, 0},
{0 , 0 , 0, 0 , 0.04563588155712, -0.02877176311425,
-0.29563588155712 , 0.55754352622850, -0.29563588155713, -0.02877176311425, 0.04563588155712 , 0}} },
{//synthesis filter
{{0 , 0 , 0, -0.04563588155712, -0.02877176311425, 0.29563588155712,
0.55754352622850, 0.29563588155713, -0.02877176311425, -0.04563588155712, 0, 0},
{0, 0.02674875741081, 0.01686411844287, -0.07822326652899, -0.26686411844288 , 0.60294901823636,
-0.26686411844287, -0.07822326652899, 0.01686411844287, 0.02674875741081, 0, 0}},
{{0 , 0, -0.04563588155712, -0.02877176311425, 0.29563588155712 , 0.55754352622850 ,
0.29563588155713, -0.02877176311425, -0.04563588155712, 0, 0 , 0},
{0.02674875741081 , 0.01686411844287, -0.07822326652899, -0.26686411844288 , 0.60294901823636, -0.26686411844287,
-0.07822326652899, 0.01686411844287 , 0.02674875741081 , 0 , 0, 0}} }
};*/
/*for (int i=0; i<4; i++)
for (int n=0; n<12; n++) {
AntonB.synth[i][n] *= 2;
}*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Initialize wavelet coeffs
//CplxWavelet FSFarras = {
/*int FSFarras_len = 10;//length of filter
int FSFarras_offset = 5;//offset
float FSFarras_anal[2][2][10] = {//analysis filter
{{0, -0.08838834764832, 0.08838834764832, 0.69587998903400, 0.69587998903400, 0.08838834764832, -0.08838834764832, 0.01122679215254 , 0.01122679215254, 0},
{ 0, -0.01122679215254, 0.01122679215254, 0.08838834764832, 0.08838834764832, -0.69587998903400, 0.69587998903400, -0.08838834764832, -0.08838834764832, 0}},
{{0.01122679215254, 0.01122679215254, -0.08838834764832, 0.08838834764832, 0.69587998903400, 0.69587998903400, 0.08838834764832, -0.08838834764832, 0, 0},
{0, 0, -0.08838834764832, -0.08838834764832, 0.69587998903400, -0.69587998903400, 0.08838834764832, 0.08838834764832, 0.01122679215254, -0.01122679215254}} };
float FSFarras_synth[2][2][10];*/
//};
/*for (int i=0; i<4; i++)
for (int n=0; n<10; n++) {
FSFarras_synth[i][n] = FSFarras_anal[i][9-n];
}*/
//sf = Reverse[af, 3];
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Initialize wavelet coeffs
/*
% Kingsbury Q-filters for the dual-tree complex DWT
%
% af{i},i=1,2-analysis filters for tree i
% sf{i},i=1,2-synthesis filters for tree i
% note:af{2} is the reverse of af{1}
% ordering is {af[1],af[2],sf[1],sf[2]}
% REFERENCE:% N.G.Kingsbury,"A dual-tree complex wavelet
% transform with improved orthogonality and symmetry
% properties",Proceedings of the IEEE Int.Conf.on
% Image Proc.(ICIP),2000 */
//CplxWavelet Kingsbury {
/*int Kingsbury_len = 10;//length of filter
int Kingsbury_offset = 5;//offset
float Kingsbury_anal[2][2][10] = {//analysis filter
{{0.03516384000000, 0, -0.08832942000000, 0.23389032000000, 0.76027237000000, 0.58751830000000, 0, -0.11430184000000 , 0, 0},
{ 0, 0, -0.11430184000000, 0, 0.58751830000000, -0.76027237000000, 0.23389032000000, 0.08832942000000, 0, -0.03516384000000}},
{{0, 0, -0.11430184000000, 0, 0.58751830000000, 0.76027237000000, 0.23389032000000, -0.08832942000000, 0, 0.03516384000000},
{-0.03516384000000, 0, 0.08832942000000, 0.23389032000000, -0.76027237000000, 0.58751830000000, 0, -0.11430184000000, 0, 0}} };
float Kingsbury_synth[2][2][10];*/
//};
/*for (int i=0; i<4; i++)
for (int n=0; n<10; n++) {
Kingsbury_synth[i][n] = Kingsbury_anal[i][9-n];
}*/
//sf = Reverse[af, 3];
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// data structure is dual_tree_coeffs[scale][2*n+m=2*(Re/Im)+dir][channel={lo,hi1,hi2,hi3}][pixel_array]
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
//dual_tree_coeffs[0][2*n+m] = new cplx_wavelet_level<internal_type>(src, first_lev_anal[n], first_lev_anal[m], first_lev_len, first_lev_offset);
dual_tree_coeffs[0][2*n+m] = new cplx_wavelet_level<internal_type>(src, m_w, m_h, first_lev_anal+first_lev_len*2*n, \
float padding = 0;//pow(2, maxlvl);//must be a multiple of two
dual_tree_coeffs[0][2*n+m] = new cplx_wavelet_level<internal_type>(src, padding, m_w, m_h, first_lev_anal+first_lev_len*2*n, \
first_lev_anal+first_lev_len*2*m, first_lev_len, first_lev_offset);
lvltot=1;
while(lvltot < maxlevels) {
//dual_tree_coeffs[lvltot][2*n+m] = new cplx_wavelet_level<internal_type>(dual_tree_coeffs[lvltot-1][2*n+m]->lopass()/*lopass*/, \
wavfilt_anal[n], wavfilt_anal[m], wavfilt_len, wavfilt_offset);
dual_tree_coeffs[lvltot][2*n+m] = new cplx_wavelet_level<internal_type>(dual_tree_coeffs[lvltot-1][2*n+m]->lopass()/*lopass*/, \
while(lvltot < maxlvl) {
dual_tree_coeffs[lvltot][2*n+m] = new cplx_wavelet_level<internal_type>(dual_tree_coeffs[lvltot-1][2*n+m]->lopass()/*lopass*/, 0/*no padding*/, \
dual_tree_coeffs[lvltot-1][2*n+m]->width(), \
dual_tree_coeffs[lvltot-1][2*n+m]->height(), \
wavfilt_anal+wavfilt_len*2*n, wavfilt_anal+wavfilt_len*2*m, wavfilt_len, wavfilt_offset);
@@ -341,23 +199,31 @@ void cplx_wavelet_decomposition::reconstruct(E * dst)
}
//y = ConstantArray[0, {vsizetmp, hsizetmp}];
array2D<internal_type> tmp(4,m_w*m_h);
internal_type ** tmp = new internal_type *[4];
for (int i=0; i<4; i++) {
tmp[i] = new internal_type[m_w*m_h];
}
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int lvl=lvltot-1; lvl>0; lvl--) {
//m_c[level]->reconstruct(m_c[level-1]->lowfreq(), alpha, n);
//dual_tree_coeffs[lvl][2*n+m]->reconstruct_level(dual_tree_coeffs[lvl-1][2*n+m]->wavcoeffs[0], wavfilt_synth[n], wavfilt_synth[m], wavfilt_len, wavfilt_offset);
dual_tree_coeffs[lvl][2*n+m]->reconstruct_level(dual_tree_coeffs[lvl-1][2*n+m]->wavcoeffs[0], wavfilt_synth+wavfilt_len*2*n, \
wavfilt_synth+wavfilt_len*2*m, wavfilt_len, wavfilt_offset);
}
//dual_tree_coeffs[0][2*n+m]->reconstruct_level(tmp[2*n+m], first_lev_synth[n], first_lev_synth[m], first_lev_len, first_lev_offset);
dual_tree_coeffs[0][2*n+m]->reconstruct_level(tmp[2*n+m], first_lev_synth+wavfilt_len*2*n, first_lev_synth+wavfilt_len*2*m, first_lev_len, first_lev_offset);
dual_tree_coeffs[0][2*n+m]->reconstruct_level(tmp[2*n+m], first_lev_synth+first_lev_len*2*n,
first_lev_synth+first_lev_len*2*m, first_lev_len, first_lev_offset);
}
}
copy_out(tmp,dst,m_w*m_h);
for (int i=0; i<4; i++) {
delete[] tmp[i];
}
delete[] tmp;
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@@ -29,238 +29,151 @@
namespace rtengine {
#define MAX(a,b) ((a) > (b) ? (a) : (b))
/*template<typename T>
class limiter //for limiting output between specified bounds
{
T min_value, max_value;
public:
limiter(T min, T max)
: min_value(min), max_value(max)
{}
T operator()(T x)
{
if(x < min_value)
return min_value;
if(x > max_value)
return max_value;
return x;
}
};*/
/*template<typename T>
class noop
{
public:
T operator()(T x)
{
return x;
}
};*/
/*template<typename T>
inline T clip(T x, T min_value, T max_value)
{
if(x < min_value)
return min_value;
if(x > max_value)
return max_value;
return x;
}*/
/*template <typename A, typename B>
void plane_copy(A ** a, B * b, size_t datalen)
{
for (size_t i=0; i<datalen; i++) {
b[j] = static_cast<B> (0.25f*(a[0][j]+a[1][j]+a[2][j]+a[3][j]))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
//////////////////////////////////////////////////////////////////////////////
template<typename T>
class cplx_wavelet_level
{
// full size
size_t m_w, m_h;
// size of low frequency part
size_t m_w2, m_h2;
// size of padded border
size_t m_pad;
// array of pointers to lines of coeffs
// actually is a single contiguous data array pointed by m_coeffs[0]
//T ** m_coeffs;
//array2D<float> wavcoeffs(4,1);
//data structure: first label is output channel (LL,LH,HL,HH), second is pixel location in flattened array
// weights storage
//T ** m_weights_rows;
//T ** m_weights_cols;
// allocation and destruction of data storage
T ** create(size_t n);
void destroy(T ** subbands);
// load a row/column of input data, possibly with padding
template<typename E>
void loadbuffer(E * src, E * dst, int srclen, int pitch);
//void dwt_2d(size_t w, size_t h);
//void idwt_2d(size_t w, size_t h, int alpha);
void AnalysisFilter (T * src, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen);
void SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen);
public:
T ** wavcoeffs;
template<typename E>
cplx_wavelet_level(E * src, int padding, size_t w, size_t h, float *filterV, float *filterH, int len, int offset)
: m_w(w), m_h(h), m_w2((w+1+2*padding)/2), m_h2((h+1+2*padding)/2), m_pad(padding), wavcoeffs(NULL)
{
//m_coeffs = create(w, h);
//m_weights_rows = create(w + 4, h);
//m_weights_cols = create(h + 4, w);
//decompose_level(src, w, h, wavcoeffs, float **filterV, float **filterH, int len, int offset);
wavcoeffs = create((m_w2)*(m_h2));
decompose_level(src, filterV, filterH, len, offset);
}
~cplx_wavelet_level()
{
destroy(wavcoeffs);
}
T ** subbands() const
{
return wavcoeffs;
}
T * lopass() const
{
return wavcoeffs[0];
}
size_t width() const
{
return m_w2;
}
size_t height() const
{
return m_h2;
}
template<typename E>
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset);
template<typename E>
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset);
};
//////////////////////////////////////////////////////////////////////////////
template<typename T>
T ** cplx_wavelet_level<T>::create(size_t n)
{
T * data = new T[4*n];
T ** subbands = new T*[4];
for(size_t j = 0; j < 4; j++)
{
subbands[j] = data + n * j;
}
return subbands;
}
}*/
//////////////////////////////////////////////////////////////////////////////
template<typename T>
class cplx_wavelet_level
{
// full size
size_t m_w, m_h;
// size of low frequency part
size_t m_w2, m_h2;
// array of pointers to lines of coeffs
// actually is a single contiguous data array pointed by m_coeffs[0]
//T ** m_coeffs;
//array2D<float> wavcoeffs(4,1);
//data structure: first label is output channel (LL,LH,HL,HH), second is pixel location in flattened array
// weights storage
//T ** m_weights_rows;
//T ** m_weights_cols;
// allocation and destruction of data storage
T ** create(size_t n);
void destroy(T ** subbands);
//void dwt_2d(size_t w, size_t h);
//void idwt_2d(size_t w, size_t h, int alpha);
void AnalysisFilter (T * src, T * dstLo, T * dstHi, T *buffer, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen);
void SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T>
void cplx_wavelet_level<T>::destroy(T ** subbands)
{
if(subbands)
{
delete[] subbands[0];
delete[] subbands;
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T> template<typename E>
void cplx_wavelet_level<T>::loadbuffer(E * src, E * dst, int pitch, int srclen)
{
E * tmp = dst + m_pad;
memset(dst, 0, (srclen+2*m_pad)*sizeof(E));
for(size_t i = 0, j = 0; i<srclen; i++, j += pitch)
{
tmp[i] = src[j];
}
public:
T ** wavcoeffs;
template<typename E>
cplx_wavelet_level(E * src, size_t w, size_t h, float *filterV, float *filterH, int len, int offset)
: m_w(w), m_h(h), m_w2((w+1)/2), m_h2((h+1)/2),
wavcoeffs(NULL)//,m_coeffs(NULL), m_weights_rows(NULL), m_weights_cols(NULL)
{
//m_coeffs = create(w, h);
//m_weights_rows = create(w + 4, h);
//m_weights_cols = create(h + 4, w);
//decompose_level(src, w, h, wavcoeffs, float **filterV, float **filterH, int len, int offset);
// extend mirror-like
wavcoeffs = create(m_w2*m_h2);
decompose_level(src, filterV, filterH, len, offset);
}
~cplx_wavelet_level()
{
//destroy(m_coeffs);
//destroy(m_weights_rows);
//destroy(m_weights_cols);
destroy(wavcoeffs);
}
T ** subbands() const
{
return wavcoeffs;//m_coeffs;
}
T * lopass() const
{
return wavcoeffs[0];//m_coeffs;
}
size_t width() const
{
return m_w2;
}
size_t height() const
{
return m_h2;
}
template<typename E>
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset);
template<typename E>
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset);
};
//////////////////////////////////////////////////////////////////////////////
/*
template<typename T>
void wavelet_level<T>::dwt_2d(size_t w, size_t h)
{
T * buffer = new T[std::max(w, h) + 4];
for(size_t j = 0; j < h; j++)
{
//dwt_haar(m_coeffs[j], 1, buffer, w);
//dwt_53(m_coeffs[j], 1, buffer, w);
dwt_wcdf(m_coeffs[j], 1, buffer, w, m_weights_rows[j]);
}
for(size_t i = 0; i < w; i++)
{
//dwt_haar(&m_coeffs[0][i], m_pitch, buffer, h);
//dwt_53(&m_coeffs[0][i], w, buffer, h);
dwt_wcdf(&m_coeffs[0][i], w, buffer, h, m_weights_cols[i]);
}
delete[] buffer;
}
template<typename T>
void wavelet_level<T>::idwt_2d(size_t w, size_t h, int alpha)
{
T * buffer = new T[std::max(w, h) + 4];
for(size_t i = 0; i < w; i++)
{
//idwt_haar(&m_coeffs[0][i], m_pitch, buffer, h, alpha);
//idwt_53(&m_coeffs[0][i], w, buffer, h, alpha);
idwt_wcdf(&m_coeffs[0][i], w, buffer, h, alpha, m_weights_cols[i]);
//idwt_noop(&m_coeffs[0][i], w, buffer, h, alpha);
}
for(size_t j = 0; j < h; j++)
{
//idwt_haar(m_coeffs[j], 1, buffer, w, alpha);
//idwt_53(m_coeffs[j], 1, buffer, w, alpha);
idwt_wcdf(m_coeffs[j], 1, buffer, w, alpha, m_weights_rows[j]);
//idwt_noop(m_coeffs[j], 1, buffer, w, alpha);
}
delete[] buffer;
}
*/
template<typename T>
T ** cplx_wavelet_level<T>::create(size_t n)
{
T * data = new T[4*n];
T ** subbands = new T*[4];
for(size_t j = 0; j < 4; j++)
{
subbands[j] = data + n * j;
}
return subbands;
}
template<typename T>
void cplx_wavelet_level<T>::destroy(T ** subbands)
{
if(subbands)
{
delete[] subbands[0];
delete[] subbands;
}
}
/*template<typename T> template<typename E>
void wavelet_level<T>::decompose(E ** src)
{
noop<T> l;
plane_copy(src, m_coeffs, m_w, m_h, l);
dwt_2d(m_w, m_h);
}
template<typename T> template<typename E, typename L>
void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
{
idwt_2d(m_w, m_h, alpha);
plane_copy(m_coeffs, dst, m_w, m_h, l);
}*/
for (size_t i=1; i<=MIN(srclen-1,m_pad); i++) {
tmp[-i] = tmp[i];
tmp[srclen+i-1] = tmp[srclen-i-1];
}
}
@@ -268,11 +181,11 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T>
void cplx_wavelet_level<T>::AnalysisFilter (T * src, T * dstLo, T * dstHi, T *buffer, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen) {
void cplx_wavelet_level<T>::AnalysisFilter (T * src, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen) {
/* Basic convolution code
* Applies an FIR filter 'filter' with 'len' taps,
* Applies an FIR filter 'filter' with filter length 'taps',
* aligning the 'offset' element of the filter
* with the input pixel, and skipping 'pitch' pixels
* between taps (eg pitch=1 for horizontal filtering,
@@ -281,47 +194,36 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
* for the full source array, until a more sophisticated
* treatment of mirror BC's is implemented.
*
* Destination arrays must be initialized to zero.
*/
T * tmp = buffer + taps;//offset
// copy data
for(size_t i = 0, j = 0; i < srclen; i++, j += pitch)
{
tmp[i] = src[j];
}
// extend mirror-like
for (size_t i=-1; i!=-offset; i--) {
tmp[i] = tmp[-i];
}
for (size_t i=0; i<taps-offset; i++) {
tmp[srclen+i] = tmp[srclen-i-2];
}
// calculate coefficients
for(ptrdiff_t i = 0; i < (ptrdiff_t)srclen; i+=2) {
for(int i = 0; i < (srclen); i+=2) {
float lo=0,hi=0;
for (int j=0; j<taps; j++) {
lo += filterLo[j] * tmp[i-offset+j];//lopass channel
hi += filterHi[j] * tmp[i-offset+j];//hipass channel
if (i>taps && i<srclen-taps) {//bulk
for (int j=0; j<taps; j++) {
lo += filterLo[j] * src[(i+offset-j)];//lopass channel
hi += filterHi[j] * src[(i+offset-j)];//hipass channel
}
} else {//boundary
for (int j=0; j<taps; j++) {
int arg = MAX(0,MIN(i+offset-j,srclen-1));//clamped BC's
lo += filterLo[j] * src[arg];//lopass channel
hi += filterHi[j] * src[arg];//hipass channel
}
}
dstLo[i] = lo;
dstHi[i] = hi;
dstLo[(pitch*(i/2))] = lo;
dstHi[(pitch*(i/2))] = hi;
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T>
void cplx_wavelet_level<T>::SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen) {
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen) {
/* Basic convolution code
* Applies an FIR filter 'filter' with 'len' taps,
@@ -333,41 +235,39 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
* for the full source array, until a more sophisticated
* treatment of mirror BC's is implemented.
*
* Destination arrays must be initialized to zero.
*/
T * tmpLo = bufferLo + taps;//offset
T * tmpHi = bufferHi + taps;//offset
// copy data
for(size_t i = 0, j = 0; i < dstlen; i++, j += pitch)
{
tmpLo[2*i] = srcLo[j];
tmpHi[2*i] = srcHi[j];
}
// extend mirror-like
for (size_t i=-1; i!=-offset; i--) {
tmpLo[2*i] = tmpLo[-i];
tmpHi[2*i] = tmpHi[-i];
}
for (size_t i=0; i<taps-offset; i++) {
tmpLo[2*(dstlen+i)] = tmpLo[dstlen-i-2];
tmpHi[2*(dstlen+i)] = tmpHi[dstlen-i-2];
}
// calculate coefficients
for(ptrdiff_t i = 0; i < (ptrdiff_t)dstlen; i++) {
float tot=0;
for (int j=0; j<taps; j++) {
tot += 0.5*(filterLo[j] * tmpLo[i-offset+j] + filterHi[j] * tmpHi[i-offset+j]);//lopass channel
}
dst[i] = tot;
int srclen=(dstlen+1+2*m_pad)/2;
for (int i=0; i<srclen; i++) {
bufferLo[i]=srcLo[i*pitch];
bufferHi[i]=srcHi[i*pitch];
}
int shift=taps-offset-1;
for(int i = m_pad; i < (dstlen-m_pad); i++) {
if (bufferLo[i]!=0) {
float xxx=bufferLo[i];
}
float tot=0;
int i_src = (i+shift)/2;
int begin = (i+shift)%2;
if (i>taps && i<(srclen-taps)) {//bulk
for (int j=begin, l=0; j<taps; j+=2, l++) {
tot += (filterLo[j] * bufferLo[i_src-l] + filterHi[j] * bufferHi[i_src-l]);
}
} else {//boundary
for (int j=begin, l=0; j<taps; j+=2, l++) {
int arg = MAX(0,MIN((i_src-l),srclen-1));//clamped BC's
tot += (filterLo[j] * bufferLo[arg] + filterHi[j] * bufferHi[arg]);
}
}
dst[pitch*(i-m_pad)] = tot;
}
}
@@ -376,24 +276,25 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
template<typename T> template<typename E>
void cplx_wavelet_level<T>::decompose_level(E *src, float *filterV, float *filterH, int taps, int offset) {
//int hfw = (W+1)/2;
//int hfh = (H+1)/2;
T *tmpLo = new T(m_w*m_h2);
T *tmpHi = new T(m_w*m_h2);
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
T *buffer = new T[MAX(m_w,m_h)+taps];
T *buffer = new T[MAX(m_w,m_h)+2*m_pad];
/* filter along columns */
for (int j=0; j<m_w; j++) {
AnalysisFilter (src+j, tmpLo+j, tmpHi+j, buffer, filterV, filterV+taps, taps, offset, m_w/*pitch*/, m_h/*srclen*/);
loadbuffer(src+j, buffer, m_w/*pitch*/, m_h/*srclen*/);//pad a column of data and load it to buffer
AnalysisFilter (buffer, tmpLo+j, tmpHi+j, filterV, filterV+taps, taps, offset, m_w/*output_pitch*/, m_h+2*m_pad/*srclen*/);
}
/* filter along rows */
for (int i=0; i<m_h2; i++) {
AnalysisFilter (tmpLo+i*m_w, wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, buffer, filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*srclen*/);
AnalysisFilter (tmpHi+i*m_w, wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, buffer, filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*srclen*/);
loadbuffer(tmpLo+i*m_w, buffer, 1/*pitch*/, m_w/*srclen*/);//pad a row of data and load it to buffer
AnalysisFilter (buffer, wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w+2*m_pad/*srclen*/);
loadbuffer(tmpHi+i*m_w, buffer, 1/*pitch*/, m_w/*srclen*/);
AnalysisFilter (buffer, wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w+2*m_pad/*srclen*/);
}
delete[] tmpLo;
delete[] tmpHi;
delete[] buffer;
@@ -407,31 +308,31 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
//int hfw = (W+1)/2;
//int hfh = (H+1)/2;
array2D<float> tmpLo(m_w2,m_h);
array2D<float> tmpHi(m_w2,m_h);
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
float *bufferLo = new float[MAX(m_w,m_h)+taps];
float *bufferHi = new float[MAX(m_w,m_h)+taps];
//bufferLo = (float (*)) calloc (MAX(m_w,m_h)+taps, sizeof *bufferLo);
//bufferHi = (float (*)) calloc (MAX(m_w,m_h)+taps, sizeof *bufferHi);
/* filter along columns */
for (int j=0; j<m_w2; j++) {
SynthesisFilter (wavcoeffs[0], wavcoeffs[1], tmpLo, bufferLo, bufferHi,
filterV, filterV+taps, taps, offset, m_w2/*pitch*/, m_h2/*srclen*/);
SynthesisFilter (wavcoeffs[2], wavcoeffs[3], tmpLo, bufferLo, bufferHi,
filterV, filterV+taps, taps, offset, m_w2/*pitch*/, m_h2/*srclen*/);
}
int buflen = MAX(m_w,m_h);
float *bufferLo = new float[buflen];
float *bufferHi = new float[buflen];
/* filter along rows */
for (int i=0; i<m_h2; i++) {
SynthesisFilter (tmpLo, tmpHi, dst, bufferLo, bufferHi,
filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w2/*srclen*/);
SynthesisFilter (wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, tmpLo+i*m_w, bufferLo, bufferHi, \
filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*dstlen*/);
SynthesisFilter (wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, tmpHi+i*m_w, bufferLo, bufferHi, \
filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*dstlen*/);
}
//free (bufferLo);
//free (bufferHi);
/* filter along columns */
for (int j=0; j<m_w; j++) {
SynthesisFilter (tmpLo+j, tmpHi+j, dst+j, bufferLo, bufferHi,
filterV, filterV+taps, taps, offset, m_w/*pitch*/, m_h/*dstlen*/);
}
delete[] tmpLo;
delete[] tmpHi;
delete[] bufferLo;
delete[] bufferHi;
@@ -440,7 +341,7 @@ void wavelet_level<T>::reconstruct(E ** dst, int alpha, L & l)
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
};
#endif

View File

@@ -35,6 +35,7 @@
#include "iccmatrices.h"
#include "color.h"
#include "calc_distort.h"
#include "cplx_wavelet_dec.h"
#ifdef _OPENMP
#include <omp.h>
@@ -558,7 +559,9 @@ void ImProcFunctions::colorCurve (LabImage* lold, LabImage* lnew) {
if (params->impulseDenoise.enabled && lab->W>=8 && lab->H>=8)
impulse_nr (lab, (float)params->impulseDenoise.thresh/20.0 );
//impulse_nr (lab, (float)params->impulseDenoise.thresh/20.0 );
{ cplx_wavelet_decomposition Ldecomp(lab->data, lab->W, lab->H, 1 /*maxlvl*/);
Ldecomp.reconstruct(lab->data);}
}
void ImProcFunctions::defringe (LabImage* lab) {