-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from cornerstonejs/fix-float-nan-values
fix: invalid float values(IEEE-754) to be valid js values
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { external } from '../externalModules.js'; | ||
|
||
const nifti = external.niftiReader; | ||
|
||
/** | ||
* It will normalize NaN or (-)Infinity values to +MAX_VALUE, -MAX_VALUE or 0. | ||
* It mutates given param | ||
* | ||
* @param {TypedArray} imageData | ||
* @return {TypedArray} Modified imageData | ||
*/ | ||
const normalizeInvalidFloat = (imageData) => { | ||
for (let it = 0; it < imageData.length; it++) { | ||
if (isNaN(imageData[it])) { | ||
// defaults to 0 | ||
imageData[it] = 0; | ||
} else if (!isFinite(imageData[it])) { | ||
// using the maximum/minimum value instead of infinity | ||
imageData[it] = Math.sign(imageData[it]) * Number.MAX_VALUE; | ||
} | ||
} | ||
|
||
return imageData; | ||
}; | ||
|
||
/** | ||
* Normalize invalid data. Applied to float data only | ||
* It mutates given imageData | ||
* @param {string} datatypeCode | ||
* @param {TypedArray} imageData | ||
* @return {TypedArray} normalized imageData | ||
*/ | ||
export default function normalizeInvalid (datatypeCode, imageData) { | ||
|
||
switch (datatypeCode) { | ||
case nifti.NIFTI1.TYPE_FLOAT32: | ||
case nifti.NIFTI1.TYPE_FLOAT64: | ||
return normalizeInvalidFloat(imageData); | ||
} | ||
|
||
// return not normalized data | ||
return imageData; | ||
} |