16 #pragma warning (disable : 4996)
26 #include "itkNrrdImageIO.h"
27 #include "itkImageFileWriter.h"
40 const std::string
DATA =
"data";
41 const std::string
ITEM =
"item";
43 const std::string
NAME =
"name";
44 const std::string
ID =
"id";
46 const std::string
LINK =
"link";
49 static std::string
GetName(std::string fileName,std::string suffix,
bool longName =
false)
51 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
53 return fileName.substr(0,fileName.length() -suffix.length()-11);
55 return fileName.substr(0,fileName.length() -suffix.length()-9);
58 static std::string
GetDate(std::string fileName,std::string suffix,
bool longName =
false)
60 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
62 fileName = fileName.substr(fileName.length() - suffix.length()-10,10);
64 fileName = fileName.substr(fileName.length() - suffix.length()-8,8);
67 fileName.insert(6,
"-");
68 fileName.insert(4,
"-");
79 QDir fileName = QFileInfo(xmlFile.c_str()).absoluteDir();
80 std::string outputFolder = fileName.path().toStdString() + QDir::separator().toLatin1();
81 QDir baseFolder(outputFolder.c_str());
82 baseFolder.mkpath(outputFolder.c_str());
84 std::ofstream xmlFileStream;
85 xmlFileStream.open (xmlFile.c_str());
86 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
88 unsigned int subColId = 0;
90 unsigned int dataId = 0;
92 QDir dir(QString::fromStdString(outputFolder));
93 for (
size_t i = 0 ; i < dataCollection->
Size(); ++i)
98 dir.mkpath(QString::fromStdString(dataCollection->
IndexToName(i)));
102 if (subCollections == NULL)
104 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
108 for (
size_t d = 0; d < subCollections->
Size(); d++ )
111 QString subPath = QString::fromStdString(dataCollection->
IndexToName(i))+
"/"+QString::fromStdString(subCollections->
IndexToName(d));
116 if (itemCollections == NULL)
118 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
122 for (
size_t s = 0; s < itemCollections->
Size(); s++)
124 if (filter.size() > 0)
126 bool isSelected =
false;
127 for (
size_t f = 0; f < filter.size(); f++)
129 if (filter.at(f) == itemCollections->
IndexToName(s) )
135 if (isSelected ==
false)
139 QString fileName = dir.path() + dir.separator() + subPath + dir.separator() + QString::fromStdString(dataCollection->
IndexToName(i)) +
"_" + QString::fromStdString(subCollections->
IndexToName(d)) +
"_" + QString::fromStdString(itemCollections->
IndexToName(s));
146 io->SetFileType( itk::ImageIOBase::Binary );
147 io->UseCompressionOn();
150 typedef itk::Image<itk::DiffusionTensor3D<TensorScalar>,3>
ImageType;
151 typedef itk::ImageFileWriter<ImageType> WriterType;
155 nrrdWriter->SetInput( outimage );
156 nrrdWriter->SetImageIO(io);
157 nrrdWriter->SetFileName(fileName.toStdString());
158 nrrdWriter->UseCompressionOn();
159 nrrdWriter->Update();
166 for (CoreObjectFactory::FileWriterList::iterator it = fileWriters.begin() ; it != fileWriters.end() ; ++it)
168 if ( (*it)->CanWriteBaseDataType(fib) ) {
169 (*it)->SetFileName( fileName.toStdString().c_str() );
170 (*it)->DoWrite( fib );
190 catch(
const std::exception& e)
192 MITK_ERROR <<
"Caught exception: " << e.what();
195 std::string relativeFilename = baseFolder.relativeFilePath(fileName).toStdString();
196 xmlFileStream <<
" <" <<
ITEM <<
" " <<
NAME <<
"=\"" <<itemCollections->
IndexToName(s) <<
"\" " <<
FILEPATH <<
"=\"" <<
"\" " <<
LINK <<
"=\"" << relativeFilename <<
"\" />\n";
198 xmlFileStream <<
" </" <<
DATA <<
">\n";
206 xmlFileStream.flush();
207 xmlFileStream.close();
213 std::vector<std::string> mods;
214 return ExportCollectionToFolder(dataCollection,xmlFile, mods);
219 QDir origFilename = QFileInfo(dataCollection->
GetXMLFile().c_str()).absoluteDir();
220 QString originalFolder = origFilename.path() + QDir::separator();
225 QDir fileName = QFileInfo(xmlFile.c_str()).absoluteDir();
226 std::string outputFolder = fileName.path().toStdString() + QDir::separator().toLatin1();
227 QDir baseFolder(outputFolder.c_str());
229 std::ofstream xmlFileStream;
230 xmlFileStream.open (xmlFile.c_str());
231 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
233 unsigned int subColId = 0;
235 unsigned int dataId = 0;
237 QDir dir(QString::fromStdString(outputFolder));
238 for (
size_t i = 0 ; i < dataCollection->
Size(); ++i)
243 dir.mkpath(QString::fromStdString(dataCollection->
IndexToName(i)));
247 if (subCollections == NULL)
249 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
253 for (
size_t d = 0; d < subCollections->
Size(); d++ )
256 QString subPath = QString::fromStdString(dataCollection->
IndexToName(i))+
"/"+QString::fromStdString(subCollections->
IndexToName(d));
261 if (itemCollections == NULL)
263 MITK_ERROR<<
"mitk::CollectionWriter::SaveCollectionToFolder: Container is illformed. Aborting";
267 for (
size_t s = 0; s < itemCollections->
Size(); s++)
269 if (filter.size() > 0)
271 bool isSelected =
false;
272 for (
size_t f = 0; f < filter.size(); f++)
274 if (filter.at(f) == itemCollections->
IndexToName(s) )
280 if (isSelected ==
false)
285 bool fullName =
false;
288 fileName = originalFolder + QString::fromStdString(itemCollections->
GetDataFilePath(s));
293 fileName = dir.path() + dir.separator() + subPath + dir.separator() + QString::fromStdString(dataCollection->
IndexToName(i)) +
"_" + QString::fromStdString(subCollections->
IndexToName(d)) +
"_" + QString::fromStdString(itemCollections->
IndexToName(s));
302 io->SetFileType( itk::ImageIOBase::Binary );
303 io->UseCompressionOn();
306 typedef itk::Image<itk::DiffusionTensor3D<TensorScalar>,3>
ImageType;
307 typedef itk::ImageFileWriter<ImageType> WriterType;
311 nrrdWriter->SetInput( outimage );
312 nrrdWriter->SetImageIO(io);
313 nrrdWriter->SetFileName(fileName.toStdString());
314 nrrdWriter->UseCompressionOn();
315 nrrdWriter->Update();
323 for (CoreObjectFactory::FileWriterList::iterator it = fileWriters.begin() ; it != fileWriters.end() ; ++it)
325 if ( (*it)->CanWriteBaseDataType(fib) ) {
326 (*it)->SetFileName( fileName.toStdString().c_str() );
327 (*it)->DoWrite( fib );
345 catch(
const std::exception& e)
347 MITK_ERROR <<
"Caught exception: " << e.what();
350 std::string relativeFilename =baseFolder.relativeFilePath(fileName).toStdString();
351 xmlFileStream <<
" <" <<
ITEM <<
" " <<
NAME <<
"=\"" <<itemCollections->
IndexToName(s) <<
"\" " <<
FILEPATH <<
"=\"" <<
"\" " <<
LINK <<
"=\"" << relativeFilename <<
"\" />\n";
353 xmlFileStream <<
" </" <<
DATA <<
">\n";
361 xmlFileStream.flush();
362 xmlFileStream.close();
371 parseDir.setFilter( QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
372 parseDir.setPath(QString::fromStdString(folder));
374 QFileInfoList qFileList = parseDir.entryInfoList();
377 std::ofstream xmlFileStream;
378 xmlFileStream.open (xmlFile.c_str());
379 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
380 xmlFileStream <<
"<" <<
COLLECTION <<
" " <<
NAME <<
"=\"" <<
"GEN" <<
"\" >\n";
382 unsigned int dataId = 0;
385 for (
int i = 0; i < qFileList.size(); ++i)
388 std::string baseFolder = qFileList.at(i).absoluteFilePath().toStdString() + QDir::separator().toLatin1() + collectionType;
390 MITK_INFO <<
"Processing : " << baseFolder;
391 if (!QFileInfo(QString::fromStdString(baseFolder)).isDir())
393 MITK_WARN <<
"Not a valid folder, skipping.";
402 if (fileList.size() <= 0 || fileList.at(0).size() <= 0)
407 std::string name =
GetName(fileList.at(0).at(0),filter.at(0));
408 xmlFileStream <<
" <" <<
SUBCOLLECTION <<
" " << NAME <<
"=\"" << name <<
"\" " <<
FILEPATH <<
"=\"\" id=\"Col" << i <<
"\" >\n";
411 for (
unsigned int k=0; k < fileList.at(0).size(); ++k)
413 std::string strDate =
GetDate(fileList.at(0).at(k),filter.at(0));
414 xmlFileStream <<
" <" <<
DATA <<
" " << NAME <<
"=\"" << strDate <<
"\" " <<
" id=\"Data" << dataId <<
"\" >\n";
416 for (
unsigned int i=0; i< filter.size(); ++i)
418 std::string fileName = fileList.at(i).at(k);
419 xmlFileStream <<
" <" <<
ITEM <<
" " << NAME <<
"=\"" << seriesNames.at(i) <<
"\" " <<
LINK <<
"=\"" << fileName <<
"\" />\n";
421 xmlFileStream <<
" </" <<
DATA <<
">\n" ;
427 xmlFileStream.flush();
428 xmlFileStream.close();
435 std::ofstream xmlFileStream;
436 xmlFileStream.open (xmlFile.c_str());
437 xmlFileStream <<
"<!-- MITK - DataCollection - File Version 1.0 --> \n";
438 xmlFileStream <<
"<" <<
COLLECTION <<
" " <<
NAME <<
"=\"" <<
"GEN" <<
"\" >\n";
440 unsigned int dataId = 0;
450 std::string name =
GetName(fileList.at(0).at(0),filter.at(0),longDate);
451 xmlFileStream <<
" <" <<
SUBCOLLECTION <<
" " << NAME <<
"=\"" << name <<
"\" " <<
FILEPATH <<
"=\"\" id=\"Col" << 0 <<
"\" >\n";
454 for (
unsigned int k=skipUntil; k < fileList.at(0).size(); ++k)
456 std::string strDate =
GetDate(fileList.at(0).at(k),filter.at(0),
true);
457 xmlFileStream <<
" <" <<
DATA <<
" " << NAME <<
"=\"" << strDate <<
"\" " <<
" id=\"Data" << dataId <<
"\" >\n";
459 for (
unsigned int i=0; i< filter.size(); ++i)
461 std::string fileName = fileList.at(i).at(k);
462 xmlFileStream <<
" <" <<
ITEM <<
" " << NAME <<
"=\"" << seriesNames.at(i) <<
"\" " <<
LINK <<
"=\"" << fileName <<
"\" />\n";
468 xmlFileStream <<
" </" <<
DATA <<
">\n" ;
470 if (GetIndexForinXMonths(fileList,months,k+1,filter)== 0)
476 xmlFileStream.flush();
477 xmlFileStream.close();
484 std::string strDate0 =
GetDate(fileList.at(0).at(curIndex),filter.at(0),
true);
486 int year0 =std::atoi(strDate0.substr(0,4).c_str());
487 int month0 =std::atoi(strDate0.substr(5,2).c_str());
488 int day0 = std::atoi(strDate0.substr(8,2).c_str());
490 size_t bestIndex = 0;
493 for (
size_t i=curIndex+1; i < fileList.at(0).size(); ++i)
495 std::string strDate =
GetDate(fileList.at(0).at(i),filter.at(0),
true);
496 int year =std::atoi(strDate.substr(0,4).c_str());
497 int month =std::atoi(strDate.substr(5,2).c_str());
498 int day = std::atoi(strDate.substr(8,2).c_str());
500 int fit = std::fabs((months * 30 ) - (((year-year0)*360) +((month-month0)*30) + (day-day0)));
itk::SmartPointer< Self > Pointer
static FileListType GenerateFileLists(std::string folder, std::vector< std::string > suffixes, bool allowGaps=false)
GenerateFileLists Returns a collection of lists with valid files names in a folder.
static bool SaveCollection(DataCollection *dataCollection, std::vector< std::string > filter, std::string xmlFile="")
SaveCollection - Stores data collection at original location.
const std::string SUBCOLLECTION
std::string GetPixelTypeAsString() const
Returns a string containing the ITK pixel type name.
vcl_size_t Size() const
Size - number of data items in collection.
static Pointer GetInstance()
static FileListType SanitizeFileList(FileListType list)
SanitizeFileList Removes all entries that are lacking at least one modality.
const std::string COLLECTION
std::string GetName() const
mitk::Image::Pointer GetMitkImage(vcl_size_t index)
GetMitkImage - casts data to mitk::Image and returns it.
static std::string GetDate(std::string fileName, std::string suffix, bool longName=false)
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
std::vector< std::vector< std::string > > FileListType
map::core::discrete::Elements< 3 >::InternalImageType ImageType
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
static bool SingleFolderToXml(std::string folder, std::string xmlFile, std::vector< std::string > filter, std::vector< std::string > seriesNames, bool longDate=true, int skipUntil=0, float months=0)
Image class for storing images.
Base Class for Fiber Bundles;.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
static bool ExportCollectionToFolder(DataCollection *dataCollection, std::string xmlFile, std::vector< std::string > filter)
ExportCollectionToFolder.
const std::string FILEPATH
static bool FolderToXml(std::string folder, std::string collectionType, std::string xmlFile, std::vector< std::string > filter, std::vector< std::string > seriesNames)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
std::string GetXMLFile()
SetXMLFile - gets xml file to which data collection is supposed to be saved.
this class encapsulates tensor images
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
std::string GetDataFilePath(vcl_size_t index) const
static std::string GetName(std::string fileName, std::string suffix, bool longName=false)
std::list< mitk::FileWriterWithInformation::Pointer > FileWriterList
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.