+/*
+ * 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
+ gdcmHeaderHelper GdcmHeader(FileName->c_str(), false, true);
+ // true : for enableSequences
+ 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->AllowLookupTable )
+ {
+ // I could raise an error is AllowLookupTable is on and HasLUT() off
+ 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;
+}
+
+//-----------------------------------------------------------------------------
+// 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)
+{
+ char * LocalName = new char[strlen(name) + 1];
+ strcpy(LocalName, name);
+ this->InternalFileNameList.push_back(LocalName);
+ delete[] LocalName;
+}
+
+/*
+ * Loads the contents of the image/volume contained by Filename at
+ * the Dest memory address. Returns the size of the data loaded.
+ */
+size_t vtkGdcmReader::LoadImageInMemory(
+ std::string FileName,
+ unsigned char * Dest,
+ const unsigned long UpdateProgressTarget,
+ unsigned long & UpdateProgressCount)