Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
ImageTypeConverter.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 "mitkCommandLineParser.h"
14 #include "mitkIOUtil.h"
15 #include <mitkImageCast.h>
16 #include <typeinfo>
17 
18 #define CONVERT_IMAGE(TYPE, DIM) \
19  { \
20  MITK_INFO << "Data Type for Conversion: " << typeid(TYPE).name(); \
21  itk::Image<TYPE, DIM>::Pointer itkImage = itk::Image<TYPE, DIM>::New(); \
22  mitk::CastToItkImage(image, itkImage); \
23  mitk::CastToMitkImage(itkImage, outputImage); \
24  }
25 
26 #define CONVERT_IMAGE_TYPE(TYPE) \
27  { \
28  unsigned int dimension = image->GetDimension(); \
29  MITK_INFO << "Image Dimension is: " << dimension; \
30  switch (dimension) { \
31  case 2 : CONVERT_IMAGE( TYPE , 2); \
32  break; \
33  case 3 : CONVERT_IMAGE( TYPE , 3); \
34  break; \
35  default: MITK_INFO << "This tool doesn't support a dimension of "<<dimension<<"."; \
36  outputImage = NULL; \
37  break; \
38  }; \
39  }
40 
41 
42 int main(int argc, char* argv[])
43 {
44  mitkCommandLineParser parser;
45 
46  parser.setTitle("Image Type Converter");
47  parser.setCategory("Basic Image Processing");
48  parser.setDescription("");
49  parser.setContributor("German Cancer Research Center (DKFZ)");
50 
51  parser.setArgumentPrefix("--","-");
52  // Add command line argument names
53  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
54  parser.addArgument("input", "i", mitkCommandLineParser::File, "Input file:", "Input file",us::Any(),false, false, false, mitkCommandLineParser::Input);
55  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
56  parser.addArgument("type", "t", mitkCommandLineParser::String, "Type definition:", "Define Scalar data type: int, uint, short, ushort, char, uchar, float, double", us::Any(), false);
57 
58  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
59 
60  if (parsedArgs.size()==0)
61  return EXIT_FAILURE;
62 
63  // Show a help message
64  if ( parsedArgs.count("help") || parsedArgs.count("h"))
65  {
66  std::cout << parser.helpText();
67  return EXIT_SUCCESS;
68  }
69 
70  std::string inputName = us::any_cast<std::string>(parsedArgs["input"]);
71  std::string outputName = us::any_cast<std::string>(parsedArgs["output"]);
72  std::string type = us::any_cast<std::string>(parsedArgs["type"]);
73 
74  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(inputName);
75  mitk::Image::Pointer outputImage = mitk::Image::New();
76 
77  if (type.compare("int") == 0) {
78  CONVERT_IMAGE_TYPE(int);
79  }
80  else if (type.compare("uint") == 0)
81  {
82  CONVERT_IMAGE_TYPE(unsigned int);
83  }
84  else if (type.compare("char") == 0)
85  {
86  CONVERT_IMAGE_TYPE(char);
87  }
88  else if (type.compare("uchar") == 0)
89  {
90  CONVERT_IMAGE_TYPE(unsigned char);
91  }
92  else if (type.compare("short") == 0)
93  {
94  CONVERT_IMAGE_TYPE(short);
95  }
96  else if (type.compare("ushort") == 0)
97  {
98  CONVERT_IMAGE_TYPE(unsigned short);
99  }
100  else if (type.compare("float") == 0)
101  {
102  CONVERT_IMAGE_TYPE(float);
103  }
104  else if (type.compare("double") == 0)
105  {
106  CONVERT_IMAGE_TYPE(double);
107  }
108  else if (type.compare("none") == 0)
109  {
110  MITK_INFO << " No conversion performed";
111  outputImage = nullptr;
112  }
113  else
114  {
115  CONVERT_IMAGE_TYPE(double);
116  }
117 
118  if (outputImage.IsNotNull())
119  {
120  mitk::IOUtil::Save(outputImage, outputName);
121  }
122  return EXIT_SUCCESS;
123 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
int main(int argc, char *argv[])
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)
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
mitk::Image::Pointer image
static Pointer New()
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
#define CONVERT_IMAGE_TYPE(TYPE)
void setTitle(std::string title)
void setDescription(std::string description)