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 //////////////////////////////////////////////////////////////////////////////
32 #include <boost/iostreams/filtering_streambuf.hpp>
33 #include <boost/iostreams/copy.hpp>
34 #include <boost/iostreams/filter/zlib.hpp>
35 //////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // Interface - public :
42 #define MAX_HEADERNUMLINES 64
46 template <typename T, typename TFunctor>
49 DGtal::VolReader<T, TFunctor>::importVol( const std::string & filename,
50 const Functor & aFunctor)
53 DGtal::IOException dgtalexception;
56 typename T::Point firstPoint( 0, 0, 0 );
57 typename T::Point lastPoint( 0, 0, 0 );
58 T nullImage( typename T::Domain( firstPoint, lastPoint ));
60 HeaderField header[ MAX_HEADERNUMLINES ];
64 err = fopen_s( &fin, filename.c_str() , "rb" );
67 trace.error() << "VolReader : can't open " << filename << std::endl;
71 fin = fopen( filename.c_str() , "rb" );
76 trace.error() << "VolReader : can't open " << filename << std::endl;
87 // Read the file line by line until ".\n" is found
88 for ( char *line = fgets( buf, 128, fin );
89 line && strcmp( line, ".\n" ) != 0 ;
90 line = fgets( line, 128, fin ), ++linecount
94 if ( line[strlen( line ) - 1] != '\n' )
96 trace.error() << "VolReader: Line " << linecount << " too long" << std::endl;
101 for ( i = 0; line[i] && line[i] != ':'; ++i )
104 if ( i == 0 || i >= 126 || line[i] != ':' )
106 trace.error() << "VolReader: Invalid header read at line " << linecount << std::endl;
107 throw dgtalexception;
112 if ( fieldcount == MAX_HEADERNUMLINES )
114 trace.warning() << "VolReader: Too many lines in HEADER, ignoring\n";
117 if ( fieldcount > MAX_HEADERNUMLINES )
120 // Remove \n from end of line
121 if ( line[ strlen( line ) - 1 ] == '\n' )
122 line[ strlen( line ) - 1 ] = 0;
124 // hack : split line into two str ...
126 header[ fieldcount++ ] = HeaderField( line, line + i + 2 );
127 // +2 cause we skip the space
128 // following the colon
132 // Check required headers
133 for ( int i = 0; requiredHeaders[i]; ++i )
135 if ( getHeaderValue( "Version" , header ) != NULL &&
136 ( strcmp( requiredHeaders[i], "Int-Endian" ) == 0 ||
137 strcmp( requiredHeaders[i], "Voxel-Endian" ) == 0 ) )
141 if ( getHeaderField( requiredHeaders[i] , header ) == -1 )
143 trace.error() << "VolReader: Required Header Field missing: "
144 << requiredHeaders[i] << std::endl;
145 throw dgtalexception;
150 int sx = 0, sy= 0, sz= 0;
151 int cx = 0, cy= 0, cz= 0;
154 getHeaderValueAsInt( "X", &sx, header );
155 getHeaderValueAsInt( "Y", &sy, header );
156 getHeaderValueAsInt( "Z", &sz, header );
157 getHeaderValueAsInt( "Version", &version, header);
159 if (! ((version == 2) || (version == 3)))
161 trace.error() << "VolReader: invalid Version header (must be either 2 or 3)\n";
162 throw dgtalexception;
167 if( getHeaderValueAsInt( "Center-X", &cx, header ) == 0 )
169 getHeaderValueAsInt( "Center-Y", &cy, header );
170 getHeaderValueAsInt( "Center-Z", &cz, header );
172 firstPoint[0] = cx - (sx - 1)/2;
173 firstPoint[1] = cy - (sy - 1)/2;
174 firstPoint[2] = cz - (sz - 1)/2;
175 lastPoint[0] = cx + sx/2;
176 lastPoint[1] = cy + sy/2;
177 lastPoint[2] = cz + sz/2;
181 firstPoint = T::Point::zero;
182 lastPoint[0] = sx - 1;
183 lastPoint[1] = sy - 1;
184 lastPoint[2] = sz - 1;
187 typename T::Domain domain( firstPoint, lastPoint );
195 typename T::Domain::ConstIterator it = domain.begin();
196 size_t total = sx * sy * sz;
197 std::stringstream main;
200 while (( count < total ) && ( fin ) )
207 if ( count != total )
209 trace.error() << "VolReader: can't read file (raw data). I read "<<count<<" bytes instead of "<<total<<".\n";
210 throw dgtalexception;
213 //Uncompress if needed
216 std::stringstream uncompressed;
217 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
218 in.push(boost::iostreams::zlib_decompressor());
220 boost::iostreams::copy(in, uncompressed);
221 //Apply to the image structure
222 for(size_t i=0; i < total; ++i)
224 val = uncompressed.get();
225 image.setValue(( *it ), aFunctor(val) );
231 //Apply to the image structure
232 for(size_t i=0; i < total; ++i)
235 image.setValue(( *it ), aFunctor(val) );
244 trace.error() << "VolReader: not enough memory\n" ;
245 throw dgtalexception;
252 template <typename T, typename TFunctor>
253 const char *DGtal::VolReader<T, TFunctor>::requiredHeaders[] =
255 "X", "Y", "Z", "Voxel-Size", "Int-Endian", "Voxel-Endian", "Alpha-Color", NULL
261 template<typename T, typename TFunctor>
264 DGtal::VolReader<T, TFunctor>::getHeaderField( const char *type, const HeaderField * header )
268 for ( int i = 0; i < MAX_HEADERNUMLINES; ++i )
270 if ( header[i].type != NULL && strcmp( header[i].type, type ) == 0 )
279 template<typename T, typename TFunctor>
282 DGtal::VolReader<T, TFunctor>::getHeaderValue( const char *type, const HeaderField * header )
285 int i = getHeaderField( type, header );
288 return header[i].value;
293 template<typename T, typename TFunctor>
296 DGtal::VolReader<T, TFunctor>::getHeaderValueAsInt( const char *type, int *dest, const HeaderField * header )
298 int i = getHeaderField( type, header );
302 return sscanf( header[i].value, "%d", dest ) == 0;