1 // $Header: /cvs/public/gdcm/vtk/vtkGdcmReader.cxx,v 1.6 2003/05/29 16:58:24 frog Exp $
2 //CLEANME#include <vtkByteSwap.h>
4 #include <vtkObjectFactory.h>
5 #include <vtkImageData.h>
6 #include <vtkPointData.h>
7 #include "vtkGdcmReader.h"
10 vtkGdcmReader::vtkGdcmReader()
15 //----------------------------------------------------------------------------
16 vtkGdcmReader::~vtkGdcmReader()
21 //----------------------------------------------------------------------------
22 // Adds a file name to the list of images to read.
23 void vtkGdcmReader::AddFileName(const char* name)
25 // We need to bypass the const pointer [since list<>.push_bash() only
26 // takes a char* (but not a const char*)] by making a local copy:
27 char * LocalName = new char[strlen(name) + 1];
28 strcpy(LocalName, name);
29 this->FileNameList.push_back(LocalName);
30 // Starting from two files we have a stack of images:
31 if(this->FileNameList.size() >= 2)
32 this->SetFileDimensionality(3);
35 //----------------------------------------------------------------------------
36 // Sets up a filename to be read.
37 void vtkGdcmReader::SetFileName(const char *name) {
38 vtkImageReader2::SetFileName(name);
39 // Since we maintain a list of filenames (when building a volume)
40 // we additionaly need to maintain this list. First we clean-up the
41 // list and then positionate the incoming filename:
42 this->FileNameList.empty();
43 this->AddFileName(name);
46 //----------------------------------------------------------------------------
47 // vtkGdcmReader can have the file names specified through two ways:
48 // (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
50 // (2) By successive calls to vtkGdcmReader::SetFileName()
51 // When the first method was used by caller we need to update the local
53 void vtkGdcmReader::BuilFileListFromPattern()
55 if (! this->FileNameList.empty())
57 if (!this->FileName && !this->FilePattern)
59 vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
60 vtkErrorMacro("specify a FileName or FilePattern.");
63 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
65 this->ComputeInternalFileName(idx);
66 vtkDebugMacro("Adding file " << this->InternalFileName);
67 this->AddFileName(this->InternalFileName);
71 //----------------------------------------------------------------------------
72 // When more than one filename is specified (i.e. we expect loading
73 // a stack or volume) we need to check the corresponding images are
75 // - they all share the same X dimensions
76 // - they all share the same Y dimensions
77 // - each file a Z dimension of 1
78 // - they all share the same type ( 8 bit signed, or unsigned...)
79 bool vtkGdcmReader::CheckFileCoherence()
81 this->BuilFileListFromPattern();
82 if (this->FileNameList.empty())
84 vtkErrorMacro("FileNames are not set.");
87 if (this->FileNameList.size() == 1)
89 vtkDebugMacro("Single file specified.");
93 // Loop on the filenames:
94 // - check for their existence and gdcm "parasability"
95 // - get the coherence check done:
96 bool FoundReferenceFile = false;
100 std::string ReferenceType;
101 for (std::list<std::string>::iterator FileName = FileNameList.begin();
102 FileName != FileNameList.end();
105 // Check for file existence.
107 fp = fopen(FileName->c_str(),"rb");
110 vtkErrorMacro("Unable to open file " << *FileName);
111 vtkErrorMacro("Removing this file from readed files " << *FileName);
112 FileNameList.remove(*FileName);
117 // Check for Gdcm parsability
118 gdcmHeader GdcmHeader(FileName->c_str());
119 if (!GdcmHeader.IsReadable())
121 vtkErrorMacro("Gdcm cannot parse file " << *FileName);
122 vtkErrorMacro("Removing this file from readed files " << *FileName);
123 FileNameList.remove(*FileName);
128 int NX = GdcmHeader.GetXSize();
129 int NY = GdcmHeader.GetYSize();
130 int NZ = GdcmHeader.GetZSize();
131 std::string type = GdcmHeader.GetPixelType();
132 if (FoundReferenceFile)
134 if ( ( NX != ReferenceNX )
135 || ( NY != ReferenceNY )
136 || ( NZ != ReferenceNZ )
137 || ( type != ReferenceType ) )
139 vtkErrorMacro("This file is not coherent with previous ones"
141 vtkErrorMacro("Removing this file from readed files " << *FileName);
142 FileNameList.remove(*FileName);
145 vtkDebugMacro("File is coherent with previous ones" << *FileName);
148 // This file shall be the reference:
149 FoundReferenceFile = true;
153 ReferenceType = type;
154 vtkDebugMacro("File is taken a coherence references" << *FileName);
156 } // End of loop on FileName
158 if (this->FileNameList.empty())
160 vtkDebugMacro("No gdcm parsable file.");
163 if (this->FileNameList.size() == 1)
165 vtkDebugMacro("Single parsable file left after coherence test.");
171 //----------------------------------------------------------------------------
172 // Configure the output e.g. WholeExtent, spacing, origin, scalar type...
173 void vtkGdcmReader::ExecuteInformation()
175 //FIXME free any old memory
177 // if the user has not set the extent, but has set the VOI
178 // set the zaxis extent to the VOI z axis
179 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
180 (this->DataVOI[4] || this->DataVOI[5]))
182 this->DataExtent[4] = this->DataVOI[4];
183 this->DataExtent[5] = this->DataVOI[5];
186 this->ComputeInternalFileName(this->DataExtent[4]);
188 // Check for file existence.
190 fp = fopen(this->InternalFileName,"rb");
193 vtkErrorMacro("Unable to open file " << this->InternalFileName);
198 // Check for Gdcm parsability
199 gdcmHeader GdcmHeader(this->InternalFileName);
200 if (!GdcmHeader.IsReadable())
202 vtkErrorMacro("Gdcm cannot parse file " << this->InternalFileName);
206 int NX = GdcmHeader.GetXSize();
207 int NY = GdcmHeader.GetYSize();
208 int NZ = GdcmHeader.GetZSize();
209 vtkDebugMacro("Image dimension as read from Gdcm:" <<
210 NX << " " << NY << " " << NZ);
212 if(NZ>1) this->SetFileDimensionality(3);
214 // When the user has set the VOI, check it's coherence with the file content.
215 if (this->DataVOI[0] || this->DataVOI[1] ||
216 this->DataVOI[2] || this->DataVOI[3] ||
217 this->DataVOI[4] || this->DataVOI[5])
219 if ((this->DataVOI[0] < 0) ||
220 (this->DataVOI[1] >= NX) ||
221 (this->DataVOI[2] < 0) ||
222 (this->DataVOI[3] >= NY) ||
223 (this->DataVOI[4] < 0) ||
224 (this->DataVOI[5] >= NZ))
226 vtkWarningMacro("The requested VOI is larger than the file's ("
227 << this->InternalFileName << ") extent ");
228 this->DataVOI[0] = 0;
229 this->DataVOI[1] = NX - 1;
230 this->DataVOI[2] = 0;
231 this->DataVOI[3] = NY - 1;
232 this->DataVOI[4] = 0;
233 this->DataVOI[5] = NZ - 1;
237 // Positionate the Extent.
238 this->DataExtent[0] = 0;
239 this->DataExtent[1] = NX - 1;
240 this->DataExtent[2] = 0;
241 this->DataExtent[3] = NY - 1;
242 if(this->GetFileDimensionality()==3)
244 this->DataExtent[4] = 0;
245 this->DataExtent[5] = NZ - 1;
248 // We don't need to positionate the Endian related stuff (by using
249 // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
250 // since the reading of the file is done by gdcm.
251 // But we do need to set up the data type for downstream filters:
252 std::string type = GdcmHeader.GetPixelType();
255 vtkDebugMacro("8 bits unsigned image");
256 this->SetDataScalarTypeToUnsignedChar();
258 else if ( type == "8S" )
260 vtkErrorMacro("Cannot handle 8 bit signed files");
263 else if ( type == "16U" )
265 vtkDebugMacro("16 bits unsigned image");
266 this->SetDataScalarTypeToUnsignedShort();
268 else if ( type == "16S" )
270 vtkDebugMacro("16 bits signed image");
271 this->SetDataScalarTypeToShort();
272 //vtkErrorMacro("Cannot handle 16 bit signed files");
274 else if ( type == "32U" )
276 vtkDebugMacro("32 bits unsigned image");
277 vtkDebugMacro("WARNING: forced to signed int !");
278 this->SetDataScalarTypeToInt();
280 else if ( type == "32S" )
282 vtkDebugMacro("32 bits signed image");
283 this->SetDataScalarTypeToInt();
287 vtkErrorMacro("Bad File Type " << this->InternalFileName
288 << "Type " << type.c_str());
292 vtkImageReader::ExecuteInformation();
295 //----------------------------------------------------------------------------
296 // Update -> UpdateData -> Execute -> ExecuteData (see vtkSource.cxx for
298 // This function (redefinition of vtkImageReader::ExecuteData, see
299 // VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
300 // extent/axes are assumed to be the
301 // same as the file extent/order.
302 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
306 vtkErrorMacro("A valid FileName must be specified.");
310 vtkImageData *data = this->AllocateOutputData(output);
311 data->SetExtent(this->DataExtent);
312 data->GetPointData()->GetScalars()->SetName("ImageFile");
314 // First check the coherence between the DataExtent and the
315 // size of the pixel data as annouced by gdcm (looks a bit paranoid).
316 gdcmFile GdcmFile(this->InternalFileName);
317 int NumColumns = this->DataExtent[1] - this->DataExtent[0] + 1;
318 int NumLines = this->DataExtent[3] - this->DataExtent[2] + 1;
319 int NumPlanes = this->DataExtent[5] - this->DataExtent[4] + 1;
320 size_t size = NumColumns * NumLines * NumPlanes * GdcmFile.GetPixelSize();
321 if ( size != GdcmFile.GetImageDataSize() )
323 vtkDebugMacro("Inconsistency with GetImageDataSize");
324 vtkDebugMacro("Number of scalar components"
325 << this->NumberOfScalarComponents);
327 // Allocate pixel data space itself.
328 unsigned char *mem = new unsigned char [size];
330 // If the data structure of vtk for image/volume representation
331 // were straigthforwards the following would suffice:
332 // GdcmFile.GetImageDataIntoVector((void*)mem, size);
333 // But vtk chose to invert the lines of an image, that is the last
334 // line comes first (for some axis related reasons?). Hence we need
335 // to load the image line by line, starting from the end:
336 int LineSize = NumColumns * GdcmFile.GetPixelSize();
337 unsigned char * Source = (unsigned char*)GdcmFile.GetImageData();
338 unsigned char * Destination = mem + size - LineSize;
339 for (int i = 0; i < NumLines; i++)
341 memcpy((void*)Destination, (void*)Source, LineSize);
343 Destination -= LineSize;
345 // The "size" of the vtkScalars data is expressed in number of points,
346 // and is not the memory size representing those points:
347 size = size / GdcmFile.GetPixelSize();
348 data->GetPointData()->GetScalars()->SetVoidArray(mem, size, 0);
353 //----------------------------------------------------------------------------
354 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
356 vtkImageReader::PrintSelf(os,indent);
357 os << indent << "Filenames : " << endl;
358 vtkIndent nextIndent = indent.GetNextIndent();
359 for (std::list<std::string>::iterator FileName = FileNameList.begin();
360 FileName != FileNameList.end();
363 os << nextIndent << *FileName << endl ;