All files / packages/core/src/RenderingEngine/helpers/cpuFallback/rendering getImageFitScale.ts

92.3% Statements 12/13
42.85% Branches 3/7
100% Functions 1/1
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                        24x       24x         24x 24x 24x 24x 24x   24x       24x     24x 24x     24x            
import { validateParameterUndefinedOrNull } from './validator';
import getImageSize from './getImageSize';
import { IImage } from '../../../../types';
 
/**
 * Calculates the horizontal, vertical and minimum scale factor for an image
   @param canvas - 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 image - The cornerstone image object
 * @param rotation - The rotation angle of the image.
 * @returns The calculated horizontal, vertical and minimum scale factor
 */
export default function (
  canvas: HTMLCanvasElement,
  image: IImage,
  rotation: number | null = null
): {
  verticalScale: number;
  horizontalScale: number;
  scaleFactor: number;
} {
  validateParameterUndefinedOrNull(
    canvas,
    'getImageScale: parameter canvas must not be undefined'
  );
  validateParameterUndefinedOrNull(
    image,
    'getImageScale: parameter image must not be undefined'
  );
 
  const imageSize = getImageSize(image, rotation);
  const rowPixelSpacing = image.rowPixelSpacing || 1;
  const columnPixelSpacing = image.columnPixelSpacing || 1;
  let verticalRatio = 1;
  let horizontalRatio = 1;
 
  Iif (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 = canvas.height / imageSize.height / verticalRatio;
  const horizontalScale = canvas.width / imageSize.width / horizontalRatio;
 
  // Fit image to window
  return {
    verticalScale,
    horizontalScale,
    scaleFactor: Math.min(horizontalScale, verticalScale),
  };
}