QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
20#include "qgsrasterviewport.h"
21#include <QDomDocument>
22#include <QDomElement>
23#include <QImage>
24#include <memory>
25
27 QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) ), mBand( band )
28{
29
30}
31
33{
34 QgsSingleBandColorDataRenderer *renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
35 renderer->copyCommonProperties( this );
36 return renderer;
37}
38
39Qgis::RasterRendererFlags QgsSingleBandColorDataRenderer::flags() const
40{
42}
43
45{
46 if ( elem.isNull() )
47 {
48 return nullptr;
49 }
50
51 const int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
53 r->readXml( elem );
54 return r;
55}
56
57QgsRasterBlock *QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
58{
59 Q_UNUSED( bandNo )
60
61 std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
62 if ( !mInput )
63 {
64 return outputBlock.release();
65 }
66
67 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( mBand, extent, width, height, feedback ) );
68 if ( !inputBlock || inputBlock->isEmpty() )
69 {
70 QgsDebugMsg( QStringLiteral( "No raster data!" ) );
71 return outputBlock.release();
72 }
73
74 const bool hasTransparency = usesTransparency();
75 if ( !hasTransparency )
76 {
77 // Nothing to do, just retype if necessary
78 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
79 return inputBlock.release();
80 }
81
82 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
83 {
84 return outputBlock.release();
85 }
86
87 // make sure input is also premultiplied!
88 inputBlock->convert( Qgis::DataType::ARGB32_Premultiplied );
89
90 QRgb *inputBits = ( QRgb * )inputBlock->bits();
91 QRgb *outputBits = ( QRgb * )outputBlock->bits();
92 for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
93 {
94 const QRgb c = inputBits[i];
95 outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
96 }
97
98 return outputBlock.release();
99}
100
101void QgsSingleBandColorDataRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
102{
103 if ( parentElem.isNull() )
104 return;
105
106 QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
107 _writeXml( doc, rasterRendererElem );
108 rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
109 parentElem.appendChild( rasterRendererElem );
110}
111
113{
114 QList<int> bandList;
115 if ( mBand != -1 )
116 {
117 bandList << mBand;
118 }
119 return bandList;
120}
121
123{
124 // Renderer can only work with numerical values in at least 1 band
125 if ( !input ) return false;
126
127 if ( !mOn )
128 {
129 // In off mode we can connect to anything
130 mInput = input;
131 return true;
132 }
133
134 if ( input->dataType( 1 ) == Qgis::DataType::ARGB32 ||
136 {
137 mInput = input;
138 return true;
139 }
140 return false;
141}
@ 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.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Feedback object tailored for raster block reading.
Raster data container.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
Raster renderer pipe that applies colors to a raster.
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)
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.
Definition: qgsrectangle.h:42
Raster renderer pipe for single band color.
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.
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:3032
#define QgsDebugMsg(str)
Definition: qgslogger.h:38