RasterTransform.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file RasterTransform.h
22 
23  \brief A Raster Transform is a class that defines functions to transform a styled raster.
24 */
25 
26 #ifndef __TERRALIB_MAPTOOLS_INTERNAL_RASTERTRANSFORM_H
27 #define __TERRALIB_MAPTOOLS_INTERNAL_RASTERTRANSFORM_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "../color/RGBAColor.h"
32 #include "../color/ColorBar.h"
33 #include "../raster.h"
34 
35 // STL
36 #include <map>
37 #include <cmath>
38 
39 namespace te
40 {
41 // Forward declarations
42  namespace se
43  {
44  class RasterSymbolizer;
45  }
46 
47  namespace rst
48  {
49  class Raster;
50  class RasterProperty;
51  }
52 
53  namespace map
54  {
55 // Forward declarations
56 
57  /*!
58  \class RasterTransform
59 
60  \brief A Raster Transform is a class that defines functions to transform a styled raster.
61 
62  \sa
63  */
65  {
66  public:
67 
68  typedef void (RasterTransform::*TransformFunction)(double, double, double, double);
69  typedef te::color::RGBAColor (RasterTransform::*RGBAFunction)(double, double);
70 
71  typedef std::pair<double, double> RasterThreshold;
72 
73  typedef std::map<RasterThreshold, te::color::RGBAColor> CategorizedMap;
74  typedef std::map<RasterThreshold, te::color::ColorBar> InterpolatedMap;
75  typedef std::map<int, te::color::RGBAColor> RecodedMap;
76 
77  //! The three channels of a display
79  {
80  RED_CHANNEL=0,
81  GREEN_CHANNEL=1,
82  BLUE_CHANNEL=2,
83  ALPHA_CHANNEL=3
84  };
85 
86  // The raster transform functions type
88  {
89  NO_TRANSF=0,
90  MONO2THREE_TRANSF=1,
91  EXTRACT2RGB_TRANSF=2,
92  RED2THREE_TRANSF=3,
93  GREEN2THREE_TRANSF=4,
94  BLUE2THREE_TRANSF=5,
95  CATEGORIZE_TRANSF=6,
96  INTERPOLATE_TRANSF=7,
97  RECODE_TRANSF = 8,
98  BAND2BAND_TRANSF=9,
100  CONTRAST_TRANSF /*!< Apply a contrast method ( ContrastType ) to the given raster - Use the methods getGain, setOffset and setOffset2 to setup contrast parameters. */
101  };
102 
103  /*! \enum Allowed contrast types. */
105  {
106  LINEAR_CONTRAST, /*!< The histogram range will be changed to the supplied min/max range ( linear function ). */
107  SQUARE_CONTRAST, /*!< The contrasted image will be created by using a square function. */
108  SQUARE_ROOT_CONTRAST, /*!< The contrasted image will be created by using a square root function. */
109  LOG_CONTRAST /*!< The contrasted image will be created by using a log function. */
110  };
111 
112  public:
113 
114  /*!
115  \brief Constructor
116 
117  \param input
118  \param output
119 
120  \note
121  */
123 
124  /*! \brief Destructor. */
125  ~RasterTransform();
126 
127  /*! \brief Gets the input raster. */
128  te::rst::Raster* getInputRaster() { return m_rasterIn; }
129 
130  /*! \brief Gets the output raster. */
131  te::rst::Raster* getOutputRaster() { return m_rasterOut; }
132 
133  /*! \brief Sets the transparency. */
134  void setTransparency(double value) { m_transp = value; }
135 
136  /*! \brief Gets the transparency. */
137  double getTransparency() { return m_transp; }
138 
139  /*! \brief Sets the gain. */
140  void setGain(double value);
141 
142  /*! \brief Gets the gain. */
143  double getGain() const;
144 
145  /*! \brief Sets the offset 1. */
146  void setOffset(double value);
147 
148  /*!
149  \brief Set the current contrast type.
150  \param newType New contrast type.
151  */
152  void setContrastType( const ContrastType& newType );
153 
154  /*! \brief Gets the offset. */
155  double getOffset();
156 
157  /*! \brief Sets the constrast value for red band. */
158  void setGammaR(double value);
159 
160  /*! \brief Gets the constrast value for red band. */
161  double getGammaR();
162 
163  /*! \brief Sets the constrast value for green band. */
164  void setGammaG(double value);
165 
166  /*! \brief Gets the constrast value for green band. */
167  double getGammaG();
168 
169  /*! \brief Sets the constrast value for blue band. */
170  void setGammaB(double value);
171 
172  /*! \brief Gets the constrast value for blue band. */
173  double getGammaB();
174 
175  /*! \brief Sets the constrast value for gray band. */
176  void setGammaM(double value);
177 
178  /*! \brief Gets the constrast value for gray band. */
179  double getGammaM();
180 
181  /*! \brief Sets the rgb map values. */
182  void setRGBMap(std::map<RGBChannels, short>& rgbMap);
183 
184  /*! Sets the mapping from a particular input band to a particular output channel */
185  void setBChannelMapping(short bIn, RGBChannels bOut) { m_rgbMap[bOut] = bIn; }
186 
187  /*! Clears current mapping from bands to channel */
188  void clearRGBMap() { m_rgbMap.clear(); }
189 
190  /*! Returns the mapping from a particular input band to a particular output channel */
191  std::map<RGBChannels, short>& getRGBMap() { return m_rgbMap; }
192 
193  /*! Sets the mono band to be transformed */
194  void setSrcBand(short n) { m_monoBand = n; }
195 
196  /*! Gets the mono band to be transformed */
197  short getSrcBand() { return m_monoBand; }
198 
199  /*! Sets the destination of the mono band */
200  void setDestBand(short n) { m_monoBandOut = n; }
201 
202  /*! Gets the destination of the mono band */
203  short getDestBand() { return m_monoBandOut; }
204 
205  /*! Sets the categorize map information */
206  void setCategorizedMap(CategorizedMap map) { m_categorizeMap = map; }
207 
208  /*! Gets the categorize map information */
209  CategorizedMap& getCategorizedMap() { return m_categorizeMap; }
210 
211  /*! Sets the interpolate map information */
212  void setInterpolatedMap(InterpolatedMap map) { m_interpolateMap = map; }
213 
214  /*! Gets the categorize map information */
215  InterpolatedMap& getInterpolatedMap() { return m_interpolateMap; }
216 
217  /*! Sets the recode map information */
218  void setRecodedMap(RecodedMap map) { m_recodeMap = map; }
219 
220  /*! Gets the recode map information */
221  RecodedMap& getRecodedMap() { return m_recodeMap; }
222 
223  /*!
224  \brief Set parameters of linear transformation
225 
226  \param vmin smallest input value
227  \param vmax largest input value
228  \param rmin smallest value of the output range
229  \param rmax largest value of the output range
230 
231  */
232  void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax);
233 
234  /*!
235  \brief Set parameters of linear transformation for each band
236 
237  \param vmin smallest input value
238  \param vmax largest input value
239  \param rmin smallest value of the output range
240  \param rmax largest value of the output range
241  \param band band id
242 
243  */
244  void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax, int band);
245 
246  /*! Returns the identifier of the transformation function currently set */
247  RasterTransfFunctions getTransfFunction();
248 
249  /*! Sets the associated transformation function from an identifier */
250  void setTransfFunction(RasterTransfFunctions func);
251 
252  /*! Sets the transformation method to be used */
253  void setTransfFunction(RasterTransform::TransformFunction transfFuncPtr) { m_transfFuncPtr = transfFuncPtr; }
254 
255  /*! Sets the transformation method to be used */
256  void setRGBAFunction(RasterTransform::RGBAFunction transfFuncPtr) { m_RGBAFuncPtr = transfFuncPtr; }
257 
258  /*! Applies the selected transformation method */
259  void apply(double icol, double ilin, double ocol, double olin) {(this->*m_transfFuncPtr)(icol,ilin,ocol,olin); }
260 
261  te::color::RGBAColor apply(double icol, double ilin){return (this->*m_RGBAFuncPtr)(icol,ilin); }
262 
263  /*!
264  \brief Set gain and offset parameters to be used by the CONTRAST_TRANSF method.
265  \param contrastGains A vector of gains for each raster band.
266  \param contrastOffsets1 A vector of offsets1 for each raster band.
267  \param contrastOffsets2 A vector of offsets2 for each raster band.
268  */
269  void setContrastGainsAndOffsets( const std::vector< double >& contrastGains,
270  const std::vector< double >& contrastOffsets1,
271  const std::vector< double >& contrastOffsets2 );
272 
273  protected:
274 
275  /*!
276  \brief Type definition for the internal contrast transform function.
277  \note void ContrastTransformFunction( bandNumber, inputValue, outputValue )
278  */
279  typedef void (RasterTransform::*ContrastTransformFunction)(const unsigned int&, const double&, double&) const;
280 
281  /*!
282  \brief Configures the contrast function pointer.
283  \param contrastType Contrast type..
284  */
285  void setContrastFuncPtr(const RasterTransform::ContrastType contrastType );
286 
287  /*! This transformation repeats the value of the first band in input three bands of the output */
288  void setMono2ThreeBand(double icol, double ilin, double ocol, double olin);
289 
290  /*! This transformation repeats the value of the first band in input three bands of the output */
291  te::color::RGBAColor getMono2ThreeBand(double icol, double ilin);
292 
293  /*! This transformation is used to define a particular mapping from input bands to RGB channels */
294  void setExtractRGB(double icol, double ilin, double ocol, double olin);
295 
296  /*! This transformation is used to define a particular mapping from input bands to RGB channels */
297  te::color::RGBAColor getExtractRGB(double icol, double ilin);
298 
299  /*! This transformation is used to define a particular mapping from input bands to RGBA channels */
300  void setExtractRGBA(double icol, double ilin, double ocol, double olin);
301 
302  /*! This transformation is used to define a particular mapping from input bands to RGBA channels */
303  te::color::RGBAColor getExtractRGBA(double icol, double ilin);
304 
305  /*! This transformation repeats the value of the first band in input three bands of the output */
306  void setRed2ThreeBand(double icol, double ilin, double ocol, double olin);
307 
308  /*! This transformation repeats the value of the first band in input three bands of the output */
309  te::color::RGBAColor getRed2ThreeBand(double icol, double ilin);
310 
311  /*! This transformation repeats the value of the first band in input three bands of the output */
312  void setGreen2ThreeBand(double icol, double ilin, double ocol, double olin);
313 
314  /*! This transformation repeats the value of the first band in input three bands of the output */
315  te::color::RGBAColor getGreen2ThreeBand(double icol, double ilin);
316 
317  /*! This transformation repeats the value of the first band in input three bands of the output */
318  void setBlue2ThreeBand(double icol, double ilin, double ocol, double olin);
319 
320  /*! This transformation repeats the value of the first band in input three bands of the output */
321  te::color::RGBAColor getBlue2ThreeBand(double icol, double ilin);
322 
323  /*! This transformation get the value of the selected band in input and set the categorized value in output bands of the output */
324  void setCategorize(double icol, double ilin, double ocol, double olin);
325 
326  /*! This transformation get the value of the selected band in input and get the categorized value */
327  te::color::RGBAColor getCategorize(double icol, double ilin);
328 
329  /*! This transformation get the value of the selected band in input and set the interpolated value in output bands of the output */
330  void setInterpolate(double icol, double ilin, double ocol, double olin);
331 
332  /*! This transformation get the value of the selected band in input and get the interpolated value */
333  te::color::RGBAColor getInterpolate(double icol, double ilin);
334 
335  /*! This transformation get the value of the selected band in input and set the recoded value in output bands of the output */
336  void setRecode(double icol, double ilin, double ocol, double olin);
337 
338  /*! This transformation get the value of the selected band in input and get the recoded value */
339  te::color::RGBAColor getRecode(double icol, double ilin);
340 
341  /*! This transformation repeats the value of the n band in input to b band in output */
342  void setBand2Band(double icol, double ilin, double ocol, double olin);
343 
344  /*! This transformation aplies a linear contrast */
345  inline void applyLinearContrast(const unsigned int& bandIdx, const double& inputValue, double& outputValue) const
346  {
347  assert( bandIdx < m_contrastOffsets1.size() );
348  outputValue = ( ( inputValue + m_contrastOffsets1[ bandIdx ] ) *
349  m_contrastGains[ bandIdx ] ) + m_contrastOffsets2[ bandIdx ];
350  }
351 
352  /*! This transformation aplies a square contrast */
353  inline void applySquareContrast(const unsigned int& bandIdx, const double& inputValue, double& outputValue) const
354  {
355  assert( bandIdx < m_contrastGains.size() );
356  outputValue = std::pow( inputValue, 2.0 ) * m_contrastGains[ bandIdx ];
357  }
358 
359  /*! This transformation aplies a square root contrast */
360  inline void applySquareRootContrast(const unsigned int& bandIdx, const double& inputValue, double& outputValue) const
361  {
362  assert( bandIdx < m_contrastGains.size() );
363  outputValue = std::sqrt( inputValue ) * m_contrastGains[ bandIdx ];
364  }
365 
366  /*! This transformation aplies a log contrast */
367  inline void applyLogContrast(const unsigned int& bandIdx, const double& inputValue, double& outputValue) const
368  {
369  assert( bandIdx < m_contrastOffsets1.size() );
370  outputValue = std::log10( inputValue + m_contrastOffsets1[ bandIdx ] + 1.0 ) *
371  m_contrastGains[ bandIdx ];
372  }
373 
374  /*! Function used to adjust the value in raster range */
375  inline void fixValue(double& value, short& band) const
376  {
377  value = std::max(m_rstMinValue[static_cast<size_t>(band)], std::min(m_rstMaxValue[static_cast<size_t>(band)], value));
378  }
379 
380  /*!
381  \brief Function used to check if value is or not a valid value
382 
383  \return True if the value is EQUAL to NoValue and false in other case.
384  */
385  inline bool checkNoValue(double& value, int band) const
386  {
387  return (m_rasterIn->getBand(static_cast<size_t>(band))->getProperty()->m_noDataValue == value);
388  }
389 
390  /*!
391  \brief Function used to check the alpha value
392 
393  \return Return the alpha value.
394  */
395  double checkAlphaValue(double icol, double ilin);
396 
397  /*! Function used to get the interpolated color given a pixel value */
398  te::color::RGBAColor getInterpolatedColor(double value);
399 
400  /*! Function used to get the categorized color given a pixel value */
401  te::color::RGBAColor getCategorizedColor(double value);
402 
403  /*! Function used to get the recoded color given a pixel value */
404  te::color::RGBAColor getRecodedColor(double value);
405 
406  private:
407 
408  te::rst::Raster* m_rasterIn; //!< Pointer to a input raster.
409  te::rst::Raster* m_rasterOut; //!< Pointer to a output raster.
410 
411  ContrastType m_currentContrastType; //!< Current contrast type.
412  ContrastTransformFunction m_contrastTransfFuncPtr; //!< Function used in contrast transformation operation.
413 
414  double m_transp; //!< Transparency factor
415  std::vector< double > m_rstMinValue; //!< Min value from input raster.
416  std::vector< double > m_rstMaxValue; //!< Max value from input raster.
417 
418  std::map<RGBChannels, short> m_rgbMap; //!< Map used to set the band order.
419  short m_monoBand; //!< Value for the gray band.
420  short m_monoBandOut; //!< Value for output gray band channel.
421 
422  TransformFunction m_transfFuncPtr; //!< Function used in transformation operation.
423  RGBAFunction m_RGBAFuncPtr; //!< Function used in transformation operation.
424 
425  CategorizedMap m_categorizeMap; //!< Attribute to define the categorized transformation.
426  InterpolatedMap m_interpolateMap; //!< Attribute to define the interpolated transformation.
427  RecodedMap m_recodeMap; //!< Attribute to define the recoded transformation.
428 
429  std::vector< double > m_gamma; //!< A vector of gamma gains for each band.
430  std::vector< double > m_contrastGains; //!< A vector of gains for each band used by the CONTRAST method.
431  std::vector< double > m_contrastOffsets1;//!< A vector of offsets1 for each band used by the CONTRAST method.
432  std::vector< double > m_contrastOffsets2;//!< A vector of offsets2 for each band used by the CONTRAST method.
433  };
434 
435  } // end namespace map
436 } // end namespace te
437 
438 #endif // __TERRALIB_MAPTOOLS_INTERNAL_RASTERTRANSFORM_H
RecodedMap & getRecodedMap()
double getTransparency()
Gets the transparency.
te::color::RGBAColor apply(double icol, double ilin)
std::vector< double > m_rstMinValue
Min value from input raster.
void setRecodedMap(RecodedMap map)
void setRGBAFunction(RasterTransform::RGBAFunction transfFuncPtr)
void setTransparency(double value)
Sets the transparency.
void fixValue(double &value, short &band) const
void applySquareContrast(const unsigned int &bandIdx, const double &inputValue, double &outputValue) const
InterpolatedMap & getInterpolatedMap()
void setTransfFunction(RasterTransform::TransformFunction transfFuncPtr)
te::rst::Raster * m_rasterIn
Pointer to a input raster.
RecodedMap m_recodeMap
Attribute to define the recoded transformation.
void applyLogContrast(const unsigned int &bandIdx, const double &inputValue, double &outputValue) const
bool checkNoValue(double &value, int band) const
Function used to check if value is or not a valid value.
ContrastTransformFunction m_contrastTransfFuncPtr
Function used in contrast transformation operation.
void setBChannelMapping(short bIn, RGBChannels bOut)
te::rst::Raster * getOutputRaster()
Gets the output raster.
ContrastType m_currentContrastType
Current contrast type.
void applySquareRootContrast(const unsigned int &bandIdx, const double &inputValue, double &outputValue) const
std::vector< double > m_contrastOffsets1
A vector of offsets1 for each band used by the CONTRAST method.
RGBAFunction m_RGBAFuncPtr
Function used in transformation operation.
std::vector< double > m_contrastGains
A vector of gains for each band used by the CONTRAST method.
te::rst::Raster * getInputRaster()
Gets the input raster.
An abstract class for raster data strucutures.
Definition: Raster.h:71
std::map< RasterThreshold, te::color::RGBAColor > CategorizedMap
void setCategorizedMap(CategorizedMap map)
URI C++ Library.
double m_transp
Transparency factor.
#define TEMAPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:60
std::vector< double > m_contrastOffsets2
A vector of offsets2 for each band used by the CONTRAST method.
short m_monoBandOut
Value for output gray band channel.
std::map< int, te::color::RGBAColor > RecodedMap
void applyLinearContrast(const unsigned int &bandIdx, const double &inputValue, double &outputValue) const
void apply(double icol, double ilin, double ocol, double olin)
TransformFunction m_transfFuncPtr
Function used in transformation operation.
te::rst::Raster * m_rasterOut
Pointer to a output raster.
std::map< RasterThreshold, te::color::ColorBar > InterpolatedMap
std::vector< double > m_gamma
A vector of gamma gains for each band.
RGBChannels
The three channels of a display.
InterpolatedMap m_interpolateMap
Attribute to define the interpolated transformation.
void setInterpolatedMap(InterpolatedMap map)
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
short m_monoBand
Value for the gray band.
A Raster Transform is a class that defines functions to transform a styled raster.
std::pair< double, double > RasterThreshold
CategorizedMap & getCategorizedMap()
std::map< RGBChannels, short > & getRGBMap()
std::map< RGBChannels, short > m_rgbMap
Map used to set the band order.
CategorizedMap m_categorizeMap
Attribute to define the categorized transformation.
std::vector< double > m_rstMaxValue
Max value from input raster.