Skip to content

Commit 478ddef

Browse files
author
ersts
committedSep 2, 2008
-Cleaning out references to obsolete class QgsColorTable
-Changed how Paletted bands are drawn as gray scale -Added cache to QgsColorRampShader to speed up rendering time when using large color tables (still poor) -Fixed small bug in rendering the palette as a pixmap git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9251 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

7 files changed

+100
-176
lines changed

7 files changed

+100
-176
lines changed
 

‎python/core/qgsrasterlayer.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public:
100100
void drawThumbnail(QPixmap * theQPixmap);
101101

102102
/** \brief Get an 8x8 pixmap of the colour palette. If the layer has no palette a white pixmap will be returned. */
103-
QPixmap getPaletteAsPixmap();
103+
QPixmap getPaletteAsPixmap(int theBand=1);
104104

105105
/** \brief This is called when the view on the rasterlayer needs to be refreshed (redrawn).
106106
*/
@@ -498,7 +498,7 @@ public slots:
498498
* \param band number
499499
* \return pointer to color table
500500
*/
501-
QgsColorTable *colorTable ( int theBandNoInt );
501+
QList<QgsColorRampShader::ColorRampItem>* getColorTable ( int theBandNoInt );
502502
protected:
503503

504504
/** reads vector layer specific state from project file Dom node.

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ void QgsRasterLayerProperties::sync()
828828
pixmapLegend->repaint();
829829

830830
//set the palette pixmap
831-
pixmapPalette->setPixmap( mRasterLayer->getPaletteAsPixmap() );
831+
pixmapPalette->setPixmap( mRasterLayer->getPaletteAsPixmap(mRasterLayer->getRasterBandNumber(mRasterLayer->getGrayBandName())));
832832
pixmapPalette->setScaledContents( true );
833833
pixmapPalette->repaint();
834834

@@ -1971,7 +1971,7 @@ void QgsRasterLayerProperties::on_pbnHistRefresh_clicked()
19711971
== QgsRasterLayer::PALETTE ) //paletted layers have hard coded color entries
19721972
{
19731973
QPolygonF myPolygon;
1974-
QgsColorTable *myColorTable = mRasterLayer->colorTable( 1 );
1974+
QgsColorRampShader* myRasterShaderFunction = ( QgsColorRampShader* )mRasterLayer->getRasterShader()->getRasterShaderFunction();
19751975
QgsDebugMsg( "Making paletted image histogram....computing band stats" );
19761976
QgsDebugMsg( QString( "myLastBinWithData = %1" ).arg( myLastBinWithData ) );
19771977

@@ -2002,8 +2002,7 @@ void QgsRasterLayerProperties::on_pbnHistRefresh_clicked()
20022002

20032003
QgsDebugMsg( QString( "myMiddle = %1" ).arg( myMiddle ) );
20042004

2005-
bool found = myColorTable->color( myMiddle, &c1, &c2, &c3 );
2006-
if ( !found )
2005+
if ( myRasterShaderFunction->generateShadedValue(myMiddle, &c1, &c2, &c3))
20072006
{
20082007
QgsDebugMsg( "Color not found" );
20092008
c1 = c2 = c3 = 180; // grey

‎src/core/raster/qgscolorrampshader.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@ originally part of the larger QgsRasterLayer class
2525
QgsColorRampShader::QgsColorRampShader( double theMinimumValue, double theMaximumValue ) : QgsRasterShaderFunction( theMinimumValue, theMaximumValue )
2626
{
2727
QgsDebugMsg( "called." );
28+
mMaximumColorCacheSize = 256; //good starting value
2829
}
2930

3031
bool QgsColorRampShader::generateShadedValue( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
3132
{
33+
//Get the shaded from the cache if it exists already
34+
QColor myColor = mColorCache.value(theValue);
35+
if(myColor.isValid())
36+
{
37+
*theReturnRedValue = myColor.red();
38+
*theReturnGreenValue = myColor.green();
39+
*theReturnBlueValue = myColor.blue();
40+
return true;
41+
}
42+
43+
//Else we have to generate the shaded value
3244
if ( QgsColorRampShader::INTERPOLATED == mColorRampType )
3345
{
3446
return getInterpolatedColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
@@ -81,15 +93,16 @@ bool QgsColorRampShader::getDiscreteColor( double theValue, int* theReturnRedVal
8193
myCurrentRampValue = it->value;
8294
if ( theValue <= myCurrentRampValue )
8395
{
84-
if ( last_it != mColorRampItemList.end() )
96+
*theReturnRedValue = it->color.red();
97+
*theReturnGreenValue = it->color.green();
98+
*theReturnBlueValue = it->color.blue();
99+
//Cache the shaded value
100+
if(mMaximumColorCacheSize <= mColorCache.size())
85101
{
86-
*theReturnRedValue = last_it->color.red();
87-
*theReturnGreenValue = last_it->color.green();
88-
*theReturnBlueValue = last_it->color.blue();
89-
return true;
102+
mColorCache.insert(theValue, it->color);
90103
}
104+
return true;
91105
}
92-
last_it = it;
93106
}
94107

95108
return false; // value not found
@@ -109,6 +122,11 @@ bool QgsColorRampShader::getExactColor( double theValue, int* theReturnRedValue,
109122
*theReturnRedValue = it->color.red();
110123
*theReturnGreenValue = it->color.green();
111124
*theReturnBlueValue = it->color.blue();
125+
//Cache the shaded value
126+
if(mMaximumColorCacheSize <= mColorCache.size())
127+
{
128+
mColorCache.insert(theValue, it->color);
129+
}
112130
return true;
113131
}
114132
}
@@ -143,6 +161,11 @@ bool QgsColorRampShader::getInterpolatedColor( double theValue, int* theReturnRe
143161
*theReturnRedValue = ( int )(( it->color.red() * myDiffTheValueLastRampValue + last_it->color.red() * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
144162
*theReturnGreenValue = ( int )(( it->color.green() * myDiffTheValueLastRampValue + last_it->color.green() * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
145163
*theReturnBlueValue = ( int )(( it->color.blue() * myDiffTheValueLastRampValue + last_it->color.blue() * myDiffCurrentRampValueTheValue ) / myCurrentRampRange );
164+
//Cache the shaded value
165+
if(mMaximumColorCacheSize <= mColorCache.size())
166+
{
167+
mColorCache.insert(theValue, it->color);
168+
}
146169
return true;
147170
}
148171
}

‎src/core/raster/qgscolorrampshader.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ originally part of the larger QgsRasterLayer class
2222
#define QGSCOLORRAMPSHADER_H
2323

2424
#include <QColor>
25+
#include <QMap>
2526

2627
#include "qgsrastershaderfunction.h"
2728

@@ -62,6 +63,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
6263
QgsColorRampShader::COLOR_RAMP_TYPE getColorRampType() {return mColorRampType;}
6364
QString getColorRampTypeAsQString();
6465

66+
/**Get the maximum size the color cache can be*/
67+
int getMaximumColorCacheSize() { return mMaximumColorCacheSize; }
68+
6569
/**Set custom colormap */
6670
void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem>& theList ) { mColorRampItemList = theList; }
6771

