Skip to content
Permalink
Browse files

default raster contrast enhancement by type

  • Loading branch information
blazek committed Aug 14, 2012
1 parent 538ebe0 commit 8c86b61966ca60d4ef3fdc496db250a39506315c
@@ -75,7 +75,6 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :

connect( spinFontSize, SIGNAL( valueChanged( const QString& ) ), this, SLOT( fontSizeChanged( const QString& ) ) );

connect( chkUseStandardDeviation, SIGNAL( stateChanged( int ) ), this, SLOT( toggleStandardDeviation( int ) ) );
#ifdef Q_WS_X11
connect( chkEnableBackbuffer, SIGNAL( stateChanged( int ) ), this, SLOT( toggleEnableBackbuffer( int ) ) );
#endif
@@ -384,33 +383,20 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
spnGreen->setValue( settings.value( "/Raster/defaultGreenBand", 2 ).toInt() );
spnBlue->setValue( settings.value( "/Raster/defaultBlueBand", 3 ).toInt() );

//add items to the color enhanceContrast combo box
cboxContrastEnhancementAlgorithm->addItem( tr( "No Stretch" ) );
cboxContrastEnhancementAlgorithm->addItem( tr( "Stretch To MinMax" ) );
cboxContrastEnhancementAlgorithm->addItem( tr( "Stretch And Clip To MinMax" ) );
cboxContrastEnhancementAlgorithm->addItem( tr( "Clip To MinMax" ) );
initContrastEnhancement( cboxContrastEnhancementAlgorithmSingleBand, "singleBand", "StretchToMinimumMaximum" );
initContrastEnhancement( cboxContrastEnhancementAlgorithmMultiBandSingleByte, "multiBandSingleByte", "NoEnhancement" );
initContrastEnhancement( cboxContrastEnhancementAlgorithmMultiBandMultiByte, "multiBandMultiByte", "StretchToMinimumMaximum" );

QString contrastEnchacement = settings.value( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" ).toString();
if ( contrastEnchacement == "NoEnhancement" )
{
cboxContrastEnhancementAlgorithm->setCurrentIndex( cboxContrastEnhancementAlgorithm->findText( tr( "No Stretch" ) ) );
}
if ( contrastEnchacement == "StretchToMinimumMaximum" )
{
cboxContrastEnhancementAlgorithm->setCurrentIndex( cboxContrastEnhancementAlgorithm->findText( tr( "Stretch To MinMax" ) ) );
}
else if ( contrastEnchacement == "StretchAndClipToMinimumMaximum" )
{
cboxContrastEnhancementAlgorithm->setCurrentIndex( cboxContrastEnhancementAlgorithm->findText( tr( "Stretch And Clip To MinMax" ) ) );
}
else if ( contrastEnchacement == "ClipToMinimumMaximum" )
{
cboxContrastEnhancementAlgorithm->setCurrentIndex( cboxContrastEnhancementAlgorithm->findText( tr( "Clip To MinMax" ) ) );
}
QString cumulativeCutText = tr( "Cumulative pixel count cut" );
cboxContrastEnhancementLimits->addItem( tr( "Cumulative pixel count cut" ), "CumulativeCut" );
cboxContrastEnhancementLimits->addItem( tr( "Minimum / maximum" ), "MinMax" );
cboxContrastEnhancementLimits->addItem( tr( "Mean +/- standard deviation" ), "StdDev" );

QString contrastEnchacementLimits = settings.value( "/Raster/defaultContrastEnhancementLimits", "CumulativeCut" ).toString();

cboxContrastEnhancementLimits->setCurrentIndex( cboxContrastEnhancementLimits->findData( contrastEnchacementLimits ) );

chkUseStandardDeviation->setChecked( settings.value( "/Raster/useStandardDeviation", false ).toBool() );
spnThreeBandStdDev->setValue( settings.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble() );
toggleStandardDeviation( chkUseStandardDeviation->checkState() );

mRasterCumulativeCutLowerDoubleSpinBox->setValue( 100.0 * settings.value( "/Raster/cumulativeCutLower", QString::number( QgsRasterLayer::CUMULATIVE_CUT_LOWER ) ).toDouble() );
mRasterCumulativeCutUpperDoubleSpinBox->setValue( 100.0 * settings.value( "/Raster/cumulativeCutUpper", QString::number( QgsRasterLayer::CUMULATIVE_CUT_UPPER ) ).toDouble() );
@@ -726,18 +712,6 @@ void QgsOptions::fontSizeChanged( const QString &fontSize )
QgisApp::instance()->setFontSize( fontSize.toInt() );
}

