Skip to content

Commit

Permalink
[backport] Fix bug where histogram can be assigned negative frequency…
Browse files Browse the repository at this point in the history
… for a pixel range. Also fix potential memory leak as new histogram vector was assigned to band stats without clearing the old.
  • Loading branch information
timlinux committed Jun 30, 2011
1 parent 199567c commit 834fc0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -2345,7 +2345,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider,
myRasterBandStats.bandName = mDataProvider->generateBandName( i );
myRasterBandStats.bandNumber = i;
myRasterBandStats.statsGathered = false;
myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector();
myRasterBandStats.histogramVector->clear();
//Store the default color table
//readColorTable( i, &myRasterBandStats.colorTable );
QList<QgsColorRampShader::ColorRampItem> ct;
Expand Down
12 changes: 10 additions & 2 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -1320,8 +1320,16 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & t

for ( int myBin = 0; myBin < theBinCount; myBin++ )
{
theBandStats.histogramVector->push_back( myHistogramArray[myBin] );
QgsDebugMsg( "Added " + QString::number( myHistogramArray[myBin] ) + " to histogram vector" );
if ( myHistogramArray[myBin] < 0 ) //can't have less than 0 pixels of any value
{
theBandStats.histogramVector->push_back( 0 );
QgsDebugMsg( "Added 0 to histogram vector as freq was negative!" );
}
else
{
theBandStats.histogramVector->push_back( myHistogramArray[myBin] );
QgsDebugMsg( "Added " + QString::number( myHistogramArray[myBin] ) + " to histogram vector" );
}
}

}
Expand Down

0 comments on commit 834fc0b

Please sign in to comment.