Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
30 {
31  mLookupTable = 0;
33  mEnhancementDirty = false;
35  mRasterDataType = theDataType;
36 
40 
41  mLookupTableOffset = mMinimumValue * -1;
42 
44 
45  //If the data type is larger than 16-bit do not generate a lookup table
46  if ( mRasterDataTypeRange <= 65535.0 )
47  {
48  mLookupTable = new int[static_cast <int>( mRasterDataTypeRange+1 )];
49  }
50 
51 }
52 
54 {
55 }
56 /*
57  *
58  * Static methods
59  *
60  */
61 
66 {
67  switch ( theDataType )
68  {
69  case QGS_Byte:
71  break;
72  case QGS_UInt16:
74  break;
75  case QGS_Int16:
77  break;
78  case QGS_UInt32:
80  break;
81  case QGS_Int32:
83  break;
84  case QGS_Float32:
86  break;
87  case QGS_Float64:
89  break;
90  case QGS_CInt16:
92  break;
93  case QGS_CInt32:
95  break;
96  case QGS_CFloat32:
98  break;
99  case QGS_CFloat64:
101  break;
102  case QGS_Unknown:
103  case QGS_TypeCount:
104  // XXX - mloskot: not handled?
105  break;
106  }
107 
109 }
114 {
115  switch ( theDataType )
116  {
117  case QGS_Byte:
119  break;
120  case QGS_UInt16:
122  break;
123  case QGS_Int16:
125  break;
126  case QGS_UInt32:
128  break;
129  case QGS_Int32:
131  break;
132  case QGS_Float32:
133  return std::numeric_limits<float>::max() * -1.0;
134  break;
135  case QGS_Float64:
136  return std::numeric_limits<double>::max() * -1.0;
137  break;
138  case QGS_CInt16:
140  break;
141  case QGS_CInt32:
143  break;
144  case QGS_CFloat32:
145  return std::numeric_limits<float>::max() * -1.0;
146  break;
147  case QGS_CFloat64:
148  return std::numeric_limits<double>::max() * -1.0;
149  break;
150  case QGS_Unknown:
151  case QGS_TypeCount:
152  // XXX - mloskot: not handled?
153  break;
154  }
155 
156  return std::numeric_limits<double>::max() * -1.0;
157 }
158 
159 /*
160  *
161  * Non-Static methods
162  *
163  */
170 {
171  if ( mEnhancementDirty )
172  {
174  }
175 
177  {
178  return mLookupTable[static_cast <int>( theValue + mLookupTableOffset )];
179  }
180  else
181  {
182  // Even if the contrast enhancement algorithms is set to NoEnhancement
183  // The input values will still have to be scaled for all data types
184  // greater than 1 byte.
185  return mContrastEnhancementFunction->enhance( theValue );
186  }
187 }
188 
193 {
194  mEnhancementDirty = false;
195 
196  if ( 0 == mContrastEnhancementFunction ) { return false; }
197  if ( NoEnhancement == mContrastEnhancementAlgorithm ) { return false; }
198  if ( QGS_Byte != mRasterDataType && QGS_UInt16 != mRasterDataType && QGS_Int16 != mRasterDataType ) { return false; }
199  if ( !mLookupTable ) { return false; }
200 
201  QgsDebugMsg( "building lookup table" );
202  QgsDebugMsg( "***MinimumValue : " + QString::number( mMinimumValue ) );
203  QgsDebugMsg( "***MaximumValue : " + QString::number( mMaximumValue ) );
204  QgsDebugMsg( "***mLookupTableOffset : " + QString::number( mLookupTableOffset ) );
205  QgsDebugMsg( "***mRasterDataTypeRange : " + QString::number( mRasterDataTypeRange ) );
206 
207  for ( int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
208  {
209  mLookupTable[myIterator] = mContrastEnhancementFunction->enhance(( double )myIterator - mLookupTableOffset );
210  }
211 
212  return true;
213 }
214 
221 {
222 
223  if ( 0 != mContrastEnhancementFunction )
224  {
226  }
227 
228  return false;
229 }
230 
238 {
239  QgsDebugMsg( "called algorithm: " + QString::number(( int )theAlgorithm ) + " generate lookup table: " + QString::number(( int )generateTable ) );
240 
241  if ( theAlgorithm != mContrastEnhancementAlgorithm )
242  {
243  switch ( theAlgorithm )
244  {
247  break;
250  break;
251  case ClipToMinimumMaximum :
253  break;
255  //Do nothing
256  break;
257  default:
259  break;
260  }
261 
262  mEnhancementDirty = true;
263  mContrastEnhancementAlgorithm = theAlgorithm;
264 
265  if ( generateTable )
266  {
268  }
269  }
270 }
271 
278 {
279  QgsDebugMsg( "called" );
280 
281  if ( 0 != theFunction )
282  {
283  mContrastEnhancementFunction = theFunction;
286  }
287 }
288 
295 void QgsContrastEnhancement::setMaximumValue( double theValue, bool generateTable )
296 {
297  QgsDebugMsg( "called value: " + QString::number( theValue ) + " generate lookup table: " + QString::number(( int )generateTable ) );
298 
299  if ( theValue > maximumValuePossible( mRasterDataType ) )
300  {
302  }
303  else
304  {
305  mMaximumValue = theValue;
306  }
307 
308  if ( 0 != mContrastEnhancementFunction )
309  {
311  }
312 
313  mEnhancementDirty = true;
314 
315  if ( generateTable )
316  {
318  }
319 }
320 
327 void QgsContrastEnhancement::setMinimumValue( double theValue, bool generateTable )
328 {
329  QgsDebugMsg( "called value: " + QString::number( theValue ) + " generate lookup table: " + QString::number(( int )generateTable ) );
330 
331  if ( theValue < minimumValuePossible( mRasterDataType ) )
332  {
334  }
335  else
336  {
337  mMinimumValue = theValue;
338  }
339 
340  if ( 0 != mContrastEnhancementFunction )
341  {
343  }
344 
345  mEnhancementDirty = true;
346 
347  if ( generateTable )
348  {
350  }
351 }