QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
91  {
92  QRgb pixelColor;
93  QRgb c = inputBlock->color( i );
94  double alpha = qAlpha( c );
95  pixelColor = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * alpha );
96  outputBlock->setColor( i, pixelColor );
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( "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 convert(QGis::DataType destDataType)
Convert data to different type.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual QgsRasterInterface * input() const
Current input.
QgsSingleBandColorDataRenderer(QgsRasterInterface *input, int band)
Raster renderer pipe for single band color.
bool usesTransparency() const
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value.
bool setColor(int row, int column, QRgb color)
Set color on position.
Raster data container.
int mAlphaBand
Read alpha value from band.
QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
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.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)
Read block of data using given extent and size.
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:420
void readXML(const QDomElement &rendererElem)
Sets base class members from xml.
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
bool setInput(QgsRasterInterface *input)
Set input.
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)
QgsRasterInterface * clone() const
Clone itself, create deep copy.
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void setOpacity(double opacity)
QRgb color(int row, int column) const
Read a single color.
void _writeXML(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXML method of subclasses) ...
QgsRasterInterface * mInput
void writeXML(QDomDocument &doc, QDomElement &parentElem) const
Write base class members to xml.
void setRasterTransparency(QgsRasterTransparency *t)
Raster renderer pipe that applies colors to a raster.
bool isEmpty() const
Returns true if block is empty, i.e.