44#include <QRegularExpression>
69 QVector<FiffCoordTrans>& additionalTrans,
70 const QString& subjectMriDir,
76 bool isCompressed = mgzFile.endsWith(
".mgz", Qt::CaseInsensitive);
80 if (!decompress(mgzFile, fileData)) {
85 if (!file.open(QIODevice::ReadOnly)) {
86 qCritical() <<
"MriMghIO::read - Could not open" << mgzFile;
89 fileData = file.readAll();
94 qCritical() <<
"MriMghIO::read - File" << mgzFile
95 <<
"is too small to be a valid MGH file ("
96 << fileData.size() <<
"bytes)";
101 if (!parseHeader(fileData, volData, verbose)) {
111 qInfo(
"Voxel -> FsSurface RAS transform:\n");
112 for (
int r = 0; r < 4; ++r) {
113 qInfo(
" %10.6f %10.6f %10.6f %10.6f\n",
114 vox2ras(r, 0), vox2ras(r, 1), vox2ras(r, 2), vox2ras(r, 3));
119 if (!readVoxelData(fileData, volData)) {
124 parseFooter(fileData, volData, additionalTrans, subjectMriDir, verbose);
127 qInfo(
"Read %d slices from %s (%dx%d pixels)\n",
128 static_cast<int>(volData.
slices.size()), qPrintable(mgzFile),
137bool MriMghIO::decompress(
const QString& mgzFile, QByteArray& rawData)
140 if (!file.open(QIODevice::ReadOnly)) {
141 qCritical() <<
"MriMghIO::decompress - Could not open" << mgzFile;
144 QByteArray compressedData = file.readAll();
147 if (compressedData.isEmpty()) {
148 qCritical() <<
"MriMghIO::decompress - File is empty:" << mgzFile;
156 int ret = inflateInit2(&strm, MAX_WBITS + 16);
158 qCritical() <<
"MriMghIO::decompress - inflateInit2 failed";
162 strm.next_in =
reinterpret_cast<Bytef*
>(compressedData.data());
163 strm.avail_in =
static_cast<uInt
>(compressedData.size());
165 const int chunkSize = 256 * 1024;
169 rawData.resize(rawData.size() + chunkSize);
170 strm.next_out =
reinterpret_cast<Bytef*
>(rawData.data() + rawData.size() - chunkSize);
171 strm.avail_out = chunkSize;
173 ret = inflate(&strm, Z_NO_FLUSH);
174 if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
175 qCritical() <<
"MriMghIO::decompress - inflate failed for" << mgzFile
176 <<
"- zlib error:" << ret;
180 }
while (ret != Z_STREAM_END);
183 rawData.resize(rawData.size() -
static_cast<int>(strm.avail_out));
191bool MriMghIO::parseHeader(
const QByteArray& data,
MriVolData& volData,
bool verbose)
209 QDataStream stream(data);
210 stream.setByteOrder(QDataStream::BigEndian);
211 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
213 qint32 version, width, height, depth, nframes, type, dof;
214 stream >> version >> width >> height >> depth >> nframes >> type >> dof;
217 qCritical() <<
"MriMghIO::parseHeader - Unknown MGH version:" << version;
222 volData.
width = width;
224 volData.
depth = depth;
230 qInfo(
"MGH file: %dx%dx%d, %d frame(s), type=%d\n",
231 width, height, depth, nframes, type);
236 stream >> goodRASflag;
237 volData.
rasGood = (goodRASflag > 0);
239 if (goodRASflag > 0) {
257 qInfo(
"Voxel sizes: %.4f x %.4f x %.4f mm\n",
259 qInfo(
"goodRAS: %d\n", goodRASflag);
260 qInfo(
"c_ras: %.4f %.4f %.4f\n",
269bool MriMghIO::readVoxelData(
const QByteArray& data,
MriVolData& volData)
277 int bpv = bytesPerVoxel(volData.
type);
279 qCritical() <<
"MriMghIO::readVoxelData - Unsupported MGH data type:" << volData.
type;
283 qint64 frameSize =
static_cast<qint64
>(volData.
width) * volData.
height * volData.
depth * bpv;
285 qCritical() <<
"MriMghIO::readVoxelData - File too small for expected data size";
289 QDataStream stream(data);
290 stream.setByteOrder(QDataStream::BigEndian);
291 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
294 int nslice = volData.
depth;
296 volData.
slices.resize(nslice);
301 for (
int k = 0; k < nslice; ++k) {
309 switch (volData.
type) {
312 slice.
pixels.resize(nPixels);
313 for (
int p = 0; p < nPixels; ++p) {
324 for (
int p = 0; p < nPixels; ++p) {
327 slice.
pixelsWord[p] =
static_cast<unsigned short>(val < 0 ? 0 : val);
336 for (
int p = 0; p < nPixels; ++p) {
347 for (
int p = 0; p < nPixels; ++p) {
363 Vector3f sliceOrigin;
364 sliceOrigin(0) = vox2ras(0, 2) * k + vox2ras(0, 3);
365 sliceOrigin(1) = vox2ras(1, 2) * k + vox2ras(1, 3);
366 sliceOrigin(2) = vox2ras(2, 2) * k + vox2ras(2, 3);
369 sliceRot.col(0) = vox2ras.block<3, 1>(0, 0);
370 sliceRot.col(1) = vox2ras.block<3, 1>(0, 1);
371 sliceRot.col(2) = vox2ras.block<3, 1>(0, 2);
374 sliceMove << sliceOrigin(0), sliceOrigin(1), sliceOrigin(2);
384bool MriMghIO::parseFooter(
const QByteArray& data,
386 QVector<FiffCoordTrans>& additionalTrans,
387 const QString& subjectMriDir,
397 int bpv = bytesPerVoxel(volData.
type);
402 qint64 frameSize =
static_cast<qint64
>(volData.
width) * volData.
height * volData.
depth * bpv;
405 if (data.size() <= footerPos) {
410 QDataStream stream(data);
411 stream.setByteOrder(QDataStream::BigEndian);
412 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
413 stream.device()->seek(footerPos);
416 constexpr int kScanParamBytes = 5 *
sizeof(float);
417 qint64 remainingBytes = data.size() - footerPos;
418 if (remainingBytes >= kScanParamBytes) {
425 while (!stream.atEnd()) {
428 if (stream.atEnd())
break;
443 if (tagLen <= 0 || tagLen > data.size())
break;
445 QByteArray tagData(tagLen,
'\0');
446 if (stream.readRawData(tagData.data(), tagLen) != tagLen)
break;
450 QString xfmPath = QString::fromLatin1(tagData).trimmed();
454 qInfo(
"Found Talairach transform reference: %s\n", qPrintable(xfmPath));
458 if (!QFileInfo(xfmPath).isAbsolute() && !subjectMriDir.isEmpty()) {
459 xfmPath = subjectMriDir +
"/transforms/" + xfmPath;
462 if (QFileInfo::exists(xfmPath)) {
463 QFile xfmFile(xfmPath);
464 if (xfmFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
473 QString xfmContent = xfmFile.readAll();
476 int ltIdx = xfmContent.indexOf(
"Linear_Transform");
478 int eqIdx = xfmContent.indexOf(
'=', ltIdx);
480 QString matStr = xfmContent.mid(eqIdx + 1).trimmed();
483 QStringList vals = matStr.split(QRegularExpression(
"\\s+"),
486 if (vals.size() >= 12) {
488 Matrix4f rasMniTal = Matrix4f::Identity();
489 for (
int r = 0; r < 3; ++r) {
490 for (
int c = 0; c < 4; ++c) {
491 rasMniTal(r, c) = vals[r * 4 + c].toFloat();
496 rasMniTal(0, 3) /= 1000.0f;
497 rasMniTal(1, 3) /= 1000.0f;
498 rasMniTal(2, 3) /= 1000.0f;
505 additionalTrans.append(talTrans);
508 qInfo(
"Read Talairach transform from %s\n", qPrintable(xfmPath));
516 qWarning(
"Talairach transform file not found: %s\n", qPrintable(xfmPath));
527int MriMghIO::bytesPerVoxel(
int type)
FreeSurfer MGH / MGZ volume reader: byte-level decoder for the 284-byte fixed header,...
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_DISPLAY
FIFF tag-kind, block-kind and type-code numerical definitions, authoritative for FIFFLIB.
#define FIFFV_MRI_PIXEL_BYTE
#define FIFFV_MRI_PIXEL_FLOAT
#define FIFFV_MRI_PIXEL_WORD
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 int MRI_MGH_VERSION
constexpr int MGH_TAG_OLD_MGH_XFORM
constexpr int MRI_MGH_DATA_OFFSET
constexpr int MGH_TAG_OLD_SURF_GEOM
constexpr int MGH_TAG_MGH_XFORM
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
Format-agnostic 3D MRI volume: header geometry, voxel buffer (as a vector of MriSlice),...
FIFFLIB::FiffCoordTrans voxelSurfRasT
QVector< MriSlice > slices
Eigen::Matrix4f computeVox2Ras() const