ami-cjs.js
Version:
<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>
311 lines (260 loc) • 12 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _parsers = require('./parsers.volume');
var _parsers2 = _interopRequireDefault(_parsers);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // use nifti-js and just parse header.???
// Slicer way to handle images
// should follow it...
// 897 if ( (this->IndexSeriesInstanceUIDs[k] != idxSeriesInstanceUID && this->IndexSeriesInstanceUIDs[k] >= 0 && idxSeriesInstanceUID >= 0) ||
// 898 (this->IndexContentTime[k] != idxContentTime && this->IndexContentTime[k] >= 0 && idxContentTime >= 0) ||
// 899 (this->IndexTriggerTime[k] != idxTriggerTime && this->IndexTriggerTime[k] >= 0 && idxTriggerTime >= 0) ||
// 900 (this->IndexEchoNumbers[k] != idxEchoNumbers && this->IndexEchoNumbers[k] >= 0 && idxEchoNumbers >= 0) ||
// 901 (this->IndexDiffusionGradientOrientation[k] != idxDiffusionGradientOrientation && this->IndexDiffusionGradientOrientation[k] >= 0 && idxDiffusionGradientOrientation >= 0) ||
// 902 (this->IndexSliceLocation[k] != idxSliceLocation && this->IndexSliceLocation[k] >= 0 && idxSliceLocation >= 0) ||
// 903 (this->IndexImageOrientationPatient[k] != idxImageOrientationPatient && this->IndexImageOrientationPatient[k] >= 0 && idxImageOrientationPatient >= 0) )
// 904 {
// 905 continue;
// 906 }
// http://brainder.org/2012/09/23/the-nifti-file-format/
/** * Imports ***/
var pako = require('pako');
var NrrdReader = require('nrrd-js');
/**
* @module parsers/nifti
*/
var ParsersNifti = function (_ParsersVolume) {
_inherits(ParsersNifti, _ParsersVolume);
function ParsersNifti(data, id) {
_classCallCheck(this, ParsersNifti);
/**
* @member
* @type {arraybuffer}
*/
var _this = _possibleConstructorReturn(this, (ParsersNifti.__proto__ || Object.getPrototypeOf(ParsersNifti)).call(this));
_this._id = id;
_this._arrayBuffer = data.buffer;
_this._url = data.url;
_this._dataSet = null;
_this._unpackedData = null;
try {
_this._dataSet = NrrdReader.parse(_this._arrayBuffer);
} catch (error) {
window.console.log('ooops... :(');
}
window.console.log(_this._dataSet);
return _this;
}
_createClass(ParsersNifti, [{
key: 'rightHanded',
value: function rightHanded() {
if (this._dataSet.space.match(/^right-anterior-superior/) || this._dataSet.space.match(/^left-posterior-superior/)) {
this._rightHanded = true;
} else {
this._rightHanded = false;
}
return this._rightHanded;
}
}, {
key: 'seriesInstanceUID',
value: function seriesInstanceUID() {
// use filename + timestamp..?
return this._url;
}
}, {
key: 'numberOfFrames',
value: function numberOfFrames() {
return this._dataSet.sizes[2];
}
}, {
key: 'numberOfChannels',
value: function numberOfChannels() {
var numberOfChannels = 1;
return numberOfChannels;
}
}, {
key: 'sopInstanceUID',
value: function sopInstanceUID() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return frameIndex;
}
}, {
key: 'rows',
value: function rows() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this._dataSet.sizes[1];
}
}, {
key: 'columns',
value: function columns() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this._dataSet.sizes[0];
}
}, {
key: 'pixelType',
value: function pixelType() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
// 0 - int
// 1 - float
var pixelType = 0;
if (this._dataSet.type === 'float') {
pixelType = 1;
}
return pixelType;
}
}, {
key: 'bitsAllocated',
value: function bitsAllocated() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var bitsAllocated = 1;
if (this._dataSet.type === 'int8' || this._dataSet.type === 'uint8' || this._dataSet.type === 'char') {
bitsAllocated = 8;
} else if (this._dataSet.type === 'int16' || this._dataSet.type === 'uint16' || this._dataSet.type === 'short') {
bitsAllocated = 16;
} else if (this._dataSet.type === 'int32' || this._dataSet.type === 'uint32' || this._dataSet.type === 'float') {
bitsAllocated = 32;
}
return bitsAllocated;
}
}, {
key: 'pixelSpacing',
value: function pixelSpacing() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var x = new THREE.Vector3(this._dataSet.spaceDirections[0][0], this._dataSet.spaceDirections[0][1], this._dataSet.spaceDirections[0][2]);
var y = new THREE.Vector3(this._dataSet.spaceDirections[1][0], this._dataSet.spaceDirections[1][1], this._dataSet.spaceDirections[1][2]);
var z = new THREE.Vector3(this._dataSet.spaceDirections[2][0], this._dataSet.spaceDirections[2][1], this._dataSet.spaceDirections[2][2]);
return [x.length(), y.length(), z.length()];
}
}, {
key: 'sliceThickness',
value: function sliceThickness() {
// should be a string...
return null; // this._dataSet.pixDims[3].toString();
}
}, {
key: 'imageOrientation',
value: function imageOrientation() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var invertX = this._dataSet.space.match(/right/) ? -1 : 1;
var invertY = this._dataSet.space.match(/anterior/) ? -1 : 1;
var x = new THREE.Vector3(this._dataSet.spaceDirections[0][0] * invertX, this._dataSet.spaceDirections[0][1] * invertY, this._dataSet.spaceDirections[0][2]);
x.normalize();
var y = new THREE.Vector3(this._dataSet.spaceDirections[1][0] * invertX, this._dataSet.spaceDirections[1][1] * invertY, this._dataSet.spaceDirections[1][2]);
y.normalize();
return [x.x, x.y, x.z, y.x, y.y, y.z];
}
}, {
key: 'imagePosition',
value: function imagePosition() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return [this._dataSet.spaceOrigin[0], this._dataSet.spaceOrigin[1], this._dataSet.spaceOrigin[2]];
}
}, {
key: 'dimensionIndexValues',
value: function dimensionIndexValues() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return null;
}
}, {
key: 'instanceNumber',
value: function instanceNumber() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return frameIndex;
}
}, {
key: 'windowCenter',
value: function windowCenter() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
// calc min and calc max
return null;
}
}, {
key: 'windowWidth',
value: function windowWidth() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
// calc min and calc max
return null;
}
}, {
key: 'rescaleSlope',
value: function rescaleSlope() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return 1; // this._dataSet.scl_slope;
}
}, {
key: 'rescaleIntercept',
value: function rescaleIntercept() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return 0; // this._dataSet.scl_intercept;
}
}, {
key: 'minMaxPixelData',
value: function minMaxPixelData() {
var pixelData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var minMax = [65535, -32768];
var numPixels = pixelData.length;
for (var index = 0; index < numPixels; index++) {
var spv = pixelData[index];
minMax[0] = Math.min(minMax[0], spv);
minMax[1] = Math.max(minMax[1], spv);
}
return minMax;
}
}, {
key: 'extractPixelData',
value: function extractPixelData() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this._decompressUncompressed(frameIndex);
}
}, {
key: '_decompressUncompressed',
value: function _decompressUncompressed() {
var frameIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var buffer = this._dataSet.buffer;
var numberOfChannels = this.numberOfChannels();
var numPixels = this.rows(frameIndex) * this.columns(frameIndex) * numberOfChannels;
if (!this.rightHanded()) {
frameIndex = this.numberOfFrames() - 1 - frameIndex;
}
var frameOffset = frameIndex * numPixels;
// unpack data if needed
if (this._unpackedData === null && this._dataSet.encoding === 'gzip') {
var unpackedData = pako.inflate(this._dataSet.buffer);
this._unpackedData = unpackedData.buffer;
buffer = this._unpackedData;
} else if (this._dataSet.encoding === 'gzip') {
buffer = this._unpackedData;
}
if (this._dataSet.type === 'int8' || this._dataSet.type === 'char') {
frameOffset = frameOffset;
return new Int8Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'uint8') {
frameOffset = frameOffset;
return new Uint8Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'int16' || this._dataSet.type === 'short') {
frameOffset = frameOffset * 2;
return new Int16Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'uint16') {
frameOffset = frameOffset * 2;
return new Uint16Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'int32') {
frameOffset = frameOffset * 4;
return new Int32Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'uint32') {
frameOffset = frameOffset * 4;
return new Uint32Array(buffer, frameOffset, numPixels);
} else if (this._dataSet.type === 'float') {
frameOffset = frameOffset * 4;
return new Float32Array(buffer, frameOffset, numPixels);
}
}
}]);
return ParsersNifti;
}(_parsers2.default);
exports.default = ParsersNifti;
module.exports = exports['default'];