Skip to content
Permalink
Browse files

[feature] Transparency support for raster color ramps (fix #7847)

  • Loading branch information
nyalldawson committed May 19, 2013
1 parent cf928a5 commit ff41a1d186226962e1d8139166f0c727d03b4032
@@ -58,10 +58,10 @@ class QgsColorRampShader : QgsRasterShaderFunction
void setMaximumColorCacheSize( int theSize );

/** \brief Generates and new RGB value based on one input value */
bool shade( double, int* /Out/, int* /Out/, int* /Out/ );
bool shade( double, int* /Out/, int* /Out/, int* /Out/, int* /Out/ );

/** \brief Generates and new RGB value based on original RGB value */
bool shade( double, double, double, int* /Out/, int* /Out/, int* /Out/ );
bool shade( double, double, double, double, int* /Out/, int* /Out/, int* /Out/, int* /Out/);

void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const;
};
@@ -29,10 +29,10 @@ class QgsRasterShader
*
*/
/** \brief generates and new RGB value based on one input value */
bool shade(double, int* /Out/, int* /Out/, int* /Out/);
bool shade(double, int* /Out/, int* /Out/, int* /Out/, int* /Out/);

/** \brief generates and new RGB value based on original RGB value */
bool shade(double, double, double, int* /Out/, int* /Out/, int* /Out/);
bool shade(double, double, double, double, int* /Out/, int* /Out/, int* /Out/, int* /Out/);

/** \brief A public method that allows the user to set their own shader function
\note Raster shader takes ownership of the shader function instance */
@@ -27,10 +27,10 @@ class QgsRasterShaderFunction
virtual void setMinimumValue( double );

/** \brief generates and new RGB value based on one input value */
virtual bool shade( double, int* /Out/, int* /Out/, int* /Out/ );
virtual bool shade( double, int* /Out/, int* /Out/, int* /Out/, int* /Out/ );

/** \brief generates and new RGB value based on original RGB value */
virtual bool shade( double, double, double, int* /Out/, int* /Out/, int* /Out/ );
virtual bool shade( double, double, double, double, int* /Out/, int* /Out/, int* /Out/, int* /Out/ );

double minimumMaximumRange() const;

@@ -49,7 +49,7 @@ QString QgsColorRampShader::colorRampTypeAsQString()
return QString( "Unknown" );
}

bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
{
int myColorRampItemCount = mColorRampItemList.count();
if ( myColorRampItemCount <= 0 )
@@ -75,6 +75,7 @@ bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue,
*theReturnRedValue = myColorRampItem.color.red();
*theReturnGreenValue = myColorRampItem.color.green();
*theReturnBlueValue = myColorRampItem.color.blue();
*theReturnAlphaValue = myColorRampItem.color.alpha();
//Cache the shaded value
if ( mMaximumColorCacheSize >= mColorCache.size() )
{
@@ -92,7 +93,7 @@ bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue,
return false; // value not found
}

bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlphaValue )
{
int myColorRampItemCount = mColorRampItemList.count();
if ( myColorRampItemCount <= 0 )
@@ -112,6 +113,7 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
*theReturnRedValue = myColorRampItem.color.red();
*theReturnGreenValue = myColorRampItem.color.green();
*theReturnBlueValue = myColorRampItem.color.blue();
*theReturnAlphaValue = myColorRampItem.color.alpha();
//Cache the shaded value
if ( mMaximumColorCacheSize >= mColorCache.size() )
{
@@ -142,7 +144,7 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
}

bool QgsColorRampShader::interpolatedColor( double theValue, int*
theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int* theReturnAlphaValue )
{
int myColorRampItemCount = mColorRampItemList.count();
if ( myColorRampItemCount <= 0 )
@@ -174,9 +176,10 @@ bool QgsColorRampShader::interpolatedColor( double theValue, int*
*theReturnRedValue = ( int )(( double ) myPreviousColorRampItem.color.red() + (( double )( myColorRampItem.color.red() - myPreviousColorRampItem.color.red() ) * scale ) ) ;
*theReturnGreenValue = ( int )(( double ) myPreviousColorRampItem.color.green() + (( double )( myColorRampItem.color.green() - myPreviousColorRampItem.color.green() ) * scale ) );
*theReturnBlueValue = ( int )(( double ) myPreviousColorRampItem.color.blue() + (( double )( myColorRampItem.color.blue() - myPreviousColorRampItem.color.blue() ) * scale ) );
*theReturnAlphaValue = ( int )(( double ) myPreviousColorRampItem.color.alpha() + (( double )( myColorRampItem.color.alpha() - myPreviousColorRampItem.color.alpha() ) * scale ) );
if ( mMaximumColorCacheSize >= mColorCache.size() )
{
QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue );
QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
mColorCache.insert( theValue, myNewColor );
}
return true;
@@ -192,9 +195,10 @@ bool QgsColorRampShader::interpolatedColor( double theValue, int*
*theReturnRedValue = myColorRampItem.color.red();
*theReturnGreenValue = myColorRampItem.color.green();
*theReturnBlueValue = myColorRampItem.color.blue();
*theReturnAlphaValue = myColorRampItem.color.alpha();
if ( mMaximumColorCacheSize >= mColorCache.size() )
{
QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue );
QColor myNewColor( *theReturnRedValue, *theReturnGreenValue, *theReturnBlueValue, *theReturnAlphaValue );
mColorCache.insert( theValue, myNewColor );
}
return true;
@@ -245,7 +249,7 @@ void QgsColorRampShader::setColorRampType( QString theType )
}
}

bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlphaValue )
{

//Get the shaded value from the cache if it exists already
@@ -255,6 +259,7 @@ bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* th
*theReturnRedValue = myColor.red();
*theReturnGreenValue = myColor.green();
*theReturnBlueValue = myColor.blue();
*theReturnAlphaValue = myColor.alpha();
return true;
}

@@ -272,27 +277,29 @@ bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* th

if ( QgsColorRampShader::EXACT == mColorRampType )
{
return exactColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
return exactColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
}
else if ( QgsColorRampShader::INTERPOLATED == mColorRampType )
{
return interpolatedColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
return interpolatedColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
}

return discreteColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
return discreteColor( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
}

bool QgsColorRampShader::shade( double theRedValue, double theGreenValue,
double theBlueValue, int* theReturnRedValue, int* theReturnGreenValue, int*
theReturnBlueValue )
double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int*
theReturnBlueValue , int* theReturnAlphaValue )
{
Q_UNUSED( theRedValue );
Q_UNUSED( theGreenValue );
Q_UNUSED( theBlueValue );
Q_UNUSED( theAlphaValue );

*theReturnRedValue = 0;
*theReturnGreenValue = 0;
*theReturnBlueValue = 0;
*theReturnAlphaValue = 0;

return false;
}
@@ -86,10 +86,10 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
void setMaximumColorCacheSize( int theSize ) { mMaximumColorCacheSize = theSize; }

/** \brief Generates and new RGB value based on one input value */
bool shade( double, int*, int*, int* );
bool shade( double, int*, int*, int*, int* );

/** \brief Generates and new RGB value based on original RGB value */
bool shade( double, double, double, int*, int*, int* );
bool shade( double, double, double, double, int*, int*, int*, int* );

void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const;

@@ -121,17 +121,17 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
/** Gets the color for a pixel value from the classification vector
* mValueClassification. Assigns the color of the lower class for every
* pixel between two class breaks.*/
bool discreteColor( double, int*, int*, int* );
bool discreteColor( double, int*, int*, int*, int* );

/** Gets the color for a pixel value from the classification vector
* mValueClassification. Assigns the color of the exact matching value in
* the color ramp item list */
bool exactColor( double, int*, int*, int* );
bool exactColor( double, int*, int*, int*, int* );

/** Gets the color for a pixel value from the classification vector
* mValueClassification. Interpolates the color between two class breaks
* linearly.*/
bool interpolatedColor( double, int*, int*, int* );
bool interpolatedColor( double, int*, int*, int*, int* );

/** Do not render values out of range */
bool mClip;
@@ -732,9 +732,9 @@ QPixmap QgsRasterLayer::paletteAsPixmap( int theBandNumber )
for ( int myCol = 0; myCol < mySize; myCol++ )
{
myValue = myStep * ( double )( myCol + myRow * mySize );
int c1, c2, c3;
myShader.shade( myValue, &c1, &c2, &c3 );
myLineBuffer[ myCol ] = qRgb( c1, c2, c3 );
int c1, c2, c3, c4;
myShader.shade( myValue, &c1, &c2, &c3, &c4 );
myLineBuffer[ myCol ] = qRgba( c1, c2, c3, c4 );
}
}

@@ -45,11 +45,11 @@ QgsRasterShader::~QgsRasterShader()
@param theReturnBlueValue The blue component of the new RGB value
@return True if the return values are valid otherwise false
*/
bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlpha )
{
if ( 0 != mRasterShaderFunction )
{
return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlpha );
}

return false;
@@ -66,11 +66,11 @@ bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theRe
@param theReturnBlueValue The blue component of the new RGB value
@return True if the return values are valid otherwise false
*/
bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
{
if ( 0 != mRasterShaderFunction )
{
return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue );
return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theAlphaValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
}

