Skip to content
Open
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ endif()
# ── Core library ────────────────────────────────────────────────────────────
add_library(trx
src/trx.cpp
src/legacy_io.cpp
src/detail/dtype_helpers.cpp
include/trx/trx.h
include/trx/trx.tpp
Expand Down
62 changes: 62 additions & 0 deletions include/trx/legacy_io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef TRX_LEGACY_IO_H
#define TRX_LEGACY_IO_H

#include <string>
#include <vector>
#include <cstdint>
#include <trx/trx.h>

namespace trx {
namespace legacy {

struct Tractogram {
std::vector<float> pts;
std::vector<uint64_t> offsets;
json11::Json header;
std::shared_ptr<trx::AnyTrxFile> original_trx;
};

#pragma pack(push, 1)
struct TrkHeader {
char magic_number[6];
int16_t dimensions[3];
float voxel_sizes[3];
float origin[3];
int16_t nb_scalars_per_point;
char scalar_name[10][20];
int16_t nb_properties_per_streamline;
char property_name[10][20];
float voxel_to_rasmm[4][4];
char reserved[444];
char voxel_order[4];
char pad2[4];
float image_orientation_patient[6];
char pad1[2];
char invert_x;
char invert_y;
char invert_z;
char swap_xy;
char swap_yz;
char swap_zx;
int32_t nb_streamlines;
int32_t version;
int32_t hdr_size;
};
#pragma pack(pop)

bool load_trx(const std::string &filename, Tractogram &tr);
bool load_trk(const std::string &filename, Tractogram &tr);
bool load_tck(const std::string &filename, Tractogram &tr);
bool load_vtk(const std::string &filename, Tractogram &tr);

bool load_nifti_header(const std::string &ref_path, json11::Json &out_header);

bool save_trx(const Tractogram &tr, const std::string &out_path, const std::string &ref_nifti_path = "");
bool save_trk(const Tractogram &tr, const std::string &out_path, const std::string &original_filename = "", const std::string &ref_nifti_path = "");
bool save_tck(const Tractogram &tr, const std::string &out_path);
bool save_vtk(const Tractogram &tr, const std::string &out_path);

} // namespace legacy
} // namespace trx

#endif // TRX_LEGACY_IO_H
68 changes: 68 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <string>
#include <trx/legacy_io.h>
#include <trx/trx.h>

int main(int argc, char** argv) {
std::string input_file;
std::string output_file;
std::string ref_path;

for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--ref") {
if (i + 1 < argc) {
ref_path = argv[++i];
} else {
std::cerr << "Error: --ref requires an argument\n";
return 1;
}
} else if (input_file.empty()) {
input_file = arg;
} else if (output_file.empty()) {
output_file = arg;
}
}

if (input_file.empty() || output_file.empty()) {
std::cerr << "Usage: convert <input> <output> [--ref <nifti_file>]\n";
return 1;
}

trx::legacy::Tractogram tr;
bool success = false;

auto ends_with = [](const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
};

if (ends_with(input_file, ".trx")) success = trx::legacy::load_trx(input_file, tr);
else if (ends_with(input_file, ".trk")) success = trx::legacy::load_trk(input_file, tr);
else if (ends_with(input_file, ".tck")) success = trx::legacy::load_tck(input_file, tr);
else if (ends_with(input_file, ".vtk")) success = trx::legacy::load_vtk(input_file, tr);

if (!success) {
std::cerr << "Error loading input file\n";
return 1;
}

bool is_tck_vtk = ends_with(input_file, ".tck") || ends_with(input_file, ".vtk");
bool is_trx_trk = ends_with(output_file, ".trx") || ends_with(output_file, ".trk");

if (is_tck_vtk && is_trx_trk && ref_path.empty()) {
std::cerr << "Error: TCK/VTK -> TRX/TRK conversion requires --ref <nifti_file>\n";
return 1;
}

success = false;
if (ends_with(output_file, ".trx")) success = trx::legacy::save_trx(tr, output_file, ref_path);
else if (ends_with(output_file, ".trk")) success = trx::legacy::save_trk(tr, output_file, input_file, ref_path);
else if (ends_with(output_file, ".tck")) success = trx::legacy::save_tck(tr, output_file);
else if (ends_with(output_file, ".vtk")) success = trx::legacy::save_vtk(tr, output_file);

if (!success) {
std::cerr << "Error saving output file\n";
return 1;
}
return 0;
}
Loading
Loading