Quantum GIS API Documentation
1.7.5-Wroclaw
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
core
raster
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
"
24
#include "
qgscontrastenhancementfunction.h
"
25
#include "
qgslinearminmaxenhancement.h
"
26
#include "
qgslinearminmaxenhancementwithclip.h
"
27
#include "
qgscliptominmaxenhancement.h
"
28
29
QgsContrastEnhancement::QgsContrastEnhancement
(
QgsRasterDataType
theDataType )
30
{
31
mLookupTable
= 0;
32
mContrastEnhancementFunction
= 0;
33
mEnhancementDirty
=
false
;
34
mContrastEnhancementAlgorithm
=
NoEnhancement
;
35
mRasterDataType
= theDataType;
36
37
mMinimumValue
=
minimumValuePossible
(
mRasterDataType
);
38
mMaximumValue
=
maximumValuePossible
(
mRasterDataType
);
39
mRasterDataTypeRange
=
mMaximumValue
-
mMinimumValue
;
40
41
mLookupTableOffset
= mMinimumValue * -1;
42
43
mContrastEnhancementFunction
=
new
QgsContrastEnhancementFunction
(
mRasterDataType
, mMinimumValue,
mMaximumValue
);
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
53
QgsContrastEnhancement::~QgsContrastEnhancement
()
54
{
55
}
56
/*
57
*
58
* Static methods
59
*
60
*/
61
65
double
QgsContrastEnhancement::maximumValuePossible
(
QgsRasterDataType
theDataType )
66
{
67
switch
( theDataType )
68
{
69
case
QGS_Byte
:
70
return
std::numeric_limits<unsigned char>::max
();
71
break
;
72
case
QGS_UInt16
:
73
return
std::numeric_limits<unsigned short>::max
();
74
break
;
75
case
QGS_Int16
:
76
return
std::numeric_limits<short>::max
();
77
break
;
78
case
QGS_UInt32
:
79
return
std::numeric_limits<unsigned int>::max
();
80
break
;
81
case
QGS_Int32
:
82
return
std::numeric_limits<int>::max
();
83
break
;
84
case
QGS_Float32
:
85
return
std::numeric_limits<float>::max
();
86
break
;
87
case
QGS_Float64
:
88
return
std::numeric_limits<double>::max
();
89
break
;
90
case
QGS_CInt16
:
91
return
std::numeric_limits<short>::max
();
92
break
;
93
case
QGS_CInt32
:
94
return
std::numeric_limits<int>::max
();
95
break
;
96
case
QGS_CFloat32
:
97
return
std::numeric_limits<float>::max
();
98
break
;
99
case
QGS_CFloat64
:
100
return
std::numeric_limits<double>::max
();
101
break
;
102
case
QGS_Unknown
:
103
case
QGS_TypeCount
:
104
// XXX - mloskot: not handled?
105
break
;
106
}
107
108
return
std::numeric_limits<double>::max
();
109
}
113
double
QgsContrastEnhancement::minimumValuePossible
(
QgsRasterDataType
theDataType )
114
{
115
switch
( theDataType )
116
{
117
case
QGS_Byte
:
118
return
std::numeric_limits<unsigned char>::min
();
119
break
;
120
case
QGS_UInt16
:
121
return
std::numeric_limits<unsigned short>::min
();
122
break
;
123
case
QGS_Int16
:
124
return
std::numeric_limits<short>::min
();
125
break
;
126
case
QGS_UInt32
:
127
return
std::numeric_limits<unsigned int>::min
();
128
break
;
129
case
QGS_Int32
:
130
return
std::numeric_limits<int>::min
();
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
:
139
return
std::numeric_limits<short>::min
();
140
break
;
141
case
QGS_CInt32
:
142
return
std::numeric_limits<int>::min
();
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
*/
169
int
QgsContrastEnhancement::enhanceContrast
(
double
theValue )
170
{
171
if
(
mEnhancementDirty
)
172
{
173
generateLookupTable
();
174
}
175
176
if
(
mLookupTable
&&
NoEnhancement
!=
mContrastEnhancementAlgorithm
)
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
192
bool
QgsContrastEnhancement::generateLookupTable
()
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
220
bool
QgsContrastEnhancement::isValueInDisplayableRange
(
double
theValue )
221
{
222
223
if
( 0 !=
mContrastEnhancementFunction
)
224
{
225
return
mContrastEnhancementFunction
->
isValueInDisplayableRange
( theValue );
226
}
227
228
return
false
;
229
}
230
237
void
QgsContrastEnhancement::setContrastEnhancementAlgorithm
(
ContrastEnhancementAlgorithm
theAlgorithm,
bool
generateTable )
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
{
245
case
StretchToMinimumMaximum
:
246
mContrastEnhancementFunction
=
new
QgsLinearMinMaxEnhancement
(
mRasterDataType
,
mMinimumValue
,
mMaximumValue
);
247
break
;
248
case
StretchAndClipToMinimumMaximum
:
249
mContrastEnhancementFunction
=
new
QgsLinearMinMaxEnhancementWithClip
(
mRasterDataType
,
mMinimumValue
,
mMaximumValue
);
250
break
;
251
case
ClipToMinimumMaximum
:
252
mContrastEnhancementFunction
=
new
QgsClipToMinMaxEnhancement
(
mRasterDataType
,
mMinimumValue
,
mMaximumValue
);
253
break
;
254
case
UserDefinedEnhancement
:
255
//Do nothing
256
break
;
257
default
:
258
mContrastEnhancementFunction
=
new
QgsContrastEnhancementFunction
(
mRasterDataType
,
mMinimumValue
,
mMaximumValue
);
259
break
;
260
}
261
262
mEnhancementDirty
=
true
;
263
mContrastEnhancementAlgorithm
= theAlgorithm;
264
265
if
( generateTable )
266
{
267
generateLookupTable
();
268
}
269
}
270
}
271
277
void
QgsContrastEnhancement::setContrastEnhancementFunction
(
QgsContrastEnhancementFunction
* theFunction )
278
{
279
QgsDebugMsg
(
"called"
);
280
281
if
( 0 != theFunction )
282
{
283
mContrastEnhancementFunction
= theFunction;
284
mContrastEnhancementAlgorithm
=
UserDefinedEnhancement
;
285
generateLookupTable
();
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
{
301
mMaximumValue
=
maximumValuePossible
(
mRasterDataType
);
302
}
303
else
304
{
305
mMaximumValue
= theValue;
306
}
307
308
if
( 0 !=
mContrastEnhancementFunction
)
309
{
310
mContrastEnhancementFunction
->
setMaximumValue
( theValue );
311
}
312
313
mEnhancementDirty
=
true
;
314
315
if
( generateTable )
316
{
317
generateLookupTable
();
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
{
333
mMinimumValue
=
minimumValuePossible
(
mRasterDataType
);
334
}
335
else
336
{
337
mMinimumValue
= theValue;
338
}
339
340
if
( 0 !=
mContrastEnhancementFunction
)
341
{
342
mContrastEnhancementFunction
->
setMinimumValue
( theValue );
343
}
344
345
mEnhancementDirty
=
true
;
346
347
if
( generateTable )
348
{
349
generateLookupTable
();
350
}
351
}
Generated on Thu Sep 19 2013 17:30:55 for Quantum GIS API Documentation by
1.8.4