21 #include <QDomDocument> 22 #include <QDomElement> 27 , mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
101 QgsDebugMsg( QStringLiteral(
"Unknown input data type" ) );
113 QgsDebugMsgLevel( QStringLiteral(
"width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.
toString() ), 4 );
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();
virtual int bandCount() const =0
Gets number of bands.
A rectangle specified with double values.
void setColorizeColor(const QColor &colorizeColor)
bool setInput(QgsRasterInterface *input) override
Set input.
DataType
Raster data types.
virtual QgsRasterInterface * input() const
Current input.
int bandCount() const override
Gets number of bands.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
#define QgsDebugMsgLevel(str, level)
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.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Unknown or unspecified type.
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...
QColor colorizeColor() const
QgsHueSaturationFilter(QgsRasterInterface *input=nullptr)
virtual QgsRectangle extent() const
Gets the extent of the interface.
Color and saturation filter pipe for rasters.
QgsHueSaturationFilter * clone() const override
Clone itself, create deep copy.
void readXml(const QDomElement &filterElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
void setColorizeStrength(int colorizeStrength)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
void setGrayscaleMode(QgsHueSaturationFilter::GrayscaleMode grayscaleMode)
void setSaturation(int saturation)