QGIS API Documentation  2.0.1-Dufour
 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 ( size_t i = 0; i < ( size_t )width*height; i++ )
90  {
91  QRgb pixelColor;
92  double alpha = 255.0;
93  QRgb c = inputBlock->color( i );
94  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  {
107  return;
108  }
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 }