29 #include <QDomDocument> 30 #include <QDomElement> 33 : mRasterDataType( dataType )
37 mRasterDataTypeRange = mMaximumValue - mMinimumValue;
39 mLookupTableOffset = mMinimumValue * -1;
44 if ( mRasterDataTypeRange <= 65535.0 )
46 mLookupTable =
new int[static_cast <
int>( mRasterDataTypeRange + 1 )];
52 : mEnhancementDirty( true )
53 , mMinimumValue( ce.mMinimumValue )
54 , mMaximumValue( ce.mMaximumValue )
55 , mRasterDataType( ce.mRasterDataType )
56 , mRasterDataTypeRange( ce.mRasterDataTypeRange )
64 if ( mRasterDataTypeRange <= 65535.0 )
66 mLookupTable =
new int[static_cast <
int>( mRasterDataTypeRange + 1 )];
72 delete [] mLookupTable;
85 return std::numeric_limits<unsigned char>::max();
87 return std::numeric_limits<unsigned short>::max();
89 return std::numeric_limits<short>::max();
91 return std::numeric_limits<unsigned int>::max();
93 return std::numeric_limits<int>::max();
95 return std::numeric_limits<float>::max();
97 return std::numeric_limits<double>::max();
99 return std::numeric_limits<short>::max();
101 return std::numeric_limits<int>::max();
103 return std::numeric_limits<float>::max();
105 return std::numeric_limits<double>::max();
113 return std::numeric_limits<double>::max();
121 return std::numeric_limits<unsigned char>::min();
123 return std::numeric_limits<unsigned short>::min();
125 return std::numeric_limits<short>::min();
127 return std::numeric_limits<unsigned int>::min();
129 return std::numeric_limits<int>::min();
131 return std::numeric_limits<float>::max() * -1.0;
133 return std::numeric_limits<double>::max() * -1.0;
135 return std::numeric_limits<short>::min();
137 return std::numeric_limits<int>::min();
139 return std::numeric_limits<float>::max() * -1.0;
141 return std::numeric_limits<double>::max() * -1.0;
149 return std::numeric_limits<double>::max() * -1.0;
160 if ( mEnhancementDirty )
162 generateLookupTable();
165 if ( mLookupTable &&
NoEnhancement != mContrastEnhancementAlgorithm )
167 double shiftedValue = value + mLookupTableOffset;
168 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
169 return mLookupTable[static_cast <
int>( shiftedValue )];
177 return mContrastEnhancementFunction->enhance( value );
181 bool QgsContrastEnhancement::generateLookupTable()
183 mEnhancementDirty =
false;
185 if ( !mContrastEnhancementFunction )
194 QgsDebugMsg( QStringLiteral(
"building lookup table" ) );
195 QgsDebugMsg(
"***MinimumValue : " + QString::number( mMinimumValue ) );
196 QgsDebugMsg(
"***MaximumValue : " + QString::number( mMaximumValue ) );
197 QgsDebugMsg(
"***mLookupTableOffset : " + QString::number( mLookupTableOffset ) );
198 QgsDebugMsg(
"***mRasterDataTypeRange : " + QString::number( mRasterDataTypeRange ) );
200 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
202 mLookupTable[myIterator] = mContrastEnhancementFunction->enhance( static_cast< double >( myIterator ) - mLookupTableOffset );
210 if ( mContrastEnhancementFunction )
212 return mContrastEnhancementFunction->isValueInDisplayableRange( value );
239 mEnhancementDirty =
true;
240 mContrastEnhancementAlgorithm =
algorithm;
244 generateLookupTable();
254 mContrastEnhancementFunction.reset(
function );
256 generateLookupTable();
262 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
270 mMaximumValue = value;
273 if ( mContrastEnhancementFunction )
275 mContrastEnhancementFunction->setMaximumValue( value );
278 mEnhancementDirty =
true;
282 generateLookupTable();
288 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
296 mMinimumValue = value;
299 if ( mContrastEnhancementFunction )
301 mContrastEnhancementFunction->setMinimumValue( value );
304 mEnhancementDirty =
true;
308 generateLookupTable();
315 QDomElement minElem = doc.createElement( QStringLiteral(
"minValue" ) );
317 minElem.appendChild( minText );
318 parentElem.appendChild( minElem );
321 QDomElement maxElem = doc.createElement( QStringLiteral(
"maxValue" ) );
323 maxElem.appendChild( maxText );
324 parentElem.appendChild( maxElem );
327 QDomElement algorithmElem = doc.createElement( QStringLiteral(
"algorithm" ) );
329 algorithmElem.appendChild( algorithmText );
330 parentElem.appendChild( algorithmElem );
335 QDomElement minValueElem = elem.firstChildElement( QStringLiteral(
"minValue" ) );
336 if ( !minValueElem.isNull() )
338 mMinimumValue = minValueElem.text().toDouble();
340 QDomElement maxValueElem = elem.firstChildElement( QStringLiteral(
"maxValue" ) );
341 if ( !maxValueElem.isNull() )
343 mMaximumValue = maxValueElem.text().toDouble();
345 QDomElement algorithmElem = elem.firstChildElement( QStringLiteral(
"algorithm" ) );
346 if ( !algorithmElem.isNull() )
348 QString algorithmString = algorithmElem.text();
351 if ( algorithmString == QLatin1String(
"0" ) )
355 else if ( algorithmString == QLatin1String(
"1" ) )
359 else if ( algorithmString == QLatin1String(
"2" ) )
363 else if ( algorithmString == QLatin1String(
"3" ) )
367 else if ( algorithmString == QLatin1String(
"4" ) )
382 if ( doc.isNull() || element.isNull() )
389 algName = QStringLiteral(
"StretchToMinimumMaximum" );
394 algName = QStringLiteral(
"ClipToMinimumMaximum" );
397 algName = QStringLiteral(
"ClipToMinimumMaximum" );
403 QgsDebugMsg( QObject::tr(
"No SLD1.0 conversion yet for stretch algorithm %1" ).arg( algName ) );
410 QDomElement normalizeElem = doc.createElement( QStringLiteral(
"sld:Normalize" ) );
411 element.appendChild( normalizeElem );
413 QDomElement vendorOptionAlgorithmElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
414 vendorOptionAlgorithmElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"algorithm" ) );
415 vendorOptionAlgorithmElem.appendChild( doc.createTextNode( algName ) );
416 normalizeElem.appendChild( vendorOptionAlgorithmElem );
418 QDomElement vendorOptionMinValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
419 vendorOptionMinValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"minValue" ) );
420 vendorOptionMinValueElem.appendChild( doc.createTextNode( QString::number(
minimumValue() ) ) );
421 normalizeElem.appendChild( vendorOptionMinValueElem );
423 QDomElement vendorOptionMaxValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
424 vendorOptionMaxValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"maxValue" ) );
425 vendorOptionMaxValueElem.appendChild( doc.createTextNode( QString::number(
maximumValue() ) ) );
426 normalizeElem.appendChild( vendorOptionMaxValueElem );
434 return QStringLiteral(
"NoEnhancement" );
436 return QStringLiteral(
"StretchToMinimumMaximum" );
438 return QStringLiteral(
"StretchAndClipToMinimumMaximum" );
440 return QStringLiteral(
"ClipToMinimumMaximum" );
442 return QStringLiteral(
"UserDefinedEnhancement" );
444 return QStringLiteral(
"NoEnhancement" );
449 if ( contrastEnhancementString == QLatin1String(
"StretchToMinimumMaximum" ) )
453 else if ( contrastEnhancementString == QLatin1String(
"StretchAndClipToMinimumMaximum" ) )
457 else if ( contrastEnhancementString == QLatin1String(
"ClipToMinimumMaximum" ) )
461 else if ( contrastEnhancementString == QLatin1String(
"UserDefinedEnhancement" ) )
~QgsContrastEnhancement()
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
Thirty two bit signed integer (qint32)
static QString printValue(double value)
Print double value with all necessary significant digits.
void setContrastEnhancementFunction(QgsContrastEnhancementFunction *function)
Allows the user to set their own custom contrast enhancement function.
static double minimumValuePossible(Qgis::DataType)
Helper function that returns the minimum possible value for a GDAL data type.
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
Thirty two bit unsigned integer (quint32)
DataType
Raster data types.
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
Thirty two bit floating point (float)
Sixteen bit signed integer (qint16)
void toSld(QDomDocument &doc, QDomElement &element) const
Write ContrastEnhancement tags following SLD v1.0 specs SLD1.0 is limited to the parameters listed in...
Sixty four bit floating point (double)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
#define QgsDebugMsgLevel(str, level)
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
A linear enhanceContrast enhancement that first clips to min max and then enhanceContrastes linearly ...
bool isValueInDisplayableRange(double value)
Returns true if a pixel value is in displayable range, false if pixel is outside of range (i...
static QString contrastEnhancementAlgorithmString(ContrastEnhancementAlgorithm algorithm)
Returns a string to serialize ContrastEnhancementAlgorithm.
void readXml(const QDomElement &elem)
Unknown or unspecified type.
A contrast enhancement function is the base class for all raster contrast enhancements.
Sixteen bit unsigned integer (quint16)
A color enhancement function that performs a linear enhanceContrast between min and max...
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
static ContrastEnhancementAlgorithm contrastEnhancementAlgorithmFromString(const QString &contrastEnhancementString)
Deserialize ContrastEnhancementAlgorithm.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
A raster contrast enhancement that will clip a value to the specified min/max range.
static double maximumValuePossible(Qgis::DataType)
Helper function that returns the maximum possible value for a GDAL data type.
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
QgsContrastEnhancement(Qgis::DataType datatype=Qgis::Byte)
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
Eight bit unsigned integer (quint8)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
int enhanceContrast(double value)
Applies the contrast enhancement to a value.