all files / src/webWorker/decodeTask/ decodeTask.js

6.9% Statements 2/29
0% Branches 0/16
0% Functions 0/4
6.9% Lines 2/29
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                                                                                                                                                                                                                             
import { initializeJPEG2000 } from './decoders/decodeJPEG2000.js';
import { initializeJPEGLS } from './decoders/decodeJPEGLS.js';
import getMinMax from '../../shared/getMinMax.js';
import decodeImageFrame from './decodeImageFrame.js';
 
const kTaskType = 'decodeTask';
 
// flag to ensure codecs are loaded only once
let codecsLoaded = false;
 
// the configuration object for the decodeTask
let decodeConfig;
 
/**
 * Function to control loading and initializing the codecs
 */
function loadCodecs() {
  // prevent loading codecs more than once
  if (codecsLoaded) {
    return;
  }
 
  // Load the codecs
  // console.time('loadCodecs');
  self.importScripts(decodeConfig.codecsPath);
  codecsLoaded = true;
  // console.timeEnd('loadCodecs');
 
  // Initialize the codecs
  if (decodeConfig.initializeCodecsOnStartup) {
    // console.time('initializeCodecs');
    initializeJPEG2000(decodeConfig);
    initializeJPEGLS(decodeConfig);
    // console.timeEnd('initializeCodecs');
  }
}
 
/**
 * Task initialization function
 * @param {Object} config The web worker manager `taskConfiguration` field.
 */
function initialize(config) {
  decodeConfig = config[kTaskType] || {};
  if (decodeConfig.loadCodecsOnStartup) {
    loadCodecs();
  }
}
 
function calculateMinMax(imageFrame) {
  const minMax = getMinMax(imageFrame.pixelData);
 
  if (decodeConfig.strict === true) {
    if (imageFrame.smallestPixelValue !== minMax.min) {
      console.warn(
        'Image smallestPixelValue tag is incorrect. Rendering performance will suffer considerably.'
      );
    }
 
    if (imageFrame.largestPixelValue !== minMax.max) {
      console.warn(
        'Image largestPixelValue tag is incorrect. Rendering performance will suffer considerably.'
      );
    }
  } else {
    imageFrame.smallestPixelValue = minMax.min;
    imageFrame.largestPixelValue = minMax.max;
  }
}
 
/**
 * Task handler function
 */
function handler({ data }, doneCallback) {
  // Load the codecs if they aren't already loaded
  loadCodecs();
 
  const imageFrame = data.imageFrame;
 
  // convert pixel data from ArrayBuffer to Uint8Array since web workers support passing ArrayBuffers but
  // not typed arrays
  const pixelData = new Uint8Array(data.pixelData);
 
  decodeImageFrame(
    imageFrame,
    data.transferSyntax,
    pixelData,
    decodeConfig,
    data.options
  );
 
  if (!imageFrame.pixelData) {
    throw new Error(
      'decodeTask: imageFrame.pixelData is undefined after decoding'
    );
  }
 
  calculateMinMax(imageFrame);
 
  // convert from TypedArray to ArrayBuffer since web workers support passing ArrayBuffers but not
  // typed arrays
  imageFrame.pixelData = imageFrame.pixelData.buffer;
 
  // invoke the callback with our result and pass the pixelData in the transferList to move it to
  // UI thread without making a copy
  doneCallback(imageFrame, [imageFrame.pixelData]);
}
 
export default {
  taskType: kTaskType,
  handler,
  initialize
};