Cornerstone WADO Image Loader

repository·master·Indexed 43 days ago

https://github.com/cornerstonejs/cornerstonewadoimageloader

A specialized image loader for the cornerstone framework designed to handle DICOM P10 instances via HTTP (WADO-URI) or DICOMWeb (WADO-RS). It manages image retrieval and decoding, utilizing web workers and WebAssembly (WASM) for performance. Note: This package is deprecated; users are encouraged to migrate to @cornerstonejs/dicom-image-loader.

Tokens
9.9K
Snippets
32
Records
38
Agent score
52%

What's inside Cornerstone WADO Image Loader

  1. Choosing between OpenJPEG and PDF.js for JPEG2000

    master

    For JPEG2000 images, cornerstoneWADOImageLoader provides two options.

    OpenJPEG (Default)

    OpenJPEG.js is the default codec because it is widely used, actively supported, and capable of decoding most JPEG2000 images found in medical imaging. It is the recommended choice for reliability.

    PDF.js (OHIF/image-JPEG2000)

    OHIF/image-JPEG2000 (based on PDF.js) is an alternative. While it may offer better performance in certain browsers or for specific image types, it is not recommended due to known issues and a lack of active support.

  2. Initialize the Web Worker framework

    master

    Because web workers require specific file paths, you must initialize the cornerstoneWADOImageLoader.webWorkerManager before attempting to load images or start any web worker tasks. This ensures the worker code is properly loaded and available to the application.

    Minimal Configuration

    For a basic setup that uses the available CPU cores and creates workers only when needed:

    var config = {
      maxWebWorkers: navigator.hardwareConcurrency || 1,
      startWebWorkersOnDemand: true,
    };
    cornerstoneWADOImageLoader.webWorkerManager.initialize(config);
    var config = {
      maxWebWorkers: navigator.hardwareConcurrency || 1,
      startWebWorkersOnDemand: true,
    };
    cornerstoneWADOImageLoader.webWorkerManager.initialize(config);
  3. Format ImageIds for cornerstone-wado-image-loader

    master

    To load images using the WADO Image Loader, you must use the wadouri: prefix in your image IDs. While the dicomweb prefix is currently supported, it is deprecated and will be removed in future versions.

    Supported formats include:

    • Absolute URLs: Full web addresses starting with http:// or https://.
    • Relative URLs: Paths relative to the current origin.
    • WADO-URI URLs: Specific WADO-URI request strings containing parameters like studyUID, seriesUID, and transferSyntax.
    • File Endpoints: Direct links to DICOM files (e.g., from an Orthanc instance).

    Important: The web server hosting the DICOM files must support Cross-Origin Resource Sharing (CORS). If CORS is not enabled on the source server, image loading will fail. You can resolve this by enabling CORS on the server or using a reverse proxy to inject the necessary CORS headers.

    // Absolute URL
    wadouri:http://cornerstonetech.org/images/ClearCanvas/USEcho/IM00001
    
    // Relative URL
    wadouri:/images/ClearCanvas/USEcho/IM00001
    
    // WADO-URI URL
    wadouri:http://localhost:3333/wado?requestType=WADO&studyUID=1.3.6.1.4.1.25403.166563008443.5076.20120418075541.5076.20120418075541.2&objectUID=1.3.6.1.4.1.25403.166563008443.5076.20120418075557.1&contentType=application%2Fdicom&transferSyntax=1.2.840.10008.1.2.1
    
    // Orthanc file endpoint
    wadouri:http://localhost:8042/instances/8cce70aa-576ad738-b76cb63f-caedb3c7-2b213aae/file
  4. How to create custom Web Worker tasks

    master

    You can extend the framework to run CPU-intensive custom logic in the background. Follow these steps:

    1. Create the Worker File

    Create a new JavaScript file (e.g., myTask.js). Inside this file, register your task using self.registerTaskHandler():

    self.registerTaskHandler({
      taskType: 'myCustomTask', // Unique string to identify the task
      initialize: (config) => {
        // Called when worker is first initialized
        // 'config' is the taskConfiguration object from initialize()
      },
      handler: (data, callback) => {
        // 'data' is the object passed from the UI thread
        // 'callback' is used to return the result
        
        const result = { processed: true };
        const transferList = []; // Optional: array of objects to transfer via postMessage
        
        callback(result, transferList);
      }
    });

    2. Register the Task Path

    In your main application thread, add the path to your new file in the webWorkerTaskPaths array during initialization:

    cornerstoneWADOImageLoader.webWorkerManager.initialize({
      webWorkerTaskPaths: ['path/to/myTask.js']
    });

    3. Queue the Task from the UI Thread

    Use cornerstoneWADOImageLoader.webWorkerManager.addTask() to execute the task. This returns a promise that resolves with the result returned by the worker's callback.

    const task = cornerstoneWADOImageLoader.webWorkerManager.addTask(
      'myCustomTask', // Must match taskType
      { inputData: 123 }, // The 'data' object
      1 // Optional priority (lower numbers = higher priority; default is 0)
    );
    
    task.then((result) => {
      console.log('Task completed:', result);
    });
    cornerstoneWADOImageLoader.webWorkerManager.addTask(
      'myCustomTask',
      { inputData: 123 },
      1
    ).then((result) => {
      console.log('Task completed:', result);
    });
  5. Configure cornerstone-wado-image-loader dependencies and web workers

    master

    The loader requires dicomParser and cornerstone to be loaded in the environment before it.

    Since version 1.0.0, you must explicitly specify which cornerstone instance the loader should register with. Additionally, you must initialize the webWorkerManager to handle CPU-intensive tasks like image decoding.

    Configuration options for webWorkerManager.initialize:

    • maxWebWorkers: Number of web workers to use (defaults to navigator.hardwareConcurrency or 1).
    • startWebWorkersOnDemand: Boolean indicating if workers should start on demand.
    // 1. Register the cornerstone instance
    cornerstoneWADOImageLoader.external.cornerstone = cornerstone;
    
    // 2. Configure the web worker framework
    var config = {
      maxWebWorkers: navigator.hardwareConcurrency || 1,
      startWebWorkersOnDemand: true,
    };
    cornerstoneWADOImageLoader.webWorkerManager.initialize(config);
  6. Upgrade to CWIL v4.x with Dynamic Imports and WASM

    master

    Version 4.x introduces WebAssembly (WASM) support for codecs, which improves performance and reduces bundle size by loading codecs dynamically at runtime.

    1. Update Imports

    Instead of a standard import, use the dynamic-import path to enable dynamic codec loading:

    import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader/dist/dynamic-import/cornerstoneWADOImageLoader.min.js';

    Alternatively, you can configure a Webpack alias:

    resolve: {
      alias: {
        'cornerstone-wado-image-loader': 'cornerstone-wado-image-loader/dist/dynamic-import/cornerstoneWADOImageLoader.min.js',
      },
    }

    2. Copy WASM files to build directory

    You must ensure the WASM codec files are available in your build folder. If using CopyWebpackPlugin, add a rule to copy the files from node_modules:

    plugins: [
      new CopyWebpackPlugin([
        {
          from: '../../../node_modules/cornerstone-wado-image-loader/dist/dynamic-import',
          to: DIST_DIR,
        },
      ]),
    ]

    Note: The from path is relative to your project's node_modules folder.

    3. Server Configuration

    If hosting on a server like IIS, ensure it is configured to serve .wasm files with the correct MIME type.

  7. Configure Web Worker options

    master

    The cornerstoneWADOImageLoader.webWorkerManager.initialize(config) function accepts a configuration object to fine-tune worker behavior and task settings.

    Configuration Options

    OptionTypeDescription
    maxWebWorkersnumberControls the maximum number of web workers to create. Defaults to navigator.hardwareConcurrency or 1 if unavailable.
    startWebWorkersOnDemandbooleanIf true, workers are created only when needed. If false, all workers are created immediately upon initialization (default).
    webWorkerTaskPathsstring[]An array of paths to custom web worker task source files.
    taskConfigurationobjectAn object containing task-specific configuration settings.

    taskConfiguration Details

    • decodeTask.initializeCodecsOnStartup: (Boolean) By default, JPEG2000 and JPEG-LS decoders are NOT initialized on startup to save CPU/time. Set to true if you frequently display these image types and want to reduce latency during the first decode.
    • Custom Task Config: You can pass arbitrary configuration objects to your custom tasks using keys that match your taskType. For example, taskConfiguration.sleepTask.sleepTime will be passed to a task registered as sleepTask.
    var config = {
      maxWebWorkers: navigator.hardwareConcurrency || 1,
      startWebWorkersOnDemand: true,
      webWorkerTaskPaths: [
        '../examples/customWebWorkerTask/sleepTask.js',
        '../examples/customWebWorkerTask/sharpenTask.js',
      ],
      taskConfiguration: {
        decodeTask: {
          initializeCodecsOnStartup: false,
        },
        sleepTask: {
          sleepTime: 3000,
        },
      },
    };
    cornerstoneWADOImageLoader.webWorkerManager.initialize(config);