]> Creatis software - creaImageIO.git/blob - src/creaImageIOGimmickView.cpp
2ff7855fdb30222137cfbde27148fe8bba39f624
[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 printf("EED GimmickView::isSingle Start %s\n" , i_file.c_str() );
468                 bool bres = true;
469                 vtkImageData* first = mReader.GetImage( i_file);
470 printf("EED GimmickView::isSingle 1\n"  );
471                 int dim[3];
472                 first->GetDimensions(dim);
473                 if (dim[2] > 1)
474                 {
475                         bres = false;
476                 }
477                 else
478                 {
479                 }
480 printf("EED GimmickView::isSingle end\n");
481
482                 return bres;
483         }
484
485         //////////////////////////////////////////////////////////
486         // get Attributes values for a file
487         //////////////////////////////////////////////////////////
488         
489         void GimmickView::getAttributes(const std::string i_file, std::map<std::string, std::string> &o_infos, OutputAttr i_attr)
490         {
491                 if(i_attr.inside.size() >0)
492                 {
493                         mGimmick->GetAttributes(i_file,o_infos,i_attr);
494                 }
495                 if(i_attr.outside.size()>0)
496                 {
497                         mReader.getAttributes(i_file,o_infos, i_attr.outside);
498                 }
499         }
500
501         //////////////////////////////////////////////////////////
502         // create an output structure with n entries = n output
503         //////////////////////////////////////////////////////////
504         void GimmickView::readImages1(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
505                          OutputAttr i_attr)
506         {
507                 i_attr.outside.push_back("D0019_100a");  // simens Number Of Images In Mosaic
508                 std::vector<std::string>::iterator it;
509                 for (it=im.begin(); it!=im.end(); ++it)
510                 {
511                         OutStrGimmick out;
512                         out.img = vtkImageData::New();
513                         out.img->ShallowCopy(mReader.GetImage(*it));
514                         if(i_attr.mult) 
515                         {
516                                 getAttributes((*it),out.infos,i_attr);
517                         }
518                         o_output.push_back(out);
519                 }
520                 // If we want only one output information structure, we set it outside the loop
521                 if(!i_attr.mult)
522                 {
523                         getAttributes(im.front(), o_output.front().infos, i_attr);
524                 }
525                 
526         }
527
528         //////////////////////////////////////////////////////////
529         // create an output structure with n entries = 1 output
530         //////////////////////////////////////////////////////////
531         void GimmickView::readImages3(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
532                          OutputAttr i_attr, double i_zspc)
533         {
534                 OutStrGimmick out;
535                 vtkImageData* first = mReader.GetImage( im.front() );
536                 out.img  = vtkImageData::New();
537                 int ext[6];
538 //EED 2017-01-01 Migration VTK7
539 #if VTK_MAJOR_VERSION <= 5
540                 first->GetWholeExtent(ext);  // send also 0,0 in Z 
541 #else
542                 first->GetExtent(ext);  // send also 0,0 in Z 
543 #endif
544                 if(ext[5] == 0)
545                 {
546                         ext[5] = (int)im.size()-1;
547                 } else {
548                         ext[5] = ext[5] * (int)im.size()-1; // to deal with multiframes 
549                 }
550                 out.img->SetExtent(ext);
551                 int dim[3];
552                 double spac[3];
553                 first->GetDimensions(dim);
554                 first->GetSpacing(spac);
555                 out.img->SetSpacing(spac);
556                 out.img->SetDimensions(dim[0], dim[1], (int)im.size() );
557
558
559
560 //EED 2017-01-01 Migration VTK7
561 #if VTK_MAJOR_VERSION <= 5
562                 out.img->SetScalarType(first->GetScalarType());
563                 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
564                 out.img->AllocateScalars();
565                 out.img->Update();
566 #else
567                 out.img->AllocateScalars(first->GetScalarType(), first->GetNumberOfScalarComponents());
568 #endif
569
570                 unsigned long imsize = dim[0] * dim[1];
571                 imsize = imsize * dim[2] ;  // deal with multiframes here
572                 // differents formats char , short, etc...
573                 // differents components 1..3  ex. jpg ->RGB 3
574                 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
575                 // Order the file name vector already did with the OutputModel!!!
576                 //!!!!out.img->SetSpacing(i_zspc);
577                 int slice = 0;
578                 std::vector<std::string>::iterator it;
579                 
580                 for (it=im.begin(); it!=im.end(); ++it) 
581                 {
582                         vtkImageData* cur = mReader.GetImage( (*it) );
583                         memcpy(out.img->GetScalarPointer(0,0,slice), cur->GetScalarPointer(0,0,0), imsize);
584                         slice++;
585                 } // for it     
586                 
587                 getAttributes(im.front(),out.infos, i_attr);
588                 o_output.push_back(out);
589         }
590
591
592         // TO DO  NO VERY SURE : NEED TO BE TESTED
593         //////////////////////////////////////////////////////////
594         // create an output structure with n entries (T size) = T output (n size)
595         //////////////////////////////////////////////////////////
596         void GimmickView::readImages2(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
597                          OutputAttr i_attr, double i_zspc)
598         {
599                 vtkImageData* first = mReader.GetImage( im.front());
600                 int dim[3];
601                 double spac[3];
602                 first->GetDimensions(dim);
603                 first->GetSpacing(spac);
604                 // differents formats char , short, etc...
605                 // differents components 1..3  ex. jpg ->RGB 3
606                 unsigned long imsize = dim[0] * dim[1];
607                 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
608
609                 // Order the file name vector already did with the OutputModel!!!
610                 std::vector<std::string>::iterator it;
611                 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
612
613                 for (it=im.begin(); it!=im.end(); ++it)//, it_out ++)
614                 {
615                         vtkImageData* cur = mReader.GetImage( (*it) );
616                         for (int slice= 0 ; slice <dim[2]; slice++)
617                         {
618                                 OutStrGimmick out;
619                                 out.img = vtkImageData::New();
620                                 out.img->SetSpacing(spac);
621                                 int ext[6];
622 //EED 2017-01-01 Migration VTK7
623 #if VTK_MAJOR_VERSION <= 5
624                                 first->GetWholeExtent(ext);  // send also 0,0 in Z 
625 #else
626                                 first->GetExtent(ext);  // send also 0,0 in Z 
627 #endif
628                                 ext[5] = 0;
629                                 out.img->SetExtent(ext);
630                                 out.img->SetDimensions(dim[0], dim[1], 1 );
631
632 //EED 2017-01-01 Migration VTK7
633 #if VTK_MAJOR_VERSION <= 5
634                                 out.img->SetScalarType(first->GetScalarType());
635                                 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
636                                 out.img->AllocateScalars();
637                                 out.img->Update();
638 #else
639                                 out.img->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
640 #endif
641
642
643                                 memcpy(out.img->GetScalarPointer(0,0,0), cur->GetScalarPointer(0,0,slice), imsize);
644                                 o_output.push_back(out);
645                         }
646         //              if(i_attr.mult)
647                 //              getAttributes((*it),(*it_out).infos,i_attr);
648                 }
649                 if(!i_attr.mult)
650                 {
651                         getAttributes(im.front(), o_output.front().infos,i_attr);
652                 }
653
654         }
655
656         //////////////////////////////////////////////////////////
657         // create an output structure with n entries (T size) = T + n output
658         //////////////////////////////////////////////////////////
659         void GimmickView::readImages4(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
660                          OutputAttr i_attr)
661         {
662                 std::vector<std::string>::iterator it;
663                 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
664                 vtkImageData* first = mReader.GetImage( im.front());
665                 int dim[3];
666                 first->GetDimensions(dim);
667                         
668                 for (int slice= 0 ; slice <dim[2]; slice++)
669                 {
670                         OutStrGimmick out;
671                         out.img = vtkImageData::New();
672                         
673                         int ext[6];
674
675 //EED 2017-01-01 Migration VTK7
676 #if VTK_MAJOR_VERSION <= 5
677                         first->GetWholeExtent(ext);  // send also 0,0 in Z 
678 #else
679                         first->GetExtent(ext);  // send also 0,0 in Z 
680 #endif
681
682                         double spac[6];
683                         ext[5] = 0;
684                         out.img->SetExtent(ext);
685                         first->GetSpacing(spac);
686                         out.img->SetSpacing(spac);
687                         out.img->SetDimensions(dim[0], dim[1], (int)im.size() );
688
689 //EED 2017-01-01 Migration VTK7
690 #if VTK_MAJOR_VERSION <= 5
691                         out.img->SetScalarType(first->GetScalarType());
692                         out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
693                         out.img->AllocateScalars();
694                         out.img->Update();
695 #else
696                         out.img->AllocateScalars(first->GetScalarType(), first->GetNumberOfScalarComponents());
697 #endif
698
699                         unsigned long imsize = dim[0] * dim[1];
700                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
701                         int index = 0;
702         
703                         for (it=im.begin(); it!=im.end(); ++it, index ++)
704                         {
705                                 vtkImageData* cur = mReader.GetImage( (*it) );
706                                 memcpy(out.img->GetScalarPointer(0,0,index), cur->GetScalarPointer(0,0,slice), imsize);
707                                 o_output.push_back(out);
708                         }
709                 }
710                 if(!i_attr.mult) // No sense to take informations in all images
711                 {
712                         getAttributes(im.front(), o_output.front().infos,i_attr);
713                 }
714
715         }
716
717
718 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
719 // Global function to read Images and create wished output (informations, multiple, type and size of output...)
720 // In function of type (file, vector) and size, the correct readImages function is selected
721 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
722         void GimmickView::readImages(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
723                          OutputAttr i_attr, int i_dim, double i_zspc)
724         {
725                 int size = (int)im.size();
726                 if ( size == 0)
727                 {
728                         return;
729                 } else if (size == 1) {
730                         // Simplest case
731                         // Only one image : give it
732                         // But take in count multiframe possibility
733                         if ( isSingle(im.front()) || i_dim != 1)
734                         {
735                                  readImages1(o_output,im, i_attr);
736                         } else {
737                                  readImages2(o_output,im, i_attr,i_zspc);
738                         }                       
739                 } else {
740                         // multiple or single frame
741                         if ( isSingle(im.front()) )
742                         {
743                                 //we deal with 2D images
744                                 if(i_dim == 1)
745                                 {
746                                         // 2D to 3D
747                                         readImages3(o_output,im, i_attr,i_zspc);
748                                 } else {
749                                         readImages1(o_output,im, i_attr);
750                                 }
751                         } else {
752                                 // we deal with multiple frames n x (2D x T)
753                                 // Differents outputs are avaialable
754                                 if(i_dim == 1)
755                                 {
756                                         // put all in one output
757                                         readImages3(o_output,im, i_attr,i_zspc);
758                                 } else if( i_dim == 2) {
759                                         // put in a vector of n x T (2D)
760                                         readImages2(o_output,im, i_attr,i_zspc);
761                                 } else if( i_dim == 3) {
762                                         // put in a vector of n (2D x T)
763                                         // No transformations.
764                                         readImages1(o_output,im, i_attr);
765                                 } else {
766                                         // put in a vector of T (2D x n)
767                                         readImages4(o_output,im, i_attr);
768                                 } // i_dim
769                         } //isSingle(im.front())
770                 } // if size
771
772 //EED UnMosaic step...  
773 //How to verifie if is a mosaic file , with how many images inside??
774         }
775
776
777
778 void GimmickView::ReadImagesNotThreadedInVector(std::vector<vtkImageData*>& s, std::vector<std::string> im, int dimension)
779 {
780         // Create the output data
781         if (im.size()==1)
782         {
783                 // Only one image : give it
784                 vtkImageData* out = vtkImageData::New();
785                 GimmickDebugMessage(3, "State Check: Full Filename: "
786                                                 <<im.front()
787                                                 <<std::endl);
788                 out->ShallowCopy(mReader.GetImage(im.front()));
789                 s.push_back( out );
790         }
791         else if (im.size()>1) // Test inutile ? JPR
792         {
793                 /// \TODO fix unused variable 'first'
794 //              vtkImageData* first = mReader.GetImage( im.front());
795                 if (dimension == 2)
796                 {
797                  // n3D
798                   std::vector<std::string>::iterator it;
799                         for (it=im.begin(); it!=im.end(); ++it)
800                         {
801                                 vtkImageData* out = vtkImageData::New();
802                                 out->ShallowCopy(mReader.GetImage(*it));
803                                 s.push_back(out);
804                         } // for
805                 } else {
806                         // n2D to 3D // NO!
807                         // n *2D + T in a vector :
808                         
809                         std::vector<std::string>::iterator it;
810                         for (it=im.begin(); it!=im.end(); ++it) 
811                         {
812                                 vtkImageData* out = mReader.GetImage( (*it));
813                                 s.push_back(out);
814                         } // for
815                 } // if dimension
816         } // if im.size
817 }
818   //======================================================================
819
820   //======================================================================
821   ///Requests the reading of an image
822   void GimmickView::RequestReading(tree::Node* n, 
823           int prio, int selection_index, boost::shared_ptr<ImagePointerHolder> p)
824   {
825           if(!mReaderStarted)
826           {
827                 mReader.Start();
828                 mReaderStarted=true;
829           }
830     ImageEventType t(n,selection_index);
831         t.pointerHolder = p;
832     mImageEventMap[n->GetAttribute("FullFileName")] = t;    
833     mReader.Request(this,n->GetAttribute("FullFileName"),prio);
834   }
835   //======================================================================
836
837   //======================================================================
838   void GimmickView::
839   OnMultiThreadImageReaderEvent(const std::string& filename,
840                                 MultiThreadImageReaderUser::EventType e,
841                                 vtkImageData* image)
842   {
843     GimmickDebugMessage(7,
844                         "MultiThreadImageReader event : "<<e<<std::endl);
845         if (e==ImageLoaded)
846         {
847                 if (filename.size()==0)
848                 {
849                   //What to do in this case?
850                   /*
851                         GimmickDebugMessage(5,
852                                         "Pushing unknown image in queue"
853                                         <<std::endl);
854                         mImageEventQueue.push_back(ImageEventType(image));*/
855                         return;
856                 }
857                 ImageEventTypeMap::iterator i;
858 //JCP 22-06-2009, test mImageEventMap.size() > 0
859                 if(mImageEventMap.size()>0){
860                         i = mImageEventMap.find(filename);
861                         if (i!=mImageEventMap.end())
862                         {
863                                 GimmickDebugMessage(5,
864                                                 "Putting image of file '"<<filename<<"' on pointer"
865                                                 <<std::endl);
866                                 ImageEventType ie(i->second);
867                                 ie.image = image;
868                                 ie.pointerHolder->Set(ie.image);
869                                 //mImageEventMap.erase(i);
870                         }
871                 }    
872         }
873         else if (e==Error)
874         {
875                 std::string mess="ERROR: MultiThreadImageReader: Cannot read image in file ";
876                 mess+=filename;
877                 mess+="\n";
878                 GimmickMessage(1,mess);
879                 ImageEventTypeMap::iterator i;
880                 i = mImageEventMap.find(filename);
881                 if (i!=mImageEventMap.end())
882                 {
883                 ImageEventType ie(i->second);
884                 ie.image = image;
885                 ie.pointerHolder->Set(GetDefaultImage());
886                 //mImageEventMap.erase(i);
887                 }
888         }
889
890         else if (e==ImageUnloaded)
891         {
892                 std::string mess="Unloaded image in file ";
893                 mess+=filename;
894                 mess+="\n";
895                 GimmickMessage(1,mess);
896                                 ImageEventTypeMap::iterator i;
897                 i = mImageEventMap.find(filename);
898                 if (i!=mImageEventMap.end())
899                 {
900                 ImageEventType ie(i->second);
901                 ie.image = image;
902                 ie.pointerHolder->Set(GetDefaultImage());
903                 //mImageEventMap.erase(i);
904                 }
905         }
906   }
907
908   //====================================================================
909
910   //====================================================================
911   void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
912   {
913     mValidationSignal.connect(callback);
914   }       
915   
916
917 #if defined(USE_GDCM)
918 ////////////////////////////////////////////////////////////////////////
919 void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
920 {
921         bool res = true;
922    std::vector<GDCM_NAME_SPACE::FileHelper *> filesH;
923    std::vector<std::string> suid;
924    std::map<std::string, std::string> msuid;
925    std::string tempuid = GDCM_NAME_SPACE::Util::CreateUniqueUID();
926    int i = 1;
927    std::vector<std::string>::iterator it = i_filenames.begin();
928    for(; it != i_filenames.end(); it++)
929    {
930
931            GDCM_NAME_SPACE::File *file;
932            file = GDCM_NAME_SPACE::File::New(  );
933            file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
934            file->SetFileName( (*it).c_str() );
935            res = file->Load();
936            if ( !res ) 
937            {
938                         std::cerr << "Sorry, " <<  (*it).c_str()  <<"  not a gdcm-readable "
939                  << "DICOM / ACR File" <<std::endl;
940                         file ->Delete();
941        //return 0;
942                 }
943                 std::cout << " ... is readable " << std::endl;
944                 
945                 // We need a gdcm::FileHelper, since we want to load the pixels        
946                 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(file);
947
948 //Borrame
949 //EED   uint8_t *imageData = fh->GetImageData();
950
951            // Institution name 
952            file->AddAnonymizeElement(0x0008, 0x0080, "*"); 
953            // Patient's name 
954            file->AddAnonymizeElement(0x0010, 0x0010, "*");   
955            // Patient's ID
956            file->AddAnonymizeElement( 0x0010, 0x0020,"1515" );   
957            // Study Instance UID
958            file->AddAnonymizeElement(0x0020, 0x000d, tempuid );
959            // Telephone
960            file->AddAnonymizeElement(0x0010, 0x2154, "3615" );
961
962            // Aware user will add here more fields to anonymize here
963
964            // The gdcm::File is modified in memory
965
966            file->AnonymizeFile();
967
968            
969            i++;
970            fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
971    
972            fh->WriteDcmExplVR(file->GetFileName() +".ano1" );
973            std::cout << i <<"  End Anonymize" << std::endl;  
974            file->ClearAnonymizeList();
975            file->Delete();
976            fh->Delete();
977    }
978 }
979 #endif
980 #if defined(USE_GDCM2)
981 void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
982 {
983 }
984 #endif
985 ////////////////////////////////////////////////////////////////////////
986 //void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
987 //{
988 //
989 //      gdcm::FileMetaInformation::SetSourceApplicationEntityTitle( "gdcmanon" );
990 //    gdcm::Global& g = gdcm::Global::GetInstance();
991 //      //if( !resourcespath )
992 // //   {
993 //      //      const char *xmlpathenv = getenv("GDCM_RESOURCES_PATH");
994 //      //      if( xmlpathenv )
995 //      //        {
996 //      //        // Make sure to look for XML dict in user explicitly specified dir first:
997 //      //        xmlpath = xmlpathenv;
998 //      //        resourcespath = 1;
999 //      //        }
1000 // //   }
1001 // // if( resourcespath )
1002 // //   {
1003 // //   // xmlpath is set either by the cmd line option or the env var
1004 // //   if( !g.Prepend( xmlpath.c_str() ) )
1005 // //     {
1006 // //     std::cerr << "Specified Resources Path is not valid: " << xmlpath << std::endl;
1007 // //     return 1;
1008 // //     }
1009 // //   }
1010 //  // All set, then load the XML files:
1011 //  if( !g.LoadResourcesFiles() )
1012 //  {
1013 //    return ;
1014 //  }
1015 //  const gdcm::Defs &defs = g.GetDefs(); (void)defs;
1016 //  if( !rootuid )
1017 //    {
1018 //    // only read the env var if no explicit cmd line option
1019 //    // maybe there is an env var defined... let's check
1020 //    const char *rootuid_env = getenv("GDCM_ROOT_UID");
1021 //    if( rootuid_env )
1022 //      {
1023 //      rootuid = 1;
1024 //      root = rootuid_env;
1025 //      }
1026 //    }
1027 //  if( rootuid )
1028 //    {
1029 //    // root is set either by the cmd line option or the env var
1030 //    if( !gdcm::UIDGenerator::IsValid( root.c_str() ) )
1031 //      {
1032 //      std::cerr << "specified Root UID is not valid: " << root << std::endl;
1033 //      return 1;
1034 //      }
1035 //    gdcm::UIDGenerator::SetRoot( root.c_str() );
1036 //    }
1037 //
1038 //      if(type == 0)
1039 //      {
1040 //                 // Get private key/certificate
1041 //           gdcm::CryptographicMessageSyntax cms;
1042 //              if( !dumb_mode )
1043 //              {
1044 //                      if( !GetRSAKeys(cms, rsa_path.c_str(), cert_path.c_str() ) )
1045 //                      {
1046 //                              return 1;
1047 //                      }
1048 //                      cms.SetCipherType( ciphertype );
1049 //              }
1050 //
1051 //      // Setup gdcm::Anonymizer
1052 //      gdcm::Anonymizer anon;
1053 //              if( !dumb_mode )
1054 //              anon.SetCryptographicMessageSyntax( &cms );
1055 //
1056 //              if( dumb_mode )
1057 //              {
1058 //                      for(unsigned int i = 0; i < nfiles; ++i)
1059 //              {
1060 //              const char *in  = filenames[i].c_str();
1061 //              const char *out = outfilenames[i].c_str();
1062 //              if( !AnonymizeOneFileDumb(anon, in, out, empty_tags, remove_tags, replace_tags_value) )
1063 //        {
1064 //                      //std::cerr << "Could not anonymize: " << in << std::endl;
1065 //                      return 1;
1066 //              }
1067 //      }
1068 //    }
1069 //  //else
1070 //  //  {
1071 //  //  for(unsigned int i = 0; i < nfiles; ++i)
1072 //  //    {
1073 //  //    const char *in  = filenames[i].c_str();
1074 //  //    const char *out = outfilenames[i].c_str();
1075 //  //    if( !AnonymizeOneFile(anon, in, out) )
1076 //  //      {
1077 //  //      //std::cerr << "Could not anonymize: " << in << std::endl;
1078 //  //      return 1;
1079 //  //      }
1080 //  //    }
1081 //  //  }
1082 //      }
1083 //}
1084 //
1085 } // EO namespace creaImageIO