Skip to content
Permalink
Browse files

invert raster colors better

  • Loading branch information
blazek committed Oct 27, 2012
1 parent ff1431c commit a8934a28f768937ba4d074e6442bd27c75270ab1
@@ -52,9 +52,6 @@ class QgsRasterRenderer : QgsRasterInterface
void setAlphaBand( int band );
int alphaBand() const;

void setInvertColor( bool invert );
bool invertColor() const;

/**Get symbology items if provided by renderer*/
virtual void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const;

@@ -195,6 +195,7 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
tableTransparency->horizontalHeader()->setResizeMode( 1, QHeaderView::Stretch );

//resampling
mResamplingGroupBox->setSaveCheckedState( true );
const QgsRasterRenderer* renderer = mRasterLayer->renderer();
mZoomedInResamplingComboBox->insertItem( 0, tr( "Nearest neighbour" ) );
mZoomedInResamplingComboBox->insertItem( 1, tr( "Bilinear" ) );
@@ -206,12 +207,6 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
//set combo boxes to current resampling types
if ( resampleFilter )
{
//invert color map
if ( renderer->invertColor() )
{
mInvertColorMapCheckBox->setCheckState( Qt::Checked );
}

const QgsRasterResampler* zoomedInResampler = resampleFilter->zoomedInResampler();
if ( zoomedInResampler )
{
@@ -744,9 +739,6 @@ void QgsRasterLayerProperties::apply()

//set global transparency
rasterRenderer->setOpacity(( 255 - sliderTransparency->value() ) / 255.0 );

//invert color map
rasterRenderer->setInvertColor( mInvertColorMapCheckBox->isChecked() );
}

QgsDebugMsg( "processing general tab" );
@@ -57,7 +57,6 @@ QgsRasterInterface * QgsMultiBandColorRenderer::clone() const
}
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setInvertColor( mInvertColor );
renderer->setRasterTransparency( mRasterTransparency );

return renderer;
@@ -136,8 +135,7 @@ QgsRasterBlock* QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
//In some (common) cases, we can simplify the drawing loop considerably and save render time
bool fastDraw = ( !usesTransparency()
&& mRedBand > 0 && mGreenBand > 0 && mBlueBand > 0
&& mAlphaBand < 1 && !mRedContrastEnhancement && !mGreenContrastEnhancement && !mBlueContrastEnhancement
&& !mInvertColor );
&& mAlphaBand < 1 && !mRedContrastEnhancement && !mGreenContrastEnhancement && !mBlueContrastEnhancement );

QSet<int> bands;
if ( mRedBand > 0 )
@@ -290,13 +288,6 @@ QgsRasterBlock* QgsMultiBandColorRenderer::block( int bandNo, QgsRectangle cons
blueVal = mBlueContrastEnhancement->enhanceContrast( blueVal );
}

if ( mInvertColor )
{
redVal = 255 - redVal;
greenVal = 255 - greenVal;
blueVal = 255 - blueVal;
}

//opacity
double currentOpacity = mOpacity;
if ( mRasterTransparency )
@@ -39,7 +39,6 @@ QgsRasterInterface * QgsPalettedRasterRenderer::clone() const
QgsPalettedRasterRenderer * renderer = new QgsPalettedRasterRenderer( 0, mBand, colors(), mNColors );
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setInvertColor( mInvertColor );
renderer->setRasterTransparency( mRasterTransparency );
return renderer;
}
@@ -179,14 +178,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
}
QColor& currentColor = mColors[val];

if ( mInvertColor )
{
outputBlock->setColor( i, qRgba( currentOpacity * currentColor.blue(), currentOpacity * currentColor.green(), currentOpacity * currentColor.red(), currentOpacity * 255 ) );
}
else
{
outputBlock->setColor( i, qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 ) );
}
outputBlock->setColor( i, qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 ) );
}
}

