56#include <QRegularExpression>
80 QVector<FiffCoordTrans>& additionalTrans,
81 const QString& subjectMriDir,
87 bool isCompressed = mgzFile.endsWith(
".mgz", Qt::CaseInsensitive);
91 if (!decompress(mgzFile, fileData)) {
96 if (!file.open(QIODevice::ReadOnly)) {
97 qCritical() <<
"MriMghIO::read - Could not open" << mgzFile;
100 fileData = file.readAll();
105 qCritical() <<
"MriMghIO::read - File" << mgzFile
106 <<
"is too small to be a valid MGH file ("
107 << fileData.size() <<
"bytes)";
112 if (!parseHeader(fileData, volData, 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));
130 if (!readVoxelData(fileData, volData)) {
135 parseFooter(fileData, volData, additionalTrans, subjectMriDir, verbose);
138 printf(
"Read %d slices from %s (%dx%d pixels)\n",
139 static_cast<int>(volData.
slices.size()), qPrintable(mgzFile),
148bool MriMghIO::decompress(
const QString& mgzFile, QByteArray& rawData)
151 if (!file.open(QIODevice::ReadOnly)) {
152 qCritical() <<
"MriMghIO::decompress - Could not open" << mgzFile;
155 QByteArray compressedData = file.readAll();
158 if (compressedData.isEmpty()) {
159 qCritical() <<
"MriMghIO::decompress - File is empty:" << mgzFile;
167 int ret = inflateInit2(&strm, MAX_WBITS + 16);
169 qCritical() <<
"MriMghIO::decompress - inflateInit2 failed";
173 strm.next_in =
reinterpret_cast<Bytef*
>(compressedData.data());
174 strm.avail_in =
static_cast<uInt
>(compressedData.size());
176 const int chunkSize = 256 * 1024;
180 rawData.resize(rawData.size() + chunkSize);
181 strm.next_out =
reinterpret_cast<Bytef*
>(rawData.data() + rawData.size() - chunkSize);
182 strm.avail_out = chunkSize;
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;
191 }
while (ret != Z_STREAM_END);
194 rawData.resize(rawData.size() -
static_cast<int>(strm.avail_out));
202bool MriMghIO::parseHeader(
const QByteArray& data,
MriVolData& volData,
bool verbose)
220 QDataStream stream(data);
221 stream.setByteOrder(QDataStream::BigEndian);
222 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
224 qint32 version, width, height, depth, nframes, type, dof;
225 stream >> version >> width >> height >> depth >> nframes >> type >> dof;
228 qCritical() <<
"MriMghIO::parseHeader - Unknown MGH version:" << version;
233 volData.
width = width;
235 volData.
depth = depth;
241 printf(
"MGH file: %dx%dx%d, %d frame(s), type=%d\n",
242 width, height, depth, nframes, type);
247 stream >> goodRASflag;
248 volData.
rasGood = (goodRASflag > 0);
250 if (goodRASflag > 0) {
268 printf(
"Voxel sizes: %.4f x %.4f x %.4f mm\n",
270 printf(
"goodRAS: %d\n", goodRASflag);
271 printf(
"c_ras: %.4f %.4f %.4f\n",
280bool MriMghIO::readVoxelData(
const QByteArray& data,
MriVolData& volData)
288 int bpv = bytesPerVoxel(volData.
type);
290 qCritical() <<
"MriMghIO::readVoxelData - Unsupported MGH data type:" << volData.
type;
294 qint64 frameSize =
static_cast<qint64
>(volData.
width) * volData.
height * volData.
depth * bpv;
296 qCritical() <<
"MriMghIO::readVoxelData - File too small for expected data size";
300 QDataStream stream(data);
301 stream.setByteOrder(QDataStream::BigEndian);
302 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
305 int nslice = volData.
depth;
307 volData.
slices.resize(nslice);
312 for (
int k = 0; k < nslice; ++k) {
313 MriSlice& slice = volData.
slices[k];
320 switch (volData.
type) {
323 slice.
pixels.resize(nPixels);
324 for (
int p = 0; p < nPixels; ++p) {
335 for (
int p = 0; p < nPixels; ++p) {
338 slice.
pixelsWord[p] =
static_cast<unsigned short>(val < 0 ? 0 : val);
347 for (
int p = 0; p < nPixels; ++p) {
358 for (
int p = 0; p < nPixels; ++p) {
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);
380 sliceRot.col(0) = vox2ras.block<3, 1>(0, 0);
381 sliceRot.col(1) = vox2ras.block<3, 1>(0, 1);
382 sliceRot.col(2) = vox2ras.block<3, 1>(0, 2);
385 sliceMove << sliceOrigin(0), sliceOrigin(1), sliceOrigin(2);
395bool MriMghIO::parseFooter(
const QByteArray& data,
397 QVector<FiffCoordTrans>& additionalTrans,
398 const QString& subjectMriDir,
408 int bpv = bytesPerVoxel(volData.
type);
413 qint64 frameSize =
static_cast<qint64
>(volData.
width) * volData.
height * volData.
depth * bpv;
416 if (data.size() <= footerPos) {
421 QDataStream stream(data);
422 stream.setByteOrder(QDataStream::BigEndian);
423 stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
424 stream.device()->seek(footerPos);
427 constexpr int kScanParamBytes = 5 *
sizeof(float);
428 qint64 remainingBytes = data.size() - footerPos;
429 if (remainingBytes >= kScanParamBytes) {
436 while (!stream.atEnd()) {
439 if (stream.atEnd())
break;
454 if (tagLen <= 0 || tagLen > data.size())
break;
456 QByteArray tagData(tagLen,
'\0');
457 if (stream.readRawData(tagData.data(), tagLen) != tagLen)
break;
461 QString xfmPath = QString::fromLatin1(tagData).trimmed();
465 printf(
"Found Talairach transform reference: %s\n", qPrintable(xfmPath));
469 if (!QFileInfo(xfmPath).isAbsolute() && !subjectMriDir.isEmpty()) {
470 xfmPath = subjectMriDir +
"/transforms/" + xfmPath;
473 if (QFileInfo::exists(xfmPath)) {
474 QFile xfmFile(xfmPath);
475 if (xfmFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
484 QString xfmContent = xfmFile.readAll();
487 int ltIdx = xfmContent.indexOf(
"Linear_Transform");
489 int eqIdx = xfmContent.indexOf(
'=', ltIdx);
491 QString matStr = xfmContent.mid(eqIdx + 1).trimmed();
494 QStringList vals = matStr.split(QRegularExpression(
"\\s+"),
497 if (vals.size() >= 12) {
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();
507 rasMniTal(0, 3) /= 1000.0f;
508 rasMniTal(1, 3) /= 1000.0f;
509 rasMniTal(2, 3) /= 1000.0f;
512 FiffCoordTrans talTrans(
516 additionalTrans.append(talTrans);
519 printf(
"Read Talairach transform from %s\n", qPrintable(xfmPath));
527 printf(
"Talairach transform file not found: %s\n", qPrintable(xfmPath));
538int MriMghIO::bytesPerVoxel(
int type)
#define FIFFV_COORD_MRI_SLICE
#define FIFFV_COORD_MRI_DISPLAY
Header file describing the numerical values used in fif files.
#define FIFFV_MRI_PIXEL_BYTE
#define FIFFV_MRI_PIXEL_FLOAT
#define FIFFV_MRI_PIXEL_WORD
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
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
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
QVector< MriSlice > slices
Eigen::Matrix4f computeVox2Ras() const