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