QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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, "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( "band", "-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 )
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 );
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 
104 {
105  if ( parentElem.isNull() )
106  return;
107 
108  QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
109  _writeXML( doc, rasterRendererElem );
110  rasterRendererElem.setAttribute( "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.
bool convert(QGis::DataType destDataType)
Convert data to different type.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height) override
Read block of data using given extent and size.
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void copyCommonProperties(const QgsRasterRenderer *other)
Copies common properties like opacity / transparency data from other renderer.
QgsSingleBandColorDataRenderer * clone() const override
Clone itself, create deep copy.
virtual QgsRasterInterface * input() const
Current input.
QgsSingleBandColorDataRenderer(QgsRasterInterface *input, int band)
void readXML(const QDomElement &rendererElem) override
Sets base class members from xml.
Raster renderer pipe for single band color.
bool usesTransparency() const
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:150
Raster data container.
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:148
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
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:500
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
bool isNull() const
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
virtual QgsRectangle extent()
Get the extent of the interface.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
char * bits(int row, int column)
Get pointer to data.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
double mOpacity
Global alpha value (0-1)
QDomElement createElement(const QString &tagName)
void _writeXML(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXML method of subclasses) ...
QgsRasterInterface * mInput
Raster renderer pipe that applies colors to a raster.
bool isEmpty() const
Returns true if block is empty, i.e.