1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2005-03-27
7  * Description : a digiKam image tool for fixing dots produced by
8  *               hot/stuck/dead pixels from a CCD.
9  *
10  * Copyright (C) 2005-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  * Copyright (C) 2005-2006 by Unai Garro <ugarro at users dot sourceforge dot net>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #include "hotpixelstool.h"
27 
28 // Qt includes
29 
30 #include <QGridLayout>
31 #include <QIcon>
32 #include <QApplication>
33 #include <QPointer>
34 
35 // KDE includes
36 
37 #include <klocalizedstring.h>
38 #include <ksharedconfig.h>
39 #include <kconfiggroup.h>
40 
41 // Local includes
42 
43 #include "dcombobox.h"
44 #include "daboutdata.h"
45 #include "dimg.h"
46 #include "dimgfiltermanager.h"
47 #include "editortooliface.h"
48 #include "editortoolsettings.h"
49 #include "hotpixelfixer.h"
50 #include "hotpixelsettings.h"
51 #include "imageiface.h"
52 #include "imageregionwidget.h"
53 
54 namespace DigikamEditorHotPixelsToolPlugin
55 {
56 
57 class Q_DECL_HIDDEN HotPixelsTool::Private
58 {
59 public:
60 
Private()61     explicit Private()
62       : previewWidget(nullptr),
63         gboxSettings (nullptr),
64         hpSettings   (nullptr)
65     {
66     }
67 
68     ImageRegionWidget*   previewWidget;
69     EditorToolSettings*  gboxSettings;
70     HotPixelSettings*    hpSettings;
71 };
72 
73 // --------------------------------------------------------
74 
HotPixelsTool(QObject * const parent)75 HotPixelsTool::HotPixelsTool(QObject* const parent)
76     : EditorToolThreaded(parent),
77       d                 (new Private)
78 {
79     setObjectName(QLatin1String("hotpixels"));
80     setToolName(i18n("Hot Pixels"));
81     setToolIcon(QIcon::fromTheme(QLatin1String("hotpixels")));
82 
83     // -------------------------------------------------------------
84 
85     d->gboxSettings  = new EditorToolSettings(nullptr);
86     d->gboxSettings->setButtons(EditorToolSettings::Default|
87                                 EditorToolSettings::Ok|
88                                 EditorToolSettings::Cancel|
89                                 EditorToolSettings::Try);
90 
91     d->hpSettings    = new HotPixelSettings(d->gboxSettings->plainPage());
92 
93     // -------------------------------------------------------------
94 
95     d->previewWidget = new ImageRegionWidget;
96 
97     setToolSettings(d->gboxSettings);
98     setToolView(d->previewWidget);
99     setPreviewModeMask(PreviewToolBar::AllPreviewModes);
100 
101     // -------------------------------------------------------------
102 
103     connect(d->hpSettings, SIGNAL(signalSettingsChanged()),
104             this, SLOT(slotPreview()));
105 
106     connect(d->hpSettings, SIGNAL(signalHotPixels(QPolygon)),
107             this, SLOT(slotHotPixels(QPolygon)));
108 }
109 
~HotPixelsTool()110 HotPixelsTool::~HotPixelsTool()
111 {
112     delete d;
113 }
114 
registerFilter()115 void HotPixelsTool::registerFilter()
116 {
117     DImgFilterManager::instance()->addGenerator(new BasicDImgFilterGenerator<HotPixelFixer>());
118 }
119 
slotResetSettings()120 void HotPixelsTool::slotResetSettings()
121 {
122     d->hpSettings->resetToDefault();
123 }
124 
readSettings()125 void HotPixelsTool::readSettings()
126 {
127     KConfigGroup group = KSharedConfig::openConfig()->group(d->hpSettings->configGroupName());
128     d->hpSettings->readSettings(group);
129 }
130 
writeSettings()131 void HotPixelsTool::writeSettings()
132 {
133     KConfigGroup group = KSharedConfig::openConfig()->group(d->hpSettings->configGroupName());
134     d->hpSettings->writeSettings(group);
135 }
136 
preparePreview()137 void HotPixelsTool::preparePreview()
138 {
139     DImg image            = d->previewWidget->getOriginalRegionImage();
140     HotPixelContainer prm = d->hpSettings->settings();
141 
142     QList<HotPixelProps> hotPixelsRegion;
143     QRect area            = d->previewWidget->getOriginalImageRegionToRender();
144 
145     for (QList<HotPixelProps>::const_iterator it = prm.hotPixelsList.constBegin() ;
146          it != prm.hotPixelsList.constEnd() ; ++it)
147     {
148         HotPixelProps hp = (*it);
149 
150         if (area.contains( hp.rect ))
151         {
152             hp.rect.moveTopLeft(QPoint( hp.rect.x()-area.x(), hp.rect.y()-area.y() ));
153             hotPixelsRegion.append(hp);
154         }
155     }
156 
157     prm.hotPixelsList = hotPixelsRegion;
158 
159     setFilter(dynamic_cast<DImgThreadedFilter*>(new HotPixelFixer(&image, this, prm)));
160 }
161 
prepareFinal()162 void HotPixelsTool::prepareFinal()
163 {
164     HotPixelContainer prm = d->hpSettings->settings();
165 
166     ImageIface iface;
167     setFilter(dynamic_cast<DImgThreadedFilter*>(new HotPixelFixer(iface.original(), this, prm)));
168 }
169 
setPreviewImage()170 void HotPixelsTool::setPreviewImage()
171 {
172     d->previewWidget->setPreviewImage(filter()->getTargetImage());
173 }
174 
setFinalImage()175 void HotPixelsTool::setFinalImage()
176 {
177     ImageIface iface;
178     iface.setOriginal(i18n("Hot Pixels Correction"), filter()->filterAction(), filter()->getTargetImage());
179 }
180 
slotHotPixels(const QPolygon & pointList)181 void HotPixelsTool::slotHotPixels(const QPolygon& pointList)
182 {
183     d->previewWidget->setHighLightPoints(pointList);
184     slotPreview();
185 }
186 
187 } // namespace DigikamEditorHotPixelsToolPlugin
188