-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixe viewport scale factor is NaN when row/col pixel spacing is note …
…set. (#264) * Fixes issue #262, scale factor is NaN causing cornerstone-tools to fail with Math. Also fixes in cornerstone tools - createImage columnPixelSpacing (and rowPixelSpacing) is undefined #190 * Refactor, reduce complexity and adding a "getImageSize_test" unit test
- Loading branch information
1 parent
adc79d9
commit 8dcce3c
Showing
10 changed files
with
316 additions
and
95 deletions.
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
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,40 @@ | ||
import { validateParameterUndefinedOrNull } from './validator.js'; | ||
import getImageSize from './getImageSize.js'; | ||
|
||
|
||
/** | ||
* Calculates the horizontal, vertical and minimum scale factor for an image | ||
@param {{width, height}} windowSize The window size where the image is displayed. This can be any HTML element or structure with a width, height fields (e.g. canvas). | ||
* @param {any} image The cornerstone image object | ||
* @param {Number} rotation Optional. The rotation angle of the image. | ||
* @return {{horizontalScale, verticalScale, scaleFactor}} The calculated horizontal, vertical and minimum scale factor | ||
* @memberof Internal | ||
*/ | ||
export default function (windowSize, image, rotation = null) { | ||
|
||
validateParameterUndefinedOrNull(windowSize, 'getImageScale: parameter windowSize must not be undefined'); | ||
validateParameterUndefinedOrNull(image, 'getImageScale: parameter image must not be undefined'); | ||
|
||
const imageSize = getImageSize(image, rotation); | ||
const rowPixelSpacing = image.rowPixelSpacing === undefined ? 1 : image.rowPixelSpacing; | ||
const columnPixelSpacing = image.columnPixelSpacing === undefined ? 1 : image.columnPixelSpacing; | ||
let verticalRatio = 1; | ||
let horizontalRatio = 1; | ||
|
||
if (rowPixelSpacing < columnPixelSpacing) { | ||
horizontalRatio = columnPixelSpacing / rowPixelSpacing; | ||
} else { | ||
// even if they are equal we want to calculate this ratio (the ration might be 0.5) | ||
verticalRatio = rowPixelSpacing / columnPixelSpacing; | ||
} | ||
|
||
const verticalScale = windowSize.height / imageSize.height / verticalRatio; | ||
const horizontalScale = windowSize.width / imageSize.width / horizontalRatio; | ||
|
||
// Fit image to window | ||
return { | ||
verticalScale, | ||
horizontalScale, | ||
scaleFactor: Math.min(horizontalScale, verticalScale) | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,19 +1,39 @@ | ||
import { validateParameterUndefinedOrNull } from './validator.js'; | ||
|
||
/** | ||
* Check if the angle is rotated | ||
* @param {Number} rotation the rotation angle | ||
* @returns {Boolean} true if the angle is rotated; Otherwise, false. | ||
* @memberof Internal | ||
*/ | ||
function isRotated (rotation) { | ||
return !(rotation === null || rotation === undefined || rotation === 0 || rotation === 180); | ||
} | ||
|
||
/** | ||
* Retrieves the current image dimensions given an enabled element | ||
* | ||
* @param {EnabledElement} enabledElement The Cornerstone Enabled Element | ||
* @return {{width, height}} The Image dimensions | ||
* @param {any} image The Cornerstone image. | ||
* @param {Number} rotation Optional. The rotation angle of the image. | ||
* @return {{width:Number, height:Number}} The Image dimensions | ||
* @memberof Internal | ||
*/ | ||
export default function (enabledElement) { | ||
if (enabledElement.viewport.rotation === 0 || enabledElement.viewport.rotation === 180) { | ||
export default function (image, rotation = null) { | ||
|
||
validateParameterUndefinedOrNull(image, 'getImageSize: parameter image must not be undefined'); | ||
validateParameterUndefinedOrNull(image.width, 'getImageSize: parameter image must have width'); | ||
validateParameterUndefinedOrNull(image.height, 'getImageSize: parameter image must have height'); | ||
|
||
|
||
if (isRotated(rotation)) { | ||
return { | ||
width: enabledElement.image.width, | ||
height: enabledElement.image.height | ||
height: image.width, | ||
width: image.height | ||
}; | ||
} | ||
|
||
return { | ||
width: enabledElement.image.height, | ||
height: enabledElement.image.width | ||
width: image.width, | ||
height: image.height | ||
}; | ||
} |
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,27 @@ | ||
|
||
/** | ||
* Check if the supplied parameter is undefined and throws and error | ||
* @param {any} checkParam the parameter to validate for undefined | ||
* @param {any} errorMsg the error message to be thrown | ||
* @returns {void} | ||
* @memberof internal | ||
*/ | ||
export function validateParameterUndefined (checkParam, errorMsg) { | ||
if (checkParam === undefined) { | ||
throw new Error(errorMsg); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Check if the supplied parameter is undefined or null and throws and error | ||
* @param {any} checkParam the parameter to validate for undefined | ||
* @param {any} errorMsg the error message to be thrown | ||
* @returns {void} | ||
* @memberof internal | ||
*/ | ||
export function validateParameterUndefinedOrNull (checkParam, errorMsg) { | ||
if (checkParam === undefined || checkParam === null) { | ||
throw new Error(errorMsg); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export default '2.2.0'; | ||
export default '2.2.1-rc1'; |
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
Oops, something went wrong.