From 15fe4da7e8c652889793a426c714aa432e00db69 Mon Sep 17 00:00:00 2001 From: Marco Hugentobler Date: Fri, 20 Jan 2012 17:24:01 +0100 Subject: [PATCH] Better array generation for paletted raster renderer --- src/core/raster/qgsrasterlayer.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/core/raster/qgsrasterlayer.cpp b/src/core/raster/qgsrasterlayer.cpp index 0fde786eb0fd..44c957f73bfa 100644 --- a/src/core/raster/qgsrasterlayer.cpp +++ b/src/core/raster/qgsrasterlayer.cpp @@ -597,15 +597,27 @@ void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawin { case PalettedColor: { - //todo: go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous int grayBand = bandNumber( grayBandName() ); QgsColorRampShader* colorRampShader = dynamic_cast( rasterShader()->rasterShaderFunction() ); if ( colorRampShader ) { QList colorEntries = colorRampShader->colorRampItemList(); - QColor* colorArray = new QColor[ colorEntries.size()]; + + //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous) + int colorArraySize = 0; QList::const_iterator colorIt = colorEntries.constBegin(); for ( ; colorIt != colorEntries.constEnd(); ++colorIt ) + { + if ( colorIt->value > colorArraySize ) + { + colorArraySize = ( int )( colorIt->value ); + } + } + + colorArraySize += 1; //usually starts at 0 + QColor* colorArray = new QColor[ colorArraySize ]; + colorIt = colorEntries.constBegin(); + for ( ; colorIt != colorEntries.constEnd(); ++colorIt ) { colorArray[( int )( colorIt->value )] = colorIt->color; } @@ -613,7 +625,7 @@ void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawin renderer = new QgsPalettedRasterRenderer( mDataProvider, grayBand, colorArray, - colorEntries.size() ); + colorArraySize ); } else //try to get it from the color table {