Skip to content

Commit ea9a2a8

Browse files
committedJan 6, 2012
Single band pseudo color renderer
1 parent 4809f06 commit ea9a2a8

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
 

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ SET(QGIS_CORE_SRCS
171171
raster/qgsmultibandcolorrenderer.cpp
172172
raster/qgssinglebandcolordatarenderer.cpp
173173
raster/qgssinglebandgrayrenderer.cpp
174+
raster/qgssinglebandpseudocolorrenderer.cpp
174175

175176
renderer/qgscontinuouscolorrenderer.cpp
176177
renderer/qgsgraduatedsymbolrenderer.cpp

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ email : tim at linfiniti.com
4242
#include "qgscubicrasterresampler.h"
4343
#include "qgsmultibandcolorrenderer.h"
4444
#include "qgssinglebandcolordatarenderer.h"
45+
#include "qgssinglebandpseudocolorrenderer.h"
4546
#include "qgssinglebandgrayrenderer.h"
4647

4748
#include <cstdio>
@@ -863,8 +864,36 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
863864
}
864865
else
865866
{
867+
//init
868+
int bandNo = bandNumber( mGrayBandName );
869+
QgsRasterBandStats myRasterBandStats = bandStatistics( bandNo );
870+
double myMinimumValue = 0.0;
871+
double myMaximumValue = 0.0;
872+
//Use standard deviations if set, otherwise, use min max of band
873+
if ( mStandardDeviations > 0 )
874+
{
875+
myMinimumValue = ( myRasterBandStats.mean - ( mStandardDeviations * myRasterBandStats.stdDev ) );
876+
myMaximumValue = ( myRasterBandStats.mean + ( mStandardDeviations * myRasterBandStats.stdDev ) );
877+
}
878+
else
879+
{
880+
myMinimumValue = myRasterBandStats.minimumValue;
881+
myMaximumValue = myRasterBandStats.maximumValue;
882+
}
883+
884+
mRasterShader->setMinimumValue( myMinimumValue );
885+
mRasterShader->setMaximumValue( myMaximumValue );
886+
887+
QgsSingleBandPseudoColorRenderer r( mDataProvider, bandNo, mRasterShader, mResampler );
888+
r.setOpacity( mTransparencyLevel / 255.0 );
889+
r.setRasterTransparency( &mRasterTransparency );
890+
r.setInvertColor( mInvertColor );
891+
r.draw( theQPainter, theRasterViewPort, theQgsMapToPixel );
892+
893+
#if 0
866894
drawSingleBandPseudoColor( theQPainter, theRasterViewPort,
867895
theQgsMapToPixel, bandNumber( mGrayBandName ) );
896+
#endif //0
868897
break;
869898
}
870899
// a single band with a color map
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/***************************************************************************
2+
qgssinglebandpseudocolorrenderer.cpp
3+
------------------------------------
4+
begin : January 2012
5+
copyright : (C) 2012 by Marco Hugentobler
6+
email : marco at sourcepole dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgssinglebandpseudocolorrenderer.h"
19+
#include "qgsrastershader.h"
20+
#include "qgsrastertransparency.h"
21+
#include "qgsrasterviewport.h"
22+
#include <QImage>
23+
24+
QgsSingleBandPseudoColorRenderer::QgsSingleBandPseudoColorRenderer( QgsRasterDataProvider* provider, int band, QgsRasterShader* shader, QgsRasterResampler* resampler ):
25+
QgsRasterRenderer( provider, resampler ), mShader( shader ), mBand( band )
26+
{
27+
}
28+
29+
QgsSingleBandPseudoColorRenderer::~QgsSingleBandPseudoColorRenderer()
30+
{
31+
}
32+
33+
void QgsSingleBandPseudoColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
34+
{
35+
if ( !p || !mProvider || !viewPort || !theQgsMapToPixel || !mShader )
36+
{
37+
return;
38+
}
39+
40+
double oversamplingX, oversamplingY;
41+
QgsRasterDataProvider::DataType transparencyType = QgsRasterDataProvider::UnknownDataType;
42+
if ( mAlphaBand > 0 )
43+
{
44+
transparencyType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mAlphaBand );
45+
}
46+
startRasterRead( mBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
47+
//Read alpha band if necessary
48+
if ( mAlphaBand > 0 && mAlphaBand != mBand )
49+
{
50+
startRasterRead( mAlphaBand, viewPort, theQgsMapToPixel, oversamplingX, oversamplingY );
51+
}
52+
53+
int nCols = 0;
54+
int nRows = 0;
55+
int topLeftCol = 0;
56+
int topLeftRow = 0;
57+
void* rasterData;
58+
void* transparencyData;
59+
double currentOpacity = mOpacity;
60+
QgsRasterDataProvider::DataType rasterType = ( QgsRasterDataProvider::DataType )mProvider->dataType( mBand );
61+
int red, green, blue;
62+
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
63+
64+
//rendering is faster without considering user-defined transparency
65+
bool hasTransparency = usesTransparency( viewPort->mSrcCRS, viewPort->mDestCRS );
66+
67+
while ( readNextRasterPart( mBand, viewPort, nCols, nRows, &rasterData, topLeftCol, topLeftRow ) )
68+
{
69+
if ( mAlphaBand > 0 && mAlphaBand != mBand )
70+
{
71+
readNextRasterPart( mAlphaBand, viewPort, nCols, nRows, &transparencyData, topLeftCol, topLeftRow );
72+
}
73+
else if ( mAlphaBand == mBand )
74+
{
75+
transparencyData = rasterData;
76+
}
77+
78+
//create image
79+
QImage img( nCols, nRows, QImage::Format_ARGB32_Premultiplied );
80+
QRgb* imageScanLine = 0;
81+
double val = 0;
82+
83+
int currentRasterPos = 0;
84+
for ( int i = 0; i < nRows; ++i )
85+
{
86+
imageScanLine = ( QRgb* )( img.scanLine( i ) );
87+
for ( int j = 0; j < nCols; ++j )
88+
{
89+
val = readValue( rasterData, rasterType, currentRasterPos );
90+
if ( !mShader->shade( val, &red, &green, &blue ) )
91+
{
92+
imageScanLine[j] = myDefaultColor;
93+
++currentRasterPos;
94+
continue;
95+
}
96+
97+
if ( !hasTransparency )
98+
{
99+
imageScanLine[j] = qRgba( red, green, blue, 255 );
100+
}
101+
else
102+
{
103+
//opacity
104+
currentOpacity = mOpacity;
105+
if ( mRasterTransparency )
106+
{
107+
currentOpacity = mRasterTransparency->alphaValue( val, mOpacity * 255 ) / 255.0;
108+
}
109+
if ( mAlphaBand > 0 )
110+
{
111+
currentOpacity *= ( readValue( transparencyData, transparencyType, currentRasterPos ) / 255.0 );
112+
}
113+
114+
imageScanLine[j] = qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * 255 );
115+
}
116+
++currentRasterPos;
117+
}
118+
}
119+
120+
drawImage( p, viewPort, img, topLeftCol, topLeftRow, nCols, nRows, oversamplingX, oversamplingY );
121+
}
122+
123+
stopRasterRead( mBand );
124+
if ( mAlphaBand > 0 && mAlphaBand != mBand )
125+
{
126+
stopRasterRead( mAlphaBand );
127+
}
128+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************
2+
qgssinglebandpseudocolorrenderer.h
3+
----------------------------------
4+
begin : January 2012
5+
copyright : (C) 2012 by Marco Hugentobler
6+
email : marco at sourcepole dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSSINGLEBANDPSEUDOCOLORRENDERER_H
19+
#define QGSSINGLEBANDPSEUDOCOLORRENDERER_H
20+
21+
#include "qgsrasterrenderer.h"
22+
23+
class QgsRasterShader;
24+
25+
class QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
26+
{
27+
public:
28+
QgsSingleBandPseudoColorRenderer( QgsRasterDataProvider* provider, int band, QgsRasterShader* shader, QgsRasterResampler* resampler = 0 );
29+
~QgsSingleBandPseudoColorRenderer();
30+
31+
virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );
32+
33+
private:
34+
QgsRasterShader* mShader;
35+
int mBand;
36+
};
37+
38+
#endif // QGSSINGLEBANDPSEUDOCOLORRENDERER_H

0 commit comments

Comments
 (0)
Please sign in to comment.