MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLTools/include/mlVirtualVolume.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00005 
00024 //----------------------------------------------------------------------------------
00025 #ifndef __mlVirtualVolume_H
00026 #define __mlVirtualVolume_H
00027 
00028 // Include dll-specific settings.
00029 #ifndef __MLToolsSystem_H
00030 #include "MLToolsSystem.h"
00031 #endif
00032 
00033 // Include most ml specific things.
00034 #ifndef __mlModuleIncludes_H
00035 #include "mlModuleIncludes.h"
00036 #endif
00037 
00038 #include <mlMemoryManager.h>
00039 
00040 class MLMemoryProfile;
00041 
00042 ML_START_NAMESPACE
00043 
00046   class VirtualVolume;
00047   class BitImage;
00048   struct PageBuffer;
00049   template<class DATATYPE> class TVirtualVolume;
00050   
00051   class VirtualVolumeMemoryTracker;
00052 
00053   //--------------------------------------------------------------------------------
00069   //--------------------------------------------------------------------------------
00070   class MLTOOLS_EXPORT VirtualVolume{
00071 
00072   public:
00073 
00074     // ---- Public member functions:
00075 
00078     VirtualVolume(PagedImage*  image,
00079                   MLDataType dType,
00080                   MLint      maxNumKB = -1,
00081                   bool       areExceptionsOnFlag = false);
00082 
00091     VirtualVolume(const ImageVector &ext                 = ImageVector(0),
00092                   MLdouble      fillVal             = 0,
00093                   MLDataType    dType               = MLuint8Type,
00094                   MLint         maxNumKB            = -1,
00095                   bool          areExceptionsOnFlag = false);
00096 
00098     virtual                    ~VirtualVolume()                 { _reset();                                  }
00099 
00101     inline MLint               getDim() const                   { return _dim;                               }
00102 
00104     inline const SubImageBox   &getBox() const                  { return _pagedImgBox;                       }
00105 
00107     inline ImageVector          getExtent() const               { return _pagedImgBox.getExtent();           }
00108 
00110     inline MLDataType           getDataType() const             { return _dataType;                          }
00111 
00114     inline Module              *getModule() const               { return _module;                            }
00115 
00118     inline PagedImage          *getInputImage() const           { return _pagedImg;                          }
00119 
00121     inline MLint                getInputIndex() const           { return _module ? _outIdx : -1;             }
00122 
00124     inline bool                 isValid() const                 { return _valid;                             }
00125 
00127     inline void                 invalidate()                    { _valid = false; _cleanUpMemory();          }
00128 
00130     inline MLuint               getNumMappedPages() const       { return _numMappedPages;                    }
00131 
00133     inline MLuint               getNumWrittenPages()            { _updateWrittenPages(); return _numWrittenPages;}
00134 
00136     inline MLuint               getNumPages()                   { return _numMappedPages+getNumWrittenPages(); }
00137 
00139     inline MLuint               getNumBytes()                   { return getNumPages()*_numPageBytes;        }
00140 
00142     inline bool                 areExceptionsOn() const         { return _areExceptionsOn;                   }
00143 
00145     inline const ImageVector    &getPageExtent() const          { return _pageExt;                           }
00146 
00148     inline const ImageVector    &getPageArrayExtent() const     { return _pageArrayExt;                      }
00149 
00152     inline const SubImageBox &getWrittenPageBox(MLuint32 wp);
00153 
00160     void                   *getWrittenPageData(MLuint32 wp);
00161 
00166     void                   unMapAndClearWrittenPages();
00167 
00170     enum PageMapped { IsMapped, IsUnMapped };
00171 
00177     typedef void (*PageFunc) (VirtualVolume *obj, PageBuffer *page, PageMapped mapped, void *userData);
00178 
00183     void registerPageFunction(PageFunc pFunc, void *userData);
00184 
00199     void resize(const ImageVector &newExt);
00200 
00211     void copyTileFromInputImage(const SubImageBox      &box,
00212                                 PagedImage             &image,
00213                                 const ImageVector         &shift,
00214                                 const ScaleShiftData &scaleShift=ScaleShiftData());
00215 
00216 
00223     void copyToSubImage(SubImage &outSubImg);
00224 
00225 #if 0
00226     // Copy area from inImg into virtual volume. This non-template version is still
00227     // not implemented but desirable. Only valid areas are copied; regions outside
00228     // input or virtual volume areas are not handled.
00229     // Errors are reported by thrown exceptions if exception handling is enabled;
00230     // otherwise they lead to ML_PRINT_ERROR or ML_PRINT_FATAL_ERROR messages.
00231     // Only valid regions of the virtual volume are written; box is clipped against
00232     // the virtual volume extents before writing the data region.
00233     void copyFromSubImg(const SubImage    &inImg,
00234                         const SubImageBox &box,
00235                         const ImageVector    &pos);
00236 #endif
00237 
00245     BitImage *getUsedPagesMask(bool enableMappedPages=true,
00246                                bool enableWrittenPages=true) const;
00247 
00250     void *createTypedVirtualVolume();
00251 
00254     static void destroyTypedVirtualVolume(void *tVV, MLDataType dt);
00255 
00256 #ifdef ML_DEPRECATED
00257 
00259 
00260 
00261 #ifdef WIN32
00262 #pragma warning(push)
00263 #pragma warning(disable : 4996 )
00264 #endif
00265 
00266 public:
00269   inline ML_DEPRECATED ImageVector getExt() const                     { return getExtent();                       }
00270 
00273   ML_DEPRECATED BaseOp* getBaseOp() const;
00274 
00277   inline ML_DEPRECATED PagedImage* getInImg() const                   { return _pagedImg;                          }
00278 
00281   inline ML_DEPRECATED MLint getInIdx() const                         { return _module ? _outIdx : -1;             }
00282 
00285   inline ML_DEPRECATED const ImageVector& getPageExt() const          { return _pageExt;                           }
00286 
00289   inline ML_DEPRECATED const ImageVector& getPageArrayExt() const     { return _pageArrayExt;                      }
00290 
00293   inline ML_DEPRECATED void copyTileFromInImg(const SubImageBox      &box,
00294                            PagedImage             &image,
00295                            const ImageVector         &shift,
00296                            const ScaleShiftData &scaleShift=ScaleShiftData()) { copyTileFromInputImage(box, image, shift, scaleShift); }
00297 
00300   inline ML_DEPRECATED void copyToSubImg(SubImage &outSubImg) { copyToSubImage(outSubImg); }
00301 
00304   ML_DEPRECATED_CONSTRUCTOR VirtualVolume(Module    *baseOp,
00305       MLint      connIdx,
00306       MLDataType dType,
00307       MLint      maxNumKB = -1,
00308       bool       areExceptionsOnFlag = false,
00309       bool       useOutConnectors    = false);
00310 
00313   ML_DEPRECATED void copyTileFromInImg(const SubImageBox      &box,
00314       Module               &op,
00315       MLint                 outIdx,
00316       const ImageVector         &shift,
00317       const ScaleShiftData &scaleShift=ScaleShiftData());
00318 
00319 #ifdef WIN32
00320 #pragma warning(pop)
00321 #endif
00322 
00323 #endif // ML_DEPRECATED
00324 
00325   protected:
00326 
00347     void                   _init(PagedImage     *img,
00348                                  const ImageVector &ext,
00349                                  MLdouble      fillVal,
00350                                  MLDataType    dType,
00351                                  MLint         maxNumKB = -1,
00352                                  bool          areExceptionsOnFlag = false);
00353 
00355     void                   _reset();
00356 
00358     void                   _cleanUpMemory();
00359 
00360 
00361 
00362     // ---- Protected member functions:
00363 
00365     template<typename> friend class TVirtualVolume;
00366 
00368     void                _default();
00369 
00371     inline static void* _getPageCB(PageBuffer *pageBuffer);
00372 
00378     static void*        _loadPageCB(PageBuffer *pageBuffer);
00379 
00383     void*               _loadPage(PageBuffer *pageBuffer);
00384 
00387     void                _updateWrittenPages();
00388 
00391     PageBuffer         *_getPageBuffer6D(const ImageVector &pos);
00392 
00393   private:
00394 
00402     MLuint              _makePowerOfTwo(MLint &num) const;
00403 
00406     long                _getPowerOfTwo(MLuint        num) const;
00407     
00410     MLMemoryProfile* _getMemoryProfile();
00411     
00412 
00413     // ---- Members:
00414 
00415 
00417 
00418 
00421     bool            _valid;
00422 
00424     Module         *_module;
00425 
00427     MLint32         _outIdx;
00428 
00430     PagedImage     *_pagedImg;
00431 
00433     SubImageBox     _pagedImgBox;
00434 
00436     ImageVector     _origVolumeExt;
00437 
00439     ImageVector     _pageExt;
00440 
00442     long            _maxNumKB;
00443 
00445     MLDataType      _dataType;
00446 
00448     MLdouble        _fillValue;
00449 
00452     bool            _areExceptionsOn;
00453 
00456     PageFunc        _pageFunc;
00457 
00460     void           *_pageFuncData;
00462 
00463 
00465     MLuint          _numPageBytes;
00466 
00468     ImageVector     _volumeExt;
00469 
00471     MLint           _dim;
00472 
00474     ImageVector     _pageArrayExt;
00475 
00477     MLuint          _numPageArrayEntries;
00478 
00480     MLuint          _maxMappedPages;
00481 
00482 
00484     ImageVector     _strides;
00485 
00487     ImageVector     _pStrides;
00488 
00491     ImageVector     _rShift;
00492 
00494     ImageVector     _idxMask;
00495 
00496 
00498     PageBuffer     *_pageArray;
00499 
00501     PageBuffer    **_mappedPages;
00502 
00504     MLuint          _numMappedPages;
00505 
00507     PageBuffer    **_writtenPages;
00508 
00510     MLuint          _numWrittenPages;
00511 
00513     MLuint          _nextThrowPage;
00514     
00516     VirtualVolumeMemoryTracker* _currentlyUsedMemoryTracker;
00517 
00519     ML_CLASS_HEADER(VirtualVolume);
00520     
00521     friend class VirtualVolumeMemoryTracker;
00522   };
00523 
00524   //--------------------------------------------------------------------------------
00535   //--------------------------------------------------------------------------------
00536   struct MLTOOLS_EXPORT PageBuffer {
00537 
00539     typedef void *(*ReturnPageFunc)(PageBuffer *toMePtr);
00540 
00542     inline PageBuffer(): _virtVol(NULL), _page(NULL), _locked(false), _getPageFkt(NULL) { }
00543 
00545     VirtualVolume *_virtVol;
00546 
00548     SubImageBox    _box;
00549 
00551     MLMemoryBlockHandle _memoryBlockHandle;
00552 
00556     void          *_page;
00557 
00559     bool           _locked;
00560 
00562     ReturnPageFunc _getPageFkt;
00563   };
00564 
00567   inline void* VirtualVolume::_getPageCB(PageBuffer *pageBuffer) { return pageBuffer->_page; }
00568 
00569 ML_END_NAMESPACE
00570 
00571 // For compatibilities we include the TVirtualVolume here.
00572 #ifndef __mlTVirtualVolume_H
00573 #include "mlTVirtualVolume.h"
00574 #endif
00575 
00576 #endif // __mlVirtualVolume_H