/* * 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 * 2014 Ingo Weyrich */ #ifndef CPLX_WAVELET_LEVEL_H_INCLUDED #define CPLX_WAVELET_LEVEL_H_INCLUDED #include #include "rt_math.h" #include "opthelper.h" namespace rtengine { template class wavelet_level { // 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 int 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 void AnalysisFilterHaarVertical (T * srcbuffer, T * dstLo, T * dstHi, int pitch, int srclen, int row); void AnalysisFilterHaarHorizontal (T * srcbuffer, T * dstLo, T * dstHi, int srclen, int row); void SynthesisFilterHaarHorizontal (T * srcLo, T * srcHi, T * dst, int dstlen); void SynthesisFilterHaarVertical (T * srcLo, T * srcHi, T * dst, int pitch, int dstlen); void AnalysisFilterSubsampHorizontal (T * srcbuffer, T * dstLo, T * dstHi, float *filterLo, float *filterHi, int taps, int offset, int pitch, int srclen, int m_w2, int row); void AnalysisFilterSubsampVertical (T * srcbuffer, T * dstLo, T * dstHi, float *filterLo, float *filterHi, int taps, int offset, int pitch, int srclen, int row); void SynthesisFilterSubsampHorizontal (T * srcLo, T * srcHi, T * dst, float *filterLo, float *filterHi, int taps, int offset, int dstlen); void SynthesisFilterSubsampVertical (T * srcLo, T * srcHi, T * dst, float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen); public: T ** wavcoeffs; // full size size_t m_w, m_h; // size of low frequency part size_t m_w2, m_h2; template wavelet_level(E * src, E * dst, 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, dst, 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, E *dst, float *filterV, float *filterH, int len, int offset); template void reconstruct_level(E* tmpLo, E* tmpHi, E *src, E *dst, float *filterV, float *filterH, int taps, int offset); }; template T ** wavelet_level::create(size_t n) { T * data = new T[3*n]; T ** subbands = new T*[4]; for(size_t j = 1; j < 4; j++) { subbands[j] = data + n * (j-1); } return subbands; } template void wavelet_level::destroy(T ** subbands) { if(subbands) { delete[] subbands[1]; delete[] subbands; } } template void wavelet_level::AnalysisFilterHaarHorizontal (T * RESTRICT srcbuffer, T * RESTRICT dstLo, T * RESTRICT dstHi, int srclen, int row) { /* Basic convolution code * Applies a Haar filter */ for(int i = 0; i < (srclen - skip); i++) { dstLo[row*srclen+i] = (srcbuffer[i] + srcbuffer[i+skip]); dstHi[row*srclen+i] = (srcbuffer[i] - srcbuffer[i+skip]); } for(size_t i = max(srclen-skip,skip); i < (srclen); i++) { dstLo[row*srclen+i] = (srcbuffer[i] + srcbuffer[i-skip]); dstHi[row*srclen+i] = (srcbuffer[i] - srcbuffer[i-skip]); } } template void wavelet_level::AnalysisFilterHaarVertical (T * RESTRICT srcbuffer, T * RESTRICT dstLo, T * RESTRICT dstHi, int pitch, int srclen, int row) { /* Basic convolution code * Applies a Haar filter */ if(row < (srclen - skip)) { for(int j=0;j=max(srclen-skip,skip)) { for(int j=0;j void wavelet_level::SynthesisFilterHaarHorizontal (T * RESTRICT srcLo, T * RESTRICT srcHi, T * RESTRICT dst, int dstlen) { /* Basic convolution code * Applies a Haar filter * */ for (int k=0; k void wavelet_level::SynthesisFilterHaarVertical (T * RESTRICT srcLo, T * RESTRICT srcHi, T * RESTRICT dst, int pitch, int dstlen) { /* Basic convolution code * Applies a Haar filter * */ for(size_t i = (m_pad); i < (m_pad+skip); i++) { for(int j=0;j void wavelet_level::AnalysisFilterSubsampHorizontal (T * RESTRICT srcbuffer, T * RESTRICT dstLo, T * RESTRICT dstHi, float * RESTRICT filterLo, float *filterHi, int taps, int offset, int pitch, int srclen, int m_w2, int row) { /* 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.f, hi = 0.f; if (LIKELY(i>skip*taps && i void wavelet_level::AnalysisFilterSubsampVertical (T * RESTRICT srcbuffer, T * RESTRICT dstLo, T * RESTRICT dstHi, float * RESTRICT filterLo, float * RESTRICT filterHi, int taps, int offset, int pitch, int srclen, int row) { /* 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 if (LIKELY(row>skip*taps && row void wavelet_level::SynthesisFilterSubsampHorizontal (T * RESTRICT srcLo, T * RESTRICT srcHi, T * RESTRICT dst, float * RESTRICT filterLo, float * RESTRICT filterHi, int taps, int offset, 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) int shift = skip*(taps-offset-1);//align filter with data for (int k=0; kskip*taps && i<(srclen-skip*taps))) {//bulk for (int j=begin, l=0; j void wavelet_level::SynthesisFilterSubsampVertical (T * RESTRICT srcLo, T * RESTRICT srcHi, T * RESTRICT dst, float * RESTRICT filterLo, float * RESTRICT 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) int shift=skip*(taps-offset-1);//align filter with data for(size_t i = m_pad; i < (dstlen+m_pad); i++) { int i_src = (i+shift)/2; int begin = (i+shift)%2; //TODO: this is correct only if skip=1; otherwise, want to work with cosets of length 'skip' if (LIKELY(i>skip*taps && i<(srclen-skip*taps))) {//bulk for (int k=0; k template void wavelet_level::decompose_level(E *src, E *dst, float *filterV, float *filterH, int taps, int offset) { T tmpLo[m_w] ALIGNED64; T tmpHi[m_w] ALIGNED64; /* filter along rows and columns */ if(subsamp_out) { for(int row=0;row template void wavelet_level::reconstruct_level(E* tmpLo, E* tmpHi, E * src, E *dst, float *filterV, float *filterH, int taps, int offset) { /* filter along rows and columns */ if (subsamp_out) { SynthesisFilterSubsampHorizontal (src, wavcoeffs[1], tmpLo, filterH, filterH+taps, taps, offset, m_w/*dstlen*/); SynthesisFilterSubsampHorizontal (wavcoeffs[2], wavcoeffs[3], tmpHi, filterH, filterH+taps, taps, offset, m_w/*dstlen*/); SynthesisFilterSubsampVertical (tmpLo, tmpHi, dst, filterV, filterV+taps, taps, offset, m_w/*pitch*/, m_h/*dstlen*/); } else { SynthesisFilterHaarHorizontal (src, wavcoeffs[1], tmpLo, m_w); SynthesisFilterHaarHorizontal (wavcoeffs[2], wavcoeffs[3], tmpHi, m_w); SynthesisFilterHaarVertical (tmpLo, tmpHi, dst, m_w, m_h); } } }; #endif