[QGIS Commit] r9198 - in trunk/qgis/src: app core/raster ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Aug 28 00:15:44 EDT 2008


Author: ersts
Date: 2008-08-28 00:15:44 -0400 (Thu, 28 Aug 2008)
New Revision: 9198

Modified:
   trunk/qgis/src/app/qgsrasterlayerproperties.cpp
   trunk/qgis/src/app/qgsrasterlayerproperties.h
   trunk/qgis/src/core/raster/qgsrasterlayer.cpp
   trunk/qgis/src/core/raster/qgsrasterlayer.h
   trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui
Log:
-Paletted image no longer open as faux RGB, but open as single band images with a color map
-Closes ticket #914
-Added a new QgsRasterlayer::readColorTable() that will read RGB/CMYK/HSV color maps from GDAL band
-Added load color map(palette) from band to color map tab
-QgsColorTable is now largely obsolete - more clean up need to totally remove
*Note raster layer type enum are no longer representative of the way layers are rendered and need to be updated

Modified: trunk/qgis/src/app/qgsrasterlayerproperties.cpp
===================================================================
--- trunk/qgis/src/app/qgsrasterlayerproperties.cpp	2008-08-27 22:18:34 UTC (rev 9197)
+++ trunk/qgis/src/app/qgsrasterlayerproperties.cpp	2008-08-28 04:15:44 UTC (rev 9198)
@@ -29,8 +29,8 @@
 #include "qgsmaplayerregistry.h"
 #include "qgscontrastenhancement.h"
 #include "qgsrastertransparency.h"
-#include "qgscolorrampshader.h"
 
+
 #include <QTableWidgetItem>
 #include <QHeaderView>
 
