All files / dicomImageLoader/src/imageLoader decodeJPEGBaseline8BitColor.ts

72.5% Statements 29/40
33.33% Branches 2/6
50% Functions 4/8
72.5% Lines 29/40

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                        20x 20x   20x 20x   20x     20x     20x 20x   20x 20x 20x 20x 20x 20x           20x 20x 20x   20x 20x 20x     20x   20x 20x 20x   20x     20x       20x         20x           20x              
import type { ByteArray } from 'dicom-parser';
import getMinMax from '../shared/getMinMax';
import type { Types } from '@cornerstonejs/core';
 
/**
 * Special decoder for 8 bit jpeg that leverages the browser's built in JPEG decoder for increased performance
 */
 
function arrayBufferToString(buffer: ArrayBuffer) {
  return binaryToString(
    String.fromCharCode.apply(
      null,
      Array.prototype.slice.apply(new Uint8Array(buffer))
    )
  );
}
 
function binaryToString(binary: string) {
  let error;
 
  try {
    return decodeURIComponent(escape(binary));
  } catch (_error) {
    error = _error;
    if (error instanceof URIError) {
      return binary;
    }
    throw error;
  }
}
 
function decodeJPEGBaseline8BitColor(
  imageFrame: Types.IImageFrame,
  pixelData: ByteArray,
  canvas: HTMLCanvasElement
): Promise<Types.IImageFrame> {
  const start = new Date().getTime();
  const imgBlob = new Blob([pixelData], { type: 'image/jpeg' });
 
  return new Promise((resolve, reject) => {
    const fileReader = new FileReader();
 
    Iif (fileReader.readAsBinaryString === undefined) {
      fileReader.readAsArrayBuffer(imgBlob);
    } else {
      fileReader.readAsBinaryString(imgBlob); // doesn't work on IE11
    }
 
    fileReader.onload = function () {
      const img = new Image();
 
      img.onload = function () {
        canvas.height = img.height;
        canvas.width = img.width;
        imageFrame.rows = img.height;
        imageFrame.columns = img.width;
        const context = canvas.getContext('2d');
 
        /**
         * @todo check this context
         */
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        context.drawImage(this as any, 0, 0);
        const imageData = context.getImageData(0, 0, img.width, img.height);
        const end = new Date().getTime();
 
        imageFrame.pixelData = new Uint8Array(imageData.data.buffer);
        imageFrame.imageData = imageData;
        imageFrame.decodeTimeInMS = end - start;
 
        // calculate smallest and largest PixelValue
        const minMax = getMinMax(imageFrame.pixelData);
 
        imageFrame.smallestPixelValue = minMax.min;
        imageFrame.largestPixelValue = minMax.max;
        imageFrame.pixelDataLength = imageFrame.pixelData.length;
 
        resolve(imageFrame);
      };
 
      img.onerror = function (error) {
        reject(error);
      };
 
      Iif (fileReader.readAsBinaryString === undefined) {
        img.src = `data:image/jpeg;base64,${window.btoa(
          arrayBufferToString(fileReader.result as ArrayBuffer)
        )}`;
      } else {
        img.src = `data:image/jpeg;base64,${window.btoa(
          fileReader.result as string
        )}`; // doesn't work on IE11
      }
    };
 
    fileReader.onerror = (e) => {
      reject(e);
    };
  });
}
 
export default decodeJPEGBaseline8BitColor;