DGtalTools  0.9.4
vol2vox.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 
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 
45 
75 void missingParam ( const std::string &param )
76 {
77  trace.error() <<" Parameter: "<<param<<" is required..";
78  trace.info() <<std::endl;
79  exit ( 1 );
80 }
81 
82 template <typename Word>
83 static
84 std::ostream& write_word( std::ostream& outs, Word value )
85 {
86  for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
87  outs.put( static_cast <char> (value & 0xFF) );
88  return outs;
89 }
90 
91 
92 int main(int argc, char**argv)
93 {
94 
95  // parse command line ----------------------------------------------
96  po::options_description general_opt ( "Allowed options are: " );
97  general_opt.add_options()
98  ( "help,h", "display this message." )
99  ( "input,i", po::value<std::string>(), "Input vol file." )
100  ( "output,o", po::value<string>(),"Output filename." );
101  bool parseOK=true;
102  po::variables_map vm;
103  try{
104  po::store(po::parse_command_line(argc, argv, general_opt), vm);
105  }catch(const std::exception& ex){
106  parseOK=false;
107  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
108  }
109  po::notify ( vm );
110  if ( !parseOK || vm.count ( "help" ) ||argc<=1 )
111  {
112  trace.info() << "Convert a vol (up to [0,126]^3) to a vox. The current exporter does not support custom colormaps, only the voxel values are exported." <<std::endl
113  << std::endl << "Basic usage: "<<std::endl
114  << "\tvol2vox --input <volFileName> --o <volOutputFileName> "<<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 
126 
127  trace.beginBlock("Loading..");
128  MyImageC imageL = VolReader< MyImageC >::importVol ( filename );
129  trace.endBlock();
130 
131  if ( (imageL.domain().upperBound() - imageL.domain().lowerBound()).max() > 126 )
132  {
133  trace.error() << "Vol file too large (width > 126)."<<std::endl;
134  trace.info() << std::endl;
135  exit(2);
136  }
137 
138  trace.beginBlock("Exporting...");
139  ofstream myfile;
140  myfile.open (outputFileName, ios::out | ios::binary);
141 
142  DGtal::uint32_t cpt=0;
143  for(auto it = imageL.range().begin(), itend = imageL.range().end();
144  it!=itend; ++it)
145  if (*it != 0)
146  cpt++;
147 
148  Point size = imageL.domain().upperBound() - imageL.domain().lowerBound();
149 
150  /*
151  * VOX file format:
152  *
153  4b VOX' '
154  4b version (150)
155 
156  4b MAIN (chunckid)
157  4b size chucnk content (n)
158  4b size chunck children (m)
159 
160  4b SIZE (chunckid)
161  4b chunck content
162  4b chunck children
163  4bx3 x,y,z
164 
165  4b VOXEL (chunckid)
166  4b chunck content
167  4b chunck children
168  4b number of voxels
169  1b x 4 (x,y,z,idcol) x numVoxels
170 
171  */
172 
173  trace.info()<<size<<std::endl;
174 
175  //HEADER
176  myfile <<'V'<<'O'<<'X'<<' ';
177  // version 150
178  write_word(myfile, (DGtal::uint32_t)150);
179 
180  //Chunck MAIN
181  myfile <<'M'<<'A'<<'I'<<'N';
182  write_word(myfile,DGtal::uint32_t(0)); //size content
183  write_word(myfile,DGtal::uint32_t( (4+4+4 + 12 ) + ((4+ 4 + 4) + (4+ cpt*4)))); //size children
184 
185  //Chunck SIZE
186  myfile <<'S'<<'I'<<'Z'<<'E';
187  write_word(myfile,DGtal::uint32_t(12)); //3x4
188  write_word(myfile,DGtal::uint32_t(0)); //0 children
189  write_word(myfile,DGtal::uint32_t(size[0]+1));
190  write_word(myfile,DGtal::uint32_t(size[1]+1));
191  write_word(myfile,DGtal::uint32_t(size[2]+1));
192 
193  //Chunck VOXEL
194  myfile << 'X'<<'Y'<<'Z'<<'I';
195  write_word(myfile, (DGtal::uint32_t)(4+cpt*4)); // 4 + numvoxel * 4
196  write_word(myfile,DGtal::uint32_t(0)); //0 children
197  write_word(myfile, DGtal::uint32_t(cpt)); //numvoxels
198 
199  trace.info() << "Number of voxels= "<<cpt<<std::endl;
200 
201  //Data
202  for(auto it = imageL.domain().begin(), itend = imageL.domain().end();
203  it!=itend; ++it)
204  if (imageL(*it) != 0)
205  {
206  Point p = (*it) - imageL.domain().lowerBound();
207  myfile.put((DGtal::uint8_t)p[0]);
208  myfile.put( (DGtal::uint8_t)p[1]);
209  myfile.put( (DGtal::uint8_t)p[2]);
210  myfile.put(imageL(*it));
211  }
212 
213  myfile.close();
214  trace.endBlock();
215 
216  return 0;
217 }
void beginBlock(const std::string &keyword="")
boost::uint32_t uint32_t
STL namespace.
double endBlock()
boost::uint8_t uint8_t
Trace trace(traceWriterTerm)
std::ostream & info()
typename Self::Point Point
std::ostream & error()