@@ -70,6 +74,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
7074

7175
/**Set the color ramp type*/
7276
void setColorRampType( QString );
77+
78+
/**Set the maximum size the color cache can be */
79+
void setMaximumColorCacheSize(int theSize) { mMaximumColorCacheSize = theSize; }
7380

7481

7582

@@ -85,6 +92,8 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
8592
QList<QgsColorRampShader::ColorRampItem> mColorRampItemList;
8693

8794
QgsColorRampShader::COLOR_RAMP_TYPE mColorRampType;
95+
QMap<double, QColor> mColorCache;
96+
int mMaximumColorCacheSize; //The color cache could eat a ton of memory if you have 32-bit data
8897
};
8998

9099
#endif

‎src/core/raster/qgsrasterbandstats.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include <limits>
2626

27-
#include "qgscolortable.h"
27+
#include "qgscolorrampshader.h"
2828
/** \ingroup core
2929
* The RasterBandStats struct is a container for statistics about a single
3030
* raster band.
@@ -82,6 +82,6 @@ class CORE_EXPORT QgsRasterBandStats
8282
/** whehter histogram compuation should include out of range values */
8383
bool histogramOutOfRangeFlag;
8484
/** Color table */
85-
QgsColorTable colorTable;
85+
QList<QgsColorRampShader::ColorRampItem> colorTable;
8686
};
8787
#endif

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 52 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ bool QgsRasterLayer::readFile( QString const & fileName )
521521
myRasterBandStats.bandNo = i;
522522
myRasterBandStats.statsGatheredFlag = false;
523523
myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector();
524-
// Read color table
525-
readColorTable( myGdalBand, &( myRasterBandStats.colorTable ) );
526-
524+
//Store the default color table
525+
readColorTable(i, &myRasterBandStats.colorTable);
526+
527527
mRasterStatsList.push_back( myRasterBandStats );
528528

