QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #include <memory>
25 
27  QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) ), mBand( band )
28 {
29 
30 }
31 
33 {
34  QgsSingleBandColorDataRenderer *renderer = new QgsSingleBandColorDataRenderer( nullptr, mBand );
35  renderer->copyCommonProperties( this );
36  return renderer;
37 }
38 
40 {
41  if ( elem.isNull() )
42  {
43  return nullptr;
44  }
45 
46  int band = elem.attribute( QStringLiteral( "band" ), QStringLiteral( "-1" ) ).toInt();
48  r->readXml( elem );
49  return r;
50 }
51 
52 QgsRasterBlock *QgsSingleBandColorDataRenderer::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
53 {
54  Q_UNUSED( bandNo )
55 
56  std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
57  if ( !mInput )
58  {
59  return outputBlock.release();
60  }
61 
62  std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( mBand, extent, width, height, feedback ) );
63  if ( !inputBlock || inputBlock->isEmpty() )
64  {
65  QgsDebugMsg( QStringLiteral( "No raster data!" ) );
66  return outputBlock.release();
67  }
68 
69  bool hasTransparency = usesTransparency();
70  if ( !hasTransparency )
71  {
72  // Nothing to do, just retype if necessary
73  inputBlock->convert( Qgis::ARGB32_Premultiplied );
74  return inputBlock.release();
75  }
76 
77  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
78  {
79  return outputBlock.release();
80  }
81 
82  // make sure input is also premultiplied!
83  inputBlock->convert( Qgis::ARGB32_Premultiplied );
84 
85  QRgb *inputBits = ( QRgb * )inputBlock->bits();
86  QRgb *outputBits = ( QRgb * )outputBlock->bits();
87  for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
88  {
89  QRgb c = inputBits[i];
90  outputBits[i] = qRgba( mOpacity * qRed( c ), mOpacity * qGreen( c ), mOpacity * qBlue( c ), mOpacity * qAlpha( c ) );
91  }
92 
93  return outputBlock.release();
94 }
95 
96 void QgsSingleBandColorDataRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
97 {
98  if ( parentElem.isNull() )
99  return;
100 
101  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
102  _writeXml( doc, rasterRendererElem );
103  rasterRendererElem.setAttribute( QStringLiteral( "band" ), mBand );
104  parentElem.appendChild( rasterRendererElem );
105 }
106 
108 {
109  QList<int> bandList;
110  if ( mBand != -1 )
111  {
112  bandList << mBand;
113  }
114  return bandList;
115 }
116 
118 {
119  // Renderer can only work with numerical values in at least 1 band
120  if ( !input ) return false;
121 
122  if ( !mOn )
123  {
124  // In off mode we can connect to anything
125  mInput = input;
126  return true;
127  }
128 
129  if ( input->dataType( 1 ) == Qgis::ARGB32 ||
131  {
132  mInput = input;
133  return true;
134  }
135  return false;
136 }
QgsSingleBandColorDataRenderer::QgsSingleBandColorDataRenderer
QgsSingleBandColorDataRenderer(QgsRasterInterface *input, int band)
Definition: qgssinglebandcolordatarenderer.cpp:26
QgsRasterInterface::mInput
QgsRasterInterface * mInput
Definition: qgsrasterinterface.h:467
qgsrasterviewport.h
qgssinglebandcolordatarenderer.h
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsRectangle
Definition: qgsrectangle.h:41
QgsSingleBandColorDataRenderer
Definition: qgssinglebandcolordatarenderer.h:31
QgsRasterRenderer
Definition: qgsrasterrenderer.h:38
QgsRasterInterface::dataType
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QgsRasterRenderer::readXml
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Definition: qgsrasterrenderer.cpp:134
QgsRasterRenderer::_writeXml
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses)
Definition: qgsrasterrenderer.cpp:104
QgsRasterRenderer::copyCommonProperties
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
Definition: qgsrasterrenderer.cpp:162
QgsRasterRenderer::usesTransparency
bool usesTransparency() const
Definition: qgsrasterrenderer.cpp:89
QgsSingleBandColorDataRenderer::setInput
bool setInput(QgsRasterInterface *input) override
Set input.
Definition: qgssinglebandcolordatarenderer.cpp:117
QgsSingleBandColorDataRenderer::block
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Definition: qgssinglebandcolordatarenderer.cpp:52
qgsrastertransparency.h
QgsRasterInterface::mOn
bool mOn
Definition: qgsrasterinterface.h:476
Qgis::ARGB32_Premultiplied
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:129
QgsRasterInterface
Definition: qgsrasterinterface.h:116
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsSingleBandColorDataRenderer::writeXml
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
Definition: qgssinglebandcolordatarenderer.cpp:96
QgsRasterInterface::input
virtual QgsRasterInterface * input() const
Current input.
Definition: qgsrasterinterface.h:269
QgsRasterBlockFeedback
Definition: qgsrasterinterface.h:40
QgsSingleBandColorDataRenderer::clone
QgsSingleBandColorDataRenderer * clone() const override
Clone itself, create deep copy.
Definition: qgssinglebandcolordatarenderer.cpp:32
QgsRasterRenderer::mOpacity
double mOpacity
Global alpha value (0-1)
Definition: qgsrasterrenderer.h:164
QgsSingleBandColorDataRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgssinglebandcolordatarenderer.cpp:39
Qgis::ARGB32
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:128
QgsRasterInterface::block
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.
QgsSingleBandColorDataRenderer::usesBands
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
Definition: qgssinglebandcolordatarenderer.cpp:107
QgsRasterBlock
Definition: qgsrasterblock.h:36
QgsRasterInterface::extent
virtual QgsRectangle extent() const
Gets the extent of the interface.
Definition: qgsrasterinterface.h:229
qgssize
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:723