7 #include "AnalyzeHeader.h" 16 #ifndef DOXYGEN_SHOULD_SKIP_THIS 17 template <
typename T>
inline short type_Analyze() {
return DT_UNKNOWN; }
18 template <>
inline short type_Analyze<unsigned char>() {
return DT_UNSIGNED_CHAR; }
19 template <>
inline short type_Analyze<short>() {
return DT_SIGNED_SHORT; }
20 template <>
inline short type_Analyze<int>() {
return DT_SIGNED_INT; }
21 template <>
inline short type_Analyze<float>() {
return DT_FLOAT; }
22 template <>
inline short type_Analyze<double>() {
return DT_DOUBLE; }
35 template <
typename T,
int dim>
39 std::string noextension(name);
40 std::string::size_type size = noextension.rfind(
'.');
41 if (size != std::string::npos) noextension.resize(size);
44 std::string dataname = noextension +
".img";
45 std::string headername = noextension +
".hdr";
46 std::ifstream headerfile(headername.c_str(),std::ios::binary);
47 if (!headerfile.is_open()) {
48 std::cerr <<
"Unable to open '" << headername <<
"'" << std::endl;
51 std::ifstream datafile(dataname.c_str(),std::ios::binary);
52 if (!datafile.is_open()) {
53 std::cerr <<
"Unable to open '" << dataname <<
"'" << std::endl;
59 headerfile.read((
char *)(&header),
sizeof(Analyze::dsr));
63 int ndim = header.dime.dim[0];
65 std::cerr <<
"Dimension mismatch" << std::endl;
69 for (
int i=0;i<dim;i++) dm[i] = header.dime.dim[i+1];
73 switch (header.dime.datatype) {
74 case DT_UNSIGNED_CHAR :
return readBuffer<unsigned char,T,dim>(I,datafile);
75 case DT_SIGNED_SHORT :
return readBuffer<short int,T,dim>(I,datafile);
76 case DT_SIGNED_INT :
return readBuffer<int,T,dim>(I,datafile);
77 case DT_FLOAT :
return readBuffer<float,T,dim>(I,datafile);
78 case DT_DOUBLE :
return readBuffer<double,T,dim>(I,datafile);
81 std::cerr <<
"Unkwnown data type" << std::endl;
98 template <
typename TO,
typename TI,
int dim>
100 short type = type_Analyze<TO>();
101 if (type == DT_UNKNOWN) {
102 std::cerr <<
"Data not handled by the Analyze format" << std::endl;
107 std::string noextension(name);
108 std::string::size_type size = noextension.rfind(
'.');
109 if (size != std::string::npos) noextension.resize(size);
112 std::string dataname = noextension +
".img";
113 std::string headername = noextension +
".hdr";
114 std::ofstream headerfile(headername.c_str(),std::ios::binary);
115 if (!headerfile.is_open()) {
116 std::cerr <<
"Unable to open '" << headername <<
"'" << std::endl;
119 std::ofstream datafile(dataname.c_str(),std::ios::binary);
120 if (!datafile.is_open()) {
121 std::cerr <<
"Unable to open '" << dataname <<
"'" << std::endl;
127 memset(&header,0,
sizeof(Analyze::dsr));
128 header.hk.sizeof_hdr =
sizeof(Analyze::dsr);
129 header.hk.regular =
'r';
130 header.dime.dim[0] = dim;
131 for (
int i=0;i<dim;i++) header.dime.dim[i+1] = I.
size(i);
132 header.dime.datatype = type;
133 header.dime.pixdim[1] = 1.f;
134 header.dime.pixdim[2] = 1.f;
135 header.dime.pixdim[3] = 1.f;
136 headerfile.write((
const char *)(&header),
sizeof(Analyze::dsr));
140 return writeBuffer<TO,TI,dim>(I,datafile);
Coordinates.
Definition: Coords.h:16
void setSize(const Coords< dim > &sz)
Change sizes.
Definition: MultiArray.h:146
int size(int i) const
ith size.
Definition: MultiArray.h:225
Image.
Definition: Image.h:24
bool loadAnalyze(Image< T, dim > &I, const std::string name)
Load Analyze file.
Definition: Analyze.h:36
bool saveAnalyze(const Image< TI, dim > &I, const std::string name)
Save Analyze file.
Definition: Analyze.h:99
Imagine++ namespace.
Definition: Array.h:7