29 #include <QDomDocument>
30 #include <QDomElement>
33 : mMinimumValue( minimumValuePossible( dataType ) )
34 , mMaximumValue( maximumValuePossible( dataType ) )
35 , mRasterDataType( dataType )
36 , mRasterDataTypeRange( mMaximumValue - mMinimumValue )
37 , mLookupTableOffset( mMinimumValue * -1 )
42 if ( mRasterDataTypeRange <= 65535.0 )
44 mLookupTable =
new int[
static_cast <int>( mRasterDataTypeRange + 1 )];
49 : mEnhancementDirty( true )
50 , mMinimumValue( ce.mMinimumValue )
51 , mMaximumValue( ce.mMaximumValue )
52 , mRasterDataType( ce.mRasterDataType )
53 , mRasterDataTypeRange( ce.mRasterDataTypeRange )
61 if ( mRasterDataTypeRange <= 65535.0 )
63 mLookupTable =
new int[
static_cast <int>( mRasterDataTypeRange + 1 )];
69 delete [] mLookupTable;
74 if ( mEnhancementDirty )
76 generateLookupTable();
79 if ( mLookupTable &&
NoEnhancement != mContrastEnhancementAlgorithm )
81 const double shiftedValue = value + mLookupTableOffset;
82 if ( shiftedValue >= 0 && shiftedValue < mRasterDataTypeRange + 1 )
83 return mLookupTable[
static_cast <int>( shiftedValue )];
91 return mContrastEnhancementFunction->enhance( value );
95 bool QgsContrastEnhancement::generateLookupTable()
97 mEnhancementDirty =
false;
99 if ( !mContrastEnhancementFunction )
109 QgsDebugMsgLevel( QStringLiteral(
"***MinimumValue : %1" ).arg( mMinimumValue ), 4 );
110 QgsDebugMsgLevel( QStringLiteral(
"***MaximumValue : %1" ).arg( mMaximumValue ), 4 );
111 QgsDebugMsgLevel( QStringLiteral(
"***mLookupTableOffset : %1" ).arg( mLookupTableOffset ), 4 );
112 QgsDebugMsgLevel( QStringLiteral(
"***mRasterDataTypeRange : %1" ).arg( mRasterDataTypeRange ), 4 );
114 for (
int myIterator = 0; myIterator <= mRasterDataTypeRange; myIterator++ )
116 mLookupTable[myIterator] = mContrastEnhancementFunction->enhance(
static_cast< double >( myIterator ) - mLookupTableOffset );
124 if ( mContrastEnhancementFunction )
126 return mContrastEnhancementFunction->isValueInDisplayableRange( value );
153 mEnhancementDirty =
true;
154 mContrastEnhancementAlgorithm =
algorithm;
158 generateLookupTable();
168 mContrastEnhancementFunction.reset(
function );
170 generateLookupTable();
176 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
184 mMaximumValue = value;
187 if ( mContrastEnhancementFunction )
189 mContrastEnhancementFunction->setMaximumValue( value );
192 mEnhancementDirty =
true;
196 generateLookupTable();
202 QgsDebugMsgLevel(
"called value: " + QString::number( value ) +
" generate lookup table: " + QString::number(
static_cast< int >( generateTable ) ), 4 );
210 mMinimumValue = value;
213 if ( mContrastEnhancementFunction )
215 mContrastEnhancementFunction->setMinimumValue( value );
218 mEnhancementDirty =
true;
222 generateLookupTable();
229 QDomElement minElem = doc.createElement( QStringLiteral(
"minValue" ) );
231 minElem.appendChild( minText );
232 parentElem.appendChild( minElem );
235 QDomElement maxElem = doc.createElement( QStringLiteral(
"maxValue" ) );
237 maxElem.appendChild( maxText );
238 parentElem.appendChild( maxElem );
241 QDomElement algorithmElem = doc.createElement( QStringLiteral(
"algorithm" ) );
243 algorithmElem.appendChild( algorithmText );
244 parentElem.appendChild( algorithmElem );
249 const QDomElement minValueElem = elem.firstChildElement( QStringLiteral(
"minValue" ) );
250 if ( !minValueElem.isNull() )
252 mMinimumValue = minValueElem.text().toDouble();
254 const QDomElement maxValueElem = elem.firstChildElement( QStringLiteral(
"maxValue" ) );
255 if ( !maxValueElem.isNull() )
257 mMaximumValue = maxValueElem.text().toDouble();
259 const QDomElement algorithmElem = elem.firstChildElement( QStringLiteral(
"algorithm" ) );
260 if ( !algorithmElem.isNull() )
262 const QString algorithmString = algorithmElem.text();
265 if ( algorithmString == QLatin1String(
"0" ) )
269 else if ( algorithmString == QLatin1String(
"1" ) )
273 else if ( algorithmString == QLatin1String(
"2" ) )
277 else if ( algorithmString == QLatin1String(
"3" ) )
281 else if ( algorithmString == QLatin1String(
"4" ) )
296 if ( doc.isNull() || element.isNull() )
303 algName = QStringLiteral(
"StretchToMinimumMaximum" );
308 algName = QStringLiteral(
"ClipToMinimumMaximum" );
311 algName = QStringLiteral(
"ClipToMinimumMaximum" );
317 QgsDebugMsg( QObject::tr(
"No SLD1.0 conversion yet for stretch algorithm %1" ).arg( algName ) );
324 QDomElement normalizeElem = doc.createElement( QStringLiteral(
"sld:Normalize" ) );
325 element.appendChild( normalizeElem );
327 QDomElement vendorOptionAlgorithmElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
328 vendorOptionAlgorithmElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"algorithm" ) );
329 vendorOptionAlgorithmElem.appendChild( doc.createTextNode( algName ) );
330 normalizeElem.appendChild( vendorOptionAlgorithmElem );
332 QDomElement vendorOptionMinValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
333 vendorOptionMinValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"minValue" ) );
334 vendorOptionMinValueElem.appendChild( doc.createTextNode( QString::number(
minimumValue() ) ) );
335 normalizeElem.appendChild( vendorOptionMinValueElem );
337 QDomElement vendorOptionMaxValueElem = doc.createElement( QStringLiteral(
"sld:VendorOption" ) );
338 vendorOptionMaxValueElem.setAttribute( QStringLiteral(
"name" ), QStringLiteral(
"maxValue" ) );
339 vendorOptionMaxValueElem.appendChild( doc.createTextNode( QString::number(
maximumValue() ) ) );
340 normalizeElem.appendChild( vendorOptionMaxValueElem );
348 return QStringLiteral(
"NoEnhancement" );
350 return QStringLiteral(
"StretchToMinimumMaximum" );
352 return QStringLiteral(
"StretchAndClipToMinimumMaximum" );
354 return QStringLiteral(
"ClipToMinimumMaximum" );
356 return QStringLiteral(
"UserDefinedEnhancement" );
358 return QStringLiteral(
"NoEnhancement" );
363 if ( contrastEnhancementString == QLatin1String(
"StretchToMinimumMaximum" ) )
367 else if ( contrastEnhancementString == QLatin1String(
"StretchAndClipToMinimumMaximum" ) )
371 else if ( contrastEnhancementString == QLatin1String(
"ClipToMinimumMaximum" ) )
375 else if ( contrastEnhancementString == QLatin1String(
"UserDefinedEnhancement" ) )