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