+//----------------------------------------------------------------------------
+// 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
+ gdcmHeaderHelper GdcmHeader(FileName->c_str());
+ if (!GdcmHeader.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 = GdcmHeader.GetPixelType();
+ if ( (type != "8U") && (type != "8S")
+ && (type != "16U") && (type != "16S")
+ && (type != "32U") && (type != "32S") )
+ {
+ vtkErrorMacro("Bad File Type for file" << FileName->c_str());
+ vtkErrorMacro(" " << type.c_str());
+ vtkErrorMacro("Removing this file from readed files "
+ << FileName->c_str());
+ *FileName = "GDCM_UNREADABLE";
+ continue;
+ }
+
+ /////// Stage 2: check coherence of the set of files
+ int NX = GdcmHeader.GetXSize();
+ int NY = GdcmHeader.GetYSize();
+ int NZ = GdcmHeader.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 = GdcmHeader.GetPixelSize();
+
+ if( GdcmHeader.HasLUT() )
+ {
+ this->NumComponents = GdcmHeader.GetNumberOfScalarComponentsRaw();
+ }
+ else
+ {
+ this->NumComponents = GdcmHeader.GetNumberOfScalarComponents(); //rgb or mono
+ }
+
+ //Set image spacing
+ this->DataSpacing[0] = GdcmHeader.GetXSpacing();
+ this->DataSpacing[1] = GdcmHeader.GetYSpacing();
+ this->DataSpacing[2] = GdcmHeader.GetZSpacing();
+
+ //Set image origin
+ this->DataOrigin[0] = GdcmHeader.GetXOrigin();
+ this->DataOrigin[1] = GdcmHeader.GetYOrigin();
+ this->DataOrigin[2] = GdcmHeader.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 Filename = InternalFileNameList.begin();
+ Filename != InternalFileNameList.end();
+ ++Filename)
+ {
+ if (*Filename != "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;
+}
+
+//----------------------------------------------------------------------------