USGS

Isis 3.0 Developer's Reference (API)

Home

ProcessByBrick.h

Go to the documentation of this file.
00001 #ifndef ProcessByBrick_h
00002 #define ProcessByBrick_h
00003 
00024 #include "Brick.h"
00025 #include "Buffer.h"
00026 #include "Cube.h"
00027 #include "Process.h"
00028 
00029 using namespace std;
00030 
00031 namespace Isis {
00075   class ProcessByBrick : public Isis::Process {
00076     
00077     private:
00078       bool p_wrapOption;    
00079       bool p_inputBrickSizeSet;  
00081       bool p_outputBrickSizeSet; 
00083       std::vector<int> p_inputBrickSamples;  
00085       std::vector<int> p_inputBrickLines;    
00087       std::vector<int> p_inputBrickBands;    
00089       std::vector<int> p_outputBrickSamples; 
00091       std::vector<int> p_outputBrickLines;   
00093       std::vector<int> p_outputBrickBands;   
00096     private:
00097       std::vector<int> CalculateMaxDimensions(std::vector<Cube *> cubes) const;
00098 
00099     public:
00101       ProcessByBrick();
00102 
00104       ~ProcessByBrick() {};
00105 
00106       Isis::Cube *SetInputCube(const std::string &parameter,
00107                                const int requirements = 0);
00108 
00109       Isis::Cube *SetInputCube(const std::string &fname,
00110                                const Isis::CubeAttributeInput &att,
00111                                const int requirements = 0);
00112 
00113       void SetBrickSize(const int ns, const int nl, const int nb);
00114 
00115       void SetInputBrickSize(const int ns, const int nl, const int nb);
00116       void SetInputBrickSize(const int ns, const int nl, const int nb, 
00117                              const int tile);
00118 
00119       void SetOutputBrickSize(const int ns, const int nl, const int nb);
00120       void SetOutputBrickSize(const int ns, const int nl, const int nb, 
00121                               const int tile);
00122 
00134       void SetWrap(bool wrap) {
00135         p_wrapOption = wrap;
00136       };
00137 
00143       bool Wraps() {
00144         return p_wrapOption;
00145       };
00146 
00147       void StartProcess(void funct(Isis::Buffer &in));
00148       void StartProcess(void funct(Isis::Buffer &in, Isis::Buffer &out));
00149       void StartProcess(void funct(std::vector<Isis::Buffer *> &in,
00150                                    std::vector<Isis::Buffer *> &out));
00151       void EndProcess();
00152       
00156       bool ProcessInPlace(Isis::Cube **cube, Isis::Brick **bricks);
00157       
00167       template <typename Functor> 
00168       void StartProcessInPlace(Functor & funct) {
00169         Isis::Cube *cube=NULL;
00170         Isis::Brick *bricks=NULL;
00171         
00172         bool haveInput = ProcessInPlace(&cube, &bricks);
00173         
00174         p_progress->SetMaximumSteps(bricks->Bricks());
00175         p_progress->CheckStatus();
00176 
00177         // Loop and let the app programmer work with the bricks
00178         for(bricks->begin(); !bricks->end(); (*bricks)++) {
00179           if(haveInput) {
00180             cube->read(*bricks);  // input only
00181           }
00182           funct(*bricks);
00183           if((!haveInput) || (cube->isReadWrite())){
00184             cube->write(*bricks);  // output only or input/output
00185           }
00186           p_progress->CheckStatus();
00187 
00188           }
00189         delete bricks;
00190       }
00191       
00195       int ProcessIO(Isis::Brick **ibrick, Isis::Brick **obrick);
00196       
00208       template <typename FunctorIO>
00209       void StartProcessIO(FunctorIO & funct) {
00210 
00211         Isis::Brick *ibrick=NULL, *obrick=NULL;
00212     
00213         int numBricks = ProcessIO(&ibrick, &obrick);
00214     
00215         // Loop and let the app programmer work with the bricks
00216         p_progress->SetMaximumSteps(numBricks);
00217         p_progress->CheckStatus();
00218 
00219         ibrick->begin();
00220         obrick->begin();
00221         for(int i = 0; i < numBricks; i++) {
00222           InputCubes[0]->read(*ibrick);
00223           funct(*ibrick, *obrick);
00224           OutputCubes[0]->write(*obrick);
00225           p_progress->CheckStatus();
00226           (*ibrick)++;
00227           (*obrick)++;
00228         }
00229         delete ibrick;
00230         delete obrick;
00231       }
00232       
00240       int ProcessIOList(std::vector<Isis::Buffer *> & ibufs, 
00241                         std::vector<Isis::Buffer *> & obufs,
00242                         std::vector<Isis::Brick *> & imgrs,  
00243                         std::vector<Isis::Brick *> & omgrs);
00244       
00259       template <typename FunctorIOList>
00260       void StartProcessIOList(FunctorIOList & funct) {
00261         // Construct two vectors of brick buffer managers
00262         // The input buffer managers
00263         vector<Isis::Brick *> imgrs;
00264         vector<Isis::Buffer *> ibufs;
00265     
00266         // And the output buffer managers
00267         vector<Isis::Brick *> omgrs;
00268         vector<Isis::Buffer *> obufs;
00269     
00270         int numBricks = ProcessIOList (ibufs, obufs, imgrs, omgrs);
00271     
00272         // Loop and let the app programmer process the bricks
00273         p_progress->SetMaximumSteps(numBricks);
00274         p_progress->CheckStatus();
00275 
00276         for(int t = 0; t < numBricks; t++) {
00277           // Read the input buffers
00278           for(unsigned int i = 0; i < InputCubes.size(); i++) {
00279             InputCubes[i]->read(*ibufs[i]);
00280           }
00281 
00282           // Pass them to the application function
00283           funct(ibufs, obufs);
00284 
00285           // And copy them into the output cubes
00286           for(unsigned int i = 0; i < OutputCubes.size(); i++) {
00287             OutputCubes[i]->write(*obufs[i]);
00288             omgrs[i]->next();
00289           }
00290 
00291           for(unsigned int i = 0; i < InputCubes.size(); i++) {
00292             imgrs[i]->next();
00293             // if the manager has reached the end and the
00294             // wrap option is on, wrap around to the beginning
00295             if(Wraps() && imgrs[i]->end()) imgrs[i]->begin();
00296 
00297             // Enforce same band
00298             if(imgrs[i]->Band() != imgrs[0]->Band() &&
00299                InputCubes[i]->getBandCount() != 1) {
00300               imgrs[i]->SetBaseBand(imgrs[0]->Band());
00301             }
00302           }
00303           p_progress->CheckStatus();
00304         }
00305   
00306         for(unsigned int i = 0; i < ibufs.size(); i++) {
00307           delete ibufs[i];
00308         }
00309         ibufs.clear();
00310         imgrs.clear();
00311 
00312         for(unsigned int i = 0; i < obufs.size(); i++) {
00313           delete obufs[i];
00314         }
00315         obufs.clear();
00316         omgrs.clear();
00317       }
00318   };
00319 };
00320 
00321 #endif