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