00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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 {
00089
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
00119 ComponentPointer smpImage = Superclass::BuildMainComponent(pComponentSetup, pComponentCache);
00120
00121 if (iLoadingType==0)
00122 {
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
00133 DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pComponentCache);
00134 }
00135
00136
00137
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
00151 int iLoadingType;
00152
00153 try
00154 {
00155 SessionAccessor::GetParameterValue(pComponentCache,cParam_LoadingType,iLoadingType);
00156 }
00157 catchAllNPassMacro("Error while retrieving loading type.");
00158
00159
00160 GenericComponentType* pLoadedFlag = DirectSessionComponentAccessor::GetRepositoryElement("LoadedFlag",pComponentCache);
00161
00162 if (!pLoadedFlag)
00163 {
00164
00165
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 {
00179
00180
00181 ComponentPointer smpDummy = ComponentType::New();
00182 DirectSessionComponentAccessor::SetComponent(smpDummy,pNonConstComponentCache);
00183 }
00184 else
00185 {
00186
00187 DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pNonConstComponentCache);
00188 }
00189 return smpMedia;
00190 }
00191 else
00192 {
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 }
00213
00214 #endif