@@ -56,6 +56,7 @@
     TRSTRING_NOT_SET( tr( "Not Set" ) ),
     mRasterLayer( dynamic_cast<QgsRasterLayer*>( lyr ) )
 {
+
   ignoreSpinBoxEvent = false; //Short circuit signal loop between min max field and stdDev spin box
   mGrayActualMinimumMaximum = false;
   mRGBActualMinimumMaximum = false;
@@ -102,7 +103,7 @@
   cboxColorMap->addItem( tr( "Grayscale" ) );
   cboxColorMap->addItem( tr( "Pseudocolor" ) );
   cboxColorMap->addItem( tr( "Freak Out" ) );
-  cboxColorMap->addItem( tr( "Custom Colormap" ) );
+  cboxColorMap->addItem( tr( "Colormap" ) );
 
   //add items to the color stretch combo box
   cboxContrastEnhancementAlgorithm->addItem( tr( "No Stretch" ) );
@@ -145,139 +146,108 @@
   headerLabels << "Label";
   mColormapTreeWidget->setHeaderLabels( headerLabels );
 
-  //disable Custom colormap tab completely until 'Custom Colormap' is selected (and only for type GRAY_OR_UNDEFINED)
+  //disable colormap tab completely until 'Colormap' is selected (and only for type GRAY_OR_UNDEFINED)
   tabBar->setTabEnabled( tabBar->indexOf( tabPageColormap ), FALSE );
 
   //
   // Set up the combo boxes that contain band lists using the qstring list generated above
   //
 
-  if ( mRasterLayer->getRasterLayerType()
-       == QgsRasterLayer::PALETTE ) //paletted layers have hard coded color entries
-  {
-    cboRed->addItem( tr( "Red" ) );
-    cboGreen->addItem( tr( "Red" ) );
-    cboBlue->addItem( tr( "Red" ) );
+  QgsDebugMsg( "Populating band combo boxes" );
 
-    cboRed->addItem( tr( "Green" ) );
-    cboGreen->addItem( tr( "Green" ) );
-    cboBlue->addItem( tr( "Green" ) );
+  //
+  // Get a list of band names
+  //
+  QStringList myBandNameList;
 
-    cboRed->addItem( tr( "Blue" ) );
-    cboGreen->addItem( tr( "Blue" ) );
-    cboBlue->addItem( tr( "Blue" ) );
+  int myBandCountInt = mRasterLayer->getBandCount();
 
-    cboRed->addItem( TRSTRING_NOT_SET );
-    cboGreen->addItem( TRSTRING_NOT_SET );
-    cboBlue->addItem( TRSTRING_NOT_SET );
+  QgsDebugMsg( QString( "Looping though %1 image layers to get their names " ).arg( myBandCountInt ) );
 
-    cboGray->addItem( tr( "Red" ) );
-    cboGray->addItem( tr( "Green" ) );
-    cboGray->addItem( tr( "Blue" ) );
-    cboGray->addItem( TRSTRING_NOT_SET );
-
-    lstHistogramLabels->addItem( tr( "Palette" ) );
-  }
-  else                   // all other layer types use band name entries only
+  for ( int myIteratorInt = 1;
+        myIteratorInt <= myBandCountInt;
+        ++myIteratorInt )
   {
-    QgsDebugMsg( "Populating combos for non paletted layer" );
-
+    //find out the name of this band
+    QString myRasterBandNameQString = mRasterLayer->getRasterBandName( myIteratorInt ) ;
+    
+    //add the band to the histogram tab
     //
-    // Get a list of band names
-    //
-    QStringList myBandNameList;
+    QPixmap myPixmap( 10, 10 );
 
-    int myBandCountInt = mRasterLayer->getBandCount();
-#ifdef QGISDEBIG
-    QgsDebugMsg( QString( "Looping though %1 image layers to get their names " ).arg( myBandCountInt ) );
-#endif
-    for ( int myIteratorInt = 1;
-          myIteratorInt <= myBandCountInt;
-          ++myIteratorInt )
+    if ( myBandCountInt == 1 ) //draw single band images with black
     {
-      //find out the name of this band
-      QString myRasterBandNameQString = mRasterLayer->getRasterBandName( myIteratorInt ) ;
-
-      //
-      //add the band to the histogram tab
-      //
-      QPixmap myPixmap( 10, 10 );
-
-      if ( myBandCountInt == 1 ) //draw single band images with black
-      {
-        myPixmap.fill( Qt::black );
-      }
-      else if ( myIteratorInt == 1 )
-      {
-        myPixmap.fill( Qt::red );
-      }
-      else if ( myIteratorInt == 2 )
-      {
-        myPixmap.fill( Qt::green );
-      }
-      else if ( myIteratorInt == 3 )
-      {
-        myPixmap.fill( Qt::blue );
-      }
-      else if ( myIteratorInt == 4 )
-      {
-        myPixmap.fill( Qt::magenta );
-      }
-      else if ( myIteratorInt == 5 )
-      {
-        myPixmap.fill( Qt::darkRed );
-      }
-      else if ( myIteratorInt == 6 )
-      {
-        myPixmap.fill( Qt::darkGreen );
-      }
-      else if ( myIteratorInt == 7 )
-      {
-        myPixmap.fill( Qt::darkBlue );
-      }
-      else
-      {
-        myPixmap.fill( Qt::gray );
-      }
-      lstHistogramLabels->addItem( new QListWidgetItem( myPixmap, myRasterBandNameQString ) );
-      mGradientHeight = pixHistogram->height() / 2;
-      mGradientWidth = pixHistogram->width() / 2;
-      //keep a list of band names for later use
-      //! @note band names should not be translated!
-      myBandNameList.append( myRasterBandNameQString );
+      myPixmap.fill( Qt::black );
     }
-
-    //select all histogram layers list items by default
-    for ( int myIteratorInt = 1;
-          myIteratorInt <= myBandCountInt;
-          ++myIteratorInt )
+    else if ( myIteratorInt == 1 )
     {
-      QListWidgetItem *myItem = lstHistogramLabels->item( myIteratorInt - 1 );
-      myItem->setSelected( true );
+      myPixmap.fill( Qt::red );
     }
-
-    for ( QStringList::Iterator myIterator = myBandNameList.begin();
-          myIterator != myBandNameList.end();
-          ++myIterator )
+    else if ( myIteratorInt == 2 )
     {
-      QString myQString = *myIterator;
-#ifdef QGISDEBUG
+      myPixmap.fill( Qt::green );
+    }
+    else if ( myIteratorInt == 3 )
+    {
+      myPixmap.fill( Qt::blue );
+    }
+    else if ( myIteratorInt == 4 )
+    {
+      myPixmap.fill( Qt::magenta );
+    }
+    else if ( myIteratorInt == 5 )
+    {
+      myPixmap.fill( Qt::darkRed );
+    }
+    else if ( myIteratorInt == 6 )
+    {
+      myPixmap.fill( Qt::darkGreen );
+    }
+    else if ( myIteratorInt == 7 )
+    {
+      myPixmap.fill( Qt::darkBlue );
+    }
+    else
+    {
+      myPixmap.fill( Qt::gray );
+    }
+    lstHistogramLabels->addItem( new QListWidgetItem( myPixmap, myRasterBandNameQString ) );
+    mGradientHeight = pixHistogram->height() / 2;
+    mGradientWidth = pixHistogram->width() / 2;
+    //keep a list of band names for later use
+    //! @note band names should not be translated!
+    myBandNameList.append( myRasterBandNameQString );
+  }
 
-      QgsDebugMsg( QString( "Inserting : %1" ).arg( myQString ) );
-#endif
+  //select all histogram layers list items by default
+  for ( int myIteratorInt = 1;
+        myIteratorInt <= myBandCountInt;
+        ++myIteratorInt )
+  {
+    QListWidgetItem *myItem = lstHistogramLabels->item( myIteratorInt - 1 );
+    myItem->setSelected( true );
+  }
 
-      cboGray->addItem( myQString );
-      cboRed->addItem( myQString );
-      cboGreen->addItem( myQString );
-      cboBlue->addItem( myQString );
-    }
+  for ( QStringList::Iterator myIterator = myBandNameList.begin();
+        myIterator != myBandNameList.end();
+        ++myIterator )
+  {
+    QString myQString = *myIterator;
 
-    cboRed->addItem( TRSTRING_NOT_SET );
-    cboGreen->addItem( TRSTRING_NOT_SET );
-    cboBlue->addItem( TRSTRING_NOT_SET );
-    cboGray->addItem( TRSTRING_NOT_SET );
+    QgsDebugMsg( QString( "Inserting : %1" ).arg( myQString ) );
+
+    cboGray->addItem( myQString );
+    cboRed->addItem( myQString );
+    cboGreen->addItem( myQString );
+    cboBlue->addItem( myQString );
+    cboxColorMapBand->addItem( myQString );
   }
 
