]> Creatis software - creaImageIO.git/blob - src/creaImageIOGimmickView.cpp
0af8ded092fb945947d429616830751e53b3b4a5
[creaImageIO.git] / src / creaImageIOGimmickView.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #include <creaImageIOGimmickView.h>
30 #include <creaImageIOSystem.h>
31 #include "boost/filesystem.hpp"
32
33 #if defined(USE_GDCM)
34 #include <gdcmGlobal.h>
35 #include <gdcmSerieHelper.h>
36 #include <gdcmFileHelper.h>
37 #include <gdcmUtil.h>
38 #include <vtkGdcmReader.h>
39 #endif
40
41 /*#if defined(USE_GDCM2)
42 #include <vtkGDCMImageReader.h>
43 #include "gdcmSystem.h"
44 #include "gdcmCryptographicMessageSyntax.h"
45 #include "gdcmUIDGenerator.h"
46 #include "gdcmAnonymizer.h"
47 #include "gdcmGlobal.h"
48 #endif*/
49
50
51 namespace fs = boost::filesystem;
52 namespace creaImageIO
53 {
54
55         ///Class used to represent the actual state of the image selected and to perform comparisons on its values
56   class ImageExtent
57   {
58   public:
59     ImageExtent(const std::string& x, const std::string& y, const std::string& z, const std::string& t)
60         {
61                 sscanf(x.c_str(),"%d",&mExtent[0]);
62                 sscanf(y.c_str(),"%d",&mExtent[1]);
63                 sscanf(z.c_str(),"%d",&mExtent[2]);
64                 sscanf(t.c_str(),"%d",&mExtent[3]);
65                 if(x==""){mExtent[0]=1;}
66                 if(y==""){mExtent[1]=1;}
67                 if(z==""){mExtent[2]=1;}
68                 if(t==""){mExtent[3]=1;}
69
70                 if (mExtent[3]>1) mDim=4;
71                 else if (mExtent[2]>1) mDim=3;
72                 else if (mExtent[1]>1) mDim=2;
73                 else if (mExtent[0]>1) mDim=1;
74                 else mDim=0;
75         }
76         
77
78         ///Clears the extent
79     void Clear() { mExtent[0] = mExtent[1] = mExtent[2] = mExtent[3] = 1; }
80
81         ///Returns true if the two extents are compatible
82     bool IsCompatible( const ImageExtent& );
83
84         ///Adds the extent passed as a parameter to the current extent
85     void Add ( const ImageExtent& );
86                 
87         ///Returns the ieth position of the extent
88     int Get(int i) { return mExtent[i]; }
89     
90         ///Returns the dimension of the current image
91     void SetDimension(int dim) { mDim=dim; }
92
93         ///Returns the dimension of the current image
94     int GetDimension() { return mDim; }
95
96   private:
97     int mExtent[4];
98     int mDim;
99   };
100
101   //======================================================================
102
103   //======================================================================
104   // CTor
105   GimmickView::GimmickView(boost::shared_ptr<Gimmick> gimmick, int threads)
106     : mGimmick(gimmick),
107           mReader(threads)
108   {
109     GimmickDebugMessage(1,"GimmickView::GimmickView"
110                         <<std::endl);
111         // Anciently started the threads ...
112     // Threads now automatically start at first image request
113     //mReader.Start();
114
115   }
116   //======================================================================
117
118   //======================================================================
119   /// Destructor
120   GimmickView::~GimmickView()
121   {
122     GimmickDebugMessage(1,"GimmickView::~GimmickView"
123                         <<std::endl);
124   }
125    //======================================================================
126  
127   //======================================================================  
128   /// Initializes the view : 
129   /// Creates the TreeViews for all the TreeHandler of the Controller
130   /// 
131   void GimmickView::Initialize()
132   {
133         mReaderStarted=false;
134   }
135   //======================================================================
136
137   //======================================================================
138   /// Finalize 
139   void GimmickView::Finalize()
140   {
141   }
142
143   //======================================================================
144
145   //======================================================================
146   /// Create the tree views 
147   void GimmickView::CreateTreeViews()
148   {
149     GimmickMessage(2,"Creating the tree views"<<std::endl);
150     Gimmick::TreeHandlerMapType::const_iterator i;
151     for (i = mGimmick->GetTreeHandlerMap().begin();
152          i!= mGimmick->GetTreeHandlerMap().end();
153          ++i)
154       {
155         this->CreateTreeView(i->second);
156       }
157   }
158
159   /// Create a tree view with a given name
160   void GimmickView::CreateSingleTreeView(std::string &i_name)
161   {
162         this->CreateTreeView(mGimmick->GetTreeHandlerMap()[i_name]);
163      
164   }
165
166
167   //======================================================================
168
169   //======================================================================
170   /// Updates the TreeView of given name from level l to bottom
171   /// (calls the virtual method TreeView::Update())
172   void GimmickView::UpdateTreeViewLevel(const std::string& t, int l)
173   {
174     TreeViewMapType::iterator i;
175     i = GetTreeViewMap().find(t);
176     if ( i == GetTreeViewMap().end() )
177       {
178         GimmickError("INTERNAL ERROR : GimmickView::UpdateTreeView : '"
179                      <<t<<"' is not in TreeViewMap");
180       }
181     i->second->UpdateLevel(l);
182   }
183
184   //======================================================================
185   /// Clears the status and begins a new selection process
186   void GimmickView::ResetExtent()
187   {
188           if(mImageExtent!=0)
189           {
190                   mImageExtent.reset();
191           }
192           valid=true;
193   }
194
195   //======================================================================
196
197   //======================================================================
198   bool ImageExtent::IsCompatible(const ImageExtent& ie)
199   {
200           bool compatible=true;
201           ImageExtent * extent= (ImageExtent*)&ie;
202           if((*extent).Get(0)!=Get(0)
203                  || (*extent).Get(1)!=Get(1))
204           {
205                   compatible=false;
206           }
207           return compatible;
208   }
209
210   //======================================================================
211
212   //======================================================================
213   void ImageExtent::Add(const ImageExtent& ie)
214   {
215           ImageExtent * extent= (ImageExtent*)&ie;
216           mExtent[2]+=(*extent).Get(2);
217           if(mExtent[2]>1)
218           {
219           SetDimension(3);
220           }
221   }
222
223   //======================================================================
224   /// No selected image
225   bool GimmickView::NoValidateSelected ()
226   {
227         GimmickDebugMessage(2,"Validating selected"<<std::endl);
228         std::string mMessage;
229         mMessage="Cannot have 0 images selected!";
230         valid=false;
231         modifyValidationSignal(valid);
232         SetMessage(mMessage);
233         return valid;
234   }
235
236   //======================================================================
237   ///Validates the dimension compliance of the images with the maximum and 
238   ///minimum given, and between their sizes
239   bool GimmickView::ValidateSelected (tree::Node* sel, int min_dim, int max_dim)
240   {
241         GimmickDebugMessage(2,"Validating selected"<<std::endl);
242         std::string mMessage;
243         
244         if(sel==0)
245         {
246                 mMessage="Cannot have 0 images selected!";
247                 valid=false;
248         }
249         else
250         {
251                         boost::shared_ptr<ImageExtent> ie=boost::shared_ptr<ImageExtent>(new ImageExtent((*sel).GetAttribute("D0028_0010"),
252                                                                         (*sel).GetAttribute("D0028_0011"),
253                                                                         (*sel).GetAttribute("D0028_0012"), 
254                                                                         ""));
255
256         if(mImageExtent==0)
257         {
258                 mImageExtent=ie;
259                 if((mImageExtent->Get(min_dim-1)<2)||(mImageExtent->Get(max_dim)>1))
260                 {
261                         valid=false;
262                 }
263                 else
264                 {
265                         std::stringstream out;
266                         out << mImageExtent->GetDimension() << "D image " << mImageExtent->Get(0) << "x"<< mImageExtent->Get(1) << "x"<< mImageExtent->Get(2) <<" selected";
267                         mMessage = out.str();
268                         mImageExtent->SetDimension(2);
269                         valid=true;
270                 }
271         }
272         else
273         {
274                 if(mImageExtent->IsCompatible(*ie))
275                 {
276                         if(mImageExtent->GetDimension()==max_dim && mImageExtent->Get(max_dim)>2)
277                         {
278                                 std::stringstream out;
279                                 out<<"Cannot add this image to selection : would result in a "<<mImageExtent->GetDimension()+1<<"D image!";
280                                 mMessage=out.str();
281                                 valid=false;
282                         }
283                         else if(max_dim<3)
284                         {
285                                 std::stringstream out;
286                                 out<<"Selecting "<<mImageExtent->GetDimension()<<"D images is not allowed !";
287                                 mMessage=out.str();
288                                 valid=false;
289                         }
290                         else if(min_dim==3 && (ie->Get(2)+mImageExtent->Get(2))<2)
291                         {
292                                 std::stringstream out;
293                                 out << "Cannot build the selection as it would result in a ";
294                                 out << mImageExtent->GetDimension();
295                                 out << "D image, and the minimum is ";
296                                 out << min_dim;
297                                 out << "D!";
298                                 mMessage=out.str();
299                                 valid=false;
300                         }
301                         else
302                         {
303                                 mImageExtent->Add(*ie);
304                                 std::stringstream out;
305                                 out << mImageExtent->GetDimension() << "D image " << mImageExtent->Get(0) << "x"<< mImageExtent->Get(1) << "x"<< mImageExtent->Get(2) <<" selected";
306                                 mMessage = out.str();
307                         }
308                 }
309                 else
310                 {
311                         mMessage="The selected images are not compatible.";
312                         valid=false;
313                 }
314           }
315         }
316
317         modifyValidationSignal(valid);
318         SetMessage(mMessage);
319         return valid;
320   }
321
322   //======================================================================
323   void GimmickView::modifyValidationSignal(bool ivalid)
324   {
325
326           mValidationSignal(ivalid);
327   }
328
329   void GimmickView::stopReader()
330   {
331           mReader.Stop();
332   }
333
334    //======================================================================
335   ///Reads Images (Non Threaded)
336 void GimmickView::ReadImagesNotThreaded(std::vector<vtkImageData*>& s, std::vector<std::string> im, int dimension)
337 {
338         stopReader();
339 /* remember!
340
341 #define GIMMICK_NO_IMAGE_SELECTION 0
342 #define GIMMICK_2D_IMAGE_SELECTION 2
343 #define GIMMICK_3D_IMAGE_SELECTION 3
344 #define GIMMICK_4D_IMAGE_SELECTION 4
345
346 #define NATIVE 0
347 #define _2D    2
348 #define _3D    3
349
350 */
351         // Create the output data
352         if (im.size()==1) 
353         {
354                 vtkImageData * out=vtkImageData::New();
355                 out->ShallowCopy(mReader.GetImage(im.front()));
356                 s.push_back(out);
357         }
358     else if (im.size()>1) // Test inutile ? JPR
359         {
360                 vtkImageData* first = mReader.GetImage( im.front());
361                 if (dimension == 2)
362                 {
363                  // n3D
364                     std::vector<std::string>::iterator it;
365                         for (it=im.begin(); it!=im.end(); ++it) 
366                         {
367                                 vtkImageData* out = vtkImageData::New();
368                                 out->ShallowCopy(mReader.GetImage(*it));
369                                 s.push_back(out);
370                         }
371                 }         
372                 else 
373                 {
374                         // n*2D to 3D
375                         vtkImageData* out = vtkImageData::New();
376 //                      out->CopyStructure(first);      
377                         out->SetScalarType(first->GetScalarType());
378                         out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
379                         int ext[6];
380                         //first->GetExtent(ext);  // JPR
381                         first->GetWholeExtent(ext);  // renvoie egalement 0,0 en Z // JPR
382
383                         if(ext[5] == 0)
384                         {
385                                 ext[5] = im.size()-1;
386                         }
387                         else
388                         {
389                                 ext[5] = ext[5] * im.size()-1; // to deal with multiframes - JPR
390                         }
391                         out->SetExtent(ext);
392
393                         // LG : TODO : Z Spacing  ?
394
395                         int dim[3];
396                         first->GetDimensions(dim);
397
398                         out->SetDimensions(dim[0], dim[1], im.size() );
399                         out->AllocateScalars();
400                         out->Update();
401
402                         unsigned long imsize = dim[0] * dim[1];
403                         imsize = imsize * dim[2] ;  // deal with multiframes // JPR
404
405
406 //EED 03-11-2009
407                         // differents formats char , short, etc...
408                         // differents components 1..3  ex. jpg ->RGB 3
409                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
410
411
412                         // Order the file name vector
413
414                         double spc[3];
415                         first->GetSpacing(spc);
416
417                         // OrderTheFileNameVector is not here anymore.
418                         // Try orderFilesWithZSpacing from OutputModel FCY
419                         // spc[2]=OrderTheFileNameVector(im);   
420                         spc[2] =1;
421
422                         out->SetSpacing(spc);
423
424                         int slice = 0;
425                         std::vector<std::string>::iterator it;
426
427                         for (it=im.begin(); it!=im.end(); ++it) 
428                         {
429                                 vtkImageData* cur = mReader.GetImage( (*it) );
430                                 memcpy(out->GetScalarPointer(0,0,slice), cur->GetScalarPointer(0,0,0), imsize);
431                                 slice++;
432                         }       
433                         s.push_back(out);
434
435                 }  // dimension == 3
436
437         }  // size >1
438
439 }
440   //======================================================================
441
442
443
444         //////////////////////////////////////////////////////////
445         // Test if the image is a multiple or single frame. 
446         // For the moment only with the creation of vtkImageDta.
447         // TO DO with image size and dim!!!
448         //////////////////////////////////////////////////////////
449         bool GimmickView::isSingle(const std::string i_file)
450         {
451                 bool bres = true;
452                 vtkImageData* first = mReader.GetImage( i_file);
453                 int dim[3];
454                 first->GetDimensions(dim);
455                 if (dim[2] > 1)
456                 {
457                         bres = false;
458                 }
459                 else
460                 {
461                 }
462                 return bres;
463         }
464
465         //////////////////////////////////////////////////////////
466         // get Attributes values for a file
467         //////////////////////////////////////////////////////////
468         
469         void GimmickView::getAttributes(const std::string i_file, std::map<std::string, std::string> &o_infos, OutputAttr i_attr)
470         {
471                 if(i_attr.inside.size() >0)
472                 {
473                         mGimmick->GetAttributes(i_file,o_infos,i_attr);
474                 }
475                 if(i_attr.outside.size()>0)
476                 {
477                         mReader.getAttributes(i_file,o_infos, i_attr.outside);
478                 }
479         }
480
481         //////////////////////////////////////////////////////////
482         // create an output structure with n entries = n output
483         //////////////////////////////////////////////////////////
484         void GimmickView::readImages1(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
485                          OutputAttr i_attr)
486         {
487                 std::vector<std::string>::iterator it;
488                 for (it=im.begin(); it!=im.end(); ++it)
489                 {
490                         OutStrGimmick out;
491                         out.img = vtkImageData::New();
492                         out.img->ShallowCopy(mReader.GetImage(*it));
493                         if(i_attr.mult)
494                                 getAttributes((*it),out.infos,i_attr);
495                         o_output.push_back(out);
496                 }
497                 // If we want only one output information structure, we set it outside the loop
498                 if(!i_attr.mult)
499                 {
500                         getAttributes(im.front(), o_output.front().infos, i_attr);
501                 }
502                 
503         }
504
505         //////////////////////////////////////////////////////////
506         // create an output structure with n entries = 1 output
507         //////////////////////////////////////////////////////////
508         void GimmickView::readImages3(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
509                          OutputAttr i_attr, double i_zspc)
510         {
511                 OutStrGimmick out;
512                 vtkImageData* first = mReader.GetImage( im.front());
513                 out.img  = vtkImageData::New();
514                 out.img->SetScalarType(first->GetScalarType());
515                 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
516                 int ext[6];
517                 first->GetWholeExtent(ext);  // send also 0,0 in Z 
518                 if(ext[5] == 0)
519                 {
520                         ext[5] = im.size()-1;
521                 }
522                 else
523                 {
524                         ext[5] = ext[5] * im.size()-1; // to deal with multiframes 
525                 }
526                 out.img->SetExtent(ext);
527                 int dim[3];
528                 double spac[3];
529                 first->GetDimensions(dim);
530                 first->GetSpacing(spac);
531                 out.img->SetSpacing(spac);
532                 out.img->SetDimensions(dim[0], dim[1], im.size() );
533                 out.img->AllocateScalars();
534                 out.img->Update();
535                 unsigned long imsize = dim[0] * dim[1];
536                 imsize = imsize * dim[2] ;  // deal with multiframes here
537                 // differents formats char , short, etc...
538                 // differents components 1..3  ex. jpg ->RGB 3
539                 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
540                 // Order the file name vector already did with the OutputModel!!!
541                 //!!!!out.img->SetSpacing(i_zspc);
542                 int slice = 0;
543                 std::vector<std::string>::iterator it;
544                 for (it=im.begin(); it!=im.end(); ++it) 
545                 {
546                         vtkImageData* cur = mReader.GetImage( (*it) );
547                         memcpy(out.img->GetScalarPointer(0,0,slice), cur->GetScalarPointer(0,0,0), imsize);
548                         slice++;
549                 }       
550                 getAttributes(im.front(),out.infos, i_attr);
551                 o_output.push_back(out);
552         }
553
554
555         // TO DO  NO VERY SURE : NEED TO BE TESTED
556         //////////////////////////////////////////////////////////
557         // create an output structure with n entries (T size) = T output (n size)
558         //////////////////////////////////////////////////////////
559         void GimmickView::readImages2(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
560                          OutputAttr i_attr, double i_zspc)
561         {
562                 vtkImageData* first = mReader.GetImage( im.front());
563                 int dim[3];
564                 double spac[3];
565                 first->GetDimensions(dim);
566                 first->GetSpacing(spac);
567                 // differents formats char , short, etc...
568                 // differents components 1..3  ex. jpg ->RGB 3
569                 unsigned long imsize = dim[0] * dim[1];
570                 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
571
572                 // Order the file name vector already did with the OutputModel!!!
573                 std::vector<std::string>::iterator it;
574                 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
575
576                 for (it=im.begin(); it!=im.end(); ++it, it_out += dim[2])
577                 {
578                         vtkImageData* cur = mReader.GetImage( (*it) );
579                         for (int slice= 0 ; slice <dim[2]; slice++)
580                         {
581                                 OutStrGimmick out;
582                                 out.img = vtkImageData::New();
583                                 out.img->SetScalarType(first->GetScalarType());
584                                 out.img->SetSpacing(spac);
585                                 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
586                                 int ext[6];
587                                 first->GetWholeExtent(ext);  // send also 0,0 in Z 
588                                 ext[5] = 0;
589                                 out.img->SetExtent(ext);
590
591                                 out.img->SetDimensions(dim[0], dim[1], 1 );
592                                 out.img->AllocateScalars();
593                                 out.img->Update();
594                                 memcpy(out.img->GetScalarPointer(0,0,0), cur->GetScalarPointer(0,0,slice), imsize);
595                                 o_output.push_back(out);
596                         }
597                         if(i_attr.mult)
598                                 getAttributes((*it),(*it_out).infos,i_attr);
599                 }
600                 if(!i_attr.mult)
601                 {
602                         getAttributes(im.front(), o_output.front().infos,i_attr);
603                 }
604
605         }
606
607         //////////////////////////////////////////////////////////
608         // create an output structure with n entries (T size) = T + n output
609         //////////////////////////////////////////////////////////
610         void GimmickView::readImages4(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
611                          OutputAttr i_attr)
612         {
613                 std::vector<std::string>::iterator it;
614                 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
615                 vtkImageData* first = mReader.GetImage( im.front());
616                 int dim[3];
617                 first->GetDimensions(dim);
618                         
619                 for (int slice= 0 ; slice <dim[2]; slice++)
620                 {
621                         OutStrGimmick out;
622                         out.img = vtkImageData::New();
623                         out.img->SetScalarType(first->GetScalarType());
624                         out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
625                         
626                         int ext[6];
627                         double spac[6];
628                         first->GetWholeExtent(ext);  // send also 0,0 in Z 
629                         ext[5] = 0;
630                         out.img->SetExtent(ext);
631                         first->GetSpacing(spac);
632                         out.img->SetSpacing(spac);
633                         out.img->SetDimensions(dim[0], dim[1], im.size() );
634                         out.img->AllocateScalars();
635                         out.img->Update();
636                         unsigned long imsize = dim[0] * dim[1];
637                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
638                         int index = 0;
639         
640                         for (it=im.begin(); it!=im.end(); ++it, index ++)
641                         {
642                                 vtkImageData* cur = mReader.GetImage( (*it) );
643                                 memcpy(out.img->GetScalarPointer(0,0,index), cur->GetScalarPointer(0,0,slice), imsize);
644                                 o_output.push_back(out);
645                         }
646                 }
647                 if(!i_attr.mult) // No sense to take informations in all images
648                 {
649                         getAttributes(im.front(), o_output.front().infos,i_attr);
650                 }
651
652         }
653
654
655 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
656 // Global function to read Images and create wished output (informations, multiple, type and size of output...)
657 // In function of type (file, vector) and size, the correct readImages function is selected
658 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
659         void GimmickView::readImages(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
660                          OutputAttr i_attr, int i_dim, double i_zspc)
661         {
662                 int size = im.size();
663                 if ( size == 0)
664                 {
665                         return;
666                 }
667                 else if (size == 1)
668                 {
669                         // Simplest case
670                         // Only one image : give it
671                         // But take in count multiframe possibility
672                         if ( isSingle(im.front()) || i_dim != 1)
673                         {
674                                  readImages1(o_output,im, i_attr);
675                         }
676                         else
677                         {
678                                 readImages2(o_output,im, i_attr,i_zspc);
679                         }
680                         
681                 }
682                 else
683                 {
684                         // multiple or single frame
685                         if ( isSingle(im.front()) )
686                         {
687                                 //we deal with 2D images
688                                 if(i_dim == 1)
689                                 {
690                                         // 2D to 3D
691                                         readImages3(o_output,im, i_attr,i_zspc);
692                                 }
693                                 else
694                                 {
695                                         readImages1(o_output,im, i_attr);
696                                 }
697                         }
698                         else
699                         {
700                                 // we deal with multiple frames n x (2D x T)
701                                 // Differents outputs are avaialable
702                                 if(i_dim == 1)
703                                 {
704                                         // put all in one output
705                                         readImages3(o_output,im, i_attr,i_zspc);
706
707                                 }
708                                 else if( i_dim == 2)
709                                 {
710                                         // put in a vector of n x T (2D)
711                                         readImages2(o_output,im, i_attr,i_zspc);
712                                 }
713                                 else if( i_dim == 3)
714                                 {
715                                         // put in a vector of n (2D x T)
716                                         // No transformations.
717                                         readImages1(o_output,im, i_attr);
718                                 }
719                                 else
720                                 {
721                                         // put in a vector of T (2D x n)
722                                         readImages4(o_output,im, i_attr);
723                                 }
724                         }
725                 }
726         }
727
728
729
730 void GimmickView::ReadImagesNotThreadedInVector(std::vector<vtkImageData*>& s, std::vector<std::string> im, int dimension)
731 {
732         // Create the output data
733         if (im.size()==1)
734         {
735                 // Only one image : give it
736                 vtkImageData* out = vtkImageData::New();
737                 GimmickDebugMessage(3, "State Check: Full Filename: "
738                                                 <<im.front()
739                                                 <<std::endl);
740                 out->ShallowCopy(mReader.GetImage(im.front()));
741                 s.push_back( out );
742         }
743         else if (im.size()>1) // Test inutile ? JPR
744         {
745                 /// \TODO fix unused variable 'first'
746                 vtkImageData* first = mReader.GetImage( im.front());
747                 if (dimension == 2)
748                 {
749                  // n3D
750                   std::vector<std::string>::iterator it;
751                         for (it=im.begin(); it!=im.end(); ++it)
752                         {
753                                 vtkImageData* out = vtkImageData::New();
754                                 out->ShallowCopy(mReader.GetImage(*it));
755                                 s.push_back(out);
756                         }
757                 }
758                 else
759                 {
760                         // n2D to 3D // NO!
761                         // n *2D + T in a vector :
762                         
763                         std::vector<std::string>::iterator it;
764                         for (it=im.begin(); it!=im.end(); ++it) 
765                         {
766                                 vtkImageData* out = mReader.GetImage( (*it));
767                                 s.push_back(out);
768                         }
769                 }
770         }
771 }
772   //======================================================================
773
774   //======================================================================
775   ///Requests the reading of an image
776   void GimmickView::RequestReading(tree::Node* n, 
777           int prio, int selection_index, boost::shared_ptr<ImagePointerHolder> p)
778   {
779           if(!mReaderStarted)
780           {
781                 mReader.Start();
782                 mReaderStarted=true;
783           }
784     ImageEventType t(n,selection_index);
785         t.pointerHolder = p;
786     mImageEventMap[n->GetAttribute("FullFileName")] = t;    
787     mReader.Request(this,n->GetAttribute("FullFileName"),prio);
788   }
789   //======================================================================
790
791   //======================================================================
792   void GimmickView::
793   OnMultiThreadImageReaderEvent(const std::string& filename,
794                                 MultiThreadImageReaderUser::EventType e,
795                                 vtkImageData* image)
796   {
797     GimmickDebugMessage(7,
798                         "MultiThreadImageReader event : "<<e<<std::endl);
799         if (e==ImageLoaded)
800         {
801                 if (filename.size()==0)
802                 {
803                   //What to do in this case?
804                   /*
805                         GimmickDebugMessage(5,
806                                         "Pushing unknown image in queue"
807                                         <<std::endl);
808                         mImageEventQueue.push_back(ImageEventType(image));*/
809                         return;
810                 }
811                 ImageEventTypeMap::iterator i;
812 //JCP 22-06-2009, test mImageEventMap.size() > 0
813                 if(mImageEventMap.size()>0){
814                         i = mImageEventMap.find(filename);
815                         if (i!=mImageEventMap.end())
816                         {
817                                 GimmickDebugMessage(5,
818                                                 "Putting image of file '"<<filename<<"' on pointer"
819                                                 <<std::endl);
820                                 ImageEventType ie(i->second);
821                                 ie.image = image;
822                                 ie.pointerHolder->Set(ie.image);
823                                 //mImageEventMap.erase(i);
824                         }
825                 }    
826         }
827         else if (e==Error)
828         {
829                 std::string mess="ERROR: MultiThreadImageReader: Cannot read image in file ";
830                 mess+=filename;
831                 mess+="\n";
832                 GimmickMessage(1,mess);
833                 ImageEventTypeMap::iterator i;
834                 i = mImageEventMap.find(filename);
835                 if (i!=mImageEventMap.end())
836                 {
837                 ImageEventType ie(i->second);
838                 ie.image = image;
839                 ie.pointerHolder->Set(GetDefaultImage());
840                 //mImageEventMap.erase(i);
841                 }
842         }
843
844         else if (e==ImageUnloaded)
845         {
846                 std::string mess="Unloaded image in file ";
847                 mess+=filename;
848                 mess+="\n";
849                 GimmickMessage(1,mess);
850                                 ImageEventTypeMap::iterator i;
851                 i = mImageEventMap.find(filename);
852                 if (i!=mImageEventMap.end())
853                 {
854                 ImageEventType ie(i->second);
855                 ie.image = image;
856                 ie.pointerHolder->Set(GetDefaultImage());
857                 //mImageEventMap.erase(i);
858                 }
859         }
860   }
861
862   //====================================================================
863
864   //====================================================================
865   void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
866   {
867     mValidationSignal.connect(callback);
868   }       
869   
870
871 #if defined(USE_GDCM)
872 ////////////////////////////////////////////////////////////////////////
873 void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
874 {
875         bool res = true;
876    std::vector<GDCM_NAME_SPACE::FileHelper *> filesH;
877    std::vector<std::string> suid;
878    std::map<std::string, std::string> msuid;
879    std::string tempuid = GDCM_NAME_SPACE::Util::CreateUniqueUID();
880    int i = 1;
881    std::vector<std::string>::iterator it = i_filenames.begin();
882    for(; it != i_filenames.end(); it++)
883    {
884
885            GDCM_NAME_SPACE::File *file;
886            file = GDCM_NAME_SPACE::File::New(  );
887            file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
888            file->SetFileName( (*it).c_str() );
889            res = file->Load();
890            if ( !res ) 
891            {
892                         std::cerr << "Sorry, " <<  (*it).c_str()  <<"  not a gdcm-readable "
893                  << "DICOM / ACR File" <<std::endl;
894                         file ->Delete();
895        //return 0;
896                 }
897                 std::cout << " ... is readable " << std::endl;
898                 
899                 // We need a gdcm::FileHelper, since we want to load the pixels        
900                 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(file);
901         
902    uint8_t *imageData = fh->GetImageData();
903
904            // Institution name 
905            file->AddAnonymizeElement(0x0008, 0x0080, "*"); 
906            // Patient's name 
907            file->AddAnonymizeElement(0x0010, 0x0010, "*");   
908            // Patient's ID
909            file->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
910            // Study Instance UID
911            file->AddAnonymizeElement(0x0020, 0x000d, tempuid );
912            // Telephone
913            file->AddAnonymizeElement(0x0010, 0x2154, "3615" );
914
915            // Aware user will add here more fields to anonymize here
916
917            // The gdcm::File is modified in memory
918
919            file->AnonymizeFile();
920
921            
922            i++;
923            fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
924    
925            fh->WriteDcmExplVR(file->GetFileName() +".ano1" );
926            std::cout << i <<"  End Anonymize" << std::cout;  
927            file->ClearAnonymizeList();
928            file->Delete();
929            fh->Delete();
930    }
931 }
932 #endif
933 #if defined(USE_GDCM2)
934 void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
935 {
936 }
937 #endif
938 ////////////////////////////////////////////////////////////////////////
939 //void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
940 //{
941 //
942 //      gdcm::FileMetaInformation::SetSourceApplicationEntityTitle( "gdcmanon" );
943 //    gdcm::Global& g = gdcm::Global::GetInstance();
944 //      //if( !resourcespath )
945 // //   {
946 //      //      const char *xmlpathenv = getenv("GDCM_RESOURCES_PATH");
947 //      //      if( xmlpathenv )
948 //      //        {
949 //      //        // Make sure to look for XML dict in user explicitly specified dir first:
950 //      //        xmlpath = xmlpathenv;
951 //      //        resourcespath = 1;
952 //      //        }
953 // //   }
954 // // if( resourcespath )
955 // //   {
956 // //   // xmlpath is set either by the cmd line option or the env var
957 // //   if( !g.Prepend( xmlpath.c_str() ) )
958 // //     {
959 // //     std::cerr << "Specified Resources Path is not valid: " << xmlpath << std::endl;
960 // //     return 1;
961 // //     }
962 // //   }
963 //  // All set, then load the XML files:
964 //  if( !g.LoadResourcesFiles() )
965 //  {
966 //    return ;
967 //  }
968 //  const gdcm::Defs &defs = g.GetDefs(); (void)defs;
969 //  if( !rootuid )
970 //    {
971 //    // only read the env var if no explicit cmd line option
972 //    // maybe there is an env var defined... let's check
973 //    const char *rootuid_env = getenv("GDCM_ROOT_UID");
974 //    if( rootuid_env )
975 //      {
976 //      rootuid = 1;
977 //      root = rootuid_env;
978 //      }
979 //    }
980 //  if( rootuid )
981 //    {
982 //    // root is set either by the cmd line option or the env var
983 //    if( !gdcm::UIDGenerator::IsValid( root.c_str() ) )
984 //      {
985 //      std::cerr << "specified Root UID is not valid: " << root << std::endl;
986 //      return 1;
987 //      }
988 //    gdcm::UIDGenerator::SetRoot( root.c_str() );
989 //    }
990 //
991 //      if(type == 0)
992 //      {
993 //                 // Get private key/certificate
994 //           gdcm::CryptographicMessageSyntax cms;
995 //              if( !dumb_mode )
996 //              {
997 //                      if( !GetRSAKeys(cms, rsa_path.c_str(), cert_path.c_str() ) )
998 //                      {
999 //                              return 1;
1000 //                      }
1001 //                      cms.SetCipherType( ciphertype );
1002 //              }
1003 //
1004 //      // Setup gdcm::Anonymizer
1005 //      gdcm::Anonymizer anon;
1006 //              if( !dumb_mode )
1007 //              anon.SetCryptographicMessageSyntax( &cms );
1008 //
1009 //              if( dumb_mode )
1010 //              {
1011 //                      for(unsigned int i = 0; i < nfiles; ++i)
1012 //              {
1013 //              const char *in  = filenames[i].c_str();
1014 //              const char *out = outfilenames[i].c_str();
1015 //              if( !AnonymizeOneFileDumb(anon, in, out, empty_tags, remove_tags, replace_tags_value) )
1016 //        {
1017 //                      //std::cerr << "Could not anonymize: " << in << std::endl;
1018 //                      return 1;
1019 //              }
1020 //      }
1021 //    }
1022 //  //else
1023 //  //  {
1024 //  //  for(unsigned int i = 0; i < nfiles; ++i)
1025 //  //    {
1026 //  //    const char *in  = filenames[i].c_str();
1027 //  //    const char *out = outfilenames[i].c_str();
1028 //  //    if( !AnonymizeOneFile(anon, in, out) )
1029 //  //      {
1030 //  //      //std::cerr << "Could not anonymize: " << in << std::endl;
1031 //  //      return 1;
1032 //  //      }
1033 //  //    }
1034 //  //  }
1035 //      }
1036 //}
1037 //
1038 } // EO namespace creaImageIO