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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "formats/mrtrix_utils.h"
namespace MR
{
namespace Formats
{
vector<ssize_t> parse_axes (size_t ndim, const std::string& specifier)
{
vector<ssize_t> parsed (ndim);
size_t sub = 0;
size_t lim = 0;
size_t end = specifier.size();
size_t current = 0;
try {
while (sub <= end) {
bool pos = true;
if (specifier[sub] == '+') {
pos = true;
sub++;
}
else if (specifier[sub] == '-') {
pos = false;
sub++;
}
else if (!isdigit (specifier[sub])) throw 0;
lim = sub;
while (isdigit (specifier[lim])) lim++;
if (specifier[lim] != ',' && specifier[lim] != '\0') throw 0;
parsed[current] = to<ssize_t> (specifier.substr (sub, lim-sub)) + 1;
if (!pos) parsed[current] = -parsed[current];
sub = lim+1;
current++;
}
}
catch (int) {
throw Exception ("malformed axes specification \"" + specifier + "\"");
}
if (current != ndim)
throw Exception ("incorrect number of axes in axes specification \"" + specifier + "\"");
if (parsed.size() != ndim)
throw Exception ("incorrect number of dimensions for axes specifier");
for (size_t n = 0; n < parsed.size(); n++) {
if (!parsed[n] || size_t (abs (parsed[n])) > ndim)
throw Exception ("axis ordering " + str (parsed[n]) + " out of range");
for (size_t i = 0; i < n; i++)
if (abs (parsed[i]) == abs (parsed[n]))
throw Exception ("duplicate axis ordering (" + str (abs (parsed[n])) + ")");
}
return parsed;
}
bool next_keyvalue (File::KeyValue::Reader& kv, std::string& key, std::string& value)
{
key.clear(); value.clear();
if (!kv.next())
return false;
key = kv.key();
value = kv.value();
return true;
}
bool next_keyvalue (File::GZ& gz, std::string& key, std::string& value)
{
key.clear(); value.clear();
std::string line = gz.getline();
line = strip (line.substr (0, line.find_first_of ('#')));
if (line.empty() || line == "END")
return false;
size_t colon = line.find_first_of (':');
if (colon == std::string::npos) {
INFO ("malformed key/value entry (\"" + line + "\") in file \"" + gz.name() + "\" - ignored");
} else {
key = strip (line.substr (0, colon));
value = strip (line.substr (colon+1));
if (key.empty() || value.empty()) {
INFO ("malformed key/value entry (\"" + line + "\") in file \"" + gz.name() + "\" - ignored");
key.clear();
value.clear();
}
}
return true;
}
void get_mrtrix_file_path (Header& H, const std::string& flag, std::string& fname, size_t& offset)
{
auto i = H.keyval().find (flag);
if (i == H.keyval().end())
throw Exception ("missing \"" + flag + "\" specification for MRtrix image \"" + H.name() + "\"");
const std::string path = i->second;
H.keyval().erase (i);
std::istringstream file_stream (path);
file_stream >> fname;
offset = 0;
if (file_stream.good()) {
try {
file_stream >> offset;
}
catch (...) {
throw Exception ("invalid offset specified for file \"" + fname
+ "\" in MRtrix image header \"" + H.name() + "\"");
}
}
if (fname == ".") {
if (offset == 0)
throw Exception ("invalid offset specified for embedded MRtrix image \"" + H.name() + "\"");
fname = H.name();
} else {
if (fname[0] != PATH_SEPARATORS[0]
#ifdef MRTRIX_WINDOWS
&& fname[0] != PATH_SEPARATORS[1]
#endif
)
fname = Path::join (Path::dirname (H.name()), fname);
}
}
}
}
|