29 #include <QDomDocument>
30 #include <QDomElement>
33 : mMinimumValue( minimumValuePossible( dataType ) )
34 , mMaximumValue( maximumValuePossible( dataType ) )
35 , mRasterDataType( dataType )
36 , mRasterDataTypeRange( mMaximumValue - mMinimumValue )
37 , mLookupTableOffset( mMinimumValue * -1 )
42 if ( mRasterDataTypeRange <= 65535.0 )
44 mLookupTable =
new int[
static_cast <int>( mRasterDataTypeRange + 1 )];
49 : mEnhancementDirty( true )
50 , mMinimumValue( ce.mMinimumValue )
51 , mMaximumValue( ce.mMaximumValue )
52 , mRasterDataType( ce.mRasterDataType )
53 , mRasterDataTypeRange( ce.mRasterDataTypeRange )
61 if ( mRasterDataTypeRange <= 65535.0 )
63 mLookupTable =
new int[
static_cast <int>( mRasterDataTypeRange + 1 )];
69 delete [] mLookupTable;
74 if ( mEnhancementDirty )
76 generateLookupTable();
79 if ( mLookupTable &&
NoEnhancement != mContrastEnhancementAlgorithm )
81 double shiftedValue = value + mLookupTableOffset;
82 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
83 return mLookupTable[
static_cast <int>( shiftedValue )];
91 return mContrastEnhancementFunction->enhance( value );
95 bool QgsContrastEnhancement::generateLookupTable()
97 mEnhancementDirty =
false;
99 if ( !mContrastEnhancementFunction )
109 QgsDebugMsgLevel( QStringLiteral(
"***MinimumValue : %1" ).arg( mMinimumValue ), 4 );
110 QgsDebugMsgLevel( QStringLiteral(
"***MaximumValue : %1" ).arg( mMaximumValue ), 4 );
111 QgsDebugMsgLevel( QStringLiteral(
"***mLookupTableOffset : %1" ).arg( mLookupTableOffset ), 4 );
112 QgsDebugMsgLevel( QStringLiteral(
"***mRasterDataTypeRange : %1" ).arg( mRasterDataTypeRange ), 4 );
114 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
116 mLookupTable[myIterator] = mContrastEnhancementFunction->enhance(
static_cast< double >( myIterator ) - mLookupTableOffset );
124 if ( mContrastEnhancementFunction )
126 return mContrastEnhancementFunction->isValueInDisplayableRange( value );
153 mEnhancementDirty =
true;
154 mContrastEnhancementAlgorithm =
algorithm;
158 generateLookupTable();
168 mContrastEnhancementFunction.reset(
function );
170 generateLookupTable();
176 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
184 mMaximumValue = value;
187 if ( mContrastEnhancementFunction )
189 mContrastEnhancementFunction->setMaximumValue( value );
192 mEnhancementDirty =
true;
196 generateLookupTable();
202 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
210 mMinimumValue = value;
213 if ( mContrastEnhancementFunction )
215 mContrastEnhancementFunction->setMinimumValue( value );
218 mEnhancementDirty =
true;
222 generateLookupTable();
229 QDomElement minElem = doc.createElement( QStringLiteral(
"minValue" ) );
231 minElem.appendChild( minText );
232 parentElem.appendChild( minElem );
235 QDomElement maxElem = doc.createElement( QStringLiteral(
"maxValue" ) );
237 maxElem.appendChild( maxText );
238 parentElem.appendChild( maxElem );
241 QDomElement algorithmElem = doc.createElement( QStringLiteral(
"algorithm" ) );
243 algorithmElem.appendChild( algorithmText );
244 parentElem.appendChild( algorithmElem );
249 QDomElement minValueElem = elem.firstChildElement( QStringLiteral(
"minValue" ) );
250 if ( !minValueElem.isNull() )
252 mMinimumValue = minValueElem.text().toDouble();
254 QDomElement maxValueElem = elem.firstChildElement( QStringLiteral(
"maxValue" ) );
255 if ( !maxValueElem.isNull() )
257 mMaximumValue = maxValueElem.text().toDouble();
259 QDomElement algorithmElem = elem.firstChildElement( QStringLiteral(
"algorithm" ) );
260 if ( !algorithmElem.isNull() )
262 QString algorithmString = algorithmElem.text();
265 if ( algorithmString == QLatin1String(
"0" ) )
269 else if ( algorithmString == QLatin1String(
"1" ) )
273 else if ( algorithmString == QLatin1String(
"2" ) )
277 else if ( algorithmString == QLatin1String(
"3" ) )
281 else if ( algorithmString == QLatin1String(
"4" ) )
296 if ( doc.isNull() || element.isNull() )
303 algName = QStringLiteral(
"StretchToMinimumMaximum" );
308 algName = QStringLiteral(
"ClipToMinimumMaximum" );
311 algName = QStringLiteral(
"ClipToMinimumMaximum" );
317 QgsDebugMsg( QObject::tr(
"No SLD1.0 conversion yet for stretch algorithm %1" ).arg( algName ) );
324 QDomElement normalizeElem = doc.createElement( QStringLiteral(
"sld:Normalize" ) );
325 element.appendChild( normalizeElem );
327 QDomElement vendorOptionAlgorithmElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
328 vendorOptionAlgorithmElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"algorithm" ) );
329 vendorOptionAlgorithmElem.appendChild( doc.createTextNode( algName ) );
330 normalizeElem.appendChild( vendorOptionAlgorithmElem );
332 QDomElement vendorOptionMinValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
333 vendorOptionMinValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"minValue" ) );
334 vendorOptionMinValueElem.appendChild( doc.createTextNode( QString::number(
minimumValue() ) ) );
335 normalizeElem.appendChild( vendorOptionMinValueElem );
337 QDomElement vendorOptionMaxValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
338 vendorOptionMaxValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"maxValue" ) );
339 vendorOptionMaxValueElem.appendChild( doc.createTextNode( QString::number(
maximumValue() ) ) );
340 normalizeElem.appendChild( vendorOptionMaxValueElem );
348 return QStringLiteral(
"NoEnhancement" );
350 return QStringLiteral(
"StretchToMinimumMaximum" );
352 return QStringLiteral(
"StretchAndClipToMinimumMaximum" );
354 return QStringLiteral(
"ClipToMinimumMaximum" );
356 return QStringLiteral(
"UserDefinedEnhancement" );
358 return QStringLiteral(
"NoEnhancement" );
363 if ( contrastEnhancementString == QLatin1String(
"StretchToMinimumMaximum" ) )
367 else if ( contrastEnhancementString == QLatin1String(
"StretchAndClipToMinimumMaximum" ) )
371 else if ( contrastEnhancementString == QLatin1String(
"ClipToMinimumMaximum" ) )
375 else if ( contrastEnhancementString == QLatin1String(
"UserDefinedEnhancement" ) )
DataType
Raster data types.
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
A raster contrast enhancement that will clip a value to the specified min/max range.
A contrast enhancement function is the base class for all raster contrast enhancements.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
@ StretchToMinimumMaximum
Linear histogram.
@ StretchAndClipToMinimumMaximum
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
QgsContrastEnhancement(Qgis::DataType datatype=Qgis::DataType::Byte)
Constructor for QgsContrastEnhancement, for the specified data type.
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
static QString contrastEnhancementAlgorithmString(ContrastEnhancementAlgorithm algorithm)
Returns a string to serialize ContrastEnhancementAlgorithm.
static ContrastEnhancementAlgorithm contrastEnhancementAlgorithmFromString(const QString &contrastEnhancementString)
Deserialize ContrastEnhancementAlgorithm.
static double maximumValuePossible(Qgis::DataType dataType)
Helper function that returns the maximum possible value for a data type.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
bool isValueInDisplayableRange(double value)
Returns true if a pixel value is in displayable range, false if pixel is outside of range (i....
int enhanceContrast(double value)
Applies the contrast enhancement to a value.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
void readXml(const QDomElement &elem)
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
static double minimumValuePossible(Qgis::DataType dataType)
Helper function that returns the minimum possible value for a data type.
void setContrastEnhancementFunction(QgsContrastEnhancementFunction *function)
Allows the user to set their own custom contrast enhancement function.
void toSld(QDomDocument &doc, QDomElement &element) const
Write ContrastEnhancement tags following SLD v1.0 specs SLD1.0 is limited to the parameters listed in...
~QgsContrastEnhancement()
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
A linear enhanceContrast enhancement that first clips to min max and then enhanceContrastes linearly ...
A color enhancement function that performs a linear enhanceContrast between min and max.
static QString printValue(double value)
Print double value with all necessary significant digits.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define QgsDebugMsgLevel(str, level)