23#include <QDomDocument>
28 return mTransparentSingleValuePixelList;
33 return mTransparentThreeValuePixelList;
39 mTransparentSingleValuePixelList.clear();
48 mTransparentThreeValuePixelList.clear();
56 mTransparentSingleValuePixelList = newList;
61 mTransparentThreeValuePixelList = newList;
66 return static_cast< int >(
opacityForValue( value ) * globalTransparency );
72 if ( std::isnan( value ) )
78 auto it = std::find_if( mTransparentSingleValuePixelList.constBegin(), mTransparentSingleValuePixelList.constEnd(), [value](
const TransparentSingleValuePixel & p )
80 return ( value > p.min && value < p.max )
81 || ( p.includeMinimum && qgsDoubleNear( value, p.min ) )
82 || ( p.includeMaximum && qgsDoubleNear( value, p.max ) );
85 if ( it != mTransparentSingleValuePixelList.constEnd() )
95 return static_cast< int >(
opacityForRgbValues( redValue, greenValue, blueValue ) * globalTransparency );
101 if ( std::isnan( redValue ) || std::isnan( greenValue ) || std::isnan( blueValue ) )
107 auto it = std::find_if( mTransparentThreeValuePixelList.constBegin(), mTransparentThreeValuePixelList.constEnd(), [redValue, greenValue, blueValue](
const TransparentThreeValuePixel & p )
109 return qgsDoubleNear( p.red, redValue )
110 && qgsDoubleNear( p.green, greenValue )
111 && qgsDoubleNear( p.blue, blueValue );
114 if ( it != mTransparentThreeValuePixelList.constEnd() )
124 return mTransparentSingleValuePixelList.isEmpty() && mTransparentThreeValuePixelList.isEmpty();
129 QDomElement rasterTransparencyElem = doc.createElement( QStringLiteral(
"rasterTransparency" ) );
130 if ( !mTransparentSingleValuePixelList.isEmpty() )
132 QDomElement singleValuePixelListElement = doc.createElement( QStringLiteral(
"singleValuePixelList" ) );
133 auto it = mTransparentSingleValuePixelList.constBegin();
134 for ( ; it != mTransparentSingleValuePixelList.constEnd(); ++it )
136 QDomElement pixelListElement = doc.createElement( QStringLiteral(
"pixelListEntry" ) );
139 pixelListElement.setAttribute( QStringLiteral(
"percentTransparent" ), QString::number( 100.0 * ( 1 - it->opacity ) ) );
140 singleValuePixelListElement.appendChild( pixelListElement );
142 rasterTransparencyElem.appendChild( singleValuePixelListElement );
145 if ( !mTransparentThreeValuePixelList.isEmpty() )
147 QDomElement threeValuePixelListElement = doc.createElement( QStringLiteral(
"threeValuePixelList" ) );
148 auto it = mTransparentThreeValuePixelList.constBegin();
149 for ( ; it != mTransparentThreeValuePixelList.constEnd(); ++it )
151 QDomElement pixelListElement = doc.createElement( QStringLiteral(
"pixelListEntry" ) );
155 pixelListElement.setAttribute( QStringLiteral(
"percentTransparent" ), QString::number( 100.0 * ( 1 - it->opacity ) ) );
156 threeValuePixelListElement.appendChild( pixelListElement );
158 rasterTransparencyElem.appendChild( threeValuePixelListElement );
160 parentElem.appendChild( rasterTransparencyElem );
170 mTransparentSingleValuePixelList.clear();
171 mTransparentThreeValuePixelList.clear();
172 QDomElement currentEntryElem;
174 const QDomElement singlePixelListElem = elem.firstChildElement( QStringLiteral(
"singleValuePixelList" ) );
175 if ( !singlePixelListElem.isNull() )
177 const QDomNodeList entryList = singlePixelListElem.elementsByTagName( QStringLiteral(
"pixelListEntry" ) );
178 for (
int i = 0; i < entryList.size(); ++i )
180 currentEntryElem = entryList.at( i ).toElement();
183 const double opacity = 1.0 - currentEntryElem.attribute( QStringLiteral(
"percentTransparent" ) ).toDouble() / 100.0;
185 if ( currentEntryElem.hasAttribute( QStringLiteral(
"pixelValue" ) ) )
187 min = max = currentEntryElem.attribute( QStringLiteral(
"pixelValue" ) ).toDouble();
191 min = currentEntryElem.attribute( QStringLiteral(
"min" ) ).toDouble();
192 max = currentEntryElem.attribute( QStringLiteral(
"max" ) ).toDouble();
197 const QDomElement threeValuePixelListElem = elem.firstChildElement( QStringLiteral(
"threeValuePixelList" ) );
198 if ( !threeValuePixelListElem.isNull() )
200 const QDomNodeList entryList = threeValuePixelListElem.elementsByTagName( QStringLiteral(
"pixelListEntry" ) );
201 for (
int i = 0; i < entryList.size(); ++i )
203 currentEntryElem = entryList.at( i ).toElement();
204 const double red = currentEntryElem.attribute( QStringLiteral(
"red" ) ).toDouble();
205 const double green = currentEntryElem.attribute( QStringLiteral(
"green" ) ).toDouble();
206 const double blue = currentEntryElem.attribute( QStringLiteral(
"blue" ) ).toDouble();
207 const double opacity = 1.0 - currentEntryElem.attribute( QStringLiteral(
"percentTransparent" ) ).toDouble() / 100.0;
static QString printValue(double value)
Print double value with all necessary significant digits.
void setTransparentSingleValuePixelList(const QVector< QgsRasterTransparency::TransparentSingleValuePixel > &newList)
Sets the transparent single value pixel list, replacing the whole existing list.
QVector< QgsRasterTransparency::TransparentSingleValuePixel > transparentSingleValuePixelList() const
Returns the transparent single value pixel list.
Q_DECL_DEPRECATED int alphaValue(double value, int globalTransparency=255) const
Returns the transparency value for a single value pixel.
void initializeTransparentPixelList(double value)
Resets the transparency list to a single value.
double opacityForRgbValues(double redValue, double greenValue, double blueValue) const
Returns the opacity (as a value from 0 to 1) for a set of RGB pixel values.
void setTransparentThreeValuePixelList(const QVector< QgsRasterTransparency::TransparentThreeValuePixel > &newList)
Sets the transparent three value pixel list, replacing the whole existing list.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Writes the transparency information to an XML document.
QVector< QgsRasterTransparency::TransparentThreeValuePixel > transparentThreeValuePixelList() const
Returns the transparent three value pixel list.
bool isEmpty() const
True if there are no entries in the pixel lists except the nodata value.
double opacityForValue(double value) const
Returns the opacity (as a value from 0 to 1) for a single value pixel.
void readXml(const QDomElement &elem)
Reads the transparency information from an XML document.
Defines the transparency for a range of single-band pixel values.
Defines the transparency for a RGB pixel value.