Skip to content

Commit 720dcab

Browse files
committed
fixed built-in NIFTI importer: Handle byte arrays + handle import failure
1 parent 21b7dbe commit 720dcab

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Assets/3rdparty/Nifti.NET/Nifti.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public float[] ToSingleArray()
135135
return Array.ConvertAll<short, float>(this.Data as short[], Convert.ToSingle);
136136
else if(type == typeof(ushort))
137137
return Array.ConvertAll<ushort, float>(this.Data as ushort[], Convert.ToSingle);
138+
else if (type == typeof(byte))
139+
return Array.ConvertAll<byte, float>(this.Data as byte[], Convert.ToSingle);
138140
else
139141
return null;
140142
}

Assets/Scripts/Importing/ImageFileImporter/Nifti.NET/NiftiImporter.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public VolumeDataset Import(string filePath)
3030

3131
// Create dataset
3232
VolumeDataset volumeDataset = ScriptableObject.CreateInstance<VolumeDataset>();
33-
ImportInternal(volumeDataset, niftiFile, filePath);
33+
bool succeeded = ImportInternal(volumeDataset, niftiFile, filePath);
34+
35+
if (!succeeded)
36+
volumeDataset = null;
3437

3538
return volumeDataset;
3639
}
@@ -55,17 +58,26 @@ public async Task<VolumeDataset> ImportAsync(string filePath)
5558
return null;
5659
}
5760

58-
await Task.Run(() => ImportInternal(volumeDataset,niftiFile,filePath));
61+
bool succeeded = await Task.Run(() => ImportInternal(volumeDataset,niftiFile,filePath));
62+
63+
if (!succeeded)
64+
volumeDataset = null;
5965

6066
return volumeDataset;
6167
}
62-
private void ImportInternal(VolumeDataset volumeDataset,Nifti.NET.Nifti niftiFile,string filePath)
68+
private bool ImportInternal(VolumeDataset volumeDataset,Nifti.NET.Nifti niftiFile,string filePath)
6369
{
6470
int dimX = niftiFile.Header.dim[1];
6571
int dimY = niftiFile.Header.dim[2];
6672
int dimZ = niftiFile.Header.dim[3];
6773
float[] pixelData = niftiFile.ToSingleArray();
6874

75+
if (pixelData == null)
76+
{
77+
Debug.LogError($"Failed to read data, of type: {niftiFile.Data?.GetType()}");
78+
return false;
79+
}
80+
6981
Vector3 pixdim = new Vector3(niftiFile.Header.pixdim[1], niftiFile.Header.pixdim[2], niftiFile.Header.pixdim[3]);
7082
Vector3 size = new Vector3(dimX * pixdim.x, dimY * pixdim.y, dimZ * pixdim.z);
7183

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

8193
volumeDataset.FixDimensions();
8294
volumeDataset.rotation = Quaternion.Euler(90.0f, 0.0f, 0.0f);
95+
96+
return true;
8397
}
8498
}
8599
}

0 commit comments

Comments
 (0)