QGIS API Documentation  2.4.0-Chugiak
 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 "qgsrasterviewport.h"
20 #include <QDomDocument>
21 #include <QDomElement>
22 #include <QImage>
23 
25  QgsRasterRenderer( input, "singlebandcolordata" ), mBand( band )
26 {
27 
28 }
29 
31 {
32 }
33 
35 {
37  renderer->setOpacity( mOpacity );
38  renderer->setAlphaBand( mAlphaBand );
40  return renderer;
41 }
42 
44 {
45  if ( elem.isNull() )
46  {
47  return 0;
48  }
49 
50  int band = elem.attribute( "band", "-1" ).toInt();
51  QgsRasterRenderer* r = new QgsSingleBandColorDataRenderer( input, band );
52  r->readXML( elem );
53  return r;
54 }
55 
56 QgsRasterBlock* QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
57 {
58  Q_UNUSED( bandNo );
59 
60  QgsRasterBlock *outputBlock = new QgsRasterBlock();
61  if ( !mInput )
62  {
63  return outputBlock;
64  }
65 
66  QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height );
67  if ( !inputBlock || inputBlock->isEmpty() )
68  {
69  QgsDebugMsg( "No raster data!" );
70  delete inputBlock;
71  return outputBlock;
72  }
73 
74  bool hasTransparency = usesTransparency();
75  if ( !hasTransparency )
76  {
77  // Nothing to do, just retype if necessary
78  inputBlock->convert( QGis::ARGB32_Premultiplied );
79  delete outputBlock;
80  return inputBlock;
81  }
82 
83  if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) )
84  {
85  delete inputBlock;
86  return outputBlock;
87  }
88 
89  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
90  {
91  QRgb pixelColor;
92  QRgb c = inputBlock->color( i );
93  double alpha = qAlpha( c );
94  pixelColor = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * alpha );
95  outputBlock->setColor( i, pixelColor );
96  }
97 
98  delete inputBlock;
99  return outputBlock;
100 }
101 
102 void QgsSingleBandColorDataRenderer::writeXML( QDomDocument& doc, QDomElement& parentElem ) const
103 {
104  if ( parentElem.isNull() )
105  return;
106 
107  QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
108  _writeXML( doc, rasterRendererElem );
109  rasterRendererElem.setAttribute( "band", mBand );
110  parentElem.appendChild( rasterRendererElem );
111 }
112 
114 {
115  QList<int> bandList;
116  if ( mBand != -1 )
117  {
118  bandList << mBand;
119  }
120  return bandList;
121 }
122 
124 {
125  // Renderer can only work with numerical values in at least 1 band
126  if ( !input ) return false;
127 
128  if ( !mOn )
129  {
130  // In off mode we can connect to anything
131  mInput = input;
132  return true;
133  }
134 
135  if ( input->dataType( 1 ) == QGis::ARGB32 ||
136  input->dataType( 1 ) == QGis::ARGB32_Premultiplied )
137  {
138  mInput = input;
139  return true;
140  }
141  return false;
142 }
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:36
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:424
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.
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.