1 /*
2  * * Copyright (C) 2006-2011 Anders Brander <anders@brander.dk>,
3  * * Anders Kvist <akv@lnxbx.dk> and Klaus Post <klauspost@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 #include "fftdenoiseryuv.h"
21 #include <math.h>
22 
23 namespace RawStudio {
24 namespace FFTFilter {
25 
FFTDenoiserYUV(void)26 FFTDenoiserYUV::FFTDenoiserYUV(void)
27 {
28 }
29 
~FFTDenoiserYUV(void)30 FFTDenoiserYUV::~FFTDenoiserYUV(void)
31 {
32 }
33 
denoiseImage(RS_IMAGE16 * image)34 void FFTDenoiserYUV::denoiseImage( RS_IMAGE16* image )
35 {
36   FloatPlanarImage img;
37   img.bw = FFT_BLOCK_SIZE;
38   img.bh = FFT_BLOCK_SIZE;
39   img.ox = FFT_BLOCK_OVERLAP;
40   img.oy = FFT_BLOCK_OVERLAP;
41 
42   img.redCorrection = redCorrection;
43   img.blueCorrection = blueCorrection;
44 
45   if ((image->w < FFT_BLOCK_SIZE) || (image->h < FFT_BLOCK_SIZE))
46      return;   // Image too small to denoise
47 
48   if (image->channels != 3 || image->filters!=0)
49      return;   // No conversion possible with this image
50 
51   waitForJobs(img.getUnpackInterleavedYUVJobs(image));
52 
53   if (abort) return;
54 
55   img.mirrorEdges();
56   if (abort) return;
57 
58   FFTWindow window(img.bw,img.bh);
59   window.createHalfCosineWindow(img.ox, img.oy);
60 
61   ComplexFilter *filter = new ComplexWienerFilterDeGrid(img.bw, img.bh, beta, sigmaLuma, 1.0, plan_forward, &window);
62   filter->setSharpen(sharpen, sharpenMinSigma, sharpenMaxSigma, sharpenCutoff);
63   img.setFilter(0,filter,&window);
64 
65   filter = new ComplexWienerFilterDeGrid(img.bw, img.bh, betaChroma, sigmaChroma, 1.0, plan_forward, &window);
66   filter->setSharpen(sharpenChroma, sharpenMinSigmaChroma, sharpenMaxSigmaChroma, sharpenCutoffChroma);
67   img.setFilter(1,filter,&window);
68 
69   filter = new ComplexWienerFilterDeGrid(img.bw, img.bh, betaChroma, sigmaChroma, 1.0, plan_forward, &window);
70   filter->setSharpen(sharpenChroma, sharpenMinSigmaChroma, sharpenMaxSigmaChroma, sharpenCutoffChroma);
71   img.setFilter(2,filter,&window);
72 
73   FloatPlanarImage outImg(img);
74 
75   processJobs(img, outImg);
76   if (abort) return;
77 
78   // Convert back
79   waitForJobs(outImg.getPackInterleavedYUVJobs(image));
80 }
81 
82 
setParameters(FFTDenoiseInfo * info)83 void FFTDenoiserYUV::setParameters( FFTDenoiseInfo *info )
84 {
85   FFTDenoiser::setParameters(info);
86   sigmaLuma = info->sigmaLuma*SIGMA_FACTOR;
87   sigmaChroma = info->sigmaChroma*SIGMA_FACTOR;
88   betaChroma = info->betaChroma;
89   sharpen = info->sharpenLuma;
90   sharpenCutoff = info->sharpenCutoffLuma;
91   sharpenMinSigma = info->sharpenMinSigmaLuma*SIGMA_FACTOR;
92   sharpenMaxSigma = info->sharpenMaxSigmaLuma*SIGMA_FACTOR;
93   sharpenChroma = info->sharpenChroma;
94   sharpenCutoffChroma = info->sharpenCutoffChroma*SIGMA_FACTOR;
95   sharpenMinSigmaChroma = info->sharpenMinSigmaChroma*SIGMA_FACTOR;
96   sharpenMaxSigmaChroma = info->sharpenMaxSigmaChroma*SIGMA_FACTOR;
97   redCorrection = info->redCorrection;
98   blueCorrection = info->blueCorrection;
99 }
100 
101 }}// namespace RawStudio::FFTFilter
102