529529
//Build a new contrast enhancement for the band and store in list
@@ -562,14 +562,11 @@ bool QgsRasterLayer::readFile( QString const & fileName )
562562

563563
drawingStyle = PALETTED_COLOR; //sensible default
564564

565-
//Load the color table from the band
566-
QList<QgsColorRampShader::ColorRampItem> myColorRampList;
567-
readColorTable( 1, &myColorRampList );
568565
//Set up a new color ramp shader
569566
setColorShadingAlgorithm( COLOR_RAMP );
570567
QgsColorRampShader* myColorRampShader = ( QgsColorRampShader* ) mRasterShader->getRasterShaderFunction();
571568
myColorRampShader->setColorRampType( QgsColorRampShader::EXACT );
572-
myColorRampShader->setColorRampItemList( myColorRampList );
569+
myColorRampShader->setColorRampItemList( *getColorTable(1) );
573570
}
574571
else if ( rasterLayerType == MULTIBAND )
575572
{
@@ -886,27 +883,23 @@ void QgsRasterLayer::drawThumbnail( QPixmap * theQPixmap )
886883

887884

888885

889-
QPixmap QgsRasterLayer::getPaletteAsPixmap()
886+
QPixmap QgsRasterLayer::getPaletteAsPixmap(int theBandNumber)
890887
{
891888
QgsDebugMsg( "entered." );
892889

893890
// Only do this for the non-provider (hard-coded GDAL) scenario...
894891
// Maybe WMS can do this differently using QImage::numColors and QImage::color()
895-
if (
896-
( mProviderKey.isEmpty() ) &&
897-
( hasBand( "Palette" ) ) //dont tr() this its a gdal word!
898-
)
892+
if (mProviderKey.isEmpty() && hasBand( "Palette" ) && theBandNumber > 0) //dont tr() this its a gdal word!
899893
{
900894
QgsDebugMsg( "....found paletted image" );
901-
QgsColorTable *myColorTable = colorTable( 1 );
902-
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, 1 );
903-
if ( GDALGetRasterColorInterpretation( myGdalBand ) == GCI_PaletteIndex && myColorTable->defined() )
895+
QgsColorRampShader myShader;
896+
QList<QgsColorRampShader::ColorRampItem> myColorRampItemList = myShader.getColorRampItemList();
897+
898+
if(readColorTable(1, &myColorRampItemList))
904899
{
905-
QgsDebugMsg( "....found GCI_PaletteIndex" );
906-
double myMin = myColorTable->rmin();
907-
double myMax = myColorTable->rmax();
908-
QgsDebugMsg( "myMin = " + QString::number( myMin ) + " myMax = " + QString::number( myMax ) );
909-
900+
QgsDebugMsg( "....got color ramp item list" );
901+
myShader.setColorRampItemList(myColorRampItemList);
902+
myShader.setColorRampType(QgsColorRampShader::DISCRETE);
910903
// Draw image
911904
int mySize = 100;
912905
QPixmap myPalettePixmap( mySize, mySize );
@@ -916,20 +909,17 @@ QPixmap QgsRasterLayer::getPaletteAsPixmap()
916909
myQImage.fill( 0 );
917910
myPalettePixmap.fill();
918911

919-
double myStep = ( myMax - myMin ) / ( mySize * mySize );
920-
912+
double myStep = ( (double)myColorRampItemList.size() - 1 ) / (double)( mySize * mySize );
913+
double myValue = 0.0;
921914
for ( int myRow = 0; myRow < mySize; myRow++ )
922915
{
923916
for ( int myCol = 0; myCol < mySize; myCol++ )
924917
{
925918

926-
double myValue = myMin + myStep * ( myCol + myRow * mySize );
927-
919+
myValue = myStep * (double)( myCol + myRow * mySize );
928920
int c1, c2, c3;
929-
bool found = myColorTable->color( myValue, &c1, &c2, &c3 );
930-
931-
if ( found )
932-
myQImage.setPixel( myCol, myRow, qRgb( c1, c2, c3 ) );
921+
myShader.generateShadedValue( myValue, &c1, &c2, &c3 );
922+
myQImage.setPixel( myCol, myRow, qRgb( c1, c2, c3 ) );
933923
}
934924
}
935925

@@ -1257,7 +1247,7 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
12571247

12581248
int myBandNo = 1;
12591249
drawPalettedSingleBandGray( theQPainter, theRasterViewPort,
1260-
theQgsMapToPixel, myBandNo, mGrayBandName );
1250+
theQgsMapToPixel, myBandNo);
12611251

12621252
break;
12631253
}
@@ -1498,6 +1488,7 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
14981488

14991489
double myPixelValue = 0.0;
15001490
int myAlphaValue = 0;
1491+
QgsDebugMsg( "Starting main render loop" );
15011492
for ( int myColumn = 0; myColumn < theRasterViewPort->drawableAreaYDim; ++myColumn )
15021493
{
15031494
for ( int myRow = 0; myRow < theRasterViewPort->drawableAreaXDim; ++myRow )
@@ -1581,7 +1572,8 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
15811572
int myGreenValue = 0;
15821573
int myBlueValue = 0;
15831574
int myAlphaValue = 0;
1584-
1575+
1576+
QgsDebugMsg( "Starting main render loop" );
15851577
for ( int myColumn = 0; myColumn < theRasterViewPort->drawableAreaYDim; ++myColumn )
15861578
{
15871579
for ( int myRow = 0; myRow < theRasterViewPort->drawableAreaXDim; ++myRow )
@@ -1637,8 +1629,7 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
16371629
* @param theColorQString - QString containing either 'Red' 'Green' or 'Blue' indicating which part of the rgb triplet will be used to render gray.
16381630
*/
16391631
void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort,
1640-
const QgsMapToPixel* theQgsMapToPixel, int theBandNo,
1641-
QString const & theColorQString )
1632+
const QgsMapToPixel* theQgsMapToPixel, int theBandNo)
16421633
{
16431634
QgsDebugMsg( "entered." );
16441635
//Invalid band number, segfault prevention
@@ -1647,7 +1638,11 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
16471638
return;
16481639
}
16491640

1650-
QgsRasterBandStats myRasterBandStats = getRasterBandStats( theBandNo );
1641+
if ( NULL == mRasterShader )
1642+
{
1643+
return;
1644+
}
1645+
16511646
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
16521647
GDALDataType myDataType = GDALGetRasterDataType( myGdalBand );
16531648
void *myGdalScanData = readData( myGdalBand, theRasterViewPort );
@@ -1658,41 +1653,23 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
16581653
return;
16591654
}
16601655

