QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
19
21
22#include <QDomDocument>
23#include <QDomElement>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
32
34{
35 QgsDebugMsgLevel( u"Entered"_s, 4 );
37 filter->setBrightness( mBrightness );
38 filter->setContrast( mContrast );
39 filter->setGamma( mGamma );
40 return filter;
41}
42
44{
45 if ( mOn )
46 {
47 return 1;
48 }
49
50 if ( mInput )
51 {
52 return mInput->bandCount();
53 }
54
55 return 0;
56}
57
59{
60 if ( mOn )
61 {
63 }
64
65 if ( mInput )
66 {
67 return mInput->dataType( bandNo );
68 }
69
71}
72
74{
75 QgsDebugMsgLevel( u"Entered"_s, 4 );
76
77 // Brightness filter can only work with single band ARGB32_Premultiplied
78 if ( !input )
79 {
80 QgsDebugMsgLevel( u"No input"_s, 4 );
81 return false;
82 }
83
84 if ( !mOn )
85 {
86 // In off mode we can connect to anything
87 QgsDebugMsgLevel( u"OK"_s, 4 );
88 mInput = input;
89 return true;
90 }
91
92 if ( input->bandCount() < 1 )
93 {
94 QgsDebugError( u"No input band"_s );
95 return false;
96 }
97
98 if ( input->dataType( 1 ) != Qgis::DataType::ARGB32_Premultiplied &&
99 input->dataType( 1 ) != Qgis::DataType::ARGB32 )
100 {
101 QgsDebugError( u"Unknown input data type"_s );
102 return false;
103 }
104
105 mInput = input;
106 QgsDebugMsgLevel( u"OK"_s, 4 );
107 return true;
108}
109
111{
112 Q_UNUSED( bandNo )
113 QgsDebugMsgLevel( u"width = %1 height = %2 extent = %3"_s.arg( width ).arg( height ).arg( extent.toString() ), 4 );
114
115 auto outputBlock = std::make_unique<QgsRasterBlock>();
116 if ( !mInput )
117 {
118 return outputBlock.release();
119 }
120
121 // At this moment we know that we read rendered image
122 int bandNumber = 1;
123 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNumber, extent, width, height, feedback ) );
124 if ( !inputBlock || inputBlock->isEmpty() )
125 {
126 QgsDebugError( u"No raster data!"_s );
127 return outputBlock.release();
128 }
129
130 if ( mBrightness == 0 && mContrast == 0 && mGamma == 1.0 )
131 {
132 QgsDebugMsgLevel( u"No brightness/contrast/gamma changes."_s, 4 );
133 return inputBlock.release();
134 }
135
136 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
137 {
138 return outputBlock.release();
139 }
140
141 // adjust image
142 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
143 QRgb myColor;
144
145 int r, g, b, alpha;
146 double f = std::pow( ( mContrast + 100 ) / 100.0, 2 );
147 double gammaCorrection = 1.0 / mGamma;
148
149 for ( qgssize i = 0; i < ( qgssize )width * height; i++ )
150 {
151 if ( inputBlock->color( i ) == myNoDataColor )
152 {
153 outputBlock->setColor( i, myNoDataColor );
154 continue;
155 }
156
157 myColor = inputBlock->color( i );
158 alpha = qAlpha( myColor );
159
160 r = adjustColorComponent( qRed( myColor ), alpha, mBrightness, f, gammaCorrection );
161 g = adjustColorComponent( qGreen( myColor ), alpha, mBrightness, f, gammaCorrection );
162 b = adjustColorComponent( qBlue( myColor ), alpha, mBrightness, f, gammaCorrection );
163
164 outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
165 }
166
167 return outputBlock.release();
168}
169
171{
172 mBrightness = std::clamp( brightness, -255, 255 );
173}
174
176{
177 mContrast = std::clamp( contrast, -100, 100 );
178}
179
181{
182 mGamma = std::clamp( gamma, 0.1, 10.0 );
183}
184
185int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int alpha, int brightness, double contrastFactor, double gammaCorrection ) const
186{
187 if ( alpha == 255 )
188 {
189 // Opaque pixel, do simpler math
190 return std::clamp( ( int )( 255 * std::pow( ( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255.0, gammaCorrection ) ), 0, 255 );
191 }
192 else if ( alpha == 0 )
193 {
194 // Totally transparent pixel
195 return 0;
196 }
197 else
198 {
199 // Semi-transparent pixel. We need to adjust the math since we are using Qgis::DataType::ARGB32_Premultiplied
200 // and color values have been premultiplied by alpha
201 double alphaFactor = alpha / 255.;
202 double adjustedColor = colorComponent / alphaFactor;
203
204 // Make sure to return a premultiplied color
205 return alphaFactor * std::clamp( 255 * std::pow( ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255, gammaCorrection ), 0., 255. );
206 }
207}
208
209void QgsBrightnessContrastFilter::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
210{
211 if ( parentElem.isNull() )
212 {
213 return;
214 }
215
216 QDomElement filterElem = doc.createElement( u"brightnesscontrast"_s );
217
218 filterElem.setAttribute( u"brightness"_s, QString::number( mBrightness ) );
219 filterElem.setAttribute( u"contrast"_s, QString::number( mContrast ) );
220 filterElem.setAttribute( u"gamma"_s, QString::number( mGamma ) );
221 parentElem.appendChild( filterElem );
222}
223
224void QgsBrightnessContrastFilter::readXml( const QDomElement &filterElem )
225{
226 if ( filterElem.isNull() )
227 {
228 return;
229 }
230
231 mBrightness = filterElem.attribute( u"brightness"_s, u"0"_s ).toInt();
232 mContrast = filterElem.attribute( u"contrast"_s, u"0"_s ).toInt();
233 mGamma = filterElem.attribute( u"gamma"_s, u"1"_s ).toDouble();
234}
DataType
Raster data types.
Definition qgis.h:379
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition qgis.h:394
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:380
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition qgis.h:393
int contrast() const
Returns current contrast level.
int bandCount() const override
Gets number of bands.
QgsBrightnessContrastFilter * clone() const override
Clone itself, create deep copy.
bool setInput(QgsRasterInterface *input) override
Set input.
QgsBrightnessContrastFilter(QgsRasterInterface *input=nullptr)
int brightness() const
Returns current brightness level.
double gamma() const
Returns current gamma value.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
void setGamma(double gamma)
Set gamma value.
void setContrast(int contrast)
Set contrast level.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void setBrightness(int brightness)
Set brightness level.
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Feedback object tailored for raster block reading.
Raster data container.
QgsRasterInterface(QgsRasterInterface *input=nullptr)
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
A rectangle specified with double values.
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:7423
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59