-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added raster renderer interface and test implementation for paletted …
…rasters
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |