QGIS API Documentation  2.99.0-Master (0a63d1f)
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 #include "qgsrastertransparency.h"
20 #include "qgsrasterviewport.h"
21 #include <QDomDocument>
22 #include <QDomElement>
23 #include <QImage>
24 
26  QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) ), mBand( band )
27 {
28 
29 }
30 
32 {
33  QgsSingleBandColorDataRenderer * renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
34  renderer->copyCommonProperties( this );
35  return renderer;
36 }
37 
39 {
40  if ( elem.isNull() )
41  {
42  return nullptr;
43  }
44 
45  int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
46  QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band );
47  r->readXml( elem );
48  return r;
49 }
50 
51 QgsRasterBlock* QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height, QgsRasterBlockFeedback* feedback )
52 {
53  Q_UNUSED( bandNo );
54 
55  QgsRasterBlock *outputBlock = new QgsRasterBlock();
56  if ( !mInput )
57  {
58  return outputBlock;
59  }
60 
61  QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height, feedback );
62  if ( !inputBlock || inputBlock->isEmpty() )
63  {
64  QgsDebugMsg( "No raster data!" );
65  delete inputBlock;
66  return outputBlock;
67  }
68 
69  bool hasTransparency = usesTransparency();
70  if ( !hasTransparency )
71  {
72  // Nothing to do, just retype if necessary
73  inputBlock->convert( Qgis::ARGB32_Premultiplied );
74  delete outputBlock;
75  return inputBlock;
76  }
77 
78  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
79  {
80  delete inputBlock;
81  return outputBlock;
82  }
83 
84  // make sure input is also premultiplied!
85  inputBlock->convert( Qgis::ARGB32_Premultiplied );
86 
87  QRgb* inputBits = ( QRgb* )inputBlock->bits();
88  QRgb* outputBits = ( QRgb* )outputBlock->bits();
89  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
90  {
91  QRgb c = inputBits[i];
92  outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
93  }
94 
95  delete inputBlock;
96  return outputBlock;
97 }
98 
99 void QgsSingleBandColorDataRenderer::writeXml( QDomDocument& doc, QDomElement& parentElem ) const
100 {
101  if ( parentElem.isNull() )
102  return;
103 
104  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
105  _writeXml( doc, rasterRendererElem );
106  rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
107  parentElem.appendChild( rasterRendererElem );
108 }
109 
111 {
112  QList<int> bandList;
113  if ( mBand != -1 )
114  {
115  bandList << mBand;
116  }
117  return bandList;
118 }
119 
121 {
122  // Renderer can only work with numerical values in at least 1 band
123  if ( !input ) return false;
124 
125  if ( !mOn )
126  {
127  // In off mode we can connect to anything
128  mInput = input;
129  return true;
130  }
131 
132  if ( input->dataType( 1 ) == Qgis::ARGB32 ||
133  input->dataType( 1 ) == Qgis::ARGB32_Premultiplied )
134  {
135  mInput = input;
136  return true;
137  }
138  return false;
139 }
A rectangle specified with double values.
Definition: qgsrectangle.h:36
bool setInput(QgsRasterInterface *input) override
Set input.
virtual QgsRectangle extent() const
Get the extent of the interface.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
virtual QgsRasterInterface * input() const
Current input.
QgsSingleBandColorDataRenderer * clone() const override
Clone itself, create deep copy.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
QgsSingleBandColorDataRenderer(QgsRasterInterface *input, int band)
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
Raster renderer pipe for single band color.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:76
bool convert(Qgis::DataType destDataType)
Convert data to different type.
Raster data container.
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.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
bool usesTransparency() const
bool isEmpty() const
Returns true if block is empty, i.e.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Base class for processing filters like renderers, reprojector, resampler etc.
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:333
bool reset(Qgis::DataType theDataType, int theWidth, int theHeight)
Reset block.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
char * bits(int row, int column)
Get pointer to data.
double mOpacity
Global alpha value (0-1)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
Raster renderer pipe that applies colors to a raster.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:75