2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
24 * Implementation of inline methods defined in VolReader.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
33#if defined(__GNUC__) || defined(__clang__)
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wall"
36#pragma GCC diagnostic ignored "-Wextra"
37#pragma GCC diagnostic ignored "-Wpedantic"
38#pragma GCC diagnostic ignored "-Wshadow"
39#pragma GCC diagnostic ignored "-Wtype-limits"
40#pragma GCC diagnostic ignored "-Wtautological-compare"
42#include <boost/iostreams/filtering_streambuf.hpp>
43#include <boost/iostreams/copy.hpp>
44#include <boost/iostreams/filter/zlib.hpp>
45#if defined(__GNUC__) || defined(__clang__)
46#pragma GCC diagnostic pop
50//////////////////////////////////////////////////////////////////////////////
53///////////////////////////////////////////////////////////////////////////////
54// Interface - public :
57#define MAX_HEADERNUMLINES 64
61template <typename T, typename TFunctor>
64DGtal::VolReader<T, TFunctor>::importVol( const std::string & filename,
65 const Functor & aFunctor)
68 DGtal::IOException dgtalexception;
71 typename T::Point firstPoint( 0, 0, 0 );
72 typename T::Point lastPoint( 0, 0, 0 );
73 T nullImage( typename T::Domain( firstPoint, lastPoint ));
75 HeaderField header[ MAX_HEADERNUMLINES ];
79 err = fopen_s( &fin, filename.c_str() , "rb" );
82 trace.error() << "VolReader : can't open " << filename << std::endl;
86 fin = fopen( filename.c_str() , "rb" );
91 trace.error() << "VolReader : can't open " << filename << std::endl;
102 // Read the file line by line until ".\n" is found
103 for ( line = fgets( buf, 128, fin );
104 line && strcmp( line, ".\n" ) != 0 ;
105 line = fgets( line, 128, fin ), ++linecount
109 if ( line[strlen( line ) - 1] != '\n' )
111 trace.error() << "VolReader: Line " << linecount << " too long" << std::endl;
112 throw dgtalexception;
116 for ( i = 0; line[i] && line[i] != ':'; ++i )
119 if ( i == 0 || i >= 126 || line[i] != ':' )
121 trace.error() << "VolReader: Invalid header read at line " << linecount << std::endl;
122 throw dgtalexception;
127 if ( fieldcount == MAX_HEADERNUMLINES )
129 trace.warning() << "VolReader: Too many lines in HEADER, ignoring\n";
132 if ( fieldcount > MAX_HEADERNUMLINES )
135 // Remove \n from end of line
136 if ( line[ strlen( line ) - 1 ] == '\n' )
137 line[ strlen( line ) - 1 ] = 0;
139 // hack : split line into two str ...
141 header[ fieldcount++ ] = HeaderField( line, line + i + 2 );
142 // +2 cause we skip the space
143 // following the colon
147 // Check required headers
148 for ( int i = 0; requiredHeaders[i]; ++i )
150 if ( getHeaderValue( "Version" , header ) != NULL &&
151 ( strcmp( requiredHeaders[i], "Int-Endian" ) == 0 ||
152 strcmp( requiredHeaders[i], "Voxel-Endian" ) == 0 ) )
156 if ( getHeaderField( requiredHeaders[i] , header ) == -1 )
158 trace.error() << "VolReader: Required Header Field missing: "
159 << requiredHeaders[i] << std::endl;
160 throw dgtalexception;
165 int sx = 0, sy= 0, sz= 0;
166 int cx = 0, cy= 0, cz= 0;
169 getHeaderValueAsInt( "X", &sx, header );
170 getHeaderValueAsInt( "Y", &sy, header );
171 getHeaderValueAsInt( "Z", &sz, header );
172 getHeaderValueAsInt( "Version", &version, header);
174 if (! ((version == 2) || (version == 3)))
176 trace.error() << "VolReader: invalid Version header (must be either 2 or 3)\n";
177 throw dgtalexception;
182 if( getHeaderValueAsInt( "Center-X", &cx, header ) == 0 )
184 getHeaderValueAsInt( "Center-Y", &cy, header );
185 getHeaderValueAsInt( "Center-Z", &cz, header );
187 firstPoint[0] = cx - (sx - 1)/2;
188 firstPoint[1] = cy - (sy - 1)/2;
189 firstPoint[2] = cz - (sz - 1)/2;
190 lastPoint[0] = cx + sx/2;
191 lastPoint[1] = cy + sy/2;
192 lastPoint[2] = cz + sz/2;
196 firstPoint = T::Point::zero;
197 lastPoint[0] = sx - 1;
198 lastPoint[1] = sy - 1;
199 lastPoint[2] = sz - 1;
202 typename T::Domain domain( firstPoint, lastPoint );
210 typename T::Domain::ConstIterator it = domain.begin();
211 size_t total = sx * sy * sz;
212 std::stringstream main;
215 while (( count < total ) && ( fin ) )
222 if ( count != total )
224 trace.error() << "VolReader: can't read file (raw data). I read "<<count<<" bytes instead of "<<total<<".\n";
225 throw dgtalexception;
228 //Uncompress if needed
231 std::stringstream uncompressed;
232 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
233 in.push(boost::iostreams::zlib_decompressor());
235 boost::iostreams::copy(in, uncompressed);
236 //Apply to the image structure
237 for(size_t i=0; i < total; ++i)
239 val = uncompressed.get();
240 image.setValue(( *it ), aFunctor(val) );
246 //Apply to the image structure
247 for(size_t i=0; i < total; ++i)
250 image.setValue(( *it ), aFunctor(val) );
259 trace.error() << "VolReader: not enough memory\n" ;
260 throw dgtalexception;
267 template <typename T, typename TFunctor>
268 const char *DGtal::VolReader<T, TFunctor>::requiredHeaders[] =
270 "X", "Y", "Z", "Voxel-Size", "Int-Endian", "Voxel-Endian", "Alpha-Color", NULL
276 template<typename T, typename TFunctor>
279 DGtal::VolReader<T, TFunctor>::getHeaderField( const char *type, const HeaderField * header )
283 for ( int i = 0; i < MAX_HEADERNUMLINES; ++i )
285 if ( header[i].type != NULL && strcmp( header[i].type, type ) == 0 )
294 template<typename T, typename TFunctor>
297 DGtal::VolReader<T, TFunctor>::getHeaderValue( const char *type, const HeaderField * header )
300 int i = getHeaderField( type, header );
303 return header[i].value;
308 template<typename T, typename TFunctor>
311 DGtal::VolReader<T, TFunctor>::getHeaderValueAsInt( const char *type, int *dest, const HeaderField * header )
313 int i = getHeaderField( type, header );
317 return sscanf( header[i].value, "%d", dest ) == 0;