~ubuntu-branches/ubuntu/karmic/digikam/karmic-backports

« back to all changes in this revision

Viewing changes to imageplugins/charcoal/charcoal.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ubuntu Archive Auto-Backport
  • Date: 2009-12-07 19:03:53 UTC
  • mfrom: (54.1.4 lucid)
  • Revision ID: james.westby@ubuntu.com-20091207190353-oara3lenjxymto3i
Tags: 2:1.0.0~rc-1ubuntu1~karmic1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
#include <cmath>
36
36
 
 
37
// KDE includes
 
38
 
 
39
#include <kdebug.h>
 
40
 
37
41
// Local includes
38
42
 
39
43
#include "dimg.h"
40
44
#include "dimggaussianblur.h"
41
45
#include "dimgimagefilters.h"
42
 
#include "debug.h"
43
46
 
44
47
namespace DigikamCharcoalImagesPlugin
45
48
{
57
60
{
58
61
    if (m_orgImage.isNull())
59
62
    {
60
 
       kWarning(imagePluginsAreaCode) << "No image data available!";
 
63
       kWarning() << "No image data available!";
61
64
       return;
62
65
    }
63
66
 
74
77
 
75
78
    if ((int)m_orgImage.width() < kernelWidth)
76
79
    {
77
 
        kWarning(imagePluginsAreaCode) << "Image is smaller than radius!";
 
80
        kWarning() << "Image is smaller than radius!";
78
81
        return;
79
82
    }
80
83
 
82
85
 
83
86
    if (!kernel)
84
87
    {
85
 
        kWarning(imagePluginsAreaCode) << "Unable to allocate memory!";
 
88
        kWarning() << "Unable to allocate memory!";
86
89
        return;
87
90
    }
88
91
 
95
98
 
96
99
    // -- Applying Gaussian blur effect ---------------------------------------
97
100
 
98
 
    Digikam::DImgGaussianBlur(this, m_destImage, m_destImage, 50, 60, (int)(m_smooth/10.0));
 
101
    Digikam::DImgGaussianBlur(this, m_destImage, m_destImage, 80, 85, (int)(m_smooth/10.0));
99
102
 
100
103
    if (m_cancel)
101
104
        return;
104
107
 
105
108
    Digikam::DImgImageFilters().stretchContrastImage(m_destImage.bits(), m_destImage.width(),
106
109
                                m_destImage.height(), m_destImage.sixteenBit());
107
 
    postProgress( 70 );
 
110
    postProgress( 90 );
108
111
    if (m_cancel)
109
112
        return;
110
113
 
112
115
 
113
116
    Digikam::DImgImageFilters().invertImage(m_destImage.bits(), m_destImage.width(),
114
117
                                m_destImage.height(), m_destImage.sixteenBit());
115
 
    postProgress( 80 );
 
118
    postProgress( 95 );
116
119
    if (m_cancel)
117
120
        return;
118
121
 
126
129
                   0.3F, 0.59F , 0.11F,                             // Red channel gains.
127
130
                   0.0F, 1.0F,   0.0F,                              // Green channel gains (not used).
128
131
                   0.0F, 0.0F,   1.0F);                             // Blue channel gains (not used).
129
 
    postProgress( 90 );
 
132
    postProgress( 100 );
130
133
    if (m_cancel)
131
134
        return;
132
135
}
138
141
    long    kernelWidth, i;
139
142
    double  red, green, blue, alpha, normalize=0.0;
140
143
    double *k=0;
141
 
    Digikam::DColor  color;
142
144
 
143
145
    kernelWidth = order;
144
146
 
145
147
    if ((kernelWidth % 2) == 0)
146
148
    {
147
 
        kWarning(imagePluginsAreaCode) << "Kernel width must be an odd number!";
 
149
        kWarning() << "Kernel width must be an odd number!";
148
150
        return(false);
149
151
    }
150
152
 
151
 
    double *normal_kernel = new double[kernelWidth*kernelWidth];
 
153
    double *normal_kernel = new double[kernelWidth * kernelWidth];
152
154
 
153
155
    if (!normal_kernel)
154
156
    {
155
 
        kWarning(imagePluginsAreaCode) << "Unable to allocate memory!";
 
157
        kWarning() << "Unable to allocate memory!";
156
158
        return(false);
157
159
    }
158
160
 
159
 
    for(i=0 ; i < (kernelWidth*kernelWidth) ; ++i)
 
161
    for (i = 0; i < (kernelWidth * kernelWidth); ++i)
 
162
    {
160
163
        normalize += kernel[i];
 
164
    }
161
165
 
162
166
    if (fabs(normalize) <= Epsilon)
 
167
    {
163
168
        normalize=1.0;
 
169
    }
164
170
 
165
171
    normalize = 1.0/normalize;
166
172
 
167
 
    for(i=0 ; i < (kernelWidth*kernelWidth) ; ++i)
