QGIS API Documentation  2.99.0-Master (8ec3eaf)
qgssinglebandpseudocolorrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglebandpseudocolorrenderer.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 "qgscolorrampshader.h"
20 #include "qgsrastershader.h"
21 #include "qgsrastertransparency.h"
22 #include "qgsrasterviewport.h"
23 #include <QDomDocument>
24 #include <QDomElement>
25 #include <QImage>
26 
28  QgsRasterRenderer( input, QStringLiteral( "singlebandpseudocolor" ) )
29  , mShader( shader )
30  , mBand( band )
31  , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
32  , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
33  , mClassificationMinMaxOrigin( QgsRasterRenderer::MinMaxUnknown )
34 {
35 }
36 
38 {
39  delete mShader;
40 }
41 
43 {
44  if ( bandNo > mInput->bandCount() || bandNo <= 0 )
45  {
46  return;
47  }
48  mBand = bandNo;
49 }
50 
52 {
53  QgsRasterShader *shader = nullptr;
54 
55  if ( mShader )
56  {
57  shader = new QgsRasterShader( mShader->minimumValue(), mShader->maximumValue() );
58 
59  // Shader function
60  const QgsColorRampShader* origColorRampShader = dynamic_cast<const QgsColorRampShader*>( mShader->rasterShaderFunction() );
61 
62  if ( origColorRampShader )
63  {
64  QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() );
65 
66  if ( origColorRampShader->sourceColorRamp() )
67  {
68  colorRampShader->setSourceColorRamp( origColorRampShader->sourceColorRamp()->clone() );
69  }
70  colorRampShader->setColorRampType( origColorRampShader->colorRampType() );
71  colorRampShader->setClip( origColorRampShader->clip() );
72  colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() );
73  shader->setRasterShaderFunction( colorRampShader );
74  }
75  }
76  QgsSingleBandPseudoColorRenderer * renderer = new QgsSingleBandPseudoColorRenderer( nullptr, mBand, shader );
77  renderer->copyCommonProperties( this );
78 
79  return renderer;
80 }
81 
83 {
84  delete mShader;
85  mShader = shader;
86 }
87 
89 {
90  if ( elem.isNull() )
91  {
92  return nullptr;
93  }
94 
95  int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
96  QgsRasterShader* shader = nullptr;
97  QDomElement rasterShaderElem = elem.firstChildElement( QStringLiteral( "rastershader" ) );
98  if ( !rasterShaderElem.isNull() )
99  {
100  shader = new QgsRasterShader();
101  shader->readXml( rasterShaderElem );
102  }
103 
105  r->readXml( elem );
106 
107  // TODO: add _readXML in superclass?
108  r->setClassificationMin( elem.attribute( QStringLiteral( "classificationMin" ), QStringLiteral( "NaN" ) ).toDouble() );
109  r->setClassificationMax( elem.attribute( QStringLiteral( "classificationMax" ), QStringLiteral( "NaN" ) ).toDouble() );
110  r->setClassificationMinMaxOrigin( QgsRasterRenderer::minMaxOriginFromName( elem.attribute( QStringLiteral( "classificationMinMaxOrigin" ), QStringLiteral( "Unknown" ) ) ) );
111 
112  return r;
113 }
114 
116 {
117  Q_UNUSED( bandNo );
118 
119  QgsRasterBlock *outputBlock = new QgsRasterBlock();
120  if ( !mInput || !mShader )
121  {
122  return outputBlock;
123  }
124 
125 
126  QgsRasterBlock *inputBlock = mInput->block( mBand, extent, width, height, feedback );
127  if ( !inputBlock || inputBlock->isEmpty() )
128  {
129  QgsDebugMsg( "No raster data!" );
130  delete inputBlock;
131  return outputBlock;
132  }
133 
134  //rendering is faster without considering user-defined transparency
135  bool hasTransparency = usesTransparency();
136 
137  QgsRasterBlock *alphaBlock = nullptr;
138  if ( mAlphaBand > 0 && mAlphaBand != mBand )
139  {
140  alphaBlock = mInput->block( mAlphaBand, extent, width, height, feedback );
141  if ( !alphaBlock || alphaBlock->isEmpty() )
142  {
143  delete inputBlock;
144  delete alphaBlock;
145  return outputBlock;
146  }
147  }
148  else if ( mAlphaBand == mBand )
149  {
150  alphaBlock = inputBlock;
151  }
152 
153  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
154  {
155  delete inputBlock;
156  delete alphaBlock;
157  return outputBlock;
158  }
159 
160  QRgb myDefaultColor = NODATA_COLOR;
161 
162  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
163  {
164  if ( inputBlock->isNoData( i ) )
165  {
166  outputBlock->setColor( i, myDefaultColor );
167  continue;
168  }
169  double val = inputBlock->value( i );
170  int red, green, blue, alpha;
171  if ( !mShader->shade( val, &red, &green, &blue, &alpha ) )
172  {
173  outputBlock->setColor( i, myDefaultColor );
174  continue;
175  }
176 
177  if ( alpha < 255 )
178  {
179  // Working with premultiplied colors, so multiply values by alpha
180  red *= ( alpha / 255.0 );
181  blue *= ( alpha / 255.0 );
182  green *= ( alpha / 255.0 );
183  }
184 
185  if ( !hasTransparency )
186  {
187  outputBlock->setColor( i, qRgba( red, green, blue, alpha ) );
188  }
189  else
190  {
191  //opacity
192  double currentOpacity = mOpacity;
193  if ( mRasterTransparency )
194  {
195  currentOpacity = mRasterTransparency->alphaValue( val, mOpacity * 255 ) / 255.0;
196  }
197  if ( mAlphaBand > 0 )
198  {
199  currentOpacity *= alphaBlock->value( i ) / 255.0;
200  }
201 
202  outputBlock->setColor( i, qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha ) );
203  }
204  }
205 
206  delete inputBlock;
207  if ( mAlphaBand > 0 && mBand != mAlphaBand )
208  {
209  delete alphaBlock;
210  }
211 
212  return outputBlock;
213 }
214 
215 void QgsSingleBandPseudoColorRenderer::writeXml( QDomDocument& doc, QDomElement& parentElem ) const
216 {
217  if ( parentElem.isNull() )
218  {
219  return;
220  }
221 
222  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
223  _writeXml( doc, rasterRendererElem );
224  rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
225  if ( mShader )
226  {
227  mShader->writeXml( doc, rasterRendererElem ); //todo: include color ramp items directly in this renderer
228  }
229  rasterRendererElem.setAttribute( QStringLiteral( "classificationMin" ), QgsRasterBlock::printValue( mClassificationMin ) );
230  rasterRendererElem.setAttribute( QStringLiteral( "classificationMax" ), QgsRasterBlock::printValue( mClassificationMax ) );
231  rasterRendererElem.setAttribute( QStringLiteral( "classificationMinMaxOrigin" ), QgsRasterRenderer::minMaxOriginName( mClassificationMinMaxOrigin ) );
232 
233  parentElem.appendChild( rasterRendererElem );
234 }
235 
236 void QgsSingleBandPseudoColorRenderer::legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const
237 {
238  if ( mShader )
239  {
240  QgsRasterShaderFunction* shaderFunction = mShader->rasterShaderFunction();
241  if ( shaderFunction )
242  {
243  shaderFunction->legendSymbologyItems( symbolItems );
244  }
245  }
246 }
247 
249 {
250  QList<int> bandList;
251  if ( mBand != -1 )
252  {
253  bandList << mBand;
254  }
255  return bandList;
256 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Interface for all raster shaders.
static QString printValue(double value)
Print double value with all necessary significant digits.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Get symbology items if provided by renderer.
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
virtual QgsRectangle extent() const
Get the extent of the interface.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
int alphaValue(double, int theGlobalTransparency=255) const
Returns the transparency value for a single value Pixel.
virtual QgsRasterInterface * input() const
Current input.
void copyCommonProperties(const QgsRasterRenderer *other)
Copies common properties like opacity / transparency data from other renderer.
static int minMaxOriginFromName(const QString &theName)
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
virtual void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const
bool isNoData(int row, int column)
Check if value at position is no data.
void setShader(QgsRasterShader *shader)
Takes ownership of the shader.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
bool setColor(int row, int column, QRgb color)
Set color on position.
static const QRgb NODATA_COLOR
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:75
Raster data container.
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band, QgsRasterShader *shader)
Note: takes ownership of QgsRasterShader.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
QgsRasterShaderFunction * rasterShaderFunction()
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
double maximumValue()
Return the maximum value for the raster shader.
Raster renderer pipe for single band pseudocolor.
bool usesTransparency() const
bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
bool isEmpty() const
Returns true if block is empty, i.e.
int mAlphaBand
Read alpha value from band.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
void setRasterShaderFunction(QgsRasterShaderFunction *)
A public method that allows the user to set their own shader function.
Base class for processing filters like renderers, reprojector, resampler etc.
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
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:336
bool reset(Qgis::DataType theDataType, int theWidth, int theHeight)
Reset block.
void writeXml(QDomDocument &doc, QDomElement &parent) const
static QString minMaxOriginName(int theOrigin)
double minimumValue()
Return the minimum value for the raster shader.
double value(int row, int column) const
Read a single value if type of block is numeric.
void setBand(int bandNo)
Sets the band used by the renderer.
double mOpacity
Global alpha value (0-1)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Raster renderer pipe that applies colors to a raster.
int band() const
Returns the band used by the renderer.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void readXml(const QDomElement &elem)