All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RasterTransform.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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 // TerraLib
27 #include "RasterTransform.h"
28 
29 #include "../raster.h"
30 #include "../raster/RasterProperty.h"
31 #include "../se/RasterSymbolizer.h"
32 
33 // STL
34 #include <limits>
35 
37  m_rasterIn(input),
38  m_rasterOut(output),
39  m_transp(TE_OPAQUE),
40  m_gain(1.),
41  m_offset(0.),
42  m_rContrast(TE_SE_DEFAULT_GAMMA_VALUE),
43  m_gContrast(TE_SE_DEFAULT_GAMMA_VALUE),
44  m_bContrast(TE_SE_DEFAULT_GAMMA_VALUE),
45  m_mContrast(TE_SE_DEFAULT_GAMMA_VALUE),
46  m_monoBand(0),
47  m_monoBandOut(0),
48  m_transfFuncPtr(&RasterTransform::setExtractRGB),
49  m_RGBAFuncPtr(&RasterTransform::getExtractRGB)
50 {
51  m_rstMinValue = -std::numeric_limits<double>::max();
52  m_rstMaxValue = std::numeric_limits<double>::max();
53 }
54 
56 {
57  m_categorizeMap.clear();
58  m_interpolateMap.clear();
59  m_rgbMap.clear();
60 }
61 
62 void te::map::RasterTransform::setRGBMap(std::map<RGBChannels, short>& rgbMap)
63 {
64  m_rgbMap.clear();
65  m_rgbMap[RED_CHANNEL] = -1;
66  m_rgbMap[GREEN_CHANNEL] = -1;
67  m_rgbMap[BLUE_CHANNEL] = -1;
68 
69  std::map<RGBChannels,short>::iterator it = m_rgbMap.begin();
70  while (it != m_rgbMap.end())
71  {
72  if (it->first == RED_CHANNEL)
73  m_rgbMap[RED_CHANNEL] = it->second;
74 
75  else if (it->first == GREEN_CHANNEL)
76  m_rgbMap[GREEN_CHANNEL] = it->second;
77 
78  else if (it->first == BLUE_CHANNEL)
79  m_rgbMap[BLUE_CHANNEL] = it->second;
80 
81  ++it;
82  }
83 }
84 
85 void te::map::RasterTransform::setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
86 {
87  m_rstMinValue = rmin;
88  m_rstMaxValue = rmax;
89 
90  if( vmax == vmin )
91  {
92  m_gain = 0.0;
93  m_offset = 0.0;
94  }
95  else
96  {
97  m_gain = (double)(rmax-rmin)/(vmax-vmin);
98  m_offset = -1*m_gain*vmin+rmin;
99  }
100 }
101 
103 {
104  if (m_transfFuncPtr == 0 || m_RGBAFuncPtr)
105  {
106  return NO_TRANSF;
107  }
108  else if (m_transfFuncPtr == &RasterTransform::setMono2ThreeBand &&
109  m_RGBAFuncPtr == &RasterTransform::getMono2ThreeBand)
110  {
111  return MONO2THREE_TRANSF;
112  }
113  else if (m_transfFuncPtr == &RasterTransform::setExtractRGB &&
114  m_RGBAFuncPtr == &RasterTransform::getExtractRGB)
115  {
116  return EXTRACT2RGB_TRANSF;
117  }
118  else if (m_transfFuncPtr == &RasterTransform::setRed2ThreeBand &&
119  m_RGBAFuncPtr == &RasterTransform::getRed2ThreeBand)
120  {
121  return RED2THREE_TRANSF;
122  }
123  else if (m_transfFuncPtr == &RasterTransform::setGreen2ThreeBand &&
124  m_RGBAFuncPtr == &RasterTransform::getGreen2ThreeBand)
125  {
126  return GREEN2THREE_TRANSF;
127  }
128  else if (m_transfFuncPtr == &RasterTransform::setBlue2ThreeBand &&
129  m_RGBAFuncPtr == &RasterTransform::getBlue2ThreeBand)
130  {
131  return BLUE2THREE_TRANSF;
132  }
133  else if (m_transfFuncPtr == &RasterTransform::setCategorize &&
134  m_RGBAFuncPtr == &RasterTransform::getCategorize)
135  {
136  return CATEGORIZE_TRANSF;
137  }
138  else if (m_transfFuncPtr == &RasterTransform::setInterpolate &&
139  m_RGBAFuncPtr == &RasterTransform::getInterpolate)
140  {
141  return INTERPOLATE_TRANSF;
142  }
143  else if (m_transfFuncPtr == &RasterTransform::setBand2Band)
144  {
145  return BAND2BAND_TRANSF;
146  }
147  else
148  {
149  return NO_TRANSF;
150  }
151 }
152 
154 {
155  if (func == MONO2THREE_TRANSF)
156  {
157  m_transfFuncPtr = &RasterTransform::setMono2ThreeBand;
158  m_RGBAFuncPtr = &RasterTransform::getMono2ThreeBand;
159  }
160  else if (func == EXTRACT2RGB_TRANSF)
161  {
162  m_transfFuncPtr = &RasterTransform::setExtractRGB;
163  m_RGBAFuncPtr = &RasterTransform::getExtractRGB;
164  }
165  else if (func == RED2THREE_TRANSF)
166  {
167  m_transfFuncPtr = &RasterTransform::setRed2ThreeBand;
168  m_RGBAFuncPtr = &RasterTransform::getRed2ThreeBand;
169  }
170  else if (func == GREEN2THREE_TRANSF)
171  {
172  m_transfFuncPtr = &RasterTransform::setGreen2ThreeBand;
173  m_RGBAFuncPtr = &RasterTransform::getGreen2ThreeBand;
174  }
175  else if (func == BLUE2THREE_TRANSF)
176  {
177  m_transfFuncPtr = &RasterTransform::setBlue2ThreeBand;
178  m_RGBAFuncPtr = &RasterTransform::getBlue2ThreeBand;
179  }
180  else if (func == CATEGORIZE_TRANSF)
181  {
182  m_transfFuncPtr = &RasterTransform::setCategorize;
183  m_RGBAFuncPtr = &RasterTransform::getCategorize;
184  }
185  else if (func == INTERPOLATE_TRANSF)
186  {
187  m_transfFuncPtr = &RasterTransform::setInterpolate;
188  m_RGBAFuncPtr = &RasterTransform::getInterpolate;
189  }
190  else if (func == BAND2BAND_TRANSF)
191  {
192  m_transfFuncPtr = &RasterTransform::setBand2Band;
193  m_RGBAFuncPtr = 0;
194  }
195  else
196  {
197  m_transfFuncPtr = 0;
198  m_RGBAFuncPtr = 0;
199  }
200 }
201 
202 void te::map::RasterTransform::setMono2ThreeBand(double icol, double ilin, double ocol, double olin)
203 {
204  double val;
205 
206  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
207 
208  if(checkNoValue(val, m_monoBand) == false)
209  {
210  val = (val * m_gain + m_offset) * m_mContrast;
211 
212  fixValue(val);
213 
214  std::vector<double> vecValues;
215  vecValues.resize(3, val);
216 
217  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
218  }
219 }
220 
222 {
223  double val;
224 
225  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
226 
227  if(checkNoValue(val, m_monoBand) == false)
228  {
229  val = (val * m_gain + m_offset) * m_mContrast;
230 
231  fixValue(val);
232 
233  te::color::RGBAColor c(static_cast<int>(val), static_cast<int>(val), static_cast<int>(val), static_cast<int>(m_transp));
234 
235  return c;
236  }
237 
238  return te::color::RGBAColor();
239 }
240 
241 void te::map::RasterTransform::setExtractRGB(double icol, double ilin, double ocol, double olin)
242 {
243  double valR, valG, valB;
244 
245  m_rasterIn->getValue((int)icol, (int)ilin, valR, m_rgbMap[RED_CHANNEL]);
246  m_rasterIn->getValue((int)icol, (int)ilin, valG, m_rgbMap[GREEN_CHANNEL]);
247  m_rasterIn->getValue((int)icol, (int)ilin, valB, m_rgbMap[BLUE_CHANNEL]);
248 
249  if(checkNoValue(valR, m_rgbMap[RED_CHANNEL]) == false ||
250  checkNoValue(valG, m_rgbMap[GREEN_CHANNEL]) == false ||
251  checkNoValue(valB, m_rgbMap[BLUE_CHANNEL]) == false)
252  {
253  std::vector<double> vecValues;
254 
255  valR = (valR * m_gain + m_offset) * m_rContrast;
256  fixValue(valR);
257  vecValues.push_back(valR);
258 
259  valG = (valG * m_gain + m_offset) * m_gContrast;
260  fixValue(valG);
261  vecValues.push_back(valG);
262 
263  valB = (valB * m_gain + m_offset) * m_bContrast;
264  fixValue(valB);
265  vecValues.push_back(valB);
266 
267  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
268  }
269 }
270 
272 {
273  double valR, valG, valB;
274 
275  m_rasterIn->getValue((int)icol, (int)ilin, valR, m_rgbMap[RED_CHANNEL]);
276  m_rasterIn->getValue((int)icol, (int)ilin, valG, m_rgbMap[GREEN_CHANNEL]);
277  m_rasterIn->getValue((int)icol, (int)ilin, valB, m_rgbMap[BLUE_CHANNEL]);
278 
279  if(checkNoValue(valR, m_rgbMap[RED_CHANNEL]) == false ||
280  checkNoValue(valG, m_rgbMap[GREEN_CHANNEL]) == false ||
281  checkNoValue(valB, m_rgbMap[BLUE_CHANNEL]) == false)
282  {
283  valR = (valR * m_gain + m_offset) * m_rContrast;
284  fixValue(valR);
285 
286  valG = (valG * m_gain + m_offset) * m_gContrast;
287  fixValue(valG);
288 
289  valB = (valB * m_gain + m_offset) * m_bContrast;
290  fixValue(valB);
291 
292  te::color::RGBAColor c(static_cast<int>(valR), static_cast<int>(valG), static_cast<int>(valB), static_cast<int>(m_transp));
293 
294  return c;
295  }
296 
297  return te::color::RGBAColor();
298 }
299 
300 void te::map::RasterTransform::setRed2ThreeBand(double icol, double ilin, double ocol, double olin)
301 {
302  double val;
303 
304  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[RED_CHANNEL]);
305 
306  if(checkNoValue(val, m_rgbMap[RED_CHANNEL]) == false)
307  {
308  val = (val * m_gain + m_offset) * m_rContrast;
309 
310  fixValue(val);
311 
312  std::vector<double> vecValues;
313  vecValues.push_back(val);
314  vecValues.push_back(0.);
315  vecValues.push_back(0.);
316 
317  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
318  }
319 }
320 
322 {
323  double val;
324 
325  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[RED_CHANNEL]);
326 
327  if(checkNoValue(val, m_rgbMap[RED_CHANNEL]) == false)
328  {
329  val = (val * m_gain + m_offset) * m_rContrast;
330 
331  fixValue(val);
332 
333  te::color::RGBAColor c(static_cast<int>(val), static_cast<int>(0.), static_cast<int>(0.), static_cast<int>(m_transp));
334 
335  return c;
336 
337  }
338 
339  return te::color::RGBAColor();
340 }
341 
342 void te::map::RasterTransform::setGreen2ThreeBand(double icol, double ilin, double ocol, double olin)
343 {
344  double val;
345 
346  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[GREEN_CHANNEL]);
347 
348  if(checkNoValue(val, m_rgbMap[GREEN_CHANNEL]) == false)
349  {
350  val = (val * m_gain + m_offset) * m_gContrast;
351 
352  fixValue(val);
353 
354  std::vector<double> vecValues;
355  vecValues.push_back(0.);
356  vecValues.push_back(val);
357  vecValues.push_back(0.);
358 
359  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
360  }
361 }
362 
364 {
365  double val;
366 
367  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[GREEN_CHANNEL]);
368 
369  if(checkNoValue(val, m_rgbMap[GREEN_CHANNEL]) == false)
370  {
371  val = (val * m_gain + m_offset) * m_gContrast;
372 
373  fixValue(val);
374 
375  te::color::RGBAColor c(static_cast<int>(0.), static_cast<int>(val), static_cast<int>(0.), static_cast<int>(m_transp));
376 
377  return c;
378 
379  }
380 
381  return te::color::RGBAColor();
382 }
383 
384 void te::map::RasterTransform::setBlue2ThreeBand(double icol, double ilin, double ocol, double olin)
385 {
386  double val;
387 
388  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[BLUE_CHANNEL]);
389 
390  if(checkNoValue(val, m_rgbMap[BLUE_CHANNEL]) == false)
391  {
392  val = (val * m_gain + m_offset) * m_bContrast;
393 
394  fixValue(val);
395 
396  std::vector<double> vecValues;
397  vecValues.push_back(0.);
398  vecValues.push_back(0.);
399  vecValues.push_back(val);
400 
401  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
402  }
403 }
404 
406 {
407  double val;
408 
409  m_rasterIn->getValue((int)icol, (int)ilin, val, m_rgbMap[BLUE_CHANNEL]);
410 
411  if(checkNoValue(val, m_rgbMap[BLUE_CHANNEL]) == false)
412  {
413  val = (val * m_gain + m_offset) * m_bContrast;
414 
415  fixValue(val);
416 
417  te::color::RGBAColor c(static_cast<int>(0.), static_cast<int>(0.), static_cast<int>(val), static_cast<int>(m_transp));
418 
419  return c;
420 
421  }
422 
423  return te::color::RGBAColor();
424 }
425 
426 void te::map::RasterTransform::setCategorize(double icol, double ilin, double ocol, double olin)
427 {
428  double val;
429 
430  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
431 
432  te::color::RGBAColor c= getCategorizedColor(val);
433 
434  std::vector<double> vecValues;
435  vecValues.push_back(c.getRed());
436  vecValues.push_back(c.getGreen());
437  vecValues.push_back(c.getBlue());
438 
439  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
440 }
441 
443 {
444  double val;
445 
446  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
447 
448  return getCategorizedColor(val);
449 }
450 
451 void te::map::RasterTransform::setInterpolate(double icol, double ilin, double ocol, double olin)
452 {
453  double val;
454 
455  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
456 
457  te::color::RGBAColor c= getInterpolatedColor(val);
458 
459  std::vector<double> vecValues;
460  vecValues.push_back(c.getRed());
461  vecValues.push_back(c.getGreen());
462  vecValues.push_back(c.getBlue());
463 
464  m_rasterOut->setValues((int)ocol, (int)olin, vecValues);
465 }
466 
468 {
469  double val;
470 
471  m_rasterIn->getValue((int)icol, (int)ilin, val, m_monoBand);
472 
473  return getInterpolatedColor(val);
474 }
475 
476 void te::map::RasterTransform::setBand2Band(double icol, double ilin, double ocol, double olin)
477 {
478  double val;
479 
480  for(std::size_t t = 0; t < m_rasterIn->getNumberOfBands(); ++t)
481  {
482  m_rasterIn->getValue((int)icol, (int)ilin, val, t);
483 
484  if(checkNoValue(val, t) == false)
485  {
486  val = (val * m_gain + m_offset);
487 
488  fixValue(val);
489 
490  m_rasterOut->setValue((int)ocol, (int)olin, val, t);
491  }
492  }
493 }
494 
496 {
497  if (value < m_rstMinValue)
498  {
499  value = m_rstMinValue;
500  }
501  else if (value > m_rstMaxValue)
502  {
503  value = m_rstMaxValue;
504  }
505 }
506 
507 bool te::map::RasterTransform::checkNoValue(double& value, int band)
508 {
509  if(m_rasterIn->getBand(band)->getProperty()->m_noDataValue == value)
510  {
511  return true;
512  }
513 
514  return false;
515 }
516 
518 {
519  InterpolatedMap::iterator it = m_interpolateMap.begin();
520 
521  while(it != m_interpolateMap.end())
522  {
523  RasterThreshold rt = it->first;
524 
525  if(rt.first<= value && rt.second > value)
526  {
527  int distance = int(value - it->first.first);
528 
529  if(distance < 0)
530  {
531  distance = 0;
532  }
533  else if(distance >= (int)it->second.getColorBar().size())
534  {
535  distance = it->second.getColorBar().size() - 1;
536  }
537 
538  return it->second.getColorBar()[distance];
539  }
540 
541  ++it;
542  }
543 
544  return te::color::RGBAColor();
545 }
546 
548 {
549  CategorizedMap::iterator it = m_categorizeMap.begin();
550 
551  while(it != m_categorizeMap.end())
552  {
553  RasterThreshold rt = it->first;
554 
555  if(rt.first<= value && rt.second > value)
556  {
557  return it->second;
558  }
559 
560  ++it;
561  }
562 
563  return te::color::RGBAColor();
564 }
void setRed2ThreeBand(double icol, double ilin, double ocol, double olin)
double m_rstMinValue
Min value from input raster.
void fixValue(double &value)
void setBand2Band(double icol, double ilin, double ocol, double olin)
RasterTransform(te::rst::Raster *input, te::rst::Raster *output)
Constructor.
void setTransfFunction(RasterTransfFunctions func)
te::color::RGBAColor getCategorizedColor(double value)
double m_rstMaxValue
Max value from input raster.
#define TE_SE_DEFAULT_GAMMA_VALUE
It specifies the default gamma value.
Definition: Config.h:107
te::color::RGBAColor getInterpolatedColor(double value)
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
te::color::RGBAColor getRed2ThreeBand(double icol, double ilin)
void setMono2ThreeBand(double icol, double ilin, double ocol, double olin)
void setRGBMap(std::map< RGBChannels, short > &rgbMap)
Sets the rgb map values.
std::pair< double, double > RasterThreshold
RasterTransfFunctions getTransfFunction()
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void setBlue2ThreeBand(double icol, double ilin, double ocol, double olin)
void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
Set parameters of linear transformation.
void setExtractRGB(double icol, double ilin, double ocol, double olin)
te::color::RGBAColor getCategorize(double icol, double ilin)
te::color::RGBAColor getBlue2ThreeBand(double icol, double ilin)
te::color::RGBAColor getMono2ThreeBand(double icol, double ilin)
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void setGreen2ThreeBand(double icol, double ilin, double ocol, double olin)
te::color::RGBAColor getInterpolate(double icol, double ilin)
bool checkNoValue(double &value, int band)
Function used to check if value is or not a valid value.
void setInterpolate(double icol, double ilin, double ocol, double olin)
A Raster Transform is a class that defines functions to transform a styled raster.
An abstract class for raster data strucutures.
Definition: Raster.h:70
void setCategorize(double icol, double ilin, double ocol, double olin)
A Raster Transform is a class that defines functions to transform a styled raster.
te::color::RGBAColor getGreen2ThreeBand(double icol, double ilin)
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
te::color::RGBAColor getExtractRGB(double icol, double ilin)
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295