168
 
        normal_kernel[i] = normalize*kernel[i];
 
173
    for (i = 0; i < (kernelWidth * kernelWidth); ++i)
 
174
    {
 
175
        normal_kernel[i] = normalize * kernel[i];
 
176
    }
 
177
 
 
178
    // --------------------------------------------------------
 
179
 
 
180
    // caching
 
181
    uint height     = m_destImage.height();
 
182
    uint width      = m_destImage.width();
 
183
    bool sixteenBit = m_destImage.sixteenBit();
 
184
    uchar *ddata    = m_destImage.bits();
 
185
    int ddepth      = m_destImage.bytesDepth();
 
186
 
 
187
    uchar *sdata    = m_orgImage.bits();
 
188
    int sdepth      = m_orgImage.bytesDepth();
169
189
 
170
190
    double maxClamp = m_destImage.sixteenBit() ? 16777215.0 : 65535.0;
171
191
 
172
 
    for(y=0 ; !m_cancel && (y < m_destImage.height()) ; ++y)
 
192
    // --------------------------------------------------------
 
193
 
 
194
    for (y = 0; !m_cancel && (y < height); ++y)
173
195
    {
174
 
        sy = y-(kernelWidth/2);
 
196
        sy = y - (kernelWidth / 2);
175
197
 
176
 
        for(x=0 ; !m_cancel && (x < m_destImage.width()) ; ++x)
 
198
        for (x = 0; !m_cancel && (x < width); ++x)
177
199
        {
178
 
            k   = normal_kernel;
 
200
            k = normal_kernel;
179
201
            red = green = blue = alpha = 0;
180
 
            sy  = y-(kernelWidth/2);
 
202
            sy = y - (kernelWidth / 2);
181
203
 
182
 
            for(mcy=0 ; !m_cancel && (mcy < kernelWidth) ; ++mcy, ++sy)
 
204
            for (mcy = 0; !m_cancel && (mcy < kernelWidth); ++mcy, ++sy)
183
205
            {
184
 
                my = sy < 0 ? 0 : sy > (int)m_destImage.height()-1 ? m_destImage.height()-1 : sy;
185
 
                sx = x+(-kernelWidth/2);
 
206
                my = sy < 0 ? 0 : sy > (int) height - 1 ? height - 1 : sy;
 
207
                sx = x + (-kernelWidth / 2);
186
208
 
187
 
                for(mcx=0 ; !m_cancel && (mcx < kernelWidth) ; ++mcx, ++sx)
 
209
                for (mcx = 0; !m_cancel && (mcx < kernelWidth); ++mcx, ++sx)
188
210
                {
189
 
                    mx     = sx < 0 ? 0 : sx > (int)m_destImage.width()-1 ? m_destImage.width()-1 : sx;
190
 
                    color  = m_orgImage.getPixelColor(mx, my);
191
 
                    red   += (*k)*(color.red()   * 257.0);
192
 
                    green += (*k)*(color.green() * 257.0);
193
 
                    blue  += (*k)*(color.blue()  * 257.0);
194
 
                    alpha += (*k)*(color.alpha() * 257.0);
 
211
                    mx = sx < 0 ? 0 : sx > (int) width - 1 ? width - 1 : sx;
 
212
                    Digikam::DColor color(sdata + mx * sdepth + (width * my * sdepth), sixteenBit);
 
213
                    red += (*k) * (color.red() * 257.0);
 
214
                    green += (*k) * (color.green() * 257.0);
 
215
                    blue += (*k) * (color.blue() * 257.0);
 
216
                    alpha += (*k) * (color.alpha() * 257.0);
195
217
                    ++k;
196
218
                }
197
219
            }
201
223
            blue  =  blue < 0.0 ? 0.0 :  blue > maxClamp ? maxClamp :  blue+0.5;
202
224
            alpha = alpha < 0.0 ? 0.0 : alpha > maxClamp ? maxClamp : alpha+0.5;
203
225
 
204
 
            m_destImage.setPixelColor(x, y, Digikam::DColor((int)(red / 257UL),  (int)(green / 257UL),
205
 
                                            (int)(blue / 257UL), (int)(alpha / 257UL),
206
 
                                            m_destImage.sixteenBit()));
 
226
            Digikam::DColor color((int)(red / 257UL),  (int)(green / 257UL),
 
227
                                  (int)(blue / 257UL), (int)(alpha / 257UL), sixteenBit);
 
228
            color.setPixel((ddata + x * ddepth + (width * y * ddepth)));
207
229
        }
208
230
 
209
 
        progress = (int)(((double)y * 50.0) / m_destImage.height());
210
 
        if ( progress%5 == 0 )
211
 
           postProgress( progress );
 
231
        progress = (int) (((double) y * 80.0) / height);
 
232
        if (progress % 5 == 0)
 
233
        {
 
234
            postProgress(progress);
 
235
        }
212
236
    }
213
237
 
214
238
    delete [] normal_kernel;