+/*
+ * 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();
+
+ 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;
+ }
+
+ 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);
+ }
+ }
+}
+
+/*
+ * 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)
+ */
+int vtkGdcmReader::CheckFileCoherence()
+{
+ int ReturnedTotalNumberOfPlanes = 0; // The returned value.
+
+ this->BuildFileListFromPattern();
+ if (this->InternalFileNameList.empty())
+ {
+ vtkErrorMacro(<< "FileNames are not set.");
+ return 0;
+ }
+
+ bool FoundReferenceFile = false;
+ int ReferenceNZ = 0;
+
+ // Loop on the filenames:
+ // - check for their existence and gdcm "parsability"
+ // - get the coherence check done:
+ for (std::list<std::string>::iterator filename = InternalFileNameList.begin();
+ filename != InternalFileNameList.end();
+ ++filename)
+ {
+ // The file is always added in the number of planes
+ // - If file doesn't exist, it will be replaced by a black plane in the
+ // ExecuteData method
+ // - If file has more than 1 plane, other planes will be added later to
+ // to the ReturnedTotalNumberOfPlanes variable counter
+ ReturnedTotalNumberOfPlanes += 1;
+
+ /////// Stage 0: check for file name:
+ if(*filename == std::string("GDCM_UNREADABLE"))
+ continue;
+
+ /////// Stage 1: check for file readability:
+ // Stage 1.1: check for file existence.
+ FILE *fp;
+ fp = fopen(filename->c_str(),"rb");
+ if (!fp)
+ {
+ vtkErrorMacro(<< "Unable to open file " << filename->c_str());
+ vtkErrorMacro(<< "Removing this file from readed files "
+ << filename->c_str());
+ *filename = "GDCM_UNREADABLE";
+ continue;
+ }
+ fclose(fp);
+
+ // Stage 1.2: check for Gdcm parsability
+ gdcm::File GdcmFile(filename->c_str() );
+ if (!GdcmFile.IsReadable())
+ {
+ vtkErrorMacro(<< "Gdcm cannot parse file " << filename->c_str());
+ vtkErrorMacro(<< "Removing this file from readed files "
+ << filename->c_str());
+ *filename = "GDCM_UNREADABLE";
+ continue;
+ }
+
+ // Stage 1.3: further gdcm compatibility on PixelType
+ std::string type = GdcmFile.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 readed files");
+ *filename = "GDCM_UNREADABLE";
+ continue;
+ }
+
+ // Stage 2: check coherence of the set of files
+ int NX = GdcmFile.GetXSize();
+ int NY = GdcmFile.GetYSize();
+ int NZ = GdcmFile.GetZSize();
+ if (FoundReferenceFile)
+ {
+ // Stage 2.1: mandatory coherence stage:
+ if ( ( NX != this->NumColumns )
+ || ( NY != this->NumLines )
+ || ( type != this->ImageType ) )
+ {
+ vtkErrorMacro(<< "This file is not coherent with previous ones"
+ << filename->c_str());
+ vtkErrorMacro(<< "Removing this file from readed files "
+ << filename->c_str());
+ *filename = "GDCM_UNREADABLE";
+ continue;
+ }
+
+ // Stage 2.2: optional coherence stage
+ if ( NZ != ReferenceNZ )
+ {
+ vtkErrorMacro(<< "File is not coherent in Z with previous ones"
+ << filename->c_str());
+ }
+ else
+ {
+ vtkDebugMacro(<< "File is coherent with previous ones"
+ << filename->c_str());
+ }
+
+ // Stage 2.3: when the file contains a volume (as opposed to an image),
+ // notify the caller.
+ if (NZ > 1)
+ {
+ vtkErrorMacro(<< "This file contains multiple planes (images)"
+ << filename->c_str());
+ }
+
+ // Eventually, this file can be added on the stack. Update the
+ // full size of the stack
+ vtkDebugMacro("Number of planes added to the stack: " << NZ);
+ ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
+ continue;
+
+ }
+ else
+ {
+ // We didn't have a workable reference file yet.
+ // Set this one as the reference.
+ FoundReferenceFile = true;
+ vtkDebugMacro(<< "This file taken as coherence reference:"
+ << filename->c_str());
+ vtkDebugMacro(<< "Image dimension of reference file as read from Gdcm:"
+ << NX << " " << NY << " " << NZ);
+ vtkDebugMacro(<< "Number of planes added to the stack: " << NZ);
+ // Set aside the size of the image
+ this->NumColumns = NX;
+ this->NumLines = NY;
+ ReferenceNZ = NZ;
+ ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
+ this->ImageType = type;
+ this->PixelSize = GdcmFile.GetPixelSize();
+
+ if( GdcmFile.HasLUT() && this->AllowLookupTable )
+ {
+ // I could raise an error is AllowLookupTable is on and HasLUT() off
+ this->NumComponents = GdcmFile.GetNumberOfScalarComponentsRaw();
+ }
+ else
+ {
+ this->NumComponents = GdcmFile.GetNumberOfScalarComponents(); //rgb or mono
+ }
+
+ //Set image spacing
+ this->DataSpacing[0] = GdcmFile.GetXSpacing();
+ this->DataSpacing[1] = GdcmFile.GetYSpacing();
+ this->DataSpacing[2] = GdcmFile.GetZSpacing();
+
+ //Set image origin
+ //this->DataOrigin[0] = GdcmFile.GetXOrigin();
+ //this->DataOrigin[1] = GdcmFile.GetYOrigin();
+ //this->DataOrigin[2] = GdcmFile.GetZOrigin();
+
+ }
+ } // End of loop on filename
+
+ ///////// The files we CANNOT load are flaged. On debugging purposes
+ // count the loadable number of files and display their number:
+ int NumberCoherentFiles = 0;
+ for (std::list<std::string>::iterator it = InternalFileNameList.begin();
+ it != InternalFileNameList.end();
+ ++it)
+ {
+ if (*it != "GDCM_UNREADABLE")
+ {
+ NumberCoherentFiles++;
+ }
+ }
+ vtkDebugMacro(<< "Number of coherent files: " << NumberCoherentFiles);
+
+ if (ReturnedTotalNumberOfPlanes == 0)
+ {
+ vtkErrorMacro(<< "No loadable file.");
+ }
+
+ vtkDebugMacro(<< "Total number of planes on the stack: "
+ << ReturnedTotalNumberOfPlanes);
+
+ return ReturnedTotalNumberOfPlanes;
+}
+
+//-----------------------------------------------------------------------------
+// Private
+/*
+ * Remove all file names to the internal list of images to read.
+ */
+void vtkGdcmReader::RemoveAllInternalFileName(void)
+{
+ this->InternalFileNameList.clear();
+}
+
+/*
+ * Adds a file name to the internal list of images to read.
+ */
+void vtkGdcmReader::AddInternalFileName(const char* name)