summaryrefslogtreecommitdiffstats
path: root/src/imageplugins/hotpixels/hotpixelfixer.cpp
blob: d29b8525e9527b2994a3c7cf4890237db686c347 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-03-27
 * Description : Threaded image filter to fix hot pixels
 * 
 * Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2005-2006 by Unai Garro <ugarro at users dot sourceforge dot net>
 * 
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * ============================================================ */

// C++ includes.

#include <cmath>
#include <cstdlib>

// TQt includes.

#include <tqcolor.h>
#include <tqregexp.h>
#include <tqstringlist.h>

// Local includes.

#include "dimg.h"
#include "ddebug.h"
#include "hotpixelfixer.h"

#ifdef HAVE_FLOAT_H
#if HAVE_FLOAT_H
# include <float.h>
#endif
#endif

#ifndef DBL_MIN
# define DBL_MIN 1e-37
#endif
#ifndef DBL_MAX
# define DBL_MAX 1e37
#endif

namespace DigikamHotPixelsImagesPlugin
{

HotPixelFixer::HotPixelFixer(Digikam::DImg *orgImage, TQObject *parent, const TQValueList<HotPixel>& hpList, 
                             int interpolationMethod)
             : Digikam::DImgThreadedFilter(orgImage, parent, "HotPixels")
{
    m_hpList              = hpList;
    m_interpolationMethod = interpolationMethod;
    mWeightList.clear();
    
    initFilter();
}

HotPixelFixer::~HotPixelFixer()
{
}

void HotPixelFixer::filterImage(void)
{
    TQValueList <HotPixel>::ConstIterator it;
    TQValueList <HotPixel>::ConstIterator end(m_hpList.end()); 
    for (it = m_hpList.begin() ; it != end ; ++it)
    {
        HotPixel hp = *it;
        interpolate(m_orgImage, hp, m_interpolationMethod);
    }
        
    m_destImage = m_orgImage;
}

// Interpolates a pixel block
void HotPixelFixer::interpolate (Digikam::DImg &img, HotPixel &hp, int method)
{
    const int xPos = hp.x();
    const int yPos = hp.y();
    bool sixtBits  = img.sixteenBit();
    
    // Interpolate pixel.
    switch (method)
    {
        case AVERAGE_INTERPOLATION:
        {
            // We implement the bidimendional one first.
            // TODO: implement the rest of directions (V & H) here
        
            //case twodim:
            // {
            int sum_weight = 0;
            double vr=0.0,vg=0.0,vb=0.0;
            int x, y;
            Digikam::DColor col;
        
            for (x = xPos; x < xPos+hp.width(); ++x)
            {
                if (validPoint(img,TQPoint(x,yPos-1)))
                {
                    col=img.getPixelColor(x,yPos-1);
                    vr += col.red();
                    vg += col.green();
                    vb += col.blue();
                    ++sum_weight;
                }
                if (validPoint(img,TQPoint(x,yPos+hp.height())))
                {
                    col=img.getPixelColor(x,yPos+hp.height());
                    vr += col.red();
                    vg += col.green();
                    vb += col.blue();
                    ++sum_weight;
                }
            }
        
            for (y = yPos; y < hp.height(); ++y)
            {
                if (validPoint(img,TQPoint(xPos-1,y)))
                {
                    col=img.getPixelColor(xPos,y);
                    vr += col.red();
                    vg += col.green();
                    vb += col.blue();
                    ++sum_weight;
                }
                if (validPoint(img,TQPoint(xPos+hp.width(),y)))
                {
                    col=img.getPixelColor(xPos+hp.width(),y);
                    vr += col.red();
                    vg += col.green();
                    vb += col.blue();
                    ++sum_weight;
                }
            }
        
            if (sum_weight > 0)
            {
                vr /= (double)sum_weight;
                vg /= (double)sum_weight;
                vb /= (double)sum_weight;
    
                
                for (x = 0; x < hp.width(); ++x)
                for (y = 0; y < hp.height(); ++y)
                if (validPoint(img,TQPoint(xPos+x,yPos+y)))
                {
                    int alpha=sixtBits ? 65535 : 255;
                    int ir=(int )round(vr),ig=(int) round(vg),ib=(int) round(vb);
                    img.setPixelColor(xPos+x,yPos+y,Digikam::DColor(ir,ig,ib,alpha,sixtBits));
                }
            }
            break;
        } //Case average
    
        case LINEAR_INTERPOLATION:
            //(Bi)linear interpolation.
            weightPixels (img,hp,LINEAR_INTERPOLATION,TWODIM_DIRECTION,sixtBits ? 65535: 255);
            break;
    
        case QUADRATIC_INTERPOLATION:
            // (Bi)quadratic interpolation.
            weightPixels (img,hp,QUADRATIC_INTERPOLATION,TWODIM_DIRECTION,sixtBits ? 65535 : 255);
            break;
    
        case CUBIC_INTERPOLATION:
            // (Bi)cubic interpolation. 
            weightPixels (img,hp,CUBIC_INTERPOLATION,TWODIM_DIRECTION,sixtBits ? 65535 : 255);
            break;
    } //switch
}

void HotPixelFixer::weightPixels (Digikam::DImg &img, HotPixel &px, int method, Direction dir,int maxComponent)
{
    //TODO: implement direction here too
        
    for (int iComp = 0; iComp < 3; ++iComp)
    {
        // Obtain weight data block.  
    
        Weights w;
        int polynomeOrder=-1;
        
        switch (method)
        {
            case AVERAGE_INTERPOLATION:  // Gilles: to prevent warnings from compiler.
                break;
            case LINEAR_INTERPOLATION:
                polynomeOrder=1;
                break;
            case QUADRATIC_INTERPOLATION:
                polynomeOrder=2;
                break;
            case CUBIC_INTERPOLATION:
                polynomeOrder=3;
                break;        
        }
        if (polynomeOrder<0) return;
        
        // In the one-dimensional case, the width must be 1,
        // and the size must be stored in height 
        
        w.setWidth(dir == TWODIM_DIRECTION ? px.width() : 1);
        w.setHeight(dir == HORIZONTAL_DIRECTION ? px.width() : px.height());
        w.setPolynomeOrder(polynomeOrder);
        w.setTwoDim(dir == TWODIM_DIRECTION);
    
        //TODO: check this, it must not recalculate existing calculated weights
        //for now I don't think it is finding the duplicates fine, so it uses
        //the previous one always...
        
        //if (mWeightList.find(w)==mWeightList.end())
        //{
            w.calculateWeights();
            
        //    mWeightList.append(w);
            
        //}
     
        // Calculate weighted pixel sum.  
        for (int y = 0; y<px.height(); ++y)
        {
            for (int x = 0; x < px.width(); ++x)
            {
                if (validPoint (img,TQPoint(px.x()+x,px.y()+y)))
                {
                    double sum_weight = 0.0, v = 0.0;
                    size_t i;
            
                    for (i = 0; i < w.positions().count(); ++i)
                    {
                        // In the one-dimensional case, only the y coordinate is used.  
                        const int xx = px.x()+(dir == VERTICAL_DIRECTION ? x : 
                                  dir== HORIZONTAL_DIRECTION ? w.positions()[i].y() : w.positions()[i].x());
                        const int yy = px.y()+(dir == HORIZONTAL_DIRECTION ? y : 
                                  w.positions()[i].y());
            
                        if (validPoint (img,TQPoint(xx, yy)))
                        {
                            //TODO: check this. I think it is broken
                            double weight;
                            if (dir==VERTICAL_DIRECTION)
                            {
                                weight = w[i][y][0];
                            }
                            else if (dir==HORIZONTAL_DIRECTION)
                            {
                                weight=w[i][0][x];
                            }
                            else
                            {
                                weight=w[i][y][x];
                            }
                                    
                            if (iComp==0) v += weight * img.getPixelColor(xx, yy).red();
                            else if (iComp==1) v += weight * img.getPixelColor(xx, yy).green();
                            else v += weight * img.getPixelColor(xx, yy).blue();
                            
                            sum_weight += weight;
                        }
                    } 
                        
                    Digikam::DColor color=img.getPixelColor(px.x()+x,px.y()+y);
                    int component;
                    if (fabs (v) <= DBL_MIN)
                        component=0;
                    else if (sum_weight >= DBL_MIN)
                    {	
                        component=(int) (v/sum_weight);
                        //Clamp value
                        if (component<0) component=0;
                        if (component>maxComponent) component=maxComponent;
                    }
                    else if (v >= 0.0)
                        component=maxComponent;
                    else
                        component=0;
                    
                    if (iComp==0) color.setRed(component);
                    else if (iComp==1) color.setGreen(component);
                    else color.setBlue(component);

                    
                    img.setPixelColor(px.x()+x,px.y()+y,color);
                } 
            } 
        }
    }
}

}  // NameSpace DigikamHotPixelsImagesPlugin