void QgsOptions::toggleStandardDeviation( int state )
{
if ( Qt::Checked == state )
{
spnThreeBandStdDev->setEnabled( true );
}
else
{
spnThreeBandStdDev->setEnabled( false );
}
}

void QgsOptions::toggleEnableBackbuffer( int state )
{
#ifdef Q_WS_X11
@@ -904,24 +878,13 @@ void QgsOptions::saveOptions()
settings.setValue( "/Raster/defaultGreenBand", spnGreen->value() );
settings.setValue( "/Raster/defaultBlueBand", spnBlue->value() );

if ( cboxContrastEnhancementAlgorithm->currentText() == tr( "No Stretch" ) )
{
settings.setValue( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" );
}
else if ( cboxContrastEnhancementAlgorithm->currentText() == tr( "Stretch To MinMax" ) )
{
settings.setValue( "/Raster/defaultContrastEnhancementAlgorithm", "StretchToMinimumMaximum" );
}
else if ( cboxContrastEnhancementAlgorithm->currentText() == tr( "Stretch And Clip To MinMax" ) )
{
settings.setValue( "/Raster/defaultContrastEnhancementAlgorithm", "StretchAndClipToMinimumMaximum" );
}
else if ( cboxContrastEnhancementAlgorithm->currentText() == tr( "Clip To MinMax" ) )
{
settings.setValue( "/Raster/defaultContrastEnhancementAlgorithm", "ClipToMinimumMaximum" );
}
saveContrastEnhancement( cboxContrastEnhancementAlgorithmSingleBand, "singleBand" );
saveContrastEnhancement( cboxContrastEnhancementAlgorithmMultiBandSingleByte, "multiBandSingleByte" );
saveContrastEnhancement( cboxContrastEnhancementAlgorithmMultiBandMultiByte, "multiBandMultiByte" );

QString contrastEnhancementLimits = cboxContrastEnhancementLimits->itemData( cboxContrastEnhancementLimits->currentIndex() ).toString();
settings.setValue( "/Raster/defaultContrastEnhancementLimits", contrastEnhancementLimits );

settings.setValue( "/Raster/useStandardDeviation", chkUseStandardDeviation->isChecked() );
settings.setValue( "/Raster/defaultStandardDeviation", spnThreeBandStdDev->value() );

settings.setValue( "/Raster/cumulativeCutLower", mRasterCumulativeCutLowerDoubleSpinBox->value() / 100.0 );
@@ -1655,3 +1618,24 @@ void QgsOptions::on_pbnExportScales_clicked()
QgsDebugMsg( msg );
}
}

void QgsOptions::initContrastEnhancement( QComboBox *cbox, QString name, QString defaultVal )
{
QSettings settings;

//add items to the color enhanceContrast combo box
cbox->addItem( tr( "No Stretch" ), "NoEnhancement" );
cbox->addItem( tr( "Stretch To MinMax" ), "StretchToMinimumMaximum" );
cbox->addItem( tr( "Stretch And Clip To MinMax" ), "StretchAndClipToMinimumMaximum" );
cbox->addItem( tr( "Clip To MinMax" ), "ClipToMinimumMaximum" );

QString contrastEnchacement = settings.value( "/Raster/defaultContrastEnhancementAlgorithm/" + name, defaultVal ).toString();
cbox->setCurrentIndex( cbox->findData( contrastEnchacement ) );
}

