Skip to content

Commit

Permalink
Added raster renderer interface and test implementation for paletted …
Browse files Browse the repository at this point in the history
…rasters
  • Loading branch information
mhugent committed Dec 18, 2011
1 parent 70865e5 commit 79b8a76
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "qgspalettedrasterrenderer.h"
#include "qgsrasterviewport.h"
#include <QColor>
#include <QImage>
#include <QPainter>

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterDataProvider* provider, int bandNumber, QColor* colorArray, int nColors ):
QgsRasterRenderer( provider ), mBandNumber( bandNumber), mColors( colorArray ), mNColors( nColors )
{
}

QgsPalettedRasterRenderer::~QgsPalettedRasterRenderer()
{
delete[] mColors;
}

void QgsPalettedRasterRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
{
if( !p || !mProvider || !viewPort || !theQgsMapToPixel )
{
return;
}

//read data from provider
int typeSize = mProvider->dataTypeSize( mBandNumber ) / 8;
QgsRasterDataProvider::DataType rasterType = (QgsRasterDataProvider::DataType)mProvider->dataType( mBandNumber );
void* rasterData = VSIMalloc( typeSize * viewPort->drawableAreaXDim * viewPort->drawableAreaYDim);
mProvider->readBlock( mBandNumber, viewPort->mDrawnExtent, viewPort->drawableAreaXDim, viewPort->drawableAreaYDim,
viewPort->mSrcCRS, viewPort->mDestCRS, rasterData );
int currentRasterPos = 0;

//raster image
QImage img( viewPort->drawableAreaXDim, viewPort->drawableAreaYDim, QImage::Format_ARGB32_Premultiplied );
QRgb* imageScanLine = 0;

for( int i = 0; i < viewPort->drawableAreaYDim; ++i )
{
imageScanLine = ( QRgb* )( img.scanLine( i ) );
for( int j = 0; j < viewPort->drawableAreaXDim; ++j )
{
int val = readValue( rasterData, rasterType, currentRasterPos );
imageScanLine[j] = mColors[ val ].rgba();
++currentRasterPos;
}
}
CPLFree( rasterData );
p->drawImage( QPointF( viewPort->topLeftPoint.x(), viewPort->topLeftPoint.y() ), img );
}
24 changes: 24 additions & 0 deletions src/core/raster/qgspalettedrasterrenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef QGSPALETTEDRASTERRENDERER_H
#define QGSPALETTEDRASTERRENDERER_H

#include "qgsrasterrenderer.h"

class QColor;

class QgsPalettedRasterRenderer: public QgsRasterRenderer
{
public:
/**Renderer owns color array*/
QgsPalettedRasterRenderer( QgsRasterDataProvider* provider, int bandNumber, QColor* colorArray, int nColors );
~QgsPalettedRasterRenderer();
void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

private:
int mBandNumber;
/**Color array*/
QColor* mColors;
/**Number of colors*/
int mNColors;
};

#endif // QGSPALETTEDRASTERRENDERER_H
9 changes: 9 additions & 0 deletions src/core/raster/qgsrasterrenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "qgsrasterrenderer.h"

QgsRasterRenderer::QgsRasterRenderer( QgsRasterDataProvider* provider ): mProvider( provider )
{
}

QgsRasterRenderer::~QgsRasterRenderer()
{
}
66 changes: 66 additions & 0 deletions src/core/raster/qgsrasterrenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef QGSRASTERRENDERER_H
#define QGSRASTERRENDERER_H

#include "qgsrasterdataprovider.h"

class QPainter;
class QgsMapToPixel;
class QgsRasterViewPort;

class QgsRasterRenderer
{
public:
QgsRasterRenderer( QgsRasterDataProvider* provider );
virtual ~QgsRasterRenderer();
virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel ) = 0;

protected:
inline double readValue( void *data, QgsRasterDataProvider::DataType type, int index );

QgsRasterDataProvider* mProvider;
};

inline double QgsRasterRenderer::readValue( void *data, QgsRasterDataProvider::DataType type, int index )
{
if( !mProvider )
{
return 0;
}

if( !data )
{
return mProvider->noDataValue();
}

switch ( type )
{
case QgsRasterDataProvider::Byte:
return ( double )(( GByte * )data )[index];
break;
case QgsRasterDataProvider::UInt16:
return ( double )(( GUInt16 * )data )[index];
break;
case QgsRasterDataProvider::Int16:
return ( double )(( GInt16 * )data )[index];
break;
case QgsRasterDataProvider::UInt32:
return ( double )(( GUInt32 * )data )[index];
break;
case QgsRasterDataProvider::Int32:
return ( double )(( GInt32 * )data )[index];
break;
case QgsRasterDataProvider::Float32:
return ( double )(( float * )data )[index];
break;
case QgsRasterDataProvider::Float64:
return ( double )(( double * )data )[index];
break;
default:
//QgsMessageLog::logMessage( tr( "GDAL data type %1 is not supported" ).arg( type ), tr( "Raster" ) );
break;
}

return mProvider->noDataValue();
}

#endif // QGSRASTERRENDERER_H

0 comments on commit 79b8a76

Please sign in to comment.