21 #include <QDomDocument>
22 #include <QDomElement>
28 mSaturationScale( 1 ),
31 mColorizeColor( QColor::fromRgb( 255, 128, 128 ) ),
34 mColorizeStrength( 100 )
124 QgsDebugMsg( QString(
"width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.
toString() ) );
135 if ( !inputBlock || inputBlock->
isEmpty() )
142 if ( mSaturation == 0 && mGrayscaleMode ==
GrayscaleOff && !mColorizeOn )
156 QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
161 double alphaFactor = 1.0;
165 if ( inputBlock->
color( i ) == myNoDataColor )
167 outputBlock->
setColor( i, myNoDataColor );
171 myRgb = inputBlock->
color( i );
172 myColor = QColor( myRgb );
175 alpha = qAlpha( myRgb );
185 myColor.getRgb( &r, &g, &b );
190 alphaFactor = alpha / 255.;
194 myColor = QColor::fromRgb( r, g, b );
197 myColor.getHsl( &h, &s, &l );
200 if (( mGrayscaleMode !=
GrayscaleOff ) || ( mSaturationScale != 1 ) )
202 processSaturation( r, g, b, h, s, l );
208 processColorization( r, g, b, h, s, l );
220 outputBlock->
setColor( i, qRgba( r, g, b, alpha ) );
228 void QgsHueSaturationFilter::processColorization(
int &r,
int &g,
int &b,
int &h,
int &s,
int &l )
237 QColor colorizedColor = QColor::fromHsl( h, s, l );
239 if ( mColorizeStrength == 100 )
242 myColor = colorizedColor;
245 myColor.getRgb( &r, &g, &b );
250 int colorizedR, colorizedG, colorizedB;
251 colorizedColor.getRgb( &colorizedR, &colorizedG, &colorizedB );
254 double p = ( double ) mColorizeStrength / 100.;
255 r = p * colorizedR + ( 1 - p ) * r;
256 g = p * colorizedG + ( 1 - p ) * g;
257 b = p * colorizedB + ( 1 - p ) * b;
260 myColor = QColor::fromRgb( r, g, b );
261 myColor.getHsl( &h, &s, &l );
266 void QgsHueSaturationFilter::processSaturation(
int &r,
int &g,
int &b,
int &h,
int &s,
int &l )
272 switch ( mGrayscaleMode )
280 myColor = QColor::fromHsl( h, s, l );
281 myColor.getRgb( &r, &g, &b );
287 int luminosity = 0.21 * r + 0.72 * g + 0.07 * b;
288 r = g = b = luminosity;
291 myColor = QColor::fromRgb( r, g, b );
292 myColor.getHsl( &h, &s, &l );
298 int average = ( r + g + b ) / 3;
302 myColor = QColor::fromRgb( r, g, b );
303 myColor.getHsl( &h, &s, &l );
309 if ( mSaturationScale < 1 )
312 s = qMin((
int )( s * mSaturationScale ), 255 );
318 s = qMin((
int )( 255. * ( 1 - pow( 1 - ( s / 255. ), pow( mSaturationScale, 2 ) ) ) ), 255 );
322 myColor = QColor::fromHsl( h, s, l );
323 myColor.getRgb( &r, &g, &b );
331 mSaturation = qBound( -100, saturation, 100 );
334 mSaturationScale = (( double ) mSaturation / 100 ) + 1;
342 mColorizeH = mColorizeColor.hue();
343 mColorizeS = mColorizeColor.saturation();
348 if ( parentElem.isNull() )
353 QDomElement filterElem = doc.createElement(
"huesaturation" );
355 filterElem.setAttribute(
"saturation", QString::number( mSaturation ) );
356 filterElem.setAttribute(
"grayscaleMode", QString::number( mGrayscaleMode ) );
357 filterElem.setAttribute(
"colorizeOn", QString::number( mColorizeOn ) );
358 filterElem.setAttribute(
"colorizeRed", QString::number( mColorizeColor.red() ) );
359 filterElem.setAttribute(
"colorizeGreen", QString::number( mColorizeColor.green() ) );
360 filterElem.setAttribute(
"colorizeBlue", QString::number( mColorizeColor.blue() ) );
361 filterElem.setAttribute(
"colorizeStrength", QString::number( mColorizeStrength ) );
363 parentElem.appendChild( filterElem );
368 if ( filterElem.isNull() )
373 setSaturation( filterElem.attribute(
"saturation",
"0" ).toInt() );
376 mColorizeOn = ( bool )filterElem.attribute(
"colorizeOn",
"0" ).toInt();
377 int mColorizeRed = filterElem.attribute(
"colorizeRed",
"255" ).toInt();
378 int mColorizeGreen = filterElem.attribute(
"colorizeGreen",
"128" ).toInt();
379 int mColorizeBlue = filterElem.attribute(
"colorizeBlue",
"128" ).toInt();
380 setColorizeColor( QColor::fromRgb( mColorizeRed, mColorizeGreen, mColorizeBlue ) );
381 mColorizeStrength = filterElem.attribute(
"colorizeStrength",
"100" ).toInt();