Skip to content

Commit

Permalink
Faster draw for multiband color in case there is no transparency, str…
Browse files Browse the repository at this point in the history
…ech, inversion
  • Loading branch information
mhugent committed Jan 28, 2012
1 parent 91c37fb commit 3b36620
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
25 changes: 22 additions & 3 deletions src/core/raster/qgsmultibandcolorrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgsmultibandcolorrenderer.h"
#include "qgscontrastenhancement.h"
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include <QImage>
#include <QSet>

Expand All @@ -41,6 +42,13 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
return;
}

//In some (common) cases, we can simplify the drawing loop considerably and save render time
bool fastDraw = (
!usesTransparency( viewPort->mSrcCRS, viewPort->mDestCRS )
&& mRedBand > 0 && mGreenBand > 0 && mBlueBand > 0
&& mAlphaBand < 1 && !mRedContrastEnhancement && !mGreenContrastEnhancement && !mBlueContrastEnhancement
&& !mInvertColor );

QgsRasterDataProvider::DataType redType;
if ( mRedBand > 0 )
{
Expand Down Expand Up @@ -155,15 +163,26 @@ void QgsMultiBandColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort,
imageScanLine = ( QRgb* )( img.scanLine( i ) );
for ( int j = 0; j < nRasterCols; ++j )
{
if ( mRedBand )

if ( fastDraw ) //fast rendering if no transparency, stretching, color inversion, etc.
{
redVal = readValue( redData, redType, currentRasterPos );
greenVal = readValue( greenData, greenType, currentRasterPos );
blueVal = readValue( blueData, blueType, currentRasterPos );
imageScanLine[j] = qRgba( redVal, greenVal, blueVal, 255 );
++currentRasterPos;
continue;
}

if ( mRedBand > 0 )
{
redVal = readValue( redData, redType, currentRasterPos );
}
if ( mGreenBand )
if ( mGreenBand > 0 )
{
greenVal = readValue( greenData, greenType, currentRasterPos );
}
if ( mBlueBand )
if ( mBlueBand > 0 )
{
blueVal = readValue( blueData, blueType, currentRasterPos );
}
Expand Down
15 changes: 12 additions & 3 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,21 +693,30 @@ void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawin
if ( mRedBandName != TRSTRING_NOT_SET )
{
red = bandNumber( mRedBandName );
redEnhancement = contrastEnhancement( red );
if ( contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement )
{
redEnhancement = contrastEnhancement( red );
}
}
int green = -1;
QgsContrastEnhancement* greenEnhancement = 0;
if ( mGreenBandName != TRSTRING_NOT_SET )
{
green = bandNumber( mGreenBandName );
greenEnhancement = contrastEnhancement( green );
if ( contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement )
{
greenEnhancement = contrastEnhancement( green );
}
}
int blue = -1;
QgsContrastEnhancement* blueEnhancement = 0;
if ( mBlueBandName != TRSTRING_NOT_SET )
{
blue = bandNumber( mBlueBandName );
blueEnhancement = contrastEnhancement( blue );
if ( contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement )
{
blueEnhancement = contrastEnhancement( blue );
}
}
renderer = new QgsMultiBandColorRenderer( mDataProvider, red, green, blue,
redEnhancement, greenEnhancement, blueEnhancement );
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrastertransparency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ bool QgsRasterTransparency::isEmpty( double nodataValue ) const
( mTransparentSingleValuePixelList.size() == 1 && doubleNear( mTransparentSingleValuePixelList.at( 0 ).pixelValue, nodataValue ) ) )
&&
( mTransparentThreeValuePixelList.isEmpty() ||
( mTransparentThreeValuePixelList.size() == 1 && doubleNear( mTransparentThreeValuePixelList.at( 0 ).red, nodataValue ) &&
( mTransparentThreeValuePixelList.size() < 4 && doubleNear( mTransparentThreeValuePixelList.at( 0 ).red, nodataValue ) &&
doubleNear( mTransparentThreeValuePixelList.at( 0 ).green, nodataValue ) && doubleNear( mTransparentThreeValuePixelList.at( 0 ).blue, nodataValue ) ) ) );
}

0 comments on commit 3b36620

Please sign in to comment.