/*
* 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"
#include "gauss.h"
namespace rtengine {
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#define SQR(x) ((x)*(x))
//////////////////////////////////////////////////////////////////////////////
template
class 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;
// level of decomposition
int lvl;
// spacing of filter taps
size_t skip;
// 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);
// load a row/column of input data, possibly with padding
template
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 * srcbuffer, 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);
void imp_nr (T* src, int width, int height, double thresh);
public:
T ** wavcoeffs;
template
wavelet_level(E * src, int level, 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), m_h2(h), m_pad(padding), wavcoeffs(NULL), lvl(level), skip(1<
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset, int skip);
template
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset, int skip);
};
//////////////////////////////////////////////////////////////////////////////
template
T ** 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 wavelet_level::destroy(T ** subbands)
{
if(subbands)
{
delete[] subbands[0];
delete[] subbands;
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template template
void wavelet_level::loadbuffer(E * src, E * dst, int pitch, int srclen)
{
E * tmp = dst + m_pad;
memset(dst, 0, (MAX(m_w2,m_h2))*sizeof(E));
/*int cosetlen = (srclen+1)/skip;
//create buffer with 'skip' rows and 'cosetlen' columns from src data
//'skip' is the spacing of taps on the wavelet filter to be applied to src rows/columns
//therefore there are 'skip' cosets of the row/column data, each of length 'cosetlen'
//'pitch' is 1 for rows, W for columns
for (size_t i = 0, j = 0; i
void wavelet_level::AnalysisFilter (T * srcbuffer, 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 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,
* 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.
*
*/
//input data is 'skip' rows and cosetlen=srclen/skip columns (which includes padding at either and)
/*int cosetlen = srclen/skip;
for (size_t coset=0; cosettaps && iskip*taps && i
void 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.
*
*/
// load into buffer
/*
int srclen=(dstlen+(dstlen%skip)+2*m_pad); //length of row/col in src (coarser level)
int cosetlen = srclen/skip; //length of coset (skip is spacing of taps in filter)
for (size_t i=0, j=0; itaps && i<(cosetlen-taps)) {//bulk
for (int j=0, l=-shift; jskip*taps && i<(srclen-skip*taps)) {//bulk
for (int j=0, l=-skip*shift; j65535.0f) {
float xxx=tot;
float yyy=1.0f;
}
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template template
void wavelet_level::decompose_level(E *src, float *filterV, float *filterH, int taps, int offset, int skip) {
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
T *buffer = new T[MAX(m_w2,m_h2)];
/* filter along columns */
for (int j=0; j template
void wavelet_level::reconstruct_level(E *dst, float *filterV, float *filterH, int taps, int offset, int skip) {
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
int buflen = MAX(m_w2,m_h2);
float *bufferLo = new float[buflen];
float *bufferHi = new float[buflen];
/* filter along rows */
for (int i=0; i
void wavelet_level::imp_nr (T* src, int width, int height, double thresh) {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// impulse noise removal
// local variables
float hpfabs, hfnbrave;
const float eps = 0.01;
// buffer for the lowpass image
float * lpf = new float[width*height];
// buffer for the highpass image
float * impish = new float[width*height];
//The cleaning algorithm starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// modified bilateral filter for lowpass image, omitting input pixel; or Gaussian blur
/*
static float eps = 1.0;
float wtdsum[3], dirwt, norm;
int i1, j1;
AlignedBuffer* buffer = new AlignedBuffer (MAX(width,height));
gaussHorizontal (src, lpf, buffer, width, height, MAX(2.0,thresh-1.0), false);
gaussVertical (lpf, lpf, buffer, width, height, MAX(2.0,thresh-1.0), false);
delete buffer;
*/
boxblur(src, lpf, 2, 2, width, height);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
float impthr = MAX(1.0,5.5-thresh);
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
hpfabs = fabs(src[i*width+j]-lpf[i*width+j]);
//block average of high pass data
for (int i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (int j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
hfnbrave += fabs(src[i1*width+j1]-lpf[i1*width+j1]);
}
hfnbrave = (hfnbrave-hpfabs)/24;
hpfabs>(hfnbrave*impthr) ? impish[i*width+j]=1 : impish[i*width+j]=0;
}//now impulsive values have been identified
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
if (!impish[i*width+j]) continue;
float norm=0.0;
float wtdsum=0.0;
for (int i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (int j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
if (i1==i && j1==j) continue;
if (impish[i1*width+j1]) continue;
float dirwt = 1/(SQR(src[i1*width+j1]-src[i*width+j])+eps);//use more sophisticated rangefn???
wtdsum += dirwt*src[i1*width+j1];
norm += dirwt;
}
//wtdsum /= norm;
if (norm) {
src[i*width+j]=wtdsum/norm;//low pass filter
}
}//now impulsive values have been corrected
delete [] lpf;
delete [] impish;
}
};
#endif