freImageMediaControllerBase.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freImageMediaControllerBase.txx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 
00023 #ifndef __freImageMediaControllerBase_txx
00024 #define __freImageMediaControllerBase_txx
00025 
00026 #include "freImageMediaControllerBase.h"
00027 #include "freExceptions.h"
00028 
00029 namespace FREE
00030 {
00031 
00035 
00036 
00037 template <class TImage>
00038 const char* const ImageMediaControllerBase<TImage> :: cParam_ImageSource = "ImageSource";
00039 template <class TImage>
00040 const char* const ImageMediaControllerBase<TImage> :: cParamDsc_ImageSource = "Indicates the source of the image. 0: user dependent. 1: load by file. 2: by callback.";
00041 template <class TImage>
00042 const char* const ImageMediaControllerBase<TImage> :: cParam_LoadingType = "LoadingType";
00043 template <class TImage>
00044 const char* const ImageMediaControllerBase<TImage> :: cParamDsc_LoadingType = "Defines the loading type of the image media. 0: load media when session is build (preload). 1: load media when needed the first time and keep media. 2: load media everytime it is needed, don't cache it in memory.";
00045 
00046 template <class TImage>
00047 ImageMediaControllerBase<TImage>::
00048 ImageMediaControllerBase()
00049 {
00050   //Profile settings
00051   this->UpdateControllerID(ControllerID::ImageMediaControllerBase);
00052   this->m_Description = "Base class for media based on itk::image.";
00053 };
00054 
00055 template <class TImage>
00056 void
00057 ImageMediaControllerBase<TImage>::
00058 GenerateProfile(CtrlProfile::ControllerProfile& profile,
00059                 const SessionComponentCache* pComponentCache,
00060                 bool bRegardOldSetup) const
00061 {
00062   Superclass::GenerateProfile(profile, pComponentCache, bRegardOldSetup);
00063 
00064         //Parameters
00065   profile.Parameters().AddParameter(cParam_ImageSource,Parameter::PVTInteger,cParamDsc_ImageSource,1,"0");
00066   profile.Parameters().AddParameter(cParam_LoadingType,Parameter::PVTInteger,cParamDsc_LoadingType,1,"0");
00067 
00068   //Media
00069   profile.MediaMap().AddMedia("media",this->ControllerID(), DAGet, this->GetMediaDimension());
00070 };
00071 
00072 template <class TImage>
00073 ValidityTag::Pointer
00074 ImageMediaControllerBase<TImage>::
00075 GetMediaValidityTagRequirement(const MediaID& mediaID, SessionComponentCache* pComponentCache) const
00076 {
00077   if (mediaID == "media")
00078   {
00079     int iLoadingType;
00080     
00081     try
00082     {
00083       SessionAccessor::GetParameterValue(pComponentCache,cParam_LoadingType,iLoadingType);
00084     }
00085     catchAllNPassMacro("Error while retrieving loading type.");
00086 
00087     if (iLoadingType == 2)
00088     { //media is not keept in memory and therefore should allways be loaded,
00089       //so validity tag requirements is always "now"
00090       return ValidityTag::New();
00091     }
00092   }
00093 
00094   return Superclass::GetMediaValidityTagRequirement(mediaID, pComponentCache);
00095 }
00096 
00097 template <class TImage>
00098 unsigned long
00099 ImageMediaControllerBase<TImage>::
00100 GetMediaDimension() const
00101 {
00102   return TImage::GetImageDimension();
00103 };
00104 
00105 template <class TImage>
00106 typename ImageMediaControllerBase<TImage>::ComponentPointer
00107 ImageMediaControllerBase<TImage>::
00108 BuildMainComponent(ComponentSetup* pComponentSetup, SessionComponentCache* pComponentCache) const
00109 {
00110   int iLoadingType;
00111   
00112   try
00113   {
00114     SessionAccessor::GetParameterValue(pComponentCache,cParam_LoadingType,iLoadingType);
00115   }
00116   catchAllNPassMacro("Error while retrieving loading type.");
00117 
00118   //generate empty dummy media because component pointer in cache must not be NULL
00119   ComponentPointer smpImage = Superclass::BuildMainComponent(pComponentSetup, pComponentCache);
00120 
00121   if (iLoadingType==0)
00122   { //preload -> load while building
00123     if (!LoadMedia(pComponentCache)) throwCtrlExceptionMacro("", "Unable to load image media.");
00124 
00125     try
00126     {
00127       smpImage = dynamic_cast<ComponentType*>(DirectSessionComponentAccessor::GetComponent(pComponentCache));
00128     }
00129     catchAllNPassMacro("Error; component loaded wasn't the right media type.");
00130     if (smpImage.IsNull()) throwCtrlExceptionMacro("","Error; component loaded wasn't the right media type.");
00131 
00132     //add the loaded flag, because the media is kept in memory
00133     DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pComponentCache);
00134   }
00135 
00136   //Actualize validitiy tag even if no media is loaded yet, because the validity requirement for
00137   //media images not preloaded is at least equal to now.
00138   this->ActualizeMediaValidityTag("media",pComponentCache);
00139 
00140   return smpImage;
00141 };
00142 
00143 template <class TImage>
00144 typename ImageMediaControllerBase<TImage>::ComponentPointer
00145 ImageMediaControllerBase<TImage>::
00146 GetComponent(const SessionComponentCache* pComponentCache) const
00147 {
00148   ComponentPointer smpMedia;
00149   
00150   //get loading type
00151   int iLoadingType;
00152   
00153   try
00154   {
00155     SessionAccessor::GetParameterValue(pComponentCache,cParam_LoadingType,iLoadingType);
00156   }
00157   catchAllNPassMacro("Error while retrieving loading type.");
00158   
00159   //The value of the flag isn't important, only its presence or absence
00160   GenericComponentType* pLoadedFlag = DirectSessionComponentAccessor::GetRepositoryElement("LoadedFlag",pComponentCache);
00161 
00162   if (!pLoadedFlag)
00163   { //there is no flag indicating that the image was loaded,
00164     //so load now and add to the cache, therefore
00165     //exceptionally cast away const.
00166     SessionComponentCache* pNonConstComponentCache = const_cast<SessionComponentCache*>(pComponentCache);
00167 
00168     if (!LoadMedia(pNonConstComponentCache)) throwCtrlExceptionMacro("", "Unable to load image media.");
00169 
00170     try
00171     {
00172       smpMedia = dynamic_cast<ComponentType*>(DirectSessionComponentAccessor::GetComponent(pNonConstComponentCache));
00173     }
00174     catchAllNPassMacro("Error; component loaded wasn't the right media type.");
00175     if (smpMedia.IsNull()) throwCtrlExceptionMacro("","Error; component loaded wasn't the right media type.");
00176 
00177     if (iLoadingType==2)
00178     { //don't cache media, media is stored in the smart pointer smpMedia,
00179       //so set component smart pointer (set by LoadMedia) to an empty dummy
00180       //media again.
00181       ComponentPointer smpDummy = ComponentType::New();
00182       DirectSessionComponentAccessor::SetComponent(smpDummy,pNonConstComponentCache);
00183     }
00184     else
00185     {
00186       //add the loaded flag, because the media is kept in memory
00187       DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pNonConstComponentCache);
00188     }
00189     return smpMedia;
00190   }
00191   else
00192   { //it was preloaded so handle like other components
00193     return Superclass::GetComponent(pComponentCache);
00194   }
00195 }; 
00196 
00197 
00198 template <class TImage>
00199 typename ImageMediaControllerBase<TImage>::GenericMediaPointer
00200 ImageMediaControllerBase<TImage>::
00201 GetMediaCasted(const MediaID& mediaID,ComponentType* pComponent, SessionComponentCache* pComponentCache, SessionInfo* pSessionInfo) const
00202 {
00203         if (mediaID == "media")
00204   { 
00205     GenericMediaPointer smpMedia = this->GetGenericComponent(pComponentCache);
00206     return smpMedia;
00207   }
00208 
00209   return Superclass::GetMediaCasted(mediaID, pComponent, pComponentCache, pSessionInfo);
00210 }; 
00211 
00212 } //end of namespace free
00213 
00214 #endif

Generated at Sat Oct 13 16:06:10 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000