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

60% Statements 24/40
50% Branches 13/26
100% Functions 1/1
60% Lines 24/40

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                  1159x   1159x         1159x           1159x   1159x 4x       1159x 1159x     1159x     1159x     1159x 1159x             1159x                                                                                       1159x     1159x 4x       1159x           1159x 4x     1159x           1159x       1159x         1159x   1159x    
import { Transform } from './transform';
import {
  CPUFallbackEnabledElement,
  CPUFallbackTransform,
} from '../../../../types';
 
/**
 * Calculate the transform for a Cornerstone enabled element
 *
 * @param enabledElement - The Cornerstone Enabled Element
 * @param scale - The viewport scale
 * @returns The current transform
 */
export default function (
  enabledElement: CPUFallbackEnabledElement,
  scale?: number
): CPUFallbackTransform {
  const transform = new Transform();
 
  Iif (!enabledElement.viewport.displayedArea) {
    return transform;
  }
 
  // Move to center of canvas
  transform.translate(
    enabledElement.canvas.width / 2,
    enabledElement.canvas.height / 2
  );
 
  // Apply the rotation before scaling for non square pixels
  const angle = enabledElement.viewport.rotation;
 
  if (angle !== 0) {
    transform.rotate((angle * Math.PI) / 180);
  }
 
  // Apply the scale
  let widthScale = enabledElement.viewport.scale;
  let heightScale = enabledElement.viewport.scale;
 
  const width =
    enabledElement.viewport.displayedArea.brhc.x -
    (enabledElement.viewport.displayedArea.tlhc.x - 1);
  const height =
    enabledElement.viewport.displayedArea.brhc.y -
    (enabledElement.viewport.displayedArea.tlhc.y - 1);
 
  Eif (enabledElement.viewport.displayedArea.presentationSizeMode === 'NONE') {
    Iif (
      enabledElement.image.rowPixelSpacing <
      enabledElement.image.columnPixelSpacing
    ) {
      widthScale *=
        enabledElement.image.columnPixelSpacing /
        enabledElement.image.rowPixelSpacing;
    } else Iif (
      enabledElement.image.columnPixelSpacing <
      enabledElement.image.rowPixelSpacing
    ) {
      heightScale *=
        enabledElement.image.rowPixelSpacing /
        enabledElement.image.columnPixelSpacing;
    }
  } else {
    // These should be good for "TRUE SIZE" and "MAGNIFY"
    widthScale = enabledElement.viewport.displayedArea.columnPixelSpacing;
    heightScale = enabledElement.viewport.displayedArea.rowPixelSpacing;
 
    if (
      enabledElement.viewport.displayedArea.presentationSizeMode ===
      'SCALE TO FIT'
    ) {
      // Fit TRUE IMAGE image (width/height) to window
      const verticalScale =
        enabledElement.canvas.height / (height * heightScale);
      const horizontalScale =
        enabledElement.canvas.width / (width * widthScale);
 
      // Apply new scale
      widthScale = heightScale = Math.min(horizontalScale, verticalScale);
 
      if (
        enabledElement.viewport.displayedArea.rowPixelSpacing <
        enabledElement.viewport.displayedArea.columnPixelSpacing
      ) {
        widthScale *=
          enabledElement.viewport.displayedArea.columnPixelSpacing /
          enabledElement.viewport.displayedArea.rowPixelSpacing;
      } else if (
        enabledElement.viewport.displayedArea.columnPixelSpacing <
        enabledElement.viewport.displayedArea.rowPixelSpacing
      ) {
        heightScale *=
          enabledElement.viewport.displayedArea.rowPixelSpacing /
          enabledElement.viewport.displayedArea.columnPixelSpacing;
      }
    }
  }
 
  transform.scale(widthScale, heightScale);
 
  // Unrotate to so we can translate unrotated
  if (angle !== 0) {
    transform.rotate((-angle * Math.PI) / 180);
  }
 
  // Apply the pan offset
  transform.translate(
    enabledElement.viewport.translation.x,
    enabledElement.viewport.translation.y
  );
 
  // Rotate again so we can apply general scale
  if (angle !== 0) {
    transform.rotate((angle * Math.PI) / 180);
  }
 
  Iif (scale !== undefined) {
    // Apply the font scale
    transform.scale(scale, scale);
  }
 
  // Apply Flip if required
  Iif (enabledElement.viewport.hflip) {
    transform.scale(-1, 1);
  }
 
  Iif (enabledElement.viewport.vflip) {
    transform.scale(1, -1);
  }
 
  // Move back from center of image
  transform.translate(-width / 2, -height / 2);
 
  return transform;
}