17
17
18
18
#include " qgsbilinearrasterresampler.h"
19
19
#include < QImage>
20
+ #include < cmath>
20
21
21
22
QgsBilinearRasterResampler::QgsBilinearRasterResampler ()
22
23
{
@@ -33,43 +34,74 @@ void QgsBilinearRasterResampler::resample( const QImage& srcImage, QImage& dstIm
33
34
34
35
double currentSrcRow = nSrcPerDstY / 2.0 - 0.5 ;
35
36
double currentSrcCol;
37
+ int currentSrcRowInt, currentSrcColInt;
38
+ double u, v;
36
39
37
40
QRgb px1, px2, px3, px4;
38
41
39
42
for ( int i = 0 ; i < dstImage.height (); ++i )
40
43
{
41
- // avoid access to invalid rows
42
- if ( currentSrcRow < 0 )
43
- {
44
- currentSrcRow += nSrcPerDstY;
45
- }
46
-
47
44
currentSrcCol = nSrcPerDstX / 2.0 - 0.5 ;
45
+ currentSrcRowInt = floor ( currentSrcRow );
46
+ v = currentSrcRow - currentSrcRowInt;
47
+
48
48
for ( int j = 0 ; j < dstImage.width (); ++j )
49
49
{
50
- double u = currentSrcCol - ( int )currentSrcCol;
51
- double v = currentSrcRow - ( int )currentSrcRow;
52
- if ( currentSrcCol > srcImage.width () - 2 )
53
- {
54
- // resample in one direction only
55
- px1 = srcImage.pixel ( currentSrcCol, currentSrcRow );
56
- px2 = srcImage.pixel ( currentSrcCol, currentSrcRow + 1 );
57
- dstImage.setPixel ( j, i, resampleColorValue ( v, px1, px2 ) );
58
- currentSrcCol += nSrcPerDstX;
59
- continue ;
60
- }
61
- else if ( currentSrcRow > srcImage.height () - 2 )
50
+ currentSrcColInt = floor ( currentSrcCol );
51
+ u = currentSrcCol - currentSrcColInt;
52
+
53
+ // handle eight edge-cases
54
+ if ( currentSrcRowInt < 0 || currentSrcRowInt >= (srcImage.height () - 1 ) || currentSrcColInt < 0 || currentSrcColInt >= (srcImage.width () - 1 ) )
62
55
{
63
- px1 = srcImage.pixel ( currentSrcCol, currentSrcRow );
64
- px2 = srcImage.pixel ( currentSrcCol + 1 , currentSrcRow );
65
- dstImage.setPixel ( j, i, resampleColorValue ( u, px1, px2 ) );
56
+ // pixels at the border of the source image needs to be handled in a special way
57
+ if ( currentSrcRowInt < 0 && currentSrcColInt < 0 )
58
+ {
59
+ dstImage.setPixel ( j, i, srcImage.pixel (0 , 0 ) );
60
+ }
61
+ else if ( currentSrcRowInt < 0 && currentSrcColInt >= ( srcImage.width () - 1 ) )
62
+ {
63
+ dstImage.setPixel ( j, i, srcImage.pixel ( srcImage.width () - 1 , 0 ) );
64
+ }
65
+ else if ( currentSrcRowInt >= (srcImage.height () - 1 ) && currentSrcColInt >= ( srcImage.width () - 1 ) )
66
+ {
67
+ dstImage.setPixel ( j, i, srcImage.pixel ( srcImage.width () - 1 , srcImage.height () - 1 ) );
68
+ }
69
+ else if ( currentSrcRowInt >= (srcImage.height () - 1 ) && currentSrcColInt < 0 )
70
+ {
71
+ dstImage.setPixel ( j, i, srcImage.pixel ( 0 , srcImage.height () - 1 ) );
72
+ }
73
+ else if ( currentSrcRowInt < 0 )
74
+ {
75
+ px1 = srcImage.pixel ( currentSrcColInt, 0 );
76
+ px2 = srcImage.pixel ( currentSrcColInt + 1 , 0 );
77
+ dstImage.setPixel ( j, i, resampleColorValue ( u, px1, px2 ) );
78
+ }
79
+ else if ( currentSrcRowInt >= (srcImage.height () - 1 ) )
80
+ {
81
+ px1 = srcImage.pixel ( currentSrcColInt, srcImage.height () - 1 );
82
+ px2 = srcImage.pixel ( currentSrcColInt + 1 , srcImage.height () - 1 );
83
+ dstImage.setPixel ( j, i, resampleColorValue ( u, px1, px2 ) );
84
+ }
85
+ else if ( currentSrcColInt < 0 )
86
+ {
87
+ px1 = srcImage.pixel ( 0 , currentSrcRowInt );
88
+ px2 = srcImage.pixel ( 0 , currentSrcRowInt + 1 );
89
+ dstImage.setPixel ( j, i, resampleColorValue ( v, px1, px2 ) );
90
+ }
91
+ else if ( currentSrcColInt >= ( srcImage.width () - 1 ) )
92
+ {
93
+ px1 = srcImage.pixel ( srcImage.width () - 1 , currentSrcRowInt );
94
+ px2 = srcImage.pixel ( srcImage.width () - 1 , currentSrcRowInt + 1 );
95
+ dstImage.setPixel ( j, i, resampleColorValue ( v, px1, px2 ) );
96
+ }
66
97
currentSrcCol += nSrcPerDstX;
67
98
continue ;
68
99
}
69
- px1 = srcImage.pixel ( currentSrcCol, currentSrcRow );
70
- px2 = srcImage.pixel ( currentSrcCol + 1 , currentSrcRow );
71
- px3 = srcImage.pixel ( currentSrcCol + 1 , currentSrcRow + 1 );
72
- px4 = srcImage.pixel ( currentSrcCol, currentSrcRow + 1 );
100
+
101
+ px1 = srcImage.pixel ( currentSrcColInt, currentSrcRowInt );
102
+ px2 = srcImage.pixel ( currentSrcColInt + 1 , currentSrcRowInt );
103
+ px3 = srcImage.pixel ( currentSrcColInt + 1 , currentSrcRowInt + 1 );
104
+ px4 = srcImage.pixel ( currentSrcColInt, currentSrcRowInt + 1 );
73
105
dstImage.setPixel ( j, i, resampleColorValue ( u, v, px1, px2, px3, px4 ) );
74
106
currentSrcCol += nSrcPerDstX;
75
107
}
0 commit comments