Skip to content

Commit 0fa410c

Browse files
committed
Remove nodejs Buffer from nifti1 and nifti2 toArrayBuffer
1 parent 0e406b0 commit 0fa410c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

__tests__/nifti1.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,14 @@ describe("NIFTI-Reader-JS", function () {
6464
let cloneText = JSON.stringify(clone);
6565
expect(cloneText).to.equal(niftiHeaderText);
6666
});
67+
68+
it("description, aux_file, intent_name and magic are preserved", function () {
69+
bytes = nifti1!.toArrayBuffer();
70+
clone = readHeader(bytes);
71+
expect(clone!.description).to.equal(nifti1!.description);
72+
expect(clone!.aux_file).to.equal(nifti1!.aux_file);
73+
expect(clone!.intent_name).to.equal(nifti1!.intent_name);
74+
expect(clone!.magic).to.equal(nifti1!.magic);
75+
});
6776
});
6877
});

__tests__/nifti2-gz.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import * as fs from "fs";
1212
import { Utils } from "../src/utilities";
1313
import { NIFTI2 } from "../src/nifti2";
1414

15+
function parseJSONFromBytes(bytes: Uint8Array): any {
16+
const decoder = new TextDecoder();
17+
const jsonString = decoder.decode(bytes);
18+
return JSON.parse(jsonString);
19+
}
20+
1521
const buf = fs.readFileSync("./data/avg152T1_LR_nifti2.nii.gz");
1622
let data = Utils.toArrayBuffer(buf);
1723
let nifti2: NIFTI1 | NIFTI2 | null;
@@ -71,5 +77,14 @@ describe("NIFTI-Reader-JS", function () {
7177
let cloneText = JSON.stringify(clone);
7278
expect(cloneText).to.equal(niftiHeaderText);
7379
});
80+
81+
it("description, aux_file, intent_name and magic are preserved", function() {
82+
bytes = nifti2!.toArrayBuffer();
83+
clone = readHeader(bytes);
84+
expect(clone!.description).to.equal(nifti2!.description);
85+
expect(clone!.aux_file).to.equal(nifti2!.aux_file);
86+
expect(clone!.intent_name).to.equal(nifti2!.intent_name);
87+
expect(clone!.magic).to.equal(nifti2!.magic);
88+
});
7489
});
7590
});

src/nifti1.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,8 @@ import { Utils } from "./utilities";
12881288
// glmax, glmin are unused
12891289

12901290
// descrip and aux_file
1291-
byteArray.set(Buffer.from(this.description), 148);
1292-
byteArray.set(Buffer.from(this.aux_file), 228);
1291+
byteArray.set(new TextEncoder().encode(this.description), 148);
1292+
byteArray.set(new TextEncoder().encode(this.aux_file), 228);
12931293

12941294
// qform_code, sform_code
12951295
view.setInt16(252, this.qform_code, this.littleEndian);
@@ -1313,8 +1313,8 @@ import { Utils } from "./utilities";
13131313
}
13141314

13151315
// intent_name and magic
1316-
byteArray.set(Buffer.from(this.intent_name), 328);
1317-
byteArray.set(Buffer.from(this.magic), 344);
1316+
byteArray.set(new TextEncoder().encode(this.intent_name), 328);
1317+
byteArray.set(new TextEncoder().encode(this.magic), 344);
13181318

13191319
// add our extension data
13201320
if (includeExtensions) {

src/nifti2.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ export class NIFTI2 {
495495
view.setInt32(0, 540, this.littleEndian);
496496

497497
// magic
498-
byteArray.set(Buffer.from(this.magic), 4);
498+
byteArray.set(new TextEncoder().encode(this.magic), 4);
499499

500500
// datatype
501501
view.setInt16(12, this.datatypeCode, this.littleEndian);
@@ -558,10 +558,10 @@ export class NIFTI2 {
558558
view.setBigInt64(232, BigInt(this.slice_end), this.littleEndian);
559559

560560
// descrip
561-
byteArray.set(Buffer.from(this.description), 240);
561+
byteArray.set(new TextEncoder().encode(this.description), 240);
562562

563563
// aux_file
564-
byteArray.set(Buffer.from(this.aux_file), 320);
564+
byteArray.set(new TextEncoder().encode(this.aux_file), 320);
565565

566566
// qform_code
567567
view.setInt32(344, this.qform_code, this.littleEndian);
@@ -601,7 +601,7 @@ export class NIFTI2 {
601601
// intent_code
602602
view.setInt32(504, this.intent_code, this.littleEndian);
603603
// intent_name
604-
byteArray.set(Buffer.from(this.intent_name), 508);
604+
byteArray.set(new TextEncoder().encode(this.intent_name), 508);
605605
// dim_info
606606
view.setUint8(524, this.dim_info);
607607

0 commit comments

Comments
 (0)