v2.0.0
Loading...
Searching...
No Matches
mri_cor_io.cpp
Go to the documentation of this file.
1//=============================================================================================================
23
24//=============================================================================================================
25// INCLUDES
26//=============================================================================================================
27
28#include "mri_cor_io.h"
29
31#include <fiff/fiff_constants.h>
32#include <fiff/fiff_file.h>
33
34//=============================================================================================================
35// QT INCLUDES
36//=============================================================================================================
37
38#include <QDir>
39#include <QFile>
40#include <QDebug>
41
42//=============================================================================================================
43// EIGEN INCLUDES
44//=============================================================================================================
45
46#include <Eigen/Core>
47
48//=============================================================================================================
49// USED NAMESPACES
50//=============================================================================================================
51
52using namespace MRILIB;
53using namespace FIFFLIB;
54using namespace Eigen;
55
56//=============================================================================================================
57// DEFINE MEMBER METHODS
58//=============================================================================================================
59
60bool MriCorIO::read(const QString& dir,
61 QVector<MriSlice>& slices,
62 bool verbose)
63{
64 //
65 // COR files contain 256 coronal slices of 256×256 unsigned chars (1mm isotropic).
66 //
67 // The coordinate system is:
68 // - Coronal orientation
69 // - 1mm pixel spacing
70 // - Origin offset: (0.128, -0.128, 0.128) meters
71 // - Rotation: x -> -x, y -> z, z -> y (coronal to MRI surface RAS)
72 //
73 // Ported from make_cor_set() in mne_make_cor_set/make_cor_set.c
74 //
75
76 int nPixels = COR_WIDTH * COR_HEIGHT;
77
78 slices.resize(COR_NSLICE);
79
80 for (int k = 0; k < COR_NSLICE; ++k) {
81 QString fileName = QString("%1/COR-%2").arg(dir).arg(k + 1, 3, 10, QChar('0'));
82 QFile file(fileName);
83
84 if (!file.open(QIODevice::ReadOnly)) {
85 qCritical() << "MriCorIO::read - Could not open COR file" << fileName;
86 return false;
87 }
88
89 QByteArray data = file.readAll();
90 file.close();
91
92 if (data.size() < nPixels) {
93 qCritical() << "MriCorIO::read - COR file" << fileName << "is too small:"
94 << data.size() << "bytes (expected" << nPixels << ")";
95 return false;
96 }
97
98 MriSlice& slice = slices[k];
99 slice.fileName = fileName;
100 slice.width = COR_WIDTH;
101 slice.height = COR_HEIGHT;
102 slice.dimx = COR_PIXEL_SIZE;
103 slice.dimy = COR_PIXEL_SIZE;
104 slice.scale = 1.0f;
106
107 // Copy pixel data
108 slice.pixels.resize(nPixels);
109 memcpy(slice.pixels.data(), data.constData(), nPixels);
110
111 // Build the coordinate transformation: slice -> MRI (surface RAS)
112 // Ported from the original C code in make_cor_set.c:
113 // move[X] = 0.128; move[Y] = -0.128 + k/1000.0; move[Z] = 0.128;
114 // rot = {{-1,0,0},{0,0,1},{0,1,0}};
115 Matrix3f rot;
116 rot << -1.0f, 0.0f, 0.0f,
117 0.0f, 0.0f, 1.0f,
118 0.0f, 1.0f, 0.0f;
119
120 Eigen::Vector3f move;
121 move << 0.128f, -0.128f + static_cast<float>(k) / 1000.0f, 0.128f;
122
124 }
125
126 if (verbose) {
127 printf("Read %d COR slices from %s\n", COR_NSLICE, qPrintable(dir));
128 }
129
130 return true;
131}
Reader for the legacy FreeSurfer COR-NNN per-slice volume layout (256 unsigned-char coronal slices on...
return FiffCoordTrans(from_frame, to_frame, R, moveVec)
Symbolic FIFF tag, block, value, unit and channel-type constants shared across FIFFLIB.
#define FIFFV_COORD_MRI_SLICE
#define FIFFV_COORD_MRI
FIFF tag-kind, block-kind and type-code numerical definitions, authoritative for FIFFLIB.
#define FIFFV_MRI_PIXEL_BYTE
Definition fiff_file.h:695
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
FIFF file I/O, in-memory data structures and high-level readers/writers.
Volume I/O, voxel geometry and slice resampling for structural MRI data inside mne-cpp.
constexpr float COR_PIXEL_SIZE
Definition mri_types.h:136
constexpr int COR_HEIGHT
Definition mri_types.h:135
constexpr int COR_WIDTH
Definition mri_types.h:134
constexpr int COR_NSLICE
Definition mri_types.h:133
static bool read(const QString &dir, QVector< MriSlice > &slices, bool verbose=false)
Single 2D MRI slice (pixels + slice→RAS transform) used as the volume's storage unit.
QVector< unsigned char > pixels
FIFFLIB::FiffCoordTrans trans