/*
* 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 widthITheightOUT ANY widthARRANTY; without even the implied warranty of
* MERCheightANTABILITY 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 Emil Martinec
*
*/
#define SQR(x) ((x)*(x))
#include
#include
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// modified version of Gabor's implementation of bilateral filtering, without input pixel
#define NBRWT(a,b) (src[i - a][j - b] * ec[src[i - a][j - b]-src[i][j]+0x10000])
#define NORM(a,b) (ec[src[i - a][j - b]-src[i][j]+0x10000])
//ec[i] = (int)(exp(-(double)(i-0x10000)*(double)(i-0x10000) / (2.0*rangewidth*rangewidth))*scale); \
#define RB_BEGIN(a,b) double scale = (a); \
int* ec = new int [0x20000]; \
for (int i=0; i<0x20000; i++) \
ec[i] = (int)(1 + 1024/(abs(i-0x10000) + 1)); \
int rstart = b; \
int rend = H-b; \
int cstart = b; \
int cend = W-b;
#define RB_END(b) buffer[i][j] = v; }} delete [] ec; \
for (int i=0; i=rend || j>=cend) \
dst[i][j] = src[i][j]; \
else \
dst[i][j] = buffer[i][j];
#define RB_OPER5 for (int i=rstart; i void rangeblur (T** src, T** dst, T** buffer, int W, int H, double rangewidth, bool multiThread) {
RB_BEGIN(753,2)
#pragma omp parallel for if (multiThread)
RB_OPER5
RB_END(2)
}
template void impulse_nr (T** src, T** dst, int width, int height, double thresh) {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// impulse noise removal
// local variables
float hpfabs, hfnbrave;
// buffer for the lowpass image
unsigned short ** lpf = new unsigned short *[height];
for (int i=0; i (src, lpf, impish /*used as buffer here*/, width, height, thresh, false);
AlignedBuffer* buffer = new AlignedBuffer (MAX(width,height));
gaussHorizontal (src, lpf, buffer, width, height, 2.0, false /*multiThread*/);
gaussVertical (lpf, lpf, buffer, width, height, 2.0, false);
delete buffer;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
hpfabs = fabs(src[i][j]-lpf[i][j]);
//block average of high pass data
for (i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
hfnbrave += fabs(src[i1][j1]-lpf[i1][j1]);
}
hfnbrave = (hfnbrave-hpfabs)/24;
hpfabs>(hfnbrave*(5.5-thresh)) ? impish[i][j]=1 : impish[i][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][j]) continue;
norm=0.0;
wtdsum=0.0;
for (i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
if (i1==i && j1==j) continue;
if (impish[i1][j1]) continue;
dirwt = 1/(SQR(src[i1][j1]-src[i][j])+eps);//use more sophisticated rangefn???
wtdsum += dirwt*src[i1][j1];
norm += dirwt;
}
//wtdsum /= norm;
if (norm) {
src[i][j]=wtdsum/norm;//low pass filter
}
}//now impulsive values have been corrected
for (int i=0; i