23#include <QDomDocument>
27using namespace Qt::StringLiterals;
55 const int band = elem.attribute( u
"band"_s, u
"1"_s ).toInt();
56 auto r = std::make_unique<QgsRasterSingleColorRenderer>(
input, band,
color );
64 QgsDebugMsgLevel( u
"width = %1 height = %2"_s.arg( width ).arg( height ), 4 );
66 auto outputBlock = std::make_unique<QgsRasterBlock>();
67 if ( !
mInput || mInputBand == -1 )
69 return outputBlock.release();
72 const std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->block( mInputBand,
extent, width, height, feedback ) );
73 if ( !inputBlock || inputBlock->isEmpty() )
76 return outputBlock.release();
79 std::shared_ptr< QgsRasterBlock > alphaBlock;
82 alphaBlock = inputBlock;
87 return outputBlock.release();
91 const QRgb rendererColor = qRgba( mColor.red(), mColor.green(), mColor.blue(), mColor.alpha() );
93 bool isNoData =
false;
94 const qgssize blockSize =
static_cast< qgssize >( width ) * height;
95 for (
qgssize i = 0; i < blockSize; i++ )
97 double value = inputBlock->valueAndNoData( i, isNoData );
100 outputBlock->setColor( i, defaultColor );
111 const double alpha = alphaBlock->value( i );
114 outputBlock->setColor( i, defaultColor );
119 currentAlpha *= alpha / 255.0;
125 outputBlock->setColor( i, rendererColor );
129 outputBlock->setColor(
132 static_cast<int>( currentAlpha * mColor.red() ),
133 static_cast<int>( currentAlpha * mColor.green() ),
134 static_cast<int>( currentAlpha * mColor.blue() ),
135 static_cast<int>( currentAlpha * mColor.alpha() )
141 return outputBlock.release();
157 if ( parentElem.isNull() )
162 QDomElement rasterRendererElem = doc.createElement( u
"rasterrenderer"_s );
166 rasterRendererElem.setAttribute( u
"band"_s, mInputBand );
168 parentElem.appendChild( rasterRendererElem );
189 if ( mInputBand != -1 )
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
@ InternalLayerOpacityHandling
The renderer internally handles the raster layer's opacity, so the default layer level opacity handli...
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Feedback object tailored for raster block reading.
QgsRasterInterface(QgsRasterInterface *input=nullptr)
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
QgsRasterRenderer(QgsRasterInterface *input=nullptr, const QString &type=QString())
Constructor for QgsRasterRenderer.
double mOpacity
Global alpha value (0-1).
int mAlphaBand
Read alpha value from band.
QRgb renderColorForNodataPixel() const
Returns the color for the renderer to use to represent nodata pixels.
std::unique_ptr< QgsRasterTransparency > mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses).
int bandCount() const override
Gets number of bands.
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
static std::unique_ptr< QgsRasterRenderer > create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by the renderer registry).
bool setInputBand(int band) override
Attempts to set the input band for the renderer.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
Qgis::RasterRendererFlags flags() const override
Returns flags which dictate renderer behavior.
QgsRasterSingleColorRenderer * clone() const override
Clone itself, create deep copy.
QgsRasterSingleColorRenderer(QgsRasterInterface *input, int band, const QColor &color)
Creates a single color renderer.
QColor color() const
Returns the single color used by the renderer.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
void setColor(const QColor &color)
Sets the single color used by the renderer.
A rectangle specified with double values.
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...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)