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        : 2009-08-09
0007  * Description : a tool to enhance image with local contrasts (as human eye does).
0008  *
0009  * SPDX-FileCopyrightText: 2009      by Julien Pontabry <julien dot pontabry at gmail dot com>
0010  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "localcontrasttool.h"
0017 
0018 // Qt includes
0019 
0020 #include <QCheckBox>
0021 #include <QFile>
0022 #include <QGridLayout>
0023 #include <QImage>
0024 #include <QLabel>
0025 #include <QString>
0026 #include <QTextStream>
0027 #include <QIcon>
0028 
0029 // KDE includes
0030 
0031 #include <klocalizedstring.h>
0032 #include <ksharedconfig.h>
0033 #include <kconfiggroup.h>
0034 
0035 // Local includes
0036 
0037 #include "dimg.h"
0038 #include "editortoolsettings.h"
0039 #include "imageiface.h"
0040 #include "histogramwidget.h"
0041 #include "imageregionwidget.h"
0042 #include "localcontrastfilter.h"
0043 #include "localcontrastsettings.h"
0044 #include "localcontrastcontainer.h"
0045 
0046 namespace DigikamEditorLocalContrastToolPlugin
0047 {
0048 
0049 class Q_DECL_HIDDEN LocalContrastTool::Private
0050 {
0051 public:
0052 
0053     explicit Private()
0054       : settingsView (nullptr),
0055         previewWidget(nullptr),
0056         gboxSettings (nullptr)
0057     {
0058     }
0059 
0060     static const QString   configGroupName;
0061     static const QString   configHistogramChannelEntry;
0062     static const QString   configHistogramScaleEntry;
0063 
0064     LocalContrastSettings* settingsView;
0065     ImageRegionWidget*     previewWidget;
0066     EditorToolSettings*    gboxSettings;
0067 };
0068 
0069 const QString LocalContrastTool::Private::configGroupName(QLatin1String("localcontrast Tool"));
0070 const QString LocalContrastTool::Private::configHistogramChannelEntry(QLatin1String("Histogram Channel"));
0071 const QString LocalContrastTool::Private::configHistogramScaleEntry(QLatin1String("Histogram Scale"));
0072 
0073 // --------------------------------------------------------
0074 
0075 LocalContrastTool::LocalContrastTool(QObject* const parent)
0076     : EditorToolThreaded(parent),
0077       d                 (new Private)
0078 {
0079     setObjectName(QLatin1String("localcontrast"));
0080 
0081     d->previewWidget = new ImageRegionWidget;
0082     setToolView(d->previewWidget);
0083     setPreviewModeMask(PreviewToolBar::AllPreviewModes);
0084 
0085     // -------------------------------------------------------------
0086 
0087     d->gboxSettings = new EditorToolSettings(nullptr);
0088     d->gboxSettings->setTools(EditorToolSettings::Histogram);
0089     d->gboxSettings->setHistogramType(LRGBC);
0090     d->gboxSettings->setButtons(EditorToolSettings::Default|
0091                                 EditorToolSettings::Ok|
0092                                 EditorToolSettings::Cancel|
0093                                 EditorToolSettings::Load|
0094                                 EditorToolSettings::SaveAs|
0095                                 EditorToolSettings::Try);
0096 
0097     // -------------------------------------------------------------
0098 
0099     d->settingsView = new LocalContrastSettings(d->gboxSettings->plainPage());
0100     setToolSettings(d->gboxSettings);
0101 }
0102 
0103 LocalContrastTool::~LocalContrastTool()
0104 {
0105     delete d;
0106 }
0107 
0108 void LocalContrastTool::readSettings()
0109 {
0110     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0111     KConfigGroup group        = config->group(d->configGroupName);
0112 
0113     d->gboxSettings->histogramBox()->setChannel((ChannelType)group.readEntry(d->configHistogramChannelEntry, (int)LuminosityChannel));
0114     d->gboxSettings->histogramBox()->setScale((HistogramScale)group.readEntry(d->configHistogramScaleEntry,  (int)LogScaleHistogram));
0115     d->settingsView->readSettings(group);
0116 }
0117 
0118 void LocalContrastTool::writeSettings()
0119 {
0120     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0121     KConfigGroup group        = config->group(d->configGroupName);
0122 
0123     group.writeEntry(d->configHistogramChannelEntry, (int)d->gboxSettings->histogramBox()->channel());
0124     group.writeEntry(d->configHistogramScaleEntry,   (int)d->gboxSettings->histogramBox()->scale());
0125     d->settingsView->writeSettings(group);
0126     group.sync();
0127 }
0128 
0129 void LocalContrastTool::slotResetSettings()
0130 {
0131     d->settingsView->resetToDefault();
0132 }
0133 
0134 void LocalContrastTool::preparePreview()
0135 {
0136     // See bug #235601 : we cannot use downscaled image to render preview. It will differs than final rendering.
0137     DImg image = d->previewWidget->getOriginalRegionImage(false);
0138     setFilter(new LocalContrastFilter(&image, this, d->settingsView->settings()));
0139 }
0140 
0141 void LocalContrastTool::prepareFinal()
0142 {
0143     ImageIface iface;
0144     setFilter(new LocalContrastFilter(iface.original(), this, d->settingsView->settings()));
0145 }
0146 
0147 void LocalContrastTool::setPreviewImage()
0148 {
0149     DImg preview = filter()->getTargetImage();
0150     d->previewWidget->setPreviewImage(preview);
0151 
0152     // Update histogram.
0153 
0154     d->gboxSettings->histogramBox()->histogram()->updateData(preview.copy(), DImg(), false);
0155 }
0156 
0157 void LocalContrastTool::setFinalImage()
0158 {
0159     ImageIface iface;
0160     iface.setOriginal(i18n("Local Contrast"), filter()->filterAction(), filter()->getTargetImage());
0161 }
0162 
0163 void LocalContrastTool::slotLoadSettings()
0164 {
0165     d->settingsView->loadSettings();
0166     d->gboxSettings->histogramBox()->histogram()->reset();
0167     slotPreview();
0168 }
0169 
0170 void LocalContrastTool::slotSaveAsSettings()
0171 {
0172     d->settingsView->saveAsSettings();
0173 }
0174 
0175 } // namespace DigikamEditorLocalContrastToolPlugin
0176 
0177 #include "moc_localcontrasttool.cpp"