Skip to content

Commit

Permalink
Detect if transparency can be ignored for paletted renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jan 3, 2012
1 parent 8dc45e4 commit 5ee5a17
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/core/raster/qgspalettedrasterrenderer.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgspalettedrasterrenderer.h"
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include <QColor>
#include <QImage>

Expand Down Expand Up @@ -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 ) )
Expand Down
10 changes: 8 additions & 2 deletions src/core/raster/qgsrasterrenderer.cpp
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterrenderer.h
Expand Up @@ -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; }
Expand Down
11 changes: 9 additions & 2 deletions src/core/raster/qgsrastertransparency.cpp
Expand Up @@ -18,6 +18,7 @@ email : ersts@amnh.org
#include <QList>

#include "qgsrastertransparency.h"
#include "qgis.h"

QgsRasterTransparency::QgsRasterTransparency()
{
Expand Down Expand Up @@ -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 ) ) ) );
}
4 changes: 2 additions & 2 deletions src/core/raster/qgsrastertransparency.h
Expand Up @@ -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 */
Expand Down

0 comments on commit 5ee5a17

Please sign in to comment.