/* * 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 "gauss.h" #include "rt_math.h" namespace rtengine { ////////////////////////////////////////////////////////////////////////////// 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; // whether to subsample the output bool subsamp_out; // spacing of filter taps size_t skip; // 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 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 AnalysisFilterHaar (T * srcbuffer, T * dstLo, T * dstHi, int pitch, int srclen); void SynthesisFilterHaar (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi, int pitch, int dstlen); void AnalysisFilterSubsamp (T * srcbuffer, T * dstLo, T * dstHi, float *filterLo, float *filterHi, int taps, int offset, int pitch, int srclen); void SynthesisFilterSubsamp (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi, float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen); void AnalysisFilterSubsampHaar (T * srcbuffer, T * dstLo, T * dstHi, int pitch, int srclen); void SynthesisFilterSubsampHaar (T * srcLo, T * srcHi, T * dst, 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 subsamp, 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<>level)&1) { if (subsamp) { skip = 1; for (int n=0; n>n)&1); } } m_w2 = (subsamp_out ? ((w+1+2*skip*padding)/2) : (w+2*skip*padding)); m_h2 = (subsamp_out ? ((h+1+2*skip*padding)/2) : (h+2*skip*padding)); m_pad= skip*padding; wavcoeffs = create((m_w2)*(m_h2)); decompose_level(src, filterV, filterH, len, offset); } ~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; } size_t padding() const { return m_pad/skip; } size_t stride() const { return skip; } 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 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)); //create padded buffer from src data 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 'skip' pixels between taps * */ for (size_t i = 0; i < (srclen); i++) { float lo=0,hi=0; if (i>skip*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 filter length 'taps', * aligning the 'offset' element of the filter with * the input pixel, and skipping 'skip' pixels between taps * */ // load into buffer int srclen = (dstlen==m_w ? m_w2 : m_h2);//length of row/col in src (coarser level) for (size_t i=0, j=0; iskip*taps && i<(srclen-skip*taps)) {//bulk for (int j=0, l=-shift; j void wavelet_level::AnalysisFilterHaar (T * srcbuffer, T * dstLo, T * dstHi, int pitch, int srclen) { /* Basic convolution code * Applies a Haar filter * */ for(size_t i = 0; i < (srclen - skip); i++) { dstLo[(pitch*(i))] = 0.5*(srcbuffer[i] + srcbuffer[i+skip]); dstHi[(pitch*(i))] = 0.5*(srcbuffer[i] - srcbuffer[i+skip]); } for(size_t i = (srclen-skip); i < (srclen); i++) { dstLo[(pitch*(i))] = 0.5*(srcbuffer[i] + srcbuffer[i-skip]); dstHi[(pitch*(i))] = 0.5*(srcbuffer[i] - srcbuffer[i-skip]); } } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% template void wavelet_level::SynthesisFilterHaar (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi, int pitch, int dstlen) { /* Basic convolution code * Applies a Haar filter * */ int srclen = (dstlen==m_w ? m_w2 : m_h2);//length of row/col in src (coarser level) for (size_t i=0, j=0; i void wavelet_level::AnalysisFilterSubsamp (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 'skip' pixels between taps * Output is subsampled by two */ // calculate coefficients for(int i = 0; i < (srclen); i+=2) { float lo=0,hi=0; if (i>skip*taps && i void wavelet_level::SynthesisFilterSubsamp (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 filter length 'taps', * aligning the 'offset' element of the filter with * the input pixel, and skipping 'skip' pixels between taps * Output is subsampled by two */ // calculate coefficients int srclen = (dstlen==m_w ? m_w2 : m_h2);//length of row/col in src (coarser level) //fill a buffer with a given row/column of data for (size_t i=0, j=0; iskip*taps && i<(srclen-skip*taps)) {//bulk for (int j=begin, l=0; j void wavelet_level::AnalysisFilterSubsampHaar (T * srcbuffer, T * dstLo, T * dstHi, int pitch, int srclen) { /* Basic convolution code * Applies a Haar filter * Output is subsampled by two */ // calculate coefficients for(size_t i = 0; i < (srclen - skip); i+=2) { dstLo[(pitch*(i/2))] = 0.5*(srcbuffer[i] + srcbuffer[i+skip]); dstHi[(pitch*(i/2))] = 0.5*(srcbuffer[i] - srcbuffer[i+skip]); } for(size_t i = (srclen-skip)-((srclen-skip)&1); i < (srclen); i+=2) { dstLo[(pitch*(i/2))] = 0.5*(srcbuffer[i] + srcbuffer[i-skip]); dstHi[(pitch*(i/2))] = 0.5*(srcbuffer[i] - srcbuffer[i-skip]); } } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% template void wavelet_level::SynthesisFilterSubsampHaar (T * srcLo, T * srcHi, T * dst, int pitch, int dstlen) { /* Basic convolution code * Applies a Haar filter * Input was subsampled by two */ // calculate coefficients //TODO: this code is buggy... for (int n=0; n template void wavelet_level::decompose_level(E *src, float *filterV, float *filterH, int taps, int offset) { 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)+2*m_pad+skip]; /* filter along columns */ //OpenMP here for (int j=0; j template void wavelet_level::reconstruct_level(E *dst, float *filterV, float *filterH, int taps, int offset) { 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 */ //OpenMP here for (int i=0; i