50 typename T::Point firstPoint( 0, 0, 0 );
51 typename T::Point lastPoint( 0, 0, 0 );
52 T nullImage(
typename T::Domain( firstPoint, lastPoint ));
58 err = fopen_s( &fin, filename.c_str() ,
"r" );
61 trace.
error() <<
"VolReader : can't open " << filename << endl;
65 fin = fopen( filename.c_str() ,
"r" );
70 trace.
error() <<
"VolReader : can't open " << filename << endl;
82 for (
char *line = fgets( buf, 128, fin );
83 line && strcmp( line,
".\n" ) != 0 ;
84 line = fgets( line, 128, fin ), ++linecount
88 if ( line[strlen( line ) - 1] !=
'\n' )
90 trace.
error() <<
"VolReader: Line " << linecount <<
" too long" << std::endl;
95 for ( i = 0; line[i] && line[i] !=
':'; ++i )
98 if ( i == 0 || i >= 126 || line[i] !=
':' )
100 trace.
error() <<
"VolReader: Invalid header read at line " << linecount << std::endl;
101 throw dgtalexception;
106 if ( fieldcount == MAX_HEADERNUMLINES )
108 trace.
warning() <<
"VolReader: Too many lines in HEADER, ignoring\n";
111 if ( fieldcount > MAX_HEADERNUMLINES )
115 if ( line[ strlen( line ) - 1 ] ==
'\n' )
116 line[ strlen( line ) - 1 ] = 0;
120 header[ fieldcount++ ] =
HeaderField( line, line + i + 2 );
127 for (
int i = 0; requiredHeaders[i]; ++i )
129 if ( getHeaderValue(
"Version" , header ) != NULL &&
130 ( strcmp( requiredHeaders[i],
"Int-Endian" ) == 0 ||
131 strcmp( requiredHeaders[i],
"Voxel-Endian" ) == 0 ) )
135 if ( getHeaderField( requiredHeaders[i] , header ) == -1 )
137 trace.
error() <<
"VolReader: Required Header Field missing: "
138 << requiredHeaders[i] << std::endl;
139 throw dgtalexception;
146 getHeaderValueAsInt(
"X", &sx, header );
147 getHeaderValueAsInt(
"Y", &sy, header );
148 getHeaderValueAsInt(
"Z", &sz, header );
150 if ( getHeaderValue(
"Version", header ) == NULL )
152 int rawsx, rawsy, rawsz;
155 count += (long)fread( &rawsx,
sizeof(
int ), 1, fin );
156 count += (long)fread( &rawsy,
sizeof(
int ), 1, fin );
157 count += (long)fread( &rawsz,
sizeof(
int ), 1, fin );
161 trace.
error() <<
"VolReader: can't read file (raw header)\n";
162 throw dgtalexception;
166 if ( sx != rawsx || sy != rawsy || sz != rawsz )
168 trace.
warning() <<
"VolReader: Warning : Incoherent vol header with raw header !\n";
172 if ( getHeaderValueAsInt(
"Voxel-Size", &voxsize, header ) == 0 && voxsize !=
sizeof(
voxel ) )
174 trace.
error() <<
"VolReader: This file was generated with a voxel-size that we do not support.\n";
175 throw dgtalexception;
180 count = (long)fread( &tmp,
sizeof(
char ), 1, fin );
182 if ( count != 1 || tmp !=
'\n' )
184 trace.
error() <<
"VolReader: I thouhgt I would have read a \\n !\n";
185 throw dgtalexception;
192 firstPoint = T::Point::zero;
193 lastPoint[0] = sx - 1;
194 lastPoint[1] = sy - 1;
195 lastPoint[2] = sz - 1;
196 typename T::Domain domain( firstPoint, lastPoint );
204 typename T::Domain::ConstIterator it = domain.begin();
205 long int total = sx * sy * sz;
207 while (( count < total ) && ( fin ) )
210 image.setValue(( *it ), val );
215 if ( count != total )
217 trace.
error() <<
"VolReader: can't read file (raw data) !\n";
218 throw dgtalexception;
226 trace.
error() <<
"VolReader: not enough memory\n" ;
227 throw dgtalexception;
234 template <
typename T>
237 "X",
"Y",
"Z",
"Voxel-Size",
"Int-Endian",
"Voxel-Endian",
"Alpha-Color", NULL
250 for (
int i = 0; i < MAX_HEADERNUMLINES; ++i )
252 if ( header[i].type != NULL && strcmp( header[i].type, type ) == 0 )
267 int i = getHeaderField( type, header );
270 return header[i].
value;
280 int i = getHeaderField( type, header );
284 return sscanf( header[i].value,
"%d", dest ) != 0;