Skip to content

Commit

Permalink
Fix contrast enhancement buttons in raster branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Apr 13, 2012
1 parent 9135c01 commit 47bba04
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 45 deletions.
140 changes: 95 additions & 45 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
#include "qgsmessageviewer.h"
#include "qgsmimedatautils.h"
#include "qgsmessagelog.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgsnewvectorlayerdialog.h"
#include "qgsoptions.h"
#include "qgspastetransformations.h"
Expand All @@ -157,10 +158,12 @@
#include "qgsrastercalcdialog.h"
#include "qgsrasterlayer.h"
#include "qgsrasterlayerproperties.h"
#include "qgsrasterrenderer.h"
#include "qgsrectangle.h"
#include "qgsrenderer.h"
#include "qgsscalecombobox.h"
#include "qgsshortcutsmanager.h"
#include "qgssinglebandgrayrenderer.h"
#include "qgssnappingdialog.h"
#include "qgssponsors.h"
#include "qgstextannotationitem.h"
Expand Down Expand Up @@ -5056,6 +5059,11 @@ void QgisApp::options()
}

void QgisApp::fullHistogramStretch()
{
histogramStretch( false );
}

void QgisApp::histogramStretch( bool visibleAreaOnly )
{
QgsMapLayer * layer = mMapLegend->currentLayer();

Expand All @@ -5075,76 +5083,118 @@ void QgisApp::fullHistogramStretch()
tr( "To perform a full histogram stretch, you need to have a raster layer selected." ) );
return;
}
if ( rlayer->providerType() == "wms" )

QgsRasterDataProvider* provider = rlayer->dataProvider();
if ( !provider )
{
return;
}
if ( rlayer->drawingStyle() == QgsRasterLayer::SingleBandGray ||
rlayer->drawingStyle() == QgsRasterLayer::MultiBandSingleBandGray ||
rlayer->drawingStyle() == QgsRasterLayer::MultiBandColor
)

//get renderer
QgsRasterRenderer* renderer = rlayer->renderer();
if ( !renderer )
{
rlayer->setContrastEnhancementAlgorithm( "StretchToMinimumMaximum" );
rlayer->setMinimumMaximumUsingDataset();
rlayer->setCacheImage( NULL );
//refreshLayerSymbology( rlayer->getLayerID() );
mMapCanvas->refresh();
return;
}

//singleband gray <-> multiband color
if ( renderer->type() == "singlebandgray" )
{
QgsSingleBandGrayRenderer* grayRenderer = static_cast<QgsSingleBandGrayRenderer*>( renderer );
if ( !grayRenderer )
{
return;
}

//create new contrast enhancements
int grayBand = grayRenderer->grayBand();
if ( grayBand == -1 )
{
return;
}

QgsContrastEnhancement* e = rasterContrastEnhancement( rlayer, grayBand, visibleAreaOnly );
if ( !e )
{
return;
}
grayRenderer->setContrastEnhancement( e );
}
else if ( renderer->type() == "multibandcolor" )
{
QgsMultiBandColorRenderer* colorRenderer = static_cast<QgsMultiBandColorRenderer*>( renderer );
if ( !colorRenderer )
{
return;
}

QgsContrastEnhancement* redEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->redBand(), visibleAreaOnly );
if ( redEnhancement )
{
colorRenderer->setRedContrastEnhancement( redEnhancement );
}
QgsContrastEnhancement* greenEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->greenBand(), visibleAreaOnly );
if ( greenEnhancement )
{
colorRenderer->setGreenContrastEnhancement( greenEnhancement );
}
QgsContrastEnhancement* blueEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->blueBand(), visibleAreaOnly );
if ( blueEnhancement )
{
colorRenderer->setBlueContrastEnhancement( blueEnhancement );
}
}
else
{
QMessageBox::information( this,
tr( "No Valid Raster Layer Selected" ),
tr( "To perform a local histogram stretch, you need to have a grayscale "
"or multiband (multiband single layer, singleband grayscale or multiband color) "
" raster layer selected." ) );
return;
}

mMapCanvas->refresh();
}

void QgisApp::localHistogramStretch()
QgsContrastEnhancement* QgisApp::rasterContrastEnhancement( QgsRasterLayer* rlayer, int band,
bool visibleAreaOnly ) const
{
QgsMapLayer * layer = mMapLegend->currentLayer();

if ( !layer )
if ( !rlayer || band == -1 )
{
QMessageBox::information( this,
tr( "No Layer Selected" ),
tr( "To perform a local histogram stretch, you need to have a raster layer selected." ) );
return;
return 0;
}

QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( !rlayer )
QgsRasterDataProvider* provider = rlayer->dataProvider();
if ( !provider )
{
QMessageBox::information( this,
tr( "No Raster Layer Selected" ),
tr( "To perform a local histogram stretch, you need to have a raster layer selected." ) );
return;
return 0;
}
if ( rlayer->drawingStyle() == QgsRasterLayer::SingleBandGray ||
rlayer->drawingStyle() == QgsRasterLayer::MultiBandSingleBandGray ||
rlayer->drawingStyle() == QgsRasterLayer::MultiBandColor
)

QgsContrastEnhancement* e = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
provider->dataType( band ) ) );
double minValue = 0;
double maxValue = 0;

if ( visibleAreaOnly )
{
rlayer->setContrastEnhancementAlgorithm( "StretchToMinimumMaximum" );
rlayer->setMinimumMaximumUsingLastExtent();
rlayer->setCacheImage( NULL );
//refreshLayerSymbology( rlayer->getLayerID() );
mMapCanvas->refresh();
return;
double minMax[2];
rlayer->computeMinimumMaximumFromLastExtent( band, minMax );
minValue = minMax[0];
maxValue = minMax[1];
}
else
{
QMessageBox::information( this,
tr( "No Valid Raster Layer Selected" ),
tr( "To perform a local histogram stretch, you need to have a grayscale "
"or multiband (multiband single layer, singleband grayscale or multiband color) "
" raster layer selected." ) );
return;
QgsRasterBandStats rasterBandStats = rlayer->bandStatistics( band );
minValue = rasterBandStats.minimumValue;
maxValue = rasterBandStats.maximumValue;
}

e->setMinimumValue( minValue );
e->setMaximumValue( maxValue );
e->setContrastEnhancementAlgorithm( QgsContrastEnhancement::StretchToMinimumMaximum );
return e;
}

void QgisApp::localHistogramStretch()
{
histogramStretch( true );
}

void QgisApp::helpContents()
{
Expand Down
7 changes: 7 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class QgsAnnotationItem;
class QgsClipboard;
class QgsComposer;
class QgsComposerView;
class QgsContrastEnhancement;
class QgsGeometry;
class QgsFeature;

Expand Down Expand Up @@ -964,6 +965,12 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void updateCRSStatusBar();
void createDecorations();

/**Do histogram stretch for singleband gray / multiband color rasters*/
void histogramStretch( bool visibleAreaOnly = false );
/**Create raster enhancement object for a raster band*/
QgsContrastEnhancement* rasterContrastEnhancement( QgsRasterLayer* rlayer, int band,
bool visibleAreaOnly = false ) const;

// actions for menus and toolbars -----------------

#ifdef Q_WS_MAC
Expand Down

0 comments on commit 47bba04

Please sign in to comment.