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();
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
bool setInput(QgsRasterInterface *input) override
Set input.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height) override
Read block of data using given extent and size.
QgsHueSaturationFilter(QgsRasterInterface *input=0)
virtual QgsRasterInterface * input() const
Current input.
int bandCount() const override
Get number of bands.
QGis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
bool setColor(int row, int column, QRgb color)
Set color on position.
QgsRasterInterface * clone() const override
Clone itself, create deep copy.
void setColorizeColor(QColor colorizeColor)
~QgsHueSaturationFilter()
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
void setColorizeOn(bool colorizeOn)
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...
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
QColor colorizeColor() const
virtual QgsRectangle extent()
Get the extent of the interface.
void readXML(const QDomElement &filterElem) override
Sets base class members from xml.
Color and saturation filter pipe for rasters.
DataType
Raster data types.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
void setColorizeStrength(int colorizeStrength)
QRgb color(int row, int column) const
Read a single color.
QgsRasterInterface * mInput
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
void setGrayscaleMode(QgsHueSaturationFilter::GrayscaleMode grayscaleMode)
void setSaturation(int saturation)
bool isEmpty() const
Returns true if block is empty, i.e.