+  cboRed->addItem( TRSTRING_NOT_SET );
+  cboGreen->addItem( TRSTRING_NOT_SET );
+  cboBlue->addItem( TRSTRING_NOT_SET );
+  cboGray->addItem( TRSTRING_NOT_SET );
+
   cboxTransparencyBand->addItem( TRSTRING_NOT_SET );
 
   QIcon myPyramidPixmap( QgisApp::getThemeIcon( "/mIconPyramid.png" ) );
@@ -292,6 +262,7 @@
   pbtnMakeBandCombinationDefault->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
   pbtnMakeContrastEnhancementAlgorithmDefault->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
 
+  pbtnLoadColorMapFromBand->setIcon( QgisApp::getThemeIcon( "/mActionNewAttribute.png" ) );
   pbtnExportColorMapToFile->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
   pbtnLoadColorMapFromFile->setIcon( QgisApp::getThemeIcon( "/mActionFileOpen.png" ) );
 
@@ -357,6 +328,21 @@
  * PUBLIC METHODS
  *
  */
+void QgsRasterLayerProperties::populateColorMapTable(const QList<QgsColorRampShader::ColorRampItem>& theColorRampList)
+{
+  if ( theColorRampList.size() > 0 )
+  {
+    mColormapTreeWidget->clear();
+    QList<QgsColorRampShader::ColorRampItem>::const_iterator it;
+    for ( it = theColorRampList.begin(); it != theColorRampList.end(); ++it )
+    {
+      QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
+      newItem->setText( 0, QString::number( it->value, 'f' ) );
+      newItem->setBackground( 1, QBrush( it->color ) );
+      newItem->setText( 2, it->label );
+    }
+  }
+} 
 void QgsRasterLayerProperties::populateTransparencyTable()
 {
   //Clear existsing color transparency list
@@ -505,7 +491,7 @@
       labelContrastEnhancement->setEnabled( false );
       break;
     case QgsRasterLayer::PALETTED_SINGLE_BAND_PSEUDO_COLOR:
-      rbtnThreeBand->setEnabled( true );
+      rbtnThreeBand->setEnabled( false );
       rbtnSingleBand->setEnabled( true );
       rbtnSingleBand->setChecked( true );
       rbtnThreeBandMinMax->setEnabled( false );
@@ -602,7 +588,7 @@
     }
     else if ( mRasterLayer->getColorShadingAlgorithm() == QgsRasterLayer::COLOR_RAMP )
     {
-      cboxColorMap->setCurrentIndex( cboxColorMap->findText( tr( "Custom Colormap" ) ) );
+      cboxColorMap->setCurrentIndex( cboxColorMap->findText( tr( "Colormap" ) ) );
     }
     else if ( mRasterLayer->getColorShadingAlgorithm() == QgsRasterLayer::USER_DEFINED )
     {
@@ -783,10 +769,7 @@
   }
 
   //restore colormap tab if the layer has custom classification
