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/>.
18 * @file LongvolReader.ih
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 LongvolReader.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//! Maximum number of fields in a .longvol file header
39#define MAX_HEADERNUMLINES 64
42///////////////////////////////////////////////////////////////////////////////
43// Interface - public :
44template <typename T, typename TFunctor>
47DGtal::LongvolReader<T, TFunctor>::importLongvol( const std::string & filename,
48 const Functor & aFunctor)
51 DGtal::IOException dgtalexception;
54 typename T::Point firstPoint( 0, 0, 0 );
55 typename T::Point lastPoint( 0, 0, 0 );
56 T nullImage( typename T::Domain(firstPoint, lastPoint ));
58 HeaderField header[ MAX_HEADERNUMLINES ];
60 fin = fopen( filename.c_str() , "rb" );
64 trace.error() << "LongvolReader : can't open " << filename << std::endl;
75 // Read the file line by line until ".\n" is found
76 for ( char *line = fgets( buf, 128, fin );
77 line && strcmp( line, ".\n" ) != 0 ;
78 line = fgets( line, 128, fin ), ++linecount
82 if ( line[strlen( line ) - 1] != '\n' )
84 trace.error() << "LongvolReader: Line " << linecount << " too long" << std::endl;
89 for ( i = 0; line[i] && line[i] != ':'; ++i )
92 if ( i == 0 || i >= 126 || line[i] != ':' )
94 trace.error() << "LongvolReader: Invalid header read at line " << linecount << std::endl;
100 if ( fieldcount == MAX_HEADERNUMLINES )
102 trace.warning() << "LongvolReader: Too many lines in HEADER, ignoring\n";
105 if ( fieldcount > MAX_HEADERNUMLINES )
108 // Remove \n from end of line
109 if ( line[ strlen( line ) - 1 ] == '\n' )
110 line[ strlen( line ) - 1 ] = 0;
112 // hack : split line in two str ...
114 header[ fieldcount++ ] = HeaderField( line, line + i + 2 );
115 // +2 cause we skip the space
116 // following the colon
120 // Check required headers
121 for ( int i = 0; requiredHeaders[i]; ++i )
123 if ( getHeaderValue( "Version" , header ) != NULL &&
124 ( strcmp( requiredHeaders[i], "Int-Endian" ) == 0 ||
125 strcmp( requiredHeaders[i], "Lvoxel-Endian" ) == 0 ) )
129 if ( getHeaderField( requiredHeaders[i] , header ) == -1 )
131 trace.error() << "LongvolReader: Required Header Field missing: "
132 << requiredHeaders[i] << std::endl;
133 throw dgtalexception;
138 int sx = 0, sy = 0, sz=0;
139 int cx = 0, cy = 0, cz=0;
141 getHeaderValueAsInt( "X", &sx, header );
142 getHeaderValueAsInt( "Y", &sy, header );
143 getHeaderValueAsInt( "Z", &sz, header );
144 getHeaderValueAsInt( "Version", &version, header);
146 if (! ((version == 2) || (version == 3)))
148 trace.error() << "LongvolReader: invalid Version header (must be either 2 or 3)\n";
149 throw dgtalexception;
154 if( getHeaderValueAsInt( "Center-X", &cx, header ) == 0 )
156 getHeaderValueAsInt( "Center-Y", &cy, header );
157 getHeaderValueAsInt( "Center-Z", &cz, header );
159 firstPoint[0] = cx - (sx - 1)/2;
160 firstPoint[1] = cy - (sy - 1)/2;
161 firstPoint[2] = cz - (sz - 1)/2;
162 lastPoint[0] = cx + sx/2;
163 lastPoint[1] = cy + sy/2;
164 lastPoint[2] = cz + sz/2;
168 firstPoint = T::Point::zero;
169 lastPoint[0] = sx - 1;
170 lastPoint[1] = sy - 1;
171 lastPoint[2] = sz - 1;
173 typename T::Domain domain( firstPoint, lastPoint );
180 DGtal::uint64_t val=0;
182 typename T::Domain::ConstIterator it = domain.begin();
183 size_t total = sx * sy * sz ;
184 size_t totalbytes = total * sizeof(val);
185 std::stringstream main;
187 unsigned char c_temp;
188 while (( count < totalbytes ) && ( fin ) )
190 c_temp = getc( fin );
195 if ( count != totalbytes )
197 trace.error() << "LongvolReader: can't read file (raw data). I read "<<count<<" bytes instead of "<<total<<".\n";
198 throw dgtalexception;
201 //Uncompress if needed
204 std::stringstream uncompressed;
205 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
206 in.push(boost::iostreams::zlib_decompressor());
208 boost::iostreams::copy(in, uncompressed);
209 //Apply to the image structure
210 for(size_t i=0; i < total; ++i)
212 read_word(uncompressed , val);
213 image.setValue(( *it ), aFunctor(val) );
219 //Apply to the image structure
220 for(size_t i=0; i < total; ++i)
222 read_word(main, val);
223 image.setValue(( *it ), aFunctor(val) );
232 trace.error() << "LongvolReader: not enough memory\n" ;
233 throw dgtalexception;
240 template <typename T, typename TFunctor>
241 const char *DGtal::LongvolReader<T, TFunctor>::requiredHeaders[] =
243 "X", "Y", "Z", "Lvoxel-Size", "Int-Endian", "Lvoxel-Endian", "Alpha-Color", NULL
249 template<typename T, typename TFunctor>
252 DGtal::LongvolReader<T,TFunctor>::getHeaderField( const char *type, const HeaderField * header )
256 for ( int i = 0; i < MAX_HEADERNUMLINES; ++i )
258 if ( header[i].type != NULL && strcmp( header[i].type, type ) == 0 )
267 template<typename T, typename TFunctor>
270 DGtal::LongvolReader<T, TFunctor>::getHeaderValue( const char *type, const HeaderField * header )
273 int i = getHeaderField( type, header );
276 return header[i].value;
281 template<typename T, typename TFunctor>
284 DGtal::LongvolReader<T, TFunctor>::getHeaderValueAsInt( const char *type, int *dest, const HeaderField * header )
286 int i = getHeaderField( type, header );
290 return sscanf( header[i].value, "%d", dest ) == 0;