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