Skip to content

Commit

Permalink
Make raster engine safer with memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 17, 2017
1 parent 68ec95d commit 2dbb128
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 197 deletions.
18 changes: 7 additions & 11 deletions src/core/raster/qgsbrightnesscontrastfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,30 @@ QgsRasterBlock *QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle co
Q_UNUSED( bandNo );
QgsDebugMsgLevel( QString( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );

QgsRasterBlock *outputBlock = new QgsRasterBlock();
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
if ( !mInput )
{
return outputBlock;
return outputBlock.release();
}

// At this moment we know that we read rendered image
int bandNumber = 1;
QgsRasterBlock *inputBlock = mInput->block( bandNumber, extent, width, height, feedback );
std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, extent, width, height, feedback ) );
if ( !inputBlock || inputBlock->isEmpty() )
{
QgsDebugMsg( "No raster data!" );
delete inputBlock;
return outputBlock;
return outputBlock.release();
}

if ( mBrightness == 0 && mContrast == 0 )
{
QgsDebugMsgLevel( "No brightness changes.", 4 );
delete outputBlock;
return inputBlock;
return inputBlock.release();
}

if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
{
delete inputBlock;
return outputBlock;
return outputBlock.release();
}

// adjust image
Expand Down Expand Up @@ -164,8 +161,7 @@ QgsRasterBlock *QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle co
outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
}

delete inputBlock;
return outputBlock;
return outputBlock.release();
}

int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int alpha, int brightness, double contrastFactor ) const
Expand Down
22 changes: 7 additions & 15 deletions src/core/raster/qgscontrastenhancement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class originally created circa 2004 by T.Sutton, Gary E.Sherman, Steve Halasz

QgsContrastEnhancement::QgsContrastEnhancement( Qgis::DataType dataType )
: mContrastEnhancementAlgorithm( NoEnhancement )
, mContrastEnhancementFunction( nullptr )
, mEnhancementDirty( false )
, mLookupTable( nullptr )
, mRasterDataType( dataType )
Expand All @@ -42,7 +41,7 @@ QgsContrastEnhancement::QgsContrastEnhancement( Qgis::DataType dataType )

mLookupTableOffset = mMinimumValue * -1;

mContrastEnhancementFunction = new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue );
mContrastEnhancementFunction.reset( new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue ) );

//If the data type is larger than 16-bit do not generate a lookup table
if ( mRasterDataTypeRange <= 65535.0 )
Expand All @@ -53,8 +52,7 @@ QgsContrastEnhancement::QgsContrastEnhancement( Qgis::DataType dataType )
}

