QGIS API Documentation  2.14.0-Essen
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 <qmath.h>
22 #include <QDomDocument>
23 #include <QDomElement>
24 
26  : QgsRasterInterface( input )
27  , mBrightness( 0 )
28  , mContrast( 0 )
29 {
30 }
31 
33 {
34 }
35 
37 {
38  QgsDebugMsgLevel( "Entered", 4 );
40  filter->setBrightness( mBrightness );
41  filter->setContrast( mContrast );
42  return filter;
43 }
44 
46 {
47  if ( mOn )
48  {
49  return 1;
50  }
51 
52  if ( mInput )
53  {
54  return mInput->bandCount();
55  }
56 
57  return 0;
58 }
59 
61 {
62  if ( mOn )
63  {
65  }
66 
67  if ( mInput )
68  {
69  return mInput->dataType( bandNo );
70  }
71 
72  return QGis::UnknownDataType;
73 }
74 
76 {
77  QgsDebugMsgLevel( "Entered", 4 );
78 
79  // Brightness filter can only work with single band ARGB32_Premultiplied
80  if ( !input )
81  {
82  QgsDebugMsgLevel( "No input", 4 );
83  return false;
84  }
85 
86  if ( !mOn )
87  {
88  // In off mode we can connect to anything
89  QgsDebugMsgLevel( "OK", 4 );
90  mInput = input;
91  return true;
92  }
93 
94  if ( input->bandCount() < 1 )
95  {
96  QgsDebugMsg( "No input band" );
97  return false;
98  }
99 
100  if ( input->dataType( 1 ) != QGis::ARGB32_Premultiplied &&
101  input->dataType( 1 ) != QGis::ARGB32 )
102  {
103  QgsDebugMsg( "Unknown input data type" );
104  return false;
105  }
106 
107  mInput = input;
108  QgsDebugMsgLevel( "OK", 4 );
109  return true;
110 }
111 
112 QgsRasterBlock * QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle const & extent, int width, int height )
113 {
114  Q_UNUSED( bandNo );
115  QgsDebugMsgLevel( QString( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );
116 
117  QgsRasterBlock *outputBlock = new QgsRasterBlock();
118  if ( !mInput )
119  {
120  return outputBlock;
121  }
122 
123  // At this moment we know that we read rendered image
124  int bandNumber = 1;
125  QgsRasterBlock *inputBlock = mInput->block( bandNumber, extent, width, height );
126  if ( !inputBlock || inputBlock->isEmpty() )
127  {
128  QgsDebugMsg( "No raster data!" );
129  delete inputBlock;
130  return outputBlock;
131  }
132 
133  if ( mBrightness == 0 && mContrast == 0 )
134  {
135  QgsDebugMsgLevel( "No brightness changes.", 4 );
136  delete outputBlock;
137  return inputBlock;
138  }
139 
140  if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) )
141  {
142  delete inputBlock;
143  return outputBlock;
144  }
145 
146  // adjust image
147  QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
148  QRgb myColor;
149 
150  int r, g, b, alpha;
151  double f = qPow(( mContrast + 100 ) / 100.0, 2 );
152 
153  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
154  {
155  if ( inputBlock->color( i ) == myNoDataColor )
156  {
157  outputBlock->setColor( i, myNoDataColor );
158  continue;
159  }
160 
161  myColor = inputBlock->color( i );
162  alpha = qAlpha( myColor );
163 
164  r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f );
165  g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f );
166  b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f );
167 
168  outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
169  }
170 
171  delete inputBlock;
172  return outputBlock;
173 }
174 
175 int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int alpha, int brightness, double contrastFactor ) const
176 {
177  if ( alpha == 255 )
178  {
179  // Opaque pixel, do simpler math
180  return qBound( 0, ( int )(((((( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255 );
181  }
182  else if ( alpha == 0 )
183  {
184  // Totally transparent pixel
185  return 0;
186  }
187  else
188  {
189  // Semi-transparent pixel. We need to adjust the math since we are using QGis::ARGB32_Premultiplied
190  // and color values have been premultiplied by alpha
191  double alphaFactor = alpha / 255.;
192  double adjustedColor = colorComponent / alphaFactor;
193 
194  // Make sure to return a premultiplied color
195  return alphaFactor * qBound( 0., (((((( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255. );
196  }
197 }
198 
200 {
201  if ( parentElem.isNull() )
202  {
203  return;
204  }
205 
206  QDomElement filterElem = doc.createElement( "brightnesscontrast" );
207 
208  filterElem.setAttribute( "brightness", QString::number( mBrightness ) );
209  filterElem.setAttribute( "contrast", QString::number( mContrast ) );
210  parentElem.appendChild( filterElem );
211 }
212 
214 {
215  if ( filterElem.isNull() )
216  {
217  return;
218  }
219 
220  mBrightness = filterElem.attribute( "brightness", "0" ).toInt();
221  mContrast = filterElem.attribute( "contrast", "0" ).toInt();
222 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsBrightnessContrastFilter * clone() const override
Clone itself, create deep copy.
Unknown or unspecified type.
Definition: qgis.h:131
QDomNode appendChild(const QDomNode &newChild)
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height) override
Read block of data using given extent and size.
QString attribute(const QString &name, const QString &defValue) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual QgsRasterInterface * input() const
Current input.
int bandCount() const override
Get number of bands.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
bool setColor(int row, int column, QRgb color)
Set color on position.
QGis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
QString number(int n, int base)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:146
Raster data container.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:144
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
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:392
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
bool isNull() const
virtual QgsRectangle extent()
Get the extent of the interface.
bool setInput(QgsRasterInterface *input) override
Set input.
Brightness/contrast filter pipe for rasters.
DataType
Raster data types.
Definition: qgis.h:129
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
QRgb color(int row, int column) const
Read a single color.
QDomElement createElement(const QString &tagName)
void readXML(const QDomElement &filterElem) override
Sets base class members from xml.
QgsRasterInterface * mInput
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
bool isEmpty() const
Returns true if block is empty, i.e.
QgsBrightnessContrastFilter(QgsRasterInterface *input=nullptr)