1661-
QgsColorTable *myColorTable = &( myRasterBandStats.colorTable );
1662-
16631656
QImage myQImage = QImage( theRasterViewPort->drawableAreaXDim, theRasterViewPort->drawableAreaYDim, QImage::Format_ARGB32 );
16641657
myQImage.fill( qRgba( 255, 255, 255, 0 ) ); // fill transparent
16651658

1666-
bool found = false;
16671659
double myPixelValue = 0.0;
1668-
int myRedLUTValue = 0;
1669-
int myGreenLUTValue = 0;
1670-
int myBlueLUTValue = 0;
1660+
int myRedValue = 0;
1661+
int myGreenValue = 0;
1662+
int myBlueValue = 0;
16711663
int myAlphaValue = 0;
16721664

1673-
//Set a pointer to the LUT color channel
1674-
int* myGrayValue;
1675-
if ( theColorQString == mRedBandName )
1676-
{
1677-
myGrayValue = &myRedLUTValue;
1678-
}
1679-
else if ( theColorQString == mGreenBandName )
1680-
{
1681-
myGrayValue = &myGreenLUTValue;
1682-
}
1683-
else
1684-
{
1685-
myGrayValue = &myBlueLUTValue;
1686-
}
1687-
1665+
QgsDebugMsg( "Starting main render loop" );
16881666
for ( int myColumn = 0; myColumn < theRasterViewPort->drawableAreaYDim; ++myColumn )
16891667
{
16901668
for ( int myRow = 0; myRow < theRasterViewPort->drawableAreaXDim; ++myRow )
16911669
{
1692-
myRedLUTValue = 0;
1693-
myGreenLUTValue = 0;
1694-
myBlueLUTValue = 0;
1695-
found = false;
1670+
myRedValue = 0;
1671+
myGreenValue = 0;
1672+
myBlueValue = 0;
16961673
myPixelValue = readValue( myGdalScanData, ( GDALDataType )myDataType,
16971674
myColumn * theRasterViewPort->drawableAreaXDim + myRow );
16981675

@@ -1707,15 +1684,23 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
17071684
continue;
17081685
}
17091686

1710-
found = myColorTable->color( myPixelValue, &myRedLUTValue, &myGreenLUTValue, &myBlueLUTValue );
1711-
if ( !found ) continue;
1687+
if ( !mRasterShader->generateShadedValue( myPixelValue, &myRedValue, &myGreenValue, &myBlueValue ) )
1688+
{
1689+
continue;
1690+
}
17121691

17131692
if ( mInvertPixelsFlag )
17141693
{
1715-
*myGrayValue = 255 - *myGrayValue;
1694+
//Invert flag, flip blue and read
1695+
double myGrayValue = (0.3 * (double)myRedValue) + (0.59 * (double)myGreenValue) + (0.11 * (double)myBlueValue);
1696+
myQImage.setPixel( myRow, myColumn, qRgba( (int)myGrayValue, (int)myGrayValue, (int)myGrayValue, myAlphaValue ) );
1697+
}
1698+
else
1699+
{
1700+
//Normal
1701+
double myGrayValue = (0.3 * (double)myBlueValue) + (0.59 * (double)myGreenValue) + (0.11 * (double)myRedValue);
1702+
myQImage.setPixel( myRow, myColumn, qRgba( (int)myGrayValue, (int)myGrayValue, (int)myGrayValue, myAlphaValue ) );
17161703
}
1717-
1718-
myQImage.setPixel( myRow, myColumn, qRgba( *myGrayValue, *myGrayValue, *myGrayValue, myAlphaValue ) );
17191704
}
17201705
}
17211706
CPLFree( myGdalScanData );
@@ -1787,6 +1772,7 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
17871772
int myBlueValue = 0;
17881773
int myAlphaValue = 0;
17891774

