QGIS API Documentation  2.2.0-Valmiera
 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 }