8 #include <gdcmImageReader.h> 11 if( gimage.IsEmpty( ) ) {
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!" ) );
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++ );
34 else if( gimage.GetPhotometricInterpretation( ) == gdcm::PhotometricInterpretation::MONOCHROME2 ) {
37 if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::UINT8 ) {
39 unsigned
char *ubuffer = (
unsigned char* ) buffer;
40 for(
size_t pxl = 0; pxl < res.
size( ); ++pxl ) {
41 res[ pxl ] =
static_cast< int >( *ubuffer++ );
44 else if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::INT16 ) {
46 short *buffer16 = (
short* ) buffer;
47 for(
size_t pxl = 0; pxl < res.size( ); ++pxl ) {
48 res[ pxl ] =
static_cast< int >( *buffer16++ );
51 else if( gimage.GetPixelFormat( ) == gdcm::PixelFormat::UINT16 ) {
53 unsigned short *buffer16 = (
unsigned short* ) buffer;
54 for(
size_t pxl = 0; pxl < res.size( ); ++pxl ) {
55 res[ pxl ] =
static_cast< int >( *buffer16++ );
60 <<
". Format is: " << gimage.GetPixelFormat( ) <<
". Pixel Format is: " << gimage.GetPixelFormat( )
62 throw std::runtime_error(
BIAL_ERROR(
"Unhandled Pixel Format!" ) );
67 BIAL_WARNING(
"Unhandled PhotometricInterpretation" << gimage.GetPhotometricInterpretation( ) )
68 throw std::runtime_error(
BIAL_ERROR(
"Unhandled PhotometricInterpretation" ) );
74 ir.SetFileName( filename.c_str( ) );
76 return( Bial::Read< int >( filename ) );
78 catch( std::bad_alloc e ) {
81 catch( std::runtime_error e ) {
84 catch( std::out_of_range e ) {
87 catch( std::logic_error e ) {
91 BIAL_WARNING(
"Could not read " << filename <<
" with gdcm." );
92 throw std::runtime_error(
BIAL_ERROR(
"GDCM CAN'T READ THIS IMAGE" ) );
94 COMMENT(
"Getting image from GDCM ImageReader.", 1 );
95 gdcm::Image gimage = ir.GetImage( );
96 if( gimage.IsEmpty( ) ) {
98 throw std::runtime_error(
BIAL_ERROR(
"GDCM CAN'T READ THIS IMAGE" ) );
100 if( gimage.GetNumberOfDimensions( ) == 2 ) {
101 return( Convert2D( gimage ) );
104 BIAL_WARNING(
"3D image conversion not implemented yet." );
105 throw std::runtime_error(
BIAL_ERROR(
"3D image conversion not implemented yet." ) );
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 ) ) {
129 std::string msg(
BIAL_ERROR(
"Could not open Nifti image. Unsupported data type." ) );
130 throw( std::runtime_error( msg ) );
133 if( ( filename.rfind(
".ppm" ) != std::string::npos ) || ( filename.rfind(
".pnm" ) != std::string::npos ) ) {
#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.
static Bial::MultiImage OpenGImage(const std::string &filename)
Content: Commonly used macros, types, static variables and functions. Description: Some macros used...
The MultiImage class may have only one of the following image types: int, float, Color, or RealColor.
#define BIAL_WARNING(exp)
Use BIAL_WARNING to print a message to the output stream warn the user that something bad may have ha...