Skip to content

Commit b0112f1

Browse files
committedOct 20, 2011
Merge pull request #52 from brushtyler/raster-transparency
[BACKPORT] Handle raster layer's transparency band while rendering (fix #2491)
2 parents 5e45bda + b62daee commit b0112f1

File tree

2 files changed

+220
-25
lines changed

2 files changed

+220
-25
lines changed
 

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
176176
cboGreen->addItem( myRasterBandName );
177177
cboBlue->addItem( myRasterBandName );
178178
cboxColorMapBand->addItem( myRasterBandName );
179+
cboxTransparencyBand->addItem( myRasterBandName );
180+
cboxTransparencyBand->setEnabled( true );
179181
}
180182

181183
cboRed->addItem( TRSTRING_NOT_SET );

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 218 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
822822

823823
drawPalettedSingleBandColor( theQPainter, theRasterViewPort,
824824
theQgsMapToPixel, bandNumber( mGrayBandName ) );
825-
826825
break;
827826
}
828827
// a "Palette" layer drawn in gray scale (using only one of the color components)
@@ -2377,7 +2376,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider,
23772376
mGreenBandName = bandName( 2 );
23782377
}
23792378

2380-
//for the third layer we cant be sure so..
2379+
//for the third band we cant be sure so..
23812380
if (( mDataProvider->bandCount() > 2 ) )
23822381
{
23832382
mBlueBandName = bandName( myQSettings.value( "/Raster/defaultBlueBand", 3 ).toInt() ); // sensible default
@@ -3675,16 +3674,22 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
36753674
return;
36763675
}
36773676

3677+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
3678+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
3679+
36783680
int myRedType = mDataProvider->dataType( myRedBandNo );
36793681
int myGreenType = mDataProvider->dataType( myGreenBandNo );
36803682
int myBlueType = mDataProvider->dataType( myBlueBandNo );
3683+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
36813684

36823685
QRgb* redImageScanLine = 0;
36833686
void* redRasterScanLine = 0;
36843687
QRgb* greenImageScanLine = 0;
36853688
void* greenRasterScanLine = 0;
36863689
QRgb* blueImageScanLine = 0;
36873690
void* blueRasterScanLine = 0;
3691+
QRgb* transparencyImageScanLine = 0;
3692+
void* transparencyRasterScanLine = 0;
36883693

36893694
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
36903695

@@ -3730,6 +3735,7 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
37303735
double myRedValue = 0.0;
37313736
double myGreenValue = 0.0;
37323737
double myBlueValue = 0.0;
3738+
int myTransparencyValue = 0;
37333739

37343740
int myStretchedRedValue = 0;
37353741
int myStretchedGreenValue = 0;
@@ -3748,12 +3754,31 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
37483754
blueImageBuffer.setWritingEnabled( false ); //only draw to redImageBuffer
37493755
blueImageBuffer.reset();
37503756

3757+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
3758+
if ( hasTransparencyBand )
3759+
{
3760+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
3761+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to redImageBuffer
3762+
transparencyImageBuffer->reset();
3763+
}
3764+
37513765
while ( redImageBuffer.nextScanLine( &redImageScanLine, &redRasterScanLine )
37523766
&& greenImageBuffer.nextScanLine( &greenImageScanLine, &greenRasterScanLine )
3753-
&& blueImageBuffer.nextScanLine( &blueImageScanLine, &blueRasterScanLine ) )
3767+
&& blueImageBuffer.nextScanLine( &blueImageScanLine, &blueRasterScanLine )
3768+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
37543769
{
37553770
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
37563771
{
3772+
if ( transparencyImageBuffer )
3773+
{
3774+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
3775+
if ( 0 == myTransparencyValue )
3776+
{
3777+
redImageScanLine[ i ] = myDefaultColor;
3778+
continue;
3779+
}
3780+
}
3781+
37573782
myRedValue = readValue( redRasterScanLine, myRedType, i );
37583783
myGreenValue = readValue( greenRasterScanLine, myGreenType, i );
37593784
myBlueValue = readValue( blueRasterScanLine, myBlueType, i );
@@ -3806,9 +3831,15 @@ void QgsRasterLayer::drawMultiBandColor( QPainter * theQPainter, QgsRasterViewPo
38063831
myStretchedBlueValue = 255 - myStretchedBlueValue;
38073832
}
38083833

3834+
if ( myTransparencyValue )
3835+
myAlphaValue *= myTransparencyValue / 255.0;
3836+
38093837
redImageScanLine[ i ] = qRgba( myStretchedRedValue, myStretchedGreenValue, myStretchedBlueValue, myAlphaValue );
38103838
}
38113839
}
3840+
3841+
if ( transparencyImageBuffer )
3842+
delete transparencyImageBuffer;
38123843
}
38133844

