QGIS API Documentation  2.8.2-Wien
 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  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 }