QGIS API Documentation  2.17.0-Master (973e4b0)
qgscontrastenhancement.cpp
Go to the documentation of this file.
1 /* **************************************************************************
2  qgscontrastenhancement.cpp - description
3  -------------------
4 begin : Mon Oct 22 2007
5 copyright : (C) 2007 by Peter J. Ersts
6 email : ersts@amnh.org
7 
8 This class contains code that was originally part of the larger QgsRasterLayer
9 class originally created circa 2004 by T.Sutton, Gary E.Sherman, Steve Halasz
10 ****************************************************************************/
11 
12 /* **************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #include "qgslogger.h"
22 
23 #include "qgscontrastenhancement.h"
28 #include "qgsrasterblock.h"
29 #include <QDomDocument>
30 #include <QDomElement>
31 
33  : mContrastEnhancementAlgorithm( NoEnhancement )
34  , mContrastEnhancementFunction( nullptr )
35  , mEnhancementDirty( false )
36  , mLookupTable( nullptr )
37  , mRasterDataType( theDataType )
38 {
39  mMinimumValue = minimumValuePossible( mRasterDataType );
40  mMaximumValue = maximumValuePossible( mRasterDataType );
41  mRasterDataTypeRange = mMaximumValue - mMinimumValue;
42 
43  mLookupTableOffset = mMinimumValue * -1;
44 
45  mContrastEnhancementFunction = new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue );
46 
47  //If the data type is larger than 16-bit do not generate a lookup table
48  if ( mRasterDataTypeRange <= 65535.0 )
49  {
50  mLookupTable = new int[static_cast <int>( mRasterDataTypeRange+1 )];
51  }
52 
53 }
54 
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 )
63 {
64  mLookupTableOffset = minimumValuePossible( mRasterDataType ) * -1;
65 
66  // setContrastEnhancementAlgorithm sets also QgsContrastEnhancementFunction
67  setContrastEnhancementAlgorithm( ce.mContrastEnhancementAlgorithm, false );
68 
69  //If the data type is larger than 16-bit do not generate a lookup table
70  if ( mRasterDataTypeRange <= 65535.0 )
71  {
72  mLookupTable = new int[static_cast <int>( mRasterDataTypeRange+1 )];
73  }
74 }
75 
77 {
78  delete [] mLookupTable;
79  delete mContrastEnhancementFunction;
80 }
81 /*
82  *
83  * Static methods
84  *
85  */
86 
91 {
92  switch ( theDataType )
93  {
94  case QGis::Byte:
96  case QGis::UInt16:
98  case QGis::Int16:
100  case QGis::UInt32:
102  case QGis::Int32:
104  case QGis::Float32:
106  case QGis::Float64:
108  case QGis::CInt16:
110  case QGis::CInt32:
112  case QGis::CFloat32:
114  case QGis::CFloat64:
116  case QGis::ARGB32:
119  // XXX - mloskot: not handled?
120  break;
121  }
122 
124 }
129 {
130  switch ( theDataType )
131  {
132  case QGis::Byte:
134  case QGis::UInt16:
136  case QGis::Int16:
138  case QGis::UInt32:
140  case QGis::Int32:
142  case QGis::Float32:
143  return std::numeric_limits<float>::max() * -1.0;
144  case QGis::Float64:
145  return std::numeric_limits<double>::max() * -1.0;
146  case QGis::CInt16:
148  case QGis::CInt32:
150  case QGis::CFloat32:
151  return std::numeric_limits<float>::max() * -1.0;
152  case QGis::CFloat64:
153  return std::numeric_limits<double>::max() * -1.0;
154  case QGis::ARGB32:
157  // XXX - mloskot: not handled?
158  break;
159  }
160 
161  return std::numeric_limits<double>::max() * -1.0;
162 }
163 
164 /*
165  *
166  * Non-Static methods
167  *
168  */
175 {
176  if ( mEnhancementDirty )
177  {
178  generateLookupTable();
179  }
180 
181  if ( mLookupTable && NoEnhancement != mContrastEnhancementAlgorithm )
182  {
183  return mLookupTable[static_cast <int>( theValue + mLookupTableOffset )];
184  }
185  else
186  {
187  // Even if the contrast enhancement algorithms is set to NoEnhancement
188  // The input values will still have to be scaled for all data types
189  // greater than 1 byte.
190  return mContrastEnhancementFunction->enhance( theValue );
191  }
192 }
193 
197 bool QgsContrastEnhancement::generateLookupTable()
198 {
199  mEnhancementDirty = false;
200 
201  if ( !mContrastEnhancementFunction )
202  return false;
203  if ( NoEnhancement == mContrastEnhancementAlgorithm )
204  return false;
205  if ( QGis::Byte != mRasterDataType && QGis::UInt16 != mRasterDataType && QGis::Int16 != mRasterDataType )
206  return false;
207  if ( !mLookupTable )
208  return false;
209 
210  QgsDebugMsg( "building lookup table" );
211  QgsDebugMsg( "***MinimumValue : " + QString::number( mMinimumValue ) );
212  QgsDebugMsg( "***MaximumValue : " + QString::number( mMaximumValue ) );
213  QgsDebugMsg( "***mLookupTableOffset : " + QString::number( mLookupTableOffset ) );
214  QgsDebugMsg( "***mRasterDataTypeRange : " + QString::number( mRasterDataTypeRange ) );
215 
216  for ( int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
217  {
218  mLookupTable[myIterator] = mContrastEnhancementFunction->enhance( static_cast< double >( myIterator ) - mLookupTableOffset );
219  }
220 
221  return true;
222 }
223 
230 {
231  if ( mContrastEnhancementFunction )
232  {
233  return mContrastEnhancementFunction->isValueInDisplayableRange( theValue );
234  }
235 
236  return false;
237 }
238 
246 {
247  switch ( theAlgorithm )
248  {
250  delete mContrastEnhancementFunction;
251  mContrastEnhancementFunction = new QgsLinearMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue );
252  break;
254  delete mContrastEnhancementFunction;
255  mContrastEnhancementFunction = new QgsLinearMinMaxEnhancementWithClip( mRasterDataType, mMinimumValue, mMaximumValue );
256  break;
257  case ClipToMinimumMaximum :
258  delete mContrastEnhancementFunction;
259  mContrastEnhancementFunction = new QgsClipToMinMaxEnhancement( mRasterDataType, mMinimumValue, mMaximumValue );
260  break;
262  //Do nothing
263  break;
264  default:
265  delete mContrastEnhancementFunction;
266  mContrastEnhancementFunction = new QgsContrastEnhancementFunction( mRasterDataType, mMinimumValue, mMaximumValue );
267  break;
268  }
269 
270  mEnhancementDirty = true;
271  mContrastEnhancementAlgorithm = theAlgorithm;
272 
273  if ( generateTable )
274  {
275  generateLookupTable();
276  }
277 }
278 
285 {
286  QgsDebugMsgLevel( "called", 4 );
287 
288  if ( theFunction )
289  {
290  delete mContrastEnhancementFunction;
291  mContrastEnhancementFunction = theFunction;
292  mContrastEnhancementAlgorithm = UserDefinedEnhancement;
293  generateLookupTable();
294  }
295 }
296 
303 void QgsContrastEnhancement::setMaximumValue( double theValue, bool generateTable )
304 {
305  QgsDebugMsgLevel( "called value: " + QString::number( theValue ) + " generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
306 
307  if ( theValue > maximumValuePossible( mRasterDataType ) )
308  {
309  mMaximumValue = maximumValuePossible( mRasterDataType );
310  }
311  else
312  {
313  mMaximumValue = theValue;
314  }
315 
316  if ( mContrastEnhancementFunction )
317  {
318  mContrastEnhancementFunction->setMaximumValue( theValue );
319  }
320 
321  mEnhancementDirty = true;
322 
323  if ( generateTable )
324  {
325  generateLookupTable();
326  }
327 }
328 
335 void QgsContrastEnhancement::setMinimumValue( double theValue, bool generateTable )
336 {
337  QgsDebugMsgLevel( "called value: " + QString::number( theValue ) + " generate lookup table: " + QString::number( static_cast< int >( generateTable ) ), 4 );
338 
339  if ( theValue < minimumValuePossible( mRasterDataType ) )
340  {
341  mMinimumValue = minimumValuePossible( mRasterDataType );
342  }
343  else
344  {
345  mMinimumValue = theValue;
346  }
347 
348  if ( mContrastEnhancementFunction )
349  {
350  mContrastEnhancementFunction->setMinimumValue( theValue );
351  }
352 
353  mEnhancementDirty = true;
354 
355  if ( generateTable )
356  {
357  generateLookupTable();
358  }
359 }
360 
362 {
363  //minimum value
364  QDomElement minElem = doc.createElement( "minValue" );
365  QDomText minText = doc.createTextNode( QgsRasterBlock::printValue( mMinimumValue ) );
366  minElem.appendChild( minText );
367  parentElem.appendChild( minElem );
368 
369  //maximum value
370  QDomElement maxElem = doc.createElement( "maxValue" );
371  QDomText maxText = doc.createTextNode( QgsRasterBlock::printValue( mMaximumValue ) );
372  maxElem.appendChild( maxText );
373  parentElem.appendChild( maxElem );
374 
375  //algorithm
376  QDomElement algorithmElem = doc.createElement( "algorithm" );
377  QDomText algorithmText = doc.createTextNode( contrastEnhancementAlgorithmString( mContrastEnhancementAlgorithm ) );
378  algorithmElem.appendChild( algorithmText );
379  parentElem.appendChild( algorithmElem );
380 }
381 
383 {
384  QDomElement minValueElem = elem.firstChildElement( "minValue" );
385  if ( !minValueElem.isNull() )
386  {
387  mMinimumValue = minValueElem.text().toDouble();
388  }
389  QDomElement maxValueElem = elem.firstChildElement( "maxValue" );
390  if ( !maxValueElem.isNull() )
391  {
392  mMaximumValue = maxValueElem.text().toDouble();
393  }
394  QDomElement algorithmElem = elem.firstChildElement( "algorithm" );
395  if ( !algorithmElem.isNull() )
396  {
397  QString algorithmString = algorithmElem.text();
399  // old version ( < 19 Apr 2013) was using enum directly -> for backward compatibility
400  if ( algorithmString == "0" )
401  {
402  algorithm = NoEnhancement;
403  }
404  else if ( algorithmString == "1" )
405  {
406  algorithm = StretchToMinimumMaximum;
407  }
408  else if ( algorithmString == "2" )
409  {
410  algorithm = StretchAndClipToMinimumMaximum;
411  }
412  else if ( algorithmString == "3" )
413  {
414  algorithm = ClipToMinimumMaximum;
415  }
416  else if ( algorithmString == "4" )
417  {
418  algorithm = UserDefinedEnhancement;
419  }
420  else
421  {
422  algorithm = contrastEnhancementAlgorithmFromString( algorithmString );
423  }
424 
425  setContrastEnhancementAlgorithm( algorithm );
426  }
427 }
428 
430 {
431  switch ( algorithm )
432  {
433  case NoEnhancement:
434  return "NoEnhancement";
436  return "StretchToMinimumMaximum";
438  return "StretchAndClipToMinimumMaximum";
440  return "ClipToMinimumMaximum";
442  return "UserDefinedEnhancement";
443  }
444  return "NoEnhancement";
445 }
446 
448 {
449  if ( contrastEnhancementString == "StretchToMinimumMaximum" )
450  {
452  }
453  else if ( contrastEnhancementString == "StretchAndClipToMinimumMaximum" )
454  {
456  }
457  else if ( contrastEnhancementString == "ClipToMinimumMaximum" )
458  {
459  return ClipToMinimumMaximum;
460  }
461  else if ( contrastEnhancementString == "UserDefinedEnhancement" )
462  {
463  return UserDefinedEnhancement;
464  }
465  else
466  {
467  return NoEnhancement;
468  }
469 }
Eight bit unsigned integer (quint8)
Definition: qgis.h:136
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.
Definition: qgis.h:135
void readXML(const QDomElement &elem)
Complex Float32.
Definition: qgis.h:145
QDomNode appendChild(const QDomNode &newChild)
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
bool isValueInDisplayableRange(double)
Return true if pixel is in stretable range, false if pixel is outside of range (i.e., clipped)
Complex Int32.
Definition: qgis.h:144
Thirty two bit floating point (float)
Definition: qgis.h:141
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)
Definition: qgis.h:137
Sixty four bit floating point (double)
Definition: qgis.h:142
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:150
QString text() const
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
Thirty two bit signed integer (qint32)
Definition: qgis.h:140
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)
Definition: qgis.h:139
static QString contrastEnhancementAlgorithmString(ContrastEnhancementAlgorithm algorithm)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:148
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)
Definition: qgis.h:138
QDomText createTextNode(const QString &value)
QgsContrastEnhancement(QGis::DataType theDatatype=QGis::Byte)
static ContrastEnhancementAlgorithm contrastEnhancementAlgorithmFromString(const QString &contrastEnhancementString)
bool isNull() const
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.
Definition: qgis.h:133
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)
Complex Int16.
Definition: qgis.h:143
void setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
void setMaximumValue(double)
Mustator for the maximum value.
Complex Float64.
Definition: qgis.h:146