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

0% Statements 0/11
100% Branches 0/0
0% Functions 0/1
0% Lines 0/11

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                                                                                                     
import now from './now';
import { IImage } from '../../../../types';
 
/**
 * This function transforms stored pixel values into a canvas image data buffer
 * by using a LUT.  This is the most performance sensitive code in cornerstone and
 * we use a special trick to make this go as fast as possible.  Specifically we
 * use the alpha channel only to control the luminance rather than the red, green and
 * blue channels which makes it over 3x faster. The canvasImageDataData buffer needs
 * to be previously filled with white pixels.
 *
 * NOTE: Attribution would be appreciated if you use this technique!
 *
 * @param {Image} image A Cornerstone Image Object
 * @param {Array} lut Lookup table array
 * @param {Uint8ClampedArray} canvasImageDataData canvasImageData.data buffer filled with white pixels
 *
 * @returns {void}
 * @memberof Internal
 */
export default function (
  image: IImage,
  lutFunction: (value: number) => number,
  canvasImageDataData: Uint8ClampedArray
): void {
  let start = now();
  const pixelData = image.getPixelData();
 
  image.stats.lastGetPixelDataTime = now() - start;
 
  const numPixels = pixelData.length;
  // const minPixelValue = image.minPixelValue;
  let canvasImageDataIndex = 3;
  let storedPixelDataIndex = 0;
 
  // NOTE: As of Nov 2014, most javascript engines have lower performance when indexing negative indexes.
  // We have a special code path for this case that improves performance.  Thanks to @jpambrun for this enhancement
 
  // Added two paths (Int16Array, Uint16Array) to avoid polymorphic deoptimization in chrome.
  start = now();
 
  while (storedPixelDataIndex < numPixels) {
    canvasImageDataData[canvasImageDataIndex] = lutFunction(
      pixelData[storedPixelDataIndex++]
    ); // Alpha
    canvasImageDataIndex += 4;
  }
 
  image.stats.lastStoredPixelDataToCanvasImageDataTime = now() - start;
}