Skip to content

Commit 28313b6

Browse files
committedJan 7, 2012
Use QImage method for bilinear resampling
1 parent ea9a2a8 commit 28313b6

File tree

2 files changed

+1
-105
lines changed

2 files changed

+1
-105
lines changed
 

‎src/core/raster/qgsbilinearrasterresampler.cpp

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -29,99 +29,5 @@ QgsBilinearRasterResampler::~QgsBilinearRasterResampler()
2929

3030
void QgsBilinearRasterResampler::resample( const QImage& srcImage, QImage& dstImage )
3131
{
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 );
12733
}

‎src/core/raster/qgsbilinearrasterresampler.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ class QgsBilinearRasterResampler: public QgsRasterResampler
2929

3030
void resample( const QImage& srcImage, QImage& dstImage );
3131
QString type() const { return "bilinear"; }
32-
33-
private:
34-
QRgb resampleColorValue( double u, double v, QRgb col1, QRgb col2, QRgb col3, QRgb col4 ) const;
35-
QRgb resampleColorValue( double u, QRgb col1, QRgb col2 ) const;
36-
inline double bilinearInterpolation( double u, double v, int val1, int val2, int val3, int val4 ) const;
3732
};
3833

39-
double QgsBilinearRasterResampler::bilinearInterpolation( double u, double v, int val1, int val2, int val3, int val4 ) const
40-
{
41-
return ( val1 * ( 1 - u ) * ( 1 - v ) + val2 * ( 1 - v ) * u + val4 * ( 1 - u ) * v + val3 * u * v );
42-
}
43-
4434
#endif // QGSBILINEARRASTERRESAMPLER_H

0 commit comments

Comments
 (0)
Please sign in to comment.