Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nodejs Buffer from nifti1 and nifti2 toArrayBuffer #49

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/nifti1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,14 @@ describe("NIFTI-Reader-JS", function () {
let cloneText = JSON.stringify(clone);
expect(cloneText).to.equal(niftiHeaderText);
});

it("description, aux_file, intent_name and magic are preserved", function () {
bytes = nifti1!.toArrayBuffer();
clone = readHeader(bytes);
expect(clone!.description).to.equal(nifti1!.description);
expect(clone!.aux_file).to.equal(nifti1!.aux_file);
expect(clone!.intent_name).to.equal(nifti1!.intent_name);
expect(clone!.magic).to.equal(nifti1!.magic);
});
});
});
15 changes: 15 additions & 0 deletions __tests__/nifti2-gz.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import * as fs from "fs";
import { Utils } from "../src/utilities";
import { NIFTI2 } from "../src/nifti2";

function parseJSONFromBytes(bytes: Uint8Array): any {
const decoder = new TextDecoder();
const jsonString = decoder.decode(bytes);
return JSON.parse(jsonString);
}

const buf = fs.readFileSync("./data/avg152T1_LR_nifti2.nii.gz");
let data = Utils.toArrayBuffer(buf);
let nifti2: NIFTI1 | NIFTI2 | null;
Expand Down Expand Up @@ -71,5 +77,14 @@ describe("NIFTI-Reader-JS", function () {
let cloneText = JSON.stringify(clone);
expect(cloneText).to.equal(niftiHeaderText);
});

it("description, aux_file, intent_name and magic are preserved", function() {
bytes = nifti2!.toArrayBuffer();
clone = readHeader(bytes);
expect(clone!.description).to.equal(nifti2!.description);
expect(clone!.aux_file).to.equal(nifti2!.aux_file);
expect(clone!.intent_name).to.equal(nifti2!.intent_name);
expect(clone!.magic).to.equal(nifti2!.magic);
});
});
});
8 changes: 4 additions & 4 deletions src/nifti1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,8 @@ import { Utils } from "./utilities";
// glmax, glmin are unused

// descrip and aux_file
byteArray.set(Buffer.from(this.description), 148);
byteArray.set(Buffer.from(this.aux_file), 228);
byteArray.set(new TextEncoder().encode(this.description), 148);
byteArray.set(new TextEncoder().encode(this.aux_file), 228);

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

// intent_name and magic
byteArray.set(Buffer.from(this.intent_name), 328);
byteArray.set(Buffer.from(this.magic), 344);
byteArray.set(new TextEncoder().encode(this.intent_name), 328);
byteArray.set(new TextEncoder().encode(this.magic), 344);

// add our extension data
if (includeExtensions) {
Expand Down
8 changes: 4 additions & 4 deletions src/nifti2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class NIFTI2 {
view.setInt32(0, 540, this.littleEndian);

// magic
byteArray.set(Buffer.from(this.magic), 4);
byteArray.set(new TextEncoder().encode(this.magic), 4);

// datatype
view.setInt16(12, this.datatypeCode, this.littleEndian);
Expand Down Expand Up @@ -558,10 +558,10 @@ export class NIFTI2 {
view.setBigInt64(232, BigInt(this.slice_end), this.littleEndian);

// descrip
byteArray.set(Buffer.from(this.description), 240);
byteArray.set(new TextEncoder().encode(this.description), 240);

// aux_file
byteArray.set(Buffer.from(this.aux_file), 320);
byteArray.set(new TextEncoder().encode(this.aux_file), 320);

// qform_code
view.setInt32(344, this.qform_code, this.littleEndian);
Expand Down Expand Up @@ -601,7 +601,7 @@ export class NIFTI2 {
// intent_code
view.setInt32(504, this.intent_code, this.littleEndian);
// intent_name
byteArray.set(Buffer.from(this.intent_name), 508);
byteArray.set(new TextEncoder().encode(this.intent_name), 508);
// dim_info
view.setUint8(524, this.dim_info);

Expand Down