Skip to content

Commit

Permalink
Hillshaderenderer: Fix transparency effects (#3185)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsgerPetersen authored and NathanW2 committed Jun 8, 2016
1 parent 2265115 commit 85fbeb2
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/core/raster/qgshillshaderenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <QColor>

#include "qgshillshaderenderer.h"

#include "qgsrastertransparency.h"
#include "qgsrasterinterface.h"
#include "qgsrasterblock.h"
#include "qgsrectangle.h"
Expand All @@ -40,6 +40,10 @@ QgsHillshadeRenderer *QgsHillshadeRenderer::clone() const
QgsHillshadeRenderer* r = new QgsHillshadeRenderer( nullptr, mBand, mLightAzimuth, mLightAngle );
r->setZFactor( mZFactor );
r->setMultiDirectional( mMultiDirectional );
// "Effects"
r->setOpacity( mOpacity );
r->setAlphaBand( mAlphaBand );
r->setRasterTransparency( mRasterTransparency ? new QgsRasterTransparency( *mRasterTransparency ) : nullptr );
return r;
}

Expand Down Expand Up @@ -98,12 +102,34 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext
return outputBlock;
}

QgsRasterBlock *alphaBlock = nullptr;

if ( mAlphaBand > 0 && mBand != mAlphaBand )
{

alphaBlock = mInput->block( mAlphaBand, extent, width, height );
if ( !alphaBlock || alphaBlock->isEmpty() )
{
// TODO: better to render without alpha
delete inputBlock;
delete alphaBlock;
return outputBlock;
}
}
else if ( mAlphaBand > 0 )
{
alphaBlock = inputBlock;
}

if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) )
{
delete inputBlock;
delete alphaBlock;
return outputBlock;
}



double cellXSize = extent.width() / double( width );
double cellYSize = extent.height() / double( height );
double zenithRad = qMax( 0.0, 90 - mLightAngle ) * M_PI / 180.0;
Expand Down Expand Up @@ -224,9 +250,33 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext
double color3 = cosSlope + sinSlope * cos( angle3Rad - aspectRad ) ;
grayValue = qBound( 0.0, 255 * ( weight0 * color0 + weight1 * color1 + weight2 * color2 + weight3 * color3 ) * 0.5, 255.0 );
}
outputBlock->setColor( i, j, qRgb( grayValue, grayValue, grayValue ) );

double currentAlpha = mOpacity;
if ( mRasterTransparency )
{
currentAlpha = mRasterTransparency->alphaValue( x22, mOpacity * 255 ) / 255.0;
}
if ( mAlphaBand > 0 )
{
currentAlpha *= alphaBlock->value( i ) / 255.0;
}

if ( qgsDoubleNear( currentAlpha, 1.0 ) )
{
outputBlock->setColor( i, j, qRgba( grayValue, grayValue, grayValue, 255 ) );
}
else
{
outputBlock->setColor( i, j, qRgba( currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * grayValue, currentAlpha * 255 ) );
}
}
}

delete inputBlock;
if ( mAlphaBand > 0 && mBand != mAlphaBand )
{
delete alphaBlock;
}
return outputBlock;
}

Expand Down

2 comments on commit 85fbeb2

@nirvn
Copy link
Contributor

@nirvn nirvn commented on 85fbeb2 Jul 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AsgerPetersen , @NathanW2 , it appears the global transparency is not working anymore.

@AsgerPetersen
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nirvn @NathanW2: It seems like global transparency is only broken in the style dock. Using the layer properties dialog it works ok. Tested in 600ff4f.

I am not sure this is something I can fix.

Please sign in to comment.