Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cpp/core/dwi/gradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ void save_bvecs_bvals(const Header &, const std::string &, const std::string &);

namespace {
template <class MatrixType> std::string scheme2str(const MatrixType &G) {
std::string dw_scheme;
std::ostringstream dw_scheme;
dw_scheme.precision(10);
for (ssize_t row = 0; row < G.rows(); ++row) {
std::string line = str(G(row, 0), 10);
if (row > 0)
dw_scheme << "\n";
dw_scheme << G(row, 0);
for (ssize_t col = 1; col < G.cols(); ++col)
line += "," + str(G(row, col), 10);
add_line(dw_scheme, line);
dw_scheme << "," << G(row, col);
}
return dw_scheme;
return dw_scheme.str();
}
} // namespace

Expand Down
56 changes: 29 additions & 27 deletions cpp/core/file/mgh.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ template <class Input> void read_other(Header &H, Input &in) {
auto read_mri_frame = [&](Input &in, const int64_t len) {
const int64_t fstart = in.tellg();
const size_t nframes = H.ndim() == 4 ? H.size(3) : 1;
std::string table;
std::ostringstream table;
Eigen::IOFormat format(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", " ", "", "", "", "");
for (size_t frame_index = 0; frame_index != nframes; ++frame_index) {
mri_frame frame;
Expand Down Expand Up @@ -345,14 +345,14 @@ template <class Input> void read_other(Header &H, Input &in) {
frame.thresh = fetch<float32>(in);
frame.units = fetch<int32_t>(in);

std::string line = str(frame.type) + "," + str(frame.TE) + "," + str(frame.TR) + "," + str(frame.flip) + "," +
str(frame.TI) + "," + str(frame.TD) + "," + str(frame.sequence_type) + "," +
str(frame.echo_spacing) + "," + str(frame.echo_train_len) + "," + str(frame.read_dir[0]) +
"," + str(frame.read_dir[1]) + "," + str(frame.read_dir[2]) + "," + str(frame.pe_dir[0]) +
"," + str(frame.pe_dir[1]) + "," + str(frame.pe_dir[2]) + "," + str(frame.slice_dir[0]) + "," +
str(frame.slice_dir[1]) + "," + str(frame.slice_dir[2]) + "," + str(frame.label) + "," +
frame.name + "," + str(frame.dof) + "," + str(frame.m_ras2vox->format(format)) + "," +
str(frame.thresh) + "," + str(frame.units);
if (frame_index > 0)
table << "\n";
table << frame.type << "," << frame.TE << "," << frame.TR << "," << frame.flip << "," << frame.TI << ","
<< frame.TD << "," << frame.sequence_type << "," << frame.echo_spacing << "," << frame.echo_train_len << ","
<< frame.read_dir[0] << "," << frame.read_dir[1] << "," << frame.read_dir[2] << "," << frame.pe_dir[0]
<< "," << frame.pe_dir[1] << "," << frame.pe_dir[2] << "," << frame.slice_dir[0] << ","
<< frame.slice_dir[1] << "," << frame.slice_dir[2] << "," << frame.label << "," << frame.name << ","

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay]

            << frame.slice_dir[1] << "," << frame.slice_dir[2] << "," << frame.label << "," << frame.name << ","
                                                                                               ^

<< frame.dof << "," << frame.m_ras2vox->format(format) << "," << frame.thresh << "," << frame.units;

delete frame.m_ras2vox;
frame.m_ras2vox = nullptr;
Expand Down Expand Up @@ -383,15 +383,12 @@ template <class Input> void read_other(Header &H, Input &in) {
frame.D4_flat = fetch<int64_t>(in);
frame.D4_amp = fetch<float64>(in);

line += "," + str(frame.DX) + "," + str(frame.DY) + "," + str(frame.DZ) + "," + str(frame.DR) + "," +
str(frame.DP) + "," + str(frame.DS) + "," + str(frame.bvalue) + "," + str(frame.TM) + "," +
str(frame.D1_ramp) + "," + str(frame.D1_flat) + "," + str(frame.D1_amp) + "," + str(frame.D2_ramp) +
"," + str(frame.D2_flat) + "," + str(frame.D2_amp) + "," + str(frame.D3_ramp) + "," +
str(frame.D3_flat) + "," + str(frame.D3_amp) + "," + str(frame.D4_ramp) + "," + str(frame.D4_flat) +
"," + str(frame.D4_amp);
table << "," << frame.DX << "," << frame.DY << "," << frame.DZ << "," << frame.DR << "," << frame.DP << ","
<< frame.DS << "," << frame.bvalue << "," << frame.TM << "," << frame.D1_ramp << "," << frame.D1_flat
<< "," << frame.D1_amp << "," << frame.D2_ramp << "," << frame.D2_flat << "," << frame.D2_amp << ","
<< frame.D3_ramp << "," << frame.D3_flat << "," << frame.D3_amp << "," << frame.D4_ramp << ","
<< frame.D4_flat << "," << frame.D4_amp;
}

add_line(table, line);
}

// Test to see if the correct amount of data has been read
Expand All @@ -403,13 +400,13 @@ template <class Input> void read_other(Header &H, Input &in) {
in.read(buffer, empty_space_len);
}

return table;
return table.str();
};

auto read_colourtable_V1 = [&](Input &in, const int32_t nentries) {
if (!nentries)
throw Exception("Error reading colour table from file \"" + H.name() + "\": No entries");
std::string table;
std::ostringstream table;
const int32_t filename_length = fetch<int32_t>(in);
std::string filename(filename_length, '\0');
in.read(const_cast<char *>(filename.data()), filename_length);
Expand All @@ -419,16 +416,18 @@ template <class Input> void read_other(Header &H, Input &in) {
throw Exception("Error reading colour table from file \"" + H.name() + "\": Negative structure name length");
std::string structurename(structurename_length, '\0');
in.read(const_cast<char *>(structurename.data()), structurename_length);
while (!structurename.empty() && !structurename.back())
while (!structurename.empty() && structurename.back() == '\0')
structurename.pop_back();
const int32_t r = fetch<int32_t>(in);
const int32_t g = fetch<int32_t>(in);
const int32_t b = fetch<int32_t>(in);
const int32_t t = fetch<int32_t>(in);
const int32_t a = 255 - t; // Alpha = 255 - transparency
add_line(table, structurename + "," + str(r) + "," + str(g) + "," + str(b) + "," + str(a));
if (structure > 0)
table << "\n";
table << structurename << "," << r << "," << g << "," << b << "," << a;
}
return table;
return table.str();
};

auto read_colourtable_V2 = [&](Input &in) {
Expand Down Expand Up @@ -456,7 +455,7 @@ template <class Input> void read_other(Header &H, Input &in) {
" Negative structure name length"); //
std::string structurename(structurename_length, '\0');
in.read(const_cast<char *>(structurename.data()), structurename_length);
while (!structurename.empty() && !structurename.back())
while (!structurename.empty() && structurename.back() == '\0')
structurename.pop_back();
const int32_t r = fetch<int32_t>(in);
const int32_t g = fetch<int32_t>(in);
Expand All @@ -465,12 +464,15 @@ template <class Input> void read_other(Header &H, Input &in) {
const int32_t a = 255 - t; // Alpha = 255 - transparency
table[structure] = structurename + "," + str(r) + "," + str(g) + "," + str(b) + "," + str(a);
}
std::string result;
std::ostringstream result;
for (size_t index = 0; index != table.size(); ++index) {
if (!table[index].empty())
add_line(result, str(index) + "," + table[index]);
if (!table[index].empty()) {
if (!result.str().empty())
result << "\n";
result << index << "," << table[index];
}
}
return result;
return result.str();
};

// Start the function read_other() proper
Expand Down
28 changes: 18 additions & 10 deletions cpp/core/metadata/phase_encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,29 @@ void set_scheme(KeyValues &keyval, const scheme_type &PE) {
erase(keyval, "TotalReadoutTime");
return;
}
std::string pe_scheme;
std::ostringstream pe_scheme;
std::string first_line;
bool variation = false;
for (ssize_t row = 0; row < PE.rows(); ++row) {
std::string line = str(PE(row, 0));
for (ssize_t col = 1; col < PE.cols(); ++col)
line += "," + str(PE(row, col), 3);
add_line(pe_scheme, line);
if (first_line.empty())
first_line = line;
else if (line != first_line)
variation = true;
std::ostringstream line;
line.precision(0);
line << static_cast<int>(PE(row, 0));
for (ssize_t col = 1; col < 3; ++col)
line << "," << static_cast<int>(PE(row, col));
line.precision(3);
for (ssize_t col = 3; col < PE.cols(); ++col)
line << "," << PE(row, col);
if (row == 0) {
first_line = line.str();
} else {
pe_scheme << "\n";
if (line.str() != first_line)
variation = true;
}
pe_scheme << line.str();
}
if (variation) {
keyval["pe_scheme"] = pe_scheme;
keyval["pe_scheme"] = pe_scheme.str();
erase(keyval, "PhaseEncodingDirection");
erase(keyval, "TotalReadoutTime");
} else {
Expand Down
Loading