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

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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-03-27
 * Description : a class to calculate filter weights
 * 
 * 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 <cstring>

// Local includes.

#include "weights.h"

namespace DigikamHotPixelsImagesPlugin
{

Weights::Weights(const Weights &w)
{
    (*this) = w;    
}

void Weights::operator=(const Weights &w)
{
    mHeight=w.height();
    mWidth=w.width();
    mPositions=(w.positions());
    mCoefficientNumber=w.coefficientNumber();
    mTwoDim=w.twoDim();
    mPolynomeOrder=w.polynomeOrder();
    
    // Allocate memory and copy weights
    // if the original one was calculated
    
    if (!w.weightMatrices()) return;
    else
    {
        double*** origMatrices=w.weightMatrices();
        mWeightMatrices = new double**[mPositions.count()]; //allocate mPositions.count() matrices
    
        for (uint i=0; i<mPositions.count(); i++)
        {
            mWeightMatrices[i]=new double*[mHeight]; //allocate mHeight rows on each position
            for (unsigned int j=0; j<mHeight; j++)
            {
                mWeightMatrices[i][j]=new double[mWidth]; //Allocate mWidth columns on each row
                for (unsigned int k=0; k<mWidth; k++) 
                {
                    mWeightMatrices[i][j][k]=origMatrices[i][j][k];
                }
            }
        }
    }
}
    
void Weights::calculateWeights()
{
    mCoefficientNumber = (mTwoDim
                              ? ((size_t)mPolynomeOrder + 1) * ((size_t)mPolynomeOrder + 1)
                              : (size_t)mPolynomeOrder + 1);
    double *matrix;  /* num_coeff * num_coeff */
    double *vector0; /* mPositions.count()   * num_coeff */
    double *vector1; /* mPositions.count()   * num_coeff */
    size_t ix,iy,i,j;
    int x, y;
    
    // Determine coordinates of pixels to be sampled
    
    if (mTwoDim)
    {

	int iPolynomeOrder=(int) mPolynomeOrder; //lets avoid signed/unsigned comparison warnings
	int iHeight = (int) height();            //"
	int iWidth = (int) width();              //"
	
        for (y = -iPolynomeOrder; y < iHeight + iPolynomeOrder; ++y)
        {
            for (x = -iPolynomeOrder; x < iWidth + iPolynomeOrder; ++x)
            {
                if ((x < 0 && y < 0 && -x - y < iPolynomeOrder + 2)
                    || (x < 0 && y >= iHeight && -x + y - iHeight < iPolynomeOrder + 1)
                    || (x >= iWidth && y < 0 && x - y - iWidth < iPolynomeOrder + 1)
                    || (x >= iWidth && y >= iHeight && x + y - iWidth - iHeight < iPolynomeOrder)
                    || (x < 0 && y >= 0 && y < iHeight) || (x >= iWidth  && y >= 0 && y < iHeight)
                    || (y < 0 && x >= 0 && x < iWidth ) || (y >= iHeight && x >= 0 && x < iWidth))
                {
                    QPoint position(x,y);
		    mPositions.append(position);
                }
            }
        }
    }
    else
    {
        // In the one-dimensional case, only the y coordinate and y size is used.  */

        for (y = -mPolynomeOrder; y < 0; ++y)
        {
            QPoint position(0,y);
            mPositions.append(position);
        }

        for (y = (int) height(); y < (int) height() + (int) mPolynomeOrder; ++y)
        {
            QPoint position(0,y);
            mPositions.append(position);
        }
    }

    // Allocate memory.
    
    matrix  = new double[mCoefficientNumber*mCoefficientNumber];
    vector0 = new double[mPositions.count() * mCoefficientNumber];
    vector1 = new double[mPositions.count() * mCoefficientNumber];
    
    // Calculate coefficient matrix and vectors
    
    for (iy = 0; iy < mCoefficientNumber; ++iy)
    {
        for (ix = 0; ix < mCoefficientNumber; ++ix)
            matrix [iy*mCoefficientNumber+ix] = 0.0;

        for (j = 0; j < mPositions.count(); ++j)
        {
            vector0 [iy * mPositions.count() + j] = polyTerm (iy, mPositions [j].x(), 
                    mPositions [j].y(), mPolynomeOrder);

            for (ix = 0; ix < mCoefficientNumber; ++ix)
                matrix [iy * mCoefficientNumber + ix] += (vector0 [iy * mPositions.count() + j]
                           * polyTerm (ix, mPositions [j].x(), mPositions[j].y(), mPolynomeOrder));
        }
    }

    // Invert matrix.
    
    matrixInv (matrix, mCoefficientNumber);

    // Multiply inverse matrix with vector.
    
    for (iy = 0; iy < mCoefficientNumber; ++iy)
        for (j = 0; j < mPositions.count(); ++j)
        {
            vector1 [iy * mPositions.count() + j] = 0.0;

            for (ix = 0; ix < mCoefficientNumber; ++ix)
                vector1 [iy * mPositions.count() + j] += matrix [iy * mCoefficientNumber + ix] 
                            * vector0 [ix * mPositions.count() + j];
        }

    // Store weights
    
    mWeightMatrices = new double**[mPositions.count()]; //allocate mPositions.count() matrices
    
    for (i=0; i<mPositions.count(); i++)
    {
        mWeightMatrices[i] = new double*[mHeight]; //allocate mHeight rows on each position
        for (j=0; j<mHeight; j++) 
            mWeightMatrices[i][j] = new double[mWidth]; //Allocate mWidth columns on each row
    }

    for (y = 0; y < (int) mHeight; ++y)
    {
        for (x = 0; x < (int) mWidth; ++x)
        {
            for (j = 0; j < mPositions.count(); ++j)
            {
                mWeightMatrices [j][y][x] = 0.0;

                for (iy = 0; iy < mCoefficientNumber; ++iy)
                   mWeightMatrices [j][y][x] += vector1 [iy * mPositions.count() + j] 
                                             * polyTerm (iy, x, y, mPolynomeOrder);

                mWeightMatrices [j][y][x] *= (double) mPositions.count();
            }
        }
    }
    
    delete[] vector1;
    delete[] vector0;
    delete[] matrix;
}

bool Weights::operator==(const Weights& ws) const
{
    return (mHeight==ws.height() &&
            mWidth==ws.width() &&
            mPolynomeOrder==ws.polynomeOrder() &&
            mTwoDim==ws.twoDim()
            );
}

