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

4.76% Statements 1/21
0% Branches 0/11
0% Functions 0/3
4.76% Lines 1/21
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                                                                                                                                                                             
import { initializeJPEG2000 } from '../../shared/decoders/decodeJPEG2000.js';
import { initializeJPEGLS } from '../../shared/decoders/decodeJPEGLS.js';
import calculateMinMax from '../../shared/calculateMinMax.js';
import decodeImageFrame from '../../shared/decodeImageFrame.js';
 
// 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
 * @param config
 */
function loadCodecs (config) {
  // prevent loading codecs more than once
  if (codecsLoaded) {
    return;
  }
 
  // Load the codecs
  // console.time('loadCodecs');
  self.importScripts(config.decodeTask.codecsPath);
  codecsLoaded = true;
  // console.timeEnd('loadCodecs');
 
  // Initialize the codecs
  if (config.decodeTask.initializeCodecsOnStartup) {
    // console.time('initializeCodecs');
    initializeJPEG2000(config.decodeTask);
    initializeJPEGLS(config.decodeTask);
    // console.timeEnd('initializeCodecs');
  }
}
 
/**
 * Task initialization function
 */
function initialize (config) {
  decodeConfig = config;
  if (config.decodeTask.loadCodecsOnStartup) {
    loadCodecs(config);
  }
}
 
/**
 * Task handler function
 */
function handler (data, doneCallback) {
  // Load the codecs if they aren't already loaded
  loadCodecs(decodeConfig);
 
  const strict = decodeConfig && decodeConfig.decodeTask && decodeConfig.decodeTask.strict;
  const imageFrame = data.data.imageFrame;
 
  // convert pixel data from ArrayBuffer to Uint8Array since web workers support passing ArrayBuffers but
  // not typed arrays
  const pixelData = new Uint8Array(data.data.pixelData);
 
  decodeImageFrame(
    imageFrame,
    data.data.transferSyntax,
    pixelData,
    decodeConfig.decodeTask,
    data.data.options);
 
  if (!imageFrame.pixelData) {
    throw new Error('decodeTask: imageFrame.pixelData is undefined after decoding');
  }
 
  calculateMinMax(imageFrame, strict);
 
  // 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: 'decodeTask',
  handler,
  initialize
};