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