Skip to content

Commit

Permalink
Fix Window/Level application in slices rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson authored and Enet4 committed Feb 14, 2022
1 parent 75be632 commit 72f6486
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
18 changes: 9 additions & 9 deletions core/dicomUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ X.dicomUtils.applyPixelPaddingToHULookupTable = function (img, HULookupTable) {
if (paddingValuesStartIndex >= HULookupTable.length) {
return;
}
var isInverse = img.presentationLutShape == PresentationLutShape.INVERSE ? true : img.photometricInterpretation == X.dicomUtils.PhotometricInterpretation.MONOCHROME1 ? true : false;
var isInverse = img.presentationLutShape == X.dicomUtils.PresentationLutShape.INVERSE ? true : img.photometricInterpretation == X.dicomUtils.PhotometricInterpretation.MONOCHROME1 ? true : false;
var fill;
if (isInverse) {
fill = HULookupTable[HULookupTable.length - 1];
Expand Down Expand Up @@ -85,7 +85,7 @@ X.dicomUtils.calculateLookupTable = function (img) {

if (img.voiFunction == X.dicomUtils.VOIFunction.LINEAR) {

var unsigned = img.pixelRepresentation == 0; //PixelRepresentation.UNSIGNED
var unsigned = img.pixelRepresentation == X.dicomUtils.PixelRepresentation.UNSIGNED;
// Get offset and window limits
var offset = (unsigned == true) ? 0 : size / 2,
xMin = offset + wc - 0.5 - (ww - 1) / 2,
Expand All @@ -111,9 +111,9 @@ X.dicomUtils.calculateLookupTable = function (img) {

} else if (img.voiFunction == X.dicomUtils.VOIFunction.SIGMOID) {

var outRangeSize = (1 << BitsAllocated.B8) - 1;
var maxOutValue = img.pixelRepresentation == PixelRepresentation.SIGNED ? (1 << (BitsAllocated.B8 - 1)) - 1 : outRangeSize;
var minOutValue = img.pixelRepresentation == PixelRepresentation.SIGNED ? -(maxOutValue + 1) : 0;
var outRangeSize = (1 << X.dicomUtils.BitsAllocated.B8) - 1;
var maxOutValue = img.pixelRepresentation == X.dicomUtils.PixelRepresentation.SIGNED ? (1 << (X.dicomUtils.BitsAllocated.B8 - 1)) - 1 : outRangeSize;
var minOutValue = img.pixelRepresentation == X.dicomUtils.PixelRepresentation.SIGNED ? -(maxOutValue + 1) : 0;
var minInValue = 0;

var nFactor = -20.0; // factor defined by default in Dicom standard ( -20*2/10 = -4 )
Expand Down Expand Up @@ -165,15 +165,15 @@ X.dicomUtils.initColorTableFromLUT = function (img, colorTable, lut) {
switch (img.photometricInterpretation) {
case X.dicomUtils.PhotometricInterpretation.MONOCHROME1:
for (var i = 0; i < lut.length; i++) {
r = g = b = ((255 - lut[i]) / 255.0);
colorTable.add(i, 'lut', r, g, b, 1);
r = g = b = (255 - lut[i]);
colorTable.add(i, 'lut', r, g, b, 255);
}
break;

case X.dicomUtils.PhotometricInterpretation.MONOCHROME2:
for (var i = 0; i < lut.length; i++) {
r = g = b = (lut[i] / 255.0);
colorTable.add(i - offset, 'lut', r, g, b, 1);
r = g = b = lut[i];
colorTable.add(i - offset, 'lut', r, g, b, 255);
}
break;
default:
Expand Down
20 changes: 4 additions & 16 deletions io/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,32 +905,20 @@ X.parser.reslice2 = function(_sliceOrigin, _sliceXYSpacing, _sliceNormal, _color

// map to 0 if necessary
var pixval = _IJKVolume[_k][_j][_i];
var pixelValue_r = 0;
var pixelValue_g = 0;
var pixelValue_b = 0;
var pixelValue_a = 0;

if (colorTable) {

// color table!
var lookupValue = colorTable.get(pixval);
// check for out of range and use the last label value in this case
if (!lookupValue) {

lookupValue = [ 0, .61, 0, 0, 1 ];

}

pixelValue_r = 255 * lookupValue[1];
pixelValue_g = 255 * lookupValue[2];
pixelValue_b = 255 * lookupValue[3];
pixelValue_a = 255 * lookupValue[4];
var pixelValue_r = pixelValue_g = pixelValue_b = lookupValue[1] || 0;
var pixelValue_a = lookupValue[4];

}
else {
// normalization should not happen here, only in the shaders/canvas??
pixelValue_r = pixelValue_g = pixelValue_b = 255 * ((pixval - object._min )/ (object._max - object._min));
pixelValue_a = 255;
var pixelValue_r = pixelValue_g = pixelValue_b = 255 * ((pixval - object._min )/ (object._max - object._min));
var pixelValue_a = 255;
}

textureForCurrentSlice[textureStartIndex] = pixelValue_r;
Expand Down
21 changes: 15 additions & 6 deletions io/parserIMAGEJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,11 @@ X.parserIMAGEJS.prototype.parseStream = function (data, object) {
// 1.2.840.10008.1.2.2: Explicit VT Big Endian
slice['transfer_syntax_uid'] = "1.2.840.10008.1.2"; //TODO: check this value

slice['rows'] = image.size.getWidth();
slice['columns'] = image.size.getHeight();
slice.bits_allocated = image.bits;
slice.pixel_representation = image.pixelRepresentation;
slice['rows'] = image.size.getHeight();
slice['columns'] = image.size.getWidth();
slice['bits_allocated'] = image.bits;
slice['pixel_representation'] = image.pixelRepresentation;
slice['photometric_interpretation'] = image.photometricInterpretation;
slice['bits_stored'] = image.bitsStored;
slice['number_of_images'] = image.series.images.length;
slice['pixel_spacing'] = [image.spacing.getRowSpacing(), image.spacing.getColumnsSpacing(), Infinity];
Expand All @@ -622,19 +623,27 @@ X.parserIMAGEJS.prototype.parseStream = function (data, object) {

slice['sop_instance_uid'] = image.id;

if (image.pixelRepresentation == X.dicomUtils.PixelRepresentation.UNSIGNED) {
slice['offset'] = 0;
} else {
var rs = image.rescale.getSlope();
var ri = image.rescale.getIntercept();
slice['offset'] = Math.pow(2, image.pixelPaddingValue != null ? image.bits : (rs == 1.0 && ri == 0.0 && image.pixelPaddingValue == null) ? image.bits : image.bitsStored) / 2;
}

// check for data type and parse accordingly
var _data = null;
////Get pixels
switch (slice.bits_allocated) {
case X.dicomUtils.BitsAllocated.B8:
if (image.pixelRepresentation === X.dicomUtils.UNSIGNED) {
if (image.pixelRepresentation === X.dicomUtils.PixelRepresentation.UNSIGNED) {
_data = new Uint8Array(pixelData);
} else {
_data = new Int8Array(pixelData);
}
break;
case X.dicomUtils.BitsAllocated.B16:
if (image.pixelRepresentation === X.dicomUtils.UNSIGNED) {
if (image.pixelRepresentation === X.dicomUtils.PixelRepresentation.UNSIGNED) {
_data = new Uint16Array(pixelData);
} else {
_data = new Int16Array(pixelData);
Expand Down

0 comments on commit 72f6486

Please sign in to comment.