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() )
111 QDomElement greenContrastElem = elem.firstChildElement( QStringLiteral(
"greenContrastEnhancement" ) );
112 if ( !greenContrastElem.isNull() )
120 QDomElement blueContrastElem = elem.firstChildElement( QStringLiteral(
"blueContrastEnhancement" ) );
121 if ( !blueContrastElem.isNull() )
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 )
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 );