38143845
void QgsRasterLayer::drawMultiBandSingleBandGray( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort,
@@ -3842,30 +3873,56 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
38423873
return;
38433874
}
38443875

3876+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
3877+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
3878+
38453879
if ( NULL == mRasterShader )
38463880
{
38473881
return;
38483882
}
38493883

38503884
int myDataType = mDataProvider->dataType( theBandNo );
3885+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
38513886

38523887
QgsRasterImageBuffer imageBuffer( mDataProvider, theBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
38533888
imageBuffer.reset();
38543889

3890+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
3891+
if ( hasTransparencyBand )
3892+
{
3893+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
3894+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to imageBuffer
3895+
transparencyImageBuffer->reset();
3896+
}
3897+
38553898
QRgb* imageScanLine = 0;
38563899
void* rasterScanLine = 0;
3900+
QRgb* transparencyImageScanLine = 0;
3901+
void* transparencyRasterScanLine = 0;
38573902

38583903
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
38593904
double myPixelValue = 0.0;
38603905
int myRedValue = 0;
38613906
int myGreenValue = 0;
38623907
int myBlueValue = 0;
3908+
int myTransparencyValue = 0;
38633909
int myAlphaValue = 0;
38643910

3865-
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine ) )
3911+
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine )
3912+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
38663913
{
38673914
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
38683915
{
3916+
if ( transparencyImageBuffer )
3917+
{
3918+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
3919+
if ( 0 == myTransparencyValue )
3920+
{
3921+
imageScanLine[ i ] = myDefaultColor;
3922+
continue;
3923+
}
3924+
}
3925+
38693926
myRedValue = 0;
38703927
myGreenValue = 0;
38713928
myBlueValue = 0;
@@ -3891,9 +3948,12 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
38913948
continue;
38923949
}
38933950

3951+
if ( myTransparencyValue )
3952+
myAlphaValue *= myTransparencyValue / 255.0;
3953+
38943954
if ( mInvertColor )
38953955
{
3896-
//Invert flag, flip blue and read
3956+
//Invert flag, flip blue and red
38973957
imageScanLine[ i ] = qRgba( myBlueValue, myGreenValue, myRedValue, myAlphaValue );
38983958
}
38993959
else
@@ -3903,6 +3963,9 @@ void QgsRasterLayer::drawPalettedSingleBandColor( QPainter * theQPainter, QgsRas
39033963
}
39043964
}
39053965
}
3966+
3967+
if ( transparencyImageBuffer )
3968+
delete transparencyImageBuffer;
39063969
}
39073970

