1 #include <creaImageIOGimmickView.h>
2 #include <creaImageIOSystem.h>
3 #include "boost/filesystem.hpp"
6 #include <gdcmGlobal.h>
7 #include <gdcmSerieHelper.h>
8 #include <vtkGdcmReader.h>
11 #if defined(USE_GDCM2)
12 #include <vtkGDCMImageReader.h>
13 #include "gdcmSystem.h"
14 #include "gdcmCryptographicMessageSyntax.h"
15 #include "gdcmUIDGenerator.h"
16 #include "gdcmAnonymizer.h"
17 #include "gdcmGlobal.h"
21 namespace fs = boost::filesystem;
25 ///Class used to represent the actual state of the image selected and to perform comparisons on its values
29 ImageExtent(const std::string& x, const std::string& y, const std::string& z, const std::string& t)
31 sscanf(x.c_str(),"%d",&mExtent[0]);
32 sscanf(y.c_str(),"%d",&mExtent[1]);
33 sscanf(z.c_str(),"%d",&mExtent[2]);
34 sscanf(t.c_str(),"%d",&mExtent[3]);
35 if(x==""){mExtent[0]=1;}
36 if(y==""){mExtent[1]=1;}
37 if(z==""){mExtent[2]=1;}
38 if(t==""){mExtent[3]=1;}
40 if (mExtent[3]>1) mDim=4;
41 else if (mExtent[2]>1) mDim=3;
42 else if (mExtent[1]>1) mDim=2;
43 else if (mExtent[0]>1) mDim=1;
49 void Clear() { mExtent[0] = mExtent[1] = mExtent[2] = mExtent[3] = 1; }
51 ///Returns true if the two extents are compatible
52 bool IsCompatible( const ImageExtent& );
54 ///Adds the extent passed as a parameter to the current extent
55 void Add ( const ImageExtent& );
57 ///Returns the ieth position of the extent
58 int Get(int i) { return mExtent[i]; }
60 ///Returns the dimension of the current image
61 void SetDimension(int dim) { mDim=dim; }
63 ///Returns the dimension of the current image
64 int GetDimension() { return mDim; }
71 //======================================================================
73 //======================================================================
75 GimmickView::GimmickView(boost::shared_ptr<Gimmick> gimmick, int threads)
79 GimmickDebugMessage(1,"GimmickView::GimmickView"
81 // Anciently started the threads ...
82 // Threads now automatically start at first image request
86 //======================================================================
88 //======================================================================
90 GimmickView::~GimmickView()
92 GimmickDebugMessage(1,"GimmickView::~GimmickView"
95 //======================================================================
97 //======================================================================
98 /// Initializes the view :
99 /// Creates the TreeViews for all the TreeHandler of the Controller
101 void GimmickView::Initialize()
103 mReaderStarted=false;
105 //======================================================================
107 //======================================================================
109 void GimmickView::Finalize()
113 //======================================================================
115 //======================================================================
116 /// Create the tree views
117 void GimmickView::CreateTreeViews()
119 GimmickMessage(2,"Creating the tree views"<<std::endl);
120 Gimmick::TreeHandlerMapType::const_iterator i;
121 for (i = mGimmick->GetTreeHandlerMap().begin();
122 i!= mGimmick->GetTreeHandlerMap().end();
125 this->CreateTreeView(i->second);
129 /// Create a tree view with a given name
130 void GimmickView::CreateSingleTreeView(std::string &i_name)
132 this->CreateTreeView(mGimmick->GetTreeHandlerMap()[i_name]);
137 //======================================================================
139 //======================================================================
140 /// Updates the TreeView of given name from level l to bottom
141 /// (calls the virtual method TreeView::Update())
142 void GimmickView::UpdateTreeViewLevel(const std::string& t, int l)
144 TreeViewMapType::iterator i;
145 i = GetTreeViewMap().find(t);
146 if ( i == GetTreeViewMap().end() )
148 GimmickError("INTERNAL ERROR : GimmickView::UpdateTreeView : '"
149 <<t<<"' is not in TreeViewMap");
151 i->second->UpdateLevel(l);
154 //======================================================================
155 /// Clears the status and begins a new selection process
156 void GimmickView::ResetExtent()
160 mImageExtent.reset();
165 //======================================================================
167 //======================================================================
168 bool ImageExtent::IsCompatible(const ImageExtent& ie)
170 bool compatible=true;
171 ImageExtent * extent= (ImageExtent*)&ie;
172 if((*extent).Get(0)!=Get(0)
173 || (*extent).Get(1)!=Get(1))
180 //======================================================================
182 //======================================================================
183 void ImageExtent::Add(const ImageExtent& ie)
185 ImageExtent * extent= (ImageExtent*)&ie;
186 mExtent[2]+=(*extent).Get(2);
193 //======================================================================
194 /// No selected image
195 bool GimmickView::NoValidateSelected ()
197 GimmickDebugMessage(2,"Validating selected"<<std::endl);
198 std::string mMessage;
199 mMessage="Cannot have 0 images selected!";
201 modifyValidationSignal(valid);
202 SetMessage(mMessage);
206 //======================================================================
207 ///Validates the dimension compliance of the images with the maximum and
208 ///minimum given, and between their sizes
209 bool GimmickView::ValidateSelected (tree::Node* sel, int min_dim, int max_dim)
211 GimmickDebugMessage(2,"Validating selected"<<std::endl);
212 std::string mMessage;
216 mMessage="Cannot have 0 images selected!";
221 boost::shared_ptr<ImageExtent> ie=boost::shared_ptr<ImageExtent>(new ImageExtent((*sel).GetAttribute("D0028_0010"),
222 (*sel).GetAttribute("D0028_0011"),
223 (*sel).GetAttribute("D0028_0012"),
229 if((mImageExtent->Get(min_dim-1)<2)||(mImageExtent->Get(max_dim)>1))
235 std::stringstream out;
236 out << mImageExtent->GetDimension() << "D image " << mImageExtent->Get(0) << "x"<< mImageExtent->Get(1) << "x"<< mImageExtent->Get(2) <<" selected";
237 mMessage = out.str();
238 mImageExtent->SetDimension(2);
244 if(mImageExtent->IsCompatible(*ie))
246 if(mImageExtent->GetDimension()==max_dim && mImageExtent->Get(max_dim)>2)
248 std::stringstream out;
249 out<<"Cannot add this image to selection : would result in a "<<mImageExtent->GetDimension()+1<<"D image!";
255 std::stringstream out;
256 out<<"Selecting "<<mImageExtent->GetDimension()<<"D images is not allowed !";
260 else if(min_dim==3 && (ie->Get(2)+mImageExtent->Get(2))<2)
262 std::stringstream out;
263 out << "Cannot build the selection as it would result in a ";
264 out << mImageExtent->GetDimension();
265 out << "D image, and the minimum is ";
273 mImageExtent->Add(*ie);
274 std::stringstream out;
275 out << mImageExtent->GetDimension() << "D image " << mImageExtent->Get(0) << "x"<< mImageExtent->Get(1) << "x"<< mImageExtent->Get(2) <<" selected";
276 mMessage = out.str();
281 mMessage="The selected images are not compatible.";
287 modifyValidationSignal(valid);
288 SetMessage(mMessage);
292 //======================================================================
293 void GimmickView::modifyValidationSignal(bool ivalid)
296 mValidationSignal(ivalid);
299 //======================================================================
300 ///Reads Images (Non Threaded)
301 void GimmickView::ReadImagesNotThreaded(std::vector<vtkImageData*>& s, std::vector<std::string> im, int dimension)
306 #define GIMMICK_NO_IMAGE_SELECTION 0
307 #define GIMMICK_2D_IMAGE_SELECTION 2
308 #define GIMMICK_3D_IMAGE_SELECTION 3
309 #define GIMMICK_4D_IMAGE_SELECTION 4
316 // Create the output data
319 vtkImageData * out=vtkImageData::New();
320 out->ShallowCopy(mReader.GetImage(im.front()));
323 else if (im.size()>1) // Test inutile ? JPR
325 vtkImageData* first = mReader.GetImage( im.front());
329 std::vector<std::string>::iterator it;
330 for (it=im.begin(); it!=im.end(); ++it)
332 vtkImageData* out = vtkImageData::New();
333 out->ShallowCopy(mReader.GetImage(*it));
340 vtkImageData* out = vtkImageData::New();
341 // out->CopyStructure(first);
342 out->SetScalarType(first->GetScalarType());
343 out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
345 //first->GetExtent(ext); // JPR
346 first->GetWholeExtent(ext); // renvoie egalement 0,0 en Z // JPR
350 ext[5] = im.size()-1;
354 ext[5] = ext[5] * im.size()-1; // to deal with multiframes - JPR
358 // LG : TODO : Z Spacing ?
361 first->GetDimensions(dim);
363 out->SetDimensions(dim[0], dim[1], im.size() );
364 out->AllocateScalars();
367 unsigned long imsize = dim[0] * dim[1];
368 imsize = imsize * dim[2] ; // deal with multiframes // JPR
372 // differents formats char , short, etc...
373 // differents components 1..3 ex. jpg ->RGB 3
374 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
377 // Order the file name vector
380 first->GetSpacing(spc);
382 // OrderTheFileNameVector is not here anymore.
383 // Try orderFilesWithZSpacing from OutputModel FCY
384 // spc[2]=OrderTheFileNameVector(im);
387 out->SetSpacing(spc);
390 std::vector<std::string>::iterator it;
392 for (it=im.begin(); it!=im.end(); ++it)
394 vtkImageData* cur = mReader.GetImage( (*it) );
395 memcpy(out->GetScalarPointer(0,0,slice), cur->GetScalarPointer(0,0,0), imsize);
405 //======================================================================
409 //////////////////////////////////////////////////////////
410 // Test if the image is a multiple or single frame.
411 // For the moment only with the creation of vtkImageDta.
412 // TO DO with image size and dim!!!
413 //////////////////////////////////////////////////////////
414 bool GimmickView::isSingle(const std::string i_file)
417 vtkImageData* first = mReader.GetImage( i_file);
419 first->GetDimensions(dim);
430 //////////////////////////////////////////////////////////
431 // get Attributes values for a file
432 //////////////////////////////////////////////////////////
434 void GimmickView::getAttributes(const std::string i_file, std::map<std::string, std::string> &o_infos, OutputAttr i_attr)
436 if(i_attr.inside.size() >0)
438 mGimmick->GetAttributes(i_file,o_infos,i_attr);
440 if(i_attr.outside.size()>0)
442 mReader.getAttributes(i_file,o_infos, i_attr.outside);
446 //////////////////////////////////////////////////////////
447 // create an output structure with n entries = n output
448 //////////////////////////////////////////////////////////
449 void GimmickView::readImages1(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
452 std::vector<std::string>::iterator it;
453 for (it=im.begin(); it!=im.end(); ++it)
456 out.img = vtkImageData::New();
457 out.img->ShallowCopy(mReader.GetImage(*it));
459 getAttributes((*it),out.infos,i_attr);
460 o_output.push_back(out);
462 // If we want only one output information structure, we set it outside the loop
465 getAttributes(im.front(), o_output.front().infos, i_attr);
470 //////////////////////////////////////////////////////////
471 // create an output structure with n entries = 1 output
472 //////////////////////////////////////////////////////////
473 void GimmickView::readImages3(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
474 OutputAttr i_attr, double i_zspc)
477 vtkImageData* first = mReader.GetImage( im.front());
478 out.img = vtkImageData::New();
479 out.img->SetScalarType(first->GetScalarType());
480 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
482 first->GetWholeExtent(ext); // send also 0,0 in Z
485 ext[5] = im.size()-1;
489 ext[5] = ext[5] * im.size()-1; // to deal with multiframes
491 out.img->SetExtent(ext);
493 first->GetDimensions(dim);
494 out.img->SetDimensions(dim[0], dim[1], im.size() );
495 out.img->AllocateScalars();
497 unsigned long imsize = dim[0] * dim[1];
498 imsize = imsize * dim[2] ; // deal with multiframes here
499 // differents formats char , short, etc...
500 // differents components 1..3 ex. jpg ->RGB 3
501 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
502 // Order the file name vector already did with the OutputModel!!!
503 //!!!!out.img->SetSpacing(i_zspc);
505 std::vector<std::string>::iterator it;
506 for (it=im.begin(); it!=im.end(); ++it)
508 vtkImageData* cur = mReader.GetImage( (*it) );
509 memcpy(out.img->GetScalarPointer(0,0,slice), cur->GetScalarPointer(0,0,0), imsize);
512 getAttributes(im.front(),out.infos, i_attr);
513 o_output.push_back(out);
517 // TO DO NO VERY SURE : NEED TO BE TESTED
518 //////////////////////////////////////////////////////////
519 // create an output structure with n entries (T size) = T output (n size)
520 //////////////////////////////////////////////////////////
521 void GimmickView::readImages2(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
522 OutputAttr i_attr, double i_zspc)
524 vtkImageData* first = mReader.GetImage( im.front());
526 first->GetDimensions(dim);
527 // differents formats char , short, etc...
528 // differents components 1..3 ex. jpg ->RGB 3
529 unsigned long imsize = dim[0] * dim[1];
530 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
532 // Order the file name vector already did with the OutputModel!!!
533 std::vector<std::string>::iterator it;
534 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
536 for (it=im.begin(); it!=im.end(); ++it, it_out += dim[2])
538 vtkImageData* cur = mReader.GetImage( (*it) );
539 for (int slice= 0 ; slice <dim[2]; slice++)
542 out.img = vtkImageData::New();
543 out.img->SetScalarType(first->GetScalarType());
545 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
547 first->GetWholeExtent(ext); // send also 0,0 in Z
549 out.img->SetExtent(ext);
551 out.img->SetDimensions(dim[0], dim[1], 1 );
552 out.img->AllocateScalars();
554 memcpy(out.img->GetScalarPointer(0,0,0), cur->GetScalarPointer(0,0,slice), imsize);
555 o_output.push_back(out);
558 getAttributes((*it),(*it_out).infos,i_attr);
562 getAttributes(im.front(), o_output.front().infos,i_attr);
567 //////////////////////////////////////////////////////////
568 // create an output structure with n entries (T size) = T + n output
569 //////////////////////////////////////////////////////////
570 void GimmickView::readImages4(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
573 std::vector<std::string>::iterator it;
574 std::vector<OutStrGimmick>::iterator it_out = o_output.begin();
575 vtkImageData* first = mReader.GetImage( im.front());
577 first->GetDimensions(dim);
579 for (int slice= 0 ; slice <dim[2]; slice++)
582 out.img = vtkImageData::New();
583 out.img->SetScalarType(first->GetScalarType());
584 out.img->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
587 first->GetWholeExtent(ext); // send also 0,0 in Z
589 out.img->SetExtent(ext);
591 out.img->SetDimensions(dim[0], dim[1], im.size() );
592 out.img->AllocateScalars();
594 unsigned long imsize = dim[0] * dim[1];
595 imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
598 for (it=im.begin(); it!=im.end(); ++it, index ++)
600 vtkImageData* cur = mReader.GetImage( (*it) );
601 memcpy(out.img->GetScalarPointer(0,0,index), cur->GetScalarPointer(0,0,slice), imsize);
602 o_output.push_back(out);
605 if(!i_attr.mult) // No sense to take informations in all images
607 getAttributes(im.front(), o_output.front().infos,i_attr);
613 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
614 // Global function to read Images and create wished output (informations, multiple, type and size of output...)
615 // In function of type (file, vector) and size, the correct readImages function is selected
616 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
617 void GimmickView::readImages(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
618 OutputAttr i_attr, int i_dim, double i_zspc)
620 int size = im.size();
628 // Only one image : give it
629 // But take in count multiframe possibility
630 if ( isSingle(im.front()) || i_dim != 1)
632 readImages1(o_output,im, i_attr);
636 readImages2(o_output,im, i_attr,i_zspc);
642 // multiple or single frame
643 if ( isSingle(im.front()) )
645 //we deal with 2D images
649 readImages3(o_output,im, i_attr,i_zspc);
653 readImages1(o_output,im, i_attr);
658 // we deal with multiple frames n x (2D x T)
659 // Differents outputs are avaialable
662 // put all in one output
663 readImages3(o_output,im, i_attr,i_zspc);
668 // put in a vector of n x T (2D)
669 readImages2(o_output,im, i_attr,i_zspc);
673 // put in a vector of n (2D x T)
674 // No transformations.
675 readImages1(o_output,im, i_attr);
679 // put in a vector of T (2D x n)
680 readImages4(o_output,im, i_attr);
688 void GimmickView::ReadImagesNotThreadedInVector(std::vector<vtkImageData*>& s, std::vector<std::string> im, int dimension)
690 // Create the output data
693 // Only one image : give it
694 vtkImageData* out = vtkImageData::New();
695 GimmickDebugMessage(3, "State Check: Full Filename: "
698 out->ShallowCopy(mReader.GetImage(im.front()));
701 else if (im.size()>1) // Test inutile ? JPR
703 vtkImageData* first = mReader.GetImage( im.front());
707 std::vector<std::string>::iterator it;
708 for (it=im.begin(); it!=im.end(); ++it)
710 vtkImageData* out = vtkImageData::New();
711 out->ShallowCopy(mReader.GetImage(*it));
718 // n *2D + T in a vector :
720 std::vector<std::string>::iterator it;
721 for (it=im.begin(); it!=im.end(); ++it)
723 vtkImageData* out = mReader.GetImage( (*it));
729 //======================================================================
731 //======================================================================
732 ///Requests the reading of an image
733 void GimmickView::RequestReading(tree::Node* n,
734 int prio, int selection_index, boost::shared_ptr<ImagePointerHolder> p)
741 ImageEventType t(n,selection_index);
743 mImageEventMap[n->GetAttribute("FullFileName")] = t;
744 mReader.Request(this,n->GetAttribute("FullFileName"),prio);
746 //======================================================================
748 //======================================================================
750 OnMultiThreadImageReaderEvent(const std::string& filename,
751 MultiThreadImageReaderUser::EventType e,
754 GimmickDebugMessage(7,
755 "MultiThreadImageReader event : "<<e<<std::endl);
758 if (filename.size()==0)
760 //What to do in this case?
762 GimmickDebugMessage(5,
763 "Pushing unknown image in queue"
765 mImageEventQueue.push_back(ImageEventType(image));*/
768 ImageEventTypeMap::iterator i;
769 //JCP 22-06-2009, test mImageEventMap.size() > 0
770 if(mImageEventMap.size()>0){
771 i = mImageEventMap.find(filename);
772 if (i!=mImageEventMap.end())
774 GimmickDebugMessage(5,
775 "Putting image of file '"<<filename<<"' on pointer"
777 ImageEventType ie(i->second);
779 ie.pointerHolder->Set(ie.image);
780 //mImageEventMap.erase(i);
786 std::string mess="ERROR: MultiThreadImageReader: Cannot read image in file ";
789 GimmickMessage(1,mess);
790 ImageEventTypeMap::iterator i;
791 i = mImageEventMap.find(filename);
792 if (i!=mImageEventMap.end())
794 ImageEventType ie(i->second);
796 ie.pointerHolder->Set(GetDefaultImage());
797 //mImageEventMap.erase(i);
801 else if (e==ImageUnloaded)
803 std::string mess="Unloaded image in file ";
806 GimmickMessage(1,mess);
807 ImageEventTypeMap::iterator i;
808 i = mImageEventMap.find(filename);
809 if (i!=mImageEventMap.end())
811 ImageEventType ie(i->second);
813 ie.pointerHolder->Set(GetDefaultImage());
814 //mImageEventMap.erase(i);
819 //====================================================================
821 //====================================================================
822 void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
824 mValidationSignal.connect(callback);
829 ////////////////////////////////////////////////////////////////////////
830 //void GimmickView::Anonymize(std::vector<std::string> i_filenames, int type)
833 // gdcm::FileMetaInformation::SetSourceApplicationEntityTitle( "gdcmanon" );
834 // gdcm::Global& g = gdcm::Global::GetInstance();
835 // //if( !resourcespath )
837 // // const char *xmlpathenv = getenv("GDCM_RESOURCES_PATH");
838 // // if( xmlpathenv )
840 // // // Make sure to look for XML dict in user explicitly specified dir first:
841 // // xmlpath = xmlpathenv;
842 // // resourcespath = 1;
845 // // if( resourcespath )
847 // // // xmlpath is set either by the cmd line option or the env var
848 // // if( !g.Prepend( xmlpath.c_str() ) )
850 // // std::cerr << "Specified Resources Path is not valid: " << xmlpath << std::endl;
854 // // All set, then load the XML files:
855 // if( !g.LoadResourcesFiles() )
859 // const gdcm::Defs &defs = g.GetDefs(); (void)defs;
862 // // only read the env var if no explicit cmd line option
863 // // maybe there is an env var defined... let's check
864 // const char *rootuid_env = getenv("GDCM_ROOT_UID");
868 // root = rootuid_env;
873 // // root is set either by the cmd line option or the env var
874 // if( !gdcm::UIDGenerator::IsValid( root.c_str() ) )
876 // std::cerr << "specified Root UID is not valid: " << root << std::endl;
879 // gdcm::UIDGenerator::SetRoot( root.c_str() );
884 // // Get private key/certificate
885 // gdcm::CryptographicMessageSyntax cms;
888 // if( !GetRSAKeys(cms, rsa_path.c_str(), cert_path.c_str() ) )
892 // cms.SetCipherType( ciphertype );
895 // // Setup gdcm::Anonymizer
896 // gdcm::Anonymizer anon;
898 // anon.SetCryptographicMessageSyntax( &cms );
902 // for(unsigned int i = 0; i < nfiles; ++i)
904 // const char *in = filenames[i].c_str();
905 // const char *out = outfilenames[i].c_str();
906 // if( !AnonymizeOneFileDumb(anon, in, out, empty_tags, remove_tags, replace_tags_value) )
908 // //std::cerr << "Could not anonymize: " << in << std::endl;
915 // // for(unsigned int i = 0; i < nfiles; ++i)
917 // // const char *in = filenames[i].c_str();
918 // // const char *out = outfilenames[i].c_str();
919 // // if( !AnonymizeOneFile(anon, in, out) )
921 // // //std::cerr << "Could not anonymize: " << in << std::endl;
929 } // EO namespace creaImageIO