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