all files / src/imageLoader/ decodeImageFrame.js

83.33% Statements 40/48
82.5% Branches 33/40
75% Functions 3/4
83.33% Lines 40/48
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                    22× 22× 22× 22×   22× 11×       11× 11× 11× 11×   11×   11×             11×                       22×   20×   18×   16×     16×   14×                 12×     12×     12×                                                        
import { getOptions } from './internal/options.js';
import webWorkerManager from './webWorkerManager.js';
import decodeJPEGBaseline8BitColor from './decodeJPEGBaseline8BitColor.js';
import { default as decodeImageFrameHandler } from '../shared/decodeImageFrame.js';
import calculateMinMax from '../shared/calculateMinMax.js';
import { initializeJPEG2000 } from '../shared/decoders/decodeJPEG2000.js';
import { initializeJPEGLS } from '../shared/decoders/decodeJPEGLS.js';
 
let codecsInitialized = false;
 
function processDecodeTask (imageFrame, transferSyntax, pixelData, options) {
  const priority = options.priority || undefined;
  const transferList = options.transferPixelData ? [pixelData.buffer] : undefined;
  const loaderOptions = getOptions();
  const { strict, decodeConfig, useWebWorkers } = loaderOptions;
 
  if (useWebWorkers === false) {
    if (codecsInitialized === false) {
      initializeJPEG2000(decodeConfig);
      initializeJPEGLS(decodeConfig);
 
      codecsInitialized = true;
    }
 
    return new Promise((resolve, reject) => {
      try {
        const decodeArguments = [imageFrame, transferSyntax, pixelData, decodeConfig, options];
        const decodedImageFrame = decodeImageFrameHandler(...decodeArguments);
 
        calculateMinMax(decodedImageFrame, strict);
 
        resolve(decodedImageFrame);
      } catch (error) {
        reject(error);
      }
    });
  }
 
  return webWorkerManager.addTask(
    'decodeTask',
    {
      imageFrame,
      transferSyntax,
      pixelData,
      options
    }, priority, transferList).promise;
}
 
function decodeImageFrame (imageFrame, transferSyntax, pixelData, canvas, options = {}) {
  // TODO: Turn this into a switch statement instead
  if (transferSyntax === '1.2.840.10008.1.2') {
    // Implicit VR Little Endian
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.1') {
    // Explicit VR Little Endian
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.2') {
    // Explicit VR Big Endian (retired)
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else Iif (transferSyntax === '1.2.840.10008.1.2.1.99') {
    // Deflate transfer syntax (deflated by dicomParser)
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.5') {
    // RLE Lossless
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.4.50') {
    // JPEG Baseline lossy process 1 (8 bit)
 
    // Handle 8-bit JPEG Baseline color images using the browser's built-in
    // JPEG decoding
    Iif (imageFrame.bitsAllocated === 8 &&
       (imageFrame.samplesPerPixel === 3 || imageFrame.samplesPerPixel === 4)) {
      return decodeJPEGBaseline8BitColor(imageFrame, pixelData, canvas);
    }
 
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else Iif (transferSyntax === '1.2.840.10008.1.2.4.51') {
    // JPEG Baseline lossy process 2 & 4 (12 bit)
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else Iif (transferSyntax === '1.2.840.10008.1.2.4.57') {
    // JPEG Lossless, Nonhierarchical (Processes 14)
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.4.70') {
    // JPEG Lossless, Nonhierarchical (Processes 14 [Selection 1])
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.4.80') {
    // JPEG-LS Lossless Image Compression
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else Iif (transferSyntax === '1.2.840.10008.1.2.4.81') {
    // JPEG-LS Lossy (Near-Lossless) Image Compression
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else if (transferSyntax === '1.2.840.10008.1.2.4.90') {
    // JPEG 2000 Lossless
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  } else Eif (transferSyntax === '1.2.840.10008.1.2.4.91') {
    // JPEG 2000 Lossy
    return processDecodeTask(imageFrame, transferSyntax, pixelData, options);
  }
 
  /* Don't know if these work...
   // JPEG 2000 Part 2 Multicomponent Image Compression (Lossless Only)
   else if(transferSyntax === "1.2.840.10008.1.2.4.92")
   {
   return cornerstoneWADOImageLoader.decodeJPEG2000(dataSet, frame);
   }
   // JPEG 2000 Part 2 Multicomponent Image Compression
   else if(transferSyntax === "1.2.840.10008.1.2.4.93")
   {
   return cornerstoneWADOImageLoader.decodeJPEG2000(dataSet, frame);
   }
   */
 
  return new Promise((resolve, reject) => {
    reject(new Error(`No decoder for transfer syntax ${transferSyntax}`));
  });
}
 
export default decodeImageFrame;