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

#include <PNMReader.h>

Public Types

enum  MagicNumber {
  P1, P2, P3, P4,
  P5, P6
}
typedef TImageContainer ImageContainer
typedef
TImageContainer::Domain::Vector 
Vector

Public Member Functions

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

Static Public Member Functions

static ImageContainer importPGM (const std::string &aFilename, bool topbotomOrder=true) throw (DGtal::IOException)
static ImageContainer importPGM3D (const std::string &aFilename) throw (DGtal::IOException)

Detailed Description

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

Aim: Import a 2D or 3D using the Netpbm formats (ASCII mode).

Description of class 'PNMReader'

Examples:
topology/ctopo-2.cpp.

Definition at line 90 of file PNMReader.h.


Member Typedef Documentation

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

Definition at line 95 of file PNMReader.h.

template<typename TImageContainer >
typedef TImageContainer::Domain::Vector DGtal::PNMReader< TImageContainer >::Vector

Definition at line 96 of file PNMReader.h.


Member Enumeration Documentation

template<typename TImageContainer >
enum DGtal::PNMReader::MagicNumber
Enumerator:
P1 
P2 
P3 
P4 
P5 
P6 

Definition at line 98 of file PNMReader.h.

{P1,P2,P3,P4,P5,P6};

Member Function Documentation

template<typename TImageContainer >
DGtal::PNMReader< TImageContainer >::BOOST_STATIC_ASSERT ( (ImageContainer::Domain::dimension==2)||(ImageContainer::Domain::dimension==3)  )
template<typename TImageContainer >
TImageContainer DGtal::PNMReader< TImageContainer >::importPGM ( const std::string &  aFilename,
bool  topbotomOrder = true 
) throw (DGtal::IOException)
inlinestatic

Main method to import a Pgm (8bits) into an instance of the template parameter ImageContainer.

Parameters:
filenamethe file name to import.
topbotomOrderif true, the point of coordinate (0,0) will be the bottom left corner image point (default) else the center of image coordinate will be the top left of the image (not usual).
Returns:
an instance of the ImageContainer.
Examples:
tutorial-examples/freemanChainFromImage.cpp.

Definition at line 52 of file PNMReader.ih.

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

{
ifstream infile;
BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 2));
try
{
infile.open (aFilename.c_str(), ifstream::in);
}
catch( ... )
{
trace.error() << "PNMReader : can't open " << aFilename << endl;
throw dgtalio;
}
bool isASCIImode = false;
string str;
getline( infile, str );
if ( ! infile.good() )
{
trace.error() << "PNMReader : can't read " << aFilename << endl;
throw dgtalio;
}
if ( str != "P5" && str != "P2")
{
trace.error() << "PNMReader : No P5 or P2 format in " << aFilename << endl;
throw dgtalio;
}
if(str== "P2")
{
isASCIImode=true;
}
do
{
getline( infile, str );
if ( ! infile.good() )
{
trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
}
while ( str[ 0 ] == '#' || str=="");
istringstream str_in( str );
unsigned int w, h;
str_in >> w >> h;
typename TImageContainer::Point firstPoint;
typename TImageContainer::Point lastPoint;
firstPoint = TImageContainer::Point::zero;
lastPoint[0] = w-1;
lastPoint[1] = h-1;
typename TImageContainer::Domain domain(firstPoint,lastPoint);
TImageContainer image(domain);
getline( infile, str );
istringstream str2_in( str );
unsigned int max_value;
str2_in >> max_value;
if ( ! infile.good() )
{
trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
unsigned int nb_read = 0;
if(!isASCIImode)
infile >> noskipws;
else
infile >> skipws;
for(unsigned int y=0; y <h; y++)
for(unsigned int x=0; x <w; x++)
{
typename TImageContainer::Point pt;
if (topbotomOrder){
pt[0]=x; pt[1]=h-1-y;
}else{
pt[0]=x; pt[1]=y;
}
if(!isASCIImode)
{
unsigned char c;
infile >> c;
if ( infile.good() )
{
++nb_read;
image.setValue( pt, c);
}
}
else
{
int c;
infile >> c;
if ( infile.good() )
{
++nb_read;
image.setValue( pt, c);
}
}
}
if ( infile.fail() || infile.bad() )
{
trace.error() << "# nbread=" << nb_read << endl;
throw dgtalio;
}
infile >> skipws;
return image;
}
template<typename TImageContainer >
TImageContainer DGtal::PNMReader< TImageContainer >::importPGM3D ( const std::string &  aFilename) throw (DGtal::IOException)
inlinestatic

Main method to import a Pgm3D (8bits) into an instance of the template parameter ImageContainer.

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

Definition at line 171 of file PNMReader.ih.

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

{
ifstream infile;
BOOST_STATIC_ASSERT( (ImageContainer::Domain::dimension == 3));
try
{
infile.open (aFilename.c_str(), ifstream::in);
}
catch( ... )
{
trace.error() << "PNMReader : can't open " << aFilename << endl;
throw dgtalio;
}
string str;
getline( infile, str );
if ( ! infile.good() ) {
trace.error() << "PNMReader : can't read " << aFilename << endl;
throw dgtalio;
}
if ( str != "P3d" && str != "P3D"){
trace.error() << "PNMReader : No P3d format in " << aFilename << endl;
throw dgtalio;
}
do
{
getline( infile, str );
if ( ! infile.good() ){
trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
}
while ( str[ 0 ] == '#' || str=="");
istringstream str_in( str );
unsigned int w, h, e;
str_in >> w >> h >> e ;
typename TImageContainer::Point firstPoint;
typename TImageContainer::Point lastPoint;
firstPoint = TImageContainer::Point::zero;
lastPoint[0] = w-1;
lastPoint[1] = h-1;
lastPoint[2] = e-1;
typename TImageContainer::Domain domain(firstPoint,lastPoint);
TImageContainer image(domain);
getline( infile, str );
istringstream str2_in( str );
int max_value;
str2_in >> max_value;
if ( ! infile.good() ){
trace.error() << "PNMReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
unsigned int nb_read = 0;
infile >> noskipws;
for(unsigned int z=0; z <e; z++){
for(unsigned int y=0; y <h; y++){
for(unsigned int x=0; x <w; x++){
typename TImageContainer::Point pt;
pt[0]=x; pt[1]=y; pt[2]=z;
char c;
infile >> c;
if ( infile.good() ){
++nb_read;
image.setValue( pt, c);
}
}
}
}
if ( infile.fail() || infile.bad() )
{
trace.error() << "# nbread=" << nb_read << endl;
throw dgtalio;
}
infile >> skipws;
return image;
}

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