1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2005-05-25
* Description : Antivignetting threaded image filter.
*
* SPDX-FileCopyrightText: 2005-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2010 by Julien Narboux <julien at narboux dot fr>
* SPDX-FileCopyrightText: 2010 by Martin Klapetek <martin dot klapetek at gmail dot com>
*
* Original AntiVignettingFilter algorithm copyrighted 2003 by
* John Walker from 'pnmctrfilt' implementation. See
* https://www.fourmilab.ch/netpbm/pnmctrfilt for more
* information.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "antivignettingfilter.h"
// C++ includes
#include <cmath>
#include <cstdlib>
// Local includes
#include "dimg.h"
#include "digikam_globals_p.h" // For KF6::Ki18n deprecated
namespace Digikam
{
AntiVignettingFilter::AntiVignettingFilter(QObject* const parent)
: DImgThreadedFilter(parent)
{
initFilter();
}
AntiVignettingFilter::AntiVignettingFilter(DImg* const orgImage, QObject* const parent,
const AntiVignettingContainer& settings)
: DImgThreadedFilter(orgImage, parent, QLatin1String("AntiVignettingFilter")),
m_settings (settings)
{
initFilter();
}
AntiVignettingFilter::~AntiVignettingFilter()
{
cancelFilter();
}
QString AntiVignettingFilter::DisplayableName()
{
return QString::fromUtf8(I18N_NOOP("Anti-Vignetting Tool"));
}
/**
* This method is inspired from John Walker 'pnmctrfilt' algorithm code.
*/
void AntiVignettingFilter::filterImage()
{
int progress;
int col, row, xd, td, yd, p;
int xsize, ysize, /*diagonal,*/ erad, irad, xctr, yctr;
uchar* NewBits = m_destImage.bits();
uchar* data = m_orgImage.bits();<--- Variable 'data' can be declared as pointer to const
unsigned short* NewBits16 = reinterpret_cast<unsigned short*>(m_destImage.bits());
unsigned short* data16 = reinterpret_cast<unsigned short*>(m_orgImage.bits());<--- Variable 'data16' can be declared as pointer to const
int Width = m_orgImage.width();
int Height = m_orgImage.height();
// Determine the shift in pixels from the shift in percentage.
m_settings.yshift = m_settings.yshift * Height / 200.0;
m_settings.xshift = m_settings.xshift * Width / 200.0;
// Determine the outer radius of the filter. This is the half diagonal
// measure of the image multiplied by the radius factor.
xsize = (Height + 1) / 2;
ysize = (Width + 1) / 2;
erad = qRound(hypothenuse(xsize, ysize) * m_settings.outerradius);
irad = qRound(hypothenuse(xsize, ysize) * m_settings.outerradius * m_settings.innerradius);
/*
xsize = qRound(Width / 2.0 + fabs(m_settings.xshift));
ysize = qRound(Height / 2.0 + fabs(m_settings.yshift));
diagonal = qRound(hypothenuse(xsize, ysize)) + 1;
*/
xctr = qRound(Width / 2.0 + m_settings.xshift);
yctr = qRound(Height / 2.0 + m_settings.yshift);
for (row = 0 ; runningFlag() && (row < Width) ; ++row)
{
yd = abs(xctr - row);
for (col = 0 ; runningFlag() && (col < Height) ; ++col)
{
p = (col * Width + row) * 4;
xd = abs(yctr - col);
td = qRound(hypothenuse(xd, yd));
if (!m_orgImage.sixteenBit()) // 8 bits image
{
NewBits[ p ] = clamp8bits(data[ p ] * real_attenuation(irad, erad, td));
NewBits[p + 1] = clamp8bits(data[p + 1] * real_attenuation(irad, erad, td));
NewBits[p + 2] = clamp8bits(data[p + 2] * real_attenuation(irad, erad, td));
NewBits[p + 3] = data[p + 3];
}
else // 16 bits image.
{
NewBits16[ p ] = clamp16bits(data16[ p ] * real_attenuation(irad, erad, td));
NewBits16[p + 1] = clamp16bits(data16[p + 1] * real_attenuation(irad, erad, td));
NewBits16[p + 2] = clamp16bits(data16[p + 2] * real_attenuation(irad, erad, td));
NewBits16[p + 3] = data16[p + 3];
}
}
// Update the progress bar in dialog.
progress = (int)(((double)row * 100.0) / Width);
if ((progress % 5) == 0)
{
postProgress(progress);
}
}
}
inline double AntiVignettingFilter::hypothenuse(double x, double y)
{
return (sqrt(x * x + y * y));
}
double AntiVignettingFilter::attenuation(double r1, double r2, double dist_center)
{
if (dist_center < r1)
{
return 1.0;
}
else if (dist_center > r2)
{
return 1.0 + m_settings.density;
}
else
{
return (1.0 + m_settings.density * (pow((dist_center - r1) / (r2 - r1), m_settings.power)));
}
}
double AntiVignettingFilter::real_attenuation(double r1, double r2, double dist_center)
{
if (!m_settings.addvignetting)
{
return (attenuation(r1, r2, dist_center));
}
else
{
return (1.0 / attenuation(r1, r2, dist_center));
}
}
uchar AntiVignettingFilter::clamp8bits(double x)
{
if (x < 0)
{
return 0;
}
else if (x > 255)
{
return 255;
}
else
{
return ((uchar) x);
}
}
unsigned short AntiVignettingFilter::clamp16bits(double x)
{
if (x < 0)
{
return 0;
}
else if (x > 65535)
{
return 65535;
}
else
{
return ((unsigned short) x);
}
}
FilterAction AntiVignettingFilter::filterAction()
{
FilterAction action(FilterIdentifier(), CurrentVersion());
action.setDisplayableName(DisplayableName());
action.addParameter(QLatin1String("addvignetting"), m_settings.addvignetting);
action.addParameter(QLatin1String("density"), m_settings.density);
action.addParameter(QLatin1String("innerradius"), m_settings.innerradius);
action.addParameter(QLatin1String("outerradius"), m_settings.outerradius);
action.addParameter(QLatin1String("power"), m_settings.power);
action.addParameter(QLatin1String("xshift"), m_settings.xshift);
action.addParameter(QLatin1String("yshift"), m_settings.yshift);
return action;
}
void AntiVignettingFilter::readParameters(const Digikam::FilterAction& action)
{
m_settings.addvignetting = action.parameter(QLatin1String("addvignetting")).toBool();
m_settings.density = action.parameter(QLatin1String("density")).toDouble();
m_settings.innerradius = action.parameter(QLatin1String("innerradius")).toDouble();
m_settings.outerradius = action.parameter(QLatin1String("outerradius")).toDouble();
m_settings.power = action.parameter(QLatin1String("power")).toDouble();
m_settings.xshift = action.parameter(QLatin1String("xshift")).toDouble();
m_settings.yshift = action.parameter(QLatin1String("yshift")).toDouble();
}
} // namespace Digikam
#include "moc_antivignettingfilter.cpp"
|