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;
88 return std::numeric_limits<unsigned char>::max();
90 return std::numeric_limits<unsigned short>::max();
92 return std::numeric_limits<short>::max();
94 return std::numeric_limits<unsigned int>::max();
96 return std::numeric_limits<int>::max();
98 return std::numeric_limits<float>::max();
100 return std::numeric_limits<double>::max();
102 return std::numeric_limits<short>::max();
104 return std::numeric_limits<int>::max();
106 return std::numeric_limits<float>::max();
108 return std::numeric_limits<double>::max();
116 return std::numeric_limits<double>::max();
127 return std::numeric_limits<unsigned char>::min();
129 return std::numeric_limits<unsigned short>::min();
131 return std::numeric_limits<short>::min();
133 return std::numeric_limits<unsigned int>::min();
135 return std::numeric_limits<int>::min();
137 return std::numeric_limits<float>::max() * -1.0;
139 return std::numeric_limits<double>::max() * -1.0;
141 return std::numeric_limits<short>::min();
143 return std::numeric_limits<int>::min();
145 return std::numeric_limits<float>::max() * -1.0;
147 return std::numeric_limits<double>::max() * -1.0;
155 return std::numeric_limits<double>::max() * -1.0;
171 if ( mEnhancementDirty )
173 generateLookupTable();
176 if ( mLookupTable &&
NoEnhancement != mContrastEnhancementAlgorithm )
178 double shiftedValue = value + mLookupTableOffset;
179 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
180 return mLookupTable[static_cast <
int>( shiftedValue )];
188 return mContrastEnhancementFunction->enhance( value );
195 bool QgsContrastEnhancement::generateLookupTable()
197 mEnhancementDirty =
false;
199 if ( !mContrastEnhancementFunction )
209 QgsDebugMsg(
"***MinimumValue : " + QString::number( mMinimumValue ) );
210 QgsDebugMsg(
"***MaximumValue : " + QString::number( mMaximumValue ) );
211 QgsDebugMsg(
"***mLookupTableOffset : " + QString::number( mLookupTableOffset ) );
212 QgsDebugMsg(
"***mRasterDataTypeRange : " + QString::number( mRasterDataTypeRange ) );
214 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
216 mLookupTable[myIterator] = mContrastEnhancementFunction->enhance( static_cast< double >( myIterator ) - mLookupTableOffset );
229 if ( mContrastEnhancementFunction )
231 return mContrastEnhancementFunction->isValueInDisplayableRange( value );
264 mEnhancementDirty =
true;
265 mContrastEnhancementAlgorithm = algorithm;
269 generateLookupTable();
284 mContrastEnhancementFunction.reset(
function );
286 generateLookupTable();
298 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
306 mMaximumValue = value;
309 if ( mContrastEnhancementFunction )
311 mContrastEnhancementFunction->setMaximumValue( value );
314 mEnhancementDirty =
true;
318 generateLookupTable();
330 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
338 mMinimumValue = value;
341 if ( mContrastEnhancementFunction )
343 mContrastEnhancementFunction->setMinimumValue( value );
346 mEnhancementDirty =
true;
350 generateLookupTable();
357 QDomElement minElem = doc.createElement( QStringLiteral(
"minValue" ) );
359 minElem.appendChild( minText );
360 parentElem.appendChild( minElem );
363 QDomElement maxElem = doc.createElement( QStringLiteral(
"maxValue" ) );
365 maxElem.appendChild( maxText );
366 parentElem.appendChild( maxElem );
369 QDomElement algorithmElem = doc.createElement( QStringLiteral(
"algorithm" ) );
371 algorithmElem.appendChild( algorithmText );
372 parentElem.appendChild( algorithmElem );
377 QDomElement minValueElem = elem.firstChildElement( QStringLiteral(
"minValue" ) );
378 if ( !minValueElem.isNull() )
380 mMinimumValue = minValueElem.text().toDouble();
382 QDomElement maxValueElem = elem.firstChildElement( QStringLiteral(
"maxValue" ) );
383 if ( !maxValueElem.isNull() )
385 mMaximumValue = maxValueElem.text().toDouble();
387 QDomElement algorithmElem = elem.firstChildElement( QStringLiteral(
"algorithm" ) );
388 if ( !algorithmElem.isNull() )
390 QString algorithmString = algorithmElem.text();
393 if ( algorithmString == QLatin1String(
"0" ) )
397 else if ( algorithmString == QLatin1String(
"1" ) )
401 else if ( algorithmString == QLatin1String(
"2" ) )
405 else if ( algorithmString == QLatin1String(
"3" ) )
409 else if ( algorithmString == QLatin1String(
"4" ) )
427 return QStringLiteral(
"NoEnhancement" );
429 return QStringLiteral(
"StretchToMinimumMaximum" );
431 return QStringLiteral(
"StretchAndClipToMinimumMaximum" );
433 return QStringLiteral(
"ClipToMinimumMaximum" );
435 return QStringLiteral(
"UserDefinedEnhancement" );
437 return QStringLiteral(
"NoEnhancement" );
442 if ( contrastEnhancementString == QLatin1String(
"StretchToMinimumMaximum" ) )
446 else if ( contrastEnhancementString == QLatin1String(
"StretchAndClipToMinimumMaximum" ) )
450 else if ( contrastEnhancementString == QLatin1String(
"ClipToMinimumMaximum" ) )
454 else if ( contrastEnhancementString == QLatin1String(
"UserDefinedEnhancement" ) )
~QgsContrastEnhancement()
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
Thirty two bit signed integer (qint32)
static QString printValue(double value)
Print double value with all necessary significant digits.
static double minimumValuePossible(Qgis::DataType)
Helper function that returns the minimum possible value for a GDAL data type.
bool isValueInDisplayableRange(double)
Return true if pixel is in stretable range, false if pixel is outside of range (i.e., clipped)
Thirty two bit unsigned integer (quint32)
DataType
Raster data types.
Thirty two bit floating point (float)
Sixteen bit signed integer (qint16)
Sixty four bit floating point (double)
void setContrastEnhancementFunction(QgsContrastEnhancementFunction *)
A public method that allows the user to set their own custom contrast enhancement function...
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
#define QgsDebugMsgLevel(str, level)
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.
static QString contrastEnhancementAlgorithmString(ContrastEnhancementAlgorithm algorithm)
Return 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...
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.
int enhanceContrast(double)
Apply the contrast enhancement to a value. Return values are 0 - 254, -1 means the pixel was clipped ...
static double maximumValuePossible(Qgis::DataType)
Helper function that returns the maximum possible value for a GDAL data type.
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 setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
Eight bit unsigned integer (quint8)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.