1775+
QgsDebugMsg( "Starting main render loop" );
17901776
for ( int myColumn = 0; myColumn < theRasterViewPort->drawableAreaYDim; ++myColumn )
17911777
{
17921778
for ( int myRow = 0; myRow < theRasterViewPort->drawableAreaXDim; ++myRow )
@@ -1964,6 +1950,8 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
19641950
QgsContrastEnhancement* myRedContrastEnhancement = getContrastEnhancement( myRedBandNo );
19651951
QgsContrastEnhancement* myGreenContrastEnhancement = getContrastEnhancement( myGreenBandNo );
19661952
QgsContrastEnhancement* myBlueContrastEnhancement = getContrastEnhancement( myBlueBandNo );
1953+
1954+
QgsDebugMsg( "Starting main render loop" );
19671955
for ( int myColumn = 0; myColumn < theRasterViewPort->drawableAreaYDim; ++myColumn )
19681956
{
19691957
for ( int myRow = 0; myRow < theRasterViewPort->drawableAreaXDim; ++myRow )
@@ -2209,10 +2197,6 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats( int theBandNo )
22092197

22102198
QString myColorerpretation = GDALGetColorInterpretationName( GDALGetRasterColorInterpretation( myGdalBand ) );
22112199

2212-
//declare a colorTable to hold a palette - will only be used if the layer color interp is palette ???
2213-
//get the palette colour table
2214-
QgsColorTable *myColorTable = &( myRasterBandStats.colorTable );
2215-
22162200
// XXX this sets the element count to a sensible value; but then you ADD to
22172201
// XXX it later while iterating through all the pixels?
22182202
//myRasterBandStats.elementCount = mRasterXDim * mRasterYDim;
@@ -2395,32 +2379,6 @@ const QgsRasterBandStats QgsRasterLayer::getRasterBandStats( int theBandNo )
23952379
continue; // NULL
23962380
}
23972381

2398-
//get the nth element from the current row
2399-
if ( myColorerpretation == "Palette" ) // dont translate this its a gdal string
2400-
{
2401-
//this is a palette layer so red / green / blue 'layers are 'virtual'
2402-
//in that we need to obtain the palette entry and then get the r,g or g
2403-
//component from that palette entry
2404-
2405-
int c1, c2, c3;
2406-
bool found = myColorTable->color( my, &c1, &c2, &c3 );
2407-
if ( !found ) continue;
2408-
2409-
//check for alternate color mappings
2410-
switch ( theBandNo )
2411-
{
2412-
case 1:
2413-
my = c1;
2414-
break;
2415-
case 2:
2416-
my = c2;
2417-
break;
2418-
case 3:
2419-
my = c3;
2420-
break;
2421-
}
2422-
}
2423-
24242382
myRasterBandStats.sumSqrDev += static_cast < double >
24252383
( pow( my - myRasterBandStats.mean, 2 ) );
24262384
}
@@ -3770,67 +3728,7 @@ bool QgsRasterLayer::readColorTable( int theBandNumber, QList<QgsColorRampShader
37703728
return true;
37713729
}
37723730

