Skip to content

Commit 478ddef

Browse files
author
ersts
committed
-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
1 parent 1ca3099 commit 478ddef

7 files changed

+100
-176
lines changed

python/core/qgsrasterlayer.sip

+2-2
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

+3-4
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

+29-6
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

+9
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

+2-2
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

0 commit comments

Comments
 (0)