DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Static Private Member Functions | Static Private Attributes
DGtal::VolReader< TImageContainer > Struct Template Reference

#include <VolReader.h>

Data Structures

struct  HeaderField

Public Types

typedef TImageContainer ImageContainer

Public Member Functions

 BOOST_STATIC_ASSERT (ImageContainer::Domain::dimension==3)

Static Public Member Functions

static ImageContainer importVol (const std::string &filename) throw (DGtal::IOException)

Private Types

typedef unsigned char voxel

Static Private Member Functions

static const char * getHeaderValue (const char *type, const HeaderField *header)
static int getHeaderValueAsInt (const char *type, int *dest, const HeaderField *header)
static int getHeaderField (const char *type, const HeaderField *header)

Static Private Attributes

static const int MAX_HEADERNUMLINES = 64
static const char * requiredHeaders []

Detailed Description

template<typename TImageContainer>
struct DGtal::VolReader< TImageContainer >

Aim: implements methods to read a "Vol" file format.

Description of template class 'VolReader'

The main import method "importVol" returns an instance of the template parameter TImageContainer.

The private methods have been backported from the SimpleVol project (see http://liris.cnrs.fr/david.coeurjolly).

Example usage:

...
typedef SpaceND<int,3> Space3;
typedef HyperRectDomain<Space3> TDomain;
typedef TDomain::Point Point;
//Default image container = STLVector
typedef ImageSelector<TDomain, int>::Type Image;
Image image = VolReader<Image>::importVol("data.vol");
trace.info() << image <<endl;
...
Template Parameters:
TImageContainerthe image container to use.
See also:
testVolReader.cpp
Examples:
geometry/surfaces/greedy-plane-segmentation-ex2.cpp, geometry/surfaces/greedy-plane-segmentation.cpp, io/digitalSetFromVol.cpp, io/display3DToOFF.cpp, topology/3dBorderExtractionImg.cpp, topology/ctopo-2-3d.cpp, topology/volScanBoundary.cpp, topology/volToOFF.cpp, and topology/volTrackBoundary.cpp.

Definition at line 88 of file VolReader.h.


Member Typedef Documentation

template<typename TImageContainer >
typedef TImageContainer DGtal::VolReader< TImageContainer >::ImageContainer

Definition at line 92 of file VolReader.h.

template<typename TImageContainer >
typedef unsigned char DGtal::VolReader< TImageContainer >::voxel
private

Definition at line 110 of file VolReader.h.


Member Function Documentation

template<typename TImageContainer >
DGtal::VolReader< TImageContainer >::BOOST_STATIC_ASSERT ( ImageContainer::Domain::dimension  = =3)
template<typename T >
int DGtal::VolReader< T >::getHeaderField ( const char *  type,
const HeaderField header 
)
inlinestaticprivate

Internal method which returns the index of a field or -1 if not found.

Definition at line 246 of file VolReader.ih.

{
for ( int i = 0; i < MAX_HEADERNUMLINES; ++i )
{
if ( header[i].type != NULL && strcmp( header[i].type, type ) == 0 )
{
return i;
}
}
return -1;
}
template<typename T >
const char * DGtal::VolReader< T >::getHeaderValue ( const char *  type,
const HeaderField header 
)
inlinestaticprivate

Returns NULL if this field is not found.

Definition at line 264 of file VolReader.ih.

References DGtal::VolReader< TImageContainer >::HeaderField::value.

{
int i = getHeaderField( type, header );
if ( i == -1 )
return NULL;
return header[i].value;
}
template<typename T >
int DGtal::VolReader< T >::getHeaderValueAsInt ( const char *  type,
int *  dest,
const HeaderField header 
)
inlinestaticprivate

Returns non-zero if failure.

Definition at line 278 of file VolReader.ih.

{
int i = getHeaderField( type, header );
if ( i == -1 )
return 1;
return sscanf( header[i].value, "%d", dest ) != 0;
}
template<typename T >
T DGtal::VolReader< T >::importVol ( const std::string &  filename) throw (DGtal::IOException)
inlinestatic

Main method to import a Vol into an instance of the template parameter ImageContainer.

Parameters:
filenamethe file name to import.
Returns:
an instance of the ImageContainer.

Definition at line 44 of file VolReader.ih.

References DGtal::Trace::error(), DGtal::trace, and DGtal::Trace::warning().

{
FILE * fin;
DGtal::IOException dgtalexception;
typename T::Point firstPoint( 0, 0, 0 );
typename T::Point lastPoint( 0, 0, 0 );
T nullImage( typename T::Domain( firstPoint, lastPoint ));
HeaderField header[ MAX_HEADERNUMLINES ];
#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;
}
// Read header
// Buf for a line
char buf[128];
int linecount = 1;
int fieldcount = 0;
// Read the file line by line until ".\n" is found
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
{
if ( fieldcount == MAX_HEADERNUMLINES )
{
trace.warning() << "VolReader: Too many lines in HEADER, ignoring\n";
continue;
}
if ( fieldcount > MAX_HEADERNUMLINES )
continue;
// Remove \n from end of line
if ( line[ strlen( line ) - 1 ] == '\n' )
line[ strlen( line ) - 1 ] = 0;
// hack : split line in two str ...
line[i] = 0;
header[ fieldcount++ ] = HeaderField( line, line + i + 2 );
// +2 cause we skip the space
// following the colon
}
}
// Check required headers
for ( int i = 0; requiredHeaders[i]; ++i )
{
if ( getHeaderValue( "Version" , header ) != NULL &&
( strcmp( requiredHeaders[i], "Int-Endian" ) == 0 ||
strcmp( requiredHeaders[i], "Voxel-Endian" ) == 0 ) )
{
continue;
}
if ( getHeaderField( requiredHeaders[i] , header ) == -1 )
{
trace.error() << "VolReader: Required Header Field missing: "
<< requiredHeaders[i] << std::endl;
throw dgtalexception;
}
}
int sx, sy, sz;
getHeaderValueAsInt( "X", &sx, header );
getHeaderValueAsInt( "Y", &sy, header );
getHeaderValueAsInt( "Z", &sz, header );
if ( getHeaderValue( "Version", header ) == NULL )
{
int rawsx, rawsy, rawsz;
long int count = 0;
// Size of the volume
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;
if ( getHeaderValueAsInt( "Voxel-Size", &voxsize, header ) == 0 && voxsize != sizeof( voxel ) )
{
trace.error() << "VolReader: This file was generated with a voxel-size that we do not support.\n";
throw dgtalexception;
}
// We should have a useless \n in the file at this point
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;
}
}
//Raw Data
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;
}
}

Field Documentation

template<typename TImageContainer >
const int DGtal::VolReader< TImageContainer >::MAX_HEADERNUMLINES = 64
staticprivate

Maximum number of fields in a .vol file header.

Definition at line 155 of file VolReader.h.

template<typename TImageContainer >
const char * DGtal::VolReader< T >::requiredHeaders
staticprivate
Initial value:
{
"X", "Y", "Z", "Voxel-Size", "Int-Endian", "Voxel-Endian", "Alpha-Color", NULL
}

Global list of required fields in a .vol file.

Definition at line 162 of file VolReader.h.


The documentation for this struct was generated from the following files: