All files / extensions/cornerstone/src/components/FidelityIndicator fidelityAmount.ts

61.11% Statements 11/18
62.5% Branches 10/16
100% Functions 1/1
61.11% Lines 11/18

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                277x                                                     795x 316x   479x 479x   479x         325x 325x                 479x 479x                       479x    
import {
  Enums,
  ProgressiveRetrieveImages,
  formatReductionFactor,
  isReduced,
} from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
const { ImageQualityStatus } = Enums;
 
/** One tooltip line: an i18n key of the `FidelityIndicator` namespace and its values. */
export type FidelityAmountLine = {
  kind: 'load' | 'reduction';
  key: string;
  values?: Record<string, string | number>;
};
 
/**
 * The quantified detail lines of the tooltip (VF-INFO-3): the copy distance of
 * a load that shows copies of nearby slices, and the reduction factors of a
 * reduced volume. Empty when the state gives no amount.
 *
 * A reduction gives one line for the slices and one for the image size, each
 * only when that factor is above 1. The lines follow the cause line, for
 * example "Reduced to fit this device". The image size line gives columns × rows
 * only when the two factors differ.
 *
 * `progressive.lowest` is the quality of the farthest copy, and
 * `ProgressiveRetrieveImages.replicateDistanceOf` gives its distance in slices.
 * With several volumes, the amount describes the worst volume only
 * (Cornerstone3D takes `magnitude` and `progressive` from the worst volume).
 */
export function fidelityAmountOf(
  state: Types.ViewportFidelityState | null | undefined
): FidelityAmountLine[] {
  if (!state?.lossy) {
    return [];
  }
  const lines: FidelityAmountLine[] = [];
  const lowest = state.progressive?.lowest;
 
  if (
    state.cause === 'incompleteLoad' &&
    lowest !== undefined &&
    lowest < ImageQualityStatus.SUBRESOLUTION
  ) {
    const distance = ProgressiveRetrieveImages.replicateDistanceOf(lowest);
    lines.push(
      distance === 1
        ? { kind: 'load', key: 'LoadCopyAdjacent' }
        : distance
          ? { kind: 'load', key: 'LoadCopyDistance', values: { distance } }
          : { kind: 'load', key: 'LoadCopiedSlices' }
    );
  }
 
  const magnitude = state.magnitude;
  Iif (magnitude && isReduced(magnitude)) {
    const [x, y, z] = magnitude.map(formatReductionFactor);
    Iif (z !== '1') {
      lines.push({ kind: 'reduction', key: 'ReducedSlices', values: { z } });
    }
    if (x !== y) {
      lines.push({ kind: 'reduction', key: 'ReducedColumnsRows', values: { x, y } });
    } else Iif (x !== '1') {
      lines.push({ kind: 'reduction', key: 'ReducedImage', values: { size: x } });
    }
  }
 
  return lines;
}