/* * 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)) #define MIN(a,b) ((a) > (b) ? (b) : (a)) ////////////////////////////////////////////////////////////////////////////// template 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 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 * 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 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 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 ** 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 cplx_wavelet_level::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 void cplx_wavelet_level::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 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. * */ // calculate coefficients for(int i = 0; i < (srclen); i+=2) { float lo=0,hi=0; if (i>taps && 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. * */ // calculate coefficients int srclen=(dstlen+1+2*m_pad)/2; for (int i=0; itaps && i<(srclen-taps)) {//bulk for (int j=begin, l=0; j template void cplx_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]; /* 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; T *tmpLo = new T[m_w*m_h2]; T *tmpHi = new T[m_w*m_h2]; 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