1 #include <creaImageIOGimmickView.h>
2 #include <creaImageIOSystem.h>
4 #include "boost/filesystem.hpp"
6 namespace fs = boost::filesystem;
10 //======================================================================
12 GimmickView::GimmickView(Gimmick* gimmick, int threads)
16 GimmickDebugMessage(1,"GimmickView::GimmickView"
18 // Start the threads ...
22 //======================================================================
24 //======================================================================
26 GimmickView::~GimmickView()
28 GimmickDebugMessage(1,"GimmickView::~GimmickView"
31 //======================================================================
33 //======================================================================
34 /// Initializes the view :
35 /// Creates the TreeViews for all the TreeHandler of the Controller
37 void GimmickView::Initialize()
40 //======================================================================
42 //======================================================================
44 void GimmickView::Finalize()
48 //======================================================================
50 //======================================================================
51 /// Create the tree views
52 void GimmickView::CreateTreeViews()
54 GimmickMessage(2,"Creating the tree views"<<std::endl);
55 Gimmick::TreeHandlerMapType::const_iterator i;
56 for (i = mGimmick->GetTreeHandlerMap().begin();
57 i!= mGimmick->GetTreeHandlerMap().end();
60 this->CreateTreeView(i->second);
63 //======================================================================
65 //======================================================================
66 /// Updates the TreeView of given name from level l to bottom
67 /// (calls the virtual method TreeView::Update())
68 void GimmickView::UpdateTreeViewLevel(const std::string& t, int l)
70 TreeViewMapType::iterator i;
71 i = GetTreeViewMap().find(t);
72 if ( i == GetTreeViewMap().end() )
74 GimmickError("INTERNAL ERROR : GimmickView::UpdateTreeView : '"
75 <<t<<"' is not in TreeViewMap");
77 i->second->UpdateLevel(l);
79 //======================================================================
80 ///Validates the dimension compliance of the images with the maximum and
81 ///minimum given, and between their sizes
82 bool GimmickView::ValidateSelected (std::vector<tree::Node*>& sel, int min_dim, int max_dim)
84 GimmickMessage(2,"Validating selected"<<std::endl);
90 std::vector<tree::Node*>::iterator i;
95 //Validation between image sizes
96 for (i=sel.begin(); i!=sel.end() && valid; ++i)
100 row=(*i)->GetAttribute("D0028_0010");
101 col=(*i)->GetAttribute("D0028_0011");
102 plane=(*i)->GetAttribute("D0028_0012");
103 level=(*i)->GetLevel();
108 if(((*i)->GetAttribute("D0028_0010"))!=row ||
109 ((*i)->GetAttribute("D0028_0011"))!=col ||
110 ((*i)->GetAttribute("D0028_0012"))!=plane)
112 mMessage="The selected images are not compatible.";
118 //Dimention validation
119 //Compatibility with maximum and minimum
127 std::istringstream t(s);
136 if(plane==""){planes=1;}
140 else if (cols>1) dim=2;
141 else if (rows>1) dim=1;
145 mMessage="Unknown image dimension : cannot select !";
148 else if (dim>max_dim)
150 mMessage="Selecting ";
152 mMessage+="D images is not allowed !";
155 if ( dim == max_dim )
157 mMessage="Cannot add this image to selection : would result in a ";
159 mMessage+="D image!";
163 if ( dim < min_dim && sel.size()<2 )
165 GimmickDebugMessage(2, "State Check: Dim: "
170 mMessage="Cannot build the selection as it would result in a ";
172 mMessage+="D image, and the minimum is ";
181 mMessage="Cannot have 0 images selected";
186 mMessage="Selection OK !";
188 mValidationSignal(valid);
189 SetMessage(mMessage);
193 //======================================================================
195 //======================================================================
196 ///Reads Images (Non Threaded)
197 void GimmickView::ReadImagesNotThreaded(std::vector<vtkImageData*>& s,std::vector<tree::Node*> im, int dimension)
200 // Create the output data
204 // Only one image : give it
205 vtkImageData* out = vtkImageData::New();
206 GimmickMessage(1, "State Check: Full Filename: "
207 <<im.front()->GetAttribute("FullFileName")
209 out->ShallowCopy(mReader.GetImage(im.front()->GetAttribute("FullFileName")));
213 else if (im.size()>1)
215 vtkImageData* first = mReader.GetImage( im.front()->GetAttribute("FullFileName"));
219 vtkImageData* out = vtkImageData::New();
220 out->CopyStructure(first);
221 out->SetScalarType(first->GetScalarType());
223 first->GetExtent(ext);
226 // LG : TODO : Z Spacing ?
228 out->AllocateScalars();
230 //first->Print(std::cout);
231 // out->Print(std::cout);
234 first->GetDimensions(dim);
235 unsigned long imsize =
236 ( (unsigned long)first->GetScalarPointer(0,1,0)
237 - (unsigned long)first->GetScalarPointer(0,0,0))
241 std::vector<tree::Node*>::iterator it;
242 for (it=im.begin(); it!=im.end(); ++it)
244 //std::cout << "copying slice "<<slice <<std::endl;
245 vtkImageData* cur = mReader.GetImage( (*it)->GetAttribute("FullFileName"));
247 void* src = cur->GetScalarPointer(0,0,0);
248 void* dst = out->GetScalarPointer(0,0,slice);
249 // std::cout << "src="<<src<<std::endl;
250 // std::cout << "dst="<<dst<<std::endl;
251 // std::cout << "siz="<<imsize<<std::endl;
252 memcpy(dst,src,imsize);
261 std::vector<tree::Node*>::iterator it;
262 for (it=im.begin(); it!=im.end(); ++it)
264 vtkImageData* out = vtkImageData::New();
265 out->ShallowCopy(mReader.GetImage((*it)->GetAttribute("FullFileName")));
272 //======================================================================
274 //======================================================================
275 ///Requests the reading of an image
276 void GimmickView::RequestReading(tree::Node* n,
277 int prio, int selection_index)
279 ImageEventType t(n,0,selection_index);
280 mImageEventMap[n->GetAttribute("FullFileName")] = t;
281 mReader.Request(this,n->GetAttribute("FullFileName"),prio);
283 //======================================================================
285 //======================================================================
287 OnMultiThreadImageReaderEvent(const std::string& filename,
288 MultiThreadImageReaderUser::EventType e,
291 GimmickDebugMessage(5,
292 "MultiThreadImageReader event : "<<e<<std::endl);
293 if (filename.size()==0)
295 GimmickDebugMessage(5,
296 "Pushing unknown image in queue"
298 mImageEventQueue.push_back(ImageEventType(image));
301 ImageEventTypeMap::iterator i;
302 i = mImageEventMap.find(filename);
303 if (i!=mImageEventMap.end())
305 GimmickDebugMessage(5,
306 "Pushing image of file '"<<filename<<"' in queue"
308 ImageEventType e(i->second);
310 mImageEventQueue.push_back(e);
311 mImageEventMap.erase(i);
315 //====================================================================
317 //====================================================================
318 void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
320 mValidationSignal.connect(callback);
324 } // EO namespace creaImageIO