From: Frederic Cervenansky Date: Mon, 23 May 2011 15:33:58 +0000 (+0000) Subject: Add Analyze Reader to creaImageIO X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=d9a70eeba314e0295337364bcd88cf97cdf06b51;p=creaImageIO.git Add Analyze Reader to creaImageIO Add black box to read analyze files --- diff --git a/appli/TestWxItkView/main.cxx b/appli/TestWxItkView/main.cxx index 60898b9..fb21ace 100644 --- a/appli/TestWxItkView/main.cxx +++ b/appli/TestWxItkView/main.cxx @@ -1,11 +1,12 @@ #include #include -#include -#include +//#include +//#include #include #include +#include -#define ITK_DIM_TO_TEST 4 +#define ITK_DIM_TO_TEST 3 // Just to test the selection of file(s), directory or from creaImageIO database. class myApp : public wxApp @@ -30,45 +31,64 @@ bool myApp::OnInit( ) std::vector vtktest; std::vector exts; vtkImageData* im = vtkImageData::New(); - if(ITK_DIM_TO_TEST == 3) - { + + creaImageIO::WxAnySimpleDlg w(0); + w.ShowModal(); + std::vector Wtest = w.getImagesSelected(); + + + typedef itk::Image ImageType; + ImageType::Pointer img = ImageType::New(); + img = boost::any_cast(Wtest[0]); + //int count = boost::unsafe_any_cast(Wtest[0]).GetReferenceCount(); + std::vector test; + //test.push_back( (ImageType::Pointer)( + + // w.getImage(Wtest.front()); + test = w.getTemplatedImagesSelected(); + + - typedef itk::Image ImageType; - creaImageIO::WxISimpleDlg x(0); - // files extension to take in count - exts.push_back(".hdr"); - x.setExts(exts); - x.ShowModal(); - // get itkImage vector - std::vector test = x.getImagesSelected(); - // convert 3D itk to 3D vtk - x.split3Din3Dvtk(); - vtktest = x.getVtkImagesSelected(); - if(vtktest.size() >0) - { - crea::VtkBasicSlicer(vtktest.front()); - } - } - else if (ITK_DIM_TO_TEST == 4) - { - typedef itk::Image ImageType; - creaImageIO::WxISimpleDlg x(0); - x.ShowModal(); - // get itkImage vector - std::vector test = x.getImagesSelected(); - // convert 4D itk to 3D vtk with a directory to create tempory vtk files. - x.split4Din3Dvtk("d:/temp2"); - // get vtkImageData* vector - vtktest = x.getVtkImagesSelected(); - if(vtktest.size() >0) - { - crea::VtkBasicSlicer(vtktest.front()); - } - } - else{} + // if(ITK_DIM_TO_TEST == 3) + // { + // + // typedef itk::Image ImageType; + // creaImageIO::WxISimpleDlg x(0); + // // files extension to take in count + // exts.push_back(".hdr"); + // x.setExts(exts); + // x.ShowModal(); + // // get itkImage vector + // std::vector test = x.getImagesSelected(); + // // convert 3D itk to 3D vtk + // x.split3Din3Dvtk(); + // vtktest = x.getVtkImagesSelected(); + // if(vtktest.size() >0) + // { + // crea::VtkBasicSlicer(vtktest.front()); + // } + // } + // else if (ITK_DIM_TO_TEST == 4) + // { + // typedef itk::Image ImageType; + // creaImageIO::WxISimpleDlg x(0); + // x.ShowModal(); + // // get itkImage vector + // std::vector test = x.getImagesSelected(); + // // convert 4D itk to 3D vtk with a directory to create tempory vtk files. + // x.split4Din3Dvtk("d:/temp2"); + // // get vtkImageData* vector + // vtktest = x.getVtkImagesSelected(); + // if(vtktest.size() >0) + // { + // crea::VtkBasicSlicer(vtktest.front()); + // } + //} + // + // else{} - + // return false; } diff --git a/bbtk/CMakeLists.txt b/bbtk/CMakeLists.txt index e5e6793..8c2ae04 100644 --- a/bbtk/CMakeLists.txt +++ b/bbtk/CMakeLists.txt @@ -104,7 +104,12 @@ SET(${BBTK_PACKAGE_NAME}_INCLUDE_ALL_BBS_APPLI ON) # !! THE PATH MUST BE RELATIVE TO THE bbs FOLDER !! #) #=========================================================================== - +IF(USE_ITK) +SET(${BBTK_PACKAGE_NAME}_INCLUDE_DIRS + ${BBTK_DIR}/../../../include/bbitk + ${BBTK_DIR}/../packages/itk/src + ) +ELSE(USE_ITK) #=========================================================================== SET(${BBTK_PACKAGE_NAME}_INCLUDE_DIRS # LIST HERE YOUR ADDITIONAL INCLUDE DIRECTORIES @@ -115,7 +120,7 @@ SET(${BBTK_PACKAGE_NAME}_INCLUDE_DIRS # - the dirs automatically set by other libraries found by FIND_PACKAGE ) #=========================================================================== - +ENDIF(USE_ITK) #=========================================================================== SET(${BBTK_PACKAGE_NAME}_LIBS # LIST HERE THE ADDITIONAL LIBS TO LINK AGAINST diff --git a/bbtk/src/bbcreaImageIOImagesChooserDialogBox.xml b/bbtk/src/bbcreaImageIOImagesChooserDialogBox.xml index 4709dd4..8310f1c 100644 --- a/bbtk/src/bbcreaImageIOImagesChooserDialogBox.xml +++ b/bbtk/src/bbcreaImageIOImagesChooserDialogBox.xml @@ -24,7 +24,7 @@ - + diff --git a/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.cxx b/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.cxx new file mode 100644 index 0000000..c1c6e4c --- /dev/null +++ b/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.cxx @@ -0,0 +1,79 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== + +#include "bbcreaImageIOItkImagesChooserDialogBox.h" +#include "bbcreaImageIOPackage.h" + +namespace bbcreaImageIO +{ + +BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaImageIO,ItkImagesChooserDialogBox) +BBTK_BLACK_BOX_IMPLEMENTATION(ItkImagesChooserDialogBox,bbtk::AtomicBlackBox); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void ItkImagesChooserDialogBox::Process() +{ + + dlg = new creaImageIO::WxAnySimpleDlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database"); + dlg->ShowModal(); + + // FCY to test + bbtk::TypeInfo typ = bbitk::GetITKImagePtrTypeInfoFromPixelTypeInfoAndDimension(*dlg->getTypes().front(), dlg->getDims().front()); + BBTK_TEMPLATE_ITK_IMAGE_SWITCH(typ, Export); + bbSetOutputOutVImages(dlg->getVtkImagesSelected()); + delete dlg; +} +template +void ItkImagesChooserDialogBox::Export() +{ + if (dlg->getImagesSelected().size() == 1) + { + bbSetOutputOut( dlg->getTemplatedImagesSelected().front() ); + } + else if (dlg->getImagesSelected().size() > 1) + { +// bbSetOutputOutImages( bbSetOutputOutIImages( dlg->getTemplatedImagesSelected() )); + } + else + { + bbSetOutputOut( NULL); + } + // FCY To test the Output + //typedef itk::Image ImageType; + //typedef itk::ImageFileReader< ImageType > itkReaderType; + // typename itkReaderType::Pointer reader = itkReaderType::New(); + // + // reader->SetFileName("d:\test.hdr"); + // reader->Update(); + //reader->GetOutput()->Register(); + // this->bbSetOutputOut(reader->GetOutput()); +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void ItkImagesChooserDialogBox::bbUserSetDefaultValues() +{ + + + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void ItkImagesChooserDialogBox::bbUserInitializeProcessing() +{ + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void ItkImagesChooserDialogBox::bbUserFinalizeProcessing() +{ + +} +} +// EO namespace bbcreaImageIO + + diff --git a/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.h b/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.h new file mode 100644 index 0000000..368a638 --- /dev/null +++ b/bbtk/src/bbcreaImageIOItkImagesChooserDialogBox.h @@ -0,0 +1,78 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +#ifndef __bbcreaImageIOItkImagesChooserDialogBox_h_INCLUDED__ +#define __bbcreaImageIOItkImagesChooserDialogBox_h_INCLUDED__ + +#define BBTK_ITK_IMAGE_DIM_2 +#define BBTK_ITK_IMAGE_DIM_3 +#define BBTK_ITK_IMAGE_DIM_4 +#define BBTK_ITK_IMAGE_TYPE_int8_t +#define BBTK_ITK_IMAGE_TYPE_uint8_t +#define BBTK_ITK_IMAGE_TYPE_int16_t +#define BBTK_ITK_IMAGE_TYPE_uint16_t +#define BBTK_ITK_IMAGE_TYPE_int32_t +#define BBTK_ITK_IMAGE_TYPE_uint32_t +#define BBTK_ITK_IMAGE_TYPE_float +#define BBTK_ITK_IMAGE_TYPE_double + +#include "bbcreaImageIO_EXPORT.h" +#include "bbtkAtomicBlackBox.h" +#include "bbitkImage.h" +#include "bbtkAny.h" +#include "creaImageIOWxAnySimpleDlg.h" + +namespace bbcreaImageIO +{ +typedef std::vector OutputImagesVTKType; +typedef std::vector OutputImagesITKType; + +class bbcreaImageIO_EXPORT ItkImagesChooserDialogBox + : + public bbtk::AtomicBlackBox +{ + BBTK_BLACK_BOX_INTERFACE(ItkImagesChooserDialogBox,bbtk::AtomicBlackBox); + + + creaImageIO::WxAnySimpleDlg *dlg; + + +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== + BBTK_DECLARE_INPUT(Title,std::string); + BBTK_DECLARE_OUTPUT(OutVImages,OutputImagesVTKType); + BBTK_DECLARE_OUTPUT(OutIImages,OutputImagesITKType); + BBTK_DECLARE_OUTPUT(Out,bbitk::anyImagePointer); + BBTK_PROCESS(Process); + void Process(); + + private: + // Template read method + template void Export(); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +}; + +BBTK_BEGIN_DESCRIBE_BLACK_BOX(ItkImagesChooserDialogBox,bbtk::AtomicBlackBox); +BBTK_NAME("ItkImagesChooserDialogBox"); +BBTK_AUTHOR("cervenansky.frederic@creatis.insa-lyon.fr"); +BBTK_DESCRIPTION("ImagesChooserDialogBox is a simple application to select file(s) or directory or data from Gimmick database, handle them as ITKImage and/or display them. It handles Analayze, DICOM, jpg, tif, png, bmp and mhd."); +BBTK_CATEGORY("image;reader;dicom;"); + + +BBTK_INPUT(ItkImagesChooserDialogBox,Title,"Title of the dialog",std::string,""); +BBTK_OUTPUT(ItkImagesChooserDialogBox,OutVImages,"The selected Vtkimage",OutputImagesVTKType,""); +BBTK_OUTPUT(ItkImagesChooserDialogBox,Out,"Output Image",bbitk::anyImagePointer,""); +BBTK_OUTPUT(ItkImagesChooserDialogBox,OutIImages,"Vector of selected images",OutputImagesITKType,""); +BBTK_END_DESCRIBE_BLACK_BOX(ItkImagesChooserDialogBox); + +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +} +// EO namespace bbcreaImageIO + +#endif // __bbcreaImageIOItkImagesChooserDialogBox_h_INCLUDED__ + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea8d332..27d93ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -75,6 +75,8 @@ IF(USE_ITK) FILE(GLOB SOURCES_CREAIMAGEIO_ITK_DLG creaImageIOWxIsimpleDlg.h creaImageIOWxIsimpleDlg.hpp + creaImageIOWxAnySimpleDlg.h + creaImageIOWxAnySimpleDlg.cpp ) ENDIF(USE_ITK) diff --git a/src/creaImageIOWxAnySimpleDlg.cpp b/src/creaImageIOWxAnySimpleDlg.cpp new file mode 100644 index 0000000..b47346a --- /dev/null +++ b/src/creaImageIOWxAnySimpleDlg.cpp @@ -0,0 +1,606 @@ +#include "creaImageIOWxAnySimpleDlg.h" + +#include "creaImageIOWxGimmickReaderDialog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "itkImageToVTKImageFilter.h" + +namespace creaImageIO +{ + + ///Ctor + WxAnySimpleDlg::WxAnySimpleDlg(wxWindow *parent, + wxString i_title, + const std::string i_namedescp , + const std::string i_namedb) + : wxDialog(parent, -1,_T("DISPLAY IMAGES"), wxDefaultPosition, wxSize(230,150)) + { + namedescp = i_namedescp; + namedb = i_namedb; + + if(!i_title.empty()) + { + this->SetTitle(i_title); + } + // Button to select file(s) + wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) ); + Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadFile ); + + // Button to select directory + wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) ); + Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadDirectory ); + + // button to select creaImageIO + wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) ); + Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadGimmick ); + + /// \TODO Button to select Bruker directory + m_dicom = true; + Layout(); + + } +////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + + void WxAnySimpleDlg::OnReadFile(wxCommandEvent& event) + { + int resultShowModal; + wxFileDialog* fileDlg = new wxFileDialog( 0, _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition); + + resultShowModal = fileDlg->ShowModal(); + if ( resultShowModal==wxID_OK ) + { + wxArrayString wxArray; + fileDlg->GetPaths(wxArray); + if(wxArray.size() >0) + { + for( int i = 0; i < wxArray.GetCount(); i++) + { + std::string name = crea::wx2std(wxArray[i]); + // FOR THE MOMENT ONLY short 3D et short 4D + readImg(name); + } + } + else { + // TO DO WARNING MESSAGES + } + } + SetReturnCode( resultShowModal ); +// Close(); + EndModal( resultShowModal ); + } + + + + void WxAnySimpleDlg::setExts(std::vector i_exts) + { + m_exts = i_exts; + } + ////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + + void WxAnySimpleDlg::OnReadDirectory(wxCommandEvent &event) + { + int resultShowModal; + bool bvalid = false; + long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST; + wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style); + + resultShowModal = dirDlg->ShowModal(); + if ( resultShowModal==wxID_OK ) + { + std::string path = crea::wx2std(dirDlg->GetPath()); + typedef boost::filesystem::directory_iterator dir_it; + dir_it itr(path); + dir_it end_itr; + /*if (boost::filesystem::exists(path)) + {*/ + for(;itr != end_itr; ++itr) + { + bvalid = m_exts.size() == 0? true : false; + std::vector::iterator it = m_exts.begin(); + std::string ext = itr->filename().substr(itr->filename().find_last_of(".")); + for(; it != m_exts.end(); it++) + { + if(ext == (*it) ) + { + bvalid = true; + break; + } + } + if (!boost::filesystem::is_directory(itr->status()) && bvalid) + { + readImg(itr->string().c_str()); + } + } + } + SetReturnCode( resultShowModal ); +// Close(); + EndModal( resultShowModal ); + } + + ////////////////////////////////////////////////////////////////////// +// // +////////////////////////////////////////////////////////////////////// + + void WxAnySimpleDlg::OnReadGimmick(wxCommandEvent &event) + { + // Run Gimmick + WxGimmickReaderDialog dlg(0,-1, + namedescp, + namedb, + _T("Select image(s) - Gimmick! (c) CREATIS-LRMN 2008"), + wxDefaultPosition, + wxSize(810,750), + GIMMICK_2D_IMAGE_SELECTION, + GIMMICK_3D_IMAGE_SELECTION, + _3D, + 1); + dlg.ShowModal(); + if (dlg.GetReturnCode() == wxID_OK) + { + std::vector out; + dlg.stopReading(); + dlg.GetSelectedFiles(out); + if(m_dicom) + { + readDicomImg( out); + } + else + { + std::vector::iterator ii = out.begin(); + for (;ii != out.end();ii++) + { + readImg( (*ii).c_str() ); + } + } + dlg.OnExit(); + } + SetReturnCode( dlg.GetReturnCode() ); +// Close(); + EndModal( dlg.GetReturnCode() ); + } + + + + wxString WxAnySimpleDlg::getInfoImage() + { + return infoimage; + } + + + // FCY : just to check but not needed, we hardly suppose that we load only same type and dim of images + const std::type_info & WxAnySimpleDlg::getType(const std::string &i_name) + { + typedef itk::ImageIOBase::IOComponentType ScalarPixelType; + itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode); + imageIO->SetFileName(i_name.c_str()); + imageIO->ReadImageInformation(); + return imageIO->GetComponentTypeInfo();//AsString( imageIO->GetComponentType()); + } + + const size_t WxAnySimpleDlg::getNumberOfDimensions(const std::string &i_name) + { + typedef itk::ImageIOBase::IOComponentType ScalarPixelType; + itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode); + imageIO->SetFileName(i_name.c_str()); + imageIO->ReadImageInformation(); + return imageIO->GetNumberOfDimensions(); + } + + void WxAnySimpleDlg::readImg(const std::string &i_name) + { + size_t dims = getNumberOfDimensions(i_name); + const std::type_info *type= &getType(i_name); + switch(dims) + { + case 3: + if (getType(i_name) == typeid(unsigned char)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed char)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(short)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned short)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned int)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed int)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned long)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed long)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(float)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else if (getType(i_name) == typeid(double)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + } + else + { + //????FCY, so what the type???? + } + break; + //assume that we have only one 4D file + case 4: + if (getType(i_name) == typeid(unsigned char)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed char)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned short)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if(getType(i_name) == typeid(short)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + try + { + reader->Update(); + } + catch( itk::ExceptionObject & err ) + { + std::cout << "Caught an exception reading" << i_name << ": " << std::endl; + std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; + throw err; + } + catch(...) + { + std::cout << "Error while reading image " << i_name << std::endl; + throw; + } + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned short)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned int)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed int)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(unsigned long)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(signed long)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(float)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else if (getType(i_name) == typeid(double)) + { + typedef itk::Image TImage; + typedef itk::ImageFileReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName( i_name ); + reader->Update(); + reader->GetOutput()->Register(); + m_AnyImages.push_back(reader->GetOutput()); + split4Din3Dvtk(reader->GetOutput()); + } + else + { + //????FCY, so what the type???? + } + break; + } + + // Test to know if we have to add new type + bool btest = false; + if( m_AnyDims.size() != 0) + { + btest = true; + } + else + { + m_AnyDims.push_back(dims); + m_AnyType.push_back(&getType(i_name)); + } + if (btest) + { + if(m_AnyDims.front() != dims || m_AnyType.front()->before(getType(i_name))) + { + m_AnyDims.push_back(dims); + m_AnyType.push_back(&getType(i_name)); + } + else{} + } + else {} + } + + + + + void WxAnySimpleDlg::readDicomImg(const std::vector &i_names) + { + typedef itk::Image TImage; + typedef itk::GDCMImageIO GDCMType; + typedef itk::DICOMSeriesFileNames dicnames; + GDCMType::Pointer gdcmIO = GDCMType::New(); + dicnames::Pointer generator = dicnames::New(); + + + typedef itk::ImageSeriesReader ReaderType; + ReaderType::Pointer reader = ReaderType::New(); + reader->SetImageIO(gdcmIO); + reader->SetFileNames(i_names); + // reader->SetFileName( i_name ); + try + { + reader->Update(); + } + catch( itk::ExceptionObject & err ) + { + // std::cout << "Caught an exception reading" << i_name << ": " << std::endl; + std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; + throw err; + } + catch(...) + { + //std::cout << "Error while reading image " << i_name << std::endl; + throw; + } + //m_Iresults.push_back(reader->GetOutput()); + + } + + + + + template + void WxAnySimpleDlg::split3Din3Dvtk(TImage* i_Img) + { + typedef itk::ImageToVTKImageFilter< TImage > ConnectorType; + ConnectorType::Pointer connector = ConnectorType::New(); + connector->SetInput(i_Img); + connector->GetImporter()->SetDataScalarTypeToUnsignedChar(); + connector->Update(); + vtkImageData *im = vtkImageData::New(); + im->ShallowCopy(connector->GetOutput()); + im->Update(); + m_Vresults.push_back(im); + } + + + template + void WxAnySimpleDlg::split4Din3Dvtk(TImage* i_Img) + { + if(i_Img->GetImageDimension() == 4) + { + + // size of fourth dimension + int dsize = i_Img->GetLargestPossibleRegion().GetSize(3); + // Output Type + typedef itk::Image ImageOutputType; + typedef itk::ImageSeriesWriter SeriesWriterType; + typedef itk::MetaImageIO MetaImageType; + MetaImageType::Pointer metaIO; + SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New(); + + // from JPR file to generate output files + typedef itk::NumericSeriesFileNames NamesGeneratorType; + NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New(); + namesGenerator->SetStartIndex(0); + namesGenerator->SetEndIndex(dsize-1); + namesGenerator->SetIncrementIndex(1); + std::string format = m_dir; + format += "/image%03d.mhd"; + namesGenerator->SetSeriesFormat( format.c_str() ); + + const std::vector names = namesGenerator->GetFileNames(); + seriesWriter->SetFileNames( names ); + seriesWriter->SetInput(i_Img); + seriesWriter->SetImageIO(metaIO); + try + { + seriesWriter->Update(); + vtkMetaImageReader *vReader = vtkMetaImageReader::New(); + std::vector::const_iterator it = names.begin(); + for( ;it != names.end(); ++it) + { + vReader->SetFileName((*it).c_str()); + vReader->Update(); + m_Vresults.push_back(vReader->GetOutput()); + + } + } + catch( itk::ExceptionObject & excp ) + { + std::cerr << "Exception thrown while writing the series " << std::endl; + std::cerr << excp << std::endl; + //return EXIT_FAILURE; + } + } + } + + bool WxAnySimpleDlg::AllSameType() + { + if(m_AnyType.size() == 1) + { + return true; + } + else + { + return false; + } + + } + + }// namespace end + diff --git a/src/creaImageIOWxAnySimpleDlg.h b/src/creaImageIOWxAnySimpleDlg.h new file mode 100644 index 0000000..08ae0b0 --- /dev/null +++ b/src/creaImageIOWxAnySimpleDlg.h @@ -0,0 +1,101 @@ +#include "creaImageIOSimpleView.h" +#include +#include +#include +namespace creaImageIO +{ + /** + * \ingroup View + */ + + /// Simple Wxwidgets interface to select file(s) or directory or from creaImageIO database to display + class __declspec(dllexport) WxAnySimpleDlg : public wxDialog + { + public: + /// Ctor + WxAnySimpleDlg( wxWindow *parent, + wxString i_title =_T(""), + const std::string i_namedescp = "localdatabase_Descriptor.dscp", + const std::string i_namedb = "Local Database" + ); + + ~WxAnySimpleDlg(){}; + + void setExts(std::vector); + /// Callback to read file(s) + void OnReadFile(wxCommandEvent& event); + + /// Callback to read directory, no recursive + void OnReadDirectory(wxCommandEvent &event); + + /// Callback to select from creaImageIO database + void OnReadGimmick(wxCommandEvent &event); + + /// return a vtkImageData vector of selected images, if available + std::vector getVtkImagesSelected() {return m_Vresults;} + + wxString getInfoImage(); + + + void set(bool i_dicom){m_dicom= i_dicom;} + std::vector & getImagesSelected(){ return m_AnyImages;} + + // return the size of readed images + std::vector getDims(){ return m_AnyDims;} + + // return the type of readed images + std::vector getTypes(){ return m_AnyType;} + + // get an Image on wanted type + template + typename TImage::Pointer getImage(boost::any i_AnyImage) + { + typedef itk::Image ImageOut; + ImageOut::Pointer img = ImageOut::New(); + img = boost::any_cast< ImageOut::Pointer>(i_AnyImage); + return img; + } + + // get the images on itk type defined by user + template + std::vector getTemplatedImagesSelected() + { + std::vector imgs; + std::vector::iterator it = m_AnyImages.begin(); + for(; it != m_AnyImages.end(); it++) + { + imgs.push_back(boost::any_cast (*it)); + } + return imgs; + } + bool AllSameType(); + private: + + + std::vector m_AnyDims; + std::vector m_AnyType; + std::vector m_AnyImages; + std::vector m_exts; + std::string namedescp; + std::string namedb; + wxString infoimage; + std::string m_dir; + template + void split3Din3Dvtk(TImage* i_Img); + + template + void split4Din3Dvtk(TImage* i_Img); + + bool m_dicom; + /// interface to read data + SimpleView m_view; + void readImg(const std::string &i_name); + void readDicomImg(const std::vector &i_names); + std::vector m_Vresults; + + const size_t getNumberOfDimensions(const std::string &i_name); + const std::type_info & getType(const std::string &i_name); + }; + + +}