v2.0.0
Loading...
Searching...
No Matches
mri_mgh_io.cpp
Go to the documentation of this file.
1//=============================================================================================================
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "mri_mgh_io.h"
43
45#include <fiff/fiff_constants.h>
46#include <fiff/fiff_file.h>
47
48//=============================================================================================================
49// QT INCLUDES
50//=============================================================================================================
51
52#include <QFile>
53#include <QFileInfo>
54#include <QDataStream>
55#include <QDebug>
56#include <QRegularExpression>
57
58#include <zlib.h>
59
60//=============================================================================================================
61// EIGEN INCLUDES
62//=============================================================================================================
63
64#include <Eigen/Core>
65
66//=============================================================================================================
67// USED NAMESPACES
68//=============================================================================================================
69
70using namespace MRILIB;
71using namespace FIFFLIB;
72using namespace Eigen;
73
74//=============================================================================================================
75// DEFINE MEMBER METHODS
76//=============================================================================================================
77
78bool MriMghIO::read(const QString& mgzFile,
79 MriVolData& volData,
80 QVector<FiffCoordTrans>& additionalTrans,
81 const QString& subjectMriDir,
82 bool verbose)
83{
84 volData.fileName = mgzFile;
85
86 // Step 1: Get raw (decompressed) bytes
87 bool isCompressed = mgzFile.endsWith(".mgz", Qt::CaseInsensitive);
88 QByteArray fileData;
89
90 if (isCompressed) {
91 if (!decompress(mgzFile, fileData)) {
92 return false;
93 }
94 } else {
95 QFile file(mgzFile);
96 if (!file.open(QIODevice::ReadOnly)) {
97 qCritical() << "MriMghIO::read - Could not open" << mgzFile;
98 return false;
99 }
100 fileData = file.readAll();
101 file.close();
102 }
103
104 if (fileData.size() < MRI_MGH_DATA_OFFSET) {
105 qCritical() << "MriMghIO::read - File" << mgzFile
106 << "is too small to be a valid MGH file ("
107 << fileData.size() << "bytes)";
108 return false;
109 }
110
111 // Step 2: Parse header
112 if (!parseHeader(fileData, volData, verbose)) {
113 return false;
114 }
115
116 // Step 3: Build the voxel -> surface RAS transform
117 Matrix4f vox2ras = volData.computeVox2Ras();
119 FIFFV_COORD_MRI_SLICE, FIFFV_COORD_MRI, vox2ras, true);
120
121 if (verbose) {
122 printf("Voxel -> FsSurface RAS transform:\n");
123 for (int r = 0; r < 4; ++r) {
124 printf(" %10.6f %10.6f %10.6f %10.6f\n",
125 vox2ras(r, 0), vox2ras(r, 1), vox2ras(r, 2), vox2ras(r, 3));
126 }
127 }
128
129 // Step 4: Read voxel data
130 if (!readVoxelData(fileData, volData)) {
131 return false;
132 }
133
134 // Step 5: Parse footer (optional)
135 parseFooter(fileData, volData, additionalTrans, subjectMriDir, verbose);
136
137 if (verbose) {
138 printf("Read %d slices from %s (%dx%d pixels)\n",
139 static_cast<int>(volData.slices.size()), qPrintable(mgzFile),
140 volData.width, volData.height);
141 }
142
143 return true;
144}
145
146//=============================================================================================================
147
148bool MriMghIO::decompress(const QString& mgzFile, QByteArray& rawData)
149{
150 QFile file(mgzFile);
151 if (!file.open(QIODevice::ReadOnly)) {
152 qCritical() << "MriMghIO::decompress - Could not open" << mgzFile;
153 return false;
154 }
155 QByteArray compressedData = file.readAll();
156 file.close();
157
158 if (compressedData.isEmpty()) {
159 qCritical() << "MriMghIO::decompress - File is empty:" << mgzFile;
160 return false;
161 }
162
163 // Use zlib to decompress gzip data in memory
164 z_stream strm = {};
165
166 // MAX_WBITS + 16 tells zlib to detect and handle gzip headers
167 int ret = inflateInit2(&strm, MAX_WBITS + 16);
168 if (ret != Z_OK) {
169 qCritical() << "MriMghIO::decompress - inflateInit2 failed";
170 return false;
171 }
172
173 strm.next_in = reinterpret_cast<Bytef*>(compressedData.data());
174 strm.avail_in = static_cast<uInt>(compressedData.size());
175
176 const int chunkSize = 256 * 1024; // 256 KB chunks
177 rawData.clear();
178
179 do {
180 rawData.resize(rawData.size() + chunkSize);
181 strm.next_out = reinterpret_cast<Bytef*>(rawData.data() + rawData.size() - chunkSize);
182 strm.avail_out = chunkSize;
183
184 ret = inflate(&strm, Z_NO_FLUSH);
185 if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
186 qCritical() << "MriMghIO::decompress - inflate failed for" << mgzFile
187 << "- zlib error:" << ret;
188 inflateEnd(&strm);
189 return false;
190 }
191 } while (ret != Z_STREAM_END);
192
193 // Trim to actual decompressed size
194 rawData.resize(rawData.size() - static_cast<int>(strm.avail_out));
195 inflateEnd(&strm);
196
197 return true;
198}
199
200//=============================================================================================================
201
202bool MriMghIO::parseHeader(const QByteArray& data, MriVolData& volData, bool verbose)
203{
204 //
205 // MGH header layout (all big-endian):
206 // Bytes 0-3: version (int32)
207 // Bytes 4-7: width (int32)
208 // Bytes 8-11: height (int32)
209 // Bytes 12-15: depth (int32)
210 // Bytes 16-19: nframes (int32)
211 // Bytes 20-23: type (int32)
212 // Bytes 24-27: dof (int32)
213 // Bytes 28-29: goodRASflag (int16)
214 // Bytes 30-41: spacingX/Y/Z (3×float32) — only if goodRASflag > 0
215 // Bytes 42-77: Mdc (9×float32) — direction cosines, only if goodRASflag > 0
216 // Bytes 78-89: c_ras (3×float32) — center RAS, only if goodRASflag > 0
217 // Bytes 90-283: unused (padding)
218 //
219
220 QDataStream stream(data);
221 stream.setByteOrder(QDataStream::BigEndian);
222 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
223
224 qint32 version, width, height, depth, nframes, type, dof;
225 stream >> version >> width >> height >> depth >> nframes >> type >> dof;
226
227 if (version != MRI_MGH_VERSION) {
228 qCritical() << "MriMghIO::parseHeader - Unknown MGH version:" << version;
229 return false;
230 }
231
232 volData.version = version;
233 volData.width = width;
234 volData.height = height;
235 volData.depth = depth;
236 volData.nframes = nframes;
237 volData.type = type;
238 volData.dof = dof;
239
240 if (verbose) {
241 printf("MGH file: %dx%dx%d, %d frame(s), type=%d\n",
242 width, height, depth, nframes, type);
243 }
244
245 // goodRASflag (2 bytes short)
246 qint16 goodRASflag;
247 stream >> goodRASflag;
248 volData.rasGood = (goodRASflag > 0);
249
250 if (goodRASflag > 0) {
251 // Voxel sizes
252 stream >> volData.xsize >> volData.ysize >> volData.zsize;
253
254 // Direction cosines (Mdc matrix):
255 // xr, xa, xs (x-direction cosines)
256 // yr, ya, ys (y-direction cosines)
257 // zr, za, zs (z-direction cosines)
258 stream >> volData.x_ras[0] >> volData.x_ras[1] >> volData.x_ras[2];
259 stream >> volData.y_ras[0] >> volData.y_ras[1] >> volData.y_ras[2];
260 stream >> volData.z_ras[0] >> volData.z_ras[1] >> volData.z_ras[2];
261
262 // Center RAS
263 stream >> volData.c_ras[0] >> volData.c_ras[1] >> volData.c_ras[2];
264 }
265 // Else: default values from MriVolData constructor are used
266
267 if (verbose) {
268 printf("Voxel sizes: %.4f x %.4f x %.4f mm\n",
269 volData.xsize, volData.ysize, volData.zsize);
270 printf("goodRAS: %d\n", goodRASflag);
271 printf("c_ras: %.4f %.4f %.4f\n",
272 volData.c_ras[0], volData.c_ras[1], volData.c_ras[2]);
273 }
274
275 return true;
276}
277
278//=============================================================================================================
279
280bool MriMghIO::readVoxelData(const QByteArray& data, MriVolData& volData)
281{
282 //
283 // Read voxel data starting at byte 284 (MRI_MGH_DATA_OFFSET).
284 // Data layout in MGH: [width][height][depth][frames] in Fortran order (x fastest).
285 // Only the first frame is read.
286 //
287
288 int bpv = bytesPerVoxel(volData.type);
289 if (bpv == 0) {
290 qCritical() << "MriMghIO::readVoxelData - Unsupported MGH data type:" << volData.type;
291 return false;
292 }
293
294 qint64 frameSize = static_cast<qint64>(volData.width) * volData.height * volData.depth * bpv;
295 if (data.size() < MRI_MGH_DATA_OFFSET + frameSize) {
296 qCritical() << "MriMghIO::readVoxelData - File too small for expected data size";
297 return false;
298 }
299
300 QDataStream stream(data);
301 stream.setByteOrder(QDataStream::BigEndian);
302 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
303 stream.device()->seek(MRI_MGH_DATA_OFFSET);
304
305 int nslice = volData.depth;
306 int nPixels = volData.width * volData.height;
307 volData.slices.resize(nslice);
308
309 // Build the vox2ras transform for per-slice transforms
310 Matrix4f vox2ras = volData.computeVox2Ras();
311
312 for (int k = 0; k < nslice; ++k) {
313 MriSlice& slice = volData.slices[k];
314 slice.width = volData.width;
315 slice.height = volData.height;
316 slice.dimx = volData.xsize / 1000.0f; // mm -> meters
317 slice.dimy = volData.ysize / 1000.0f;
318
319 // Read pixel data for this slice
320 switch (volData.type) {
321 case MRI_UCHAR: {
323 slice.pixels.resize(nPixels);
324 for (int p = 0; p < nPixels; ++p) {
325 quint8 val;
326 stream >> val;
327 slice.pixels[p] = val;
328 }
329 slice.scale = 1.0f;
330 break;
331 }
332 case MRI_SHORT: {
334 slice.pixelsWord.resize(nPixels);
335 for (int p = 0; p < nPixels; ++p) {
336 qint16 val;
337 stream >> val;
338 slice.pixelsWord[p] = static_cast<unsigned short>(val < 0 ? 0 : val);
339 }
340 slice.scale = 1.0f;
341 break;
342 }
343 case MRI_INT: {
344 // Convert INT to FLOAT
346 slice.pixelsFloat.resize(nPixels);
347 for (int p = 0; p < nPixels; ++p) {
348 qint32 val;
349 stream >> val;
350 slice.pixelsFloat[p] = static_cast<float>(val);
351 }
352 slice.scale = 1.0f;
353 break;
354 }
355 case MRI_FLOAT: {
357 slice.pixelsFloat.resize(nPixels);
358 for (int p = 0; p < nPixels; ++p) {
359 float val;
360 stream >> val;
361 slice.pixelsFloat[p] = val;
362 }
363 slice.scale = 1.0f;
364 break;
365 }
366 }
367
368 //
369 // Build per-slice coordinate transform (slice -> MRI surface RAS).
370 // For each slice k:
371 // sliceOrigin = vox2ras * [0, 0, k, 1]^T
372 // sliceRot = vox2ras rotation columns (x, y, z pixel axes)
373 //
374 Vector3f sliceOrigin;
375 sliceOrigin(0) = vox2ras(0, 2) * k + vox2ras(0, 3);
376 sliceOrigin(1) = vox2ras(1, 2) * k + vox2ras(1, 3);
377 sliceOrigin(2) = vox2ras(2, 2) * k + vox2ras(2, 3);
378
379 Matrix3f sliceRot;
380 sliceRot.col(0) = vox2ras.block<3, 1>(0, 0); // x-pixel direction
381 sliceRot.col(1) = vox2ras.block<3, 1>(0, 1); // y-pixel direction
382 sliceRot.col(2) = vox2ras.block<3, 1>(0, 2); // z (normal) direction
383
384 Vector3f sliceMove;
385 sliceMove << sliceOrigin(0), sliceOrigin(1), sliceOrigin(2);
386
387 slice.trans = FiffCoordTrans(FIFFV_COORD_MRI_SLICE, FIFFV_COORD_MRI, sliceRot, sliceMove);
388 }
389
390 return true;
391}
392
393//=============================================================================================================
394
395bool MriMghIO::parseFooter(const QByteArray& data,
396 MriVolData& volData,
397 QVector<FiffCoordTrans>& additionalTrans,
398 const QString& subjectMriDir,
399 bool verbose)
400{
401 //
402 // The footer starts after the voxel data.
403 // It contains (in order):
404 // 1. Scan parameters: TR(f32), flipAngle(f32), TE(f32), TI(f32), FoV(f32)
405 // 2. Tags: tagType(i32) + tagLen(i32 or i64) + tagData
406 //
407
408 int bpv = bytesPerVoxel(volData.type);
409 if (bpv == 0) {
410 bpv = 1; // Fallback for footer calculation
411 }
412
413 qint64 frameSize = static_cast<qint64>(volData.width) * volData.height * volData.depth * bpv;
414 qint64 footerPos = MRI_MGH_DATA_OFFSET + frameSize;
415
416 if (data.size() <= footerPos) {
417 // No footer — that's fine
418 return true;
419 }
420
421 QDataStream stream(data);
422 stream.setByteOrder(QDataStream::BigEndian);
423 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
424 stream.device()->seek(footerPos);
425
426 // Read scan parameters (5 × float32 = 20 bytes)
427 constexpr int kScanParamBytes = 5 * sizeof(float);
428 qint64 remainingBytes = data.size() - footerPos;
429 if (remainingBytes >= kScanParamBytes) {
430 stream >> volData.TR >> volData.flipAngle >> volData.TE >> volData.TI >> volData.FoV;
431 } else {
432 return true;
433 }
434
435 // Parse tags
436 while (!stream.atEnd()) {
437 qint32 tagType;
438 stream >> tagType;
439 if (stream.atEnd()) break;
440
441 qint64 tagLen;
442 // For TAG_OLD_SURF_GEOM (20) and TAG_OLD_MGH_XFORM (30), length is 4 bytes
443 // For newer tags, length is 8 bytes
444 if (tagType == MGH_TAG_OLD_SURF_GEOM || tagType == MGH_TAG_OLD_MGH_XFORM) {
445 qint32 len32;
446 stream >> len32;
447 tagLen = len32;
448 } else {
449 qint64 len64;
450 stream >> len64;
451 tagLen = len64;
452 }
453
454 if (tagLen <= 0 || tagLen > data.size()) break;
455
456 QByteArray tagData(tagLen, '\0');
457 if (stream.readRawData(tagData.data(), tagLen) != tagLen) break;
458
459 if (tagType == MGH_TAG_MGH_XFORM) {
460 // TAG_MGH_XFORM: contains path to talairach.xfm
461 QString xfmPath = QString::fromLatin1(tagData).trimmed();
462 volData.talairachXfmPath = xfmPath;
463
464 if (verbose) {
465 printf("Found Talairach transform reference: %s\n", qPrintable(xfmPath));
466 }
467
468 // Resolve relative paths using subject MRI directory
469 if (!QFileInfo(xfmPath).isAbsolute() && !subjectMriDir.isEmpty()) {
470 xfmPath = subjectMriDir + "/transforms/" + xfmPath;
471 }
472
473 if (QFileInfo::exists(xfmPath)) {
474 QFile xfmFile(xfmPath);
475 if (xfmFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
476 // Parse Linear_Transform from .xfm file
477 // Format:
478 // MNI Transform File
479 // ...
480 // Linear_Transform =
481 // mat[0][0] mat[0][1] mat[0][2] mat[0][3]
482 // mat[1][0] mat[1][1] mat[1][2] mat[1][3]
483 // mat[2][0] mat[2][1] mat[2][2] mat[2][3] ;
484 QString xfmContent = xfmFile.readAll();
485 xfmFile.close();
486
487 int ltIdx = xfmContent.indexOf("Linear_Transform");
488 if (ltIdx >= 0) {
489 int eqIdx = xfmContent.indexOf('=', ltIdx);
490 if (eqIdx >= 0) {
491 QString matStr = xfmContent.mid(eqIdx + 1).trimmed();
492 matStr.remove(';');
493
494 QStringList vals = matStr.split(QRegularExpression("\\s+"),
495 Qt::SkipEmptyParts);
496
497 if (vals.size() >= 12) {
498 // RAS -> MNI Talairach (3×4 matrix, in mm)
499 Matrix4f rasMniTal = Matrix4f::Identity();
500 for (int r = 0; r < 3; ++r) {
501 for (int c = 0; c < 4; ++c) {
502 rasMniTal(r, c) = vals[r * 4 + c].toFloat();
503 }
504 }
505
506 // Convert translation from mm to meters
507 rasMniTal(0, 3) /= 1000.0f;
508 rasMniTal(1, 3) /= 1000.0f;
509 rasMniTal(2, 3) /= 1000.0f;
510
511 // Create RAS -> MNI Talairach transform
512 FiffCoordTrans talTrans(
514 rasMniTal, true);
515
516 additionalTrans.append(talTrans);
517
518 if (verbose) {
519 printf("Read Talairach transform from %s\n", qPrintable(xfmPath));
520 }
521 }
522 }
523 }
524 }
525 } else {
526 if (verbose) {
527 printf("Talairach transform file not found: %s\n", qPrintable(xfmPath));
528 }
529 }
530 }
531 }
532
533 return true;
534}
535
536//=============================================================================================================
537
538int MriMghIO::bytesPerVoxel(int type)
539{
540 switch (type) {
541 case MRI_UCHAR: return 1;
542 case MRI_SHORT: return 2;
543 case MRI_INT: return 4;
544 case MRI_FLOAT: return 4;
545 default: return 0;
546 }
547}
Fiff constants.
#define FIFFV_COORD_MRI_SLICE
#define FIFFV_COORD_MRI_DISPLAY
#define FIFFV_COORD_MRI
Header file describing the numerical values used in fif files.
#define FIFFV_MRI_PIXEL_BYTE
Definition fiff_file.h:702
#define FIFFV_MRI_PIXEL_FLOAT
Definition fiff_file.h:705
#define FIFFV_MRI_PIXEL_WORD
Definition fiff_file.h:703
FiffCoordTrans class declaration.
MriMghIO 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 int MRI_MGH_VERSION
Definition mri_types.h:57
constexpr int MGH_TAG_OLD_MGH_XFORM
Definition mri_types.h:125
constexpr int MRI_MGH_DATA_OFFSET
Definition mri_types.h:107
constexpr int MGH_TAG_OLD_SURF_GEOM
Definition mri_types.h:124
constexpr int MGH_TAG_MGH_XFORM
Definition mri_types.h:126
constexpr int MRI_SHORT
Definition mri_types.h:79
constexpr int MRI_UCHAR
Definition mri_types.h:75
constexpr int MRI_INT
Definition mri_types.h:76
constexpr int MRI_FLOAT
Definition mri_types.h:78
Coordinate transformation description.
static bool read(const QString &mgzFile, MriVolData &volData, QVector< FIFFLIB::FiffCoordTrans > &additionalTrans, const QString &subjectMriDir=QString(), bool verbose=false)
QVector< unsigned char > pixels
FIFFLIB::FiffCoordTrans trans
QVector< unsigned short > pixelsWord
QVector< float > pixelsFloat
MRI volume data from FreeSurfer MGH/MGZ file.
FIFFLIB::FiffCoordTrans voxelSurfRasT
Eigen::Vector3f y_ras
Eigen::Vector3f x_ras
QVector< MriSlice > slices
Eigen::Matrix4f computeVox2Ras() const
Eigen::Vector3f z_ras
Eigen::Vector3f c_ras