From e70b894b51dba25e9329fca249f92fb57463bb49 Mon Sep 17 00:00:00 2001 From: Mickey <327191467@qq.com> Date: Mon, 2 Mar 2020 22:09:02 +0800 Subject: [PATCH] fix: cornerstonejs#1176 Pan and Zoom issue with segmentation on image with non-square pixels (#1183) Co-authored-by: MickeyMiao7 <531388612@qq.com> --- .../internals/renderSegmentationFill.js | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/eventListeners/internals/renderSegmentationFill.js b/src/eventListeners/internals/renderSegmentationFill.js index ae57151c8..f4f1f69cf 100644 --- a/src/eventListeners/internals/renderSegmentationFill.js +++ b/src/eventListeners/internals/renderSegmentationFill.js @@ -122,10 +122,7 @@ export function renderFill(evt, labelmapCanvas, isActiveLabelMap) { transformCanvasContext(context, canvas, viewport); - const canvasViewportTranslation = { - x: viewport.translation.x * viewport.scale, - y: viewport.translation.y * viewport.scale, - }; + const canvasViewportTranslation = getCanvasViewportTranslation(eventData); context.drawImage( labelmapCanvas, @@ -142,3 +139,28 @@ export function renderFill(evt, labelmapCanvas, isActiveLabelMap) { resetCanvasContextTransform(context); } + +/** + * GetCanvasViewportTranslation - Returns translation coordinations for + * canvas viewport with calculation of image row/column pixel spacing. + * + * @param {Object} eventData The data associated with the event. + * @returns {Object} The coordinates of the translation. + */ +function getCanvasViewportTranslation(eventData) { + const { viewport, image } = eventData; + + let widthScale = viewport.scale; + let heightScale = viewport.scale; + + if (image.rowPixelSpacing < image.columnPixelSpacing) { + widthScale *= image.columnPixelSpacing / image.rowPixelSpacing; + } else if (image.columnPixelSpacing < image.rowPixelSpacing) { + heightScale *= image.rowPixelSpacing / image.columnPixelSpacing; + } + + return { + x: viewport.translation.x * widthScale, + y: viewport.translation.y * heightScale, + }; +}