25 #include <QDomDocument>
26 #include <QDomElement>
36 , mGreenBand( greenBand )
37 , mBlueBand( blueBand )
38 , mRedContrastEnhancement( redEnhancement )
39 , mGreenContrastEnhancement( greenEnhancement )
40 , mBlueContrastEnhancement( blueEnhancement )
46 delete mRedContrastEnhancement;
47 delete mGreenContrastEnhancement;
48 delete mBlueContrastEnhancement;
56 if ( mRedContrastEnhancement )
60 if ( mGreenContrastEnhancement )
64 if ( mBlueContrastEnhancement )
74 delete mRedContrastEnhancement;
75 mRedContrastEnhancement = ce;
80 delete mGreenContrastEnhancement;
81 mGreenContrastEnhancement = ce;
86 delete mBlueContrastEnhancement;
87 mBlueContrastEnhancement = ce;
98 const int redBand = elem.attribute( QStringLiteral(
"redBand" ), QStringLiteral(
"-1" ) ).toInt();
99 const int greenBand = elem.attribute( QStringLiteral(
"greenBand" ), QStringLiteral(
"-1" ) ).toInt();
100 const int blueBand = elem.attribute( QStringLiteral(
"blueBand" ), QStringLiteral(
"-1" ) ).toInt();
104 const QDomElement redContrastElem = elem.firstChildElement( QStringLiteral(
"redContrastEnhancement" ) );
105 if ( !redContrastElem.isNull() )
113 const QDomElement greenContrastElem = elem.firstChildElement( QStringLiteral(
"greenContrastEnhancement" ) );
114 if ( !greenContrastElem.isNull() )
122 const QDomElement blueContrastElem = elem.firstChildElement( QStringLiteral(
"blueContrastEnhancement" ) );
123 if ( !blueContrastElem.isNull() )
139 std::unique_ptr< QgsRasterBlock > outputBlock(
new QgsRasterBlock() );
142 return outputBlock.release();
147 && mRedBand > 0 && mGreenBand > 0 && mBlueBand > 0
155 if ( mGreenBand > 0 )
167 return outputBlock.release();
175 QMap<int, QgsRasterBlock *> bandBlocks;
177 QList<int>::const_iterator bandIt = bands.constBegin();
178 for ( ; bandIt != bands.constEnd(); ++bandIt )
180 bandBlocks.insert( *bandIt, defaultPointer );
188 bandIt = bands.constBegin();
189 for ( ; bandIt != bands.constEnd(); ++bandIt )
192 if ( !bandBlocks[*bandIt] )
197 for ( ; bandIt != bands.constBegin(); --bandIt )
199 delete bandBlocks[*bandIt];
201 return outputBlock.release();
207 redBlock = bandBlocks[mRedBand];
209 if ( mGreenBand > 0 )
211 greenBlock = bandBlocks[mGreenBand];
215 blueBlock = bandBlocks[mBlueBand];
224 for (
int i = 0; i < bandBlocks.size(); i++ )
226 delete bandBlocks.
value( i );
228 return outputBlock.release();
231 QRgb *outputBlockColorData = outputBlock->colorData();
235 const quint8 *redData =
nullptr, *greenData =
nullptr, *blueData =
nullptr;
260 hasEnhancement = mRedContrastEnhancement || mGreenContrastEnhancement || mBlueContrastEnhancement;
262 if ( hasEnhancement )
267 for (
qgssize i = 0; i < count; i++ )
277 outputBlock->setColor( i, myDefaultColor );
281 outputBlockColorData[i] = qRgb( redData[i], greenData[i], blueData[i] );
286 bool redIsNoData =
false;
287 bool greenIsNoData =
false;
288 bool blueIsNoData =
false;
298 if ( !redIsNoData && !greenIsNoData )
305 outputBlock->setColor( i, myDefaultColor );
309 outputBlockColorData[i] = qRgb( redVal, greenVal, blueVal );
315 bool isNoData =
false;
323 if ( !isNoData && mGreenBand > 0 )
327 if ( !isNoData && mBlueBand > 0 )
333 outputBlock->setColor( i, myDefaultColor );
342 outputBlock->setColor( i, myDefaultColor );
347 if ( mRedContrastEnhancement )
351 if ( mGreenContrastEnhancement )
355 if ( mBlueContrastEnhancement )
368 currentOpacity *= alphaBlock->
value( i ) / 255.0;
373 outputBlock->setColor( i, qRgba( redVal, greenVal, blueVal, 255 ) );
377 outputBlock->setColor( i, qRgba( currentOpacity * redVal, currentOpacity * greenVal, currentOpacity * blueVal, currentOpacity * 255 ) );
382 QMap<int, QgsRasterBlock *>::const_iterator bandDelIt = bandBlocks.constBegin();
383 for ( ; bandDelIt != bandBlocks.constEnd(); ++bandDelIt )
385 delete bandDelIt.value();
388 return outputBlock.release();
393 if ( parentElem.isNull() )
398 QDomElement rasterRendererElem = doc.createElement( QStringLiteral(
"rasterrenderer" ) );
400 rasterRendererElem.setAttribute( QStringLiteral(
"redBand" ), mRedBand );
401 rasterRendererElem.setAttribute( QStringLiteral(
"greenBand" ), mGreenBand );
402 rasterRendererElem.setAttribute( QStringLiteral(
"blueBand" ), mBlueBand );
405 if ( mRedContrastEnhancement )
407 QDomElement redContrastElem = doc.createElement( QStringLiteral(
"redContrastEnhancement" ) );
408 mRedContrastEnhancement->
writeXml( doc, redContrastElem );
409 rasterRendererElem.appendChild( redContrastElem );
411 if ( mGreenContrastEnhancement )
413 QDomElement greenContrastElem = doc.createElement( QStringLiteral(
"greenContrastEnhancement" ) );
414 mGreenContrastEnhancement->
writeXml( doc, greenContrastElem );
415 rasterRendererElem.appendChild( greenContrastElem );
417 if ( mBlueContrastEnhancement )
419 QDomElement blueContrastElem = doc.createElement( QStringLiteral(
"blueContrastEnhancement" ) );
420 mBlueContrastEnhancement->
writeXml( doc, blueContrastElem );
421 rasterRendererElem.appendChild( blueContrastElem );
423 parentElem.appendChild( rasterRendererElem );
429 if ( mRedBand != -1 )
431 bandList << mRedBand;
433 if ( mGreenBand != -1 )
435 bandList << mGreenBand;
437 if ( mBlueBand != -1 )
439 bandList << mBlueBand;
446 QList<QgsLayerTreeModelLegendNode *> res;
447 if ( mRedBand != -1 )
451 if ( mGreenBand != -1 )
455 if ( mBlueBand != -1 )
478 bool isDefaultCombination =
true;
479 QList<int> defaultBandCombination( { 1, 2, 3 } );
481 isDefaultCombination = isDefaultCombination && (
usesBands() == defaultBandCombination );
482 isDefaultCombination = isDefaultCombination && (
488 if ( isDefaultCombination )
491 isDefaultCombination = isDefaultCombination && (
496 if ( isDefaultCombination )
499 isDefaultCombination = isDefaultCombination && (
504 if ( isDefaultCombination )
507 isDefaultCombination = isDefaultCombination && (
512 if ( isDefaultCombination )
517 QDomNodeList elements = element.elementsByTagName( QStringLiteral(
"sld:RasterSymbolizer" ) );
518 if ( elements.size() == 0 )
522 QDomElement rasterSymbolizerElem = elements.at( 0 ).toElement();
527 QDomElement channelSelectionElem = doc.createElement( QStringLiteral(
"sld:ChannelSelection" ) );
528 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Opacity" ) );
529 if ( elements.size() != 0 )
531 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
535 elements = rasterSymbolizerElem.elementsByTagName( QStringLiteral(
"sld:Geometry" ) );
536 if ( elements.size() != 0 )
538 rasterSymbolizerElem.insertAfter( channelSelectionElem, elements.at( 0 ) );
542 rasterSymbolizerElem.insertBefore( channelSelectionElem, rasterSymbolizerElem.firstChild() );
547 static QStringList tags { QStringLiteral(
"sld:RedChannel" ), QStringLiteral(
"sld:GreenChannel" ), QStringLiteral(
"sld:BlueChannel" ) };
549 QList<QgsContrastEnhancement *> contrastEnhancements;
550 contrastEnhancements.append( mRedContrastEnhancement );
551 contrastEnhancements.append( mGreenContrastEnhancement );
552 contrastEnhancements.append( mBlueContrastEnhancement );
555 QList<int>::const_iterator bandIt = bands.constBegin();
556 for (
int tagCounter = 0 ; bandIt != bands.constEnd(); ++bandIt, ++tagCounter )
561 QDomElement channelElem = doc.createElement( tags[ tagCounter ] );
562 channelSelectionElem.appendChild( channelElem );
565 QDomElement sourceChannelNameElem = doc.createElement( QStringLiteral(
"sld:SourceChannelName" ) );
566 sourceChannelNameElem.appendChild( doc.createTextNode( QString::number( *bandIt ) ) );
567 channelElem.appendChild( sourceChannelNameElem );
572 if ( contrastEnhancements[ tagCounter ] )
574 QDomElement contrastEnhancementElem = doc.createElement( QStringLiteral(
"sld:ContrastEnhancement" ) );
575 contrastEnhancements[ tagCounter ]->toSld( doc, contrastEnhancementElem );
576 channelElem.appendChild( contrastEnhancementElem );