void QgsOptions::saveContrastEnhancement( QComboBox *cbox, QString name )
{
QSettings settings;
QString value = cbox->itemData( cbox->currentIndex() ).toString();
settings.setValue( "/Raster/defaultContrastEnhancementAlgorithm/" + name, value );
}
@@ -72,8 +72,6 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase

void fontSizeChanged( const QString &fontSize );

void toggleStandardDeviation( int );

//! Slot to change backbuffering. This is handled when the user changes
// the value of the checkbox
void toggleEnableBackbuffer( int );
@@ -191,6 +189,8 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase

private:
QStringList i18nList();
void initContrastEnhancement( QComboBox *cbox, QString name, QString defaultVal );
void saveContrastEnhancement( QComboBox *cbox, QString name );
QgsCoordinateReferenceSystem mDefaultCrs;
QgsCoordinateReferenceSystem mLayerDefaultCrs;
bool mLoadedGdalDriverList;
@@ -395,3 +395,23 @@ void QgsContrastEnhancement::readXML( const QDomElement& elem )
setContrastEnhancementAlgorithm(( ContrastEnhancementAlgorithm )( algorithmElem.text().toInt() ) );
}
}

QgsContrastEnhancement::ContrastEnhancementAlgorithm QgsContrastEnhancement::contrastEnhancementAlgorithmFromString( const QString& contrastEnhancementString )
{
if ( contrastEnhancementString == "StretchToMinimumMaximum" )
{
return StretchToMinimumMaximum;
}
else if ( contrastEnhancementString == "StretchAndClipToMinimumMaximum" )
{
return StretchAndClipToMinimumMaximum;
}
else if ( contrastEnhancementString == "ClipToMinimumMaximum" )
{
return ClipToMinimumMaximum;
}
else
{
return NoEnhancement;
}
}
@@ -26,6 +26,7 @@ class originally created circa 2004 by T.Sutton, Gary E.Sherman, Steve Halasz
class QgsContrastEnhancementFunction;
class QDomDocument;
class QDomElement;
class QString;

