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