ctk::vtkImageDataToQImage generates weird results

I’m trying to convert vtkImageData to a QImage and display it.

Image below is the original image data displayed in sliceWidget.

And this is what I got by using ctk::vtkImageDataToQImage
Screenshot from 2022-10-25 11-03-23

It confused me since in most cases, ctk::vtkImageDataToQImage generates the right result, I don’t know why this kind of case happened

The problem is due to QImage() constructor.
In most cases, QImage(int width, int height,QImage::QFormat format) works fine, however, sometimes the bytesPerLine need to be specified, otherwise this kind of tilted issue happens.
This issue can be solved by the code below:

QImage VTKImageDataToQImage(vtkImageData* imageData)
{
  if (!imageData || !imageData->GetPointData() || !imageData->GetPointData()->GetScalars() ||
      imageData->GetScalarType() != VTK_UNSIGNED_CHAR)
  {
    return QImage();
  }

  int width = imageData->GetDimensions()[0];
  int height = imageData->GetDimensions()[1];
  vtkIdType numberOfScalarComponents = imageData->GetNumberOfScalarComponents();
  QImage image;
  QImage::Format format;
  if (numberOfScalarComponents == 3)
  {
    format = QImage::Format_RGB888;
  }
  else if (numberOfScalarComponents == 4)
  {
    format = QImage::Format_RGBA8888;
  }
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
  else if (numberOfScalarComponents == 1)
  {
    format = QImage::Format_Grayscale8;
  }
#endif
  else
  {
    // unsupported pixel format
    return QImage();
  }
  image = QImage(static_cast<uint8_t*>(imageData->GetScalarPointer(0, 0, 0)), width, height,
                 width * numberOfScalarComponents, format);
  return image.mirrored();
}

Thank you, your fix was integrated into CTK at