/** \ingroup core
* Manipulates raster pixel values so that they enhanceContrast or clip into a
@@ -98,6 +99,8 @@ class CORE_EXPORT QgsContrastEnhancement

ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const { return mContrastEnhancementAlgorithm; }

static ContrastEnhancementAlgorithm contrastEnhancementAlgorithmFromString( const QString& contrastEnhancementString );

/*
*
* Non-Static methods
@@ -128,15 +128,19 @@ QgsRasterLayer::QgsRasterLayer(
{
QgsDebugMsg( "Entered" );

// TODO, call constructor with provider key for now
// TODO, call constructor with provider key
init();
setDataProvider( "gdal" );

bool defaultLoadedFlag = false;
if ( mValid && loadDefaultStyleFlag )
{
bool defaultLoadedFlag = false;
loadDefaultStyle( defaultLoadedFlag );
}
if ( !defaultLoadedFlag )
{
setDefaultContrastEnhancement();
}
return;
} // QgsRasterLayer ctor

@@ -164,13 +168,16 @@ QgsRasterLayer::QgsRasterLayer( const QString & uri,
init();
setDataProvider( providerKey );

// load default style if provider is gdal and if no style was given
// this should be an argument like in the other constructor
if ( mValid && providerKey == "gdal" && loadDefaultStyleFlag )
// load default style
bool defaultLoadedFlag = false;
if ( mValid && loadDefaultStyleFlag )
{
bool defaultLoadedFlag = false;
loadDefaultStyle( defaultLoadedFlag );
}
if ( !defaultLoadedFlag )
{
setDefaultContrastEnhancement();
}

// Default for the popup menu
// TODO: popMenu = 0;
@@ -1974,6 +1981,86 @@ void QgsRasterLayer::setContrastEnhancementFunction( QgsContrastEnhancementFunct
}
}

void QgsRasterLayer::setDefaultContrastEnhancement()
{
QgsDebugMsg( QString( "mDrawingStyle = %1" ).arg( mDrawingStyle ) );

QSettings mySettings;

QString myKey;
QString myDefault;

if ( mDrawingStyle == SingleBandGray || mDrawingStyle == MultiBandSingleBandGray )
{
myKey = "singleBand";
myDefault = "StretchToMinimumMaximum";
}
else if ( mDrawingStyle == MultiBandColor )
{
if ( dataProvider()->typeSize( dataProvider()->srcDataType( 1 ) ) == 8 )
{
myKey = "multiBandSingleByte";
myDefault = "NoEnhancement";
}
else
{
myKey = "multiBandMultiByte";
myDefault = "StretchToMinimumMaximum";
}
}

if ( myKey.isEmpty() )
{
QgsDebugMsg( "No default contrast enhancement for this drawing style" );
}
QgsDebugMsg( "myKey = " + myKey );

QString myAlgorithmString = mySettings.value( "/Raster/defaultContrastEnhancementAlgorithm/" + myKey, myDefault ).toString();

QgsContrastEnhancement::ContrastEnhancementAlgorithm myAlgorithm = QgsContrastEnhancement::contrastEnhancementAlgorithmFromString( myAlgorithmString );

QString myLimitsString = mySettings.value( "/Raster/defaultContrastEnhancementLimits", "CumulativeCut" ).toString();
ContrastEnhancementLimits myLimits = contrastEnhancementLimitsFromString( myLimitsString );

setContrastEnhancementAlgorithm( myAlgorithm, myLimits );
}

QString QgsRasterLayer::contrastEnhancementLimitsAsString( ContrastEnhancementLimits theLimits )
{
switch ( theLimits )
{
case QgsRasterLayer::ContrastEnhancementMinMax:
return "MinMax";
break;
case QgsRasterLayer::ContrastEnhancementStdDev:
return "StdDev";
break;
case QgsRasterLayer::ContrastEnhancementCumulativeCut:
return "CumulativeCut";
break;
default:
break;
}
return "None";
}

QgsRasterLayer::ContrastEnhancementLimits QgsRasterLayer::contrastEnhancementLimitsFromString( QString theLimits )
{
if ( theLimits == "MinMax" )
{
return ContrastEnhancementMinMax;
}
else if ( theLimits == "StdDev" )
{
return ContrastEnhancementStdDev;
}
else if ( theLimits == "CumulativeCut" )
{
return ContrastEnhancementCumulativeCut;
}
return ContrastEnhancementNone;
}

/**
*
* Implemented mainly for serialisation / deserialisation of settings to xml.
@@ -257,6 +257,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Contrast enhancement limits */
enum ContrastEnhancementLimits
{
ContrastEnhancementNone,
ContrastEnhancementMinMax,
ContrastEnhancementStdDev,
ContrastEnhancementCumulativeCut
@@ -565,6 +566,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Mutator for color shader algorithm */
Q_DECL_DEPRECATED void setColorShadingAlgorithm( QString theShaderAlgorithm );

static QString contrastEnhancementLimitsAsString( QgsRasterLayer::ContrastEnhancementLimits theLimits );
static ContrastEnhancementLimits contrastEnhancementLimitsFromString( QString theLimits );

/** \brief Mutator for contrast enhancement algorithm using min/max */
// TODO: remove in 2.0, replaced by following
void setContrastEnhancementAlgorithm( QgsContrastEnhancement::ContrastEnhancementAlgorithm theAlgorithm,
@@ -590,6 +594,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Mutator for contrast enhancement function */
void setContrastEnhancementFunction( QgsContrastEnhancementFunction* theFunction );

/** \brief Set default contrast enhancement */
void setDefaultContrastEnhancement();

/** \brief Overloaded version of the above function for convenience when restoring from xml */
void setDrawingStyle( const QString & theDrawingStyleQString );

0 comments on commit 8c86b61

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