QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgssinglebandcolordatarenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssinglebandcolordatarenderer.cpp
3 ----------------------------------
4 begin : January 2012
5 copyright : (C) 2012 by Marco Hugentobler
6 email : marco at sourcepole 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 <memory>
21
23#include "qgsrasterviewport.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27#include <QImage>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
33 : QgsRasterRenderer( input, u"singlebandcolordata"_s )
34 , mBand( band )
35{
36
37}
38
40{
41 QgsSingleBandColorDataRenderer *renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
42 renderer->copyCommonProperties( this );
43 return renderer;
44}
45
50
52{
53 if ( elem.isNull() )
54 {
55 return nullptr;
56 }
57
58 const int band = elem.attribute( u"band"_s, u"-1"_s ).toInt();
60 r->readXml( elem );
61 return r;
62}
63
65{
66 Q_UNUSED( bandNo )
67
68 auto outputBlock = std::make_unique<QgsRasterBlock>();
69 if ( !mInput )
70 {
71 return outputBlock.release();
72 }
73
74 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( mBand, extent, width, height, feedback ) );
75 if ( !inputBlock || inputBlock->isEmpty() )
76 {
77 QgsDebugError( u"No raster data!"_s );
78 return outputBlock.release();
79 }
80
81 const bool hasTransparency = usesTransparency();
82 if ( !hasTransparency )
83 {
84 // Nothing to do, just retype if necessary
85 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
86 return inputBlock.release();
87 }
88
89 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
90 {
91 return outputBlock.release();
92 }
93
94 // make sure input is also premultiplied!
95 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
96
97 QRgb *inputBits = ( QRgb * )inputBlock->bits();
98 QRgb *outputBits = ( QRgb * )outputBlock->bits();
99 for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
100 {
101 const QRgb c = inputBits[i];
102 outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
103 }
104
105 return outputBlock.release();
106}
107
108void QgsSingleBandColorDataRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
109{
110 if ( parentElem.isNull() )
111 return;
112
113 QDomElement rasterRendererElem = doc.createElement( u"rasterrenderer"_s );
114 _writeXml( doc, rasterRendererElem );
115 rasterRendererElem.setAttribute( u"band"_s, mBand );
116 parentElem.appendChild( rasterRendererElem );
117}
118
120{
121 QList<int> bandList;
122 if ( mBand != -1 )
123 {
124 bandList << mBand;
125 }
126 return bandList;
127}
128
130{
131 // Renderer can only work with numerical values in at least 1 band
132 if ( !input ) return false;
133
134 if ( !mOn )
135 {
136 // In off mode we can connect to anything
137 mInput = input;
138 return true;
139 }
140
141 if ( input->dataType( 1 ) == Qgis::DataType::ARGB32 ||
143 {
144 mInput = input;
145 return true;
146 }
147 return false;
148}
149
151{
152 return mBand;
153}
154
156{
157 if ( !mInput )
158 {
159 mBand = band;
160 return true;
161 }
162 else if ( band > 0 && band <= mInput->bandCount() )
163 {
164 mBand = band;
165 return true;
166 }
167 return false;
168}
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
Definition qgis.h:1570
@ InternalLayerOpacityHandling
The renderer internally handles the raster layer's opacity, so the default layer level opacity handli...
Definition qgis.h:1561
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition qgis.h:394
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition qgis.h:393
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).
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.
bool usesTransparency() const
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.
A rectangle specified with double values.
Qgis::RasterRendererFlags flags() const override
Returns flags which dictate renderer behavior.
QgsSingleBandColorDataRenderer(QgsRasterInterface *input, int band)
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
QgsSingleBandColorDataRenderer * clone() const override
Clone itself, create deep copy.
bool setInput(QgsRasterInterface *input) override
Set input.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
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.
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.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
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:7458
#define QgsDebugError(str)
Definition qgslogger.h:59