All files / packages/tools/src/utilities annotationFrameRange.ts

4.54% Statements 1/22
0% Branches 0/12
0% Functions 0/6
4.76% Lines 1/21

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                          1x                                                                                                                                  
import { triggerEvent, eventTarget } from '@cornerstonejs/core';
import Events from '../enums/Events';
import { Annotation } from '../types';
 
export type FramesRange = [number, number] | number;
 
/**
 * This class handles the annotation frame range values for multiframes.
 * Mostly used for the Video viewport, it allows references to
 * a range of frame values.
 */
export default class AnnotationFrameRange {
  protected static frameRangeExtractor =
    /(\/frames\/|[&?]frameNumber=)([^/&?]*)/i;
 
  protected static imageIdToFrames(imageId: string): FramesRange {
    const match = imageId.match(this.frameRangeExtractor);
    if (!match || !match[2]) {
      return null;
    }
    const range = match[2].split('-').map((it) => Number(it));
    if (range.length === 1) {
      return range[0];
    }
    return range as FramesRange;
  }
 
  public static framesToString(range) {
    if (Array.isArray(range)) {
      return `${range[0]}-${range[1]}`;
    }
    return String(range);
  }
 
  protected static framesToImageId(
    imageId: string,
    range: FramesRange | string
  ): string {
    const match = imageId.match(this.frameRangeExtractor);
    if (!match || !match[2]) {
      return null;
    }
    const newRangeString = this.framesToString(range);
    return imageId.replace(
      this.frameRangeExtractor,
      `${match[1]}${newRangeString}`
    );
  }
 
  /**
   * Sets the range of frames to associate with the given annotation.
   * The range can be a single frame number (1 based according to DICOM),
   * or a range of values in the format `min-max` where min, max are inclusive
   * Modifies the referencedImageID to specify the updated URL.
   */
  public static setFrameRange(
    annotation: Annotation,
    range: FramesRange | string,
    eventBase?: { viewportId; renderingEngineId }
  ) {
    const { referencedImageId } = annotation.metadata;
    annotation.metadata.referencedImageId = this.framesToImageId(
      referencedImageId,
      range
    );
    const eventDetail = {
      ...eventBase,
      annotation,
    };
    triggerEvent(eventTarget, Events.ANNOTATION_MODIFIED, eventDetail);
  }
 
  public static getFrameRange(
    annotation: Annotation
  ): number | [number, number] {
    return this.imageIdToFrames(annotation.metadata.referencedImageId);
  }
}