21 #include <QDomDocument>
22 #include <QDomElement>
27 , mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
101 QgsDebugMsg( QStringLiteral(
"Unknown input data type" ) );
115 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
118 return outputBlock.release();
123 std::unique_ptr< QgsRasterBlock > inputBlock(
mInput->
block( bandNumber,
extent, width, height, feedback ) );
124 if ( !inputBlock || inputBlock->isEmpty() )
126 QgsDebugMsg( QStringLiteral(
"No raster data!" ) );
127 return outputBlock.release();
130 if ( mSaturation == 0 && mGrayscaleMode ==
GrayscaleOff && !mColorizeOn )
133 return inputBlock.release();
138 return outputBlock.release();
142 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
147 double alphaFactor = 1.0;
151 if ( inputBlock->color( i ) == myNoDataColor )
153 outputBlock->setColor( i, myNoDataColor );
157 myRgb = inputBlock->color( i );
158 myColor = QColor( myRgb );
161 alpha = qAlpha( myRgb );
166 outputBlock->setColor( i, myRgb );
171 myColor.getRgb( &r, &g, &b );
176 alphaFactor = alpha / 255.;
180 myColor = QColor::fromRgb( r, g, b );
183 myColor.getHsl( &h, &s, &l );
186 if ( ( mGrayscaleMode !=
GrayscaleOff ) || ( mSaturationScale != 1 ) )
188 processSaturation( r, g, b, h, s, l );
194 processColorization( r, g, b, h, s, l );
206 outputBlock->setColor( i, qRgba( r, g, b, alpha ) );
209 return outputBlock.release();
213 void QgsHueSaturationFilter::processColorization(
int &r,
int &g,
int &b,
int &h,
int &s,
int &l )
222 QColor colorizedColor = QColor::fromHsl( h, s, l );
224 if ( mColorizeStrength == 100 )
227 myColor = colorizedColor;
230 myColor.getRgb( &r, &g, &b );
235 int colorizedR, colorizedG, colorizedB;
236 colorizedColor.getRgb( &colorizedR, &colorizedG, &colorizedB );
239 double p = ( double ) mColorizeStrength / 100.;
240 r = p * colorizedR + ( 1 - p ) * r;
241 g = p * colorizedG + ( 1 - p ) * g;
242 b = p * colorizedB + ( 1 - p ) * b;
245 myColor = QColor::fromRgb( r, g, b );
246 myColor.getHsl( &h, &s, &l );
251 void QgsHueSaturationFilter::processSaturation(
int &r,
int &g,
int &b,
int &h,
int &s,
int &l )
257 switch ( mGrayscaleMode )
265 myColor = QColor::fromHsl( h, s, l );
266 myColor.getRgb( &r, &g, &b );
272 int luminosity = 0.21 * r + 0.72 * g + 0.07 * b;
273 r = g = b = luminosity;
276 myColor = QColor::fromRgb( r, g, b );
277 myColor.getHsl( &h, &s, &l );
283 int average = ( r + g + b ) / 3;
287 myColor = QColor::fromRgb( r, g, b );
288 myColor.getHsl( &h, &s, &l );
294 if ( mSaturationScale < 1 )
297 s = std::min( (
int )( s * mSaturationScale ), 255 );
303 s = std::min( (
int )( 255. * ( 1 - std::pow( 1 - ( s / 255. ), std::pow( mSaturationScale, 2 ) ) ) ), 255 );
307 myColor = QColor::fromHsl( h, s, l );
308 myColor.getRgb( &r, &g, &b );
316 mSaturation = qBound( -100,
saturation, 100 );
319 mSaturationScale = ( ( double ) mSaturation / 100 ) + 1;
327 mColorizeH = mColorizeColor.hue();
328 mColorizeS = mColorizeColor.saturation();
333 if ( parentElem.isNull() )
338 QDomElement filterElem = doc.createElement( QStringLiteral(
"huesaturation" ) );
340 filterElem.setAttribute( QStringLiteral(
"saturation" ), QString::number( mSaturation ) );
341 filterElem.setAttribute( QStringLiteral(
"grayscaleMode" ), QString::number( mGrayscaleMode ) );
342 filterElem.setAttribute( QStringLiteral(
"colorizeOn" ), QString::number( mColorizeOn ) );
343 filterElem.setAttribute( QStringLiteral(
"colorizeRed" ), QString::number( mColorizeColor.red() ) );
344 filterElem.setAttribute( QStringLiteral(
"colorizeGreen" ), QString::number( mColorizeColor.green() ) );
345 filterElem.setAttribute( QStringLiteral(
"colorizeBlue" ), QString::number( mColorizeColor.blue() ) );
346 filterElem.setAttribute( QStringLiteral(
"colorizeStrength" ), QString::number( mColorizeStrength ) );
348 parentElem.appendChild( filterElem );
353 if ( filterElem.isNull() )
358 setSaturation( filterElem.attribute( QStringLiteral(
"saturation" ), QStringLiteral(
"0" ) ).toInt() );
361 mColorizeOn = ( bool )filterElem.attribute( QStringLiteral(
"colorizeOn" ), QStringLiteral(
"0" ) ).toInt();
362 int mColorizeRed = filterElem.attribute( QStringLiteral(
"colorizeRed" ), QStringLiteral(
"255" ) ).toInt();
363 int mColorizeGreen = filterElem.attribute( QStringLiteral(
"colorizeGreen" ), QStringLiteral(
"128" ) ).toInt();
364 int mColorizeBlue = filterElem.attribute( QStringLiteral(
"colorizeBlue" ), QStringLiteral(
"128" ) ).toInt();
365 setColorizeColor( QColor::fromRgb( mColorizeRed, mColorizeGreen, mColorizeBlue ) );
366 mColorizeStrength = filterElem.attribute( QStringLiteral(
"colorizeStrength" ), QStringLiteral(
"100" ) ).toInt();