DGtalTools  0.9.4
raw2HDF5.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/io/readers/RawReader.h>
31 #include <DGtal/io/writers/HDF5Writer.h>
32 #include <DGtal/helpers/StdDefs.h>
33 #include <DGtal/images/Image.h>
34 #include <DGtal/images/ImageContainerBySTLVector.h>
35 
36 #include <boost/program_options/options_description.hpp>
37 #include <boost/program_options/parsers.hpp>
38 #include <boost/program_options/variables_map.hpp>
39 
40 using namespace std;
41 using namespace DGtal;
42 using namespace Z3i;
43 
44 namespace po = boost::program_options;
45 
79 void missingParam ( std::string param )
80 {
81  trace.error() <<" Parameter: "<<param<<" is required..";
82  trace.info() <<std::endl;
83  exit ( 1 );
84 }
85 
86 
87 int main(int argc, char**argv)
88 {
89 
90  // parse command line ----------------------------------------------
91  po::options_description general_opt ( "Allowed options are: " );
92  general_opt.add_options()
93  ( "help,h", "display this message." )
94  ( "input,i", po::value<std::string>(), "Input raw file." )
95  ( "output,o", po::value<string>(),"Output HDF5 filename." )
96  ( "x,x", po::value<unsigned int>(),"x extent." )
97  ( "y,y", po::value<unsigned int >(),"y extent." )
98  ( "z,z", po::value<unsigned int>(),"z extent." );
99 
100  bool parseOK=true;
101  po::variables_map vm;
102  try{
103  po::store(po::parse_command_line(argc, argv, general_opt), vm);
104  }catch(const std::exception& ex){
105  parseOK=false;
106  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
107  }
108 
109  po::notify ( vm );
110  if (!parseOK || vm.count ( "help" ) ||argc<=1 )
111  {
112  trace.info() << "Converts a 3D 8-bit raw file to HDF5."<<std::endl
113  << std::endl << "Basic usage: "<<std::endl
114  << "\traw2HDF5 -x 128 -y 128 -z 128 --input <RawFileName> --output <HDF5OutputFileName> "<<std::endl
115  << general_opt << "\n";
116  return 0;
117  }
118 
119  //Parse options
120  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
121  std::string filename = vm["input"].as<std::string>();
122  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
123  std::string outputFileName = vm["output"].as<std::string>();
124  if ( ! ( vm.count ( "x" ) ) ) missingParam ( "--x" );
125  unsigned int x = vm["x"].as<unsigned int>();
126  if ( ! ( vm.count ( "y" ) ) ) missingParam ( "--y" );
127  unsigned int y = vm["y"].as<unsigned int>();
128  if ( ! ( vm.count ( "z" ) ) ) missingParam ( "--z" );
129  unsigned int z = vm["z"].as<unsigned int>();
130 
131 
133 
134  MyImageC imageC = RawReader< MyImageC >::importRaw8 ( filename, Z3i::Vector(x,y,z) );
135  bool res = HDF5Writer< MyImageC>::exportHDF5_3D(outputFileName, imageC, "/UInt8Array3D");
136 
137  if (res)
138  return 0;
139  else
140  {
141  trace.error()<< "Error while exporting the HDF5 file."<<std::endl;
142  return 1;
143  }
144 }
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()