all files / src/ createImage.js

4.35% Statements 1/23
0% Branches 0/4
0% Functions 0/5
4.35% Lines 1/23
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                                                                                                                                                               
import { external } from './externalModules.js';
 
const canvas = document.createElement('canvas');
let lastImageIdDrawn;
 
/**
 * creates a cornerstone Image object for the specified Image and imageId
 *
 * @param image - An Image
 * @param imageId - the imageId for this image
 * @returns Cornerstone Image Object
 */
export default function (image, imageId) {
  // extract the attributes we need
  const rows = image.naturalHeight;
  const columns = image.naturalWidth;
 
  function getPixelData () {
    const imageData = getImageData();
 
 
    return imageData.data;
  }
 
  function getImageData () {
    let context;
 
    if (lastImageIdDrawn === imageId) {
      context = canvas.getContext('2d');
    } else {
      canvas.height = image.naturalHeight;
      canvas.width = image.naturalWidth;
      context = canvas.getContext('2d');
      context.drawImage(image, 0, 0);
      lastImageIdDrawn = imageId;
    }
 
    return context.getImageData(0, 0, image.naturalWidth, image.naturalHeight);
  }
 
  function getCanvas () {
    if (lastImageIdDrawn === imageId) {
      return canvas;
    }
 
    canvas.height = image.naturalHeight;
    canvas.width = image.naturalWidth;
    const context = canvas.getContext('2d');
 
    context.drawImage(image, 0, 0);
    lastImageIdDrawn = imageId;
 
    return canvas;
  }
 
  // Extract the various attributes we need
  return {
    imageId,
    minPixelValue: 0,
    maxPixelValue: 255,
    slope: 1,
    intercept: 0,
    windowCenter: 128,
    windowWidth: 255,
    render: external.cornerstone.renderWebImage,
    getPixelData,
    getCanvas,
    getImage: () => image,
    rows,
    columns,
    height: rows,
    width: columns,
    color: true,
    rgba: false,
    columnPixelSpacing: undefined,
    rowPixelSpacing: undefined,
    invert: false,
    sizeInBytes: rows * columns * 4
  };
}