~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/dimg/filters/sharp/sharpenfilter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2010-04-09 21:30:01 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409213001-4bfyibrd359rn7o3
Tags: 2:1.2.0-0ubuntu1
* New upstream release (LP: #560576)
* Remove all patches, fixed upstream
  - Remove quilt build-depend

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-17-07
 
7
 * Description : A Sharpen threaded image filter.
 
8
 *
 
9
 * Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
10
 *
 
11
 * Original Sharpen algorithm copyright 2002
 
12
 * by Daniel M. Duley <mosfet@kde.org> from KImageEffect API.
 
13
 *
 
14
 * This program is free software; you can redistribute it
 
15
 * and/or modify it under the terms of the GNU General
 
16
 * Public License as published by the Free Software Foundation;
 
17
 * either version 2, or (at your option)
 
18
 * any later version.
 
19
 *
 
20
 * This program is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 *
 
25
 * ============================================================ */
 
26
 
 
27
#define SQ2PI   2.50662827463100024161235523934010416269302368164062
 
28
#define Epsilon 1.0e-12
 
29
 
 
30
#include "sharpenfilter.h"
 
31
 
 
32
// C++ includes
 
33
 
 
34
#include <cmath>
 
35
#include <cstdlib>
 
36
 
 
37
// KDE includes
 
38
 
 
39
#include <kdebug.h>
 
40
 
 
41
namespace Digikam
 
42
{
 
43
 
 
44
SharpenFilter::SharpenFilter(DImg* orgImage, QObject* parent, double radius, double sigma)
 
45
             : DImgThreadedFilter(orgImage, parent, "Sharpen")
 
46
{
 
47
    m_radius = radius;
 
48
    m_sigma  = sigma;
 
49
    initFilter();
 
50
}
 
51
 
 
52
SharpenFilter::SharpenFilter(DImgThreadedFilter* parentFilter,
 
53
                             const DImg& orgImage, const DImg& destImage,
 
54
                             int progressBegin, int progressEnd, double radius, double sigma)
 
55
             : DImgThreadedFilter(parentFilter, orgImage, destImage, progressBegin, progressEnd,
 
56
                                  parentFilter->filterName() + ": Sharpen")
 
57
{
 
58
    m_radius = radius;
 
59
    m_sigma  = sigma;
 
60
    // We need to provide support for orgImage == destImage.
 
61
    // The algorithm does not support this out of the box, so use a temporary.
 
62
    if (orgImage.bits() == destImage.bits())
 
63
        m_destImage = DImg(destImage.width(), destImage.height(), destImage.sixteenBit());
 
64
    filterImage();
 
65
    if (orgImage.bits() == destImage.bits())
 
66
        memcpy(destImage.bits(), m_destImage.bits(), m_destImage.numBytes());
 
67
}
 
68
 
 
69
void SharpenFilter::filterImage()
 
70
{
 
71
    sharpenImage(m_radius, m_sigma);
 
72
}
 
73
 
 
74
/** Function to apply the sharpen filter on an image*/
 
75
 
 
76
void SharpenFilter::sharpenImage(double radius, double sigma)
 
77
{
 
78
    if (m_orgImage.isNull())
 
79
    {
 
80
       kWarning() << "No image data available!";
 
81
       return;
 
82
    }
 
83
 
 
84
    if (radius <= 0.0)
 
85
    {
 
86
       m_destImage = m_orgImage;
 
87
       return;
 
88
    }
 
89
 
 
90
    double        alpha, normalize=0.0;
 
91
    register long i=0, u, v;
 
92
 
 
93
    int kernelWidth     = getOptimalKernelWidth(radius, sigma);
 
94
    int halfKernelWidth = kernelWidth / 2;
 
95
 
 
96
    if((int)m_orgImage.width() < kernelWidth)
 
97
    {
 
98
        kWarning() << "Image is smaller than radius!";
 
99
        return;
 
100
    }
 
101
 
 
102
    double *kernel = new double[kernelWidth*kernelWidth];
 
103
 
 
104
    if(!kernel)
 
105
    {
 
106
        kWarning() << "Unable to allocate memory!";
 
107
        return;
 
108
    }
 
109
 
 
110
    for(v = -halfKernelWidth; v <= halfKernelWidth; ++v)
 
111
    {
 
112
        for(u = -halfKernelWidth; u <= halfKernelWidth; ++u)
 
113
        {
 
114
            alpha      = exp(-((double) u*u+v*v)/(2.0*sigma*sigma));
 
115
            kernel[i]  = alpha/(2.0*M_PI*sigma*sigma);
 
116
            normalize += kernel[i];
 
117
            ++i;
 
118
        }
 
119
    }
 
120
 
 
121
    kernel[i/2] = (-2.0)*normalize;
 
122
    convolveImage(kernelWidth, kernel);
 
123
 
 
124
    delete [] kernel;
 
125
}
 
126
 
 
127
bool SharpenFilter::convolveImage(const unsigned int order, const double* kernel)
 
128
{
 
129
    uint    x, y;
 
130
    int     mx, my, sx, sy, mcx, mcy, progress;
 
131
    long    kernelWidth, i;
 
132
    double  red, green, blue, alpha, normalize=0.0;
 
133
    double *k=0;
 
134
    DColor  color;
 
135
 
 
136
    kernelWidth          = order;
 
137
    long halfKernelWidth = kernelWidth / 2;
 
138
 
 
139
    if((kernelWidth % 2) == 0)
 
140
    {
 
141
        kWarning() << "Kernel width must be an odd number!";
 
142
        return(false);
 
143
    }
 
144
 
 
145
    double *normal_kernel = new double[kernelWidth*kernelWidth];
 
146
 
 
147
    if(!normal_kernel)
 
148
    {
 
149
        kWarning() << "Unable to allocate memory!";
 
150
        return(false);
 
151
    }
 
152
 
 
153
    for(i=0 ; i < (kernelWidth*kernelWidth) ; ++i)
 
154
        normalize += kernel[i];
 
155
 
 
156
    if(fabs(normalize) <= Epsilon)
 
157
        normalize=1.0;
 
158
 
 
159
    normalize = 1.0/normalize;
 
160
 
 
161
    for(i=0 ; i < (kernelWidth*kernelWidth) ; ++i)
 
162
        normal_kernel[i] = normalize*kernel[i];
 
163
 
 
164
    double maxClamp = m_destImage.sixteenBit() ? 16777215.0 : 65535.0;
 
165
 
 
166
    for(y=0 ; !m_cancel && (y < m_destImage.height()) ; ++y)
 
167
    {
 
168
        // FIXME: this calculation seems to be useless, since we already do it in the following loop
 
169
//        sy = y-halfKernelWidth;
 
170
 
 
171
        for(x=0 ; !m_cancel && (x < m_destImage.width()) ; ++x)
 
172
        {
 
173
            k   = normal_kernel;
 
174
            red = green = blue = alpha = 0;
 
175
            sy  = y-halfKernelWidth;
 
176
 
 
177
            for(mcy=0 ; !m_cancel && (mcy < kernelWidth) ; ++mcy, ++sy)
 
178
            {
 
179
                my = sy < 0 ? 0 : sy > (int)m_destImage.height()-1 ? m_destImage.height()-1 : sy;
 
180
                sx = x+(-halfKernelWidth);
 
181
 
 
182
                for(mcx=0 ; !m_cancel && (mcx < kernelWidth) ; ++mcx, ++sx)
 
183
                {
 
184
                    mx     = sx < 0 ? 0 : sx > (int)m_destImage.width()-1 ? m_destImage.width()-1 : sx;
 
185
                    color  = m_orgImage.getPixelColor(mx, my);
 
186
                    red   += (*k)*(color.red()   * 257.0);
 
187
                    green += (*k)*(color.green() * 257.0);
 
188
                    blue  += (*k)*(color.blue()  * 257.0);
 
189
                    alpha += (*k)*(color.alpha() * 257.0);
 
190
                    ++k;
 
191
                }
 
192
            }
 
193
 
 
194
            red   =   red < 0.0 ? 0.0 :   red > maxClamp ? maxClamp :   red+0.5;
 
195
            green = green < 0.0 ? 0.0 : green > maxClamp ? maxClamp : green+0.5;
 
196
            blue  =  blue < 0.0 ? 0.0 :  blue > maxClamp ? maxClamp :  blue+0.5;
 
197
            alpha = alpha < 0.0 ? 0.0 : alpha > maxClamp ? maxClamp : alpha+0.5;
 
198
 
 
199
            m_destImage.setPixelColor(x, y, DColor((int)(red / 257UL),  (int)(green / 257UL),
 
200
                                                   (int)(blue / 257UL), (int)(alpha / 257UL),
 
201
                                                   m_destImage.sixteenBit()));
 
202
        }
 
203
 
 
204
        progress = (int)(((double)y * 100.0) / m_destImage.height());
 
205
        if ( progress%5 == 0 )
 
206
           postProgress( progress );
 
207
    }
 
208
 
 
209
    delete [] normal_kernel;
 
210
    return(true);
 
211
}
 
212
 
 
213
int SharpenFilter::getOptimalKernelWidth(double radius, double sigma)
 
214
{
 
215
    double        normalize, value;
 
216
    long          kernelWidth;
 
217
    register long u;
 
218
 
 
219
    if(radius > 0.0)
 
220
        return((int)(2.0*ceil(radius)+1.0));
 
221
 
 
222
    for(kernelWidth=5; ;)
 
223
    {
 
224
        normalize=0.0;
 
225
 
 
226
        for(u=(-kernelWidth/2) ; u <= (kernelWidth/2) ; ++u)
 
227
            normalize += exp(-((double) u*u)/(2.0*sigma*sigma))/(SQ2PI*sigma);
 
228
 
 
229
        u     = kernelWidth/2;
 
230
        value = exp(-((double) u*u)/(2.0*sigma*sigma))/(SQ2PI*sigma)/normalize;
 
231
 
 
232
        if((long)(65535*value) <= 0)
 
233
            break;
 
234
 
 
235
        kernelWidth+=2;
 
236
    }
 
237
 
 
238
    return((int)kernelWidth-2);
 
239
}
 
240
 
 
241
}  // namespace Digikam