@@ -40,7 +40,7 @@ const QRgb QgsRasterRenderer::NODATA_COLOR = qRgba( 0, 0, 0, 0 );
QgsRasterRenderer::QgsRasterRenderer( QgsRasterInterface* input, const QString& type )
: QgsRasterInterface( input )
, mType( type ), mOpacity( 1.0 ), mRasterTransparency( 0 )
, mAlphaBand( -1 ), mInvertColor( false )
, mAlphaBand( -1 ) //, mInvertColor( false )
{
}

@@ -117,7 +117,7 @@ void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendere
rasterRendererElem.setAttribute( "type", mType );
rasterRendererElem.setAttribute( "opacity", QString::number( mOpacity ) );
rasterRendererElem.setAttribute( "alphaBand", mAlphaBand );
rasterRendererElem.setAttribute( "invertColor", mInvertColor );
//rasterRendererElem.setAttribute( "invertColor", mInvertColor );

if ( mRasterTransparency )
{
@@ -135,7 +135,7 @@ void QgsRasterRenderer::readXML( const QDomElement& rendererElem )
mType = rendererElem.attribute( "type" );
mOpacity = rendererElem.attribute( "opacity", "1.0" ).toDouble();
mAlphaBand = rendererElem.attribute( "alphaBand", "-1" ).toInt();
mInvertColor = rendererElem.attribute( "invertColor", "0" ).toInt();
//mInvertColor = rendererElem.attribute( "invertColor", "0" ).toInt();

//todo: read mRasterTransparency
QDomElement rasterTransparencyElem = rendererElem.firstChildElement( "rasterTransparency" );
@@ -92,8 +92,8 @@ class CORE_EXPORT QgsRasterRenderer : public QgsRasterInterface
void setAlphaBand( int band ) { mAlphaBand = band; }
int alphaBand() const { return mAlphaBand; }

void setInvertColor( bool invert ) { mInvertColor = invert; }
bool invertColor() const { return mInvertColor; }
//void setInvertColor( bool invert ) { mInvertColor = invert; }
//bool invertColor() const { return mInvertColor; }

/**Get symbology items if provided by renderer*/
virtual void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const { Q_UNUSED( symbolItems ); }
@@ -124,7 +124,7 @@ class CORE_EXPORT QgsRasterRenderer : public QgsRasterInterface
Default: -1 (not set)*/
int mAlphaBand;

bool mInvertColor;
//bool mInvertColor;
};

#endif // QGSRASTERRENDERER_H
@@ -36,7 +36,6 @@ QgsRasterInterface * QgsSingleBandColorDataRenderer::clone() const
QgsSingleBandColorDataRenderer * renderer = new QgsSingleBandColorDataRenderer( 0, mBand );
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setInvertColor( mInvertColor );
renderer->setRasterTransparency( mRasterTransparency );
return renderer;
}
@@ -23,7 +23,7 @@
#include <QImage>

QgsSingleBandGrayRenderer::QgsSingleBandGrayRenderer( QgsRasterInterface* input, int grayBand ):
QgsRasterRenderer( input, "singlebandgray" ), mGrayBand( grayBand ), mContrastEnhancement( 0 )
QgsRasterRenderer( input, "singlebandgray" ), mGrayBand( grayBand ), mGradient( BlackToWhite ), mContrastEnhancement( 0 )
{
}

