QGIS API Documentation 3.99.0-Master (a26b91b364d)
qgshuesaturationfilter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshuesaturationfilter.cpp
3 ---------------------
4 begin : February 2013
5 copyright : (C) 2013 by Alexander Bruy, Nyall Dawson
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
20
21#include <QDomDocument>
22#include <QDomElement>
23
24
26 : QgsRasterInterface( input )
27 , mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
28{
29}
30
32{
33 QgsDebugMsgLevel( QStringLiteral( "Entered hue/saturation filter" ), 4 );
34 QgsHueSaturationFilter *filter = new QgsHueSaturationFilter( nullptr );
35 filter->setInvertColors( mInvertColors );
36 filter->setSaturation( mSaturation );
37 filter->setGrayscaleMode( mGrayscaleMode );
38 filter->setColorizeOn( mColorizeOn );
39 filter->setColorizeColor( mColorizeColor );
40 filter->setColorizeStrength( mColorizeStrength );
41 return filter;
42}
43
45{
46 if ( mOn )
47 {
48 return 1;
49 }
50
51 if ( mInput )
52 {
53 return mInput->bandCount();
54 }
55
56 return 0;
57}
58
60{
61 if ( mOn )
62 {
64 }
65
66 if ( mInput )
67 {
68 return mInput->dataType( bandNo );
69 }
70
72}
73
75{
76 QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
77
78 // Hue/saturation filter can only work with single band ARGB32_Premultiplied
79 if ( !input )
80 {
81 QgsDebugError( QStringLiteral( "No input" ) );
82 return false;
83 }
84
85 if ( !mOn )
86 {
87 // In off mode we can connect to anything
88 QgsDebugMsgLevel( QStringLiteral( "OK" ), 4 );
89 mInput = input;
90 return true;
91 }
92
93 if ( input->bandCount() < 1 )
94 {
95 QgsDebugError( QStringLiteral( "No input band" ) );
96 return false;
97 }
98
101 {
102 QgsDebugError( QStringLiteral( "Unknown input data type" ) );
103 return false;
104 }
105
106 mInput = input;
107 QgsDebugMsgLevel( QStringLiteral( "OK" ), 4 );
108 return true;
109}
110
111QgsRasterBlock *QgsHueSaturationFilter::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
112{
113 Q_UNUSED( bandNo )
114 QgsDebugMsgLevel( QStringLiteral( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ), 4 );
115
116 if ( !mInput )
117 {
118 return nullptr;
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( QStringLiteral( "No raster data!" ) );
127 return nullptr;
128 }
129
130 if ( !mInvertColors && mSaturation == 0 && mGrayscaleMode == GrayscaleOff && !mColorizeOn )
131 {
132 QgsDebugMsgLevel( QStringLiteral( "No hue/saturation change." ), 4 );
133 return inputBlock.release();
134 }
135
136 auto outputBlock = std::make_unique<QgsRasterBlock>();
137
138 if ( !outputBlock->reset( Qgis::DataType::ARGB32_Premultiplied, width, height ) )
139 {
140 return nullptr;
141 }
142
143 // adjust image
144 const QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
145 int h, s, l;
146 int r, g, b;
147 double alphaFactor = 1.0;
148
149 const QRgb *inputColorData = inputBlock->colorData();
150 const int imageHeight = inputBlock->image().height();
151 const int imageWidth = inputBlock->image().width();
152
153 QRgb *outputColorData = outputBlock->colorData();
154
155 for ( int row = 0; row < height; ++row )
156 {
157 if ( feedback->isCanceled() )
158 return nullptr;
159
160 for ( int col = 0; col < width; ++col )
161 {
162 const qgssize i = static_cast< qgssize >( row ) * width + static_cast< qgssize >( col );
163
164 if ( !inputColorData || row >= imageHeight || col >= imageWidth || inputColorData[i] == myNoDataColor )
165 {
166 outputColorData[i] = myNoDataColor;
167 continue;
168 }
169
170 const QRgb inputColor = inputColorData[i];
171 QColor myColor = QColor( inputColor );
172
173 // Alpha must be taken from QRgb, since conversion from QRgb->QColor loses alpha
174 const int alpha = qAlpha( inputColor );
175
176 if ( alpha == 0 )
177 {
178 // totally transparent, no changes required
179 outputColorData[i] = inputColor;
180 continue;
181 }
182
183 // Get rgb for color
184 myColor.getRgb( &r, &g, &b );
185
186 if ( mInvertColors || alpha != 255 )
187 {
188 if ( alpha != 255 )
189 {
190 // Semi-transparent pixel. We need to adjust the colors since we are using Qgis::DataType::ARGB32_Premultiplied
191 // and color values have been premultiplied by alpha
192 alphaFactor = alpha / 255.;
193 r /= alphaFactor;
194 g /= alphaFactor;
195 b /= alphaFactor;
196 }
197 if ( mInvertColors )
198 {
199 r = 255 - r;
200 g = 255 - g;
201 b = 255 - b;
202 }
203 myColor = QColor::fromRgb( r, g, b );
204 }
205
206 myColor.getHsl( &h, &s, &l );
207
208 // Changing saturation?
209 if ( ( mGrayscaleMode != GrayscaleOff ) || ( mSaturationScale != 1 ) )
210 {
211 processSaturation( r, g, b, h, s, l );
212 }
213
214 // Colorizing?
215 if ( mColorizeOn )
216 {
217 processColorization( r, g, b, h, s, l );
218 }
219
220 // Convert back to rgb
221 if ( alpha != 255 )
222 {
223 // Transparent pixel, need to premultiply color components
224 r *= alphaFactor;
225 g *= alphaFactor;
226 b *= alphaFactor;
227 }
228
229 outputColorData[i] = qRgba( r, g, b, alpha );
230 }
231 }
232
233 return outputBlock.release();
234}
235
236// Process a colorization and update resultant HSL & RGB values
237void QgsHueSaturationFilter::processColorization( int &r, int &g, int &b, int &h, int &s, int &l ) const
238{
239 QColor myColor;
240
241 // Overwrite hue and saturation with values from colorize color
242 h = mColorizeH;
243 s = mColorizeS;
244
245
246 QColor colorizedColor = QColor::fromHsl( h, s, l );
247
248 if ( mColorizeStrength == 100 )
249 {
250 // Full strength
251 myColor = colorizedColor;
252
253 // RGB may have changed, update them
254 myColor.getRgb( &r, &g, &b );
255 }
256 else
257 {
258 // Get rgb for colorized color
259 int colorizedR, colorizedG, colorizedB;
260 colorizedColor.getRgb( &colorizedR, &colorizedG, &colorizedB );
261
262 // Now, linearly scale by colorize strength
263 double p = ( double ) mColorizeStrength / 100.;
264 r = p * colorizedR + ( 1 - p ) * r;
265 g = p * colorizedG + ( 1 - p ) * g;
266 b = p * colorizedB + ( 1 - p ) * b;
267
268 // RGB changed, so update HSL values
269 myColor = QColor::fromRgb( r, g, b );
270 myColor.getHsl( &h, &s, &l );
271 }
272}
273
274// Process a change in saturation and update resultant HSL & RGB values
275void QgsHueSaturationFilter::processSaturation( int &r, int &g, int &b, int &h, int &s, int &l )
276{
277
278 QColor myColor;
279
280 // Are we converting layer to grayscale?
281 switch ( mGrayscaleMode )
282 {
284 {
285 // Lightness mode, set saturation to zero
286 s = 0;
287
288 // Saturation changed, so update rgb values
289 myColor = QColor::fromHsl( h, s, l );
290 myColor.getRgb( &r, &g, &b );
291 return;
292 }
294 {
295 // Grayscale by weighted rgb components
296 int luminosity = 0.21 * r + 0.72 * g + 0.07 * b;
297 r = g = b = luminosity;
298
299 // RGB changed, so update HSL values
300 myColor = QColor::fromRgb( r, g, b );
301 myColor.getHsl( &h, &s, &l );
302 return;
303 }
304 case GrayscaleAverage:
305 {
306 // Grayscale by average of rgb components
307 int average = ( r + g + b ) / 3;
308 r = g = b = average;
309
310 // RGB changed, so update HSL values
311 myColor = QColor::fromRgb( r, g, b );
312 myColor.getHsl( &h, &s, &l );
313 return;
314 }
315 case GrayscaleOff:
316 {
317 // Not being made grayscale, do saturation change
318 if ( mSaturationScale < 1 )
319 {
320 // Lowering the saturation. Use a simple linear relationship
321 s = std::min( ( int )( s * mSaturationScale ), 255 );
322 }
323 else
324 {
325 // Raising the saturation. Use a saturation curve to prevent
326 // clipping at maximum saturation with ugly results.
327 s = std::min( ( int )( 255. * ( 1 - std::pow( 1 - ( s / 255. ), std::pow( mSaturationScale, 2 ) ) ) ), 255 );
328 }
329
330 // Saturation changed, so update rgb values
331 myColor = QColor::fromHsl( h, s, l );
332 myColor.getRgb( &r, &g, &b );
333 return;
334 }
335 }
336}
337
339{
340 mSaturation = std::clamp( saturation, -100, 100 );
341
342 // Scale saturation value to [0-2], where 0 = desaturated
343 mSaturationScale = ( ( double ) mSaturation / 100 ) + 1;
344}
345
346void QgsHueSaturationFilter::setColorizeColor( const QColor &colorizeColor )
347{
348 mColorizeColor = colorizeColor;
349
350 // Get hue, saturation for colorized color
351 mColorizeH = mColorizeColor.hue();
352 mColorizeS = mColorizeColor.saturation();
353}
354
355void QgsHueSaturationFilter::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
356{
357 if ( parentElem.isNull() )
358 {
359 return;
360 }
361
362 QDomElement filterElem = doc.createElement( QStringLiteral( "huesaturation" ) );
363
364 filterElem.setAttribute( QStringLiteral( "saturation" ), QString::number( mSaturation ) );
365 filterElem.setAttribute( QStringLiteral( "grayscaleMode" ), QString::number( mGrayscaleMode ) );
366 filterElem.setAttribute( QStringLiteral( "invertColors" ), QString::number( mInvertColors ) );
367 filterElem.setAttribute( QStringLiteral( "colorizeOn" ), QString::number( mColorizeOn ) );
368 filterElem.setAttribute( QStringLiteral( "colorizeRed" ), QString::number( mColorizeColor.red() ) );
369 filterElem.setAttribute( QStringLiteral( "colorizeGreen" ), QString::number( mColorizeColor.green() ) );
370 filterElem.setAttribute( QStringLiteral( "colorizeBlue" ), QString::number( mColorizeColor.blue() ) );
371 filterElem.setAttribute( QStringLiteral( "colorizeStrength" ), QString::number( mColorizeStrength ) );
372
373 parentElem.appendChild( filterElem );
374}
375
376void QgsHueSaturationFilter::readXml( const QDomElement &filterElem )
377{
378 if ( filterElem.isNull() )
379 {
380 return;
381 }
382
383 setSaturation( filterElem.attribute( QStringLiteral( "saturation" ), QStringLiteral( "0" ) ).toInt() );
384 mGrayscaleMode = static_cast< QgsHueSaturationFilter::GrayscaleMode >( filterElem.attribute( QStringLiteral( "grayscaleMode" ), QStringLiteral( "0" ) ).toInt() );
385 mInvertColors = static_cast< bool >( filterElem.attribute( QStringLiteral( "invertColors" ), QStringLiteral( "0" ) ).toInt() );
386
387 mColorizeOn = static_cast< bool >( filterElem.attribute( QStringLiteral( "colorizeOn" ), QStringLiteral( "0" ) ).toInt() );
388 int mColorizeRed = filterElem.attribute( QStringLiteral( "colorizeRed" ), QStringLiteral( "255" ) ).toInt();
389 int mColorizeGreen = filterElem.attribute( QStringLiteral( "colorizeGreen" ), QStringLiteral( "128" ) ).toInt();
390 int mColorizeBlue = filterElem.attribute( QStringLiteral( "colorizeBlue" ), QStringLiteral( "128" ) ).toInt();
391 setColorizeColor( QColor::fromRgb( mColorizeRed, mColorizeGreen, mColorizeBlue ) );
392 mColorizeStrength = filterElem.attribute( QStringLiteral( "colorizeStrength" ), QStringLiteral( "100" ) ).toInt();
393
394}
DataType
Raster data types.
Definition qgis.h:351
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Color and saturation filter pipe for rasters.
void setColorizeOn(bool colorizeOn)
void setSaturation(int saturation)
void setGrayscaleMode(QgsHueSaturationFilter::GrayscaleMode grayscaleMode)
QgsHueSaturationFilter(QgsRasterInterface *input=nullptr)
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
QgsHueSaturationFilter * clone() const override
Clone itself, create deep copy.
void setInvertColors(bool invertColors)
Sets whether the filter will invert colors.
void setColorizeColor(const QColor &colorizeColor)
void setColorizeStrength(int colorizeStrength)
int bandCount() const override
Gets number of bands.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
bool setInput(QgsRasterInterface *input) override
Set input.
Feedback object tailored for raster block reading.
Raster data container.
Base class for processing filters like renderers, reprojector, resampler etc.
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.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
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:6916
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
#define QgsDebugError(str)
Definition qgslogger.h:40