3773-
void QgsRasterLayer::readColorTable( GDALRasterBandH gdalBand, QgsColorTable *theColorTable )
3774-
{
3775-
QgsDebugMsg( "entered." );
3776-
3777-
// First try to read color table from metadata
3778-
char **metadata = GDALGetMetadata( gdalBand, NULL );
3779-
theColorTable->clear();
3780-
bool found = false;
3781-
while ( metadata && metadata[0] )
3782-
{
3783-
QStringList metadataTokens = QString( *metadata ).split( "=", QString::SkipEmptyParts );
3784-
3785-
if ( metadataTokens.count() < 2 ) continue;
3786-
3787-
if ( metadataTokens[0].contains( "COLOR_TABLE_RULE_RGB_" ) )
3788-
{
3789-
double min, max;
3790-
int min_c1, min_c2, min_c3, max_c1, max_c2, max_c3;
3791-
3792-
if ( sscanf( metadataTokens[1].toLocal8Bit().data(), "%lf %lf %d %d %d %d %d %d",
3793-
&min, &max, &min_c1, &min_c2, &min_c3, &max_c1, &max_c2, &max_c3 ) != 8 )
3794-
{
3795-
continue;
3796-
}
3797-
theColorTable->add( min, max,
3798-
( unsigned char )min_c1, ( unsigned char )min_c2, ( unsigned char )min_c3, 0,
3799-
( unsigned char )max_c1, ( unsigned char )max_c2, ( unsigned char )max_c3, 0 );
3800-
found = true;
3801-
}
3802-
++metadata;
3803-
}
3804-
theColorTable->sort();
3805-
3806-
// If no color table was found, try to read it from GDALColorTable
3807-
if ( !found )
3808-
{
3809-
GDALColorTableH gdalColorTable = GDALGetRasterColorTable( gdalBand );
3810-
3811-
if ( gdalColorTable )
3812-
{
3813-
int count = GDALGetColorEntryCount( gdalColorTable );
3814-
3815-
for ( int i = 0; i < count; i++ )
3816-
{
3817-
const GDALColorEntry *colorEntry = GDALGetColorEntry( gdalColorTable, i );
3818-
3819-
if ( !colorEntry ) continue;
3820-
3821-
theColorTable->add( i, ( unsigned char ) colorEntry->c1, ( unsigned char ) colorEntry->c2,
3822-
( unsigned char ) colorEntry->c3 );
3823-
}
3824-
}
3825-
}
3826-
3827-
#ifdef QGISDEBUG
3828-
theColorTable->print();
3829-
#endif
3830-
}
3831-
3832-
3833-
QgsColorTable *QgsRasterLayer::colorTable( int theBandNo )
3731+
QList<QgsColorRampShader::ColorRampItem>* QgsRasterLayer::getColorTable( int theBandNo )
38343732
{
38353733
return &( mRasterStatsList[theBandNo-1].colorTable );
38363734
}

