| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2010-09-18 |
| 7 |
* Description : lens auto-fix batch tool. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2010-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 12 |
* |
| 13 |
* ============================================================ */ |
| 14 |
|
| 15 |
#include "lensautofix.h" |
| 16 |
|
| 17 |
// Qt includes |
| 18 |
|
| 19 |
#include <QLabel> |
| 20 |
#include <QGridLayout> |
| 21 |
#include <QWidget> |
| 22 |
#include <QApplication> |
| 23 |
#include <QStyle> |
| 24 |
#include <QScopedPointer> |
| 25 |
|
| 26 |
// KDE includes |
| 27 |
|
| 28 |
#include <klocalizedstring.h> |
| 29 |
|
| 30 |
// Local includes |
| 31 |
|
| 32 |
#include "dlayoutbox.h" |
| 33 |
#include "dimg.h" |
| 34 |
#include "lensfunsettings.h" |
| 35 |
#include "lensfuncameraselector.h" |
| 36 |
#include "lensfuniface.h" |
| 37 |
#include "dexpanderbox.h" |
| 38 |
|
| 39 |
namespace DigikamBqmLensAutoFixPlugin |
| 40 |
{ |
| 41 |
|
| 42 |
class Q_DECL_HIDDEN LensAutoFix::Private |
| 43 |
{ |
| 44 |
public: |
| 45 |
|
| 46 |
Private() = default; |
| 47 |
|
| 48 |
LensFunSettings* settingsView = nullptr; |
| 49 |
LensFunCameraSelector* cameraSelector = nullptr; |
| 50 |
|
| 51 |
bool changeSettings = true; |
| 52 |
}; |
| 53 |
|
| 54 |
LensAutoFix::LensAutoFix(QObject* const parent) |
| 55 |
: BatchTool(QLatin1String("LensAutoFix"), EnhanceTool, parent), |
| 56 |
d (new Private) |
| 57 |
{ |
| 58 |
setToolTitle(i18n("Lens Auto-Correction")); |
| 59 |
setToolDescription(i18n("Fix automatically lens distortions")); |
| 60 |
setToolIconName(QLatin1String("lensautofix")); |
| 61 |
} |
| 62 |
|
| 63 |
LensAutoFix::~LensAutoFix() |
| 64 |
{ |
| 65 |
delete d; |
| 66 |
} |
| 67 |
|
| 68 |
BatchTool* LensAutoFix::clone(QObject* const parent) const |
| 69 |
{ |
| 70 |
return new LensAutoFix(parent); |
| 71 |
} |
| 72 |
|
| 73 |
void LensAutoFix::registerSettingsWidget() |
| 74 |
{ |
| 75 |
const int spacing = layoutSpacing(); |
| 76 |
|
| 77 |
m_settingsWidget = new QWidget; |
| 78 |
QLabel* const note = new QLabel(i18n("<b>Use Metadata</b> option will parse images' information at " |
| 79 |
"queue run-time to find relevant lens features.")); |
| 80 |
note->setWordWrap(true); |
| 81 |
note->setFrameStyle(QFrame::StyledPanel | QFrame::Raised); |
| 82 |
|
| 83 |
d->cameraSelector = new LensFunCameraSelector(); |
| 84 |
DLineWidget* const line = new DLineWidget(Qt::Horizontal); |
| 85 |
d->settingsView = new LensFunSettings(); |
| 86 |
d->cameraSelector->setPassiveMetadataUsage(true); |
| 87 |
d->cameraSelector->setEnabledUseMetadata(true); |
| 88 |
|
| 89 |
QGridLayout* const grid = new QGridLayout(m_settingsWidget); |
| 90 |
grid->addWidget(note, 0, 0, 1, 2); |
| 91 |
grid->addWidget(d->cameraSelector, 1, 0, 1, 2); |
| 92 |
grid->addWidget(line, 2, 0, 1, 2); |
| 93 |
grid->addWidget(d->settingsView, 3, 0, 1, 2); |
| 94 |
grid->setRowStretch(4, 10); |
| 95 |
grid->setContentsMargins(spacing, spacing, spacing, spacing); |
| 96 |
grid->setSpacing(spacing); |
| 97 |
|
| 98 |
connect(d->settingsView, SIGNAL(signalSettingsChanged()), |
| 99 |
this, SLOT(slotSettingsChanged())); |
| 100 |
|
| 101 |
connect(d->cameraSelector, SIGNAL(signalLensSettingsChanged()), |
| 102 |
this, SLOT(slotSettingsChanged())); |
| 103 |
|
| 104 |
BatchTool::registerSettingsWidget(); |
| 105 |
} |
| 106 |
|
| 107 |
BatchToolSettings LensAutoFix::defaultSettings() |
| 108 |
{ |
| 109 |
BatchToolSettings prm; |
| 110 |
|
| 111 |
prm.insert(QLatin1String("UseMetadata"), true); |
| 112 |
prm.insert(QLatin1String("filterCCA"), true); |
| 113 |
prm.insert(QLatin1String("filterVIG"), true); |
| 114 |
prm.insert(QLatin1String("filterDST"), true); |
| 115 |
prm.insert(QLatin1String("filterGEO"), true); |
| 116 |
|
| 117 |
prm.insert(QLatin1String("cropFactor"), -1.0); |
| 118 |
prm.insert(QLatin1String("focalLength"), -1.0); |
| 119 |
prm.insert(QLatin1String("aperture"), -1.0); |
| 120 |
prm.insert(QLatin1String("subjectDistance"), -1.0); |
| 121 |
|
| 122 |
prm.insert(QLatin1String("cameraMake"), QString()); |
| 123 |
prm.insert(QLatin1String("cameraModel"), QString()); |
| 124 |
prm.insert(QLatin1String("lensModel"), QString()); |
| 125 |
|
| 126 |
return prm; |
| 127 |
} |
| 128 |
|
| 129 |
void LensAutoFix::slotAssignSettings2Widget() |
| 130 |
{ |
| 131 |
d->changeSettings = false; |
| 132 |
|
| 133 |
d->cameraSelector->setUseMetadata(settings().value(QLatin1String("UseMetadata")).toBool()); |
| 134 |
LensFunContainer lfPrm; |
| 135 |
|
| 136 |
lfPrm.filterCCA = settings().value(QLatin1String("filterCCA")).toBool(); |
| 137 |
lfPrm.filterVIG = settings().value(QLatin1String("filterVIG")).toBool(); |
| 138 |
lfPrm.filterDST = settings().value(QLatin1String("filterDST")).toBool(); |
| 139 |
lfPrm.filterGEO = settings().value(QLatin1String("filterGEO")).toBool(); |
| 140 |
|
| 141 |
lfPrm.cropFactor = settings().value(QLatin1String("cropFactor")).toDouble(); |
| 142 |
lfPrm.focalLength = settings().value(QLatin1String("focalLength")).toDouble(); |
| 143 |
lfPrm.aperture = settings().value(QLatin1String("aperture")).toDouble(); |
| 144 |
lfPrm.subjectDistance = settings().value(QLatin1String("subjectDistance")).toDouble(); |
| 145 |
|
| 146 |
lfPrm.cameraMake = settings().value(QLatin1String("cameraMake")).toString(); |
| 147 |
lfPrm.cameraModel = settings().value(QLatin1String("cameraModel")).toString(); |
| 148 |
lfPrm.lensModel = settings().value(QLatin1String("lensModel")).toString(); |
| 149 |
|
| 150 |
d->cameraSelector->setSettings(lfPrm); |
| 151 |
d->settingsView->setFilterSettings(lfPrm); |
| 152 |
|
| 153 |
// Update checkbox options about Lens corrections available. |
| 154 |
|
| 155 |
d->settingsView->setEnabledCCA(d->cameraSelector->useMetadata() ? true : d->cameraSelector->supportsCCA()); |
| 156 |
d->settingsView->setEnabledVig(d->cameraSelector->useMetadata() ? true : d->cameraSelector->supportsVig()); |
| 157 |
d->settingsView->setEnabledDist(d->cameraSelector->useMetadata() ? true : d->cameraSelector->supportsDistortion()); |
| 158 |
d->settingsView->setEnabledGeom(d->cameraSelector->useMetadata() ? true : d->cameraSelector->supportsGeometry()); |
| 159 |
|
| 160 |
d->changeSettings = true; |
| 161 |
} |
| 162 |
|
| 163 |
void LensAutoFix::slotSettingsChanged() |
| 164 |
{ |
| 165 |
if (d->changeSettings) |
| 166 |
{ |
| 167 |
BatchToolSettings prm; |
| 168 |
LensFunContainer camSettings = d->cameraSelector->settings(); |
| 169 |
LensFunContainer filterSettings = d->settingsView->settings(); |
| 170 |
|
| 171 |
prm.insert(QLatin1String("UseMetadata"), (bool)d->cameraSelector->useMetadata()); |
| 172 |
|
| 173 |
prm.insert(QLatin1String("filterCCA"), (bool)filterSettings.filterCCA); |
| 174 |
prm.insert(QLatin1String("filterVIG"), (bool)filterSettings.filterVIG); |
| 175 |
prm.insert(QLatin1String("filterDST"), (bool)filterSettings.filterDST); |
| 176 |
prm.insert(QLatin1String("filterGEO"), (bool)filterSettings.filterGEO); |
| 177 |
|
| 178 |
prm.insert(QLatin1String("cropFactor"), (double)camSettings.cropFactor); |
| 179 |
prm.insert(QLatin1String("focalLength"), (double)camSettings.focalLength); |
| 180 |
prm.insert(QLatin1String("aperture"), (double)camSettings.aperture); |
| 181 |
prm.insert(QLatin1String("subjectDistance"), (double)camSettings.subjectDistance); |
| 182 |
|
| 183 |
prm.insert(QLatin1String("cameraMake"), camSettings.cameraMake); |
| 184 |
prm.insert(QLatin1String("cameraModel"), camSettings.cameraModel); |
| 185 |
prm.insert(QLatin1String("lensModel"), camSettings.lensModel); |
| 186 |
|
| 187 |
BatchTool::slotSettingsChanged(prm); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
bool LensAutoFix::toolOperations() |
| 192 |
{ |
| 193 |
if (!loadToDImg()) |
| 194 |
{ |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
LensFunContainer prm; |
| 199 |
prm.filterCCA = settings().value(QLatin1String("filterCCA")).toBool(); |
| 200 |
prm.filterVIG = settings().value(QLatin1String("filterVIG")).toBool(); |
| 201 |
prm.filterDST = settings().value(QLatin1String("filterDST")).toBool(); |
| 202 |
prm.filterGEO = settings().value(QLatin1String("filterGEO")).toBool(); |
| 203 |
bool useMeta = settings().value(QLatin1String("UseMetadata")).toBool(); |
| 204 |
|
| 205 |
if (useMeta) |
| 206 |
{ |
| 207 |
LensFunIface iface; |
| 208 |
QScopedPointer<DMetadata> meta(new DMetadata(image().getMetadata())); |
| 209 |
LensFunIface::MetadataMatch ret = iface.findFromMetadata(meta.data()); |
| 210 |
prm = iface.settings(); |
| 211 |
|
| 212 |
if (ret != LensFunIface::MetadataExactMatch) |
| 213 |
{ |
| 214 |
setErrorDescription(i18n("Cannot find all lens information to process lens auto-corrections")); |
| 215 |
return false; |
| 216 |
} |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
prm.cropFactor = settings().value(QLatin1String("cropFactor")).toDouble(); |
| 221 |
prm.focalLength = settings().value(QLatin1String("focalLength")).toDouble(); |
| 222 |
prm.aperture = settings().value(QLatin1String("aperture")).toDouble(); |
| 223 |
prm.subjectDistance = settings().value(QLatin1String("subjectDistance")).toDouble(); |
| 224 |
|
| 225 |
prm.cameraMake = settings().value(QLatin1String("cameraMake")).toString(); |
| 226 |
prm.cameraModel = settings().value(QLatin1String("cameraModel")).toString(); |
| 227 |
prm.lensModel = settings().value(QLatin1String("lensModel")).toString(); |
| 228 |
} |
| 229 |
|
| 230 |
LensFunFilter filter(&image(), nullptr, prm); |
| 231 |
applyFilter(&filter); |
| 232 |
|
| 233 |
MetaEngineData data = image().getMetadata(); |
| 234 |
filter.registerSettingsToXmp(data); |
| 235 |
image().setMetadata(data); |
| 236 |
|
| 237 |
return savefromDImg(); |
| 238 |
} |
| 239 |
|
| 240 |
} // namespace DigikamBqmLensAutoFixPlugin |
| 241 |
|
| 242 |
#include "moc_lensautofix.cpp" |