-  if ( cboxColorMap->currentText() == tr( "Custom Colormap" ) )
-  {
-    syncColormapTab();
-  }
+  syncColormapTab();
 
   QgsDebugMsg( "populate general tab" );
   /*
@@ -873,21 +856,9 @@
     return;
   }
   //restore the colormap tab if layer has custom symbology
-  QList<QgsColorRampShader::ColorRampItem> myColorRampList = myRasterShaderFunction->getColorRampItemList();
-  if ( myColorRampList.size() > 0 )
-  {
-    QList<QgsColorRampShader::ColorRampItem>::const_iterator it;
-    for ( it = myColorRampList.begin(); it != myColorRampList.end(); ++it )
-    {
-      //restore state of colormap tab
-      QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
-      newItem->setText( 0, QString::number( it->value, 'f' ) );
-      newItem->setBackground( 1, QBrush( it->color ) );
-      newItem->setText( 2, it->label );
-    }
-  }
+  populateColorMapTable(myRasterShaderFunction->getColorRampItemList());
 
-  sboxNumberOfEntries->setValue( myColorRampList.size() );
+  sboxNumberOfEntries->setValue( mColormapTreeWidget->topLevelItemCount() );
 
   //restor state of 'color interpolation' combo box
   if ( QgsColorRampShader::INTERPOLATED == myRasterShaderFunction->getColorRampType() )
@@ -1082,7 +1053,7 @@
   {
     mRasterLayer->setColorShadingAlgorithm( QgsRasterLayer::FREAK_OUT );
   }
-  else if ( cboxColorMap->currentText() == tr( "Custom Colormap" ) )
+  else if ( cboxColorMap->currentText() == tr( "Colormap" ) )
   {
     mRasterLayer->setColorShadingAlgorithm( QgsRasterLayer::COLOR_RAMP );
   }
@@ -1379,7 +1350,7 @@
   /*
    * ColorMap Tab
    */
-  if ( cboxColorMap->currentText() == tr( "Custom Colormap" ) )
+  if ( cboxColorMap->currentText() == tr( "Colormap" ) )
   {
     QgsColorRampShader* myRasterShaderFunction = ( QgsColorRampShader* )mRasterLayer->getRasterShader()->getRasterShaderFunction();
     if ( myRasterShaderFunction )
@@ -1700,7 +1671,7 @@
     cboxContrastEnhancementAlgorithm->setEnabled( false );
     labelContrastEnhancement->setEnabled( false );
   }
-  else if ( mRasterLayerIsGdal && theText == tr( "Custom Colormap" ) )
+  else if ( mRasterLayerIsGdal && theText == tr( "Colormap" ) )
   {
     tabBar->setTabEnabled( tabBar->indexOf( tabPageColormap ), TRUE );
     rbtnSingleBandMinMax->setEnabled( false );
@@ -2712,6 +2683,22 @@
   }
 }
 
