DGtalTools  0.9.4
longvol2vol.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/helpers/StdDefs.h>
31 #include <DGtal/io/readers/LongvolReader.h>
32 #include <DGtal/io/writers/VolWriter.h>
33 #include <DGtal/images/Image.h>
34 #include <DGtal/images/ImageContainerBySTLVector.h>
35 #include <DGtal/base/BasicFunctors.h>
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 DGtal;
41 using namespace Z3i;
42 
43 namespace po = boost::program_options;
44 
75 struct CycleFunctor
76 {
77  template<typename TInput>
78  inline
79  unsigned char operator()(const TInput& aInput) const
80  {
81  if (aInput == 0)
82  return 0;
83  else
84  return static_cast<unsigned char>(1+NumberTraits<TInput>::castToInt64_t(aInput) % 32);
85  }
86 };
87 
88 
92 struct LinearFunctor
93 {
94  LinearFunctor(const DGtal::uint64_t vmax, const DGtal::uint64_t vmin):
95  myMin(vmin), myMax(vmax) {}
96 
97  inline unsigned char operator()(const DGtal::int64_t& aInput) const
98  {
99  return static_cast<unsigned char>( (NumberTraits<DGtal::uint64_t>::castToDouble(aInput - myMin)*255.0 /
100  (double) (myMax-myMin)));
101  }
102 
103  DGtal::uint64_t myMin,myMax;
104 };
105 
106 
107 
113 void missingParam ( std::string param )
114 {
115  trace.error() <<" Parameter: "<<param<<" is required..";
116  trace.info() <<std::endl;
117  exit ( 1 );
118 }
119 
120 int main(int argc, char**argv)
121 {
122 
123  // parse command line ----------------------------------------------
124  po::options_description general_opt ( "Allowed options are: " );
125  general_opt.add_options()
126  ( "help,h", "display this message." )
127  ( "input,i", po::value<std::string>(), "Input longvol filename." )
128  ( "output,o", po::value<std::string>(),"Output vole filename." )
129  ( "mode,m", po::value<unsigned int>()->default_value(0),"Conversion mode:\n\t 0 = cast (default)\n\t 1 = Linear Scaling\n\t 2 = Grayscale cycle (32 steps, except 0 values)." )
130  ;
131  bool parseOK=true;
132  po::variables_map vm;
133  try{
134  po::store(po::parse_command_line(argc, argv, general_opt), vm);
135  }catch(const std::exception& ex){
136  parseOK=false;
137  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
138  }
139  po::notify ( vm );
140  if (!parseOK || vm.count ( "help" ) ||argc<=1 )
141  {
142  trace.info() << "Converts a longvol (long int) to a vol file (unsigned char)."<<std::endl
143  << std::endl << "Basic usage: "<<std::endl
144  << "\tlongvol2vol --input <LongvolFileName> --o <VolOutputFileName> "<<std::endl
145  << general_opt << "\n";
146  return 0;
147  }
148 
149  //Parse options
150  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
151  std::string filename = vm["input"].as<std::string>();
152  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
153  std::string outputFileName = vm["output"].as<std::string>();
154  unsigned int mode = vm["mode"].as<unsigned int>();
155 
156 
157  //Main program
159  MyImageC imageC = LongvolReader< MyImageC >::importLongvol ( filename );
160  bool res = false;
161 
162  if (mode == 0)
163  res = VolWriter< MyImageC, functors::Cast<unsigned char> >::exportVol(outputFileName, imageC);
164  else
165  if (mode == 1)
166  {
167  DGtal::uint64_t vmax, vmin;
168  vmax = *std::max_element(imageC.begin(), imageC.end());
169  vmin = *std::min_element(imageC.begin(), imageC.end());
170  trace.info() << "Max value = "<< vmax<<std::endl;
171  trace.info() << "Min value = "<< vmin<<std::endl;
172  res = VolWriter<MyImageC , LinearFunctor>::exportVol(outputFileName, imageC, true, LinearFunctor(vmax,vmin));
173  }
174  if (mode == 2)
175  res = VolWriter<MyImageC, CycleFunctor>::exportVol(outputFileName, imageC);
176 
177  if (res)
178  return 0;
179  else
180  {
181  trace.error()<<"Error while exporting the Vol.";
182  trace.info()<<std::endl;
183  return 1;
184  }
185 }
static double castToDouble(const T &aT)
static DGtal::int64_t castToInt64_t(const T &aT)
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
Trace trace(traceWriterTerm)
std::ostream & info()
boost::uint64_t uint64_t
std::ostream & error()
static bool exportVol(const std::string &filename, const Image &aImage, const Functor &aFunctor=Functor())
boost::int64_t int64_t