17#ifndef __dwi_tractography_mapping_buffer_scratch_dump_h__
18#define __dwi_tractography_mapping_buffer_scratch_dump_h__
32 namespace Tractography {
36 template <
typename value_type>
41 template <
class Template>
43 Image::BufferScratch<value_type> (info) { }
45 template <
class Template>
47 Image::BufferScratch<value_type> (info, label) { }
49 void dump_to_file (
const std::string&,
const Image::Header&)
const;
53 inline const char* get_data_ptr()
const {
return reinterpret_cast<const char*
> (Image::BufferScratch<value_type>::address(0)); }
60 return reinterpret_cast<const char*
> (Image::BufferScratch<bool>::address());
65 template <
typename value_type>
66 void BufferScratchDump<value_type>::dump_to_file (
const std::string& path,
const Header&
H)
const
70 throw Exception (
"Can only perform direct dump to file for .mih / .mif files");
76 dat_path =
Path::basename (path.substr (0, path.size()-4) +
".dat");
77 const int64_t dat_size = Image::footprint (*
this);
79 File::OFStream out_header (path, std::ios::out | std::ios::binary);
81 out_header <<
"mrtrix image\n";
82 out_header <<
"dim: " <<
H.dim (0);
83 for (
size_t n = 1; n <
H.ndim(); ++n)
84 out_header <<
"," <<
H.dim (n);
86 out_header <<
"\nvox: " <<
H.vox (0);
87 for (
size_t n = 1; n <
H.ndim(); ++n)
88 out_header <<
"," <<
H.vox (n);
93 out_header <<
"\nlayout: " << (stride[0] >0 ?
"+" :
"-") <<
abs (stride[0])-1;
94 for (
size_t n = 1; n <
H.ndim(); ++n)
95 out_header <<
"," << (stride[n] >0 ?
"+" :
"-") <<
abs (stride[n])-1;
97 out_header <<
"\ndatatype: " <<
H.datatype().specifier();
99 for (
const auto& i :
H.keyval())
100 out_header <<
"\n" << i.first <<
": " << i.second;
103 out_header <<
"\ncomments: " << *i;
106 if (
H.transform().is_set()) {
107 out_header <<
"\ntransform: " <<
H.transform() (0,0) <<
"," <<
H.transform() (0,1) <<
"," <<
H.transform() (0,2) <<
"," <<
H.transform() (0,3);
108 out_header <<
"\ntransform: " <<
H.transform() (1,0) <<
"," <<
H.transform() (1,1) <<
"," <<
H.transform() (1,2) <<
"," <<
H.transform() (1,3);
109 out_header <<
"\ntransform: " <<
H.transform() (2,0) <<
"," <<
H.transform() (2,1) <<
"," <<
H.transform() (2,2) <<
"," <<
H.transform() (2,3);
112 if (
H.intensity_offset() != 0.0 ||
H.intensity_scale() != 1.0)
113 out_header <<
"\nscaling: " <<
H.intensity_offset() <<
"," <<
H.intensity_scale();
115 if (
H.DW_scheme().is_set()) {
116 for (
size_t i = 0; i <
H.DW_scheme().rows(); i++)
117 out_header <<
"\ndw_scheme: " <<
H.DW_scheme() (i,0) <<
"," <<
H.DW_scheme() (i,1) <<
"," <<
H.DW_scheme() (i,2) <<
"," <<
H.DW_scheme() (i,3);
120 out_header <<
"\nfile: ";
123 offset = out_header.tellp() + int64_t(18);
125 out_header <<
". " <<
offset <<
"\nEND\n";
127 out_header << dat_path <<
"\n";
131 File::OFStream out_dat;
134 out_dat.open (path, std::ios_base::out | std::ios_base::binary | std::ios_base::app);
136 out_dat.open (dat_path, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
139 const char* data_ptr = get_data_ptr();
140 out_dat.write (data_ptr, dat_size);
open output files for writing, checking for pre-existing file if necessary
functions and classes related to image data input/output
void resize(const std::string &filename, int64_t size)
std::string basename(const std::string &name)
bool has_suffix(const std::string &name, const std::string &suffix)
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>
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)