Skip to content

Commit

Permalink
fixed built-in NIFTI importer: Handle byte arrays + handle import fai…
Browse files Browse the repository at this point in the history
…lure
  • Loading branch information
mlavik1 committed Aug 29, 2024
1 parent 21b7dbe commit 720dcab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Assets/3rdparty/Nifti.NET/Nifti.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public float[] ToSingleArray()
return Array.ConvertAll<short, float>(this.Data as short[], Convert.ToSingle);
else if(type == typeof(ushort))
return Array.ConvertAll<ushort, float>(this.Data as ushort[], Convert.ToSingle);
else if (type == typeof(byte))
return Array.ConvertAll<byte, float>(this.Data as byte[], Convert.ToSingle);
else
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public VolumeDataset Import(string filePath)

// Create dataset
VolumeDataset volumeDataset = ScriptableObject.CreateInstance<VolumeDataset>();
ImportInternal(volumeDataset, niftiFile, filePath);
bool succeeded = ImportInternal(volumeDataset, niftiFile, filePath);

if (!succeeded)
volumeDataset = null;

return volumeDataset;
}
Expand All @@ -55,17 +58,26 @@ public async Task<VolumeDataset> ImportAsync(string filePath)
return null;
}

await Task.Run(() => ImportInternal(volumeDataset,niftiFile,filePath));
bool succeeded = await Task.Run(() => ImportInternal(volumeDataset,niftiFile,filePath));

if (!succeeded)
volumeDataset = null;

return volumeDataset;
}
private void ImportInternal(VolumeDataset volumeDataset,Nifti.NET.Nifti niftiFile,string filePath)
private bool ImportInternal(VolumeDataset volumeDataset,Nifti.NET.Nifti niftiFile,string filePath)
{
int dimX = niftiFile.Header.dim[1];
int dimY = niftiFile.Header.dim[2];
int dimZ = niftiFile.Header.dim[3];
float[] pixelData = niftiFile.ToSingleArray();

if (pixelData == null)
{
Debug.LogError($"Failed to read data, of type: {niftiFile.Data?.GetType()}");
return false;
}

Vector3 pixdim = new Vector3(niftiFile.Header.pixdim[1], niftiFile.Header.pixdim[2], niftiFile.Header.pixdim[3]);
Vector3 size = new Vector3(dimX * pixdim.x, dimY * pixdim.y, dimZ * pixdim.z);

Expand All @@ -80,6 +92,8 @@ private void ImportInternal(VolumeDataset volumeDataset,Nifti.NET.Nifti niftiFil

volumeDataset.FixDimensions();
volumeDataset.rotation = Quaternion.Euler(90.0f, 0.0f, 0.0f);

return true;
}
}
}

0 comments on commit 720dcab

Please sign in to comment.