# vtkITKImageReader on NIFTI (.nii) file format

**URL:** https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800
**Category:** Web
**Created:** [October 11, 2021, 4:24pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800 "2021-10-11T16:24:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anonymous\_iguana](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anonymous_iguana/32/3997_2.png) [@anonymous\_iguana](https://discourse.vtk.org/u/anonymous_iguana)
#### Post date: [October 11, 2021, 4:24pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/1 "2021-10-11T16:24:39Z")

</div>

Hi there,

I’m attempting to load a basic NIFTI file into vtk.js and running into some issues. I have read the .nii file into an Array Buffer (`res` in the excerpts below) and am unable to render it. In both these attempts, the result of `niftiReader.parseAsArrayBuffer(res)` then `niftiReader.getOutputData()` results in `undefined`.

Documentation for the vtkITKImageReader is a little sparse, so I have been using the following resources, but none of which are particularly up to date. I am using vtk.js v17.17.2.

- This [demo](https://glitch.com/edit/#!/mh-imagemapper-tst?path=src%2Findex.js%3A70%3A20) has a working example of loading a .nrrd file using the vtkITKImageReader I am also attempting to use, but they are using vtk.js v13.4.2
- This [forum](https://discourse.vtk.org/t/models-and-nrrd-files-exported-from-slicer-dont-align-in-vtkjs-itk/2440/11) is where the above demo originated from
- This [forum](https://github.com/Kitware/vtk-js/issues/700) where it is mentioned that setting the file name is important but it’s not clear to me how/why this is the case.

```auto
import readImageArrayBuffer from "itk/readImageArrayBuffer";
import vtkITKImageReader from "@kitware/vtk.js/IO/Misc/ITKImageReader";
... // all other imports required

vtkITKImageReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);
const niftiReader = vtkITKImageReader.newInstance();
const mapperNifti = vtkImageMapper.newInstance();
const actorNifti = vtkImageSlice.newInstance();

niftiReader.parseAsArrayBuffer(res);
const source = niftiReader.getOutputData();
renderer.addActor(actorNifti);
actorNifti.setMapper(mapperNifti);
mapperNifti.setInputData(source);

renderWindow.render();

```

This results in a blank black window being rendered, while `source` is `undefined`.

Based on the working demo above, I have also attempted the following assuming that `.parseAsArrayBuffer()` returns a Promise:

```auto
vtkITKImageReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);
const niftiReader = vtkITKImageReader.newInstance();
const mapperNifti = vtkImageMapper.newInstance();
const actorNifti = vtkImageSlice.newInstance();

niftiReader.setFileName("image.nii");
niftiReader.parseAsArrayBuffer(res).then(() => {
  const source = niftiReader.getOutputData();
  renderer.addActor(actorNifti);
  actorNifti.setMapper(mapperNifti);
  mapperNifti.setInputData(source);

  renderWindow.render();
});

```

This does not appear to ever go into the Promise callback, so I suspect that `.parseAsArrayBuffer()` no longer returns a promise…

So far, no luck! Can anyone point me in the right direction? Thanks!

---

<div class="post-metadata">

### Author: ![Donny\_Zimmerman](https://discourse.vtk.org/user_avatar/discourse.vtk.org/donny_zimmerman/32/301_2.png) [@Donny\_Zimmerman](https://discourse.vtk.org/u/Donny_Zimmerman)
#### Post date: [October 11, 2021, 6:34pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/2 "2021-10-11T18:34:24Z")

</div>

> [@anonymous\_iguana](#):
>
> `vtkITKImageReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);`

Should be:

> [@anonymous\_iguana](#):
>
> `niftiReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);`

Just a guess?

---

<div class="post-metadata">

### Author: ![anonymous\_iguana](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anonymous_iguana/32/3997_2.png) [@anonymous\_iguana](https://discourse.vtk.org/u/anonymous_iguana)
#### Post date: [October 11, 2021, 7:07pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/3 "2021-10-11T19:07:16Z")

</div>

Hi Donny,

Thanks for the response! I gave that a shot, but `niftiReader` is an instance of `vtkITKImageReader` and the `setReadImageArrayBufferFromITK` method is not defined for it. The [vtkITKImageReader class](https://kitware.github.io/vtk-js/api/IO_Misc_ITKImageReader.html) has that method defined for the class overall, but the actual instances themselves have methods like `.getOutputData()`…which seems to stuck as undefined for me right now.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/1bd81ca10780a36d6a5a2f087ec49b14fd1a8fcc.png)

Thanks for taking a crack at it…anything else is greatly appreciated!

---

<div class="post-metadata">

### Author: ![anonymous\_iguana](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anonymous_iguana/32/3997_2.png) [@anonymous\_iguana](https://discourse.vtk.org/u/anonymous_iguana)
#### Post date: [October 12, 2021, 5:52pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/4 "2021-10-12T17:52:32Z")

</div>

I’m updating this with the (unrelated) cause of this problem. I traced the call to the [webWorkerPromise called here](https://github.com/InsightSoftwareConsortium/itk-js/blob/master/src/readImageArrayBuffer.js#L8) that was never being returned. That led me to look into the itk.js setup more carefully and I realized that I had not configured [webpack correctly per instructions here](https://insightsoftwareconsortium.github.io/itk-js/examples/webpack.html) so that the WebWorkers were available. However, since my app is based on Create React App, the webpack configs were not immediately changeable and I ended up using [craco-itk](https://github.com/InsightSoftwareConsortium/craco-itk) so that I could use the WebWorkers required for reading in the .nii format files. I will also mention that there is [Create ITK App](https://github.com/InsightSoftwareConsortium/create-itk-app) for bootstraping a React App, though it does use [CRACO](https://github.com/gsoft-inc/craco), so it’s not “100% CRA” but also not ejected.

All in all, the vtkITKImageReader was a bit of a red-herring but hopefully this can help someone in the future.

---

<div class="post-metadata">

### Author: ![vax](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/v/d78d45/32.png) [@vax](https://discourse.vtk.org/u/vax)
#### Post date: [August 28, 2023, 8:51am UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/5 "2023-08-28T08:51:35Z")

</div>

Hi,

I know it’s been a while since this answer was posted. However, I’m facing a similar problem. Since most of the pages linked in the reply no longer exist I’m unable to figure out the cause. Also while setting up the craco-itk, running npm start results in failure to compile web pack.

Could you elaborate on how you solved the problem. Thanks

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [August 28, 2023, 1:13pm UTC](https://discourse.vtk.org/t/vtkitkimagereader-on-nifti-nii-file-format/6800/6 "2023-08-28T13:13:34Z")

</div>

You might want to consider using [ITK.wasm](https://wasm.itk.org/en/latest/)
