Developer documentation
Version 3.0.3-105-gd3941f44
file_base.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __dwi_tractography_file_base_h__
18#define __dwi_tractography_file_base_h__
19
20#include <iomanip>
21#include <map>
22#include <set>
23
24#include "types.h"
25#include "file/key_value.h"
26#include "file/ofstream.h"
27#include "file/path.h"
29
30
31
32
33namespace MR
34{
35 namespace DWI
36 {
37 namespace Tractography
38 {
39
41 class __ReaderBase__
43 public:
44 __ReaderBase__() : current_index (0) { }
45 ~__ReaderBase__ () {
46 if (in.is_open())
47 in.close();
48 }
49
50 void open (const std::string& file, const std::string& firstline, Properties& properties);
51
52 void close () { in.close(); }
53
54 protected:
55 std::ifstream in;
56 DataType dtype;
57 uint64_t current_index;
58 };
59
60
61 template <typename ValueType = float>
62 class __WriterBase__
64 public:
65 using value_type = ValueType;
66
67 __WriterBase__(const std::string& name) :
68 count (0),
69 total_count (0),
70 name (name),
71 dtype (DataType::from<ValueType>()),
72 count_offset (0),
73 open_success (false)
74 {
75 dtype.set_byte_order_native();
76 if (dtype != DataType::Float32LE && dtype != DataType::Float32BE &&
77 dtype != DataType::Float64LE && dtype != DataType::Float64BE)
78 throw Exception ("only supported datatype for tracks file are "
79 "Float32LE, Float32BE, Float64LE & Float64BE");
81 }
82
83 ~__WriterBase__()
84 {
85 if (open_success) {
86 File::OFStream out (name, std::ios::in | std::ios::out | std::ios::binary);
87 update_counts (out);
88 }
89 }
90
91 void create (File::OFStream& out, const Properties& properties, const std::string& type)
92 {
93 out << "mrtrix " + type + "\nEND\n";
94
95 for (const auto& i : properties) {
96 if ((i.first != "count") && (i.first != "total_count")) {
97 for (const auto& line : split_lines (i.second))
98 out << i.first << ": " << line << "\n";
99 }
100 }
101
102 for (const auto& i : properties.comments)
103 out << "comment: " << i << "\n";
104
105 for (size_t n = 0; n < properties.seeds.num_seeds(); ++n)
106 out << "roi: seed " << properties.seeds[n]->get_name() << "\n";
107 for (size_t n = 0; n < properties.include.size(); ++n)
108 out << "roi: include " << properties.include[n].parameters() << "\n";
109 for (size_t n = 0; n < properties.exclude.size(); ++n)
110 out << "roi: exclude " << properties.exclude[n].parameters() << "\n";
111 for (size_t n = 0; n < properties.mask.size(); ++n)
112 out << "roi: mask " << properties.mask[n].parameters() << "\n";
113
114 for (const auto& it : properties.prior_rois)
115 out << "prior_roi: " << it.first << " " << it.second << "\n";
116
117 out << "datatype: " << dtype.specifier() << "\n";
118 int64_t data_offset = int64_t(out.tellp()) + 65;
119 data_offset += (4 - (data_offset % 4)) % 4;
120 out << "file: . " << data_offset << "\n";
121 out << "count: ";
122 count_offset = out.tellp();
123 out << "0\nEND\n";
124 out.seekp (0);
125 out << "mrtrix " + type + " ";
126 out.seekp (data_offset);
127 }
128
129
130 void skip() { ++total_count; }
131
132
133 uint64_t count, total_count;
134
135
136 protected:
137 std::string name;
138 DataType dtype;
139 int64_t count_offset;
140 bool open_success;
141
142
143 void verify_stream (const File::OFStream& out) {
144 if (!out.good())
145 throw Exception ("error writing file \"" + name + "\": " + strerror (errno));
146 }
147
148 void update_counts (File::OFStream& out) {
149 out.seekp (count_offset);
150 out << count << "\ntotal_count: " << total_count << "\nEND\n";
151 verify_stream (out);
152 }
153 };
154
155
156
158
159
160 }
161 }
162}
163
164
165#endif
static constexpr uint8_t Float64LE
Definition: datatype.h:169
static constexpr uint8_t Float32BE
Definition: datatype.h:168
static constexpr uint8_t Float32LE
Definition: datatype.h:167
static constexpr uint8_t Float64BE
Definition: datatype.h:170
#define NOMEMALIGN
Definition: memory.h:22
void check_overwrite(const std::string &name)
Definition: app.h:168
std::unique_ptr< ImageIO::Base > create(Header &H)
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
vector< std::string > split_lines(const std::string &string, bool ignore_empty_fields=true, size_t num=std::numeric_limits< size_t >::max())
Definition: mrtrix.h:179
const std::string name
Definition: thread.h:108