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