Main method to import a Longvol 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 ));
fin = fopen( filename.c_str() , "r" );
if ( fin == NULL )
{
trace.
error() <<
"LongvolReader : 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() <<
"LongvolReader: 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() <<
"LongvolReader: Invalid header read at line " << linecount << std::endl;
throw dgtalexception;
}
else
{
{
trace.
warning() <<
"LongvolReader: 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() <<
"LongvolReader: 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() <<
"LongvolReader: can't read file (raw header)\n";
throw dgtalexception;
}
if ( sx != rawsx || sy != rawsy || sz != rawsz )
{
trace.
warning() <<
"LongvolReader: Warning : Incoherent longvol header with raw header !\n";
}
int voxsize;
{
trace.
error() <<
"LongvolReader: 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() <<
"LongvolReader: 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;
typename T::Domain::ConstIterator it = domain.begin();
long int total = sx * sy * sz;
while (( count < total ) && ( fin ) )
{
image.setValue(( *it ), val );
it++;
count++;
}
if ( count != total )
{
trace.
error() <<
"LongvolReader: can't read file (raw data) !\n";
throw dgtalexception;
}
fclose( fin );
return image;
}
catch ( ... )
{
trace.
error() <<
"LongvolReader: not enough memory\n" ;
throw dgtalexception;
}
}