+
+/*
+ * 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;
+}
+
+//-----------------------------------------------------------------------------
+// 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)
+{
+ vtkDebugMacro("Copying to memory image" << FileName.c_str());
+ gdcmFile GdcmFile(FileName.c_str());
+ size_t size;
+
+ // If the data structure of vtk for image/volume representation
+ // were straigthforwards the following would be enough:
+ // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
+ // But vtk chooses 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.GetHeader()->GetXSize();
+ int NumLines = GdcmFile.GetHeader()->GetYSize();
+ int NumPlanes = GdcmFile.GetHeader()->GetZSize();
+ int LineSize = NumComponents * NumColumns * GdcmFile.GetHeader()->GetPixelSize();
+
+ unsigned char * Source;
+
+ if( GdcmFile.GetHeader()->HasLUT() )
+ {
+ size = GdcmFile.GetImageDataSizeRaw();
+ Source = (unsigned char*) GdcmFile.GetImageDataRaw();
+ unsigned char *Lut = GdcmFile.GetHeader()->GetLUTRGBA();
+
+ if(!this->LookupTable)
+ this->LookupTable = vtkLookupTable::New();
+
+ this->LookupTable->SetNumberOfTableValues(256);
+ for (int tmp=0; tmp<256; tmp++)
+ {
+ this->LookupTable->SetTableValue(tmp,
+ (float)Lut[4*tmp+0]/255.0,
+ (float)Lut[4*tmp+1]/255.0,
+ (float)Lut[4*tmp+2]/255.0,
+ 1);
+ }
+ this->LookupTable->SetRange(0,255);
+ vtkDataSetAttributes *a=this->GetOutput()->GetPointData();
+ a->GetScalars()->SetLookupTable(this->LookupTable);
+ free(Lut);
+ }
+ else
+ {
+ size = GdcmFile.GetImageDataSize();
+ Source = (unsigned char*)GdcmFile.GetImageData();
+ }
+ unsigned char * pSource = Source; //pointer for later deletion
+ unsigned char * Destination = Dest + size - LineSize;
+
+ for (int plane = 0; plane < NumPlanes; plane++)
+ {
+ for (int line = 0; line < NumLines; line++)
+ {
+ // Copy one line at proper destination:
+ memcpy((void*)Destination, (void*)Source, LineSize);
+ Source += LineSize;
+ Destination -= LineSize;
+ // Update progress related:
+ if (!(UpdateProgressCount%UpdateProgressTarget))
+ {
+ this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
+ }
+ UpdateProgressCount++;
+ }
+ }
+ //GetImageData allocate a (void*)malloc, remove it:
+ free(pSource);
+ return size;
+}
+
+//-----------------------------------------------------------------------------