29 #include <QDomDocument> 30 #include <QDomElement> 33 : mContrastEnhancementAlgorithm( NoEnhancement )
34 , mContrastEnhancementFunction( nullptr )
35 , mEnhancementDirty( false )
36 , mLookupTable( nullptr )
37 , mRasterDataType( theDataType )
41 mRasterDataTypeRange = mMaximumValue - mMinimumValue;
43 mLookupTableOffset = mMinimumValue * -1;
48 if ( mRasterDataTypeRange <= 65535.0 )
50 mLookupTable =
new int[static_cast <
int>( mRasterDataTypeRange+1 )];
56 : mContrastEnhancementFunction( nullptr )
57 , mEnhancementDirty( true )
58 , mLookupTable( nullptr )
59 , mMinimumValue( ce.mMinimumValue )
60 , mMaximumValue( ce.mMaximumValue )
61 , mRasterDataType( ce.mRasterDataType )
62 , mRasterDataTypeRange( ce.mRasterDataTypeRange )
70 if ( mRasterDataTypeRange <= 65535.0 )
72 mLookupTable =
new int[static_cast <
int>( mRasterDataTypeRange+1 )];
78 delete [] mLookupTable;
79 delete mContrastEnhancementFunction;
92 switch ( theDataType )
130 switch ( theDataType )
176 if ( mEnhancementDirty )
178 generateLookupTable();
181 if ( mLookupTable &&
NoEnhancement != mContrastEnhancementAlgorithm )
183 double shiftedValue = theValue + mLookupTableOffset;
184 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
185 return mLookupTable[static_cast <
int>( shiftedValue )];
193 return mContrastEnhancementFunction->
enhance( theValue );
200 bool QgsContrastEnhancement::generateLookupTable()
202 mEnhancementDirty =
false;
204 if ( !mContrastEnhancementFunction )
219 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
221 mLookupTable[myIterator] = mContrastEnhancementFunction->
enhance( static_cast< double >( myIterator ) - mLookupTableOffset );
234 if ( mContrastEnhancementFunction )
250 switch ( theAlgorithm )
253 delete mContrastEnhancementFunction;
257 delete mContrastEnhancementFunction;
261 delete mContrastEnhancementFunction;
268 delete mContrastEnhancementFunction;
273 mEnhancementDirty =
true;
274 mContrastEnhancementAlgorithm = theAlgorithm;
278 generateLookupTable();
293 delete mContrastEnhancementFunction;
294 mContrastEnhancementFunction = theFunction;
296 generateLookupTable();
316 mMaximumValue = theValue;
319 if ( mContrastEnhancementFunction )
324 mEnhancementDirty =
true;
328 generateLookupTable();
348 mMinimumValue = theValue;
351 if ( mContrastEnhancementFunction )
356 mEnhancementDirty =
true;
360 generateLookupTable();
388 if ( !minValueElem.
isNull() )
393 if ( !maxValueElem.
isNull() )
398 if ( !algorithmElem.
isNull() )
403 if ( algorithmString ==
"0" )
407 else if ( algorithmString ==
"1" )
411 else if ( algorithmString ==
"2" )
415 else if ( algorithmString ==
"3" )
419 else if ( algorithmString ==
"4" )
437 return "NoEnhancement";
439 return "StretchToMinimumMaximum";
441 return "StretchAndClipToMinimumMaximum";
443 return "ClipToMinimumMaximum";
445 return "UserDefinedEnhancement";
447 return "NoEnhancement";
452 if ( contrastEnhancementString ==
"StretchToMinimumMaximum" )
456 else if ( contrastEnhancementString ==
"StretchAndClipToMinimumMaximum" )
460 else if ( contrastEnhancementString ==
"ClipToMinimumMaximum" )
464 else if ( contrastEnhancementString ==
"UserDefinedEnhancement" )
Eight bit unsigned integer (quint8)
~QgsContrastEnhancement()
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const
static QString printValue(double value)
Print double value with all necessary significant digits.
Unknown or unspecified type.
void readXML(const QDomElement &elem)
QDomNode appendChild(const QDomNode &newChild)
bool isValueInDisplayableRange(double)
Return true if pixel is in stretable range, false if pixel is outside of range (i.e., clipped)
Thirty two bit floating point (float)
static double maximumValuePossible(QGis::DataType)
Helper function that returns the maximum possible value for a GDAL data type.
double toDouble(bool *ok) const
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
void setContrastEnhancementFunction(QgsContrastEnhancementFunction *)
A public method that allows the user to set their own custom contrast enhancment function.
void setMinimumValue(double)
Mutator for the minimum value.
QString number(int n, int base)
static double minimumValuePossible(QGis::DataType)
Helper function that returns the minimum possible value for a GDAL data type.
Sixteen bit unsigned integer (quint16)
Sixty four bit floating point (double)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
#define QgsDebugMsgLevel(str, level)
Thirty two bit signed integer (qint32)
A linear enhanceContrast enhancement that first clips to min max and then enhanceContrastes linearly ...
void setMinimumValue(double, bool generateTable=true)
Return the minimum value for the contrast enhancement range.
Thirty two bit unsigned integer (quint32)
static QString contrastEnhancementAlgorithmString(ContrastEnhancementAlgorithm algorithm)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
A contrast enhancement funcion is the base class for all raster contrast enhancements.
A color enhancement function that performs a linear enhanceContrast between min and max...
Sixteen bit signed integer (qint16)
QDomText createTextNode(const QString &value)
QgsContrastEnhancement(QGis::DataType theDatatype=QGis::Byte)
static ContrastEnhancementAlgorithm contrastEnhancementAlgorithmFromString(const QString &contrastEnhancementString)
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.
int enhanceContrast(double)
Apply the contrast enhancement to a value.
QDomElement firstChildElement(const QString &tagName) const
DataType
Raster data types.
virtual int enhance(double)
A customizable method that takes in a double and returns a int between 0 and 255. ...
virtual bool isValueInDisplayableRange(double)
A customicable method to indicate if the pixels is displayable.
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
QDomElement createElement(const QString &tagName)
void setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
void setMaximumValue(double)
Mustator for the maximum value.