DGtalTools  0.9.4
vox2vol.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/io/readers/VolReader.h>
31 #include <DGtal/helpers/StdDefs.h>
32 #include <DGtal/images/Image.h>
33 #include <DGtal/images/ImageContainerBySTLVector.h>
34 #include <DGtal/io/writers/GenericWriter.h>
35 #include <boost/program_options/options_description.hpp>
36 #include <boost/program_options/parsers.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 
39 using namespace std;
40 using namespace DGtal;
41 using namespace Z3i;
42 
43 namespace po = boost::program_options;
44 
71 void missingParam ( const std::string &param )
72 {
73  trace.error() <<" Parameter: "<<param<<" is required..";
74  trace.info() <<std::endl;
75  exit ( 1 );
76 }
77 
86 template <typename Word>
87 static
88 std::istream& read_word( std::istream& fin, Word& aValue )
89 {
90  aValue = 0;
91  char c;
92  for (auto size = 0; size < sizeof( Word ); ++size)
93  {
94  fin.get( c ) ;
95  unsigned char cc=static_cast<unsigned char>(c);
96  aValue |= (cc << (8 * size));
97  }
98  return fin;
99 }
100 template <typename Word>
101 static
102 std::ostream& write_word( std::ostream& outs, Word value )
103 {
104  for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
105  outs.put( static_cast <char> (value & 0xFF) );
106  return outs;
107 }
108 
109 DGtal::uint32_t toInt(const char a,
110  const char b,
111  const char c,
112  const char d)
113 {
114  return (static_cast<DGtal::uint32_t>((unsigned char)a) +
115  ((static_cast<DGtal::uint32_t>((unsigned char) b)) <<8) +
116  ((static_cast<DGtal::uint32_t>((unsigned char) c)) <<16) +
117  ((static_cast<DGtal::uint32_t>((unsigned char) d)) <<24));
118 }
119 
120 
121 int main(int argc, char**argv)
122 {
123 
124  // parse command line ----------------------------------------------
125  po::options_description general_opt ( "Allowed options are: " );
126  general_opt.add_options()
127  ( "help,h", "display this message." )
128  ( "input,i", po::value<std::string>(), "Input vox file." )
129  ( "output,o", po::value<string>(),"Output vol filename." );
130  bool parseOK=true;
131  po::variables_map vm;
132  try{
133  po::store(po::parse_command_line(argc, argv, general_opt), vm);
134  }catch(const std::exception& ex){
135  parseOK=false;
136  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
137  }
138  po::notify ( vm );
139  if ( !parseOK || vm.count ( "help" ) ||argc<=1 )
140  {
141  trace.info() << "Convert a vox file to a vol."<<std::endl
142  << std::endl << "Basic usage: "<<std::endl
143  << "\tvox2vol --input <volFileName> --o <volOutputFileName> "<<std::endl
144  << general_opt << "\n";
145  return 0;
146  }
147 
148  //Parse options
149  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
150  std::string filename = vm["input"].as<std::string>();
151  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
152  std::string outputFileName = vm["output"].as<std::string>();
153 
154 
155  trace.beginBlock("Loading...");
156  ifstream myfile;
157  myfile.open (filename, ios::in | ios::binary);
158 
159  /* VOX file format:
160  *
161  4b VOX' '
162  4b version (150)
163 
164  4b MAIN (chunckid)
165  4b size chucnk content (n)
166  4b size chunck children (m)
167 
168  4b SIZE (chunckid)
169  4b chunck content
170  4b chunck children
171  4bx3 x,y,z
172 
173  4b VOXEL (chunckid)
174  4b chunck content
175  4b chunck children
176  4b number of voxels
177  1b x 4 (x,y,z,idcol) x numVoxels
178 
179  */
180 
181  //HEADER
182  char a,b,c,d;
183  myfile.get(a);
184  myfile.get(b);
185  myfile.get(c);
186  myfile.get(d);
187 
188 
189  if ( ( a != 'V' || (b != 'O') || (c!='X') || (d != ' ')))
190  {
191  trace.error() << "Magic number error"<<std::endl;
192  trace.error() << (int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
193  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
194 
195  exit(2);
196  }
197 
198  myfile.get(a);
199  myfile.get(b);
200  myfile.get(c);
201  myfile.get(d);
202  DGtal::uint32_t version = toInt(a,b,c,d);
203  trace.info()<<"Version = "<<version<<std::endl;
204  //read_word(myfile, version);
205  if (version != 150)
206  {
207  trace.error() << "Version error "<<version<<std::endl;
208  trace.error() << (unsigned int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
209  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
210  exit(2);
211  }
212 
213  read_word(myfile, version);
214  DGtal::uint32_t main = toInt('M','A','I','N');
215  trace.info()<< main << std::endl;
216  trace.info() <<version <<std::endl;
217  if ( version != main)
218  {
219  trace.error() << "MAIN number error"<<std::endl;
220  trace.error() << (int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
221  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
222  exit(2);
223  }
224 
225  DGtal::uint32_t XYZI= toInt('X','Y','Z','I');
226  read_word(myfile,version);
227  while ( version != XYZI)
228  read_word(myfile,version);
229 
230  //trash two ints
231  read_word(myfile,version);
232  read_word(myfile,version);
233  DGtal::uint32_t cpt;
234  read_word(myfile,cpt);
235 
236  Z3i::Domain domain(Z3i::Point(0,0,0), Z3i::Point(126,126,126));
237  ImageContainerBySTLVector<Z3i::Domain, unsigned char> image(domain);
238  trace.info()<< "Number of voxels in this chunk = "<<version<<std::endl;
239  for(auto i=0 ; i<cpt; ++i)
240  {
241  myfile.get(a);
242  myfile.get(b);
243  myfile.get(c);
244  myfile.get(d);
245  image.setValue(Z3i::Point((unsigned int)(unsigned char)a,
246  (unsigned int)(unsigned char)b,
247  (unsigned int)(unsigned char)c),
248  (unsigned char) d);
249  }
250 
251  image >> outputFileName;
252 
253 
254  return 0;
255 
256 }
void beginBlock(const std::string &keyword="")
boost::uint32_t uint32_t
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
typename Self::Point Point
std::ostream & error()
typename Self::Domain Domain