23 #include <QDomDocument> 24 #include <QDomElement> 34 , mGreenBand( greenBand )
35 , mBlueBand( blueBand )
36 , mRedContrastEnhancement( redEnhancement )
37 , mGreenContrastEnhancement( greenEnhancement )
38 , mBlueContrastEnhancement( blueEnhancement )
44 delete mRedContrastEnhancement;
45 delete mGreenContrastEnhancement;
46 delete mBlueContrastEnhancement;
54 if ( mRedContrastEnhancement )
58 if ( mGreenContrastEnhancement )
62 if ( mBlueContrastEnhancement )
72 delete mRedContrastEnhancement;
73 mRedContrastEnhancement = ce;
78 delete mGreenContrastEnhancement;
79 mGreenContrastEnhancement = ce;
84 delete mBlueContrastEnhancement;
85 mBlueContrastEnhancement = ce;
96 int redBand = elem.attribute( QStringLiteral(
"redBand" ), QStringLiteral(
"-1" ) ).toInt();
97 int greenBand = elem.attribute( QStringLiteral(
"greenBand" ), QStringLiteral(
"-1" ) ).toInt();
98 int blueBand = elem.attribute( QStringLiteral(
"blueBand" ), QStringLiteral(
"-1" ) ).toInt();
102 QDomElement redContrastElem = elem.firstChildElement( QStringLiteral(
"redContrastEnhancement" ) );
103 if ( !redContrastElem.isNull() )
107 redContrastEnhancement->
readXml( redContrastElem );
111 QDomElement greenContrastElem = elem.firstChildElement( QStringLiteral(
"greenContrastEnhancement" ) );
112 if ( !greenContrastElem.isNull() )
116 greenContrastEnhancement->
readXml( greenContrastElem );
120 QDomElement blueContrastElem = elem.firstChildElement( QStringLiteral(
"blueContrastEnhancement" ) );
121 if ( !blueContrastElem.isNull() )
125 blueContrastEnhancement->
readXml( blueContrastElem );
129 greenContrastEnhancement, blueContrastEnhancement );
137 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
140 return outputBlock.release();
145 && mRedBand > 0 && mGreenBand > 0 && mBlueBand > 0
153 if ( mGreenBand > 0 )
165 return outputBlock.release();
173 QMap<int, QgsRasterBlock *> bandBlocks;
175 QSet<int>::const_iterator bandIt = bands.constBegin();
176 for ( ; bandIt != bands.constEnd(); ++bandIt )
178 bandBlocks.insert( *bandIt, defaultPointer );
186 bandIt = bands.constBegin();
187 for ( ; bandIt != bands.constEnd(); ++bandIt )
189 bandBlocks[*bandIt] =
mInput->
block( *bandIt, extent, width, height, feedback );
190 if ( !bandBlocks[*bandIt] )
195 for ( ; bandIt != bands.constBegin(); --bandIt )
197 delete bandBlocks[*bandIt];
199 return outputBlock.release();
205 redBlock = bandBlocks[mRedBand];
207 if ( mGreenBand > 0 )
209 greenBlock = bandBlocks[mGreenBand];
213 blueBlock = bandBlocks[mBlueBand];
222 for (
int i = 0; i < bandBlocks.size(); i++ )
224 delete bandBlocks.
value( i );
226 return outputBlock.release();
229 QRgb *outputBlockColorData = outputBlock->colorData();
233 const quint8 *redData =
nullptr, *greenData =
nullptr, *blueData =
nullptr;
258 hasEnhancement = mRedContrastEnhancement || mGreenContrastEnhancement || mBlueContrastEnhancement;
260 if ( hasEnhancement )
265 for (
qgssize i = 0; i < count; i++ )
273 outputBlock->setColor( i, myDefaultColor );
279 outputBlockColorData[i] = qRgb( redData[i], greenData[i], blueData[i] );
283 int redVal =
static_cast<int>( redBlock->
value( i ) );
284 int greenVal =
static_cast<int>( greenBlock->
value( i ) );
285 int blueVal =
static_cast<int>( blueBlock->
value( i ) );
286 outputBlockColorData[i] = qRgb( redVal, greenVal, blueVal );
292 bool isNoData =
false;
300 if ( !isNoData && mGreenBand > 0 )
304 if ( !isNoData && mBlueBand > 0 )
310 outputBlock->setColor( i, myDefaultColor );
319 outputBlock->setColor( i, myDefaultColor );
324 if ( mRedContrastEnhancement )
328 if ( mGreenContrastEnhancement )
332 if ( mBlueContrastEnhancement )
345 currentOpacity *= alphaBlock->
value( i ) / 255.0;
350 outputBlock->setColor( i, qRgba( redVal, greenVal, blueVal, 255 ) );
354 outputBlock->setColor( i, qRgba( currentOpacity * redVal, currentOpacity * greenVal, currentOpacity * blueVal, currentOpacity * 255 ) );
359 QMap<int, QgsRasterBlock *>::const_iterator bandDelIt = bandBlocks.constBegin();
360 for ( ; bandDelIt != bandBlocks.constEnd(); ++bandDelIt )
362 delete bandDelIt.value();
365 return outputBlock.release();
370 if ( parentElem.isNull() )
375 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
377 rasterRendererElem.setAttribute( QStringLiteral(
"redBand" ), mRedBand );
378 rasterRendererElem.setAttribute( QStringLiteral(
"greenBand" ), mGreenBand );
379 rasterRendererElem.setAttribute( QStringLiteral(
"blueBand" ), mBlueBand );
382 if ( mRedContrastEnhancement )
384 QDomElement redContrastElem = doc.createElement( QStringLiteral(
"redContrastEnhancement" ) );
385 mRedContrastEnhancement->
writeXml( doc, redContrastElem );
386 rasterRendererElem.appendChild( redContrastElem );
388 if ( mGreenContrastEnhancement )
390 QDomElement greenContrastElem = doc.createElement( QStringLiteral(
"greenContrastEnhancement" ) );
391 mGreenContrastEnhancement->
writeXml( doc, greenContrastElem );
392 rasterRendererElem.appendChild( greenContrastElem );
394 if ( mBlueContrastEnhancement )
396 QDomElement blueContrastElem = doc.createElement( QStringLiteral(
"blueContrastEnhancement" ) );
397 mBlueContrastEnhancement->
writeXml( doc, blueContrastElem );
398 rasterRendererElem.appendChild( blueContrastElem );
400 parentElem.appendChild( rasterRendererElem );
406 if ( mRedBand != -1 )
408 bandList << mRedBand;
410 if ( mGreenBand != -1 )
412 bandList << mGreenBand;
414 if ( mBlueBand != -1 )
416 bandList << mBlueBand;
436 bool isDefaultCombination =
true;
437 QList<int> defaultBandCombination( { 1, 2, 3 } );
439 isDefaultCombination = isDefaultCombination && (
usesBands() == defaultBandCombination );
440 isDefaultCombination = isDefaultCombination && (
446 if ( isDefaultCombination )
449 isDefaultCombination = isDefaultCombination && (
454 if ( isDefaultCombination )
457 isDefaultCombination = isDefaultCombination && (
462 if ( isDefaultCombination )
465 isDefaultCombination = isDefaultCombination && (
470 if ( isDefaultCombination )
475 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
476 if ( elements.size() == 0 )
480 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
485 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
486 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Opacity" ) );
487 if ( elements.size() != 0 )
489 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
493 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Geometry" ) );
494 if ( elements.size() != 0 )
496 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
500 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
505 static QStringList tags { QStringLiteral(
"sld:RedChannel" ), QStringLiteral(
"sld:GreenChannel" ), QStringLiteral(
"sld:BlueChannel" ) };
507 QList<QgsContrastEnhancement *> contrastEnhancements;
508 contrastEnhancements.append( mRedContrastEnhancement );
509 contrastEnhancements.append( mGreenContrastEnhancement );
510 contrastEnhancements.append( mBlueContrastEnhancement );
513 QList<int>::const_iterator bandIt = bands.constBegin();
514 for (
int tagCounter = 0 ; bandIt != bands.constEnd(); ++bandIt, ++tagCounter )
519 QDomElement channelElem = doc.createElement( tags[ tagCounter ] );
520 channelSelectionElem.appendChild( channelElem );
523 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
524 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( *bandIt ) ) );
525 channelElem.appendChild( sourceChannelNameElem );
530 if ( contrastEnhancements[ tagCounter ] )
532 QDomElement contrastEnhancementElem = doc.createElement( QStringLiteral(
"sld:ContrastEnhancement" ) );
533 contrastEnhancements[ tagCounter ]->toSld( doc, contrastEnhancementElem );
534 channelElem.appendChild( contrastEnhancementElem );
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
A rectangle specified with double values.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
~QgsMultiBandColorRenderer() override
const QgsContrastEnhancement * blueContrastEnhancement() const
void writeXml(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
Qgis::DataType dataType() const
Returns data type.
virtual QgsRectangle extent() const
Gets the extent of the interface.
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.
double valueAndNoData(int row, int column, bool &isNoData) const
Reads a single value from the pixel at row and column, if type of block is numeric.
DataType
Raster data types.
double maximumValue
The maximum cell value in the raster band.
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QgsMultiBandColorRenderer * clone() const override
Clone itself, create deep copy.
QMap< QString, QString > QgsStringMap
void setGreenContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
static const QRgb NODATA_COLOR
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
The RasterBandStats struct is a container for statistics about a single raster band.
void setRedContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
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.
const quint8 * byteData() const
Gives direct access to the raster block data.
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.
bool isValueInDisplayableRange(double value)
Returns true if a pixel value is in displayable range, false if pixel is outside of range (i...
bool usesTransparency() const
void readXml(const QDomElement &elem)
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.
bool isNoData(int row, int column) const
Checks if value at position is no data.
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 toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const override
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
const QgsContrastEnhancement * redContrastEnhancement() const
const QgsContrastEnhancement * greenContrastEnhancement() const
void setBlueContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
double value(int row, int column) const
Read a single value if type of block is numeric.
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
double minimumValue
The minimum cell value in the raster band.
Renderer for multiband images with the color components.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
double mOpacity
Global alpha value (0-1)
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
QgsMultiBandColorRenderer(QgsRasterInterface *input, int redBand, int greenBand, int blueBand, QgsContrastEnhancement *redEnhancement=nullptr, QgsContrastEnhancement *greenEnhancement=nullptr, QgsContrastEnhancement *blueEnhancement=nullptr)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
Raster renderer pipe that applies colors to a raster.
Eight bit unsigned integer (quint8)
int enhanceContrast(double value)
Applies the contrast enhancement to a value.