Skip to content

Commit

Permalink
Refactor, reduce complexity and adding a "getImageSize_test" unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-Safadi committed Apr 23, 2018
1 parent 70206bb commit 533a6ac
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 41 deletions.
47 changes: 29 additions & 18 deletions src/internal/getDefaultViewport.js
Expand Up @@ -8,6 +8,8 @@ import getImageFitScale from './getImageFitScale.js';
* @memberof Internal
*/
function createViewport () {
const displayedArea = createDefaultDisplayedArea();

return {
scale: 1,
translation: {
Expand All @@ -27,25 +29,34 @@ function createViewport () {
voiLUT: undefined,
colormap: undefined,
labelmap: false,
displayedArea
};
}

/**
* C.10.4 Displayed Area Module: This Module describes Attributes required to define a Specified Displayed Area space.
*/
displayedArea: {
// Top Left Hand Corner
tlhc: {
x: 1,
y: 1
},
// Bottom Right Hand Corner
brhc: {
x: 1,
y: 1
},
rowPixelSpacing: 1,
columnPixelSpacing: 1,
presentationSizeMode: 'NONE'
}

/**
* Creates the default displayed area.
* C.10.4 Displayed Area Module: This Module describes Attributes required to define a Specified Displayed Area space.
*
* @returns {tlhc: {x,y}, brhc: {x, y},rowPixelSpacing: Number, columnPixelSpacing: Number, presentationSizeMode: Number} displayedArea object
* @memberof Internal
*/

function createDefaultDisplayedArea () {
return {
// Top Left Hand Corner
tlhc: {
x: 1,
y: 1
},
// Bottom Right Hand Corner
brhc: {
x: 1,
y: 1
},
rowPixelSpacing: 1,
columnPixelSpacing: 1,
presentationSizeMode: 'NONE'
};
}

Expand Down
12 changes: 5 additions & 7 deletions src/internal/getImageFitScale.js
@@ -1,21 +1,19 @@
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
* @memberof Internal
*/
export default function (windowSize, image, rotation = null) {
if (windowSize === undefined) {
throw new Error('getImageScale: parameter windowSize must not be undefined');
}

if (image === undefined) {
throw new Error('getImageScale: parameter image must not be undefined');
}
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;
Expand Down
32 changes: 23 additions & 9 deletions src/internal/getImageSize.js
@@ -1,25 +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 {any} image The Cornerstone image.
* @param {Number} rotation Optional. The rotation angle of the image.
* @return {{width, height}} The Image dimensions
* @return {{width:Number, height:Number}} The Image dimensions
* @memberof Internal
*/
export default function (image, rotation = null) {

if (image.width === undefined || image.height === undefined) {
throw new Error('getImageSize: parameter image must have width/height');
}
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 (rotation === undefined || rotation === 0 || rotation === 180) {
if (isRotated(rotation)) {
return {
width: image.width,
height: image.height
height: image.width,
width: image.height
};
}

return {
width: image.height,
height: image.width
width: image.width,
height: image.height
};
}
27 changes: 27 additions & 0 deletions src/internal/validator.js
@@ -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);
}
}
8 changes: 1 addition & 7 deletions src/resize.js
Expand Up @@ -110,13 +110,7 @@ export default function (element, forceFitToWindow) {
return;
}

if (forceFitToWindow === true) {
fitToWindow(element);

return;
}

if (wasFitToWindow(enabledElement, oldCanvasWidth, oldCanvasHeight)) {
if (forceFitToWindow || wasFitToWindow(enabledElement, oldCanvasWidth, oldCanvasHeight)) {
// Fit the image to the window again if it fitted before the resize
fitToWindow(element);
} else {
Expand Down
59 changes: 59 additions & 0 deletions test/internal/getImageSize_test.js
@@ -0,0 +1,59 @@
import { should, expect } from 'chai';

import getImageSize from '../../src/internal/getImageSize.js';

should();

describe('getImageSize', function () {

describe('when image parameters is not passed', function () {
it('should throw an error', function () {

expect(function () {
getImageSize();
}).to.throw('getImageSize: parameter image must not be undefined');

expect(function () {
getImageSize({ width: 50 });
}).to.throw('getImageSize: parameter image must have height');

expect(function () {
getImageSize({ height: 100 });
}).to.throw('getImageSize: parameter image must have width');
});
});

describe('when an image is passed with no rotation', function () {
it('should return the image width/height', function () {
const image = {
width: 50,
height:100
};

const imageSizeNoRotationParameter = getImageSize(image);
const imageSize0RotationParameter = getImageSize(image, 0);
const imageSize180RotationParameter = getImageSize(image, 180);

imageSizeNoRotationParameter.should.be.deep.equal(image);
imageSize0RotationParameter.should.be.deep.equal(image);
imageSize180RotationParameter.should.be.deep.equal(image);
});
});

describe('when an image is passed rotated', function () {
it('should return the image width/height rotated', function () {
let image = {
width: 50,
height:100
};

const returnedImageSize = getImageSize(image, 90);

// rotate
image.width = 100;
image.height = 50;

returnedImageSize.should.be.deep.equal(image);
});
});
});

0 comments on commit 533a6ac

Please sign in to comment.