QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
30 : QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) )
31 , mBand( band )
32{
33
34}
35
37{
38 QgsSingleBandColorDataRenderer *renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
39 renderer->copyCommonProperties( this );
40 return renderer;
41}
42
47
49{
50 if ( elem.isNull() )
51 {
52 return nullptr;
53 }
54
55 const int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
57 r->readXml( elem );
58 return r;
59}
60
62{
63 Q_UNUSED( bandNo )
64
65 auto outputBlock = std::make_unique<QgsRasterBlock>();
66 if ( !mInput )
67 {
68 return outputBlock.release();
69 }
70
71 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( mBand, extent, width, height, feedback ) );
72 if ( !inputBlock || inputBlock->isEmpty() )
73 {
74 QgsDebugError( QStringLiteral( "No raster data!" ) );
75 return outputBlock.release();
76 }
77
78 const bool hasTransparency = usesTransparency();
79 if ( !hasTransparency )
80 {
81 // Nothing to do, just retype if necessary
82 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
83 return inputBlock.release();
84 }
85
86 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
87 {
88 return outputBlock.release();
89 }
90
91 // make sure input is also premultiplied!
92 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
93
94 QRgb *inputBits = ( QRgb * )inputBlock->bits();
95 QRgb *outputBits = ( QRgb * )outputBlock->bits();
96 for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
97 {
98 const QRgb c = inputBits[i];
99 outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
100 }
101
102 return outputBlock.release();
103}
104
105void QgsSingleBandColorDataRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
106{
107 if ( parentElem.isNull() )
108 return;
109
110 QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
111 _writeXml( doc, rasterRendererElem );
112 rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
113 parentElem.appendChild( rasterRendererElem );
114}
115
117{
118 QList<int> bandList;
119 if ( mBand != -1 )
120 {
121 bandList << mBand;
122 }
123 return bandList;
124}
125
127{
128 // Renderer can only work with numerical values in at least 1 band
129 if ( !input ) return false;
130
131 if ( !mOn )
132 {
133 // In off mode we can connect to anything
134 mInput = input;
135 return true;
136 }
137
138 if ( input->dataType( 1 ) == Qgis::DataType::ARGB32 ||
140 {
141 mInput = input;
142 return true;
143 }
144 return false;
145}
146
148{
149 return mBand;
150}
151
153{
154 if ( !mInput )
155 {
156 mBand = band;
157 return true;
158 }
159 else if ( band > 0 && band <= mInput->bandCount() )
160 {
161 mBand = band;
162 return true;
163 }
164 return false;
165}
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
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition qgis.h:386
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:7142
#define QgsDebugError(str)
Definition qgslogger.h:57