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