From 5ee5a17a28cfc784a3e995e0f05b2e66f5c33719 Mon Sep 17 00:00:00 2001 From: Marco Hugentobler Date: Tue, 3 Jan 2012 11:16:28 +0100 Subject: [PATCH] Detect if transparency can be ignored for paletted renderer --- src/core/raster/qgspalettedrasterrenderer.cpp | 4 +++- src/core/raster/qgsrasterrenderer.cpp | 10 ++++++++-- src/core/raster/qgsrasterrenderer.h | 2 +- src/core/raster/qgsrastertransparency.cpp | 11 +++++++++-- src/core/raster/qgsrastertransparency.h | 4 ++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/core/raster/qgspalettedrasterrenderer.cpp b/src/core/raster/qgspalettedrasterrenderer.cpp index 0e6519572958..e09957c1776c 100644 --- a/src/core/raster/qgspalettedrasterrenderer.cpp +++ b/src/core/raster/qgspalettedrasterrenderer.cpp @@ -17,6 +17,7 @@ #include "qgspalettedrasterrenderer.h" #include "qgsrastertransparency.h" +#include "qgsrasterviewport.h" #include #include @@ -61,7 +62,8 @@ void QgsPalettedRasterRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, void* rasterData; double currentOpacity = mOpacity; - bool hasTransparency = usesTransparency(); + //rendering is faster without considering user-defined transparency + bool hasTransparency = usesTransparency( viewPort->mSrcCRS, viewPort->mDestCRS ); void* transparencyData; while ( readNextRasterPart( mBandNumber, viewPort, nCols, nRows, &rasterData, topLeftCol, topLeftRow ) ) diff --git a/src/core/raster/qgsrasterrenderer.cpp b/src/core/raster/qgsrasterrenderer.cpp index c3f73f3b7c6b..876e606eaabf 100644 --- a/src/core/raster/qgsrasterrenderer.cpp +++ b/src/core/raster/qgsrasterrenderer.cpp @@ -157,9 +157,15 @@ void QgsRasterRenderer::removePartInfo( int bandNumber ) } } -bool QgsRasterRenderer::usesTransparency() const +bool QgsRasterRenderer::usesTransparency( QgsCoordinateReferenceSystem& srcSRS, QgsCoordinateReferenceSystem& dstSRS ) const { - return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty() ) || !doubleNear( mOpacity, 1.0 ) ); + //transparency is always used if on-the-fly reprojection is enabled + bool reprojectionEnabled = ( srcSRS.isValid() && dstSRS.isValid() && srcSRS != dstSRS ); + if ( !mProvider || reprojectionEnabled ) + { + return true; + } + return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty( mProvider->noDataValue() ) ) || !doubleNear( mOpacity, 1.0 ) ); } void QgsRasterRenderer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, const QImage& img, int topLeftCol, int topLeftRow, diff --git a/src/core/raster/qgsrasterrenderer.h b/src/core/raster/qgsrasterrenderer.h index 839621b7ec5c..42c565b41fcd 100644 --- a/src/core/raster/qgsrasterrenderer.h +++ b/src/core/raster/qgsrasterrenderer.h @@ -45,7 +45,7 @@ class QgsRasterRenderer virtual ~QgsRasterRenderer(); virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel ) = 0; - bool usesTransparency() const; + bool usesTransparency( QgsCoordinateReferenceSystem& srcSRS, QgsCoordinateReferenceSystem& dstSRS ) const; void setOpacity( double opacity ) { mOpacity = opacity; } double opacity() const { return mOpacity; } diff --git a/src/core/raster/qgsrastertransparency.cpp b/src/core/raster/qgsrastertransparency.cpp index c86aa3035823..b4e7861da411 100644 --- a/src/core/raster/qgsrastertransparency.cpp +++ b/src/core/raster/qgsrastertransparency.cpp @@ -18,6 +18,7 @@ email : ersts@amnh.org #include #include "qgsrastertransparency.h" +#include "qgis.h" QgsRasterTransparency::QgsRasterTransparency() { @@ -169,7 +170,13 @@ int QgsRasterTransparency::alphaValue( double theRedValue, double theGreenValue, return theGlobalTransparency; } -bool QgsRasterTransparency::isEmpty() const +bool QgsRasterTransparency::isEmpty( double nodataValue ) const { - return ( mTransparentThreeValuePixelList.isEmpty() && mTransparentSingleValuePixelList.isEmpty() ); + return ( + ( mTransparentSingleValuePixelList.isEmpty() || + ( mTransparentSingleValuePixelList.size() == 1 && doubleNear( mTransparentSingleValuePixelList.at( 0 ).pixelValue, nodataValue ) ) ) + && + ( mTransparentThreeValuePixelList.isEmpty() || + ( mTransparentThreeValuePixelList.size() == 1 && doubleNear( mTransparentThreeValuePixelList.at( 0 ).red, nodataValue ) && + doubleNear( mTransparentThreeValuePixelList.at( 0 ).green, nodataValue ) && doubleNear( mTransparentThreeValuePixelList.at( 0 ).blue, nodataValue ) ) ) ); } diff --git a/src/core/raster/qgsrastertransparency.h b/src/core/raster/qgsrastertransparency.h index e79a92de1749..966301c524be 100644 --- a/src/core/raster/qgsrastertransparency.h +++ b/src/core/raster/qgsrastertransparency.h @@ -72,8 +72,8 @@ class CORE_EXPORT QgsRasterTransparency /** \brief Return the transparency value for a RGB Pixel */ int alphaValue( double, double, double, int theGlobalTransparency = 255 ) const; - /**True if there are no entries in the pixel lists*/ - bool isEmpty() const; + /**True if there are no entries in the pixel lists except the nodata value*/ + bool isEmpty( double nodataValue ) const; private: /** \brief The list to hold transparency values for RGB layers */