 //Invert a quadratic matrix. 
void Weights::matrixInv (double *const a, const size_t size)
{
    double *const b = new double[size * size];
    size_t ix, iy, j;

    // Copy matrix to new location.  
    
    memcpy (b, a, sizeof (double) * size * size);

    // Set destination matrix to unit matrix. 
    
    for (iy = 0; iy < size; ++iy)
        for (ix = 0; ix < size; ++ix)
            a [iy * size + ix] = ix == iy ? 1.0 : 0.0;

    // Convert matrix to upper triangle form.  
    
    for (iy = 0; iy < size - 1; ++iy)
    {
        for (j = iy + 1; j < size; ++j)
        {
            const double factor = b [j * size + iy] / b [iy * size + iy];

            for (ix = 0; ix < size; ++ix)
            {
                b [j * size + ix] -= factor * b [iy * size + ix];
                a [j * size + ix] -= factor * a [iy * size + ix];
            }
        }
    }

    // Convert matrix to diagonal form.  
    
    for (iy = size - 1; iy > 0; --iy)
    {
        for (j = 0; j < iy; ++j)
        {
            const double factor =  b [j * size + iy] / b [iy * size + iy];

            for (ix = 0; ix < size; ++ix)
                a [j * size + ix] -= factor * a [iy * size + ix];
        }
    }

    // Convert matrix to unit matrix.
    
    for (iy = 0; iy < size; ++iy)
        for (ix = 0; ix < size; ++ix)
            a [iy * size + ix] /= b [iy * size + iy];

    delete [] b;
}

// Calculates one term of the polynomial
double Weights::polyTerm (const size_t i_coeff, const int x, const int y, const int poly_order)
{
    const size_t x_power = i_coeff / ((size_t)poly_order + 1);
    const size_t y_power = i_coeff % ((size_t)poly_order + 1);
    int result;
    size_t i;

    result = 1;

    for (i = 0; i < x_power; ++i)
        result *= x;

    for (i = 0; i < y_power; ++i)
        result *= y;

    return (double)result;
}

}  // NameSpace DigikamHotPixelsImagesPlugin