v2.0.0
Loading...
Searching...
No Matches
mri_cor_io.cpp
Go to the documentation of this file.
1//=============================================================================================================
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "mri_cor_io.h"
43
45#include <fiff/fiff_constants.h>
46#include <fiff/fiff_file.h>
47
48//=============================================================================================================
49// QT INCLUDES
50//=============================================================================================================
51
52#include <QDir>
53#include <QFile>
54#include <QDebug>
55
56//=============================================================================================================
57// EIGEN INCLUDES
58//=============================================================================================================
59
60#include <Eigen/Core>
61
62//=============================================================================================================
63// USED NAMESPACES
64//=============================================================================================================
65
66using namespace MRILIB;
67using namespace FIFFLIB;
68using namespace Eigen;
69
70//=============================================================================================================
71// DEFINE MEMBER METHODS
72//=============================================================================================================
73
74bool MriCorIO::read(const QString& dir,
75 QVector<MriSlice>& slices,
76 bool verbose)
77{
78 //
79 // COR files contain 256 coronal slices of 256×256 unsigned chars (1mm isotropic).
80 //
81 // The coordinate system is:
82 // - Coronal orientation
83 // - 1mm pixel spacing
84 // - Origin offset: (0.128, -0.128, 0.128) meters
85 // - Rotation: x -> -x, y -> z, z -> y (coronal to MRI surface RAS)
86 //
87 // Ported from make_cor_set() in mne_make_cor_set/make_cor_set.c
88 //
89
90 int nPixels = COR_WIDTH * COR_HEIGHT;
91
92 slices.resize(COR_NSLICE);
93
94 for (int k = 0; k < COR_NSLICE; ++k) {
95 QString fileName = QString("%1/COR-%2").arg(dir).arg(k + 1, 3, 10, QChar('0'));
96 QFile file(fileName);
97
98 if (!file.open(QIODevice::ReadOnly)) {
99 qCritical() << "MriCorIO::read - Could not open COR file" << fileName;
100 return false;
101 }
102
103 QByteArray data = file.readAll();
104 file.close();
105
106 if (data.size() < nPixels) {
107 qCritical() << "MriCorIO::read - COR file" << fileName << "is too small:"
108 << data.size() << "bytes (expected" << nPixels << ")";
109 return false;
110 }
111
112 MriSlice& slice = slices[k];
113 slice.fileName = fileName;
114 slice.width = COR_WIDTH;
115 slice.height = COR_HEIGHT;
116 slice.dimx = COR_PIXEL_SIZE;
117 slice.dimy = COR_PIXEL_SIZE;
118 slice.scale = 1.0f;
120
121 // Copy pixel data
122 slice.pixels.resize(nPixels);
123 memcpy(slice.pixels.data(), data.constData(), nPixels);
124
125 // Build the coordinate transformation: slice -> MRI (surface RAS)
126 // Ported from the original C code in make_cor_set.c:
127 // move[X] = 0.128; move[Y] = -0.128 + k/1000.0; move[Z] = 0.128;
128 // rot = {{-1,0,0},{0,0,1},{0,1,0}};
129 Matrix3f rot;
130 rot << -1.0f, 0.0f, 0.0f,
131 0.0f, 0.0f, 1.0f,
132 0.0f, 1.0f, 0.0f;
133
134 Eigen::Vector3f move;
135 move << 0.128f, -0.128f + static_cast<float>(k) / 1000.0f, 0.128f;
136
138 }
139
140 if (verbose) {
141 printf("Read %d COR slices from %s\n", COR_NSLICE, qPrintable(dir));
142 }
143
144 return true;
145}
Fiff constants.
#define FIFFV_COORD_MRI_SLICE
#define FIFFV_COORD_MRI
Header file describing the numerical values used in fif files.
#define FIFFV_MRI_PIXEL_BYTE
Definition fiff_file.h:702
FiffCoordTrans class declaration.
MriCorIO class declaration.
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
MRI volume and coordinate-system I/O (volumes, voxel geometry, transforms).
constexpr float COR_PIXEL_SIZE
Definition mri_types.h:143
constexpr int COR_HEIGHT
Definition mri_types.h:142
constexpr int COR_WIDTH
Definition mri_types.h:141
constexpr int COR_NSLICE
Definition mri_types.h:140
Coordinate transformation description.
static bool read(const QString &dir, QVector< MriSlice > &slices, bool verbose=false)
Single MRI slice data.
QVector< unsigned char > pixels
FIFFLIB::FiffCoordTrans trans