@@ -37,7 +37,6 @@ QgsRasterInterface * QgsSingleBandGrayRenderer::clone() const
QgsSingleBandGrayRenderer * renderer = new QgsSingleBandGrayRenderer( 0, mGrayBand );
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setInvertColor( mInvertColor );
renderer->setRasterTransparency( mRasterTransparency );
if ( mContrastEnhancement )
{
@@ -57,6 +56,11 @@ QgsRasterRenderer* QgsSingleBandGrayRenderer::create( const QDomElement& elem, Q
QgsSingleBandGrayRenderer* r = new QgsSingleBandGrayRenderer( input, grayBand );
r->readXML( elem );

if ( elem.attribute( "gradient" ) == "WhiteToBlack" )
{
r->setGradient( WhiteToBlack ); // BlackToWhite is default
}

QDomElement contrastEnhancementElem = elem.firstChildElement( "contrastEnhancement" );
if ( !contrastEnhancementElem.isNull() )
{
@@ -149,7 +153,7 @@ QgsRasterBlock* QgsSingleBandGrayRenderer::block( int bandNo, QgsRectangle cons
grayVal = mContrastEnhancement->enhanceContrast( grayVal );
}

if ( mInvertColor )
if ( mGradient == WhiteToBlack )
{
grayVal = 255 - grayVal;
}
@@ -184,6 +188,18 @@ void QgsSingleBandGrayRenderer::writeXML( QDomDocument& doc, QDomElement& parent
_writeXML( doc, rasterRendererElem );

rasterRendererElem.setAttribute( "grayBand", mGrayBand );

QString gradient;
if ( mGradient == BlackToWhite )
{
gradient = "BlackToWhite";
}
else
{
gradient = "WhiteToBlack";
}
rasterRendererElem.setAttribute( "gradient", gradient );

if ( mContrastEnhancement )
{
QDomElement contrastElem = doc.createElement( "contrastEnhancement" );
@@ -29,6 +29,12 @@ class QDomElement;
class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
{
public:
enum Gradient
{
BlackToWhite,
WhiteToBlack
};

QgsSingleBandGrayRenderer( QgsRasterInterface* input, int grayBand );
~QgsSingleBandGrayRenderer();
QgsRasterInterface * clone() const;
@@ -43,6 +49,9 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
/**Takes ownership*/
void setContrastEnhancement( QgsContrastEnhancement* ce );

void setGradient( Gradient theGradient ) { mGradient = theGradient; }
Gradient gradient() const { return mGradient; }

void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;

void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const;
@@ -51,6 +60,7 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer

private:
int mGrayBand;
Gradient mGradient;
QgsContrastEnhancement* mContrastEnhancement;
};

@@ -63,7 +63,6 @@ QgsRasterInterface * QgsSingleBandPseudoColorRenderer::clone() const

renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setInvertColor( mInvertColor );
renderer->setRasterTransparency( mRasterTransparency );

return renderer;
@@ -165,14 +164,6 @@ QgsRasterBlock* QgsSingleBandPseudoColorRenderer::block( int bandNo, QgsRectangl
continue;
}

if ( mInvertColor )
{
// 1.8 was flipping blue and red
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
}

if ( !hasTransparency )
{
outputBlock->setColor( i, qRgba( red, green, blue, 255 ) );
@@ -23,6 +23,9 @@ QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget( QgsRasterLayer
{
setupUi( this );

mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );

mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );

@@ -36,7 +39,12 @@ QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget( QgsRasterLayer

mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
mMinMaxWidget->setExtent( extent );
layout()->addWidget( mMinMaxWidget );

QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
mMinMaxContainerWidget->setLayout( layout );
layout->addWidget( mMinMaxWidget );

connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
this, SLOT( loadMinMax( int, double, double, int ) ) );

@@ -84,6 +92,9 @@ QgsRasterRenderer* QgsSingleBandGrayRendererWidget::renderer()

QgsSingleBandGrayRenderer* renderer = new QgsSingleBandGrayRenderer( provider, band );
renderer->setContrastEnhancement( e );

renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->itemData( mGradientComboBox->currentIndex() ).toInt() );

return renderer;
}

@@ -127,6 +138,8 @@ void QgsSingleBandGrayRendererWidget::setFromRenderer( const QgsRasterRenderer*
//band
mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
const QgsContrastEnhancement* ce = gr->contrastEnhancement();

mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
//minmax
mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
@@ -278,15 +278,17 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
for ( int i = 0; i < numberOfEntries; ++i )
{
QColor currentColor;
currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
entryColors.push_back( currentColor );
}
}
else
{
for ( int i = 0; i < numberOfEntries; ++i )
{
entryColors.push_back( colorRamp->color((( double ) i ) / numberOfEntries ) );
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
entryColors.push_back( colorRamp->color((( double ) idx ) / numberOfEntries ) );
}
}

0 comments on commit a8934a2

Please sign in to comment.
You can’t perform that action at this time.