39083971
/**
@@ -3922,30 +3985,56 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
39223985
return;
39233986
}
39243987

3988+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
3989+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
3990+
39253991
if ( NULL == mRasterShader )
39263992
{
39273993
return;
39283994
}
39293995

39303996
int myDataType = mDataProvider->dataType( theBandNo );
3997+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
39313998

39323999
QgsRasterImageBuffer imageBuffer( mDataProvider, theBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
39334000
imageBuffer.reset();
39344001

4002+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
4003+
if ( hasTransparencyBand )
4004+
{
4005+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
4006+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to redImageBuffer
4007+
transparencyImageBuffer->reset();
4008+
}
4009+
39354010
QRgb* imageScanLine = 0;
39364011
void* rasterScanLine = 0;
4012+
QRgb* transparencyImageScanLine = 0;
4013+
void* transparencyRasterScanLine = 0;
39374014

39384015
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
39394016
double myPixelValue = 0.0;
39404017
int myRedValue = 0;
39414018
int myGreenValue = 0;
39424019
int myBlueValue = 0;
4020+
int myTransparencyValue = 0;
39434021
int myAlphaValue = 0;
39444022

3945-
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine ) )
4023+
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine )
4024+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
39464025
{
39474026
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
39484027
{
4028+
if ( transparencyImageBuffer )
4029+
{
4030+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
4031+
if ( 0 == myTransparencyValue )
4032+
{
4033+
imageScanLine[ i ] = myDefaultColor;
4034+
continue;
4035+
}
4036+
}
4037+
39494038
myRedValue = 0;
39504039
myGreenValue = 0;
39514040
myBlueValue = 0;
@@ -3971,9 +4060,12 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
39714060
continue;
39724061
}
39734062

4063+
if ( myTransparencyValue )
4064+
myAlphaValue *= myTransparencyValue / 255.0;
4065+
39744066
if ( mInvertColor )
39754067
{
3976-
//Invert flag, flip blue and read
4068+
//Invert flag, flip blue and red
39774069
double myGrayValue = ( 0.3 * ( double )myRedValue ) + ( 0.59 * ( double )myGreenValue ) + ( 0.11 * ( double )myBlueValue );
39784070
imageScanLine[ i ] = qRgba(( int )myGrayValue, ( int )myGrayValue, ( int )myGrayValue, myAlphaValue );
39794071
}
@@ -3985,6 +4077,9 @@ void QgsRasterLayer::drawPalettedSingleBandGray( QPainter * theQPainter, QgsRast
39854077
}
39864078
}
39874079
}
4080+
4081+
if ( transparencyImageBuffer )
4082+
delete transparencyImageBuffer;
39884083
}
39894084

39904085
/**
@@ -4005,14 +4100,33 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
40054100
return;
40064101
}
40074102

4103+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
4104+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
4105+
4106+
if ( NULL == mRasterShader )
4107+
{
4108+
return;
4109+
}
4110+
40084111
QgsRasterBandStats myRasterBandStats = bandStatistics( theBandNo );
40094112
int myDataType = mDataProvider->dataType( theBandNo );
4113+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
40104114

40114115
QgsRasterImageBuffer imageBuffer( mDataProvider, theBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
40124116
imageBuffer.reset();
40134117

4118+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
4119+
if ( hasTransparencyBand )
4120+
{
4121+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
4122+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to imageBuffer
4123+
transparencyImageBuffer->reset();
4124+
}
4125+
40144126
QRgb* imageScanLine = 0;
40154127
void* rasterScanLine = 0;
4128+
QRgb* transparencyImageScanLine = 0;
4129+
void* transparencyRasterScanLine = 0;
40164130

40174131
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
40184132
double myMinimumValue = 0.0;
@@ -4036,15 +4150,24 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
40364150
int myRedValue = 0;
40374151
int myGreenValue = 0;
40384152
int myBlueValue = 0;
4153+
int myTransparencyValue = 0;
40394154
int myAlphaValue = 0;
40404155

4041-
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine ) )
4156+
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine )
4157+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
40424158
{
40434159
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
40444160
{
4045-
myRedValue = 0;
4046-
myGreenValue = 0;
4047-
myBlueValue = 0;
4161+
if ( transparencyImageBuffer )
4162+
{
4163+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
4164+
if ( 0 == myTransparencyValue )
4165+
{
4166+
imageScanLine[ i ] = myDefaultColor;
4167+
continue;
4168+
}
4169+
}
4170+
40484171
myPixelValue = readValue( rasterScanLine, myDataType, i );
40494172

40504173
if ( mValidNoDataValue && ( qAbs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
@@ -4066,9 +4189,12 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
40664189
continue;
40674190
}
40684191

4192+
if ( myTransparencyValue )
4193+
myAlphaValue *= myTransparencyValue / 255.0;
4194+
40694195
if ( mInvertColor )
40704196
{
4071-
//Invert flag, flip blue and read
4197+
//Invert flag, flip blue and red
40724198
imageScanLine[ i ] = qRgba( myBlueValue, myGreenValue, myRedValue, myAlphaValue );
40734199
}
40744200
else
@@ -4078,6 +4204,9 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
40784204
}
40794205
}
40804206
}
4207+
4208+
if ( transparencyImageBuffer )
4209+
delete transparencyImageBuffer;
40814210
}
40824211

40834212
/**
@@ -4098,7 +4227,8 @@ void QgsRasterLayer::drawPalettedMultiBandColor( QPainter * theQPainter, QgsRast
40984227
QgsDebugMsg( "Not supported at this time" );
40994228
}
41004229

4101-
void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort, const QgsMapToPixel* theQgsMapToPixel, int theBandNo )
4230+
void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort,
4231+
const QgsMapToPixel* theQgsMapToPixel, int theBandNo )
41024232
{
41034233
QgsDebugMsg( "layer=" + QString::number( theBandNo ) );
41044234
//Invalid band number, segfault prevention
@@ -4107,17 +4237,33 @@ void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPo
41074237
return;
41084238
}
41094239

4240+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
4241+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
4242+
41104243
int myDataType = mDataProvider->dataType( theBandNo );
41114244
QgsDebugMsg( "myDataType = " + QString::number( myDataType ) );
4245+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
4246+
41124247
QgsRasterImageBuffer imageBuffer( mDataProvider, theBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
41134248
imageBuffer.reset();
41144249

4250+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
4251+
if ( hasTransparencyBand )
4252+
{
4253+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
4254+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to imageBuffer
4255+
transparencyImageBuffer->reset();
4256+
}
4257+
41154258
QRgb* imageScanLine = 0;
41164259
void* rasterScanLine = 0;
4260+
QRgb* transparencyImageScanLine = 0;
4261+
void* transparencyRasterScanLine = 0;
41174262

41184263
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
41194264
double myGrayValue = 0.0;
41204265
int myGrayVal = 0;
4266+
int myTransparencyValue = 0;
41214267
int myAlphaValue = 0;
41224268
QgsContrastEnhancement* myContrastEnhancement = contrastEnhancement( theBandNo );
41234269

@@ -4137,15 +4283,25 @@ void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPo
41374283
mGrayMinimumMaximumEstimated = true;
41384284
setMaximumValue( theBandNo, mDataProvider->maximumValue( theBandNo ) );
41394285
setMinimumValue( theBandNo, mDataProvider->minimumValue( theBandNo ) );
4140-
41414286
}
41424287

41434288
QgsDebugMsg( " -> imageBuffer.nextScanLine" );
4144-
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine ) )
4289+
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine )
4290+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
41454291
{
41464292
//QgsDebugMsg( " rendering line");
41474293
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
41484294
{
4295+
if ( transparencyImageBuffer )
4296+
{
4297+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
4298+
if ( 0 == myTransparencyValue )
4299+
{
4300+
imageScanLine[ i ] = myDefaultColor;
4301+
continue;
4302+
}
4303+
}
4304+
41494305
myGrayValue = readValue( rasterScanLine, myDataType, i );
41504306
//QgsDebugMsg( QString( "i = %1 myGrayValue = %2 ").arg(i).arg( myGrayValue ) );
41514307
//if ( myGrayValue != -2147483647 ) {
@@ -4179,16 +4335,20 @@ void QgsRasterLayer::drawSingleBandGray( QPainter * theQPainter, QgsRasterViewPo
41794335
myGrayVal = 255 - myGrayVal;
41804336
}
41814337

4338+
if ( myTransparencyValue )
4339+
myAlphaValue *= myTransparencyValue / 255.0;
4340+
41824341
//QgsDebugMsg( QString( "i = %1 myGrayValue = %2 myGrayVal = %3 myAlphaValue = %4").arg(i).arg( myGrayValue ).arg(myGrayVal).arg(myAlphaValue) );
41834342
imageScanLine[ i ] = qRgba( myGrayVal, myGrayVal, myGrayVal, myAlphaValue );
41844343
}
41854344
}
4345+
4346+
if ( transparencyImageBuffer )
4347+
delete transparencyImageBuffer;
41864348
} // QgsRasterLayer::drawSingleBandGray
41874349

4188-
void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
4189-
QgsRasterViewPort * theRasterViewPort,
4190-
const QgsMapToPixel* theQgsMapToPixel,
4191-
int theBandNo )
4350+
void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter, QgsRasterViewPort * theRasterViewPort,
4351+
const QgsMapToPixel* theQgsMapToPixel, int theBandNo )
41924352
{
41934353
QgsDebugMsg( "entered." );
41944354
//Invalid band number, segfault prevention
@@ -4197,20 +4357,35 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
41974357
return;
41984358
}
41994359

4360+
int myTransparencyBandNo = bandNumber( mTransparencyBandName );
4361+
bool hasTransparencyBand = 0 < myTransparencyBandNo;
4362+
4363+
if ( NULL == mRasterShader )
4364+
{
4365+
return;
4366+
}
4367+
42004368
QgsRasterBandStats myRasterBandStats = bandStatistics( theBandNo );
42014369
int myDataType = mDataProvider->dataType( theBandNo );
4370+
int myTransparencyType = hasTransparencyBand ? mDataProvider->dataType( myTransparencyBandNo ) : 0;
42024371

42034372
QgsRasterImageBuffer imageBuffer( mDataProvider, theBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
42044373
imageBuffer.reset();
42054374

4375+
QgsRasterImageBuffer *transparencyImageBuffer = 0;
4376+
if ( hasTransparencyBand )
4377+
{
4378+
transparencyImageBuffer = new QgsRasterImageBuffer( mDataProvider, myTransparencyBandNo, theQPainter, theRasterViewPort, theQgsMapToPixel, &mGeoTransform[0] );
4379+
transparencyImageBuffer->setWritingEnabled( false ); //only draw to imageBuffer
4380+
transparencyImageBuffer->reset();
4381+
}
4382+
42064383
QRgb* imageScanLine = 0;
42074384
void* rasterScanLine = 0;
4385+
QRgb* transparencyImageScanLine = 0;
4386+
void* transparencyRasterScanLine = 0;
42084387

42094388
QRgb myDefaultColor = qRgba( 255, 255, 255, 0 );
4210-
if ( NULL == mRasterShader )
4211-
{
4212-
return;
4213-
}
42144389

42154390
double myMinimumValue = 0.0;
42164391
double myMaximumValue = 0.0;
@@ -4232,14 +4407,26 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
42324407
int myRedValue = 255;
42334408
int myGreenValue = 255;
42344409
int myBlueValue = 255;
4410+
int myTransparencyValue = 0;
42354411

42364412
double myPixelValue = 0.0;
42374413
int myAlphaValue = 0;
42384414

4239-
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine ) )
4415+
while ( imageBuffer.nextScanLine( &imageScanLine, &rasterScanLine )
4416+
&& ( !transparencyImageBuffer || transparencyImageBuffer->nextScanLine( &transparencyImageScanLine, &transparencyRasterScanLine ) ) )
42404417
{
42414418
for ( int i = 0; i < theRasterViewPort->drawableAreaXDim; ++i )
42424419
{
4420+
if ( transparencyImageBuffer )
4421+
{
4422+
myTransparencyValue = readValue( transparencyRasterScanLine, myTransparencyType, i );
4423+
if ( 0 == myTransparencyValue )
4424+
{
4425+
imageScanLine[ i ] = myDefaultColor;
4426+
continue;
4427+
}
4428+
}
4429+
42434430
myPixelValue = readValue( rasterScanLine, myDataType, i );
42444431

42454432
if ( mValidNoDataValue && ( qAbs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
@@ -4261,9 +4448,12 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
42614448
continue;
42624449
}
42634450

4451+
if ( myTransparencyValue )
4452+
myAlphaValue *= myTransparencyValue / 255.0;
4453+
42644454
if ( mInvertColor )
42654455
{
4266-
//Invert flag, flip blue and read
4456+
//Invert flag, flip blue and red
42674457
imageScanLine[ i ] = qRgba( myBlueValue, myGreenValue, myRedValue, myAlphaValue );
42684458
}
42694459
else
@@ -4274,6 +4464,9 @@ void QgsRasterLayer::drawSingleBandPseudoColor( QPainter * theQPainter,
42744464
}
42754465
}
42764466
}
4467+
4468+
if ( transparencyImageBuffer )
4469+
delete transparencyImageBuffer;
42774470
}
42784471

42794472
#if 0

0 commit comments

Comments
 (0)
Please sign in to comment.