QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
38  renderer->setOpacity( mOpacity );
39  renderer->setAlphaBand( mAlphaBand );
41  return renderer;
42 }
43 
45 {
46  if ( elem.isNull() )
47  {
48  return 0;
49  }
50 
51  int band = elem.attribute( "band", "-1" ).toInt();
52  QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band );
53  r->readXML( elem );
54  return r;
55 }
56 
57 QgsRasterBlock* QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
58 {
59  Q_UNUSED( bandNo );
60 
61  QgsRasterBlock *outputBlock = new QgsRasterBlock();
62  if ( !mInput )
63  {
64  return outputBlock;
65  }
66 
67  QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height );
68  if ( !inputBlock || inputBlock->isEmpty() )
69  {
70  QgsDebugMsg( "No raster data!" );
71  delete inputBlock;
72  return outputBlock;
73  }
74 
75  bool hasTransparency = usesTransparency();
76  if ( !hasTransparency )
77  {
78  // Nothing to do, just retype if necessary
79  inputBlock->convert( QGis::ARGB32_Premultiplied );
80  delete outputBlock;
81  return inputBlock;
82  }
83 
84  if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) )
85  {
86  delete inputBlock;
87  return outputBlock;
88  }
89 
90  // make sure input is also premultiplied!
91  inputBlock->convert( QGis::ARGB32_Premultiplied );
92 
93  QRgb* inputBits = ( QRgb* )inputBlock->bits();
94  QRgb* outputBits = ( QRgb* )outputBlock->bits();
95  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
96  {
97  QRgb c = inputBits[i];
98  outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
99  }
100 
101  delete inputBlock;
102  return outputBlock;
103 }
104 
106 {
107  if ( parentElem.isNull() )
108  return;
109 
110  QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
111  _writeXML( doc, rasterRendererElem );
112  rasterRendererElem.setAttribute( "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::ARGB32 ||
139  input->dataType( 1 ) == QGis::ARGB32_Premultiplied )
140  {
141  mInput = input;
142  return true;
143  }
144  return false;
145 }
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
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
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value.
Raster data container.
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
int mAlphaBand
Read alpha value from band.
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
void setAlphaBand(int band)
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:449
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
QgsRasterInterface * clone() const override
Clone itself, create deep copy.
bool isNull() const
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.
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)
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void setOpacity(double opacity)
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
void setRasterTransparency(QgsRasterTransparency *t)
Raster renderer pipe that applies colors to a raster.
bool isEmpty() const
Returns true if block is empty, i.e.