QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsrastersinglecolorrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrastersinglecolorrenderer.cpp
3 -----------------------------
4 begin : April 2024
5 copyright : (C) 2024 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgscolorutils.h"
22
23#include <QDomDocument>
24#include <QDomElement>
25
27 : QgsRasterRenderer( input, QStringLiteral( "singlecolor" ) )
28 , mInputBand( band )
29 , mColor( color )
30{
31}
32
34{
35 QgsRasterSingleColorRenderer *renderer = new QgsRasterSingleColorRenderer( nullptr, mInputBand, mColor );
36 renderer->copyCommonProperties( this );
37 return renderer;
38}
39
44
46{
47 if ( elem.isNull() )
48 {
49 return nullptr;
50 }
51
52 const QColor color = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "color" ), QStringLiteral( "0,0,0" ) ) );
53 const int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "1" ) ).toInt();
55 r->readXml( elem );
56
57 return r;
58}
59
61{
62 QgsDebugMsgLevel( QStringLiteral( "width = %1 height = %2" ).arg( width ).arg( height ), 4 );
63
64 auto outputBlock = std::make_unique<QgsRasterBlock>();
65 if ( !mInput || mInputBand == -1 )
66 {
67 return outputBlock.release();
68 }
69
70 const std::shared_ptr< QgsRasterBlock > inputBlock( mInput->block( mInputBand, extent, width, height, feedback ) );
71 if ( !inputBlock || inputBlock->isEmpty() )
72 {
73 QgsDebugError( QStringLiteral( "No raster data!" ) );
74 return outputBlock.release();
75 }
76
77 std::shared_ptr< QgsRasterBlock > alphaBlock;
78 if ( mAlphaBand > 0 )
79 {
80 alphaBlock = inputBlock;
81 }
82
83 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
84 {
85 return outputBlock.release();
86 }
87
88 const QRgb defaultColor = renderColorForNodataPixel();
89 const QRgb rendererColor = qRgba( mColor.red(), mColor.green(), mColor.blue(), mColor.alpha() );
90
91 bool isNoData = false;
92 const qgssize blockSize = static_cast< qgssize >( width ) * height;
93 for ( qgssize i = 0; i < blockSize; i++ )
94 {
95 double value = inputBlock->valueAndNoData( i, isNoData );
96 if ( isNoData )
97 {
98 outputBlock->setColor( i, defaultColor );
99 continue;
100 }
101
102 double currentAlpha = mOpacity;
104 {
105 currentAlpha *= mRasterTransparency->opacityForValue( value );
106 }
107 if ( mAlphaBand > 0 )
108 {
109 const double alpha = alphaBlock->value( i );
110 if ( alpha == 0 )
111 {
112 outputBlock->setColor( i, defaultColor );
113 continue;
114 }
115 else
116 {
117 currentAlpha *= alpha / 255.0;
118 }
119 }
120
121 if ( qgsDoubleNear( currentAlpha, 1.0 ) )
122 {
123 outputBlock->setColor( i, rendererColor );
124 }
125 else
126 {
127 outputBlock->setColor( i, qRgba( static_cast<int>( currentAlpha * mColor.red() ),
128 static_cast<int>( currentAlpha * mColor.green() ),
129 static_cast<int>( currentAlpha * mColor.blue() ),
130 static_cast<int>( currentAlpha * mColor.alpha() ) ) );
131 }
132 }
133
134 return outputBlock.release();
135}
136
138{
139 mColor = color;;
140}
141
143{
144 return mColor;
145}
146
147void QgsRasterSingleColorRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
148{
149 if ( parentElem.isNull() )
150 {
151 return;
152 }
153
154 QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
155 _writeXml( doc, rasterRendererElem );
156
157 rasterRendererElem.setAttribute( QStringLiteral( "color" ), QgsColorUtils::colorToString( mColor ) );
158 rasterRendererElem.setAttribute( QStringLiteral( "band" ), mInputBand );
159
160 parentElem.appendChild( rasterRendererElem );
161}
162
164{
165 return mInputBand;
166}
167
169{
170 if ( !mInput || ( band > 0 && band <= mInput->bandCount() ) )
171 {
172 mInputBand = band;
173 return true;
174 }
175 return false;
176}
177
179{
180 QList<int> bands;
181 if ( mInputBand != -1 )
182 {
183 bands << mInputBand;
184 }
185 return bands;
186}
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
Definition qgis.h:1512
@ InternalLayerOpacityHandling
The renderer internally handles the raster layer's opacity, so the default layer level opacity handli...
Definition qgis.h:1503
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition qgis.h:387
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.
Raster data container.
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.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
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.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by the renderer registry).
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...
Definition qgis.h:7142
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
#define QgsDebugError(str)
Definition qgslogger.h:57