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 = std::clamp( 
saturation, -100, 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();
 
DataType
Raster data types.
@ 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.
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 setColorizeColor(const QColor &colorizeColor)
void setColorizeStrength(int colorizeStrength)
int bandCount() const override
Gets number of bands.
QColor colorizeColor() const
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.
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 QgsRasterInterface * input() const
Current input.
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
A rectangle specified with double values.
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...
#define QgsDebugMsgLevel(str, level)