rii-mango / NIFTI-Reader-JS

A JavaScript NIfTI file format reader.
MIT License
137 stars 30 forks source link

Nifti Writer #4

Closed NicolasRannou closed 1 year ago

NicolasRannou commented 8 years ago

Is there a way to export some data as NIFTI using this library?

A use case it to generate a label-map/segmentation in JavaScript then being able to save it as a NIFTI file.

Thanks

rii-mango commented 8 years ago

Currently it only supports reading, but there's no reason it can't also support writing NIFTI data at least in a Node.js environment. I'll keep this issue open as a feature request.

NicolasRannou commented 8 years ago

Yes thanks - Ideally it could generate the "right" bytestream and we could just save it out without nodejs!

DLinRadiology commented 5 years ago

I am also very interested in creating a web-based segmentation application and am looking for resources. Any news on this matter?

SachidanandAlle commented 4 years ago

Currently it only supports reading, but there's no reason it can't also support writing NIFTI data at least in a Node.js environment. I'll keep this issue open as a feature request.

Any updates on this?

SachidanandAlle commented 4 years ago

Yes thanks - Ideally it could generate the "right" bytestream and we could just save it out without nodejs!

any example to generate the right bytestream (serialize) the nifti header+data?

DLinRadiology commented 4 years ago

We are creating an online segmentation tool (MedSeg) and have a working solution for saving nifti masks, below is our code. It does save working nifti files where the header is copied from the user's loaded nifti file.

I would love it to save it as gzip though (.nii.gz), not sure how to do that. Anyone who has an idea on how it could be done? adding data = nifti.compress(data) or variants with compressing the blob does not work...

Code to get it to save a .nii:

function save_nifti(ctx){ var saveData = (function () { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; return function (data, fileName) { var blob = new Blob(data); var url = window.URL.createObjectURL(blob); a.href = url; a.download = fileName; a.click(); window.URL.revokeObjectURL(url); }; }()); var data = [new Uint8Array(image_nii_header,0,image_nii_header.length), new Uint8Array(ctx.maskImage,0,ctx.maskImage.length)]; fileName = "mask"+thectx.fileName+".nii"; saveData(data, fileName); }

xgui3783 commented 4 years ago

For gzipping data in js, you might want to take a look at https://github.com/nodeca/pako

DLinRadiology commented 4 years ago

Thanks, but that's exactly what nifti.compress(data) does (uses pako):

nifti.compress = function (data) { return pako.deflate(data); };

This function works, but even though it compresses, something happens that I don't understand with the nifti and it becomes a non-usable file even though it is small (expected size result, not too small either).

DLinRadiology commented 4 years ago

Finally figured it out!

Had to add a gzip wrapper, so in nifti-reader.js I changed to:

pako.deflate(data, {gzip: true})

Maybe this helps someone :)

neurolabusc commented 1 year ago

@DLinRadiology as you note, this library supports writing NIfTIs. For a complex but comprehensive example, see the NiiVue drawing demo that includes a Save Drawing item that writes a NIfTI file.