return false;
@@ -150,6 +150,7 @@ void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
itemElem.setAttribute( "label", itemIt->label );
itemElem.setAttribute( "value", QString::number( itemIt->value ) );
itemElem.setAttribute( "color", itemIt->color.name() );
itemElem.setAttribute( "alpha", itemIt->color.alpha() );
colorRampShaderElem.appendChild( itemElem );
}
rasterShaderElem.appendChild( colorRampShaderElem );
@@ -180,6 +181,8 @@ void QgsRasterShader::readXML( const QDomElement& elem )
itemValue = itemElem.attribute( "value" ).toDouble();
itemLabel = itemElem.attribute( "label" );
itemColor.setNamedColor( itemElem.attribute( "color" ) );
itemColor.setAlpha( itemElem.attribute( "alpha", "255" ).toInt() );

itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
}
colorRampShader->setColorRampItemList( itemList );
@@ -54,11 +54,11 @@ class CORE_EXPORT QgsRasterShader
* Non-Static methods
*
*/
/** \brief generates and new RGB value based on one input value */
bool shade( double, int*, int*, int* );
/** \brief generates and new RGBA value based on one input value */
bool shade( double, int*, int*, int* , int* );

/** \brief generates and new RGB value based on original RGB value */
bool shade( double, double, double, int*, int*, int* );
/** \brief generates and new RGBA value based on original RGBA value */
bool shade( double, double, double, double, int*, int*, int* , int* );

/** \brief A public method that allows the user to set their own shader function
\note Raster shader takes ownership of the shader function instance */
@@ -63,13 +63,14 @@ void QgsRasterShaderFunction::setMinimumValue( double theValue )
@param theReturnBlueValue The blue component of the new RGB value
@return True if the return values are valid otherwise false
*/
bool QgsRasterShaderFunction::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsRasterShaderFunction::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
{
Q_UNUSED( theValue );

*theReturnRedValue = 0;
*theReturnGreenValue = 0;
*theReturnBlueValue = 0;
*theReturnAlphaValue = 0;

return false;
}
@@ -86,15 +87,17 @@ bool QgsRasterShaderFunction::shade( double theValue, int* theReturnRedValue, in
@param theReturnBlueValue The blue component of the new RGB value
@return True if the return values are valid otherwise false
*/
bool QgsRasterShaderFunction::shade( double theRedValue, double theGreenValue, double theBlueValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue )
bool QgsRasterShaderFunction::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlphaValue )
{
Q_UNUSED( theRedValue );
Q_UNUSED( theGreenValue );
Q_UNUSED( theBlueValue );
Q_UNUSED( theAlphaValue );

*theReturnRedValue = 0;
*theReturnGreenValue = 0;
*theReturnBlueValue = 0;
*theReturnAlphaValue = 0;

return false;
}
@@ -40,11 +40,11 @@ class CORE_EXPORT QgsRasterShaderFunction
/** \brief Return the minimum value */
virtual void setMinimumValue( double );

/** \brief generates and new RGB value based on one input value */
virtual bool shade( double, int*, int*, int* );
/** \brief generates and new RGBA value based on one input value */
virtual bool shade( double, int*, int*, int*, int* );

/** \brief generates and new RGB value based on original RGB value */
virtual bool shade( double, double, double, int*, int*, int* );
/** \brief generates and new RGBA value based on original RGBA value */
virtual bool shade( double, double, double, double, int*, int*, int*, int* );

double minimumMaximumRange() const { return mMinimumMaximumRange; }

@@ -156,16 +156,24 @@ QgsRasterBlock* QgsSingleBandPseudoColorRenderer::block( int bandNo, QgsRectangl
continue;
}
double val = inputBlock->value( i );
int red, green, blue;
if ( !mShader->shade( val, &red, &green, &blue ) )
int red, green, blue, alpha;
if ( !mShader->shade( val, &red, &green, &blue, &alpha ) )
{
outputBlock->setColor( i, myDefaultColor );
continue;
}

if ( alpha < 255 )
{
// Working with premultiplied colors, so multiply values by alpha
red *= ( alpha / 255.0 );
blue *= ( alpha / 255.0 );
green *= ( alpha / 255.0 );
}

if ( !hasTransparency )
{
outputBlock->setColor( i, qRgba( red, green, blue, 255 ) );
outputBlock->setColor( i, qRgba( red, green, blue, alpha ) );
}
else
{
@@ -180,7 +188,7 @@ QgsRasterBlock* QgsSingleBandPseudoColorRenderer::block( int bandNo, QgsRectangl
currentOpacity *= alphaBlock->value( i ) / 255.0;
}

outputBlock->setColor( i, qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * 255 ) );
outputBlock->setColor( i, qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha ) );
}
}

0 comments on commit ff41a1d

Please sign in to comment.
You can’t perform that action at this time.