‎src/core/raster/qgsrasterlayer.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ int CPL_STDCALL progressCallback( double dfComplete,
5555
//
5656
// Forward declarations
5757
//
58-
class QgsColorTable;
5958
class QgsMapToPixel;
6059
class QgsRect;
6160
class QgsRasterBandStats;
@@ -276,7 +275,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
276275
void drawThumbnail( QPixmap * theQPixmap );
277276

278277
/** \brief Get an 8x8 pixmap of the color palette. If the layer has no palette a white pixmap will be returned. */
279-
QPixmap getPaletteAsPixmap();
278+
QPixmap getPaletteAsPixmap(int theBand=1);
280279

281280
/** \brief This is called when the view on the raster layer needs to be refreshed (redrawn).
282281
*/
@@ -885,7 +884,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
885884
* \param band number
886885
* \return pointer to color table
887886
*/
888-
QgsColorTable *colorTable( int theBandNoInt );
887+
QList<QgsColorRampShader::ColorRampItem>* getColorTable( int theBandNoInt );
889888
protected:
890889

891890
/** reads vector layer specific state from project file Dom node.
@@ -947,8 +946,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
947946
void drawPalettedSingleBandGray( QPainter * theQPainter,
948947
QgsRasterViewPort * theRasterViewPort,
949948
const QgsMapToPixel* theQgsMapToPixel,
950-
int theBandNoInt,
951-
const QString & theColorQString );
949+
int theBandNoInt);
952950

953951
/** \brief Drawing routine for paletted image, rendered as a single band image in pseudocolor. */
954952
void drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
@@ -988,9 +986,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
988986
void paintImageToCanvas( QPainter* theQPainter, QgsRasterViewPort * theRasterViewPort,
989987
const QgsMapToPixel* theQgsMapToPixel, QImage* theImage );
990988

991-
/** \brief Read color table from GDAL raster band */
992-
void readColorTable( GDALRasterBandH gdalBand, QgsColorTable *theColorTable );
993-
994989
/** \brief Allocate memory and load data to that allocated memory, data type is the same
995990
* as raster band. The memory must be released later!
996991
* \return pointer to the memory

0 commit comments

Comments
 (0)
Please sign in to comment.