~ubuntu-branches/ubuntu/intrepid/digikam/intrepid

« back to all changes in this revision

Viewing changes to digikam/imageplugins/lensdistortion/lensdistortion.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.3.2 upstream) (37 hardy)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20080717202539-1bw3w3nrsso7yj4z
* New upstream release
  - digiKam 0.9.4 Release Plan (KDE3) ~ 13 July 08 (Closes: #490144)
* DEB_CONFIGURE_EXTRA_FLAGS := --without-included-sqlite3
* Debhelper compatibility level V7
* Install pixmaps in debian/*.install
* Add debian/digikam.lintian-overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2005-05-25
 
7
 * Description : lens distortion algorithm.
 
8
 * 
 
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>
 
11
 * 
 
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)
 
16
 * any later version.
 
17
 * 
 
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.
 
22
 * 
 
23
 * ============================================================ */
 
24
 
 
25
// C++ includes. 
 
26
 
 
27
#include <cmath>
 
28
#include <cstdlib>
 
29
 
 
30
// Local includes.
 
31
 
 
32
#include "dimg.h"
 
33
#include "ddebug.h"
 
34
#include "pixelaccess.h"
 
35
#include "lensdistortion.h"
 
36
 
 
37
namespace DigikamLensDistortionImagesPlugin
 
38
{
 
39
 
 
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")
 
44
 
45
    m_main     = main;
 
46
    m_edge     = edge;
 
47
    m_rescale  = rescale;
 
48
    m_brighten = brighten;
 
49
    m_centre_x = center_x;
 
50
    m_centre_y = center_y;
 
51
    
 
52
    initFilter();
 
53
}
 
54
 
 
55
void LensDistortion::filterImage(void)
 
56
{
 
57
    int    Width      = m_orgImage.width();
 
58
    int    Height     = m_orgImage.height();
 
59
    int    bytesDepth = m_orgImage.bytesDepth();
 
60
 
 
61
    uchar *data       = m_destImage.bits();
 
62
 
 
63
    // initial copy
 
64
 
 
65
    m_destImage.bitBltImage(&m_orgImage, 0, 0);
 
66
 
 
67
    // initialize coefficients
 
68
 
 
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;
 
76
 
 
77
    PixelAccess *pa = new PixelAccess(&m_orgImage);
 
78
 
 
79
    /*
 
80
     * start at image (i, j), increment by (step, step)
 
81
     * output goes to dst, which is w x h x d in size
 
82
     * NB: d <= image.bpp
 
83
     */
 
84
 
 
85
    // We are working on the full image.
 
86
    int    dstWidth = Width;
 
87
    int    dstHeight = Height;
 
88
    uchar* dst = (uchar*)data;
 
89
    int    step = 1, progress;
 
90
 
 
91
    int    iLimit, jLimit;
 
92
    double srcX, srcY, mag;
 
93
 
 
94
    iLimit = dstWidth * step;
 
95
    jLimit = dstHeight * step;
 
96
 
 
97
    for (int dstJ = 0 ; !m_cancel && (dstJ < jLimit) ; dstJ += step) 
 
98
    {
 
99
        for (int dstI = 0 ; !m_cancel && (dstI < iLimit) ; dstI += step) 
 
100
        {
 
101
            // Get source Coordinates.
 
102
            double radius_sq;
 
103
            double off_x;
 
104
            double off_y;
 
105
            double radius_mult;
 
106
 
 
107
            off_x       = dstI - center_x;
 
108
            off_y       = dstJ - center_y;
 
109
            radius_sq   = (off_x * off_x) + (off_y * off_y);
 
110
 
 
111
            radius_sq  *= normallise_radius_sq;
 
112
 
 
113
            radius_mult = radius_sq * mult_sq + radius_sq * radius_sq * mult_qd;
 
114
            mag         = radius_mult;
 
115
            radius_mult = rescale * (1.0 + radius_mult);
 
116
 
 
117
            srcX        = center_x + radius_mult * off_x;
 
118
            srcY        = center_y + radius_mult * off_y;
 
119
 
 
120
            brighten = 1.0 + mag * brighten;
 
121
            pa->pixelAccessGetCubic(srcX, srcY, brighten, dst);
 
122
            dst += bytesDepth;
 
123
        }
 
124
 
 
125
       // Update progress bar in dialog.
 
126
 
 
127
        progress = (int) (((double)dstJ * 100.0) / jLimit);
 
128
        if (m_parent && progress%5 == 0)
 
129
            postProgress(progress);
 
130
    }
 
131
 
 
132
    delete pa;
 
133
}
 
134
 
 
135
}  // NameSpace DigikamLensDistortionImagesPlugin