+void QgsRasterLayerProperties::on_pbtnLoadColorMapFromBand_clicked()
+{
+  QList<QgsColorRampShader::ColorRampItem> myColorRampList;
+  if(mRasterLayer->readColorTable(cboxColorMapBand->currentIndex()+1, &myColorRampList))
+  {
+    populateColorMapTable(myColorRampList);
+    cboxColorInterpolation->setCurrentIndex( cboxColorInterpolation->findText( tr( "Exact" ) ) );
+    QgsDebugMsg("Color map loaded");
+  }
+  else
+  {
+    QMessageBox::warning(this, tr("Load Color Map"), tr("The color map for Band %n failed to load", "", cboxColorMapBand->currentIndex()+1));
+    QgsDebugMsg("Color map failed to load");
+  }
+}
+
 void QgsRasterLayerProperties::on_pbtnLoadColorMapFromFile_clicked()
 {
   int myLineCounter = 0;

Modified: trunk/qgis/src/app/qgsrasterlayerproperties.h
===================================================================
--- trunk/qgis/src/app/qgsrasterlayerproperties.h	2008-08-27 22:18:34 UTC (rev 9197)
+++ trunk/qgis/src/app/qgsrasterlayerproperties.h	2008-08-28 04:15:44 UTC (rev 9198)
@@ -22,6 +22,8 @@
 
 #include "ui_qgsrasterlayerpropertiesbase.h"
 #include "qgisgui.h"
+#include "qgscolorrampshader.h"
+
 class QgsMapLayer;
 class QgsRasterLayer;
 
@@ -108,7 +110,9 @@
     void handleColormapTreeWidgetDoubleClick( QTreeWidgetItem* item, int column );
     /**This slots saves the current color map to a file */
     void on_pbtnExportColorMapToFile_clicked();
-    /**This slots saves the current color map to a file */
+    /**This slots loads the current color map from a band */
+    void on_pbtnLoadColorMapFromBand_clicked();
+    /**This slots loads the current color map from a file */
     void on_pbtnLoadColorMapFromFile_clicked();
     /**This slot loads the minimum and maximum values from the raster band and updates the gui*/
     void on_pbtnLoadMinMax_clicked();
@@ -172,6 +176,9 @@
         included or not
      */
     bool mRasterLayerIsWms;
+    
+    /** \brief Clear current color map table and population with values from new list */
+    void populateColorMapTable(const QList<QgsColorRampShader::ColorRampItem>&);
 
     /** \brief Clear the current transparency table and populate the table with the correct types for current drawing mode and data type*/
     void populateTransparencyTable();

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2008-08-27 22:18:34 UTC (rev 9197)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2008-08-28 04:15:44 UTC (rev 9198)
@@ -537,8 +537,7 @@
   setContrastEnhancementAlgorithm( myQSettings.value( "/Raster/defaultContrastEnhancementAlgorithm", "STRETCH_TO_MINMAX" ).toString() );
 
   //decide what type of layer this is...
-  //note that multiband images can have one or more 'undefindd' bands,
-  //so we must do this check first!
+  //TODO Change this to look at the color interp and palette interp to decide which type of layer it is
   if (( GDALGetRasterCount( mGdalDataset ) > 1 ) )
   {
     rasterLayerType = MULTIBAND;
@@ -554,13 +553,23 @@
 
   if ( rasterLayerType == PALETTE )
   {
-    mRedBandName = "Red"; // sensible default
-    mGreenBandName = "Green"; // sensible default
-    mBlueBandName = "Blue";// sensible default
+    mRedBandName = TRSTRING_NOT_SET; // sensible default
+    mGreenBandName = TRSTRING_NOT_SET; // sensible default
+    mBlueBandName = TRSTRING_NOT_SET;// sensible default
     mTransparencyBandName = TRSTRING_NOT_SET; // sensible default
-    mGrayBandName = TRSTRING_NOT_SET;  //sensible default
-    drawingStyle = PALETTED_MULTI_BAND_COLOR; //sensible default
-    setContrastEnhancementAlgorithm( QgsContrastEnhancement::NO_STRETCH );
+    mGrayBandName = getRasterBandName(1);  //sensible default
+    QgsDebugMsg(mGrayBandName);
+    
+    drawingStyle = PALETTED_SINGLE_BAND_PSEUDO_COLOR; //sensible default
+    
+    //Load the color table from the band
+    QList<QgsColorRampShader::ColorRampItem> myColorRampList;
+    readColorTable(1, &myColorRampList);
+    //Set up a new color ramp shader
+    setColorShadingAlgorithm(COLOR_RAMP);
+    QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader->getRasterShaderFunction();
+    myColorRampShader->setColorRampType(QgsColorRampShader::EXACT);
+    myColorRampShader->setColorRampItemList(myColorRampList);
   }
   else if ( rasterLayerType == MULTIBAND )
   {
@@ -583,14 +592,12 @@
   }
   else                        //GRAY_OR_UNDEFINED
   {
-    //Disabled automatically generating stats to improve initial load speed.
-    //getRasterBandStats(1);
     mRedBandName = TRSTRING_NOT_SET; //sensible default
     mGreenBandName = TRSTRING_NOT_SET; //sensible default
     mBlueBandName = TRSTRING_NOT_SET;  //sensible default
     mTransparencyBandName = TRSTRING_NOT_SET;  //sensible default
     drawingStyle = SINGLE_BAND_GRAY;  //sensible default
-    mGrayBandName = getRasterBandName( 1 ); // usually gdal will return gray or undefined
+    mGrayBandName = getRasterBandName( 1 );
   }
 
   //mark the layer as valid
@@ -1710,7 +1717,12 @@
     return;
   }
 
-  QgsRasterBandStats myRasterBandStats = getRasterBandStats( theBandNo );
+  QgsRasterBandStats myRasterBandStats;
+  //If there is a color ramp, i.e., a paletted layer, then no need to generate stats
+  if(COLOR_RAMP != mColorShadingAlgorithm)
+  {
+    myRasterBandStats = getRasterBandStats( theBandNo );
+  }
   GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
   GDALDataType myDataType = GDALGetRasterDataType( myGdalBand );
   void *myGdalScanData = readData( myGdalBand, theRasterViewPort );
@@ -2186,7 +2198,7 @@
 // get the name of a band given its number
 const QString QgsRasterLayer::getRasterBandName( int theBandNo )
 {
-
+  QgsDebugMsg("entered.");
   if ( theBandNo <= mRasterStatsList.size() && theBandNo > 0 )
   {
     //vector starts at base 0, band counts at base1 !
@@ -2239,7 +2251,6 @@
 
 */
 //TODO: This method needs some cleaning up PJE 2007-12-30
-//TODO: Values that are only used in debug should be totally wrapped in debug ifdefs
 const QgsRasterBandStats QgsRasterLayer::getRasterBandStats( int theBandNo )
 {
   // check if we have received a valid band number
@@ -2277,41 +2288,7 @@
   //declare a colorTable to hold a palette - will only be used if the layer color interp is palette ???
   //get the palette colour table
   QgsColorTable *myColorTable = &( myRasterBandStats.colorTable );
-  if ( rasterLayerType == PALETTE )
-  {
 
-    //override the band name - palette images are really only one band
-    //so we are faking three band behaviour
-    switch ( theBandNo )
-    {
-        // a "Red" layer
-      case 1:
-        myRasterBandStats.bandName = mRedBandName;
-        break;
-      case 2:
-        myRasterBandStats.bandName = mBlueBandName;
-        break;
-      case 3:
-        myRasterBandStats.bandName = mGreenBandName;
-        break;
-      default:
-        //invalid band id so return
-        QgsRasterBandStats myNullReturnStats;
-        return myNullReturnStats;
-        break;
-    }
-  }
-  else if ( rasterLayerType == GRAY_OR_UNDEFINED )
-  {
-    //PJE 2008-01-14 This function should not be changing the band name,
-    //The format below is not the same as the constructor
-    //myRasterBandStats.bandName = myColorerpretation;
-  }
-  else //rasterLayerType is MULTIBAND
-  {
-    //do nothing
-  }
-
   // XXX this sets the element count to a sensible value; but then you ADD to
   // XXX it later while iterating through all the pixels?
   //myRasterBandStats.elementCount = mRasterXDim * mRasterYDim;
@@ -2559,36 +2536,29 @@
 
 QString QgsRasterLayer::validateBandName( QString const & theBandName )
 {
-  QgsDebugMsg( "validateBandName :  Checking..." );
+  QgsDebugMsg( "Checking..." );
   //check if the band is unset
   if ( theBandName == TRSTRING_NOT_SET || theBandName == QSTRING_NOT_SET )
   {
-    QgsDebugMsg( "validateBandName :  Band name is '" + QSTRING_NOT_SET + "'. Nothing to do." );
+    QgsDebugMsg( "Band name is '" + QSTRING_NOT_SET + "'. Nothing to do." );
     // Use translated name internally
     return TRSTRING_NOT_SET;
   }
 
-  //check if the image is paletted
-  if ( rasterLayerType == PALETTE && ( theBandName == "Red" || theBandName == "Green" || theBandName == "Blue" ) )
-  {
-    QgsDebugMsg( "validateBandName :  Paletted image valid faux RGB band name" );
-    return theBandName;
-  }
-
   //check that a valid band name was passed
-  QgsDebugMsg( "validateBandName :  Looking through raster band stats for matching band name" );
+  QgsDebugMsg( "Looking through raster band stats for matching band name" );
   for ( int myIterator = 0; myIterator < mRasterStatsList.size(); ++myIterator )
   {
     //find out the name of this band
     if ( mRasterStatsList[myIterator].bandName == theBandName )
     {
-      QgsDebugMsg( "validateBandName :  Matching band name found" );
+      QgsDebugMsg( "Matching band name found" );
       return theBandName;
     }
   }
-  QgsDebugMsg( "validateBandName :  No matching band name found in raster band stats" );
+  QgsDebugMsg( "No matching band name found in raster band stats" );
 
-  QgsDebugMsg( "validateBandName :  Testing older naming format" );
+  QgsDebugMsg( "Testing older naming format" );
   //See of the band in an older format #:something.
   //TODO Remove test in v2.0
   if ( theBandName.contains( ':' ) )
@@ -2599,14 +2569,14 @@
       int myBandNumber = myBandNameComponents.at( 0 ).toInt();
       if ( myBandNumber > 0 )
       {
-        QgsDebugMsg( "validateBandName :  Transformed older name format to current format" );
+        QgsDebugMsg( "Transformed older name format to current format" );
         return "Band " + QString::number( myBandNumber );
       }
     }
   }
 
   //if no matches were found default to not set
-  QgsDebugMsg( "validateBandName :  All checks failed, returning '" + QSTRING_NOT_SET + "'" );
+  QgsDebugMsg( "All checks failed, returning '" + QSTRING_NOT_SET + "'" );
   return TRSTRING_NOT_SET;
 }
 
@@ -3792,9 +3762,90 @@
   return false;
 }
 
+bool QgsRasterLayer::readColorTable( int theBandNumber, QList<QgsColorRampShader::ColorRampItem>* theList)
+{
+  QgsDebugMsg( "entered." );
+  //Invalid band number, segfault prevention
+  if (0 >= theBandNumber || 0 == theList)
+  {
+    QgsDebugMsg("Invalid paramter");
+    return false;
+  }
+  
+  GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset, theBandNumber);
+  GDALColorTableH myGdalColorTable = GDALGetRasterColorTable(myGdalBand);
 
+  if (myGdalColorTable)
+  {
+    QgsDebugMsg("Color table found");
+    int myEntryCount = GDALGetColorEntryCount(myGdalColorTable);
+    GDALColorInterp myColorInterpretation =  GDALGetRasterColorInterpretation(myGdalBand);
+    QgsDebugMsg("Color Interpretation: " + QString::number((int)myColorInterpretation));
+    GDALPaletteInterp myPaletteInterpretation  = GDALGetPaletteInterpretation(myGdalColorTable);
+    QgsDebugMsg("Palette Interpretation: " + QString::number((int)myPaletteInterpretation));
+    
+    const GDALColorEntry* myColorEntry = 0;
+    for ( int myIterator = 0; myIterator < myEntryCount; myIterator++ )
+    {
+      myColorEntry = GDALGetColorEntry( myGdalColorTable, myIterator );
+      
+      if ( !myColorEntry ) 
+      {
+        continue;
+      }
+      else
+      {
+        //Branch on the color interpretation type
+        if(myColorInterpretation == GCI_GrayIndex)
+        {
+          QgsColorRampShader::ColorRampItem myColorRampItem;
+          myColorRampItem.label = "";
+          myColorRampItem.value = (double)myIterator;
+          myColorRampItem.color = QColor::fromRgb(myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4);
+          theList->append(myColorRampItem);
+        }
+        else if(myColorInterpretation = GCI_PaletteIndex)
+        {
+          QgsColorRampShader::ColorRampItem myColorRampItem;
+          myColorRampItem.label = "";
+          myColorRampItem.value = (double)myIterator;
+          //Branch on palette interpretation
+          if(myPaletteInterpretation  == GPI_RGB)
+          {
+            myColorRampItem.color = QColor::fromRgb(myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4); 
+          }
+          else if(myPaletteInterpretation  == GPI_CMYK)
+          {
+            myColorRampItem.color = QColor::fromCmyk(myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4); 
+          }
+          else if(myPaletteInterpretation  == GPI_HLS)
+          {
+            myColorRampItem.color = QColor::fromHsv(myColorEntry->c1, myColorEntry->c3, myColorEntry->c2, myColorEntry->c4); 
+          }
+          else
+          {
+            myColorRampItem.color = QColor::fromRgb(myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4);  
+          }
+          theList->append(myColorRampItem);
+        }
+        else
+        {
+          QgsDebugMsg("Color interpretation type not supported yet");
+          return false;
+        }
+      }
+    }
+  }
+  else
+  {
+    QgsDebugMsg("No color table found for band " + QString::number(theBandNumber));
+    return false;
+  }
+  
+  QgsDebugMsg("Color table loaded sucessfully");
+  return true;
+}
 
