[5d12a0]: / src / antsImageHeaderInfo.cxx

Download this file

121 lines (102 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/unordered_map.h>
#include <nanobind/stl/list.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/vector.h>
#include <exception>
#include <vector>
#include <string>
#include "itkImage.h"
#include "itkImageFileReader.h"
namespace nb = nanobind;
using namespace nb::literals;
nb::dict antsImageHeaderInfo( std::string fname )
{
itk::ImageIOBase::Pointer imageIO =
itk::ImageIOFactory::CreateImageIO(
fname.c_str(), itk::ImageIOFactory::ReadMode);
if ( !imageIO )
{
throw std::runtime_error("Could not create ImageIO object for file " + fname);
}
imageIO->SetFileName(fname);
imageIO->ReadImageInformation();
const size_t numDimensions = imageIO->GetNumberOfDimensions();
const size_t numComponents = imageIO->GetNumberOfComponents();
const std::string pixelClass( imageIO->GetPixelTypeAsString(imageIO->GetPixelType()) );
itk::ImageIOBase::IOComponentEnum pixelCode = imageIO->GetComponentType();
std::vector<float> dimensions( numDimensions );
std::vector<float> spacing( numDimensions );
std::vector<float> origin( numDimensions );
std::vector<std::vector<float> > direction( numDimensions, std::vector<float>(numDimensions) );
for (unsigned int i=0; i<numDimensions; i++)
{
dimensions[i] = imageIO->GetDimensions(i);
spacing[i] = imageIO->GetSpacing(i);
origin[i] = imageIO->GetOrigin(i);
for (unsigned int j=0; j<numDimensions; j++)
{
direction[i][j] = imageIO->GetDirection(i)[j];
}
}
std::string pixeltype = "unknown";
switch( pixelCode )
{
case itk::ImageIOBase::IOComponentType::UNKNOWNCOMPONENTTYPE: // UNKNOWNCOMPONENTTYPE - exception here?
pixeltype = "unknown";
break;
case itk::ImageIOBase::IOComponentType::UCHAR: // UCHAR
pixeltype = "unsigned char";
break;
case itk::ImageIOBase::IOComponentType::CHAR: // CHAR
pixeltype = "char";
break;
case itk::ImageIOBase::IOComponentType::USHORT: // USHORT
pixeltype = "unsigned short";
break;
case itk::ImageIOBase::IOComponentType::SHORT: // SHORT
pixeltype = "short";
break;
case itk::ImageIOBase::IOComponentType::UINT: // UINT
pixeltype = "unsigned int";
break;
case itk::ImageIOBase::IOComponentType::INT: // INT
pixeltype = "int";
break;
case itk::ImageIOBase::IOComponentType::ULONG: // ULONG
pixeltype = "unsigned long";
break;
case itk::ImageIOBase::IOComponentType::LONG: // LONG
pixeltype = "long";
break;
case itk::ImageIOBase::IOComponentType::ULONGLONG: // LONGLONG
pixeltype = "ulonglong";
break;
case itk::ImageIOBase::IOComponentType::LONGLONG: // LONGLONG
pixeltype = "longlong";
break;
case itk::ImageIOBase::IOComponentType::FLOAT: // FLOAT
pixeltype = "float";
break;
case itk::ImageIOBase::IOComponentType::DOUBLE: // DOUBLE
pixeltype = "double";
break;
default:
pixeltype = "invalid";
}
nb::dict info;
info["pixelclass"] = pixelClass;
info["pixeltype"] = pixeltype;
info["nDimensions"] = numDimensions;
info["nComponents"] = numComponents;
info["dimensions"] = dimensions;
info["spacing"] = spacing;
info["origin"] = origin;
info["direction"] = direction;
return info;
}
void local_antsImageHeaderInfo(nb::module_ &m)
{
m.def("antsImageHeaderInfo", &antsImageHeaderInfo);
}