UNPKG

@cleerlycode/cornerstone-wado-image-loader

Version:
113 lines (93 loc) 2.94 kB
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 };