@niivue/nv-ext-dcm2niix 1.0.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # @niivue/nv-ext-dcm2niix
2
+
3
+ Browser-side DICOM-to-NIfTI conversion for [NiiVue](https://github.com/niivue), wrapping the [`@niivue/dcm2niix`](https://www.npmjs.com/package/@niivue/dcm2niix) WebAssembly build of Chris Rorden's [`dcm2niix`](https://github.com/rordenlab/dcm2niix). No data leaves the browser.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @niivue/nv-ext-dcm2niix
9
+ ```
10
+
11
+ Requires `@niivue/niivue` as a peer dependency.
12
+
13
+ ## Usage
14
+
15
+ ### Folder picker
16
+
17
+ ```ts
18
+ import NiiVue from '@niivue/niivue'
19
+ import { runDcm2niix } from '@niivue/nv-ext-dcm2niix'
20
+
21
+ const nv = new NiiVue()
22
+ await nv.attachTo('gl1')
23
+
24
+ const input = document.querySelector<HTMLInputElement>('#dicomFolder')!
25
+ input.addEventListener('change', async () => {
26
+ const niftiFiles = await runDcm2niix(input.files)
27
+ await nv.loadVolumes([{ url: niftiFiles[0] }])
28
+ })
29
+ ```
30
+
31
+ The input should declare `webkitdirectory` so the browser exposes folder structure:
32
+
33
+ ```html
34
+ <input id="dicomFolder" type="file" webkitdirectory multiple />
35
+ ```
36
+
37
+ ### Drag-and-drop folders
38
+
39
+ ```ts
40
+ import { runDcm2niix, traverseDataTransferItems } from '@niivue/nv-ext-dcm2niix'
41
+
42
+ dropTarget.addEventListener('drop', async (e) => {
43
+ e.preventDefault()
44
+ const files = await traverseDataTransferItems(e.dataTransfer!.items)
45
+ const niftiFiles = await runDcm2niix(files)
46
+ await nv.loadVolumes([{ url: niftiFiles[0] }])
47
+ })
48
+ ```
49
+
50
+ ### Power users
51
+
52
+ The underlying `Dcm2niix` class is re-exported for callers that need full control over dcm2niix command-line flags (compression level, BIDS sidecars, etc.):
53
+
54
+ ```ts
55
+ import { Dcm2niix } from '@niivue/nv-ext-dcm2niix'
56
+
57
+ const dcm2niix = new Dcm2niix()
58
+ await dcm2niix.init()
59
+ const result = await dcm2niix.input(files).compressionLevel(9).bids('n').run()
60
+ ```
61
+
62
+ ## Development
63
+
64
+ ```bash
65
+ bunx nx build nv-ext-dcm2niix
66
+ bunx nx typecheck nv-ext-dcm2niix
67
+ ```
68
+
69
+ ## Part of the [NiiVue](https://github.com/niivue) ecosystem
@@ -0,0 +1,43 @@
1
+ import { Dcm2niix } from '@niivue/dcm2niix';
2
+ export { Dcm2niix };
3
+ /** Options for {@link runDcm2niix}. */
4
+ export interface RunDcm2niixOptions {
5
+ /**
6
+ * Filter the result list down to NIfTI outputs (`.nii` and `.nii.gz`).
7
+ * BIDS sidecars and other dcm2niix outputs are dropped. Default: `true`.
8
+ */
9
+ niftiOnly?: boolean;
10
+ }
11
+ /**
12
+ * Convert DICOM files to NIfTI by spinning up a fresh dcm2niix worker,
13
+ * feeding it the files, waiting for the result, then terminating the
14
+ * worker so the WASM heap is released.
15
+ *
16
+ * Each call boots its own worker — fine for one-off conversions; for
17
+ * batch workflows, instantiate `Dcm2niix` once and reuse it (and call
18
+ * `worker?.terminate()` yourself when finished).
19
+ *
20
+ * @param files FileList from `<input webkitdirectory>` or File[]
21
+ * from a drop event (see {@link traverseDataTransferItems}).
22
+ * @param options See {@link RunDcm2niixOptions}.
23
+ * @returns Converted output files (NIfTI by default).
24
+ */
25
+ export declare function runDcm2niix(files: FileList | File[] | null | undefined, options?: RunDcm2niixOptions): Promise<File[]>;
26
+ /**
27
+ * Walk a drop event's `DataTransferItemList`, recurse into directories,
28
+ * and stamp `_webkitRelativePath` on each file so dcm2niix can group by
29
+ * series.
30
+ *
31
+ * The browser exposes folder structure on drop only via the
32
+ * `webkitGetAsEntry()` API; this helper runs that traversal for you.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * dropTarget.addEventListener('drop', async (e) => {
37
+ * e.preventDefault()
38
+ * const files = await traverseDataTransferItems(e.dataTransfer!.items)
39
+ * const niftiFiles = await runDcm2niix(files)
40
+ * })
41
+ * ```
42
+ */
43
+ export declare function traverseDataTransferItems(items: DataTransferItemList): Promise<File[]>;
@@ -0,0 +1,53 @@
1
+ import { Dcm2niix as f } from "@niivue/dcm2niix";
2
+ import { Dcm2niix as g } from "@niivue/dcm2niix";
3
+ async function h(n, r = {}) {
4
+ var i;
5
+ const { niftiOnly: e = !0 } = r;
6
+ if (!n || n.length === 0) return [];
7
+ const t = new f();
8
+ try {
9
+ await t.init();
10
+ const a = await t.input(n).run();
11
+ return e ? a.filter((s) => /\.nii(\.gz)?$/i.test(s.name)) : a;
12
+ } finally {
13
+ (i = t.worker) == null || i.terminate();
14
+ }
15
+ }
16
+ async function w(n) {
17
+ const r = [], e = [];
18
+ for (let t = 0; t < n.length; t++) {
19
+ const i = n[t].webkitGetAsEntry();
20
+ i && e.push(i);
21
+ }
22
+ return await Promise.all(e.map((t) => m(t, "", r))), r;
23
+ }
24
+ function m(n, r, e) {
25
+ return new Promise((t, i) => {
26
+ if (n.isFile) {
27
+ n.file((a) => {
28
+ const s = a;
29
+ s._webkitRelativePath = r + a.name, e.push(s), t();
30
+ }, i);
31
+ return;
32
+ }
33
+ if (n.isDirectory) {
34
+ const a = n.createReader(), s = `${r}${n.name}/`, c = () => {
35
+ a.readEntries((o) => {
36
+ if (o.length === 0) {
37
+ t();
38
+ return;
39
+ }
40
+ Promise.all(o.map((u) => m(u, s, e))).then(c).catch(i);
41
+ }, i);
42
+ };
43
+ c();
44
+ return;
45
+ }
46
+ t();
47
+ });
48
+ }
49
+ export {
50
+ g as Dcm2niix,
51
+ h as runDcm2niix,
52
+ w as traverseDataTransferItems
53
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@niivue/nv-ext-dcm2niix",
3
+ "version": "1.0.0-rc.10",
4
+ "type": "module",
5
+ "description": "DICOM-to-NIfTI conversion in the browser via the dcm2niix WASM build, packaged for NiiVue",
6
+ "main": "./dist/nv-ext-dcm2niix.js",
7
+ "module": "./dist/nv-ext-dcm2niix.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/nv-ext-dcm2niix.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "peerDependencies": {
19
+ "@niivue/niivue": "1.0.0-rc.12"
20
+ },
21
+ "dependencies": {
22
+ "@niivue/dcm2niix": "^1.2.0"
23
+ },
24
+ "devDependencies": {
25
+ "@niivue/niivue": "1.0.0-rc.12",
26
+ "typescript": "~5.8.3",
27
+ "vite": "^6.3.2",
28
+ "vite-plugin-dts": "^4.5.4"
29
+ },
30
+ "scripts": {
31
+ "build": "npx vite build",
32
+ "typecheck": "bunx tsc --noEmit"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ }
37
+ }