]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmickView.cpp
3ce0f5c398da4a2af06f89b6a0e59e90083b6341
[creaImageIO.git] / src2 / creaImageIOGimmickView.cpp
1 #include <creaImageIOGimmickView.h>
2 #include <creaImageIOSystem.h>
3
4 #include "boost/filesystem.hpp"
5
6 namespace fs = boost::filesystem;
7
8 namespace creaImageIO
9 {
10   //======================================================================
11   // CTor
12   GimmickView::GimmickView(Gimmick* gimmick, int threads)
13     : mGimmick(gimmick),
14           mReader(threads)
15   {
16     GimmickDebugMessage(1,"GimmickView::GimmickView"
17                         <<std::endl);
18         // Anciently started the threads ...
19     // Threads now automatically start at first image request
20     //mReader.Start();
21
22   }
23   //======================================================================
24
25   //======================================================================
26   /// Destructor
27   GimmickView::~GimmickView()
28   {
29     GimmickDebugMessage(1,"GimmickView::~GimmickView"
30                         <<std::endl);
31   }
32    //======================================================================
33  
34   //======================================================================  
35   /// Initializes the view : 
36   /// Creates the TreeViews for all the TreeHandler of the Controller
37   /// 
38   void GimmickView::Initialize()
39   {
40   }
41   //======================================================================
42   
43   //======================================================================
44   /// Finalize 
45   void GimmickView::Finalize()
46   {
47   }
48
49   //======================================================================
50
51   //======================================================================
52   /// Create the tree views 
53   void GimmickView::CreateTreeViews()
54   {
55     GimmickMessage(2,"Creating the tree views"<<std::endl);
56     Gimmick::TreeHandlerMapType::const_iterator i;
57     for (i = mGimmick->GetTreeHandlerMap().begin();
58          i!= mGimmick->GetTreeHandlerMap().end();
59          ++i)
60       {
61         this->CreateTreeView(i->second);
62       }
63   }
64   //======================================================================
65
66   //======================================================================
67   /// Updates the TreeView of given name from level l to bottom
68   /// (calls the virtual method TreeView::Update())
69   void GimmickView::UpdateTreeViewLevel(const std::string& t, int l)
70   {
71     TreeViewMapType::iterator i;
72     i = GetTreeViewMap().find(t);
73     if ( i == GetTreeViewMap().end() )
74       {
75         GimmickError("INTERNAL ERROR : GimmickView::UpdateTreeView : '"
76                      <<t<<"' is not in TreeViewMap");
77       }
78     i->second->UpdateLevel(l);    
79   }
80   //======================================================================
81   ///Validates the dimension compliance of the images with the maximum and 
82   ///minimum given, and between their sizes
83   bool GimmickView::ValidateSelected (std::vector<tree::Node*>& sel, int min_dim, int max_dim)
84   {
85         GimmickMessage(2,"Validating selected"<<std::endl);
86           
87         bool valid=true;
88           
89           // EED validate all
90           mValidationSignal(valid);
91           return valid;
92           
93         int level;
94         std::string mMessage;
95         if(sel.size()>0)
96         {
97                 std::vector<tree::Node*>::iterator i;
98                 std::string row;
99                 std::string col;
100                 std::string plane;
101                 
102                 //Validation between image sizes
103                 for (i=sel.begin(); i!=sel.end() && valid; ++i)
104                 {
105                         if(i==sel.begin())
106                         {
107                                 row=(*i)->GetAttribute("D0028_0010");
108                                 col=(*i)->GetAttribute("D0028_0011");
109                                 plane=(*i)->GetAttribute("D0028_0012");
110                                 level=(*i)->GetLevel();
111                                 
112                         }
113                         else
114                         {
115                                 if(((*i)->GetAttribute("D0028_0010"))!=row ||
116                                         ((*i)->GetAttribute("D0028_0011"))!=col ||
117                                         ((*i)->GetAttribute("D0028_0012"))!=plane)
118                                         {
119                                                 mMessage="The selected images are not compatible.";
120                                                 valid=false;
121                                         }
122                         }
123                 }
124
125                 //Dimention validation
126                 //Compatibility with maximum and minimum
127                         if(valid)
128                         {       
129                                 
130                                 int rows;
131                                 int cols;
132                                 int planes;
133                                 std::string s;
134                                 std::istringstream t(s);
135                                 s=row;
136                                 t >> rows;
137                                 if(row==""){rows=1;}
138                                 s=col;
139                                 t >> cols;
140                                 if(col==""){cols=1;}
141                                 s=plane;
142                                 t >> planes;
143                                 if(plane==""){planes=1;}
144
145                                 int dim = 0;
146                                 if (planes>1) dim=3;
147                                 else if (cols>1) dim=2;
148                                 else if (rows>1) dim=1;
149                             
150                                 if (dim == 0) 
151                                 {
152                                         mMessage="Unknown image dimension : cannot select !";
153                                         valid= false;
154                                 }
155                                 else if (dim>max_dim)
156                                 {
157                                         mMessage="Selecting ";
158                                         mMessage+=dim;
159                                         mMessage+="D images is not allowed !";
160                                         valid= false;
161                                 }
162                                 if ( dim == max_dim )
163                                 {
164                                         mMessage="Cannot add this image to selection : would result in a ";
165                                         mMessage+=(dim+1);
166                                         mMessage+="D image!";
167                                         
168                                         valid= false;
169                                 }
170                                 if ( dim < min_dim && sel.size()<2 )
171                                 {
172                                         GimmickDebugMessage(2, "State Check: Dim: "
173                                                 <<dim
174                                                 <<" Dim min:"
175                                                 <<min_dim
176                                                 <<std::endl);
177                                         mMessage="Cannot build the selection as it would result in a ";
178                                         mMessage+=dim;
179                                         mMessage+="D image, and the minimum is ";
180                                         mMessage+=min_dim;
181                                         mMessage+="D!";
182                                         valid= false;
183                                 }
184                         }
185           }
186         else
187         {
188                 mMessage="Cannot have 0 images selected";
189                 valid=false;
190         }
191         if(valid)
192         {
193                 mMessage="Selection OK !";
194         }
195         mValidationSignal(valid);
196         SetMessage(mMessage);
197         return valid;
198   }
199
200    //======================================================================
201
202    //======================================================================
203   ///Reads Images (Non Threaded)
204   void GimmickView::ReadImagesNotThreaded(std::vector<vtkImageData*>& s,std::vector<tree::Node*> im, int dimension)
205   {
206           
207         // Create the output data
208         if (im.size()==1) 
209       {
210                   
211                 // Only one image : give it
212                 vtkImageData* out = vtkImageData::New();
213                 GimmickMessage(1, "State Check: Full Filename: "
214                                                 <<im.front()->GetAttribute("FullFileName")
215                                                 <<std::endl);
216                 out->ShallowCopy(mReader.GetImage(im.front()->GetAttribute("FullFileName")));
217                 s.push_back( out );
218       }
219           
220     else if (im.size()>1)
221       {
222         vtkImageData* first = mReader.GetImage( im.front()->GetAttribute("FullFileName"));
223         if (dimension==2) 
224           {     
225             // n2D to 3D
226             vtkImageData* out = vtkImageData::New();
227             out->CopyStructure(first);  
228             out->SetScalarType(first->GetScalarType());
229             int ext[6];
230             first->GetExtent(ext);
231             ext[5] = im.size();
232             out->SetExtent(ext);
233             // LG : TODO : Z Spacing  ?
234             
235             out->AllocateScalars();
236             
237             //first->Print(std::cout);
238             //      out->Print(std::cout);
239             
240             int dim[3];
241             first->GetDimensions(dim);
242             unsigned long imsize = 
243               ( (unsigned long)first->GetScalarPointer(0,1,0)
244                 - (unsigned long)first->GetScalarPointer(0,0,0))
245               *dim[1];
246
247             int slice = 0;
248                 std::vector<tree::Node*>::iterator it;
249             for (it=im.begin(); it!=im.end(); ++it) 
250               {
251                 //std::cout << "copying slice "<<slice <<std::endl;
252                           vtkImageData* cur = mReader.GetImage( (*it)->GetAttribute("FullFileName"));
253                 
254                 void* src = cur->GetScalarPointer(0,0,0);
255                 void* dst = out->GetScalarPointer(0,0,slice);
256                 //              std::cout << "src="<<src<<std::endl;
257                 //              std::cout << "dst="<<dst<<std::endl;
258                 //              std::cout << "siz="<<imsize<<std::endl;
259                 memcpy(dst,src,imsize);
260
261                 slice++;
262               }
263             s.push_back(out);
264           }
265         else 
266           {
267             // n3D
268                   std::vector<tree::Node*>::iterator it;
269             for (it=im.begin(); it!=im.end(); ++it) 
270               {
271                 vtkImageData* out = vtkImageData::New();
272                 out->ShallowCopy(mReader.GetImage((*it)->GetAttribute("FullFileName")));
273                 s.push_back(out);
274               }
275           }
276       }
277         
278   }
279   //======================================================================
280
281   //======================================================================
282   ///Requests the reading of an image
283   void GimmickView::RequestReading(tree::Node* n, 
284                                    int prio, int selection_index)
285   {
286           mReader.Start();
287     ImageEventType t(n,0,selection_index);
288     mImageEventMap[n->GetAttribute("FullFileName")] = t;    
289     mReader.Request(this,n->GetAttribute("FullFileName"),prio);
290   }
291   //======================================================================
292
293   //======================================================================
294   void GimmickView::
295   OnMultiThreadImageReaderEvent(const std::string& filename,
296                                 MultiThreadImageReaderUser::EventType e,
297                                 vtkImageData* image)
298   {
299     GimmickDebugMessage(7,
300                         "MultiThreadImageReader event : "<<e<<std::endl);
301         if (e==ImageLoaded)
302         {
303     if (filename.size()==0)
304       {
305         GimmickDebugMessage(5,
306                             "Pushing unknown image in queue"
307                             <<std::endl);
308         mImageEventQueue.push_back(ImageEventType(image));
309         return;
310       }
311     ImageEventTypeMap::iterator i;
312     i = mImageEventMap.find(filename);
313     if (i!=mImageEventMap.end())
314       {
315         GimmickDebugMessage(5,
316                             "Pushing image of file '"<<filename<<"' in queue"
317                             <<std::endl);
318         ImageEventType e(i->second);
319         e.image = image;
320         mImageEventQueue.push_back(e);
321         mImageEventMap.erase(i);
322       }
323         }
324         else if (e==Error)
325         {
326                 std::string mess="ERROR: MultiThreadImageReader: Cannot read image in file ";
327                 mess+=filename;
328                 mess+="\n";
329                 GimmickMessage(1,mess);
330         }
331
332         else if (e==ImageUnloaded)
333         {
334                 std::string mess="Unloaded image in file ";
335                 mess+=filename;
336                 mess+="\n";
337                 GimmickMessage(1,mess);
338         }
339   }
340
341   //====================================================================
342
343   //====================================================================
344   void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
345   {
346     mValidationSignal.connect(callback);
347   }
348           
349   
350 } // EO namespace creaImageIO
351
352