QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsrastertransparency.cpp
Go to the documentation of this file.
1/* **************************************************************************
2 qgsrastertransparency.cpp - description
3 -------------------
4begin : Mon Nov 30 2007
5copyright : (C) 2007 by Peter J. Ersts
7
8****************************************************************************/
9
10/* **************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#include "qgsrasterinterface.h"
21#include "qgis.h"
22
23#include <QDomDocument>
24#include <QDomElement>
25
26QVector<QgsRasterTransparency::TransparentSingleValuePixel> QgsRasterTransparency::transparentSingleValuePixelList() const
27{
28 return mTransparentSingleValuePixelList;
29}
30
31QVector<QgsRasterTransparency::TransparentThreeValuePixel> QgsRasterTransparency::transparentThreeValuePixelList() const
32{
33 return mTransparentThreeValuePixelList;
34}
35
37{
38 //clear the existing list
39 mTransparentSingleValuePixelList.clear();
40
41 //add the initial value
42 mTransparentSingleValuePixelList.append( TransparentSingleValuePixel( value, value, 0 ) );
43}
44
45void QgsRasterTransparency::initializeTransparentPixelList( double redValue, double greenValue, double blueValue )
46{
47 //clear the existing list
48 mTransparentThreeValuePixelList.clear();
49
50 //add the initial values
51 mTransparentThreeValuePixelList.append( TransparentThreeValuePixel( redValue, greenValue, blueValue, 0 ) );
52}
53
54void QgsRasterTransparency::setTransparentSingleValuePixelList( const QVector<QgsRasterTransparency::TransparentSingleValuePixel> &newList )
55{
56 mTransparentSingleValuePixelList = newList;
57}
58
59void QgsRasterTransparency::setTransparentThreeValuePixelList( const QVector<QgsRasterTransparency::TransparentThreeValuePixel> &newList )
60{
61 mTransparentThreeValuePixelList = newList;
62}
63
64int QgsRasterTransparency::alphaValue( double value, int globalTransparency ) const
65{
66 return static_cast< int >( opacityForValue( value ) * globalTransparency );
67}
68
69double QgsRasterTransparency::opacityForValue( double value ) const
70{
71 //if NaN return 0, transparent
72 if ( std::isnan( value ) )
73 {
74 return 0;
75 }
76
77 //Search through the transparency list looking for a match
78 auto it = std::find_if( mTransparentSingleValuePixelList.constBegin(), mTransparentSingleValuePixelList.constEnd(), [value]( const TransparentSingleValuePixel & p )
79 {
80 return ( value > p.min && value < p.max )
81 || ( p.includeMinimum && qgsDoubleNear( value, p.min ) )
82 || ( p.includeMaximum && qgsDoubleNear( value, p.max ) );
83 } );
84
85 if ( it != mTransparentSingleValuePixelList.constEnd() )
86 {
87 return it->opacity;
88 }
89
90 return 1;
91}
92
93int QgsRasterTransparency::alphaValue( double redValue, double greenValue, double blueValue, int globalTransparency ) const
94{
95 return static_cast< int >( opacityForRgbValues( redValue, greenValue, blueValue ) * globalTransparency );
96}
97
98double QgsRasterTransparency::opacityForRgbValues( double redValue, double greenValue, double blueValue ) const
99{
100 //if NaN return 0, transparent
101 if ( std::isnan( redValue ) || std::isnan( greenValue ) || std::isnan( blueValue ) )
102 {
103 return 0;
104 }
105
106 //Search through the transparency list looking for a match
107 auto it = std::find_if( mTransparentThreeValuePixelList.constBegin(), mTransparentThreeValuePixelList.constEnd(), [redValue, greenValue, blueValue]( const TransparentThreeValuePixel & p )
108 {
109 return qgsDoubleNear( p.red, redValue )
110 && qgsDoubleNear( p.green, greenValue )
111 && qgsDoubleNear( p.blue, blueValue );
112 } );
113
114 if ( it != mTransparentThreeValuePixelList.constEnd() )
115 {
116 return it->opacity;
117 }
118
119 return 1;
120}
121
123{
124 return mTransparentSingleValuePixelList.isEmpty() && mTransparentThreeValuePixelList.isEmpty();
125}
126
127void QgsRasterTransparency::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
128{
129 QDomElement rasterTransparencyElem = doc.createElement( QStringLiteral( "rasterTransparency" ) );
130 if ( !mTransparentSingleValuePixelList.isEmpty() )
131 {
132 QDomElement singleValuePixelListElement = doc.createElement( QStringLiteral( "singleValuePixelList" ) );
133 auto it = mTransparentSingleValuePixelList.constBegin();
134 for ( ; it != mTransparentSingleValuePixelList.constEnd(); ++it )
135 {
136 QDomElement pixelListElement = doc.createElement( QStringLiteral( "pixelListEntry" ) );
137 pixelListElement.setAttribute( QStringLiteral( "min" ), QgsRasterBlock::printValue( it->min ) );
138 pixelListElement.setAttribute( QStringLiteral( "max" ), QgsRasterBlock::printValue( it->max ) );
139 pixelListElement.setAttribute( QStringLiteral( "percentTransparent" ), QString::number( 100.0 * ( 1 - it->opacity ) ) );
140 singleValuePixelListElement.appendChild( pixelListElement );
141 }
142 rasterTransparencyElem.appendChild( singleValuePixelListElement );
143
144 }
145 if ( !mTransparentThreeValuePixelList.isEmpty() )
146 {
147 QDomElement threeValuePixelListElement = doc.createElement( QStringLiteral( "threeValuePixelList" ) );
148 auto it = mTransparentThreeValuePixelList.constBegin();
149 for ( ; it != mTransparentThreeValuePixelList.constEnd(); ++it )
150 {
151 QDomElement pixelListElement = doc.createElement( QStringLiteral( "pixelListEntry" ) );
152 pixelListElement.setAttribute( QStringLiteral( "red" ), QgsRasterBlock::printValue( it->red ) );
153 pixelListElement.setAttribute( QStringLiteral( "green" ), QgsRasterBlock::printValue( it->green ) );
154 pixelListElement.setAttribute( QStringLiteral( "blue" ), QgsRasterBlock::printValue( it->blue ) );
155 pixelListElement.setAttribute( QStringLiteral( "percentTransparent" ), QString::number( 100.0 * ( 1 - it->opacity ) ) );
156 threeValuePixelListElement.appendChild( pixelListElement );
157 }
158 rasterTransparencyElem.appendChild( threeValuePixelListElement );
159 }
160 parentElem.appendChild( rasterTransparencyElem );
161}
162
163void QgsRasterTransparency::readXml( const QDomElement &elem )
164{
165 if ( elem.isNull() )
166 {
167 return;
168 }
169
170 mTransparentSingleValuePixelList.clear();
171 mTransparentThreeValuePixelList.clear();
172 QDomElement currentEntryElem;
173
174 const QDomElement singlePixelListElem = elem.firstChildElement( QStringLiteral( "singleValuePixelList" ) );
175 if ( !singlePixelListElem.isNull() )
176 {
177 const QDomNodeList entryList = singlePixelListElem.elementsByTagName( QStringLiteral( "pixelListEntry" ) );
178 for ( int i = 0; i < entryList.size(); ++i )
179 {
180 currentEntryElem = entryList.at( i ).toElement();
181 double min = 0;
182 double max = 0;
183 const double opacity = 1.0 - currentEntryElem.attribute( QStringLiteral( "percentTransparent" ) ).toDouble() / 100.0;
184 // Backward compoatibility < 1.9 : pixelValue (before ranges)
185 if ( currentEntryElem.hasAttribute( QStringLiteral( "pixelValue" ) ) )
186 {
187 min = max = currentEntryElem.attribute( QStringLiteral( "pixelValue" ) ).toDouble();
188 }
189 else
190 {
191 min = currentEntryElem.attribute( QStringLiteral( "min" ) ).toDouble();
192 max = currentEntryElem.attribute( QStringLiteral( "max" ) ).toDouble();
193 }
194 mTransparentSingleValuePixelList.append( TransparentSingleValuePixel( min, max, opacity ) );
195 }
196 }
197 const QDomElement threeValuePixelListElem = elem.firstChildElement( QStringLiteral( "threeValuePixelList" ) );
198 if ( !threeValuePixelListElem.isNull() )
199 {
200 const QDomNodeList entryList = threeValuePixelListElem.elementsByTagName( QStringLiteral( "pixelListEntry" ) );
201 for ( int i = 0; i < entryList.size(); ++i )
202 {
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;
208 mTransparentThreeValuePixelList.append( TransparentThreeValuePixel( red, green, blue, opacity ) );
209 }
210 }
211}
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.