From what you’ve told me, I’m going to use these assumptions:
-
image.pixelData(0)->size()
returns the buffer size (in bytes) of the first frame -
image.getRawPixelData(0)
returns the pointer to the buffer itself
What VTK needs is a buffer for the entire image volume. To keep things simple, let’s assume that each image has just one frame. So the buffer size that VTK needs is the buffer size per image frame, multiplied by the total number of images.
So you’re going to need a huge buffer, large enough to store all the images. At this point, let’s forget about vtkImageImport. The most efficient way to approach this is to directly access the vtkImageData’s buffer, rather than creating a temporary buffer. Then vtkImageImport becomes completely unnecessary.
// VTK will allocate a buffer of the desired size and type
int dataType = VTK_SHORT; // make sure this is correct!
int dataTypeSize = 2; // each 'short' is 2 bytes
int numberOfComponents = 1;
m_imageData->SetDimensions(imageSize.width(), imageSize.height(), dicomStack.size());
m_imageData->AllocateScalars(dataType, numberOfComponents); // allocate buffer
m_imageData->SetSpacing(spacing.x(), spacing.y(), spacing.z());
// compute the number of bytes required to store each slice of the volume
size_t bytesPerSlice = dataTypeSize * numberOfComponents;
bytesPerSlice *= imageSize.width();
bytesPerSlice *= imageSize.height();
for (int i=0; i < dicomStack.size(); i++) {
// are you sure that you don't want to use a reference here?
DcmImage currentImage = dicomStack.image(i);
if (currentImage.numFrames() > 0) {
size_t bytesPerFrame = image.pixelData(0)->size();
// do a sanity check on the size
if (bytesPerFrame == bytesPerSlice) {
// copy the image into the correct slice of the vtkImageData buffer
memcpy(m_imageData->GetScalarPointer(0, 0, i), currentImage.getRawPixelData(0), bytesPerSlice);
}
}
}
This works because m_imageData->GetScalarPointer(k, j, i)
returns a void*
to the correct position within the vtkImageData’s buffer (where k=column, j=row, i=slice).
Please note that I just typed this code and haven’t compiled it, so it probably contains typos.