Skip to content

Commit b33f24f

Browse files
committedDec 29, 2011
Use different oversampling factors in x and y directions to problems with resampled image size
1 parent 242134c commit b33f24f

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed
 

‎src/core/raster/qgsmultibandcolorrenderer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
4949
transparencyType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mAlphaBand );
5050
}
5151

52-
double oversampling;
52+
double oversamplingX, oversamplingY;
5353
QSet<int> bands;
5454
bands << mRedBand << mGreenBand << mBlueBand;
5555
if ( mAlphaBand > 0 )
@@ -63,7 +63,7 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
6363
for ( ; bandIt != bands.constEnd(); ++bandIt )
6464
{
6565
bandData.insert( *bandIt, defaultPointer );
66-
startRasterRead( *bandIt, viewPort, theQgsMapToPixel, oversampling );
66+
startRasterRead( *bandIt, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
6767
}
6868

6969
void* redData;
@@ -135,11 +135,11 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
135135
//draw image
136136
//top left position in device coords
137137
QPointF tlPoint = QPointF( viewPort->topLeftPoint.x(), viewPort->topLeftPoint.y() );
138-
tlPoint += QPointF( topLeftCol / oversampling, topLeftRow / oversampling );
138+
tlPoint += QPointF( topLeftCol / oversamplingX, topLeftRow / oversamplingY );
139139

140140
if ( mResampler ) //resample to output resolution
141141
{
142-
QImage dstImg( nCols / oversampling + 0.5, nRows / oversampling + 0.5, QImage::Format_ARGB32_Premultiplied );
142+
QImage dstImg( nCols / oversamplingX, nRows / oversamplingY, QImage::Format_ARGB32_Premultiplied );
143143
mResampler->resample( img, dstImg );
144144
p->drawImage( tlPoint, dstImg );
145145
}

‎src/core/raster/qgspalettedrasterrenderer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ void QgsPalettedRasterRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
4343
return;
4444
}
4545

46-
double oversampling;
46+
double oversamplingX, oversamplingY;
4747
QgsRasterDataProvider::DataType transparencyType;
4848
if ( mAlphaBand > 0 )
4949
{
5050
transparencyType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mAlphaBand );
5151
}
52-
startRasterRead( mBandNumber, viewPort, theQgsMapToPixel, oversampling );
52+
startRasterRead( mBandNumber, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
5353

5454
//Read alpha band if necessary
5555
if ( mAlphaBand > 0 && mAlphaBand != mBandNumber )
5656
{
57-
startRasterRead( mAlphaBand, viewPort, theQgsMapToPixel, oversampling );
57+
startRasterRead( mAlphaBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
5858
}
5959

6060
int nCols = 0;
@@ -116,12 +116,12 @@ void QgsPalettedRasterRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
116116

117117
//top left position in device coords
118118
QPointF tlPoint = QPointF( viewPort->topLeftPoint.x(), viewPort->topLeftPoint.y() );
119-
tlPoint += QPointF( topLeftCol / oversampling, topLeftRow / oversampling );
119+
tlPoint += QPointF( topLeftCol / oversamplingX, topLeftRow / oversamplingY );
120120

121121
//draw image
122122
if ( mResampler ) //resample to output resolution
123123
{
124-
QImage dstImg( nCols / oversampling, nRows / oversampling, QImage::Format_ARGB32_Premultiplied );
124+
QImage dstImg( nCols / oversamplingX, nRows / oversamplingY, QImage::Format_ARGB32_Premultiplied );
125125
mResampler->resample( img, dstImg );
126126
p->drawImage( tlPoint, dstImg );
127127
}

‎src/core/raster/qgsrasterrenderer.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "qgsmaptopixel.h"
2222

2323
QgsRasterRenderer::QgsRasterRenderer( QgsRasterDataProvider* provider, QgsRasterResampler* resampler ): mProvider( provider ), mResampler( resampler ),
24-
mOpacity( 1.0 ), mRasterTransparency( 0 ), mAlphaBand( -1 ), mInvertColor( false )
24+
mOpacity( 1.0 ), mRasterTransparency( 0 ), mAlphaBand( -1 ), mInvertColor( false )
2525
{
2626
}
2727

@@ -35,9 +35,8 @@ QgsRasterRenderer::~QgsRasterRenderer()
3535
}
3636
}
3737

38-
void QgsRasterRenderer::startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double& oversampling )
38+
void QgsRasterRenderer::startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double& oversamplingX, double& oversamplingY )
3939
{
40-
oversampling = 1.0; //default (e.g. for nearest neighbour)
4140
if ( !viewPort || !mapToPixel || !mProvider )
4241
{
4342
return;
@@ -47,6 +46,7 @@ void QgsRasterRenderer::startRasterRead( int bandNumber, QgsRasterViewPort* view
4746
removePartInfo( bandNumber );
4847

4948
//calculate oversampling factor
49+
double oversampling = 1.0; //approximate global oversampling factor
5050
if ( mResampler )
5151
{
5252
QgsRectangle providerExtent = mProvider->extent();
@@ -63,11 +63,16 @@ void QgsRasterRenderer::startRasterRead( int bandNumber, QgsRasterViewPort* view
6363
RasterPartInfo pInfo;
6464
pInfo.nCols = viewPort->drawableAreaXDim * oversampling;
6565
pInfo.nRows = viewPort->drawableAreaYDim * oversampling;
66+
67+
//effective oversampling factors are different to global one because of rounding
68+
oversamplingX = ( double )pInfo.nCols / viewPort->drawableAreaXDim;
69+
oversamplingY = ( double )pInfo.nRows / viewPort->drawableAreaYDim;
70+
6671
int totalMemoryUsage = pInfo.nCols * pInfo.nRows * mProvider->dataTypeSize( bandNumber );
67-
int parts = totalMemoryUsage / 100000000 /*100000*/ + 1;
72+
int parts = totalMemoryUsage / 100000 + 1;
6873
pInfo.nPartsPerDimension = sqrt( parts );
69-
pInfo.nColsPerPart = pInfo.nCols / pInfo.nPartsPerDimension;
70-
pInfo.nRowsPerPart = pInfo.nRows / pInfo.nPartsPerDimension;
74+
pInfo.nColsPerPart = floor( pInfo.nCols / pInfo.nPartsPerDimension / oversamplingX ) * oversamplingX;
75+
pInfo.nRowsPerPart = floor( pInfo.nRows / pInfo.nPartsPerDimension / oversamplingY ) * oversamplingY;
7176
pInfo.currentCol = 0;
7277
pInfo.currentRow = 0;
7378
pInfo.data = 0;

‎src/core/raster/qgsrasterrenderer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ class QgsRasterRenderer
5757
void setAlphaBand( int band ) { mAlphaBand = band; }
5858
int alphaBand() const { return mAlphaBand; }
5959

60-
void setInvertColor( bool invert ){ mInvertColor = invert; }
60+
void setInvertColor( bool invert ) { mInvertColor = invert; }
6161
bool invertColor() const { return mInvertColor; }
6262

6363
protected:
6464
inline double readValue( void *data, QgsRasterDataProvider::DataType type, int index );
6565

66-
void startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double& oversampling );
66+
void startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double& oversamplingX, double& oversamplingY );
6767
bool readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort, int& nCols, int& nRows, void** rasterData, int& topLeftCol, int& topLeftRow );
6868
void stopRasterRead( int bandNumber );
6969

0 commit comments

Comments
 (0)
Please sign in to comment.