TerraLib 4.1
E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeRasterTransform.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00026 #ifndef  __TERRALIB_INTERNAL_RASTERTRANSFORM_H
00027 #define  __TERRALIB_INTERNAL_RASTERTRANSFORM_H
00028 
00029 #include "TeRaster.h"
00030 #include "TeLegendEntry.h"
00031 
00032 #include <map>
00033 #include <vector>
00034 using namespace std;
00035 
00037 class TL_DLL TeRasterTransform
00038 {
00039 public:
00040 
00042            enum TeRGBChannels { TeREDCHANNEL=0, TeGREENCHANNEL=1, TeBLUECHANNEL=2 };
00043 
00044            enum TeRasterTransfFunctions
00045            {          TeNoTransf=0, TeMono2Three=1, TeBand2Band=2, TePall2Three=3, TeLUT2Three=4, 
00046                       TeExtractRGB=5, TeExtractBand=6, TeExtractBands=7, TeThreeBand2RGB=8, 
00047                       TeThree2LUTThreeBand=9, TeMono2LUTMonoBand=10
00048            };
00049 
00051            typedef void (TeRasterTransform::*TransformFunction)(double, double, double, double);
00052 
00053 private:
00054 
00055            TeRaster* rasterIn_;                        
00056            TeRaster* rasterOut_;
00057 
00058            double gain_, offset_;
00059 
00060            map<TeRGBChannels,short> rgbmap_;
00061 
00062            short mband_, mbout_;
00063 
00064            TransformFunction transfFuncPtr_;
00065 
00066            double rmin_, rmax_;
00067 
00068            int lutSize_;
00069 
00070            double contrastR_, contrastG_, contrastB_;
00071            double contrastM_;
00072 
00073            unsigned int transp_;
00074 
00075            std::string lutTable_;
00076 
00077 public:
00078 
00079            vector<unsigned char> lutr_;
00080            vector<unsigned char> lutg_;
00081            vector<unsigned char> lutb_;
00082 
00084            TeRasterTransform(TeRaster* rIn=0, TeRaster* rOut=0):  
00085              rasterIn_(rIn),
00086              rasterOut_(rOut),
00087              gain_(1),
00088              offset_(0),
00089              mband_(0), 
00090              mbout_(0),
00091              transfFuncPtr_(&TeRasterTransform::Band2Band),
00092              rmax_(TeMAXFLOAT),
00093              lutSize_(0),
00094              contrastR_(1.0),
00095              contrastG_(1.0),
00096              contrastB_(1.0),
00097              contrastM_(1.0),
00098              transp_(255),
00099              lutTable_("")
00100              { 
00101                         rmin_ = -1 * TeMAXFLOAT;
00102             }
00103 
00105            ~TeRasterTransform()
00106            {
00107                       lutr_.clear();
00108                       lutb_.clear();
00109                       lutg_.clear();
00110                       rgbmap_.clear();
00111                       transfFuncPtr_ = 0;
00112            }
00113 
00115            void setLutSize(int n)
00116            {          lutSize_ = n; }
00117 
00119            int getLutSize()
00120            {          return lutSize_; }
00121 
00123            void setRasterIn(TeRaster* rIn)
00124            {          rasterIn_ = rIn; }
00125 
00127            void setRasterOut(TeRaster* rOut)
00128            {          rasterOut_ = rOut; }
00129            
00131            void setGain(double g) 
00132            {          gain_= g;  }
00133 
00135            double getGain() const
00136            {          return gain_;         }
00137 
00139            void setOffset(double o) 
00140            {          offset_= o;           }
00141 
00143            double getOffset() const
00144            {          return offset_;       }
00145 
00147            void setTransfFunction(TeRasterTransform::TransformFunction transfFuncPtr)
00148            {          transfFuncPtr_ = transfFuncPtr; }
00149 
00151            void setRGBmap (map<TeRGBChannels,short>& rgbmap)
00152            {
00153                       rgbmap_.clear();
00154                       rgbmap_[TeREDCHANNEL] = -1;
00155                       rgbmap_[TeGREENCHANNEL] = -1;
00156                       rgbmap_[TeBLUECHANNEL] = -1;
00157 
00158                       map<TeRGBChannels,short>::iterator it = rgbmap.begin();
00159                       while (it != rgbmap.end())
00160                       {
00161                                  if (it->first == TeREDCHANNEL)
00162                                             rgbmap_[TeREDCHANNEL] = it->second;
00163 
00164                                  else if (it->first == TeGREENCHANNEL)
00165                                             rgbmap_[TeGREENCHANNEL] = it->second;
00166 
00167                                  else if (it->first == TeBLUECHANNEL)
00168                                             rgbmap_[TeBLUECHANNEL] = it->second;
00169                                  ++it;
00170                       }
00171            }
00172 
00174            void setBChannelMapping(short bIn, TeRGBChannels bOut)
00175            {
00176                       rgbmap_[bOut] = bIn;
00177            }
00178 
00180            void clearRGBMap()
00181            {
00182                       rgbmap_.clear();
00183            }
00184 
00186            map<TeRGBChannels,short>& getRGBMap()
00187            {
00188                       return rgbmap_;
00189            }
00190 
00192            void setSrcBand(short n)
00193            { mband_ = n; }
00194 
00196            short getSrcBand()
00197            {          return mband_; }
00198 
00200            void setDestBand(short n)
00201            { mbout_ = n; }
00202 
00204            short getDestBand()
00205            {          return mbout_;        }
00206            
00208            void generateLUT(TeLegendEntryVector& legs, unsigned int nentries, TeColor& backColor);
00209 
00211 
00217            void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
00218            {
00219                       rmin_ = rmin;
00220                       rmax_ = rmax;
00221                       gain_ = (double)(rmax-rmin)/(vmax-vmin);
00222                       offset_ = -1*gain_*vmin+rmin;
00223            }
00224 
00225 //--- Simple contrast linear factors
00226            void setContrastR(double cR)
00227            {          contrastR_ = cR;      }
00228            void setContrastG(double cG)
00229            {          contrastG_ = cG;      }
00230            void setContrastB(double cB)
00231            {          contrastB_ = cB;      }
00232            void setContrastM(double cM)
00233            {          contrastM_ = cM;      }
00234 
00235            double getContrastR()
00236            {          return contrastR_;    }
00237            double getContrastG()
00238            {          return contrastG_;    }
00239            double getContrastB()
00240            {          return contrastB_;    }
00241            double getContrastM()
00242            {          return contrastM_;    }
00243 
00244 
00245            void setTransparency(unsigned int transp)
00246            {          transp_ = transp; }
00247            unsigned int getTransparency()
00248            {          return transp_;       }
00249 
00250 
00251 //-- Lut Table
00252            void setLutTableName(const std::string& tableName)
00253            {          lutTable_ = tableName; }
00254            std::string getLutTableName()
00255            {          return lutTable_; }
00256 
00257 //---
00258 
00260            void apply(double icol, double ilin, double ocol, double olin)
00261            {          (this->*transfFuncPtr_)(icol,ilin,ocol,olin); }
00262 
00264            TeRasterTransfFunctions getTransfFunction();
00265 
00267            void setTransfFunction(TeRasterTransfFunctions func);
00268 
00269 // --- The transformation functions  available ----
00271            void Mono2ThreeBand(double icol, double ilin, double ocol, double olin)
00272            {
00273                       double val;
00274                       if (rasterIn_->getElement((int)icol,(int)ilin,val,mband_))
00275                       {
00276                                  val = (val*gain_ + offset_)*contrastM_;
00277                                  if (val < rmin_)
00278                                             val = rmin_;
00279                                  else if (val > rmax_)
00280                                             val = rmax_;
00281                                  rasterOut_->setElement((int)ocol,(int)olin, val, val, val, transp_);
00282                       }
00283            }
00284 
00286            void Band2Band(double icol, double ilin, double ocol, double olin)
00287            {
00288                       double val;
00289                       int n = rasterOut_->params().nBands();
00290                       for (int i=0; i<n; i++)
00291                                  if (rasterIn_->getElement((int)icol,(int)ilin,val,i))
00292                                  {
00293                                             val = val*gain_ + offset_;
00294                                             if (val < rmin_)
00295                                                        val = rmin_;
00296                                             else if (val > rmax_)
00297                                                        val = rmax_;
00298                                             rasterOut_->setElement((int)ocol,(int)olin,val,i);
00299                                  }
00300            }
00301 
00303            void Pallete2ThreeBand(double icol, double ilin, double ocol, double olin)
00304            {
00305                       double val;
00306                       if (rasterIn_->getElement((int)icol,(int)ilin,val))
00307                       {
00308                                  if (val>= 0 && (unsigned int)val < rasterIn_->params().lutr_.size())
00309                                  {
00310                                             rasterOut_->setElement((int)ocol,(int)olin,rasterIn_->params().lutr_[(int)val],
00311                                                                                                                                                              rasterIn_->params().lutg_[(int)val],
00312                                                                                                                                                              rasterIn_->params().lutb_[(int)val], transp_);
00313                                  }
00314                       }
00315            }
00316 
00318            void ThreeBand2RGB(double icol, double ilin, double ocol, double olin)
00319            {
00320                       double valR, valG, valB;
00321                       bool flag = rasterIn_->getElement((int)icol,(int)ilin,valR,0);
00322                       flag = rasterIn_->getElement((int)icol,(int)ilin,valG,1) || flag;
00323                       flag = rasterIn_->getElement((int)icol,(int)ilin,valB,2) || flag;
00324 
00325                       if (flag)
00326                                  rasterOut_->setElement((int)ocol,(int)olin, (valR*gain_ + offset_)*contrastR_, 
00327                                                                                                                                                           (valG*gain_ + offset_)*contrastG_, 
00328                                                                                                                                                           (valB*gain_ + offset_)*contrastB_, transp_);
00329            }
00330 
00332            void LUT2ThreeBand(double icol, double ilin, double ocol, double olin)
00333            {
00334                       double idx, val; 
00335                       if (rasterIn_->getElement((int)icol,(int)ilin,val,mband_))
00336                       {
00337                                  idx = (val*gain_) + offset_;
00338                                  if (idx >= 0 && idx < lutSize_)
00339                                  {
00340                                             rasterOut_->setElement((int)ocol,(int)olin,lutr_[(int)idx]*contrastR_,
00341                                                                                                                                                              lutg_[(int)idx]*contrastG_,
00342                                                                                                                                                              lutb_[(int)idx]*contrastB_, transp_);
00343                                  }
00344                       }
00345            }
00346 
00347            
00348            void ThreeBand2LUTThreeBand(double icol, double ilin, double ocol, double olin)
00349            {
00350                       double idxR, valR;
00351                       double idxG, valG;
00352                       double idxB, valB;
00353                       double outR = 0.;
00354                       double outG = 0.;
00355                       double outB = 0.;
00356 
00357                       if(rgbmap_.empty())
00358                       {
00359                                  return;
00360                       }
00361 
00362                       bool flag = rasterIn_->getElement((int)icol,(int)ilin,valR,rgbmap_[TeREDCHANNEL]);
00363 
00364                       idxR = (valR*gain_) + offset_;
00365                       if (idxR >= 0 && idxR < lutSize_)
00366                       {
00367                                  outR = lutr_[(int)idxR]*contrastR_;
00368                       }
00369 
00370                       flag = rasterIn_->getElement((int)icol,(int)ilin,valG,rgbmap_[TeGREENCHANNEL]) || flag;
00371 
00372                       idxG = (valG*gain_) + offset_;
00373                       if (idxG >= 0 && idxG < lutSize_)
00374                       {
00375                                  outG = lutg_[(int)idxG]*contrastG_;
00376                       }
00377 
00378                       flag = rasterIn_->getElement((int)icol,(int)ilin,valB,rgbmap_[TeBLUECHANNEL]) || flag;
00379 
00380                       idxB = (valB*gain_) + offset_;
00381                       if (idxB >= 0 && idxB < lutSize_)
00382                       {
00383                                  outB = lutb_[(int)idxB]*contrastB_;
00384                       }
00385 
00386                       if (flag)
00387                                  rasterOut_->setElement((int)ocol,(int)olin, outR, outG, outB, transp_);
00388 
00389            }
00390 
00391            void MonoBand2LUTMonoBand(double icol, double ilin, double ocol, double olin)
00392            {
00393                       double idxMono, valMono;
00394                       double outMono = 0.;
00395 
00396                       bool flag = rasterIn_->getElement((int)icol,(int)ilin,valMono,mband_);
00397 
00398                       if(flag)
00399                       {
00400                                  idxMono = (valMono*gain_) + offset_;
00401                                  if (idxMono >= 0 && idxMono < lutSize_)
00402                                             outMono = lutr_[(int)idxMono]*contrastM_;
00403 
00404                                  if(rasterOut_->params().nBands() == 1)
00405                                             rasterOut_->setElement((int)ocol,(int)olin, outMono);
00406                                  else
00407                                             rasterOut_->setElement((int)ocol,(int)olin, outMono, outMono, outMono, transp_);
00408                       }
00409            }
00410 
00411            // This transformation is used to define a particular mapping from input bands to RGB channels
00412            void ExtractRGB(double icol, double ilin, double ocol, double olin)
00413            {
00414                       double valR, valG, valB;
00415                       bool flag = rasterIn_->getElement((int)icol,(int)ilin,valR,rgbmap_[TeREDCHANNEL]);
00416                            flag = rasterIn_->getElement((int)icol,(int)ilin,valG,rgbmap_[TeGREENCHANNEL]) || flag;
00417                            flag = rasterIn_->getElement((int)icol,(int)ilin,valB,rgbmap_[TeBLUECHANNEL]) || flag;
00418 
00419                       if (flag)
00420                                  rasterOut_->setElement((int)ocol,(int)olin, (valR*gain_ + offset_)*contrastR_, 
00421                                                                                                                                                           (valG*gain_ + offset_)*contrastG_, 
00422                                                                                                                                                           (valB*gain_ + offset_)*contrastB_, transp_);
00423            }
00424 
00426            void ExtractBand(double icol, double ilin, double ocol, double olin)
00427            {
00428                       double val;
00429                       if (rasterIn_->getElement((int)icol,(int)ilin,val,mband_))
00430                       {
00431                                  val = val*gain_ + offset_;
00432                                  if (val < rmin_)
00433                                             val = rmin_;
00434                                  else if (val > rmax_)
00435                                             val = rmax_;
00436                                  rasterOut_->setElement((int)ocol,(int)olin,val*contrastM_,mbout_);
00437                       }
00438            }
00439 
00440            // This transformation extracts selected bands of the input raster and 
00441            // writes each to a particular band of the output
00442            void ExtractBands(double icol, double ilin, double ocol, double olin)
00443            {
00444                       double val;
00445                       int bOut;
00446                       if (rgbmap_[TeREDCHANNEL] != -1)
00447                       {
00448                                  bOut = 0;
00449                                  if (rasterIn_->getElement((int)icol,(int)ilin,val,rgbmap_[TeREDCHANNEL]))
00450                                             rasterOut_->setElement((int)ocol,(int)olin,val*contrastR_,bOut);
00451                       }
00452                       if (rgbmap_[TeGREENCHANNEL] != -1)
00453                       {
00454                                  bOut = 1;
00455                                  if (rasterIn_->getElement((int)icol,(int)ilin,val,rgbmap_[TeGREENCHANNEL]))
00456                                             rasterOut_->setElement((int)ocol,(int)olin,val*contrastG_,bOut);
00457                       }
00458                       if (rgbmap_[TeBLUECHANNEL] != -1)
00459                       {
00460                                  bOut = 2;
00461                                  if (rasterIn_->getElement((int)icol,(int)ilin,val,rgbmap_[TeBLUECHANNEL]))
00462                                             rasterOut_->setElement((int)ocol,(int)olin,val*contrastB_,bOut);
00463                       }
00464            }
00465 
00466 };
00467 #endif
00468 
00469 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines