1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : lens distortion algorithm.
9
* Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
10
* Copyright (C) 2001-2003 by David Hodson <hodsond@acm.org>
12
* This program is free software; you can redistribute it
13
* and/or modify it under the terms of the GNU General
14
* Public License as published by the Free Software Foundation;
15
* either version 2, or (at your option)
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* ============================================================ */
34
#include "pixelaccess.h"
35
#include "lensdistortion.h"
37
namespace DigikamLensDistortionImagesPlugin
40
LensDistortion::LensDistortion(Digikam::DImg *orgImage, QObject *parent, double main,
41
double edge, double rescale, double brighten,
42
int center_x, int center_y)
43
: Digikam::DImgThreadedFilter(orgImage, parent, "LensDistortion")
48
m_brighten = brighten;
49
m_centre_x = center_x;
50
m_centre_y = center_y;
55
void LensDistortion::filterImage(void)
57
int Width = m_orgImage.width();
58
int Height = m_orgImage.height();
59
int bytesDepth = m_orgImage.bytesDepth();
61
uchar *data = m_destImage.bits();
65
m_destImage.bitBltImage(&m_orgImage, 0, 0);
67
// initialize coefficients
69
double normallise_radius_sq = 4.0 / (Width * Width + Height * Height);
70
double center_x = Width * (100.0 + m_centre_x) / 200.0;
71
double center_y = Height * (100.0 + m_centre_y) / 200.0;
72
double mult_sq = m_main / 200.0;
73
double mult_qd = m_edge / 200.0;
74
double rescale = pow(2.0, - m_rescale / 100.0);
75
double brighten = - m_brighten / 10.0;
77
PixelAccess *pa = new PixelAccess(&m_orgImage);
80
* start at image (i, j), increment by (step, step)
81
* output goes to dst, which is w x h x d in size
85
// We are working on the full image.
87
int dstHeight = Height;
88
uchar* dst = (uchar*)data;
89
int step = 1, progress;
92
double srcX, srcY, mag;
94
iLimit = dstWidth * step;
95
jLimit = dstHeight * step;
97
for (int dstJ = 0 ; !m_cancel && (dstJ < jLimit) ; dstJ += step)
99
for (int dstI = 0 ; !m_cancel && (dstI < iLimit) ; dstI += step)
101
// Get source Coordinates.
107
off_x = dstI - center_x;
108
off_y = dstJ - center_y;
109
radius_sq = (off_x * off_x) + (off_y * off_y);
111
radius_sq *= normallise_radius_sq;
113
radius_mult = radius_sq * mult_sq + radius_sq * radius_sq * mult_qd;
115
radius_mult = rescale * (1.0 + radius_mult);
117
srcX = center_x + radius_mult * off_x;
118
srcY = center_y + radius_mult * off_y;
120
brighten = 1.0 + mag * brighten;
121
pa->pixelAccessGetCubic(srcX, srcY, brighten, dst);
125
// Update progress bar in dialog.
127
progress = (int) (((double)dstJ * 100.0) / jLimit);
128
if (m_parent && progress%5 == 0)
129
postProgress(progress);
135
} // NameSpace DigikamLensDistortionImagesPlugin