Skip to content

Commit

Permalink
Merge pull request #953 from ivmartel/952-anisotropic-pixel
Browse files Browse the repository at this point in the history
952 anisotropic pixel
  • Loading branch information
ivmartel authored Jun 17, 2021
2 parents c467c7a + a787885 commit 614d639
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/app/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ dwv.App = function () {
* Fit the display to the given size. To be called once the image is loaded.
*/
this.fitToContainer = function () {
layerController.fitToContainer();
layerController.fitToContainer(
self.getImage().getGeometry().getSpacing()
);
layerController.draw();
// update style
style.setBaseScale(layerController.getBaseScale());
Expand Down
35 changes: 23 additions & 12 deletions src/app/layerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,35 @@ dwv.LayerController = function (containerDiv) {
* Get the fit to container scale.
* To be called once the image is loaded.
*
* @param {object} spacing The image spacing.
* @returns {number} The scale.
*/
this.getFitToContainerScale = function () {
this.getFitToContainerScale = function (spacing) {
// get container size
var size = this.getLayerContainerSize();
var containerSize = this.getLayerContainerSize();
var realSize = {
x: layerSize.x * spacing.getColumnSpacing(),
y: layerSize.y * spacing.getRowSpacing()
};
// best fit
return Math.min(
(size.x / layerSize.x),
(size.y / layerSize.y)
(containerSize.x / realSize.x),
(containerSize.y / realSize.y)
);
};

/**
* Fit the display to the size of the container.
* To be called once the image is loaded.
*
* @param {object} spacing The image spacing.
*/
this.fitToContainer = function () {
var fitScale = this.getFitToContainerScale();
this.resize({x: fitScale, y: fitScale});
this.fitToContainer = function (spacing) {
var fitScale = this.getFitToContainerScale(spacing);
this.resize({
x: fitScale * spacing.getColumnSpacing(),
y: fitScale * spacing.getRowSpacing()
});
};

/**
Expand All @@ -309,8 +319,8 @@ dwv.LayerController = function (containerDiv) {
*/
this.addScale = function (scaleStep, center) {
var newScale = {
x: Math.max(scale.x + scaleStep, 0.1),
y: Math.max(scale.y + scaleStep, 0.1)
x: Math.max(scale.x + scale.x * scaleStep, 0.1),
y: Math.max(scale.y + scale.y * scaleStep, 0.1)
};
// center should stay the same:
// newOffset + center / newScale = oldOffset + center / oldScale
Expand Down Expand Up @@ -409,7 +419,8 @@ dwv.LayerController = function (containerDiv) {
this.updateDrawControllerToViewPosition();

// fit data
this.fitToContainer();
var spacing = image.getGeometry().getSpacing();
this.fitToContainer(spacing);
};

/**
Expand All @@ -434,8 +445,8 @@ dwv.LayerController = function (containerDiv) {
baseScale = newScale;

// resize container
var width = parseInt(layerSize.x * baseScale.x, 10);
var height = parseInt(layerSize.y * baseScale.y, 10);
var width = Math.floor(layerSize.x * baseScale.x);
var height = Math.floor(layerSize.y * baseScale.y);
containerDiv.style.width = width + 'px';
containerDiv.style.height = height + 'px';

Expand Down
37 changes: 37 additions & 0 deletions src/dicom/dicomElementsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,43 @@ dwv.dicom.DicomElementsWrapper.prototype.getFromName = function (name) {
return value;
};

/**
* Get the pixel spacing from the different spacing tags.
*
* @returns {object} The read spacing or the default [1,1].
*/
dwv.dicom.DicomElementsWrapper.prototype.getPixelSpacing = function () {
// default
var rowSpacing = 1;
var columnSpacing = 1;

// 1. PixelSpacing
// 2. ImagerPixelSpacing
// 3. NominalScannedPixelSpacing
// 4. PixelAspectRatio
var keys = ['x00280030', 'x00181164', 'x00182010', 'x00280034'];
for (var k = 0; k < keys.length; ++k) {
var spacing = this.getFromKey(keys[k], true);
if (spacing && spacing.length === 2) {
rowSpacing = parseFloat(spacing[0]);
columnSpacing = parseFloat(spacing[1]);
break;
}
}

// check
if (columnSpacing === 0) {
dwv.logger.warn('Zero column spacing.');
columnSpacing = 1;
}
if (rowSpacing === 0) {
dwv.logger.warn('Zero row spacing.');
rowSpacing = 1;
}
// return
return new dwv.image.Spacing(columnSpacing, rowSpacing);
};

/**
* Get the file list from a DICOMDIR
*
Expand Down
4 changes: 2 additions & 2 deletions src/gui/drawLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ dwv.gui.DrawLayer = function (containerDiv) {
*/
this.resize = function (newScale) {
// resize stage
konvaStage.setWidth(parseInt(layerSize.x * newScale.x, 10));
konvaStage.setHeight(parseInt(layerSize.y * newScale.y, 10));
konvaStage.setWidth(Math.floor(layerSize.x * newScale.x));
konvaStage.setHeight(Math.floor(layerSize.y * newScale.y));
// set scale
this.setScale(newScale);
};
Expand Down
4 changes: 2 additions & 2 deletions src/gui/viewLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
*/
this.resize = function (newScale) {
// resize canvas
canvas.width = parseInt(layerSize.x * newScale.x, 10);
canvas.height = parseInt(layerSize.y * newScale.y, 10);
canvas.width = Math.floor(layerSize.x * newScale.x);
canvas.height = Math.floor(layerSize.y * newScale.y);
// set scale
this.setScale(newScale);
};
Expand Down
18 changes: 1 addition & 17 deletions src/image/imageFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,8 @@ dwv.image.ImageFactory.prototype.create = function (
// image size
var size = new dwv.image.Size(sizeValues);

// spacing
var rowSpacing = null;
var columnSpacing = null;
// PixelSpacing
var pixelSpacing = dicomElements.getFromKey('x00280030');
// ImagerPixelSpacing
var imagerPixelSpacing = dicomElements.getFromKey('x00181164');
if (pixelSpacing && pixelSpacing[0] && pixelSpacing[1]) {
rowSpacing = parseFloat(pixelSpacing[0]);
columnSpacing = parseFloat(pixelSpacing[1]);
} else if (imagerPixelSpacing &&
imagerPixelSpacing[0] &&
imagerPixelSpacing[1]) {
rowSpacing = parseFloat(imagerPixelSpacing[0]);
columnSpacing = parseFloat(imagerPixelSpacing[1]);
}
// image spacing
var spacing = new dwv.image.Spacing(columnSpacing, rowSpacing);
var spacing = dicomElements.getPixelSpacing();

// TransferSyntaxUID
var transferSyntaxUID = dicomElements.getFromKey('x00020010');
Expand Down

0 comments on commit 614d639

Please sign in to comment.