Skip to content

Commit

Permalink
Reduce memory usage in ImageJS module
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson authored and Enet4 committed Feb 14, 2022
1 parent 72f6486 commit 6e5f8e0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions io/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ X.loader.prototype.parse = function(request, container, object) {
// call the parse function and pass in the container, the object and the
// data stream and some additional value
_parser.parse(container, object, _data, flags);
container._filedata = null;

}.bind(this), 100);

Expand Down
11 changes: 2 additions & 9 deletions io/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ X.parser.createIJKVolume = function(_data, _dims, _max, _min){
// initiate variables
// allocate images
var _image = new Array(_dims[2]);
var _imageN = new Array(_dims[2]);
var _nb_pix_per_slice = _dims[1] * _dims[0];
var _pix_value = 0;
var _i = 0;
Expand All @@ -477,25 +476,22 @@ X.parser.createIJKVolume = function(_data, _dims, _max, _min){
_data_pointer = 0;

// allocate images
_imageN[_k] = new Array(_dims[1]);
_image[_k] = new Array(_dims[1]);

for (_j = 0; _j < _dims[1]; _j++) {

// allocate images
_imageN[_k][_j] = new _data.constructor(_dims[0]);
_image[_k][_j] = new _data.constructor(_dims[0]);
for (_i = 0; _i < _dims[0]; _i++) {
_pix_value = _current_k[_data_pointer];
_imageN[_k][_j][_i] = 255 * ((_pix_value - _min) / (_max - _min));
_image[_k][_j][_i] = _pix_value;
_data_pointer++;

}
}
}

return [_image, _imageN];
return _image;
};

/**
Expand Down Expand Up @@ -1144,11 +1140,8 @@ X.parser.prototype.reslice = function(object) {
// Step 1: create 2 IJK volumes
// 1 full res, 1 normalized [0-255]

var _IJKVolumes = X.parser.createIJKVolume(object._data, object._dimensions, object._max, object._min);
// real volume
object._IJKVolume = _IJKVolumes[0];
// normalized volume
object._IJKVolumeN = _IJKVolumes[1];
object._IJKVolume = X.parser.createIJKVolume(object._data, object._dimensions, object._max, object._min);
X.TIMER(this._classname + '.reslice');

// ------------------------------------------
Expand Down
19 changes: 17 additions & 2 deletions io/parserIMAGEJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ X.parserIMAGEJS.prototype.parse = function (container, object, data, flag) {

}

imageSeriesPushed = null;

////////////////////////////////////////////////////////////////////////
// At this point:
// -> slices are ordered by series
Expand Down Expand Up @@ -556,13 +558,25 @@ X.parserIMAGEJS.prototype.parse = function (container, object, data, flag) {
volumeAttributes.RASOrigin = [_rasBB[0], _rasBB[2], _rasBB[4]];


X.dicomUtils.initColorTableFromLUT(data.image, object.colortable, X.dicomUtils.calculateLookupTable(data.image));
var centerImage = object.slices[Math.ceil(object.slices.length / 2)].ref_image;
X.dicomUtils.initColorTableFromLUT(centerImage, object.colortable, X.dicomUtils.calculateLookupTable(centerImage));

// create the volume object
object.create_(volumeAttributes);

for(var i = 0; i < object.slices.length; i++){
delete object.slices[i]['ref_image'];
delete object.slices[i]['data'];
}
delete object['slices'];
volumeAttributes = null;
first_image = null;
first_image_data = null;
series = null;

// re-slice the data in SAGITTAL, CORONAL and AXIAL directions
object._image = this.reslice(object);
object._data = null;

}

Expand All @@ -587,7 +601,7 @@ X.parserIMAGEJS.prototype.parse = function (container, object, data, flag) {
*/
X.parserIMAGEJS.prototype.parseStream = function (data, object) {
// attach the given data
this._data = data;
//object._data = data;

var image = data.image;
var pixelData = data.pixelData;
Expand Down Expand Up @@ -654,6 +668,7 @@ X.parserIMAGEJS.prototype.parseStream = function (data, object) {
}

slice['data'] = _data;
slice['ref_image'] = data.image;

object.slices.push(slice);

Expand Down

0 comments on commit 6e5f8e0

Please sign in to comment.