Biomedical Image Analysis Library
The Biomedical Image Analysis Library is a poweful tool for developers, physicians, researchers, engineers, and so on.
gdcm.cpp
Go to the documentation of this file.
1 #include "Common.hpp"
2 #include "FileImage.hpp"
3 #include "NiftiHeader.hpp"
4 #include "gdcm.h"
5 #include "guiimage.h"
6 
7 #ifdef LIBGDCM
8 #include <gdcmImageReader.h>
9 
10 Bial::Image< int > Convert2D( const gdcm::Image &gimage ) {
11  if( gimage.IsEmpty( ) ) {
12  BIAL_WARNING( "GDCM GImage is empty!" )
13  throw std::runtime_error( BIAL_ERROR( "GDCM GImage is empty!" ) );
14  }
15  std::vector< char > vbuffer( gimage.GetBufferLength( ) );
16  char *buffer = &vbuffer[ 0 ];
17  gimage.GetBuffer( buffer );
18  const std::vector< size_t > spc_dim = { gimage.GetDimension( 0 ), gimage.GetDimension( 1 ) };
19  const std::vector< float > pixel_size = { static_cast< float >( gimage.GetSpacing( )[ 0 ] ),
20  static_cast< float >( gimage.GetSpacing( )[ 1 ] ) };
21  if( gimage.GetPhotometricInterpretation( ) == gdcm::PhotometricInterpretation::RGB ) {
22  if( gimage.GetPixelFormat( ) != gdcm::PixelFormat::UINT8 ) {
23  throw std::runtime_error( BIAL_ERROR( "Unhandled pixel format!" ) );
24  }
25  Bial::Image< int > res( spc_dim, pixel_size, 3 );
26  const size_t channelSz = res.size( 0 ) * res.size( 1 );
27  for( size_t pxl = 0; pxl < channelSz; ++pxl ) {
28  res[ pxl ] = static_cast< int >( *buffer++ );
29  res[ pxl + channelSz ] = static_cast< int >( *buffer++ );
30  res[ pxl + channelSz * 2 ] = static_cast< int >( *buffer++ );
31  }
32  return( res );
33  }
34  else if( gimage.GetPhotometricInterpretation( ) == gdcm::PhotometricInterpretation::MONOCHROME2 ) {
35  Bial::Image< int > res( spc_dim, pixel_size );
36  COMMENT( "MONO2 FORMAT", 1 );
37  if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::UINT8 ) {
38  COMMENT( "UINT8", 1 )
39  unsigned char *ubuffer = ( unsigned char* ) buffer;
40  for( size_t pxl = 0; pxl < res.size( ); ++pxl ) {
41  res[ pxl ] = static_cast< int >( *ubuffer++ );
42  }
43  }
44  else if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::INT16 ) {
45  COMMENT( "INT16", 1 );
46  short *buffer16 = ( short* ) buffer;
47  for( size_t pxl = 0; pxl < res.size( ); ++pxl ) {
48  res[ pxl ] = static_cast< int >( *buffer16++ );
49  }
50  }
51  else if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::UINT16 ) {
52  COMMENT( "UINT16", 1 );
53  unsigned short *buffer16 = ( unsigned short* ) buffer;
54  for( size_t pxl = 0; pxl < res.size( ); ++pxl ) {
55  res[ pxl ] = static_cast< int >( *buffer16++ );
56  }
57  }
58  else {
59  BIAL_WARNING( "Unhandled Pixel Format!"
60  << ". Format is: " << gimage.GetPixelFormat( ) << ". Pixel Format is: " << gimage.GetPixelFormat( )
61  << std::endl );
62  throw std::runtime_error( BIAL_ERROR( "Unhandled Pixel Format!" ) );
63  }
64  return( res );
65  }
66  else {
67  BIAL_WARNING( "Unhandled PhotometricInterpretation" << gimage.GetPhotometricInterpretation( ) )
68  throw std::runtime_error( BIAL_ERROR( "Unhandled PhotometricInterpretation" ) );
69  }
70 }
71 
72 Bial::Image< int > GDCM::OpenGImage( const std::string &filename ) {
73  gdcm::ImageReader ir;
74  ir.SetFileName( filename.c_str( ) );
75  try {
76  return( Bial::Read< int >( filename ) );
77  }
78  catch( std::bad_alloc e ) {
79  BIAL_WARNING( e.what( ) );
80  }
81  catch( std::runtime_error e ) {
82  BIAL_WARNING( e.what( ) );
83  }
84  catch( std::out_of_range e ) {
85  BIAL_WARNING( e.what( ) );
86  }
87  catch( std::logic_error e ) {
88  BIAL_WARNING( e.what( ) );
89  }
90  if( !ir.Read( ) ) {
91  BIAL_WARNING( "Could not read " << filename << " with gdcm." );
92  throw std::runtime_error( BIAL_ERROR( "GDCM CAN'T READ THIS IMAGE" ) );
93  }
94  COMMENT( "Getting image from GDCM ImageReader.", 1 );
95  gdcm::Image gimage = ir.GetImage( );
96  if( gimage.IsEmpty( ) ) {
97  BIAL_WARNING( "GDCM GImage is empty!" );
98  throw std::runtime_error( BIAL_ERROR( "GDCM CAN'T READ THIS IMAGE" ) );
99  }
100  if( gimage.GetNumberOfDimensions( ) == 2 ) {
101  return( Convert2D( gimage ) );
102  }
103  else {
104  BIAL_WARNING( "3D image conversion not implemented yet." );
105  throw std::runtime_error( BIAL_ERROR( "3D image conversion not implemented yet." ) );
106  }
107  return( Bial::Image< int >( ) );
108 }
109 
110 #else
111 
112 Bial::MultiImage GDCM::OpenGImage( const std::string &filename ) {
113  std::string extension( Bial::File::ToLowerExtension( filename ) );
114  if( ( filename.rfind( ".nii" ) != std::string::npos ) || ( filename.rfind( ".img" ) != std::string::npos ) ||
115  ( filename.rfind( ".hdr" ) != std::string::npos ) ) {
116  Bial::NiftiHeader hdr( filename );
117  Bial::NiftiType type( hdr.DataType( ) );
118  switch( type ) {
125  return( Bial::MultiImage( Bial::Read< int >( filename ) ) );
127  return( Bial::MultiImage( Bial::Read< float >( filename ) ) );
128  default:
129  std::string msg( BIAL_ERROR( "Could not open Nifti image. Unsupported data type." ) );
130  throw( std::runtime_error( msg ) );
131  }
132  }
133  if( ( filename.rfind( ".ppm" ) != std::string::npos ) || ( filename.rfind( ".pnm" ) != std::string::npos ) ) {
134  return( Bial::MultiImage( Bial::Read< Bial::Color >( filename ) ) );
135  }
136  return( Bial::MultiImage( Bial::Read< int >( filename ) ) );
137 }
138 
139 #endif
Definition: gdcm.h:24
#define BIAL_ERROR(exp)
Use BIAL_ERROR to compose runtime error messages. Note that ERROR generates a string, instead of a stream, because it is better to throw an exception, rather than exiting the program. This way, the exception may be threated by a higher level program, like an user IDE.
Definition: Common.hpp:161
static Bial::MultiImage OpenGImage(const std::string &filename)
Definition: gdcm.cpp:112
Content: Commonly used macros, types, static variables and functions. Description: Some macros used...
static size_t size
Definition: enough.c:173
NiftiType DataType() const
Returns data type code.
Content: NiftiHeader class Description: Magnetic Resonance Image class. Future add-on&#39;s: Complete...
The MultiImage class may have only one of the following image types: int, float, Color, or RealColor.
Definition: MultiImage.hpp:34
#define const
Definition: zconf.h:217
#define BIAL_WARNING(exp)
Use BIAL_WARNING to print a message to the output stream warn the user that something bad may have ha...
Definition: Common.hpp:158
Magnetic Resonance Image class.