1 /*******************************************************************************
2 **
3 ** Photivo
4 **
5 ** Copyright (C) 2015 Bernd Schoeler <brjohn@brother-john.net>
6 **
7 ** This file is part of Photivo.
8 **
9 ** Photivo is free software: you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License version 3
11 ** as published by the Free Software Foundation.
12 **
13 ** Photivo is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with Photivo.  If not, see <http://www.gnu.org/licenses/>.
20 **
21 *******************************************************************************/
22 
23 #include "ptFilter_ColorDenoise.h"
24 #include "ptCfgItem.h"
25 #include "../ptImage.h"
26 
27 // TODO: Needed for access to m_ScaleFactor. Find a way to avoid when modernising the processor.
28 #include "../ptProcessor.h"
29 extern ptProcessor* TheProcessor;
30 
31 //------------------------------------------------------------------------------
32 
33 const QString CColorDenoiseId = "ColorDenoise";
34 
35 const QString CAStrength = "AStrength";
36 const QString CAScale    = "AScale";
37 const QString CBStrength = "BStrength";
38 const QString CBScale    = "BScale";
39 
40 //------------------------------------------------------------------------------
41 
ptFilter_ColorDenoise()42 ptFilter_ColorDenoise::ptFilter_ColorDenoise():
43   ptFilterBase()
44 {
45   FIsSlow = true;
46   this->internalInit();
47 }
48 
49 //------------------------------------------------------------------------------
50 
createColorDenoise()51 ptFilterBase *ptFilter_ColorDenoise::createColorDenoise() {
52   auto hInstance         = new ptFilter_ColorDenoise;
53   hInstance->FFilterName = CColorDenoiseId;
54   hInstance->FCaption    = tr("Color denoising");
55   return hInstance;
56 }
57 
58 //------------------------------------------------------------------------------
59 
doDefineControls()60 void ptFilter_ColorDenoise::doDefineControls() {
61   FConfig.initStores(TCfgItemList()
62     //            Id                       Type                      Default     Min           Max           Step        Decimals, commonConnect, storeable, caption, tooltip
63     << ptCfgItem({CAStrength,              ptCfgItem::Slider,        0.0,        0.0,          3.0,          0.02,       2,        true, true, tr("A channel strength"), tr("")})
64     << ptCfgItem({CAScale,                 ptCfgItem::Slider,        8.0,        4.0,         50.0,          4.0,        1,        true, true, tr("A channel scale"),    tr("")})
65     << ptCfgItem({CBStrength,              ptCfgItem::Slider,        0.0,        0.0,          3.0,          0.02,       2,        true, true, tr("B channel strength"), tr("")})
66     << ptCfgItem({CBScale,                 ptCfgItem::Slider,        8.0,        4.0,         50.0,          4.0,        1,        true, true, tr("B channel scale"),    tr("")})
67   );
68 }
69 
70 //------------------------------------------------------------------------------
71 
doCheckHasActiveCfg()72 bool ptFilter_ColorDenoise::doCheckHasActiveCfg() {
73   return
74       !qFuzzyIsNull(FConfig.value(CAStrength).toFloat()) ||
75       !qFuzzyIsNull(FConfig.value(CBStrength).toFloat());
76 }
77 
78 //------------------------------------------------------------------------------
79 
doRunFilter(ptImage * AImage)80 void ptFilter_ColorDenoise::doRunFilter(ptImage *AImage) {
81   AImage->toLab();
82 
83   double AStrength = FConfig.value(CAStrength).toFloat();
84 
85   if (!qFuzzyIsNull(AStrength)) {
86     AImage->fastBilateralChannel(
87         FConfig.value(CAScale).toFloat() * TheProcessor->m_ScaleFactor,
88         AStrength / 10.0f,
89         2,
90         ChMask_a);
91   }
92 
93   double BStrength = FConfig.value(CBStrength).toFloat();
94 
95   if (!qFuzzyIsNull(BStrength)) {
96     AImage->fastBilateralChannel(
97         FConfig.value(CBScale).toFloat() * TheProcessor->m_ScaleFactor,
98         BStrength,
99         2,
100         ChMask_b);
101   }
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 RegisterHelper ColorDenoiseRegister(&ptFilter_ColorDenoise::createColorDenoise, CColorDenoiseId);
107 
108 //------------------------------------------------------------------------------
109