+//----------------------------------------------------------------------------
+void vtkGdcmReader::LoadImageInMemory(string FileName,
+ unsigned char * Dest,
+ size_t size)
+{
+ vtkDebugMacro("Copying to memmory image" << FileName.c_str());
+ gdcmFile GdcmFile(FileName.c_str());
+
+ if (GdcmFile.GetZSize() != 1 )
+ vtkErrorMacro("Cannot handle images with multiple planes");
+
+ // First check the expected size of the image is the one found by gdcm.
+ if ( size != GdcmFile.GetImageDataSize() )
+ {
+ vtkErrorMacro("Inconsistency with GetImageDataSize for file"
+ << FileName.c_str());
+ vtkErrorMacro("Number of scalar components"
+ << this->NumberOfScalarComponents);
+ }
+
+ // If the data structure of vtk for image/volume representation
+ // were straigthforwards the following would suffice:
+ // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
+ // But vtk chose to invert the lines of an image, that is the last
+ // line comes first (for some axis related reasons?). Hence we need
+ // to load the image line by line, starting from the end:
+ int NumColumns = GdcmFile.GetXSize();
+ int NumLines = GdcmFile.GetYSize();
+ int LineSize = NumColumns * GdcmFile.GetPixelSize();
+ unsigned char * Source = (unsigned char*)GdcmFile.GetImageData();
+ unsigned char * Destination = Dest + size - LineSize;
+ for (int i = 0; i < NumLines; i++)
+ {
+ memcpy((void*)Destination, (void*)Source, LineSize);
+ Source += LineSize;
+ Destination -= LineSize;
+ }
+}
+