QGIS API Documentation  2.99.0-Master (ae4d26a)
qgsbrightnesscontrastfilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbrightnesscontrastfilter.cpp
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
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 
18 #include "qgsrasterdataprovider.h"
20 
21 #include <QDomDocument>
22 #include <QDomElement>
23 
25  : QgsRasterInterface( input )
26 {
27 }
28 
30 {
31  QgsDebugMsgLevel( "Entered", 4 );
33  filter->setBrightness( mBrightness );
34  filter->setContrast( mContrast );
35  return filter;
36 }
37 
39 {
40  if ( mOn )
41  {
42  return 1;
43  }
44 
45  if ( mInput )
46  {
47  return mInput->bandCount();
48  }
49 
50  return 0;
51 }
52 
54 {
55  if ( mOn )
56  {
58  }
59 
60  if ( mInput )
61  {
62  return mInput->dataType( bandNo );
63  }
64 
65  return Qgis::UnknownDataType;
66 }
67 
69 {
70  QgsDebugMsgLevel( "Entered", 4 );
71 
72  // Brightness filter can only work with single band ARGB32_Premultiplied
73  if ( !input )
74  {
75  QgsDebugMsgLevel( "No input", 4 );
76  return false;
77  }
78 
79  if ( !mOn )
80  {
81  // In off mode we can connect to anything
82  QgsDebugMsgLevel( "OK", 4 );
83  mInput = input;
84  return true;
85  }
86 
87  if ( input->bandCount() < 1 )
88  {
89  QgsDebugMsg( "No input band" );
90  return false;
91  }
92 
93  if ( input->dataType( 1 ) != Qgis::ARGB32_Premultiplied &&
94  input->dataType( 1 ) != Qgis::ARGB32 )
95  {
96  QgsDebugMsg( "Unknown input data type" );
97  return false;
98  }
99 
100  mInput = input;
101  QgsDebugMsgLevel( "OK", 4 );
102  return true;
103 }
104 
105 QgsRasterBlock *QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
106 {
107  Q_UNUSED( bandNo );
108  QgsDebugMsgLevel( QString( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );
109 
110  std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );
111  if ( !mInput )
112  {
113  return outputBlock.release();
114  }
115 
116  // At this moment we know that we read rendered image
117  int bandNumber = 1;
118  std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, extent, width, height, feedback ) );
119  if ( !inputBlock || inputBlock->isEmpty() )
120  {
121  QgsDebugMsg( "No raster data!" );
122  return outputBlock.release();
123  }
124 
125  if ( mBrightness == 0 && mContrast == 0 )
126  {
127  QgsDebugMsgLevel( "No brightness changes.", 4 );
128  return inputBlock.release();
129  }
130 
131  if ( !outputBlock->reset( Qgis::ARGB32_Premultiplied, width, height ) )
132  {
133  return outputBlock.release();
134  }
135 
136  // adjust image
137  QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
138  QRgb myColor;
139 
140  int r, g, b, alpha;
141  double f = std::pow( ( mContrast + 100 ) / 100.0, 2 );
142 
143  for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
144  {
145  if ( inputBlock->color( i ) == myNoDataColor )
146  {
147  outputBlock->setColor( i, myNoDataColor );
148  continue;
149  }
150 
151  myColor = inputBlock->color( i );
152  alpha = qAlpha( myColor );
153 
154  r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f );
155  g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f );
156  b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f );
157 
158  outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
159  }
160 
161  return outputBlock.release();
162 }
163 
164 int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int alpha, int brightness, double contrastFactor ) const
165 {
166  if ( alpha == 255 )
167  {
168  // Opaque pixel, do simpler math
169  return qBound( 0, ( int )( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255 );
170  }
171  else if ( alpha == 0 )
172  {
173  // Totally transparent pixel
174  return 0;
175  }
176  else
177  {
178  // Semi-transparent pixel. We need to adjust the math since we are using Qgis::ARGB32_Premultiplied
179  // and color values have been premultiplied by alpha
180  double alphaFactor = alpha / 255.;
181  double adjustedColor = colorComponent / alphaFactor;
182 
183  // Make sure to return a premultiplied color
184  return alphaFactor * qBound( 0., ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255. );
185  }
186 }
187 
188 void QgsBrightnessContrastFilter::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
189 {
190  if ( parentElem.isNull() )
191  {
192  return;
193  }
194 
195  QDomElement filterElem = doc.createElement( QStringLiteral( "brightnesscontrast" ) );
196 
197  filterElem.setAttribute( QStringLiteral( "brightness" ), QString::number( mBrightness ) );
198  filterElem.setAttribute( QStringLiteral( "contrast" ), QString::number( mContrast ) );
199  parentElem.appendChild( filterElem );
200 }
201 
202 void QgsBrightnessContrastFilter::readXml( const QDomElement &filterElem )
203 {
204  if ( filterElem.isNull() )
205  {
206  return;
207  }
208 
209  mBrightness = filterElem.attribute( QStringLiteral( "brightness" ), QStringLiteral( "0" ) ).toInt();
210  mContrast = filterElem.attribute( QStringLiteral( "contrast" ), QStringLiteral( "0" ) ).toInt();
211 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
QgsBrightnessContrastFilter * clone() const override
Clone itself, create deep copy.
virtual QgsRectangle extent() const
Get the extent of the interface.
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
virtual QgsRasterInterface * input() const
Current input.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
DataType
Raster data types.
Definition: qgis.h:78
int bandCount() const override
Get number of bands.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:93
Raster data container.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:38
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.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Unknown or unspecified type.
Definition: qgis.h:80
Base class for processing filters like renderers, reprojector, resampler etc.
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:452
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
bool setInput(QgsRasterInterface *input) override
Set input.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Brightness/contrast filter pipe for rasters.
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:92
QgsBrightnessContrastFilter(QgsRasterInterface *input=nullptr)