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;
438 bool isDefaultCombination =
true;
439 QList<int> defaultBandCombination( { 1, 2, 3 } );
441 isDefaultCombination = isDefaultCombination && (
usesBands() == defaultBandCombination );
442 isDefaultCombination = isDefaultCombination && (
448 if ( isDefaultCombination )
451 isDefaultCombination = isDefaultCombination && (
456 if ( isDefaultCombination )
459 isDefaultCombination = isDefaultCombination && (
464 if ( isDefaultCombination )
467 isDefaultCombination = isDefaultCombination && (
472 if ( isDefaultCombination )
477 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
478 if ( elements.size() == 0 )
482 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
487 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
488 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Opacity" ) );
489 if ( elements.size() != 0 )
491 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
495 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Geometry" ) );
496 if ( elements.size() != 0 )
498 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
502 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
507 static QStringList tags { QStringLiteral(
"sld:RedChannel" ), QStringLiteral(
"sld:GreenChannel" ), QStringLiteral(
"sld:BlueChannel" ) };
509 QList<QgsContrastEnhancement *> contrastEnhancements;
510 contrastEnhancements.append( mRedContrastEnhancement );
511 contrastEnhancements.append( mGreenContrastEnhancement );
512 contrastEnhancements.append( mBlueContrastEnhancement );
515 QList<int>::const_iterator bandIt = bands.constBegin();
516 for (
int tagCounter = 0 ; bandIt != bands.constEnd(); ++bandIt, ++tagCounter )
521 QDomElement channelElem = doc.createElement( tags[ tagCounter ] );
522 channelSelectionElem.appendChild( channelElem );
525 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
526 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( *bandIt ) ) );
527 channelElem.appendChild( sourceChannelNameElem );
532 if ( contrastEnhancements[ tagCounter ] )
534 QDomElement contrastEnhancementElem = doc.createElement( QStringLiteral(
"sld:ContrastEnhancement" ) );
535 contrastEnhancements[ tagCounter ]->toSld( doc, contrastEnhancementElem );
536 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.