DGtalTools  1.2.0
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 
36 #include "CLI11.hpp"
37 
38 
39 
40 using namespace std;
41 using namespace DGtal;
42 using namespace Z3i;
43 
44 
77 void missingParam ( const std::string &param )
78 {
79  trace.error() <<" Parameter: "<<param<<" is required..";
80  trace.info() <<std::endl;
81  exit ( 1 );
82 }
83 
92 template <typename Word>
93 static
94 std::istream& read_word( std::istream& fin, Word& aValue )
95 {
96  aValue = 0;
97  char c;
98  for (auto size = 0; size < sizeof( Word ); ++size)
99  {
100  fin.get( c ) ;
101  unsigned char cc=static_cast<unsigned char>(c);
102  aValue |= (cc << (8 * size));
103  }
104  return fin;
105 }
106 template <typename Word>
107 static
108 std::ostream& write_word( std::ostream& outs, Word value )
109 {
110  for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
111  outs.put( static_cast <char> (value & 0xFF) );
112  return outs;
113 }
114 
115 DGtal::uint32_t toInt(const char a,
116  const char b,
117  const char c,
118  const char d)
119 {
120  return (static_cast<DGtal::uint32_t>((unsigned char)a) +
121  ((static_cast<DGtal::uint32_t>((unsigned char) b)) <<8) +
122  ((static_cast<DGtal::uint32_t>((unsigned char) c)) <<16) +
123  ((static_cast<DGtal::uint32_t>((unsigned char) d)) <<24));
124 }
125 
126 
127 int main(int argc, char**argv)
128 {
129 
130  // parse command line using CLI ----------------------------------------------
131  CLI::App app;
132  std::string inputFileName;
133  std::string outputFileName {"result.vol"};
134  app.description("Convert a vox file to a vol. Basic usage:\n vox2vol <volFileName> <volOutputFileName> ");
135  app.add_option("-i,--input,1", inputFileName, "" )
136  ->required()
137  ->check(CLI::ExistingFile);
138  app.add_option("-o,--ouput,2", outputFileName, "", true );
139 
140  app.get_formatter()->column_width(40);
141  CLI11_PARSE(app, argc, argv);
142  // END parse command line using CLI ----------------------------------------------
143 
144  trace.beginBlock("Loading...");
145  ifstream myfile;
146  myfile.open (inputFileName, ios::in | ios::binary);
147 
148  /* VOX file format:
149  *
150  4b VOX' '
151  4b version (150)
152 
153  4b MAIN (chunckid)
154  4b size chucnk content (n)
155  4b size chunck children (m)
156 
157  4b SIZE (chunckid)
158  4b chunck content
159  4b chunck children
160  4bx3 x,y,z
161 
162  4b VOXEL (chunckid)
163  4b chunck content
164  4b chunck children
165  4b number of voxels
166  1b x 4 (x,y,z,idcol) x numVoxels
167 
168  */
169 
170  //HEADER
171  char a,b,c,d;
172  myfile.get(a);
173  myfile.get(b);
174  myfile.get(c);
175  myfile.get(d);
176 
177 
178  if ( ( a != 'V' || (b != 'O') || (c!='X') || (d != ' ')))
179  {
180  trace.error() << "Magic number error"<<std::endl;
181  trace.error() << (int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
182  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
183 
184  exit(2);
185  }
186 
187  myfile.get(a);
188  myfile.get(b);
189  myfile.get(c);
190  myfile.get(d);
191  DGtal::uint32_t version = toInt(a,b,c,d);
192  trace.info()<<"Version = "<<version<<std::endl;
193  //read_word(myfile, version);
194  if (version != 150)
195  {
196  trace.error() << "Version error "<<version<<std::endl;
197  trace.error() << (unsigned int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
198  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
199  exit(2);
200  }
201 
202  read_word(myfile, version);
203  DGtal::uint32_t main = toInt('M','A','I','N');
204  trace.info()<< main << std::endl;
205  trace.info() <<version <<std::endl;
206  if ( version != main)
207  {
208  trace.error() << "MAIN number error"<<std::endl;
209  trace.error() << (int)a<<" "<<(int)b<<" "<<(int) c<<" "<<(int)d<<std::endl;
210  trace.error() << a<<" "<<b<<" "<< c<<" "<<d<<std::endl;
211  exit(2);
212  }
213 
214  DGtal::uint32_t XYZI= toInt('X','Y','Z','I');
215  read_word(myfile,version);
216  while ( version != XYZI)
217  read_word(myfile,version);
218 
219  //trash two ints
220  read_word(myfile,version);
221  read_word(myfile,version);
222  DGtal::uint32_t cpt;
223  read_word(myfile,cpt);
224 
225  Z3i::Domain domain(Z3i::Point(0,0,0), Z3i::Point(126,126,126));
226  ImageContainerBySTLVector<Z3i::Domain, unsigned char> image(domain);
227  trace.info()<< "Number of voxels in this chunk = "<<version<<std::endl;
228  for(auto i=0 ; i<cpt; ++i)
229  {
230  myfile.get(a);
231  myfile.get(b);
232  myfile.get(c);
233  myfile.get(d);
234  image.setValue(Z3i::Point((unsigned int)(unsigned char)a,
235  (unsigned int)(unsigned char)b,
236  (unsigned int)(unsigned char)c),
237  (unsigned char) d);
238  }
239 
240  image >> outputFileName;
241 
242  return EXIT_SUCCESS;
243 
244 }
Definition: ATu0v1.h:57