@@ -69,17 +69,36 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
69
69
if ( !paletteElem.isNull () )
70
70
{
71
71
QDomNodeList paletteEntries = paletteElem.elementsByTagName ( " paletteEntry" );
72
- nColors = paletteEntries.size ();
73
- colors = new QRgb[ nColors ];
74
72
75
- int value = 0 ;
76
73
QDomElement entryElem;
74
+ int value;
75
+ nColors = 0 ;
76
+
77
+ // We cannot believe that data are correct, check first max value
78
+ for ( int i = 0 ; i < paletteEntries.size (); ++i )
79
+ {
80
+ entryElem = paletteEntries.at ( i ).toElement ();
81
+ // Could be written as doubles (with .0000) in old project files
82
+ value = ( int )entryElem.attribute ( " value" , " 0" ).toDouble ();
83
+ if ( value >= nColors && value <= 10000 ) nColors = value + 1 ;
84
+ }
85
+ QgsDebugMsg ( QString ( " nColors = %1" ).arg ( nColors ) );
86
+
87
+ colors = new QRgb[ nColors ];
88
+
77
89
for ( int i = 0 ; i < nColors; ++i )
78
90
{
79
91
entryElem = paletteEntries.at ( i ).toElement ();
80
- value = entryElem.attribute ( " value" , " 0" ).toInt ();
92
+ value = ( int ) entryElem.attribute ( " value" , " 0" ).toDouble ();
81
93
QgsDebugMsg ( entryElem.attribute ( " color" , " #000000" ) );
82
- colors[value] = QColor ( entryElem.attribute ( " color" , " #000000" ) ).rgba ();
94
+ if ( value >= 0 && value < nColors )
95
+ {
96
+ colors[value] = QColor ( entryElem.attribute ( " color" , " #000000" ) ).rgba ();
97
+ }
98
+ else
99
+ {
100
+ QgsDebugMsg ( QString ( " value %1 out of range" ).arg ( value ) );
101
+ }
83
102
}
84
103
}
85
104
QgsRasterRenderer* r = new QgsPalettedRasterRenderer ( input, bandNumber, colors, nColors );
@@ -171,7 +190,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
171
190
}
172
191
173
192
// create copy of color table with nodata values replaced by fully transparent color
174
- QVector<QRgb> colorTable (mNColors );
193
+ QVector<QRgb> colorTable ( mNColors );
175
194
for ( int i = 0 ; i < mNColors ; ++i )
176
195
{
177
196
if ( inputBlock->isNoDataValue ( i ) )
@@ -194,7 +213,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
194
213
int val = ( int ) inputBlock->value ( i );
195
214
if ( !hasTransparency )
196
215
{
197
- outputData[i] = colorTable[ val ] ;
216
+ outputData[i] = colorTable. value ( val ) ;
198
217
}
199
218
else
200
219
{
@@ -207,7 +226,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
207
226
{
208
227
currentOpacity *= alphaBlock->value ( i ) / 255.0 ;
209
228
}
210
- QColor currentColor = QColor ( colorTable[ val] );
229
+ QColor currentColor = QColor ( colorTable. value ( val ) );
211
230
outputData[i] = qRgba ( currentOpacity * currentColor.red (), currentOpacity * currentColor.green (), currentOpacity * currentColor.blue (), currentOpacity * 255 );
212
231
}
213
232
}
0 commit comments