QgsContrastEnhancement::QgsContrastEnhancement( const QgsContrastEnhancement &ce )
: mContrastEnhancementFunction( nullptr )
, mEnhancementDirty( true )
: mEnhancementDirty( true )
, mLookupTable( nullptr )
, mMinimumValue( ce.mMinimumValue )
, mMaximumValue( ce.mMaximumValue )
Expand All @@ -76,7 +74,6 @@ QgsContrastEnhancement::QgsContrastEnhancement( const QgsContrastEnhancement &ce
QgsContrastEnhancement::~QgsContrastEnhancement()
{
delete [] mLookupTable;
delete mContrastEnhancementFunction;
}
/*
*
Expand Down Expand Up @@ -249,23 +246,19 @@ void QgsContrastEnhancement::setContrastEnhancementAlgorithm( ContrastEnhancemen
switch ( algorithm )
{
case StretchToMinimumMaximum :
delete mContrastEnhancementFunction;
mContrastEnhancementFunction = new QgsLinearMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue );
mContrastEnhancementFunction.reset( new QgsLinearMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue ) );
break;
case StretchAndClipToMinimumMaximum :
delete mContrastEnhancementFunction;
mContrastEnhancementFunction = new QgsLinearMinMaxEnhancementWithClip( mRasterDataType, mMinimumValue, mMaximumValue );
mContrastEnhancementFunction.reset( new QgsLinearMinMaxEnhancementWithClip( mRasterDataType, mMinimumValue, mMaximumValue ) );
break;
case ClipToMinimumMaximum :
delete mContrastEnhancementFunction;
mContrastEnhancementFunction = new QgsClipToMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue );
mContrastEnhancementFunction.reset( new QgsClipToMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue ) );
break;
case UserDefinedEnhancement :
//Do nothing
break;
default:
delete mContrastEnhancementFunction;
mContrastEnhancementFunction = new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue );
mContrastEnhancementFunction.reset( new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue ) );
break;
}

Expand All @@ -289,8 +282,7 @@ void QgsContrastEnhancement::setContrastEnhancementFunction( QgsContrastEnhancem

if ( function )
{
delete mContrastEnhancementFunction;
mContrastEnhancementFunction = function;
mContrastEnhancementFunction.reset( function );
mContrastEnhancementAlgorithm = UserDefinedEnhancement;
generateLookupTable();
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/raster/qgscontrastenhancement.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class originally created circa 2004 by T.Sutton, Gary E.Sherman, Steve Halasz

#include "qgis.h"
#include "qgsraster.h"
#include <memory>

class QgsContrastEnhancementFunction;
class QDomDocument;
Expand Down Expand Up @@ -120,7 +121,7 @@ class CORE_EXPORT QgsContrastEnhancement
ContrastEnhancementAlgorithm mContrastEnhancementAlgorithm;

//! \brief Pointer to the contrast enhancement function
QgsContrastEnhancementFunction *mContrastEnhancementFunction = nullptr;
std::unique_ptr< QgsContrastEnhancementFunction > mContrastEnhancementFunction;

//! \brief Flag indicating if the lookup table needs to be regenerated
bool mEnhancementDirty;
Expand Down
33 changes: 10 additions & 23 deletions src/core/raster/qgshillshaderenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "qgsrasterinterface.h"
#include "qgsrasterblock.h"
#include "qgsrectangle.h"

#include <memory>

QgsHillshadeRenderer::QgsHillshadeRenderer( QgsRasterInterface *input, int band, double lightAzimuth, double lightAngle ):
QgsRasterRenderer( input, QStringLiteral( "hillshade" ) )
Expand Down Expand Up @@ -86,34 +86,30 @@ void QgsHillshadeRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem
QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback )
{
Q_UNUSED( bandNo );
QgsRasterBlock *outputBlock = new QgsRasterBlock();
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
if ( !mInput )
{
QgsDebugMsg( "No input raster!" );
return outputBlock;
return outputBlock.release();
}

QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height, feedback );
std::shared_ptr< QgsRasterBlock > inputBlock( mInput->block( mBand, extent, width, height, feedback ) );

if ( !inputBlock || inputBlock->isEmpty() )
{
QgsDebugMsg( "No raster data!" );
delete inputBlock;
return outputBlock;
return outputBlock.release();
}

QgsRasterBlock *alphaBlock = nullptr;
std::shared_ptr< QgsRasterBlock > alphaBlock;

if ( mAlphaBand > 0 && mBand != mAlphaBand )
{

alphaBlock = mInput->block( mAlphaBand, extent, width, height, feedback );
alphaBlock.reset( mInput->block( mAlphaBand, extent, width, height, feedback ) );
if ( !alphaBlock || alphaBlock->isEmpty() )
{
// TODO: better to render without alpha
delete inputBlock;
delete alphaBlock;
return outputBlock;
return outputBlock.release();
}
}
else if ( mAlphaBand > 0 )
Expand All @@ -123,13 +119,9 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext

if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
{
delete inputBlock;
delete alphaBlock;
return outputBlock;
return outputBlock.release();
}



double cellXSize = extent.width() / double( width );
double cellYSize = extent.height() / double( height );
double zenithRad = qMax( 0.0, 90 - mLightAngle ) * M_PI / 180.0;
Expand Down Expand Up @@ -272,12 +264,7 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext
}
}

delete inputBlock;
if ( mAlphaBand > 0 && mBand != mAlphaBand )
{
delete alphaBlock;
}
return outputBlock;
return outputBlock.release();
}

QList<int> QgsHillshadeRenderer::usesBands() const
Expand Down
18 changes: 7 additions & 11 deletions src/core/raster/qgshuesaturationfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,30 @@ QgsRasterBlock *QgsHueSaturationFilter::block( int bandNo, QgsRectangle const &
Q_UNUSED( bandNo );
QgsDebugMsgLevel( QString( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );

QgsRasterBlock *outputBlock = new QgsRasterBlock();
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
if ( !mInput )
{
return outputBlock;
return outputBlock.release();
}

// At this moment we know that we read rendered image
int bandNumber = 1;
QgsRasterBlock *inputBlock = mInput->block( bandNumber, extent, width, height, feedback );
std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, extent, width, height, feedback ) );
if ( !inputBlock || inputBlock->isEmpty() )
{
QgsDebugMsg( "No raster data!" );
delete inputBlock;
return outputBlock;
return outputBlock.release();
}

if ( mSaturation == 0 && mGrayscaleMode == GrayscaleOff && !mColorizeOn )
{
QgsDebugMsgLevel( "No hue/saturation change.", 4 );
delete outputBlock;
return inputBlock;
return inputBlock.release();
}

if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
{
delete inputBlock;
return outputBlock;
return outputBlock.release();
}

// adjust image
Expand Down Expand Up @@ -216,8 +213,7 @@ QgsRasterBlock *QgsHueSaturationFilter::block( int bandNo, QgsRectangle const &
outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
}

delete inputBlock;
return outputBlock;
return outputBlock.release();
}

// Process a colorization and update resultant HSL & RGB values
Expand Down
12 changes: 6 additions & 6 deletions src/core/raster/qgsmultibandcolorrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ QgsRasterRenderer *QgsMultiBandColorRenderer::create( const QDomElement &elem, Q
QgsRasterBlock *QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
{
Q_UNUSED( bandNo );
QgsRasterBlock *outputBlock = new QgsRasterBlock();
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
if ( !mInput )
{
return outputBlock;
return outputBlock.release();
}

//In some (common) cases, we can simplify the drawing loop considerably and save render time
Expand All @@ -161,7 +161,7 @@ QgsRasterBlock *QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
{
// no need to draw anything if no band is set
// TODO:: we should probably return default color block
return outputBlock;
return outputBlock.release();
}

if ( mAlphaBand > 0 )
Expand Down Expand Up @@ -195,7 +195,7 @@ QgsRasterBlock *QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
{
delete bandBlocks[*bandIt];
}
return outputBlock;
return outputBlock.release();
}
}

Expand All @@ -222,7 +222,7 @@ QgsRasterBlock *QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
{
delete bandBlocks.value( i );
}
return outputBlock;
return outputBlock.release();
}

QRgb myDefaultColor = NODATA_COLOR;
Expand Down Expand Up @@ -323,7 +323,7 @@ QgsRasterBlock *QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
delete bandDelIt.value();
}

return outputBlock;
return outputBlock.release();
}

void QgsMultiBandColorRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
Expand Down
10 changes: 4 additions & 6 deletions src/core/raster/qgsrasternuller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent,
return new QgsRasterBlock();
}

QgsRasterBlock *inputBlock = mInput->block( bandNo, extent, width, height, feedback );
std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNo, extent, width, height, feedback ) );
if ( !inputBlock )
{
return new QgsRasterBlock();
Expand All @@ -82,10 +82,10 @@ QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent,
// We don't support nuller for color types
if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
{
return inputBlock;
return inputBlock.release();
}

QgsRasterBlock *outputBlock = new QgsRasterBlock( inputBlock->dataType(), width, height );
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock( inputBlock->dataType(), width, height ) );
if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
{
double noDataValue;
Expand Down Expand Up @@ -122,8 +122,6 @@ QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent,
}
}
}
delete inputBlock;

return outputBlock;
return outputBlock.release();
}

Loading

0 comments on commit 2dbb128

Please sign in to comment.