17#ifndef __formats_mrtrix_utils_h__
18#define __formats_mrtrix_utils_h__
38 template <
class SourceType>
54 template <
class StreamType>
63 template <
class SourceType>
66 std::string dtype, layout;
71 std::string key,
value;
74 if (lkey ==
"dim") dim = parse_ints<uint64_t> (
value);
76 else if (lkey ==
"layout") layout =
value;
77 else if (lkey ==
"datatype") dtype =
value;
79 else if (lkey ==
"transform")
81 else if (key.size() &&
value.size())
86 throw Exception (
"missing \"dim\" specification for MRtrix image \"" +
H.name() +
"\"");
87 H.ndim() = dim.size();
88 for (
size_t n = 0; n < dim.size(); n++) {
90 throw Exception (
"invalid dimensions for MRtrix image \"" +
H.name() +
"\"");
95 throw Exception (
"missing \"vox\" specification for MRtrix image \"" +
H.name() +
"\"");
96 if (vox.size() < std::min (
size_t(3), dim.size()))
97 throw Exception (
"too few entries in \"vox\" specification for MRtrix image \"" +
H.name() +
"\"");
98 for (
size_t n = 0; n < std::min<size_t> (vox.size(),
H.ndim()); n++) {
100 throw Exception (
"invalid voxel size for MRtrix image \"" +
H.name() +
"\"");
101 H.spacing(n) = vox[n];
106 throw Exception (
"missing \"datatype\" specification for MRtrix image \"" +
H.name() +
"\"");
111 throw Exception (
"missing \"layout\" specification for MRtrix image \"" +
H.name() +
"\"");
113 for (
size_t i = 0; i < ax.size(); ++i)
116 if (transform.size()) {
118 auto check_transform = [&transform]() {
119 if (transform.size() < 3)
return false;
120 for (
auto row : transform)
125 if (!check_transform())
126 throw Exception (
"invalid \"transform\" specification for MRtrix image \"" +
H.name() +
"\"");
128 for (
int row = 0; row < 3; ++row)
129 for (
int col = 0; col < 4; ++col)
130 H.transform() (row,col) = transform[row][col];
134 if (scaling.size()) {
135 if (scaling.size() != 2)
136 throw Exception (
"invalid \"scaling\" specification for MRtrix image \"" +
H.name() +
"\"");
137 H.set_intensity_scaling (scaling[1], scaling[0]);
148 template <
class StreamType>
151 out <<
"dim: " <<
H.size (0);
152 for (
size_t n = 1; n <
H.ndim(); ++n)
153 out <<
"," <<
H.size (n);
155 out <<
"\nvox: " <<
H.spacing (0);
156 for (
size_t n = 1; n <
H.ndim(); ++n)
157 out <<
"," <<
H.spacing (n);
162 out <<
"\nlayout: " << (stride[0] >0 ?
"+" :
"-") <<
abs (stride[0])-1;
163 for (
size_t n = 1; n <
H.ndim(); ++n)
164 out <<
"," << (stride[n] >0 ?
"+" :
"-") <<
abs (stride[n])-1;
170 Eigen::IOFormat fmt(Eigen::FullPrecision, Eigen::DontAlignCols,
", ",
"\ntransform: ",
"",
"",
"\ntransform: ",
"");
171 out <<
H.transform().matrix().topLeftCorner(3,4).format(fmt);
173 if (
H.intensity_offset() != 0.0 ||
H.intensity_scale() != 1.0)
174 out <<
"\nscaling: " <<
H.intensity_offset() <<
"," <<
H.intensity_scale();
176 for (
const auto& i :
H.keyval())
178 out <<
"\n" << i.first <<
": " << line;
static DataType parse(const std::string &spec)
const char * specifier() const
void set_byte_order_native()
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
void symbolise(HeaderType &header)
convert strides from actual to symbolic strides
List get(const HeaderType &header)
return the strides of header as a vector<ssize_t>
vector< default_type > parse_floats(const std::string &spec)
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)
std::string lowercase(const std::string &string)
return lowercase version of string
vector< std::string > split_lines(const std::string &string, bool ignore_empty_fields=true, size_t num=std::numeric_limits< size_t >::max())
std::string & add_line(std::string &original, const std::string &new_line)
add a line to a string, taking care of inserting a newline if needed