DGtalTools  1.2.0
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 
37 #include "CLI11.hpp"
38 
39 using namespace DGtal;
40 using namespace Z3i;
41 
42 
78 struct CycleFunctor
79 {
80  template<typename TInput>
81  inline
82  unsigned char operator()(const TInput& aInput) const
83  {
84  if (aInput == 0)
85  return 0;
86  else
87  return static_cast<unsigned char>(1+NumberTraits<TInput>::castToInt64_t(aInput) % 32);
88  }
89 };
90 
91 
95 struct LinearFunctor
96 {
97  LinearFunctor(const DGtal::uint64_t vmax, const DGtal::uint64_t vmin):
98  myMin(vmin), myMax(vmax) {}
99 
100  inline unsigned char operator()(const DGtal::int64_t& aInput) const
101  {
102  return static_cast<unsigned char>( (NumberTraits<DGtal::uint64_t>::castToDouble(aInput - myMin)*255.0 /
103  (double) (myMax-myMin)));
104  }
105 
106  DGtal::uint64_t myMin,myMax;
107 };
108 
109 
110 int main(int argc, char**argv)
111 {
112 
113 
114  // parse command line using CLI ----------------------------------------------
115  CLI::App app;
116  std::string inputFileName;
117  std::string outputFileName {"result.vol"};
118  unsigned int mode {0};
119 
120  app.description("Converts a longvol (long int) to a vol file (unsigned char). \n Basic example:\n\t longvol2vol --input <LongvolFileName> --o <VolOutputFileName> ");
121  app.add_option("-i,--input,1", inputFileName, "Input longvol filename ( .longvol)" )
122  ->required()
123  ->check(CLI::ExistingFile);
124  app.add_option("-o,--output,2",outputFileName,"Output vol filename." );
125  app.add_option("-m,--mode", mode, "Conversion mode:\n\t 0 = cast (default)\n\t 1 = Linear Scaling\n\t 2 = Grayscale cycle (32 steps, except 0 values).")
126  -> check(CLI::IsMember({0, 1, 2}));
127 
128 
129  app.get_formatter()->column_width(40);
130  CLI11_PARSE(app, argc, argv);
131  // END parse command line using CLI ----------------------------------------------
132 
133 
134  //Main program
135  typedef ImageContainerBySTLVector<Z3i::Domain, DGtal::uint64_t> MyImageC;
136  MyImageC imageC = LongvolReader< MyImageC >::importLongvol ( inputFileName );
137  bool res = false;
138 
139  if (mode == 0)
140  res = VolWriter< MyImageC, functors::Cast<unsigned char> >::exportVol(outputFileName, imageC);
141  else
142  if (mode == 1)
143  {
144  DGtal::uint64_t vmax, vmin;
145  vmax = *std::max_element(imageC.begin(), imageC.end());
146  vmin = *std::min_element(imageC.begin(), imageC.end());
147  trace.info() << "Max value = "<< vmax<<std::endl;
148  trace.info() << "Min value = "<< vmin<<std::endl;
149  res = VolWriter<MyImageC , LinearFunctor>::exportVol(outputFileName, imageC, true, LinearFunctor(vmax,vmin));
150  }
151  if (mode == 2)
152  res = VolWriter<MyImageC, CycleFunctor>::exportVol(outputFileName, imageC);
153 
154  if (res)
155  return EXIT_SUCCESS;
156 
157  else
158  {
159  trace.error()<<"Error while exporting the Vol.";
160  trace.info()<<std::endl;
161  return EXIT_FAILURE;
162  }
163 }
Definition: ATu0v1.h:57