-
 void QgsRasterLayer::readColorTable( GDALRasterBandH gdalBand, QgsColorTable *theColorTable )
 {
   QgsDebugMsg( "entered." );

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.h
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.h	2008-08-27 22:18:34 UTC (rev 9197)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.h	2008-08-28 04:15:44 UTC (rev 9198)
@@ -136,6 +136,7 @@
 #include "qgscontrastenhancement.h"
 #include "qgsrastertransparency.h"
 #include "qgsrastershader.h"
+#include "qgscolorrampshader.h"
 #include "qgsrastershaderfunction.h"
 #include "qgsrasterdataprovider.h"
 
@@ -629,7 +630,9 @@
       }
     }
 
-
+    /** \brief Read color table from GDAL raster band */
+    bool readColorTable( int theBandNumber, QList<QgsColorRampShader::ColorRampItem>* theList);
+    
     /** \brief This enumerator describes the different kinds of drawing we can do.  */
     enum DRAWING_STYLE
     {
@@ -638,8 +641,8 @@
       SINGLE_BAND_PSEUDO_COLOR,// a "Gray" or "Undefined" layer drawn using a pseudocolor algorithm
       PALETTED_COLOR, //a "Palette" image drawn using color table
       PALETTED_SINGLE_BAND_GRAY,// a "Palette" layer drawn in gray scale (using only one of the color components)
-      PALETTED_SINGLE_BAND_PSEUDO_COLOR, // a "Palette" layer having only one of its color components rendered as psuedo color
-      PALETTED_MULTI_BAND_COLOR, // a "Palette" image is decomposed to 3 channels (RGB) and drawn
+      PALETTED_SINGLE_BAND_PSEUDO_COLOR, // a "Palette" layer having only one of its color components rendered as psuedo color --PJE20080827 this is no longer accurate as it is used to shade the color ramp as well
+      PALETTED_MULTI_BAND_COLOR, // a "Palette" image is decomposed to 3 channels (RGB) and drawn --PJE20080827 this is no longer accurate
       // as multiband
       MULTI_BAND_SINGLE_BAND_GRAY, // a layer containing 2 or more bands, but using only one band to produce a grayscale image
       MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR, //a layer containing 2 or more bands, but using only one band to produce a pseudocolor image

Modified: trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui	2008-08-27 22:18:34 UTC (rev 9197)
+++ trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui	2008-08-28 04:15:44 UTC (rev 9198)
@@ -1228,6 +1228,9 @@
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
+           <property name="maximum" >
+            <number>256</number>
+           </property>
           </widget>
          </item>
         </layout>
@@ -1245,7 +1248,7 @@
          </property>
         </spacer>
        </item>
-       <item row="0" column="3" colspan="3" >
+       <item row="0" column="3" colspan="4" >
         <layout class="QHBoxLayout" >
          <property name="leftMargin" >
           <number>11</number>
@@ -1284,7 +1287,7 @@
          </property>
         </spacer>
        </item>
-       <item row="1" column="3" colspan="3" >
+       <item row="1" column="3" colspan="4" >
         <layout class="QHBoxLayout" >
          <property name="leftMargin" >
           <number>11</number>
@@ -1324,20 +1327,56 @@
          </property>
         </widget>
        </item>
-       <item row="2" column="2" colspan="2" >
+       <item row="2" column="2" >
         <spacer>
          <property name="orientation" >
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" >
           <size>
-           <width>281</width>
+           <width>211</width>
            <height>27</height>
           </size>
          </property>
         </spacer>
        </item>
+       <item row="2" column="3" >
+        <layout class="QHBoxLayout" >
+         <property name="leftMargin" >
+          <number>11</number>
+         </property>
+         <item>
+          <widget class="QComboBox" name="cboxColorMapBand" />
+         </item>
+         <item>
+          <widget class="QToolButton" name="pbtnLoadColorMapFromBand" >
+           <property name="toolTip" >
+            <string>Load color map from band</string>
+           </property>
+           <property name="text" >
+            <string>...</string>
+           </property>
+           <property name="icon" >
+            <iconset>../../images/themes/default/mActionNewAttribute.png</iconset>
+           </property>
+          </widget>
+         </item>
+        </layout>
+       </item>
        <item row="2" column="4" >
+        <spacer>
+         <property name="orientation" >
+          <enum>Qt::Horizontal</enum>
+         </property>
+         <property name="sizeHint" >
+          <size>
+           <width>20</width>
+           <height>20</height>
+          </size>
+         </property>
+        </spacer>
+       </item>
+       <item row="2" column="5" >
         <widget class="QToolButton" name="pbtnLoadColorMapFromFile" >
          <property name="toolTip" >
           <string>Load color map from file</string>
@@ -1350,7 +1389,7 @@
          </property>
         </widget>
        </item>
-       <item row="2" column="5" >
+       <item row="2" column="6" >
         <widget class="QToolButton" name="pbtnExportColorMapToFile" >
          <property name="toolTip" >
           <string>Export color map to file</string>
@@ -1363,7 +1402,7 @@
          </property>
         </widget>
        </item>
-       <item row="3" column="0" colspan="6" >
+       <item row="3" column="0" colspan="7" >
         <widget class="QTreeWidget" name="mColormapTreeWidget" >
          <property name="columnCount" >
           <number>3</number>



More information about the QGIS-commit mailing list