- if (!this->FileName)
- {
- vtkErrorMacro("A valid FileName must be specified.");
- return;
- }
-
- vtkImageData *data = this->AllocateOutputData(output);
- data->SetExtent(this->DataExtent);
- data->GetPointData()->GetScalars()->SetName("ImageFile");
-
- // First check the coherence between the DataExtent and the
- // size of the pixel data as annouced by gdcm (looks a bit paranoid).
- gdcmFile GdcmFile(this->InternalFileName);
- int NumColumns = this->DataExtent[1] - this->DataExtent[0] + 1;
- int NumLines = this->DataExtent[3] - this->DataExtent[2] + 1;
- int NumPlanes = this->DataExtent[5] - this->DataExtent[4] + 1;
- int size = NumColumns * NumLines * NumPlanes * GdcmFile.GetPixelSize();
- if ( size != GdcmFile.GetImageDataSize() )
- {
- vtkDebugMacro("Inconsistency with GetImageDataSize");
- vtkDebugMacro("Number of scalar components"
- << this->NumberOfScalarComponents);
- }
- // Allocate pixel data space itself.
- unsigned char *mem = new unsigned char [size];
-
- // If the data structure of vtk for image/volume representation
- // were straigthforwards the following would suffice:
- // GdcmFile.GetImageDataIntoVector((void*)mem, 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 LineSize = NumColumns * GdcmFile.GetPixelSize();
- unsigned char * Source = (unsigned char*)GdcmFile.GetImageData();
- unsigned char * Destination = mem + size - LineSize;
- for (int i = 0; i < NumLines; i++)
- {
- memcpy((void*)Destination, (void*)Source, LineSize);
- Source += LineSize;
- Destination -= LineSize;
- }
- // The "size" of the vtkScalars data is expressed in number of points,
- // and is not the memory size representing those points:
- size = size / GdcmFile.GetPixelSize();
- data->GetPointData()->GetScalars()->SetVoidArray(mem, size, 0);
- this->Modified();
-
-}
-
-void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
-{
- vtkImageReader::PrintSelf(os,indent);
- //CLEANME os << indent << "TypeSize: " << this->TypeSize << "\n";
+ vtkImageData *data=vtkImageData::SafeDownCast(output);
+ data->SetExtent(this->DataExtent);
+
+/* if ( CoherentFileList != 0 ) // When a list of names is passed
+ {
+ if (this->CoherentFileList->empty())
+ {
+ vtkErrorMacro(<< "Coherent File List must have at least a valid File*.");
+ return;
+ }
+ }
+ else if (this->InternalFileNameList.empty())
+ {
+ vtkErrorMacro(<< "A least a valid FileName must be specified.");
+ return;
+ }
+*/
+}
+
+void vtkGdcmReader::BuildData(vtkDataObject *output)
+{
+ vtkImageData *data = this->AllocateOutputData(output);
+
+ data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
+
+ // Test if output has valid extent
+ // Prevent memory errors
+ if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
+ (this->DataExtent[3]-this->DataExtent[2]>=0) &&
+ (this->DataExtent[5]-this->DataExtent[4]>=0))
+ {
+ // The memory size for a full stack of images of course depends
+ // on the number of planes and the size of each image:
+ //size_t StackNumPixels = this->NumColumns * this->NumLines
+ // * this->TotalNumberOfPlanes * this->NumComponents;
+ //size_t stack_size = StackNumPixels * this->PixelSize; //not used
+ // Allocate pixel data space itself.
+
+ // Variables for the UpdateProgress. We shall use 50 steps to signify
+ // the advance of the process:
+ unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
+ * this->TotalNumberOfPlanes
+ / 50.0);
+ // The actual advance measure:
+ unsigned long UpdateProgressCount = 0;
+
+ // Filling the allocated memory space with each image/volume:
+
+ size_t size = this->NumColumns * this->NumLines * this->NumPlanes
+ * data->GetScalarSize() * this->NumComponents;
+ unsigned char *Dest = (unsigned char *)data->GetScalarPointer();
+ for (std::vector<gdcm::File* >::iterator it = InternalFileList.begin();
+ it != InternalFileList.end();
+ ++it)
+ {
+ this->LoadImageInMemory(*it, Dest,
+ UpdateProgressTarget,
+ UpdateProgressCount);
+ Dest += size;
+ }
+ }
+}
+
+/*
+ * vtkGdcmReader can have the file names specified through two ways:
+ * (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
+ * SetFilePattern()
+ * (2) By successive calls to vtkGdcmReader::AddFileName()
+ * When the first method was used by caller we need to update the local
+ * filename list
+ */
+void vtkGdcmReader::BuildFileListFromPattern()
+{
+ this->RemoveAllInternalFileName();
+
+ // Test miscellanous cases
+ if ((! this->FileNameList.empty()) && this->FileName )
+ {
+ vtkErrorMacro(<< "Both AddFileName and SetFileName schemes were used");
+ vtkErrorMacro(<< "No images loaded ! ");
+ return;
+ }
+
+ if ((! this->FileNameList.empty()) && this->FilePrefix )
+ {
+ vtkErrorMacro(<< "Both AddFileName and SetFilePrefix schemes were used");
+ vtkErrorMacro(<< "No images loaded ! ");
+ return;
+ }
+
+ if (this->FileName && this->FilePrefix)
+ {
+ vtkErrorMacro(<< "Both SetFileName and SetFilePrefix schemes were used");
+ vtkErrorMacro(<< "No images loaded ! ");
+ return;
+ }
+
+ // Create the InternalFileNameList
+ if (! this->FileNameList.empty() )
+ {
+ vtkDebugMacro(<< "Using the AddFileName specified files");
+ this->InternalFileNameList=this->FileNameList;
+ return;
+ }
+
+ if (!this->FileName && !this->FilePrefix)
+ {
+ vtkErrorMacro(<< "FileNames are not set. Either use AddFileName() or");
+ vtkErrorMacro(<< "specify a FileName or FilePrefix.");
+ return;
+ }
+
+ if( this->FileName )
+ {
+ // Single file loading (as given with ::SetFileName()):
+ // Case of multi-frame file considered here
+ this->ComputeInternalFileName(this->DataExtent[4]);
+ vtkDebugMacro(<< "Adding file " << this->InternalFileName);
+ this->AddInternalFileName(this->InternalFileName);
+ }
+ else
+ {
+ // Multi file loading (as given with ::SetFilePattern()):
+ for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
+ {
+ this->ComputeInternalFileName(idx);
+ vtkDebugMacro(<< "Adding file " << this->InternalFileName);
+ this->AddInternalFileName(this->InternalFileName);
+ }
+ }
+}
+
+/**
+ * Load all the files and set it in the InternalFileList
+ * For each file, the readability and the coherence of image caracteristics
+ * are tested. If an image doesn't agree the required specifications, it
+ * isn't considered and no data will be set for the planes corresponding
+ * to this image
+ *
+ * The source of this work is the list of file name generated by the
+ * BuildFileListFromPattern method
+ */
+void vtkGdcmReader::LoadFileInformation()
+{
+ gdcm::File *file;
+ bool foundReference=false;
+ std::string type;
+
+ this->OwnFile=true;
+ for (std::list<std::string>::iterator filename = InternalFileNameList.begin();
+ filename != InternalFileNameList.end();
+ ++filename)
+ {
+ // check for file readability
+ FILE *fp;
+ fp = fopen(filename->c_str(),"rb");
+ if (!fp)
+ {
+ vtkErrorMacro(<< "Unable to open file " << filename->c_str());
+ vtkErrorMacro(<< "Removing this file from read files: "
+ << filename->c_str());
+ file = NULL;
+ InternalFileList.push_back(file);
+ continue;
+ }
+ fclose(fp);
+
+ // Read the file
+ file=gdcm::File::New();
+ file->SetLoadMode( LoadMode );
+ file->SetFileName(filename->c_str() );
+ file->Load();
+
+ // Test the Dicom file readability
+ if(!file->IsReadable())
+ {
+ vtkErrorMacro(<< "Gdcm cannot parse file " << filename->c_str());
+ vtkErrorMacro(<< "Removing this file from read files: "
+ << filename->c_str());
+ file->Delete();
+ file=NULL;
+ InternalFileList.push_back(file);
+ continue;
+ }
+
+ // Test the Pixel Type recognition
+ type = file->GetPixelType();
+ if ( (type != "8U") && (type != "8S")
+ && (type != "16U") && (type != "16S")
+ && (type != "32U") && (type != "32S") )
+ {
+ vtkErrorMacro(<< "Bad File Type for file " << filename->c_str() << "\n"
+ << " File type found : " << type.c_str()
+ << " (might be 8U, 8S, 16U, 16S, 32U, 32S) \n"
+ << " Removing this file from read files");
+ file->Delete();
+ file=NULL;
+ InternalFileList.push_back(file);
+ continue;
+ }
+
+ // Test the image informations
+ if(!foundReference)
+ {
+ foundReference = true;
+ GetFileInformation(file);
+
+ vtkDebugMacro(<< "This file taken as coherence reference:"
+ << filename->c_str());
+ vtkDebugMacro(<< "Image dimensions of reference file as read from Gdcm:"
+ << this->NumColumns << " " << this->NumLines << " "
+ << this->NumPlanes);
+ }
+ else if(!TestFileInformation(file))
+ {
+ file->Delete();
+ file=NULL;
+ }
+
+ InternalFileList.push_back(file);
+ }
+}
+
+/**
+ * Update the file informations.
+ * This works exactly like LoadFileInformation, but the source of work
+ * is the list of coherent files
+ */
+void vtkGdcmReader::UpdateFileInformation()
+{
+ this->InternalFileList=*(this->CoherentFileList);
+ this->OwnFile=false;
+
+ for(gdcmFileList::iterator it=InternalFileList.begin();
+ it!=InternalFileList.end();
+ ++it)
+ {
+ if( *it != NULL)
+ {
+ GetFileInformation(*it);
+ break;
+ }
+ }
+}
+
+/**
+ * Get the informations from a file.
+ * These informations are required to specify the output image
+ * caracteristics
+ */
+void vtkGdcmReader::GetFileInformation(gdcm::File *file)
+{
+ // Get the image caracteristics
+ this->NumColumns = file->GetXSize();
+ this->NumLines = file->GetYSize();
+ this->NumPlanes = file->GetZSize();
+
+ if (CoherentFileList == 0)
+ this->TotalNumberOfPlanes = this->NumPlanes*InternalFileNameList.size();
+ else
+ this->TotalNumberOfPlanes = this->NumPlanes*CoherentFileList->size();
+
+ this->ImageType = file->GetPixelType();
+ this->PixelSize = file->GetPixelSize();
+
+ this->DataSpacing[0] = file->GetXSpacing();
+ this->DataSpacing[1] = file->GetYSpacing();
+ this->DataSpacing[2] = file->GetZSpacing();
+
+ // Get the image data caracteristics
+ if( file->HasLUT() && this->AllowLookupTable )
+ {
+ // I could raise an error is AllowLookupTable is on and HasLUT() off
+ this->NumComponents = file->GetNumberOfScalarComponentsRaw();
+ }
+ else
+ {
+ this->NumComponents = file->GetNumberOfScalarComponents(); //rgb or mono
+ }
+}
+
+/*
+ * When more than one filename is specified (i.e. we expect loading
+ * a stack or volume) we need to check that the corresponding images/volumes
+ * to be loaded are coherent i.e. to make sure:
+ * - they all share the same X dimensions
+ * - they all share the same Y dimensions
+ * - they all share the same ImageType ( 8 bit signed, or unsigned...)
+ *
+ * Eventually, we emit a warning when all the files do NOT share the
+ * Z dimension, since we can still build a stack but the
+ * files are not coherent in Z, which is probably a source a trouble...
+ * When files are not readable (either the file cannot be opened or
+ * because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".
+ * This method returns the total number of planar images to be loaded
+ * (i.e. an image represents one plane, but a volume represents many planes)
+ */
+/**
+ * Test the coherent informations of the file with the reference informations
+ * used as image caracteristics. The tested informations are :
+ * - they all share the same X dimensions
+ * - they all share the same Y dimensions
+ * - they all share the same Z dimensions
+ * - they all share the same number of components
+ * - they all share the same ImageType ( 8 bit signed, or unsigned...)
+ *
+ * \return True if the file match, False otherwise
+ */
+bool vtkGdcmReader::TestFileInformation(gdcm::File *file)
+{
+ int numColumns = file->GetXSize();
+ int numLines = file->GetYSize();
+ int numPlanes = file->GetZSize();
+ int numComponents;
+ unsigned int pixelSize = file->GetPixelSize();
+
+ if( file->HasLUT() && this->AllowLookupTable )
+ numComponents = file->GetNumberOfScalarComponentsRaw();
+ else
+ numComponents = file->GetNumberOfScalarComponents(); //rgb or mono
+
+ if( numColumns != this->NumColumns )
+ {
+ vtkErrorMacro(<< "File X value doesn't match with the previous ones: "
+ << file->GetFileName().c_str()
+ << ". Found " << numColumns << ", must be "
+ << this->NumColumns);
+ return false;
+ }
+ if( numLines != this->NumLines )
+ {
+ vtkErrorMacro(<< "File Y value doesn't match with the previous ones: "
+ << file->GetFileName().c_str()
+ << ". Found " << numLines << ", must be "
+ << this->NumLines);
+ return false;
+ }
+ if( numPlanes != this->NumPlanes )
+ {
+ vtkErrorMacro(<< "File Z value doesn't match with the previous ones: "
+ << file->GetFileName().c_str()
+ << ". Found " << numPlanes << ", must be "
+ << this->NumPlanes);
+ return false;
+ }
+ if( numComponents != this->NumComponents )
+ {
+ vtkErrorMacro(<< "File Components count doesn't match with the previous ones: "
+ << file->GetFileName().c_str()
+ << ". Found " << numComponents << ", must be "
+ << this->NumComponents);
+ return false;
+ }
+ if( pixelSize != this->PixelSize )
+ {
+ vtkErrorMacro(<< "File pixel size doesn't match with the previous ones: "
+ << file->GetFileName().c_str()
+ << ". Found " << pixelSize << ", must be "
+ << this->PixelSize);
+ return false;
+ }
+
+ return true;