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] ) ) );
A rectangle specified with double values.
QString label(int idx) const
Return optional category label.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
Renderer for paletted raster images.
virtual QgsRasterInterface * input() const
Current input.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height) override
Read block of data using given extent and size.
bool isNoData(int row, int column)
Check if value at position is no data.
QgsPalettedRasterRenderer(QgsRasterInterface *input, int bandNumber, QColor *colorArray, int nColors, const QVector< QString > labels=QVector< QString >())
Renderer owns color array.
void readXML(const QDomElement &rendererElem) override
Sets base class members from xml.
bool usesTransparency() const
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value.
static const QRgb NODATA_COLOR
int alphaValue(double, int theGlobalTransparency=255) const
Returns the transparency value for a single value Pixel.
double value(int row, int column) const
Read a single value if type of block is numeric.
~QgsPalettedRasterRenderer()
int nColors() const
Returns number of colors.
void setLabel(int idx, QString label)
Set category label.
int mAlphaBand
Read alpha value from band.
void setAlphaBand(int band)
Base class for processing filters like renderers, reprojector, resampler etc.
unsigned long long qgssize
qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
virtual QgsRectangle extent()
Get the extent of the interface.
char * bits(int row, int column)
Get pointer to data.
QRgb * rgbArray() const
Returns copy of rgb array (caller takes ownership)
QgsRasterInterface * clone() const override
Clone itself, create deep copy.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Get symbology items if provided by renderer.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
double mOpacity
Global alpha value (0-1)
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void setOpacity(double opacity)
void _writeXML(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXML method of subclasses) ...
QgsRasterInterface * mInput
void setRasterTransparency(QgsRasterTransparency *t)
Raster renderer pipe that applies colors to a raster.
QColor * colors() const
Returns copy of color array (caller takes ownership)
bool isEmpty() const
Returns true if block is empty, i.e.