22 #include <QDomDocument>
23 #include <QDomElement>
28 QColor* colorArray,
int nColors,
const QVector<QString> labels ):
29 QgsRasterRenderer( input,
"paletted" ), mBand( bandNumber ), mNColors( nColors ), mLabels( labels )
32 for (
int i = 0; i <
nColors; ++i )
34 mColors[i] = colorArray[i].rgba();
40 QgsRasterRenderer( input,
"paletted" ), mBand( bandNumber ), mColors( colorArray ), mNColors( nColors ), mLabels( labels )
55 renderer->mLabels = mLabels;
66 int bandNumber = elem.attribute(
"band",
"-1" ).toInt();
69 QVector<QString> labels;
71 QDomElement paletteElem = elem.firstChildElement(
"colorPalette" );
72 if ( !paletteElem.isNull() )
74 QDomNodeList paletteEntries = paletteElem.elementsByTagName(
"paletteEntry" );
76 QDomElement entryElem;
81 for (
int i = 0; i < paletteEntries.size(); ++i )
83 entryElem = paletteEntries.at( i ).toElement();
85 value = ( int )entryElem.attribute(
"value",
"0" ).toDouble();
86 if ( value >= nColors && value <= 10000 ) nColors = value + 1;
88 QgsDebugMsg( QString(
"nColors = %1" ).arg( nColors ) );
92 for (
int i = 0; i <
nColors; ++i )
94 entryElem = paletteEntries.at( i ).toElement();
95 value = ( int )entryElem.attribute(
"value",
"0" ).toDouble();
96 QgsDebugMsg( entryElem.attribute(
"color",
"#000000" ) );
97 if ( value >= 0 && value < nColors )
99 colors[value] = QColor( entryElem.attribute(
"color",
"#000000" ) ).rgba();
100 QString
label = entryElem.attribute(
"label" );
101 if ( !label.isEmpty() )
103 if ( value >= labels.size() ) labels.resize( value + 1 );
104 labels[value] =
label;
109 QgsDebugMsg( QString(
"value %1 out of range" ).arg( value ) );
124 QColor* colorArray =
new QColor[ mNColors ];
125 for (
int i = 0; i < mNColors; ++i )
127 colorArray[i] = QColor( mColors[i] );
138 QRgb* rgbValues =
new QRgb[mNColors];
139 for (
int i = 0; i < mNColors; ++i )
141 rgbValues[i] = mColors[i];
148 if ( idx >= mLabels.size() )
150 mLabels.resize( idx + 1 );
152 mLabels[idx] =
label;
158 if ( !
mInput || mNColors == 0 )
165 if ( !inputBlock || inputBlock->
isEmpty() )
181 if ( !alphaBlock || alphaBlock->
isEmpty() )
190 alphaBlock = inputBlock;
204 unsigned int* outputData = (
unsigned int* )( outputBlock->
bits() );
207 for (
qgssize i = 0; i < rasterSize; ++i )
211 outputData[i] = myDefaultColor;
214 int val = ( int ) inputBlock->
value( i );
215 if ( !hasTransparency )
217 outputData[i] = mColors[val];
228 currentOpacity *= alphaBlock->
value( i ) / 255.0;
230 QColor currentColor = QColor( mColors[val] );
231 outputData[i] = qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 );
246 if ( parentElem.isNull() )
251 QDomElement rasterRendererElem = doc.createElement(
"rasterrenderer" );
254 rasterRendererElem.setAttribute(
"band", mBand );
255 QDomElement colorPaletteElem = doc.createElement(
"colorPalette" );
256 for (
int i = 0; i < mNColors; ++i )
258 QDomElement colorElem = doc.createElement(
"paletteEntry" );
259 colorElem.setAttribute(
"value", i );
260 colorElem.setAttribute(
"color", QColor( mColors[i] ).name() );
261 if ( !
label( i ).isEmpty() )
263 colorElem.setAttribute(
"label",
label( i ) );
265 colorPaletteElem.appendChild( colorElem );
267 rasterRendererElem.appendChild( colorPaletteElem );
269 parentElem.appendChild( rasterRendererElem );
274 for (
int i = 0; i < mNColors; ++i )
276 QString lab =
label( i ).isEmpty() ? QString::number( i ) :
label( i );
277 symbolItems.push_back( qMakePair( lab, QColor( mColors[i] ) ) );