### Initialize Cornerstone WADO Image Loader with Web Workers Source: https://context7.com/cornerstonejs/cornerstonewadoimageloader/llms.txt Configure the external cornerstone dependency and initialize the web worker manager before loading images. This setup is required once at application startup. ```javascript import cornerstone from 'cornerstone-core'; import dicomParser from 'dicom-parser'; import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; cornerstoneWADOImageLoader.external.cornerstone = cornerstone; cornerstoneWADOImageLoader.external.dicomParser = dicomParser; const config = { maxWebWorkers: navigator.hardwareConcurrency || 1, startWebWorkersOnDemand: true, taskConfiguration: { decodeTask: { initializeCodecsOnStartup: false, strict: false, }, }, }; cornerstoneWADOImageLoader.webWorkerManager.initialize(config); ``` -------------------------------- ### Configure WADO Image Loader Source: https://github.com/cornerstonejs/cornerstonewadoimageloader/blob/master/examples/wadouri/index.html Configure WADO Image Loader, for example, to add custom headers before sending requests. This is useful for authentication. ```javascript cornerstoneWADOImageLoader.configure({ beforeSend: function(xhr) { // Add custom headers here (e.g. auth tokens) //xhr.setRequestHeader('APIKEY', 'my auth token'); }, }); ``` -------------------------------- ### Cornerstone WADO Image Loader Utility Functions Source: https://context7.com/cornerstonejs/cornerstonewadoimageloader/llms.txt Provides utility functions for color space conversion, getting min/max pixel values, checking if an image is color, parsing image IDs, and retrieving pixel data from WADO-RS responses. ```javascript // Color space converters const { convertRGBColorByPixel, convertRGBColorByPlane, convertYBRFullByPixel, convertYBRFullByPlane, convertPALETTECOLOR } = cornerstoneWADOImageLoader; // Get min/max pixel values const pixelData = image.getPixelData(); const minMax = cornerstoneWADOImageLoader.getMinMax(pixelData); console.log('Min:', minMax.min, 'Max:', minMax.max); // Check if image is color const isColor = cornerstoneWADOImageLoader.isColorImage( imageFrame.photometricInterpretation ); // Parse image ID to extract components const parsedId = cornerstoneWADOImageLoader.wadouri.parseImageId( 'wadouri:https://example.com/dicom/image.dcm?frame=2' ); console.log('Scheme:', parsedId.scheme); // 'wadouri' console.log('URL:', parsedId.url); // 'https://example.com/dicom/image.dcm' console.log('Frame:', parsedId.frame); // 2 // Get pixel data from WADO-RS response cornerstoneWADOImageLoader.wadors.getPixelData( imageURI, imageId, 'multipart/related; type=application/octet-stream' ).then(function(result) { console.log('Content-Type:', result.contentType); console.log('Pixel data:', result.imageFrame.pixelData); }); ``` -------------------------------- ### Load and Display Multiframe DICOM Image Source: https://github.com/cornerstonejs/cornerstonewadoimageloader/blob/master/examples/wadourimultiframe/index.html Loads a DICOM P10 multiframe SOP instance from a URL, determines the number of frames, creates imageIds, and displays the first frame using Cornerstone. It also initializes tools and starts playing the clip if it's the first load. ```javascript function loadAndViewImage(url) { var element = document.getElementById('dicomImage'); // since this is a multi-frame example, we need to load the DICOM SOP Instance into memory and parse it // so we know the number of frames it has so we can create the array of imageIds // so we know the number of frames it has so we can create the stack. Calling load() will increment the reference // count so it will stay in memory until unload() is explicitly called and all other reference counts // held by the cornerstone cache are gone. See below for more info cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.load(url, cornerstoneWADOImageLoader.internal.xhrRequest).then(function(dataSet) { // dataset is now loaded, get the # of frames so we can build the array of imageIds var numFrames = dataSet.intString('x00280008'); var FrameRate = 1000/dataSet.floatString('x00181063'); if(!numFrames) { alert('Missing element NumberOfFrames (0028,0008)'); return; } var imageIds = [] var imageIdRoot = 'wadouri:' + url; for(var i=0; i < numFrames; i++) { var imageId = imageIdRoot + "?frame="+i; imageIds.push(imageId); } var stack = { currentImageIdIndex : 0, imageIds: imageIds }; // Load and cache the first image frame. Each imageId cached by cornerstone increments // the reference count to make sure memory is cleaned up properly. cornerstone.loadAndCacheImage(imageIds[0]).then(function(image) { console.log(image); // now that we have an image frame in the cornerstone cache, we can decrement // the reference count added by load() above when we loaded the metadata. This way // cornerstone will free all memory once all imageId's are removed from the cache cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.unload(url); cornerstone.displayImage(element, image); if(loaded === false) { cornerstoneTools.wwwc.activate(element, 1); // ww/wc is the default tool for left mouse button // Set the stack as tool state cornerstoneTools.addStackStateManager(element, ['stack', 'playClip']); cornerstoneTools.addToolState(element, 'stack', stack); // Start playing the clip cornerstoneTools.playClip(element, FrameRate); loaded = true; } }, function(err) { alert(err); }); /*} catch(err) { alert(err); }*/ }); } ``` -------------------------------- ### Monitor Image Loading Progress Events Source: https://context7.com/cornerstonejs/cornerstonewadoimageloader/llms.txt Use these event listeners to track the lifecycle of an image load request, including progress updates, start, and completion. Ensure the imageId is correctly formatted for the loader. ```javascript // Listen for image load progress cornerstone.events.addEventListener('cornerstoneimageloadprogress', function(event) { const { imageId, percentComplete, loaded, total } = event.detail; console.log(`Loading ${imageId}: ${percentComplete}%`); console.log(`${loaded} of ${total} bytes`); // Update progress bar const progressBar = document.getElementById('progressBar'); progressBar.style.width = percentComplete + '%'; progressBar.textContent = percentComplete + '%'; }); // Listen for image load start cornerstone.events.addEventListener('cornerstoneimageloadstart', function(event) { console.log('Started loading:', event.detail.imageId); }); // Listen for image loaded cornerstone.events.addEventListener('cornerstoneimageloaded', function(event) { console.log('Loaded:', event.detail.image.imageId); }); // Load with progress tracking cornerstone.loadAndCacheImage('wadouri:https://example.com/large-image.dcm'); ``` -------------------------------- ### Toggle VOI LUT Application Source: https://github.com/cornerstonejs/cornerstonewadoimageloader/blob/master/examples/wadouri/index.html This event listener toggles the application of the Value of Interest (VOI) Look-Up Table (LUT) to the displayed image. It checks the state of a checkbox, gets the current image and viewport, and then either applies or removes the `voiLUT` from the viewport before updating it. ```javascript document.getElementById('toggleVOILUT').addEventListener('click', function() { var applyVOILUT = document.getElementById('toggleVOILUT').checked; console.log('applyVOILUT=', applyVOILUT); var image = cornerstone.getImage(element); var viewport = cornerstone.getViewport(element); if(applyVOILUT) { viewport.voiLUT = image.voiLUT; } else { viewport.voiLUT = undefined; } cornerstone.setViewport(element, viewport); }); ``` -------------------------------- ### Initialize and Load WADO-RS Image Source: https://github.com/cornerstonejs/cornerstonewadoimageloader/blob/master/examples/wadors/index.html Configures the loader with custom headers, fetches metadata, and enables the Cornerstone element for rendering. ```javascript window.cornerstoneWADOImageLoader || document.write('