All files / packages/core/src/RenderingEngine/helpers/cpuFallback drawImageSync.ts

88.88% Statements 16/18
66.66% Branches 8/12
100% Functions 1/1
88.88% Lines 16/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                                34x     34x         34x   34x               34x 34x   34x 34x 1x 33x     33x       34x       34x   34x   34x 34x    
import now from './rendering/now';
import { renderColorImage } from './rendering/renderColorImage';
import { renderGrayscaleImage } from './rendering/renderGrayscaleImage';
import { renderPseudoColorImage } from './rendering/renderPseudoColorImage';
import { CPUFallbackEnabledElement } from '../../../types';
 
/**
 * Draw an image to a given enabled element synchronously
 *
 * @param enabledElement - An enabled element to draw into
 * @param invalidated - true if pixel data has been invalidated and cached rendering should not be used
 */
export default function (
  enabledElement: CPUFallbackEnabledElement,
  invalidated: boolean
): void {
  const image = enabledElement.image;
 
  // Check if enabledElement can be redrawn
  Iif (!enabledElement.canvas || !enabledElement.image) {
    return;
  }
 
  // Start measuring the time needed to draw the image.
  const start = now();
 
  image.stats = {
    lastGetPixelDataTime: -1.0,
    lastStoredPixelDataToCanvasImageDataTime: -1.0,
    lastPutImageDataTime: -1.0,
    lastRenderTime: -1.0,
    lastLutGenerateTime: -1.0,
  };
 
  Eif (image) {
    let render = image.render;
 
    Eif (!render) {
      if (enabledElement.viewport.colormap) {
        render = renderPseudoColorImage;
      } else Iif (image.color) {
        render = renderColorImage;
      } else {
        render = renderGrayscaleImage;
      }
    }
 
    render(enabledElement, invalidated);
  }
 
  // Calculate how long it took to draw the image/layers
  const renderTimeInMs = now() - start;
 
  image.stats.lastRenderTime = renderTimeInMs;
 
  enabledElement.invalid = false;
  enabledElement.needsRedraw = false;
}