29#include <QDomDocument>
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 const double shiftedValue = value + mLookupTableOffset;
82 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
83 return mLookupTable[
static_cast <int>( shiftedValue )];
91 return mContrastEnhancementFunction->enhance( value );
95bool QgsContrastEnhancement::generateLookupTable()
97 mEnhancementDirty =
false;
99 if ( !mContrastEnhancementFunction )
104 switch ( mRasterDataType )
130 QgsDebugMsgLevel( QStringLiteral(
"***MinimumValue : %1" ).arg( mMinimumValue ), 4 );
131 QgsDebugMsgLevel( QStringLiteral(
"***MaximumValue : %1" ).arg( mMaximumValue ), 4 );
132 QgsDebugMsgLevel( QStringLiteral(
"***mLookupTableOffset : %1" ).arg( mLookupTableOffset ), 4 );
133 QgsDebugMsgLevel( QStringLiteral(
"***mRasterDataTypeRange : %1" ).arg( mRasterDataTypeRange ), 4 );
135 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
137 mLookupTable[myIterator] = mContrastEnhancementFunction->enhance(
static_cast< double >( myIterator ) - mLookupTableOffset );
145 if ( mContrastEnhancementFunction )
147 return mContrastEnhancementFunction->isValueInDisplayableRange( value );
174 mEnhancementDirty =
true;
175 mContrastEnhancementAlgorithm =
algorithm;
179 generateLookupTable();
189 mContrastEnhancementFunction.reset( function );
191 generateLookupTable();
197 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
205 mMaximumValue = value;
208 if ( mContrastEnhancementFunction )
210 mContrastEnhancementFunction->setMaximumValue( value );
213 mEnhancementDirty =
true;
217 generateLookupTable();
223 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
231 mMinimumValue = value;
234 if ( mContrastEnhancementFunction )
236 mContrastEnhancementFunction->setMinimumValue( value );
239 mEnhancementDirty =
true;
243 generateLookupTable();
250 QDomElement minElem = doc.createElement( QStringLiteral(
"minValue" ) );
252 minElem.appendChild( minText );
253 parentElem.appendChild( minElem );
256 QDomElement maxElem = doc.createElement( QStringLiteral(
"maxValue" ) );
258 maxElem.appendChild( maxText );
259 parentElem.appendChild( maxElem );
262 QDomElement algorithmElem = doc.createElement( QStringLiteral(
"algorithm" ) );
264 algorithmElem.appendChild( algorithmText );
265 parentElem.appendChild( algorithmElem );
270 const QDomElement minValueElem = elem.firstChildElement( QStringLiteral(
"minValue" ) );
271 if ( !minValueElem.isNull() )
273 mMinimumValue = minValueElem.text().toDouble();
275 const QDomElement maxValueElem = elem.firstChildElement( QStringLiteral(
"maxValue" ) );
276 if ( !maxValueElem.isNull() )
278 mMaximumValue = maxValueElem.text().toDouble();
280 const QDomElement algorithmElem = elem.firstChildElement( QStringLiteral(
"algorithm" ) );
281 if ( !algorithmElem.isNull() )
283 const QString algorithmString = algorithmElem.text();
286 if ( algorithmString == QLatin1String(
"0" ) )
290 else if ( algorithmString == QLatin1String(
"1" ) )
294 else if ( algorithmString == QLatin1String(
"2" ) )
298 else if ( algorithmString == QLatin1String(
"3" ) )
302 else if ( algorithmString == QLatin1String(
"4" ) )
317 if ( doc.isNull() || element.isNull() )
324 algName = QStringLiteral(
"StretchToMinimumMaximum" );
329 algName = QStringLiteral(
"ClipToMinimumMaximum" );
332 algName = QStringLiteral(
"ClipToMinimumMaximum" );
338 QgsDebugError( QStringLiteral(
"No SLD1.0 conversion yet for stretch algorithm %1" ).arg( algName ) );
345 QDomElement normalizeElem = doc.createElement( QStringLiteral(
"sld:Normalize" ) );
346 element.appendChild( normalizeElem );
348 QDomElement vendorOptionAlgorithmElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
349 vendorOptionAlgorithmElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"algorithm" ) );
350 vendorOptionAlgorithmElem.appendChild( doc.createTextNode( algName ) );
351 normalizeElem.appendChild( vendorOptionAlgorithmElem );
353 QDomElement vendorOptionMinValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
354 vendorOptionMinValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"minValue" ) );
355 vendorOptionMinValueElem.appendChild( doc.createTextNode( QString::number(
minimumValue() ) ) );
356 normalizeElem.appendChild( vendorOptionMinValueElem );
358 QDomElement vendorOptionMaxValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
359 vendorOptionMaxValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"maxValue" ) );
360 vendorOptionMaxValueElem.appendChild( doc.createTextNode( QString::number(
maximumValue() ) ) );
361 normalizeElem.appendChild( vendorOptionMaxValueElem );
369 return QStringLiteral(
"NoEnhancement" );
371 return QStringLiteral(
"StretchToMinimumMaximum" );
373 return QStringLiteral(
"StretchAndClipToMinimumMaximum" );
375 return QStringLiteral(
"ClipToMinimumMaximum" );
377 return QStringLiteral(
"UserDefinedEnhancement" );
379 return QStringLiteral(
"NoEnhancement" );
384 if ( contrastEnhancementString == QLatin1String(
"StretchToMinimumMaximum" ) )
388 else if ( contrastEnhancementString == QLatin1String(
"StretchAndClipToMinimumMaximum" ) )
392 else if ( contrastEnhancementString == QLatin1String(
"ClipToMinimumMaximum" ) )
396 else if ( contrastEnhancementString == QLatin1String(
"UserDefinedEnhancement" ) )
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float)
@ CFloat64
Complex Float64.
@ Int16
Sixteen bit signed integer (qint16)
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
@ Int32
Thirty two bit signed integer (qint32)
@ Float64
Sixty four bit floating point (double)
@ CFloat32
Complex Float32.
@ UInt32
Thirty two bit unsigned integer (quint32)
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, bool localized=false)
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)
#define QgsDebugError(str)