1 // $Header: /cvs/public/gdcm/vtk/vtkGdcmReader.cxx,v 1.16 2003/07/07 10:26:14 regrain Exp $
3 #include <vtkObjectFactory.h>
4 #include <vtkImageData.h>
5 #include <vtkPointData.h>
6 #include "vtkGdcmReader.h"
9 vtkGdcmReader::vtkGdcmReader()
14 //----------------------------------------------------------------------------
15 vtkGdcmReader::~vtkGdcmReader()
18 this->RemoveAllFileName();
19 this->InternalFileNameList.clear();
22 //----------------------------------------------------------------------------
23 // Remove all files from the list of images to read.
24 void vtkGdcmReader::RemoveAllFileName(void)
26 this->FileNameList.clear();
29 //----------------------------------------------------------------------------
30 // Adds a file name to the list of images to read.
31 void vtkGdcmReader::AddFileName(const char* name)
33 // We need to bypass the const pointer [since list<>.push_bash() only
34 // takes a char* (but not a const char*)] by making a local copy:
35 char * LocalName = new char[strlen(name) + 1];
36 strcpy(LocalName, name);
37 this->FileNameList.push_back(LocalName);
42 //----------------------------------------------------------------------------
43 // Sets up a filename to be read.
44 void vtkGdcmReader::SetFileName(const char *name) {
45 vtkImageReader2::SetFileName(name);
46 // Since we maintain a list of filenames, when building a volume,
47 // (see vtkGdcmReader::AddFileName), we additionaly need to purge
48 // this list when we manually positionate the filename.
49 this->FileNameList.clear();
53 //----------------------------------------------------------------------------
54 // Adds a file name to the internal list of images to read.
55 void vtkGdcmReader::RemoveAllInternalFileName(void)
57 this->InternalFileNameList.clear();
60 //----------------------------------------------------------------------------
61 // Adds a file name to the internal list of images to read.
62 void vtkGdcmReader::AddInternalFileName(const char* name)
64 char * LocalName = new char[strlen(name) + 1];
65 strcpy(LocalName, name);
66 this->InternalFileNameList.push_back(LocalName);
70 //----------------------------------------------------------------------------
71 // vtkGdcmReader can have the file names specified through two ways:
72 // (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
74 // (2) By successive calls to vtkGdcmReader::AddFileName()
75 // When the first method was used by caller we need to update the local
77 void vtkGdcmReader::BuildFileListFromPattern()
79 if ((! this->FileNameList.empty()) && this->FileName )
81 vtkErrorMacro("Both file patterns and AddFileName schemes were used");
82 vtkErrorMacro("Only the files specified with AddFileName shall be used");
86 if (! this->FileNameList.empty() )
88 vtkDebugMacro("Using the AddFileName specified files");
89 this->InternalFileNameList=this->FileNameList;
93 if (!this->FileName && !this->FilePattern)
95 vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
96 vtkErrorMacro("specify a FileName or FilePattern.");
100 this->RemoveAllInternalFileName();
101 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
103 this->ComputeInternalFileName(idx);
104 vtkDebugMacro("Adding file " << this->InternalFileName);
105 this->AddInternalFileName(this->InternalFileName);
109 //----------------------------------------------------------------------------
110 // When more than one filename is specified (i.e. we expect loading
111 // a stack or volume) we need to check that the corresponding images/volumes
112 // to be loaded are coherent i.e. to make sure:
113 // - they all share the same X dimensions
114 // - they all share the same Y dimensions
115 // - they all share the same ImageType ( 8 bit signed, or unsigned...)
117 // Eventually, we emit a warning when all the files do NOT share the
118 // Z dimension, since we can still build a stack but the
119 // files are not coherent in Z, which is probably a source a trouble...
120 // When files are not readable (either the file cannot be opened or
121 // because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".
122 // This method returns the total number of planar images to be loaded
123 // (i.e. an image represents one plane, but a volume represents many planes)
124 int vtkGdcmReader::CheckFileCoherence()
126 int ReturnedTotalNumberOfPlanes = 0; // The returned value.
128 this->BuildFileListFromPattern();
129 if (this->InternalFileNameList.empty())
131 vtkErrorMacro("FileNames are not set.");
135 bool FoundReferenceFile = false;
138 // Loop on the filenames:
139 // - check for their existence and gdcm "parasability"
140 // - get the coherence check done:
141 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
142 FileName != InternalFileNameList.end();
145 // The file is always added in the number of planes
146 // - If file doesn't exist, it will be replaced by a black place in the
147 // ExecuteData method
148 // - If file has more than 1 plane, other planes will be added later to
149 // to the ReturnedTotalNumberOfPlanes variable counter
150 ReturnedTotalNumberOfPlanes += 1;
152 /////// Stage 0: check for file name:
153 if(*FileName==std::string("GDCM_UNREADABLE"))
156 /////// Stage 1: check for file readability:
157 // Stage 1.1: check for file existence.
159 fp = fopen(FileName->c_str(),"rb");
162 vtkErrorMacro("Unable to open file " << FileName->c_str());
163 vtkErrorMacro("Removing this file from readed files "
164 << FileName->c_str());
165 *FileName = "GDCM_UNREADABLE";
170 // Stage 1.2: check for Gdcm parsability
171 gdcmHeader GdcmHeader(FileName->c_str());
172 if (!GdcmHeader.IsReadable())
174 vtkErrorMacro("Gdcm cannot parse file " << FileName->c_str());
175 vtkErrorMacro("Removing this file from readed files "
176 << FileName->c_str());
177 *FileName = "GDCM_UNREADABLE";
181 // Stage 1.3: further gdcm compatibility on PixelType
182 std::string type = GdcmHeader.GetPixelType();
183 if ( (type != "8U") && (type != "8S")
184 && (type != "16U") && (type != "16S")
185 && (type != "32U") && (type != "32S") )
187 vtkErrorMacro("Bad File Type for file" << FileName->c_str());
188 vtkErrorMacro("Removing this file from readed files "
189 << FileName->c_str());
190 *FileName = "GDCM_UNREADABLE";
194 /////// Stage 2: check coherence of the set of files
195 int NX = GdcmHeader.GetXSize();
196 int NY = GdcmHeader.GetYSize();
197 int NZ = GdcmHeader.GetZSize();
198 if (FoundReferenceFile)
201 // Stage 2.1: mandatory coherence stage:
202 if ( ( NX != this->NumColumns )
203 || ( NY != this->NumLines )
204 || ( type != this->ImageType ) )
206 vtkErrorMacro("This file is not coherent with previous ones"
207 << FileName->c_str());
208 vtkErrorMacro("Removing this file from readed files "
209 << FileName->c_str());
210 *FileName = "GDCM_UNREADABLE";
214 // Stage 2.2: optional coherence stage
215 if ( NZ != ReferenceNZ )
217 vtkErrorMacro("File is not coherent in Z with previous ones"
218 << FileName->c_str());
222 vtkDebugMacro("File is coherent with previous ones"
223 << FileName->c_str());
226 // Stage 2.3: when the file contains a volume (as opposed to an image),
227 // notify the caller.
230 vtkErrorMacro("This file contains multiple planes (images)"
231 << FileName->c_str());
234 // Eventually, this file can be added on the stack. Update the
235 // full size of the stack
236 vtkDebugMacro("Number of planes added to the stack: " << NZ);
237 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
241 // We didn't have a workable reference file yet. Set this one
243 FoundReferenceFile = true;
244 vtkDebugMacro("This file taken as coherence reference:"
245 << FileName->c_str());
246 vtkDebugMacro("Image dimension of reference file as read from Gdcm:" <<
247 NX << " " << NY << " " << NZ);
248 vtkDebugMacro("Number of planes added to the stack: " << NZ);
249 // Set aside the size of the image
250 this->NumColumns = NX;
253 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
254 this->ImageType = type;
255 this->PixelSize = GdcmHeader.GetPixelSize();
257 } // End of loop on FileName
259 ///////// The files we CANNOT load are flaged. On debugging purposes
260 // count the loadable number of files and display thir number:
261 int NumberCoherentFiles = 0;
262 for (std::list<std::string>::iterator Filename = InternalFileNameList.begin();
263 Filename != InternalFileNameList.end();
266 if (*Filename != "GDCM_UNREADABLE")
267 NumberCoherentFiles++;
269 vtkDebugMacro("Number of coherent files: " << NumberCoherentFiles);
271 if (ReturnedTotalNumberOfPlanes == 0)
273 vtkErrorMacro("No loadable file.");
276 vtkDebugMacro("Total number of planes on the stack: "
277 << ReturnedTotalNumberOfPlanes);
279 return ReturnedTotalNumberOfPlanes;
282 //----------------------------------------------------------------------------
283 // Configure the output e.g. WholeExtent, spacing, origin, scalar type...
284 void vtkGdcmReader::ExecuteInformation()
286 //FIXME free any old memory
287 this->TotalNumberOfPlanes = this->CheckFileCoherence();
288 if ( this->TotalNumberOfPlanes == 0)
290 vtkErrorMacro("File set is not coherent. Exiting...");
294 // if the user has not set the extent, but has set the VOI
295 // set the zaxis extent to the VOI z axis
296 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
297 (this->DataVOI[4] || this->DataVOI[5]))
299 this->DataExtent[4] = this->DataVOI[4];
300 this->DataExtent[5] = this->DataVOI[5];
303 // When the user has set the VOI, check it's coherence with the file content.
304 if (this->DataVOI[0] || this->DataVOI[1] ||
305 this->DataVOI[2] || this->DataVOI[3] ||
306 this->DataVOI[4] || this->DataVOI[5])
308 if ((this->DataVOI[0] < 0) ||
309 (this->DataVOI[1] >= this->NumColumns) ||
310 (this->DataVOI[2] < 0) ||
311 (this->DataVOI[3] >= this->NumLines) ||
312 (this->DataVOI[4] < 0) ||
313 (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
315 vtkWarningMacro("The requested VOI is larger than expected extent.");
316 this->DataVOI[0] = 0;
317 this->DataVOI[1] = this->NumColumns - 1;
318 this->DataVOI[2] = 0;
319 this->DataVOI[3] = this->NumLines - 1;
320 this->DataVOI[4] = 0;
321 this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
325 // Positionate the Extent.
326 this->DataExtent[0] = 0;
327 this->DataExtent[1] = this->NumColumns - 1;
328 this->DataExtent[2] = 0;
329 this->DataExtent[3] = this->NumLines - 1;
330 if(this->InternalFileNameList.size() > 1)
332 this->DataExtent[4] = 0;
333 this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
336 // We don't need to positionate the Endian related stuff (by using
337 // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
338 // since the reading of the file is done by gdcm.
339 // But we do need to set up the data type for downstream filters:
340 if ( ImageType == "8U" )
342 vtkDebugMacro("8 bits unsigned image");
343 this->SetDataScalarTypeToUnsignedChar();
345 else if ( ImageType == "8S" )
347 vtkErrorMacro("Cannot handle 8 bit signed files");
350 else if ( ImageType == "16U" )
352 vtkDebugMacro("16 bits unsigned image");
353 this->SetDataScalarTypeToUnsignedShort();
355 else if ( ImageType == "16S" )
357 vtkDebugMacro("16 bits signed image");
358 this->SetDataScalarTypeToShort();
359 //vtkErrorMacro("Cannot handle 16 bit signed files");
361 else if ( ImageType == "32U" )
363 vtkDebugMacro("32 bits unsigned image");
364 vtkDebugMacro("WARNING: forced to signed int !");
365 this->SetDataScalarTypeToInt();
367 else if ( ImageType == "32S" )
369 vtkDebugMacro("32 bits signed image");
370 this->SetDataScalarTypeToInt();
373 vtkImageReader::ExecuteInformation();
376 //----------------------------------------------------------------------------
377 // Loads the contents of the image/volume contained by Filename at
378 // the Dest memory address. Returns the size of the data loaded.
379 size_t vtkGdcmReader::LoadImageInMemory(
380 std::string FileName,
381 unsigned char * Dest,
382 const unsigned long UpdateProgressTarget,
383 unsigned long & UpdateProgressCount)
385 vtkDebugMacro("Copying to memmory image" << FileName.c_str());
386 gdcmFile GdcmFile(FileName.c_str());
387 size_t size = GdcmFile.GetImageDataSize();
389 // If the data structure of vtk for image/volume representation
390 // were straigthforwards the following would suffice:
391 // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
392 // But vtk chose to invert the lines of an image, that is the last
393 // line comes first (for some axis related reasons?). Hence we need
394 // to load the image line by line, starting from the end:
395 int NumColumns = GdcmFile.GetXSize();
396 int NumLines = GdcmFile.GetYSize();
397 int NumPlanes = GdcmFile.GetZSize();
398 int LineSize = NumColumns * GdcmFile.GetPixelSize();
399 unsigned char * Source = (unsigned char*)GdcmFile.GetImageData();
400 unsigned char * Destination = Dest + size - LineSize;
402 for (int plane = 0; plane < NumPlanes; plane++)
404 for (int line = 0; line < NumLines; line++)
406 // Copy one line at proper destination:
407 memcpy((void*)Destination, (void*)Source, LineSize);
409 Destination -= LineSize;
410 // Update progress related:
411 if (!(UpdateProgressCount%UpdateProgressTarget))
413 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
415 UpdateProgressCount++;
421 //----------------------------------------------------------------------------
422 // Update => ouput->Update => UpdateData => Execute => ExecuteData
423 // (see vtkSource.cxx for last step).
424 // This function (redefinition of vtkImageReader::ExecuteData, see
425 // VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
426 // extent/axes are assumed to be the
427 // same as the file extent/order.
428 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
430 if (this->InternalFileNameList.empty())
432 vtkErrorMacro("A least a valid FileName must be specified.");
436 // FIXME : the bad parse of header is made when allocating OuputData
437 vtkImageData *data = this->AllocateOutputData(output);
438 data->SetExtent(this->DataExtent);
439 data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
441 // Test if output has valid extent
442 // Prevent memory errors
443 if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
444 (this->DataExtent[3]-this->DataExtent[2]>=0) &&
445 (this->DataExtent[5]-this->DataExtent[4]>=0))
447 // The memory size for a full stack of images of course depends
448 // on the number of planes and the size of each image:
449 size_t StackNumPixels = this->NumColumns * this->NumLines
450 * this->TotalNumberOfPlanes;
451 size_t stack_size = StackNumPixels * this->PixelSize;
452 // Allocate pixel data space itself.
453 unsigned char *mem = new unsigned char [stack_size];
455 // Variables for the UpdateProgress. We shall use 50 steps to signify
456 // the advance of the process:
457 unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
458 * this->TotalNumberOfPlanes
460 // The actual advance measure:
461 unsigned long UpdateProgressCount = 0;
463 // Feeling the allocated memory space with each image/volume:
464 unsigned char * Dest = mem;
465 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
466 FileName != InternalFileNameList.end();
469 // Images that were tagged as unreadable in CheckFileCoherence()
470 // are substituted with a black image to let the caller visually
471 // notice something wrong is going on:
472 if (*FileName != "GDCM_UNREADABLE")
474 // Update progress related for good files is made in LoadImageInMemory
475 Dest += this->LoadImageInMemory(*FileName, Dest,
476 UpdateProgressTarget,
477 UpdateProgressCount);
479 // We insert a black image in the stack for the user to be aware that
480 // this image/volume couldn't be loaded. We simply skip one image
482 Dest += this->NumColumns * this->NumLines * this->PixelSize;
484 // Update progress related for bad files:
485 UpdateProgressCount += this->NumLines;
486 if (UpdateProgressTarget > 0)
488 if (!(UpdateProgressCount%UpdateProgressTarget))
490 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
493 } // Else, file not loadable
496 // The "size" of the vtkScalars data is expressed in number of points,
497 // and is not the memory size representing those points:
498 data->GetPointData()->GetScalars()->SetVoidArray(mem, StackNumPixels, 0);
503 //----------------------------------------------------------------------------
504 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
506 vtkImageReader::PrintSelf(os,indent);
507 os << indent << "Filenames : " << endl;
508 vtkIndent nextIndent = indent.GetNextIndent();
509 for (std::list<std::string>::iterator FileName = FileNameList.begin();
510 FileName != FileNameList.end();
513 os << nextIndent << FileName->c_str() << endl ;