diff --git a/Examples/mitkdump/CMakeLists.txt b/Examples/mitkdump/CMakeLists.txt index ad822a380c..d19d6d14fa 100644 --- a/Examples/mitkdump/CMakeLists.txt +++ b/Examples/mitkdump/CMakeLists.txt @@ -1,25 +1,29 @@ project(mitkdump) find_package(MITK) # Check prerequisites for this application. # We need the Mitk module. -MITK_CHECK_MODULE(result Mitk) +MITK_CHECK_MODULE(result Qmitk) if(result) message(SEND_ERROR "MITK module(s) \"${result}\" not available from the MITK build at ${MITK_DIR}") endif() # Set-up the build system to use the Mitk module -MITK_USE_MODULE(Mitk) -MITK_USE_MODULE(DICOMReader) +MITK_USE_MODULE(Qmitk DICOMReader) include_directories(${ALL_INCLUDE_DIRECTORIES}) link_directories(${ALL_LIBRARY_DIRS}) -add_executable(${PROJECT_NAME} mitkdump.cpp) -target_link_libraries(${PROJECT_NAME} ${ALL_LIBRARIES} ) +add_executable(mitkdump mitkdump.cpp) +target_link_libraries(mitkdump ${ALL_LIBRARIES} ) + +add_executable(mitkdumpdir mitkdumpdir.cpp) +target_link_libraries(mitkdumpdir ${ALL_LIBRARIES} ) # subproject support -set_property(TARGET ${PROJECT_NAME} PROPERTY LABELS ${MITK_DEFAULT_SUBPROJECTS}) +set_property(TARGET mitkdump PROPERTY LABELS ${MITK_DEFAULT_SUBPROJECTS}) +set_property(TARGET mitkdumpdir PROPERTY LABELS ${MITK_DEFAULT_SUBPROJECTS}) foreach(subproject ${MITK_DEFAULT_SUBPROJECTS}) - add_dependencies(${subproject} ${PROJECT_NAME}) + add_dependencies(${subproject} mitkdump) + add_dependencies(${subproject} mitkdumpdir) endforeach() diff --git a/Examples/mitkdump/mitkdump.cpp b/Examples/mitkdump/mitkdump.cpp index 2ed0be5eea..6c118975ca 100644 --- a/Examples/mitkdump/mitkdump.cpp +++ b/Examples/mitkdump/mitkdump.cpp @@ -1,163 +1,166 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ /** \file mitkdump.cpp \brief Commandline application to see what DICOMFileReaderSelector would produce from a set of files. Usage: \verbatim mitkdump [-v] file1 [... fileN] -v output more details on commandline fileN DICOM file \endverbatim The application will ask a DICOMFileReaderSelector to analyze the files given as file1 .. fileN. Once the reader with the least number of files is selected, this result is printed to commandline. If the "-v" flag is used (as a first parameter), the output will contain details about filenames, which can make the output considerably harder to read. Output is also written to a log file of name "%gt;datetime-stamp%lt;_dir_>directory-name<.mitkdump */ #include "mitkDICOMFileReaderSelector.h" using mitk::DICOMTag; std::string buildDateString() { std::time_t rawtime; std::tm* timeinfo; char buffer [80]; std::time(&rawtime); timeinfo = std::localtime(&rawtime); std::strftime(buffer,80,"%Y%m%d-%H%M%S",timeinfo); return std::string(buffer); } void gen_random(char *s, const int len) { static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < len; ++i) { s[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; } s[len] = 0; } std::string gen_random(const int len) { char retval[len]; gen_random(retval, len); return std::string(retval); } std::string removeUnsafeChars(const std::string& str) { std::string retval; for(std::string::const_iterator it = str.begin(); it != str.end(); ++it) { const char& c = *it; if ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '.') || (c == '-') || (c == '_') ) { retval += c; } } return retval; } std::string extractDirString(const std::string& dirString) { std::string wholeprefix = dirString.substr(0, dirString.find_last_of("/\\")); std::string lastDirectoryPart = wholeprefix.substr(wholeprefix.find_last_of("/\\")+1); std::string cleanLastDirectoryPart = removeUnsafeChars(lastDirectoryPart); if (!cleanLastDirectoryPart.empty()) { return cleanLastDirectoryPart; } else { std::stringstream emptydirname; emptydirname << "noname_" << gen_random(6); return emptydirname.str(); } } int main(int argc, char* argv[]) { bool fileDetails(false); int firstFileIndex = 1; // see if we got the '-v' flag to output file details if (argc > 1 && std::string(argv[firstFileIndex]) == "-v") { fileDetails = true; ++firstFileIndex; } // analyze files from argv mitk::StringList inputFiles; for (int a = firstFileIndex; a < argc; ++a) { inputFiles.push_back( std::string(argv[a]) ); } mitk::DICOMFileReaderSelector::Pointer configSelector = mitk::DICOMFileReaderSelector::New(); configSelector->LoadBuiltIn3DConfigs(); // a set of compiled in ressources with standard configurations that work well configSelector->SetInputFiles( inputFiles ); mitk::DICOMFileReader::Pointer reader = configSelector->GetFirstReaderWithMinimumNumberOfOutputImages(); if (reader.IsNull()) { MITK_ERROR << "Could not configure any DICOM reader.. Exiting..."; return EXIT_FAILURE; } // output best reader result MITK_INFO << "---- Best reader configuration '" << reader->GetConfigurationLabel() << "'"; - reader->PrintOutputs(std::cout, fileDetails); + if (fileDetails) + { + reader->PrintOutputs(std::cout, fileDetails); + } // construct the name of a log file std::string datestring = buildDateString();; std::string dirString = extractDirString(argv[firstFileIndex]); std::string logfilename = datestring + "_dir_" + dirString + ".mitkdump"; MITK_INFO << "Logfile " << logfilename; // write output to file for later analysis std::ofstream fs; fs.open(logfilename.c_str()); reader->PrintOutputs( fs, true); // always verbose in log file fs.close(); // if we got so far, everything is fine return EXIT_SUCCESS; } diff --git a/Examples/mitkdump/mitkdumpdir.cpp b/Examples/mitkdump/mitkdumpdir.cpp new file mode 100644 index 0000000000..fbf3be0871 --- /dev/null +++ b/Examples/mitkdump/mitkdumpdir.cpp @@ -0,0 +1,127 @@ +/*=================================================================== + + The Medical Imaging Interaction Toolkit (MITK) + + Copyright (c) German Cancer Research Center, + Division of Medical and Biological Informatics. + All rights reserved. + + This software is distributed WITHOUT ANY WARRANTY; without + even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. + + See LICENSE.txt or http://www.mitk.org for details. + + ===================================================================*/ + +/** + \file mitkdumpdir.cpp + + \brief Commandline application that calls the mitkdump application on multiple directories. + + Usage: + \verbatim + mitkdumpdir [-v] dir1 [... dirN] + + -v output more details on commandline + dirN directory names + \endverbatim + + The application will traverse each directory for sub-directories. + For each leaf-directory (i.e. directories without sub-directories), + the mitkdump application will be called, given all files from the + leaf directory. + + If the "-v" flag is used, it will be passed to the mitkdump application, + which can create considerably more output. +*/ + +#include + +bool printEachOutput(false); + +bool processLeafNode(const QString& dirname, const QString& executablename) +{ + qDebug() << "Processing " << qPrintable(dirname); + + QFileInfoList fileinfos = QDir( dirname ).entryInfoList( QDir::Files | QDir::Readable ); + + QStringList filenames; + foreach(QFileInfo info, fileinfos) + { + filenames << info.absoluteFilePath(); + } + + QProcess mitkdump; + mitkdump.start(executablename, filenames); + + if (!mitkdump.waitForStarted()) + return false; + + if (!mitkdump.waitForFinished()) + return false; + + if (printEachOutput) + { + QString output(mitkdump.readAll()); + qDebug() << output; + } + + return true; +} + +void processLeafNodes(const QString& rootDir, const QString& executablename) +{ + QDirIterator dirIter(rootDir, + QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable, + QDirIterator::Subdirectories); + + while (dirIter.hasNext()) + { + dirIter.next(); + + if ( QDir( dirIter.fileInfo().absoluteFilePath() ).entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable ).empty() ) + { + processLeafNodes( dirIter.filePath(), executablename ); + } + } + + if ( QDir( rootDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable ).empty() ) + { + bool successful = processLeafNode( rootDir, executablename ); + } +} + +int main(int argc, char* argv[]) +{ + QCoreApplication a(argc,argv); + + int firstDirIndex = 1; + + // see if we got the '-v' flag to output file details + if (argc > 1 && std::string(argv[firstDirIndex]) == "-v") + { + printEachOutput = true; + ++firstDirIndex; + } + + QString executablename( QCoreApplication::applicationDirPath() + QDir::separator() + "mitkdump" ); +#ifdef WIN32 + executablename += ".exe"; +#endif + + // analyze dirs from argv + QStringList inputDirs; + for (int a = firstDirIndex; a < argc; ++a) + { + inputDirs << argv[a]; + } + + foreach(QString dirname, inputDirs) + { + processLeafNodes(dirname, executablename); + } + + // if we got so far, everything is fine + return EXIT_SUCCESS; +}