Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
FileConverter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkProperties.h"
14 
15 #include "mitkCommandLineParser.h"
16 #include "mitkIOUtil.h"
17 
18 #include <itksys/SystemTools.hxx>
19 
21 
22 
23 int main(int argc, char* argv[])
24 {
25  mitkCommandLineParser parser;
26 
27  parser.setTitle("File Converter");
28  parser.setCategory("Basic Image Processing");
29  parser.setDescription("");
30  parser.setContributor("German Cancer Research Center (DKFZ)");
31 
32  parser.setArgumentPrefix("--","-");
33  // Add command line argument names
34  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
35  parser.addArgument("input", "i", mitkCommandLineParser::File, "Input file:", "Input File",us::Any(),false, false, false, mitkCommandLineParser::Input);
36  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
37  parser.addArgument("reader", "r", mitkCommandLineParser::String, "Reader Name", "Reader Name", us::Any());
38  parser.addArgument("list-readers", "lr", mitkCommandLineParser::Bool, "Reader Name", "Reader Name", us::Any());
39 
40 
41  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
42 
43  if (parsedArgs.size()==0)
44  return EXIT_FAILURE;
45 
46  // Show a help message
47  if ( parsedArgs.count("help") || parsedArgs.count("h"))
48  {
49  std::cout << parser.helpText();
50  return EXIT_SUCCESS;
51  }
52 
53  std::string inputFilename = us::any_cast<std::string>(parsedArgs["input"]);
54  std::string outputFilename = us::any_cast<std::string>(parsedArgs["output"]);
55 
57  if (parsedArgs.count("reader"))
58  {
59  preference.push_back(us::any_cast<std::string>(parsedArgs["reader"]));
60  }
61 
62  if (parsedArgs.count("list-readers"))
63  {
64  mitk::IOUtil::LoadInfo loadInfo(inputFilename);
65  auto readers = loadInfo.m_ReaderSelector.Get();
66 
67  std::string errMsg;
68  if (readers.empty())
69  {
70  if (!itksys::SystemTools::FileExists(loadInfo.m_Path.c_str()))
71  {
72  errMsg += "File '" + loadInfo.m_Path + "' does not exist\n";
73  }
74  else
75  {
76  errMsg += "No reader available for '" + loadInfo.m_Path + "'\n";
77  }
78  MITK_ERROR << errMsg;
79  return 0;
80  }
81 
82  std::cout << "Available Readers: "<<std::endl << "------------------------" << std::endl;
83  for (auto reader : loadInfo.m_ReaderSelector.Get())
84  {
85  std::cout << " : " << reader.GetDescription() << std::endl;
86  }
87  return 0;
88  }
89 
92 
93  std::string extension = itksys::SystemTools::GetFilenameExtension(outputFilename);
94  std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(outputFilename);
95  std::string path = itksys::SystemTools::GetFilenamePath(outputFilename);
96 
97  auto nodes = mitk::IOUtil::Load(inputFilename, &functor);
98 
99  unsigned count = 0;
100  for (auto node : nodes)
101  {
102  std::string writeName = path + "/" + filename + extension;
103  if (count > 0)
104  {
105  writeName = path + "/" + filename + "_" + std::to_string(count) + extension;
106  }
107  mitk::IOUtil::Save(node, writeName);
108  ++count;
109  }
110 
111  return EXIT_SUCCESS;
112 }
Option callback functor with a preference list/ black list option selection strategy.
int main(int argc, char *argv[])
#define MITK_ERROR
Definition: mitkLogMacros.h:20
std::string inputFilename
Definition: MitkMCxyz.cpp:625
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
std::string outputFilename
Definition: MitkMCxyz.cpp:626
std::vector< Item > Get() const
Get a sorted list of file reader items.
Definition: usAny.h:163
std::string helpText() const
std::string m_Path
Definition: mitkIOUtil.h:54
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
FileReaderSelector m_ReaderSelector
Definition: mitkIOUtil.h:57
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
void setTitle(std::string title)
void setDescription(std::string description)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489