File indexing completed on 2025-01-05 03:51:42

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-08-24
0007  * Description : a tool to reduce CCD noise.
0008  *
0009  * SPDX-FileCopyrightText: 2004-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "noisereductiontool.h"
0016 
0017 // Qt includes
0018 
0019 #include <QString>
0020 #include <QIcon>
0021 #include <QApplication>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 #include <ksharedconfig.h>
0027 #include <kconfiggroup.h>
0028 
0029 // Local includes
0030 
0031 #include "dimg.h"
0032 #include "nrestimate.h"
0033 #include "nrsettings.h"
0034 #include "nrfilter.h"
0035 #include "editortoolsettings.h"
0036 #include "imageiface.h"
0037 #include "imageregionwidget.h"
0038 
0039 namespace DigikamEditorNoiseReductionToolPlugin
0040 {
0041 
0042 class Q_DECL_HIDDEN NoiseReductionTool::Private
0043 {
0044 public:
0045 
0046     explicit Private()
0047       : configGroupName(QLatin1String("noisereduction Tool")),
0048         nrSettings     (nullptr),
0049         previewWidget  (nullptr),
0050         gboxSettings  (nullptr)
0051     {
0052     }
0053 
0054     const QString       configGroupName;
0055 
0056     NRSettings*         nrSettings;
0057     ImageRegionWidget*  previewWidget;
0058     EditorToolSettings* gboxSettings;
0059 };
0060 
0061 NoiseReductionTool::NoiseReductionTool(QObject* const parent)
0062     : EditorToolThreaded(parent),
0063       d                 (new Private)
0064 {
0065     setObjectName(QLatin1String("noisereduction"));
0066     setToolName(i18n("Noise Reduction"));
0067     setToolIcon(QIcon::fromTheme(QLatin1String("noisereduction")));
0068 
0069     // -------------------------------------------------------------
0070 
0071     d->gboxSettings = new EditorToolSettings(nullptr);
0072     d->gboxSettings->setButtons(EditorToolSettings::Default|
0073                                 EditorToolSettings::Ok|
0074                                 EditorToolSettings::Cancel|
0075                                 EditorToolSettings::Load|
0076                                 EditorToolSettings::SaveAs|
0077                                 EditorToolSettings::Try);
0078 
0079     d->nrSettings    = new NRSettings(d->gboxSettings->plainPage());
0080     d->previewWidget = new ImageRegionWidget;
0081 
0082     setToolSettings(d->gboxSettings);
0083     setToolView(d->previewWidget);
0084     setPreviewModeMask(PreviewToolBar::AllPreviewModes);
0085 
0086     connect(d->nrSettings, SIGNAL(signalEstimateNoise()),
0087             this, SLOT(slotEstimateNoise()));
0088 }
0089 
0090 NoiseReductionTool::~NoiseReductionTool()
0091 {
0092     delete d;
0093 }
0094 
0095 void NoiseReductionTool::readSettings()
0096 {
0097     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0098     KConfigGroup group        = config->group(d->configGroupName);
0099     d->nrSettings->readSettings(group);
0100 }
0101 
0102 void NoiseReductionTool::writeSettings()
0103 {
0104     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0105     KConfigGroup group        = config->group(d->configGroupName);
0106 
0107     d->nrSettings->writeSettings(group);
0108     group.sync();
0109 }
0110 
0111 void NoiseReductionTool::slotResetSettings()
0112 {
0113     d->nrSettings->resetToDefault();
0114 }
0115 
0116 void NoiseReductionTool::preparePreview()
0117 {
0118     DImg image      = d->previewWidget->getOriginalRegionImage();
0119     NRContainer prm = d->nrSettings->settings();
0120 
0121     setFilter(new NRFilter(&image, this, prm));
0122 }
0123 
0124 void NoiseReductionTool::prepareFinal()
0125 {
0126     NRContainer prm = d->nrSettings->settings();
0127 
0128     ImageIface iface;
0129     setFilter(new NRFilter(iface.original(), this, prm));
0130 }
0131 
0132 void NoiseReductionTool::setPreviewImage()
0133 {
0134     d->previewWidget->setPreviewImage(filter()->getTargetImage());
0135 }
0136 
0137 void NoiseReductionTool::setFinalImage()
0138 {
0139     ImageIface iface;
0140     iface.setOriginal(i18n("Noise Reduction"), filter()->filterAction(), filter()->getTargetImage());
0141 }
0142 
0143 void NoiseReductionTool::slotLoadSettings()
0144 {
0145     d->nrSettings->loadSettings();
0146 }
0147 
0148 void NoiseReductionTool::slotSaveAsSettings()
0149 {
0150     d->nrSettings->saveAsSettings();
0151 }
0152 
0153 void NoiseReductionTool::slotEstimateNoise()
0154 {
0155     ImageIface iface;
0156     setAnalyser(new NREstimate(iface.original(), this));
0157 }
0158 
0159 void NoiseReductionTool::analyserCompleted()
0160 {
0161     NREstimate* const tool = dynamic_cast<NREstimate*>(analyser());
0162     if (!tool) return;
0163 
0164     d->nrSettings->setSettings(tool->settings());
0165     qApp->restoreOverrideCursor();
0166     slotPreview();
0167 }
0168 
0169 } // namespace DigikamEditorNoiseReductionToolPlugin
0170 
0171 #include "moc_noisereductiontool.cpp"