QGIS API Documentation  2.99.0-Master (e077efd)
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 }
34 
36 {
37  QgsSingleBandColorDataRenderer * renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
38  renderer->copyCommonProperties( this );
39  return renderer;
40 }
41 
43 {
44  if ( elem.isNull() )
45  {
46  return nullptr;
47  }
48 
49  int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
50  QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band );
51  r->readXml( elem );
52  return r;
53 }
54 
55 QgsRasterBlock* QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height, QgsRasterBlockFeedback* feedback )
56 {
57  Q_UNUSED( bandNo );
58 
59  QgsRasterBlock *outputBlock = new QgsRasterBlock();
60  if ( !mInput )
61  {
62  return outputBlock;
63  }
64 
65  QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height, feedback );
66  if ( !inputBlock || inputBlock->isEmpty() )
67  {
68  QgsDebugMsg( "No raster data!" );
69  delete inputBlock;
70  return outputBlock;
71  }
72 
73  bool hasTransparency = usesTransparency();
74  if ( !hasTransparency )
75  {
76  // Nothing to do, just retype if necessary
77  inputBlock->convert( Qgis::ARGB32_Premultiplied );
78  delete outputBlock;
79  return inputBlock;
80  }
81 
82  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
83  {
84  delete inputBlock;
85  return outputBlock;
86  }
87 
88  // make sure input is also premultiplied!
89  inputBlock->convert( Qgis::ARGB32_Premultiplied );
90 
91  QRgb* inputBits = ( QRgb* )inputBlock->bits();
92  QRgb* outputBits = ( QRgb* )outputBlock->bits();
93  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
94  {
95  QRgb c = inputBits[i];
96  outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
97  }
98 
99  delete inputBlock;
100  return outputBlock;
101 }
102 
103 void QgsSingleBandColorDataRenderer::writeXml( QDomDocument& doc, QDomElement& parentElem ) const
104 {
105  if ( parentElem.isNull() )
106  return;
107 
108  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
109  _writeXml( doc, rasterRendererElem );
110  rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
111  parentElem.appendChild( rasterRendererElem );
112 }
113 
115 {
116  QList<int> bandList;
117  if ( mBand != -1 )
118  {
119  bandList << mBand;
120  }
121  return bandList;
122 }
123 
125 {
126  // Renderer can only work with numerical values in at least 1 band
127  if ( !input ) return false;
128 
129  if ( !mOn )
130  {
131  // In off mode we can connect to anything
132  mInput = input;
133  return true;
134  }
135 
136  if ( input->dataType( 1 ) == Qgis::ARGB32 ||
137  input->dataType( 1 ) == Qgis::ARGB32_Premultiplied )
138  {
139  mInput = input;
140  return true;
141  }
142  return false;
143 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
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:33
virtual QgsRasterInterface * input() const
Current input.
void copyCommonProperties(const QgsRasterRenderer *other)
Copies common properties like opacity / transparency data from other renderer.
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:75
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) ...
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:336
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:74