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
303
304
305
306
307
308
309
310
311
312
313
314
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2004-12-27
* Description : acess pixels method for lens distortion algorithm.
*
* Copyright (C) 2004-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
* Copyright (C) 2006-2007 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* 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>
#include <cmath>
#include <cstdlib>
// Local includes.
#include "ddebug.h"
#include "pixelaccess.h"
namespace DigikamLensDistortionImagesPlugin
{
PixelAccess::PixelAccess(Digikam::DImg *srcImage)
{
m_image = srcImage;
m_width = PixelAccessWidth;
m_height = PixelAccessHeight;
m_depth = m_image->bytesDepth();
m_imageWidth = m_image->width();
m_imageHeight = m_image->height();
m_sixteenBit = m_image->sixteenBit();
for ( int i = 0 ; i < PixelAccessRegions ; i++ )
{
m_buffer[i] = new Digikam::DImg(m_image->copy(0, 0, m_width, m_height));
m_tileMinX[i] = 1;
m_tileMaxX[i] = m_width - 2;
m_tileMinY[i] = 1;
m_tileMaxY[i] = m_height - 2;
}
}
PixelAccess::~PixelAccess()
{
for( int i = 0 ; i < PixelAccessRegions ; i++ )
delete m_buffer[i];
}
uchar* PixelAccess::pixelAccessAddress(int i, int j)
{
return m_buffer[0]->bits() + m_depth * (m_width * (j + 1 - m_tileMinY[0]) + (i + 1 - m_tileMinX[0]));
}
// Swap region[n] with region[0].
void PixelAccess::pixelAccessSelectRegion(int n)
{
Digikam::DImg *temp;
int a, b, c, d;
int i;
temp = m_buffer[n];
a = m_tileMinX[n];
b = m_tileMaxX[n];
c = m_tileMinY[n];
d = m_tileMaxY[n];
for( i = n ; i > 0 ; i--)
{
m_buffer[i] = m_buffer[i-1];
m_tileMinX[i] = m_tileMinX[i-1];
m_tileMaxX[i] = m_tileMaxX[i-1];
m_tileMinY[i] = m_tileMinY[i-1];
m_tileMaxY[i] = m_tileMaxY[i-1];
}
m_buffer[0] = temp;
m_tileMinX[0] = a;
m_tileMaxX[0] = b;
m_tileMinY[0] = c;
m_tileMaxY[0] = d;
}
// Buffer[0] is cleared, should start at [i, j], fill rows that overlap image.
void PixelAccess::pixelAccessDoEdge(int i, int j)
{
int lineStart, lineEnd;
int rowStart, rowEnd;
int lineWidth;
uchar* line;
lineStart = i;
if (lineStart < 0) lineStart = 0;
lineEnd = i + m_width;
if (lineEnd > m_imageWidth) lineEnd = m_imageWidth;
lineWidth = lineEnd - lineStart;
if( lineStart >= lineEnd )
return;
rowStart = j;
if (rowStart < 0) rowStart = 0;
rowEnd = j + m_height;
if (rowEnd > m_imageHeight) rowEnd = m_imageHeight;
for( int y = rowStart ; y < rowEnd ; y++ )
{
line = pixelAccessAddress(lineStart, y);
memcpy(line, m_image->scanLine(y) + lineStart * m_depth, lineWidth * m_depth);
}
}
// Moves buffer[0] so that [x, y] is inside it.
void PixelAccess::pixelAccessReposition(int xInt, int yInt)
{
int newStartX = xInt - PixelAccessXOffset;
int newStartY = yInt - PixelAccessYOffset;
m_tileMinX[0] = newStartX + 1;
m_tileMaxX[0] = newStartX + m_width - 2;
m_tileMinY[0] = newStartY + 1;
m_tileMaxY[0] = newStartY + m_height - 2;
if ( (newStartX < 0) || ((newStartX + m_width) >= m_imageWidth) ||
(newStartY < 0) || ((newStartY + m_height) >= m_imageHeight) )
{
// some data is off edge of image
m_buffer[0]->fill(Digikam::DColor(0,0,0,0, m_sixteenBit));
// This could probably be done by bitBltImage but I did not figure out how,
// so leave the working code here. And no, it is not this:
//m_buffer[0]->bitBltImage(m_image, newStartX, newStartY, m_width, m_height, 0, 0);
if ( ((newStartX + m_width) < 0) || (newStartX >= m_imageWidth) ||
((newStartY + m_height) < 0) || (newStartY >= m_imageHeight) )
{
// totally outside, just leave it.
}
else
{
pixelAccessDoEdge(newStartX, newStartY);
}
}
else
{
m_buffer[0]->bitBltImage(m_image, newStartX, newStartY, m_width, m_height, 0, 0);
}
}
void PixelAccess::pixelAccessGetCubic(double srcX, double srcY, double brighten, uchar* dst)
{
int xInt, yInt;
double dx, dy;
uchar *corner;
xInt = (int)floor(srcX);
dx = srcX - xInt;
yInt = (int)floor(srcY);
dy = srcY - yInt;
// We need 4x4 pixels, xInt-1 to xInt+2 horz, yInt-1 to yInt+2 vert
// they're probably in the last place we looked...
if ((xInt >= m_tileMinX[0]) && (xInt < m_tileMaxX[0]) &&
(yInt >= m_tileMinY[0]) && (yInt < m_tileMaxY[0]) )
{
corner = pixelAccessAddress(xInt - 1, yInt - 1);
cubicInterpolate(corner, m_depth * m_width, dst, m_sixteenBit, dx, dy, brighten);
return;
}
// Or maybe it was a while back...
for ( int i = 1 ; i < PixelAccessRegions ; i++)
{
if ((xInt >= m_tileMinX[i]) && (xInt < m_tileMaxX[i]) &&
(yInt >= m_tileMinY[i]) && (yInt < m_tileMaxY[i]) )
{
// Check here first next time
pixelAccessSelectRegion(i);
corner = pixelAccessAddress(xInt - 1, yInt - 1);
cubicInterpolate(corner, m_depth * m_width, dst, m_sixteenBit, dx, dy, brighten);
return;
}
}
// Nope, recycle an old region.
pixelAccessSelectRegion(PixelAccessRegions - 1);
pixelAccessReposition(xInt, yInt);
corner = pixelAccessAddress(xInt - 1, yInt - 1);
cubicInterpolate(corner, m_depth * m_width, dst, m_sixteenBit, dx, dy, brighten);
}
/*
* Catmull-Rom cubic interpolation
*
* equally spaced points p0, p1, p2, p3
* interpolate 0 <= u < 1 between p1 and p2
*
* (1 u u^2 u^3) ( 0.0 1.0 0.0 0.0 ) (p0)
* ( -0.5 0.0 0.5 0.0 ) (p1)
* ( 1.0 -2.5 2.0 -0.5 ) (p2)
* ( -0.5 1.5 -1.5 0.5 ) (p3)
*
*/
void PixelAccess::cubicInterpolate(uchar* src, int rowStride, uchar* dst,
bool sixteenBit, double dx, double dy, double brighten)
{
float um1, u, up1, up2;
float vm1, v, vp1, vp2;
int c;
const int numberOfComponents = 4;
float verts[4 * numberOfComponents];
um1 = ((-0.5 * dx + 1.0) * dx - 0.5) * dx;
u = (1.5 * dx - 2.5) * dx * dx + 1.0;
up1 = ((-1.5 * dx + 2.0) * dx + 0.5) * dx;
up2 = (0.5 * dx - 0.5) * dx * dx;
vm1 = ((-0.5 * dy + 1.0) * dy - 0.5) * dy;
v = (1.5 * dy - 2.5) * dy * dy + 1.0;
vp1 = ((-1.5 * dy + 2.0) * dy + 0.5) * dy;
vp2 = (0.5 * dy - 0.5) * dy * dy;
if (sixteenBit)
{
unsigned short *src16 = (unsigned short *)src;
unsigned short *dst16 = (unsigned short *)dst;
// for each component, read the values of 4 pixels into array
for (c = 0 ; c < 4 * numberOfComponents ; c++)
{
verts[c] = vm1 * src16[c] + v * src16[c+rowStride] + vp1 * src16[c+rowStride*2] + vp2 * src16[c+rowStride*3];
}
// for each component, compute resulting value from array
for (c = 0 ; c < numberOfComponents ; c++)
{
float result;
result = um1 * verts[c] + u * verts[c+numberOfComponents]
+ up1 * verts[c+numberOfComponents*2] + up2 * verts[c+numberOfComponents*3];
result *= brighten;
if (result < 0.0)
{
dst16[c] = 0;
}
else if (result > 65535.0)
{
dst16[c] = 65535;
}
else
{
dst16[c] = (uint)result;
}
}
}
else
{
for (c = 0 ; c < 4 * numberOfComponents ; c++)
{
verts[c] = vm1 * src[c] + v * src[c+rowStride] + vp1 * src[c+rowStride*2] + vp2 * src[c+rowStride*3];
}
for (c = 0 ; c < numberOfComponents ; c++)
{
float result;
result = um1 * verts[c] + u * verts[c+numberOfComponents]
+ up1 * verts[c+numberOfComponents*2] + up2 * verts[c+numberOfComponents*3];
result *= brighten;
if (result < 0.0)
{
dst[c] = 0;
}
else if (result > 255.0)
{
dst[c] = 255;
}
else
{
dst[c] = (uint)result;
}
}
}
}
} // NameSpace DigikamLensDistortionImagesPlugin
|