21 #include <QDomDocument> 22 #include <QDomElement> 29 , mGrayBand( grayBand )
30 , mGradient( BlackToWhite )
31 , mContrastEnhancement( nullptr )
41 if ( mContrastEnhancement )
55 int grayBand = elem.attribute( QStringLiteral(
"grayBand" ), QStringLiteral(
"-1" ) ).toInt();
59 if ( elem.attribute( QStringLiteral(
"gradient" ) ) == QLatin1String(
"WhiteToBlack" ) )
64 QDomElement contrastEnhancementElem = elem.firstChildElement( QStringLiteral(
"contrastEnhancement" ) );
65 if ( !contrastEnhancementElem.isNull() )
69 ce->
readXml( contrastEnhancementElem );
77 mContrastEnhancement.reset( ce );
83 QgsDebugMsgLevel( QString(
"width = %1 height = %2" ).arg( width ).arg( height ), 4 );
85 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
88 return outputBlock.release();
91 std::shared_ptr< QgsRasterBlock > inputBlock(
mInput->
block( mGrayBand, extent, width, height, feedback ) );
92 if ( !inputBlock || inputBlock->isEmpty() )
95 return outputBlock.release();
98 std::shared_ptr< QgsRasterBlock > alphaBlock;
103 if ( !alphaBlock || alphaBlock->isEmpty() )
106 return outputBlock.release();
111 alphaBlock = inputBlock;
116 return outputBlock.release();
122 if ( inputBlock->isNoData( i ) )
124 outputBlock->setColor( i, myDefaultColor );
127 double grayVal = inputBlock->value( i );
136 currentAlpha *= alphaBlock->value( i ) / 255.0;
139 if ( mContrastEnhancement )
141 if ( !mContrastEnhancement->isValueInDisplayableRange( grayVal ) )
143 outputBlock->setColor( i, myDefaultColor );
146 grayVal = mContrastEnhancement->enhanceContrast( grayVal );
151 grayVal = 255 - grayVal;
156 outputBlock->setColor( i, qRgba( grayVal, grayVal, grayVal, 255 ) );
160 outputBlock->setColor( i, qRgba( currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * grayVal, currentAlpha * 255 ) );
164 return outputBlock.release();
169 if ( parentElem.isNull() )
174 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
177 rasterRendererElem.setAttribute( QStringLiteral(
"grayBand" ), mGrayBand );
182 gradient = QStringLiteral(
"BlackToWhite" );
186 gradient = QStringLiteral(
"WhiteToBlack" );
188 rasterRendererElem.setAttribute( QStringLiteral(
"gradient" ), gradient );
190 if ( mContrastEnhancement )
192 QDomElement contrastElem = doc.createElement( QStringLiteral(
"contrastEnhancement" ) );
193 mContrastEnhancement->writeXml( doc, contrastElem );
194 rasterRendererElem.appendChild( contrastElem );
196 parentElem.appendChild( rasterRendererElem );
203 QColor minColor = ( mGradient ==
BlackToWhite ) ? Qt::black : Qt::white;
204 QColor maxColor = ( mGradient ==
BlackToWhite ) ? Qt::white : Qt::black;
205 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->minimumValue() ), minColor ) );
206 symbolItems.push_back( qMakePair( QString::number( mContrastEnhancement->maximumValue() ), maxColor ) );
213 if ( mGrayBand != -1 )
215 bandList << mGrayBand;
A rectangle specified with double values.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
QgsSingleBandGrayRenderer(QgsRasterInterface *input, int grayBand)
virtual QgsRectangle extent() const
Gets the extent of the interface.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
virtual QgsRasterInterface * input() const
Current input.
DataType
Raster data types.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QgsSingleBandGrayRenderer * clone() const override
Clone itself, create deep copy.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Gets symbology items if provided by renderer.
static const QRgb NODATA_COLOR
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.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
void readXml(const QDomElement &elem)
Raster renderer pipe for single band gray.
void setGradient(Gradient gradient)
Gradient gradient() const
int mAlphaBand
Read alpha value from band.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Base class for processing filters like renderers, reprojector, resampler etc.
int alphaValue(double value, int globalTransparency=255) const
Returns the transparency value for a single value pixel.
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...
void setContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
double mOpacity
Global alpha value (0-1)
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
Raster renderer pipe that applies colors to a raster.