DGtalTools  1.2.0
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 "CLI11.hpp"
36 
37 
38 using namespace std;
39 using namespace DGtal;
40 using namespace Z3i;
41 
42 
78 void missingParam ( const std::string &param )
79 {
80  trace.error() <<" Parameter: "<<param<<" is required..";
81  trace.info() <<std::endl;
82  exit ( 1 );
83 }
84 
85 template <typename Word>
86 static
87 std::ostream& write_word( std::ostream& outs, Word value )
88 {
89  for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
90  outs.put( static_cast <char> (value & 0xFF) );
91  return outs;
92 }
93 
94 
95 int main(int argc, char**argv)
96 {
97 
98 
99 
100  // parse command line using CLI ----------------------------------------------
101  CLI::App app;
102  std::string inputFileName;
103  std::string outputFileName{"result.vox"};
104  app.description("Convert a vol (up to [0,126]^3) to a vox.\n The current exporter does not support custom colormaps, only the voxel values are exported \n Example: vol2vox -i ${DGtal}/examples/samples/Al.100.vol -o Al.100.vox");
105  app.add_option("-i,--input,1", inputFileName, "Input vol file.")
106  ->required()
107  ->check(CLI::ExistingFile);
108  app.add_option("-o,--output,2", outputFileName, "Output filename.", true);
109  app.get_formatter()->column_width(40);
110  CLI11_PARSE(app, argc, argv);
111 
112 
113  typedef ImageContainerBySTLVector<Z3i::Domain, unsigned char> MyImageC;
114  trace.beginBlock("Loading..");
115  MyImageC imageL = VolReader< MyImageC >::importVol ( inputFileName );
116  trace.endBlock();
117 
118  if ( (imageL.domain().upperBound() - imageL.domain().lowerBound()).max() > 126 )
119  {
120  trace.error() << "Vol file too large (width > 126)."<<std::endl;
121  trace.info() << std::endl;
122  exit(2);
123  }
124 
125  trace.beginBlock("Exporting...");
126  ofstream myfile;
127  myfile.open (outputFileName, ios::out | ios::binary);
128 
129  DGtal::uint32_t cpt=0;
130  for(auto it = imageL.range().begin(), itend = imageL.range().end();
131  it!=itend; ++it)
132  if (*it != 0)
133  cpt++;
134 
135  Point size = imageL.domain().upperBound() - imageL.domain().lowerBound();
136 
137  /*
138  * VOX file format:
139  *
140  4b VOX' '
141  4b version (150)
142 
143  4b MAIN (chunckid)
144  4b size chucnk content (n)
145  4b size chunck children (m)
146 
147  4b SIZE (chunckid)
148  4b chunck content
149  4b chunck children
150  4bx3 x,y,z
151 
152  4b VOXEL (chunckid)
153  4b chunck content
154  4b chunck children
155  4b number of voxels
156  1b x 4 (x,y,z,idcol) x numVoxels
157 
158  */
159 
160  trace.info()<<size<<std::endl;
161 
162  //HEADER
163  myfile <<'V'<<'O'<<'X'<<' ';
164  // version 150
165  write_word(myfile, (DGtal::uint32_t)150);
166 
167  //Chunck MAIN
168  myfile <<'M'<<'A'<<'I'<<'N';
169  write_word(myfile,DGtal::uint32_t(0)); //size content
170  write_word(myfile,DGtal::uint32_t( (4+4+4 + 12 ) + ((4+ 4 + 4) + (4+ cpt*4)))); //size children
171 
172  //Chunck SIZE
173  myfile <<'S'<<'I'<<'Z'<<'E';
174  write_word(myfile,DGtal::uint32_t(12)); //3x4
175  write_word(myfile,DGtal::uint32_t(0)); //0 children
176  write_word(myfile,DGtal::uint32_t(size[0]+1));
177  write_word(myfile,DGtal::uint32_t(size[1]+1));
178  write_word(myfile,DGtal::uint32_t(size[2]+1));
179 
180  //Chunck VOXEL
181  myfile << 'X'<<'Y'<<'Z'<<'I';
182  write_word(myfile, (DGtal::uint32_t)(4+cpt*4)); // 4 + numvoxel * 4
183  write_word(myfile,DGtal::uint32_t(0)); //0 children
184  write_word(myfile, DGtal::uint32_t(cpt)); //numvoxels
185 
186  trace.info() << "Number of voxels= "<<cpt<<std::endl;
187 
188  //Data
189  for(auto it = imageL.domain().begin(), itend = imageL.domain().end();
190  it!=itend; ++it)
191  if (imageL(*it) != 0)
192  {
193  Point p = (*it) - imageL.domain().lowerBound();
194  myfile.put((DGtal::uint8_t)p[0]);
195  myfile.put( (DGtal::uint8_t)p[1]);
196  myfile.put( (DGtal::uint8_t)p[2]);
197  myfile.put(imageL(*it));
198  }
199 
200  myfile.close();
201  trace.endBlock();
202 
203  return 0;
204 }
Definition: ATu0v1.h:57