<commit_before>import macro from 'vtk.js/Sources/macro';

const { vtkErrorMacro } = macro;

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// TODO:
// - Support image stack
// - Support slice orientation (see stack?)
// - may need some data conversion
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// ----------------------------------------------------------------------------
// vtkImageDataToCornerstoneImage methods
// ----------------------------------------------------------------------------

function vtkImageDataToCornerstoneImage(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageDataToCornerstoneImage');

  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];

    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }

    // Retrieve output and volume data
    // const origin = input.getOrigin();
    const spacing = input.getSpacing();
    const dims = input.getDimensions();
    const scalars = input.getPointData().getScalars();
    const dataRange = scalars.getRange(0);
    const rawData = scalars.getData();

    // FIXME probably need to expand to RGBA
    let pixelData = null;
    if (dims[2] === 1) {
      pixelData = scalars.data;
    } else {
      const offset =
        model.sliceIndex * dims[0] * dims[1] * rawData.BYTES_PER_ELEMENT;
      pixelData = new macro.TYPED_ARRAYS[(scalars.getDataType())](
        rawData.buffer,
        offset,
        dims[0] * dims[1]
      );
    }

    const cornerstoneImage = {
      imageId: model.imageId,
      color: scalars.getNumberOfComponents() > 1,

      columnPixelSpacing: spacing[1],
      columns: dims[1],
      width: dims[1],

      rowPixelSpacing: spacing[0],
      rows: dims[0],
      height: dims[0],

      intercept: 0,
      invert: false,
      minPixelValue: dataRange[0],
      maxPixelValue: dataRange[1],

      sizeInBytes: pixelData.length * pixelData.BYTES_PER_ELEMENT,
      slope: 1,

      windowCenter: Math.round((dataRange[0] + dataRange[1]) / 2),
      windowWidth: dataRange[1] - dataRange[0],
      decodeTimeInMS: 0,<commit_msg>fix(ImageDataToCornerstoneImage): swap cornerstone image rows&columns<commit_after>import macro from 'vtk.js/Sources/macro';

const { vtkErrorMacro } = macro;

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// TODO:
// - Support image stack
// - Support slice orientation (see stack?)
// - may need some data conversion
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// ----------------------------------------------------------------------------
// vtkImageDataToCornerstoneImage methods
// ----------------------------------------------------------------------------

function vtkImageDataToCornerstoneImage(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageDataToCornerstoneImage');

  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];

    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }

    // Retrieve output and volume data
    // const origin = input.getOrigin();
    const spacing = input.getSpacing();
    const dims = input.getDimensions();
    const scalars = input.getPointData().getScalars();
    const dataRange = scalars.getRange(0);
    const rawData = scalars.getData();

    // FIXME probably need to expand to RGBA
    let pixelData = null;
    if (dims[2] === 1) {
      pixelData = (scalars.data === null || scalars.data === undefined) ? rawData : scalars.data;
    } else {
      const offset =
        model.sliceIndex * dims[0] * dims[1] * rawData.BYTES_PER_ELEMENT;
      pixelData = new macro.TYPED_ARRAYS[(scalars.getDataType())](
        rawData.buffer,
        offset,
        dims[0] * dims[1]
      );
    }

    const cornerstoneImage = {
      imageId: model.imageId,
      color: scalars.getNumberOfComponents() > 1,

      columnPixelSpacing: spacing[0],
      columns: dims[0],
      width: dims[0],

      rowPixelSpacing: spacing[1],
      rows: dims[1],
      height: dims[1],

      intercept: 0,
      invert: false,
      minPixelValue: dataRange[0],
      maxPixelValue: dataRange[1],

      sizeInBytes: pixelData.length * pixelData.BYTES_PER_ELEMENT,
      slope: 1,

      windowCenter: Math.round((dataRange[0] + dataRange[1]) / 2),
      windowWidth: dataRange[1] - dataRange[0],
      decodeTimeInMS: 0,
{'fraction_non_alphanumeric': 0.24166666666666667, 'fraction_numerical': 0.010964912280701754, 'mean_word_length': 2.401193139448173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5395390', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1319, 'n_words': 284}