/*
* This file is part of RawTherapee.
*
* RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see .
*
* 2010 Ilya Popov
* 2012 Emil Martinec
*/
#ifndef CPLX_WAVELET_LEVEL_H_INCLUDED
#define CPLX_WAVELET_LEVEL_H_INCLUDED
#include
#include
#include "array2D.h"
namespace rtengine {
#define MAX(a,b) ((a) > (b) ? (a) : (b))
/*template
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
class noop
{
public:
T operator()(T x)
{
return x;
}
};*/
/*template
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
void plane_copy(A ** a, B * b, size_t datalen)
{
for (size_t i=0; i (0.25f*(a[0][j]+a[1][j]+a[2][j]+a[3][j]))
}
}*/
//////////////////////////////////////////////////////////////////////////////
template
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 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);
public:
T ** wavcoeffs;
template
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);
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
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset);
template
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset);
};
//////////////////////////////////////////////////////////////////////////////
/*
template
void wavelet_level::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
void wavelet_level::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
T ** cplx_wavelet_level::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
void cplx_wavelet_level::destroy(T ** subbands)
{
if(subbands)
{
delete[] subbands[0];
delete[] subbands;
}
}
/*template template
void wavelet_level::decompose(E ** src)
{
noop l;
plane_copy(src, m_coeffs, m_w, m_h, l);
dwt_2d(m_w, m_h);
}
template template
void wavelet_level::reconstruct(E ** dst, int alpha, L & l)
{
idwt_2d(m_w, m_h, alpha);
plane_copy(m_coeffs, dst, m_w, m_h, l);
}*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template
void cplx_wavelet_level::AnalysisFilter (T * src, T * dstLo, T * dstHi, T *buffer, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen) {
/* Basic convolution code
* Applies an FIR filter 'filter' with 'len' taps,
* aligning the 'offset' element of the filter
* with the input pixel, and skipping 'pitch' pixels
* between taps (eg pitch=1 for horizontal filtering,
* pitch=W for vertical, pitch=W+1,W-1 for diagonals.
* Currently diagonal filtering is not supported
* 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
void cplx_wavelet_level::SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen) {
/* Basic convolution code
* Applies an FIR filter 'filter' with 'len' taps,
* aligning the 'offset' element of the filter
* with the input pixel, and skipping 'pitch' pixels
* between taps (eg pitch=1 for horizontal filtering,
* pitch=W for vertical, pitch=W+1,W-1 for diagonals.
* Currently diagonal filtering is not supported
* 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 template
void cplx_wavelet_level::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 *buffer = new T[MAX(m_w,m_h)+taps];
/* filter along columns */
for (int j=0; j template
void cplx_wavelet_level::reconstruct_level(E *dst, float *filterV, float *filterH, int taps, int offset) {
//int hfw = (W+1)/2;
//int hfh = (H+1)/2;
array2D tmpLo(m_w2,m_h);
array2D tmpHi(m_w2,m_h);
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