@@ -29,99 +29,5 @@ QgsBilinearRasterResampler::~QgsBilinearRasterResampler()
29
29
30
30
void QgsBilinearRasterResampler::resample ( const QImage& srcImage, QImage& dstImage )
31
31
{
32
- double nSrcPerDstX = ( double ) srcImage.width () / ( double ) dstImage.width ();
33
- double nSrcPerDstY = ( double ) srcImage.height () / ( double ) dstImage.height ();
34
-
35
- double currentSrcRow = nSrcPerDstY / 2.0 - 0.5 ;
36
- double currentSrcCol;
37
- int currentSrcRowInt, currentSrcColInt;
38
- double u, v;
39
-
40
- QRgb px1, px2, px3, px4;
41
-
42
- for ( int i = 0 ; i < dstImage.height (); ++i )
43
- {
44
- currentSrcCol = nSrcPerDstX / 2.0 - 0.5 ;
45
- currentSrcRowInt = floor ( currentSrcRow );
46
- v = currentSrcRow - currentSrcRowInt;
47
-
48
- for ( int j = 0 ; j < dstImage.width (); ++j )
49
- {
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 ) )
55
- {
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
- }
97
- currentSrcCol += nSrcPerDstX;
98
- continue ;
99
- }
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 );
105
- dstImage.setPixel ( j, i, resampleColorValue ( u, v, px1, px2, px3, px4 ) );
106
- currentSrcCol += nSrcPerDstX;
107
- }
108
- currentSrcRow += nSrcPerDstY;
109
- }
110
- }
111
-
112
- QRgb QgsBilinearRasterResampler::resampleColorValue ( double u, double v, QRgb col1, QRgb col2, QRgb col3, QRgb col4 ) const
113
- {
114
- int red = bilinearInterpolation ( u, v, qRed ( col1 ), qRed ( col2 ), qRed ( col3 ), qRed ( col4 ) );
115
- int green = bilinearInterpolation ( u, v, qGreen ( col1 ), qGreen ( col2 ), qGreen ( col3 ), qGreen ( col4 ) );
116
- int blue = bilinearInterpolation ( u, v, qBlue ( col1 ), qBlue ( col2 ), qBlue ( col3 ), qBlue ( col4 ) );
117
- int alpha = bilinearInterpolation ( u, v, qAlpha ( col1 ), qAlpha ( col2 ), qAlpha ( col3 ), qAlpha ( col4 ) );
118
- return qRgba ( red, green, blue, alpha );
119
- }
120
-
121
- QRgb QgsBilinearRasterResampler::resampleColorValue ( double u, QRgb col1, QRgb col2 ) const
122
- {
123
- return qRgba ( qRed ( col1 ) * ( 1 - u ) + qRed ( col2 ) * u,
124
- qGreen ( col1 ) * ( 1 - u ) + qGreen ( col2 ) * u,
125
- qBlue ( col1 ) * ( 1 - u ) + qBlue ( col2 ) * u,
126
- qAlpha ( col1 ) * ( 1 - u ) + qAlpha ( col2 ) * u );
32
+ dstImage = srcImage.scaled ( dstImage.width (), dstImage.height (), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
127
33
}
0 commit comments