Skip to content
Permalink
Browse files
Store colors directly as qrgb in paletted raster renderer for faster …
…drawing
  • Loading branch information
mhugent committed Dec 9, 2012
1 parent daa02e3 commit eb79e21547157d218053d53d7f2f3e14282e4f34
Showing with 38 additions and 10 deletions.
  1. +34 −9 src/core/raster/qgspalettedrasterrenderer.cpp
  2. +4 −1 src/core/raster/qgspalettedrasterrenderer.h
@@ -25,6 +25,17 @@

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber,
QColor* colorArray, int nColors ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mNColors( nColors )
{
mColors = new QRgb[nColors];
for ( int i = 0; i < nColors; ++i )
{
mColors[i] = colorArray[i].rgba();
}
delete[] colorArray;
}

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mColors( colorArray ), mNColors( nColors )
{
}
@@ -36,7 +47,7 @@ QgsPalettedRasterRenderer::~QgsPalettedRasterRenderer()

QgsRasterInterface * QgsPalettedRasterRenderer::clone() const
{
QgsPalettedRasterRenderer * renderer = new QgsPalettedRasterRenderer( 0, mBand, colors(), mNColors );
QgsPalettedRasterRenderer * renderer = new QgsPalettedRasterRenderer( 0, mBand, rgbArray(), mNColors );
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setRasterTransparency( mRasterTransparency );
@@ -52,14 +63,14 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q

int bandNumber = elem.attribute( "band", "-1" ).toInt();
int nColors = 0;
QColor* colors = 0;
QRgb* colors = 0;

QDomElement paletteElem = elem.firstChildElement( "colorPalette" );
if ( !paletteElem.isNull() )
{
QDomNodeList paletteEntries = paletteElem.elementsByTagName( "paletteEntry" );
nColors = paletteEntries.size();
colors = new QColor[ nColors ];
colors = new QRgb[ nColors ];

int value = 0;
QDomElement entryElem;
@@ -68,7 +79,7 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
entryElem = paletteEntries.at( i ).toElement();
value = entryElem.attribute( "value", "0" ).toInt();
QgsDebugMsg( entryElem.attribute( "color", "#000000" ) );
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) );
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) ).rgba();
}
}
QgsRasterRenderer* r = new QgsPalettedRasterRenderer( input, bandNumber, colors, nColors );
@@ -85,11 +96,25 @@ QColor* QgsPalettedRasterRenderer::colors() const
QColor* colorArray = new QColor[ mNColors ];
for ( int i = 0; i < mNColors; ++i )
{
colorArray[i] = mColors[i];
colorArray[i] = QColor( mColors[i] );
}
return colorArray;
}

QRgb* QgsPalettedRasterRenderer::rgbArray() const
{
if ( mNColors < 1 )
{
return 0;
}
QRgb* rgbValues = new QRgb[mNColors];
for ( int i = 0; i < mNColors; ++i )
{
rgbValues[i] = mColors[i];
}
return rgbValues;
}

QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
{
QgsRasterBlock *outputBlock = new QgsRasterBlock();
@@ -158,7 +183,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
}
if ( !hasTransparency )
{
outputData[i] = mColors[ val ].rgba();
outputData[i] = mColors[ val ];
}
else
{
@@ -171,7 +196,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
{
currentOpacity *= alphaBlock->value( i ) / 255.0;
}
QColor& currentColor = mColors[val];
QColor currentColor = QColor( mColors[val] );
outputData[i] = qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 );
}
}
@@ -201,7 +226,7 @@ void QgsPalettedRasterRenderer::writeXML( QDomDocument& doc, QDomElement& parent
{
QDomElement colorElem = doc.createElement( "paletteEntry" );
colorElem.setAttribute( "value", i );
colorElem.setAttribute( "color", mColors[i].name() );
colorElem.setAttribute( "color", QColor( mColors[i] ).name() );
colorPaletteElem.appendChild( colorElem );
}
rasterRendererElem.appendChild( colorPaletteElem );
@@ -213,7 +238,7 @@ void QgsPalettedRasterRenderer::legendSymbologyItems( QList< QPair< QString, QCo
{
for ( int i = 0; i < mNColors; ++i )
{
symbolItems.push_back( qMakePair( QString::number( i ), mColors[i] ) );
symbolItems.push_back( qMakePair( QString::number( i ), QColor( mColors[i] ) ) );
}
}

@@ -31,6 +31,7 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
public:
/**Renderer owns color array*/
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray, int nColors );
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors );
~QgsPalettedRasterRenderer();
QgsRasterInterface * clone() const;
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterInterface* input );
@@ -43,6 +44,8 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
int nColors() const { return mNColors; }
/**Returns copy of color array (caller takes ownership)*/
QColor* colors() const;
/**Returns copy of rgb array (caller takes ownership)*/
QRgb* rgbArray() const;

void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;

@@ -53,7 +56,7 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
private:
int mBand;
/**Color array*/
QColor* mColors;
QRgb* mColors;
/**Number of colors*/
int mNColors;
};

0 comments on commit eb79e21

Please sign in to comment.