Main method to import a Vol into an instance of the template parameter ImageContainer.
{
FILE * fin;
typename T::Point firstPoint( 0, 0, 0 );
typename T::Point lastPoint( 0, 0, 0 );
T nullImage( typename T::Domain( firstPoint, lastPoint ));
#ifdef WIN32
errno_t err;
err = fopen_s( &fin, filename.c_str() , "r" );
if ( err )
{
trace.
error() <<
"VolReader : can't open " << filename << endl;
throw dgtalexception;
}
#else
fin = fopen( filename.c_str() , "r" );
#endif
if ( fin == NULL )
{
trace.
error() <<
"VolReader : can't open " << filename << endl;
throw dgtalexception;
}
char buf[128];
int linecount = 1;
int fieldcount = 0;
for ( char *line = fgets( buf, 128, fin );
line && strcmp( line, ".\n" ) != 0 ;
line = fgets( line, 128, fin ), ++linecount
)
{
if ( line[strlen( line ) - 1] != '\n' )
{
trace.
error() <<
"VolReader: Line " << linecount <<
" too long" << std::endl;
throw dgtalexception;
}
int i;
for ( i = 0; line[i] && line[i] != ':'; ++i )
;
if ( i == 0 || i >= 126 || line[i] != ':' )
{
trace.
error() <<
"VolReader: Invalid header read at line " << linecount << std::endl;
throw dgtalexception;
}
else
{
{
trace.
warning() <<
"VolReader: Too many lines in HEADER, ignoring\n";
continue;
}
continue;
if ( line[ strlen( line ) - 1 ] == '\n' )
line[ strlen( line ) - 1 ] = 0;
line[i] = 0;
header[ fieldcount++ ] = HeaderField( line, line + i + 2 );
}
}
{
{
continue;
}
{
trace.
error() <<
"VolReader: Required Header Field missing: "
throw dgtalexception;
}
}
int sx, sy, sz;
{
int rawsx, rawsy, rawsz;
long int count = 0;
count += (long)fread( &rawsx, sizeof( int ), 1, fin );
count += (long)fread( &rawsy, sizeof( int ), 1, fin );
count += (long)fread( &rawsz, sizeof( int ), 1, fin );
if ( count != 3 )
{
trace.
error() <<
"VolReader: can't read file (raw header)\n";
throw dgtalexception;
}
if ( sx != rawsx || sy != rawsy || sz != rawsz )
{
trace.
warning() <<
"VolReader: Warning : Incoherent vol header with raw header !\n";
}
int voxsize;
{
trace.
error() <<
"VolReader: This file was generated with a voxel-size that we do not support.\n";
throw dgtalexception;
}
char tmp;
count = (long)fread( &tmp, sizeof( char ), 1, fin );
if ( count != 1 || tmp != '\n' )
{
trace.
error() <<
"VolReader: I thouhgt I would have read a \\n !\n";
throw dgtalexception;
}
}
long count = 0;
firstPoint = T::Point::zero;
lastPoint[0] = sx - 1;
lastPoint[1] = sy - 1;
lastPoint[2] = sz - 1;
typename T::Domain domain( firstPoint, lastPoint );
try
{
T image( domain );
count = 0;
unsigned char val;
typename T::Domain::ConstIterator it = domain.begin();
long int total = sx * sy * sz;
while (( count < total ) && ( fin ) )
{
val = getc( fin );
image.setValue(( *it ), val );
it++;
count++;
}
if ( count != total )
{
trace.
error() <<
"VolReader: can't read file (raw data) !\n";
throw dgtalexception;
}
fclose( fin );
return image;
}
catch ( ... )
{
trace.
error() <<
"VolReader: not enough memory\n" ;
throw dgtalexception;
}
}