DWVv0.37.0
src/io/dicomDataLoader.jsjavascript
import {fileContentTypes} from './filesLoader.js';
import {urlContentTypes} from './urlsLoader.js';
import {DicomBufferToData} from '../image/dicomBufferToData.js';
import {LoaderBase} from './loaderBase.js';

/**
 * DICOM data loader.
 */
export class DicomDataLoader extends LoaderBase {

  /**
   * DICOM buffer to Data (asynchronous).
   *
   */
  #db2d = new DicomBufferToData();

  /**
   * Load data.
   *
   * @param {object} buffer The DICOM buffer.
   * @param {string} origin The data origin.
   * @param {number} index The data index.
   */
  load(buffer, origin, index) {
    // setup db2d ony once
    if (!this.isLoading()) {
      // pass options
      this.#db2d.setOptions(this.getOptions());
      // connect handlers
      this.#db2d.onloadstart = this.onloadstart;
      this.#db2d.onprogress = this.onprogress;
      this.#db2d.onloaditem = this.onloaditem;
      this.#db2d.onload = this.onload;
      this.#db2d.onloadend = (event) => {
        // reset loading flag
        this.setLoading(false);
        // call listeners
        this.onloadend(event);
      };
      this.#db2d.onerror = (event) => {
        event.source = origin;
        this.onerror(event);
      };
      this.#db2d.onabort = this.onabort;
    }

    // set loading flag
    this.setLoading(true);
    // convert
    this.#db2d.convert(buffer, origin, index);
  }

  /**
   * Abort load.
   */
  abort() {
    // reset loading flag
    this.setLoading(false);
    // abort conversion, will trigger db2d.onabort
    this.#db2d.abort();
  }

  /**
   * Check if the loader supports the input extension.
   *
   * @param {string} value The extension.
   * @returns {boolean} True if it can be loaded.
   */
  canLoadExtension(value) {
    return typeof value === 'undefined' ||
      value === 'dcm';
  }

  /**
   * Check if the input is the loader name.
   *
   * @param {string} value The test name.
   * @returns {boolean} True if input is the loader name.
   */
  isLoaderName(value) {
    return value === 'dicom';
  }

  /**
   * Check if the loader supports the input media type.
   *
   * @param {string} value The media type.
   * @returns {boolean} True if it can be loaded.
   */
  canLoadMediaType(value) {
    return value === 'application/dicom';
  }

  /**
   * Get the file content type needed by the loader.
   *
   * @returns {number} One of the 'fileContentTypes'.
   */
  loadFileAs() {
    return fileContentTypes.ArrayBuffer;
  }

  /**
   * Get the url content type needed by the loader.
   *
   * @returns {number} One of the 'urlContentTypes'.
   */
  loadUrlAs() {
    return urlContentTypes.ArrayBuffer;
  }

} // class DicomDataLoader