InsightSoftwareConsortium / ITK-Wasm

High performance spatial analysis in a web browser and across programming languages and hardware architectures
https://wasm.itk.org
Apache License 2.0
199 stars 52 forks source link

IWI sample image data buffers #1239

Open neurolabusc opened 2 months ago

neurolabusc commented 2 months ago

I was able to create sensible IWI images with Python:

import itk

image = itk.imread('fslmean.nii.gz')
itk.imwrite(image, 'fslmean.iwi.cbor')

However, the validation itk-wasm test datasets seem odd, specifically

When I decode each, the data.buffer.bytelength is larger than the data.byteLength, with the latter being the number of bytes described by the header (while the former is >200 bytes too large). While padding datasets for byte alignment seems understandable, the strange thing is the the padding is at the start of the array, not the end. This makes fast reading of byte data as Uint8Array regardless of datatype (e.g. Float32Array, etc) ungainly. My best guess is these validation versions were created by beta software and should be updated to reflect the standard, but it might be worth understanding the origin of this discrepancy.

import { decode } from 'cbor-x'

export function iwi2nii(arrayBuffer) {
    // decode from cbor to JS object
    let iwi = decode(new Uint8Array(arrayBuffer))
    if (iwi.data.buffer.byteLength > iwi.data.byteLength) {
      console.log(`!buffer larger than data ${iwi.data.buffer.byteLength} vs ${iwi.data.byteLength} bytes`)
    }
    // note padding at the start!
    const img8 = new Uint8Array(iwi.data.buffer, iwi.data.buffer.byteLength-iwi.data.byteLength, iwi.data.byteLength)
    // this also works, but only if data is Uint8Array, not Float32Array:
    const imgOK = new Uint8Array(iwi.data.slice())
}
neurolabusc commented 2 months ago

The best universal solution I have found is to use byteOffset, but I am not sure why this is not zero in these validation datasets:

    const img8 = new Uint8Array(iwi.data.buffer, iwi.data.byteOffset, iwi.data.byteLength)
thewtex commented 2 months ago

My best guess is these validation versions were created by beta software and should be updated to reflect the standard, but it might be worth understanding the origin of this discrepancy.

Yes, I expect this also.