Skip to content

Commit f9d2ade

Browse files
committed
Comprehensive fix (#46)
1 parent 8147c42 commit f9d2ade

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

dist/src/nifti2.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/nifti2.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/utilities.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export declare class Utils {
1212
static getIntAt(data: DataView, start: number, littleEndian: boolean): number;
1313
static getFloatAt(data: DataView, start: number, littleEndian: boolean): number;
1414
static getDoubleAt(data: DataView, start: number, littleEndian: boolean): number;
15-
static getLongAt(data: DataView, start: number, littleEndian: boolean): number;
15+
static getInt64At(dataView: DataView, index: number, littleEndian: boolean): number;
1616
static getExtensionsAt(data: DataView, start: number, littleEndian: boolean, voxOffset: number): NIFTIEXTENSION[];
1717
static toArrayBuffer(buffer: TypedArray): ArrayBuffer;
1818
static isString(obj: Object): boolean;

dist/src/utilities.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/utilities.js

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/utilities.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nifti2.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class NIFTI2 {
129129

130130
for (ctr = 0; ctr < 8; ctr += 1) {
131131
index = 16 + ctr * 8;
132-
this.dims[ctr] = Utils.getLongAt(rawData, index, this.littleEndian);
132+
this.dims[ctr] = Utils.getInt64At(rawData, index, this.littleEndian);
133133
}
134134

135135
this.intent_p1 = Utils.getDoubleAt(rawData, 80, this.littleEndian);
@@ -141,7 +141,7 @@ export class NIFTI2 {
141141
this.pixDims[ctr] = Utils.getDoubleAt(rawData, index, this.littleEndian);
142142
}
143143

144-
this.vox_offset = Utils.getLongAt(rawData, 168, this.littleEndian);
144+
this.vox_offset = Utils.getInt64At(rawData, 168, this.littleEndian);
145145

146146
this.scl_slope = Utils.getDoubleAt(rawData, 176, this.littleEndian);
147147
this.scl_inter = Utils.getDoubleAt(rawData, 184, this.littleEndian);
@@ -153,8 +153,8 @@ export class NIFTI2 {
153153

154154
this.toffset = Utils.getDoubleAt(rawData, 216, this.littleEndian);
155155

156-
this.slice_start = Utils.getLongAt(rawData, 224, this.littleEndian);
157-
this.slice_end = Utils.getLongAt(rawData, 232, this.littleEndian);
156+
this.slice_start = Utils.getInt64At(rawData, 224, this.littleEndian);
157+
this.slice_end = Utils.getInt64At(rawData, 232, this.littleEndian);
158158

159159
this.description = Utils.getStringAt(rawData, 240, 240 + 80);
160160
this.aux_file = Utils.getStringAt(rawData, 320, 320 + 24);

src/utilities.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ export class Utils {
6464
return data.getFloat64(start, littleEndian);
6565
}
6666

67-
static getLongAt(data: DataView, start: number, littleEndian: boolean) {
68-
var ctr,
69-
array = [],
70-
value = 0;
71-
72-
for (ctr = 0; ctr < 8; ctr += 1) {
73-
array[ctr] = Utils.getByteAt(data, start + ctr);
67+
static getInt64At(dataView: DataView, index: number, littleEndian: boolean): number {
68+
const low = dataView.getUint32(index, littleEndian);
69+
const high = dataView.getInt32(index + 4, littleEndian);
70+
let result: number;
71+
if (littleEndian) {
72+
result = high * 2 ** 32 + low;
73+
} else {
74+
result = low * 2 ** 32 + high;
7475
}
75-
76-
for (ctr = array.length - 1; ctr >= 0; ctr--) {
77-
value = value * 256 + array[ctr];
76+
// Proper sign extension if the high part is negative
77+
if (high < 0) {
78+
result += -1 * 2 ** 32 * 2 ** 32;
7879
}
79-
80-
return value;
80+
return result;
8181
}
8282

8383
static getExtensionsAt(

0 commit comments

Comments
 (0)