2 //-----------------------------------------------------------------------------
3 // //////////////////////////////////////////////////////////////
4 // WARNING TODO CLENAME
5 // Actual limitations of this code:
7 // /////// Redundant and unnecessary header parsing
8 // In it's current state this code actually parses three times the Dicom
9 // header of a file before the corresponding image gets loaded in the
11 // Here is the process:
12 // 1/ First loading happens in ExecuteInformation which in order to
13 // positionate the vtk extents calls CheckFileCoherence. The purpose
14 // of CheckFileCoherence is to make sure all the images in the future
15 // stack are "homogenous" (same size, same representation...). This
16 // can only be achieved by parsing all the Dicom headers...
17 // 2/ ExecuteData is then responsible for the next two loadings:
18 // 2a/ ExecuteData calls AllocateOutputData that in turn seems to
19 // (indirectely call) ExecuteInformation which ends up in a second
21 // 2b/ the core of ExecuteData then needs gdcmFile (which in turns
22 // initialises gdcmHeader in the constructor) in order to access
26 // maintain a list of gdcmFiles (created by say ExecuteInformation) created
27 // once and for all accross the life of vtkGdcmHeader (it would only load
28 // new gdcmFile if the user changes the list). ExecuteData would then use
29 // those gdcmFile and hence avoid calling the construtor:
30 // - advantage: the header of the files would only be parser once.
31 // - drawback: once execute information is called (i.e. on creation of
32 // a vtkGdcmHeader) the gdcmFile structure is loaded in memory.
33 // The average size of a gdcmHeader being of 100Ko, is one
34 // loads 10 stacks of images with say 200 images each, you
35 // end-up with a loss of 200Mo...
37 // /////// Never unallocated memory:
38 // ExecuteData allocates space for the pixel data [which will get pointed
39 // by the vtkPointData() through the call
40 // data->GetPointData()->GetScalars()->SetVoidArray(mem, StackNumPixels, 0);]
41 // This data is never "freed" neither in the destructor nor when the
42 // filename list is extended, ExecuteData is called a second (or third)
44 // //////////////////////////////////////////////////////////////
47 #include "gdcmHeaderHelper.h"
48 #include "vtkGdcmReader.h"
51 #include <vtkObjectFactory.h>
52 #include <vtkImageData.h>
53 #include <vtkPointData.h>
54 #include <vtkLookupTable.h>
56 //-----------------------------------------------------------------------------
57 // Constructor / Destructor
58 vtkGdcmReader::vtkGdcmReader()
60 this->LookupTable = NULL;
63 vtkGdcmReader::~vtkGdcmReader()
65 this->RemoveAllFileName();
66 this->InternalFileNameList.clear();
68 this->LookupTable->Delete();
71 //-----------------------------------------------------------------------------
73 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
75 vtkImageReader::PrintSelf(os,indent);
76 os << indent << "Filenames : " << endl;
77 vtkIndent nextIndent = indent.GetNextIndent();
78 for (std::list<std::string>::iterator FileName = FileNameList.begin();
79 FileName != FileNameList.end();
82 os << nextIndent << FileName->c_str() << endl ;
86 //-----------------------------------------------------------------------------
89 * Remove all files from the list of images to read.
91 void vtkGdcmReader::RemoveAllFileName(void)
93 this->FileNameList.clear();
97 * Adds a file name to the list of images to read.
99 void vtkGdcmReader::AddFileName(const char* name)
101 // We need to bypass the const pointer [since list<>.push_bash() only
102 // takes a char* (but not a const char*)] by making a local copy:
103 char * LocalName = new char[strlen(name) + 1];
104 strcpy(LocalName, name);
105 this->FileNameList.push_back(LocalName);
111 * Sets up a filename to be read.
113 void vtkGdcmReader::SetFileName(const char *name)
115 vtkImageReader2::SetFileName(name);
116 // Since we maintain a list of filenames, when building a volume,
117 // (see vtkGdcmReader::AddFileName), we additionaly need to purge
118 // this list when we manually positionate the filename.
119 this->FileNameList.clear();
123 //-----------------------------------------------------------------------------
126 * Configure the output e.g. WholeExtent, spacing, origin, scalar type...
128 void vtkGdcmReader::ExecuteInformation()
130 this->TotalNumberOfPlanes = this->CheckFileCoherence();
131 if ( this->TotalNumberOfPlanes == 0)
133 vtkErrorMacro("File set is not coherent. Exiting...");
137 // if the user has not set the extent, but has set the VOI
138 // set the z axis extent to the VOI z axis
139 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
140 (this->DataVOI[4] || this->DataVOI[5]))
142 this->DataExtent[4] = this->DataVOI[4];
143 this->DataExtent[5] = this->DataVOI[5];
146 // When the user has set the VOI, check it's coherence with the file content.
147 if (this->DataVOI[0] || this->DataVOI[1] ||
148 this->DataVOI[2] || this->DataVOI[3] ||
149 this->DataVOI[4] || this->DataVOI[5])
151 if ((this->DataVOI[0] < 0) ||
152 (this->DataVOI[1] >= this->NumColumns) ||
153 (this->DataVOI[2] < 0) ||
154 (this->DataVOI[3] >= this->NumLines) ||
155 (this->DataVOI[4] < 0) ||
156 (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
158 vtkWarningMacro("The requested VOI is larger than expected extent.");
159 this->DataVOI[0] = 0;
160 this->DataVOI[1] = this->NumColumns - 1;
161 this->DataVOI[2] = 0;
162 this->DataVOI[3] = this->NumLines - 1;
163 this->DataVOI[4] = 0;
164 this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
168 // Positionate the Extent.
169 this->DataExtent[0] = 0;
170 this->DataExtent[1] = this->NumColumns - 1;
171 this->DataExtent[2] = 0;
172 this->DataExtent[3] = this->NumLines - 1;
173 this->DataExtent[4] = 0;
174 this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
176 // We don't need to positionate the Endian related stuff (by using
177 // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
178 // since the reading of the file is done by gdcm.
179 // But we do need to set up the data type for downstream filters:
180 if ( ImageType == "8U" )
182 vtkDebugMacro("8 bits unsigned image");
183 this->SetDataScalarTypeToUnsignedChar();
185 else if ( ImageType == "8S" )
187 vtkErrorMacro("Cannot handle 8 bit signed files");
190 else if ( ImageType == "16U" )
192 vtkDebugMacro("16 bits unsigned image");
193 this->SetDataScalarTypeToUnsignedShort();
195 else if ( ImageType == "16S" )
197 vtkDebugMacro("16 bits signed image");
198 this->SetDataScalarTypeToShort();
199 //vtkErrorMacro("Cannot handle 16 bit signed files");
201 else if ( ImageType == "32U" )
203 vtkDebugMacro("32 bits unsigned image");
204 vtkDebugMacro("WARNING: forced to signed int !");
205 this->SetDataScalarTypeToInt();
207 else if ( ImageType == "32S" )
209 vtkDebugMacro("32 bits signed image");
210 this->SetDataScalarTypeToInt();
213 //Set number of scalar components:
214 this->SetNumberOfScalarComponents(this->NumComponents);
216 this->Superclass::ExecuteInformation();
220 * Update => ouput->Update => UpdateData => Execute => ExecuteData
221 * (see vtkSource.cxx for last step).
222 * This function (redefinition of vtkImageReader::ExecuteData, see
223 * VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
224 * extent/axes are assumed to be the same as the file extent/order.
226 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
228 if (this->InternalFileNameList.empty())
230 vtkErrorMacro("A least a valid FileName must be specified.");
234 // FIXME : extraneous parsing of header is made when allocating OuputData
235 vtkImageData *data = this->AllocateOutputData(output);
236 data->SetExtent(this->DataExtent);
237 data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
239 // Test if output has valid extent
240 // Prevent memory errors
241 if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
242 (this->DataExtent[3]-this->DataExtent[2]>=0) &&
243 (this->DataExtent[5]-this->DataExtent[4]>=0))
245 // The memory size for a full stack of images of course depends
246 // on the number of planes and the size of each image:
247 size_t StackNumPixels = this->NumColumns * this->NumLines
248 * this->TotalNumberOfPlanes * this->NumComponents;
249 size_t stack_size = StackNumPixels * this->PixelSize;
250 // Allocate pixel data space itself.
252 // Variables for the UpdateProgress. We shall use 50 steps to signify
253 // the advance of the process:
254 unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
255 * this->TotalNumberOfPlanes
257 // The actual advance measure:
258 unsigned long UpdateProgressCount = 0;
260 // Feeling the allocated memory space with each image/volume:
261 unsigned char *Dest = (unsigned char *)data->GetPointData()->GetScalars()->GetVoidPointer(0);
262 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
263 FileName != InternalFileNameList.end();
266 // Images that were tagged as unreadable in CheckFileCoherence()
267 // are substituted with a black image to let the caller visually
268 // notice something wrong is going on:
269 if (*FileName != "GDCM_UNREADABLE")
271 // Update progress related for good files is made in LoadImageInMemory
272 Dest += this->LoadImageInMemory(*FileName, Dest,
273 UpdateProgressTarget,
274 UpdateProgressCount);
278 // We insert a black image in the stack for the user to be aware that
279 // this image/volume couldn't be loaded. We simply skip one image
281 Dest += this->NumColumns * this->NumLines * this->PixelSize;
283 // Update progress related for bad files:
284 UpdateProgressCount += this->NumLines;
285 if (UpdateProgressTarget > 0)
287 if (!(UpdateProgressCount%UpdateProgressTarget))
289 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
292 } // Else, file not loadable
298 * vtkGdcmReader can have the file names specified through two ways:
299 * (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
301 * (2) By successive calls to vtkGdcmReader::AddFileName()
302 * When the first method was used by caller we need to update the local
305 void vtkGdcmReader::BuildFileListFromPattern()
307 if ((! this->FileNameList.empty()) && this->FileName )
309 vtkErrorMacro("Both file patterns and AddFileName schemes were used");
310 vtkErrorMacro("Only the files specified with AddFileName shall be used");
314 if (! this->FileNameList.empty() )
316 vtkDebugMacro("Using the AddFileName specified files");
317 this->InternalFileNameList=this->FileNameList;
321 if (!this->FileName && !this->FilePattern)
323 vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
324 vtkErrorMacro("specify a FileName or FilePattern.");
328 this->RemoveAllInternalFileName();
329 if( this->FileNameList.empty() )
332 this->ComputeInternalFileName(this->DataExtent[4]);
333 vtkDebugMacro("Adding file " << this->InternalFileName);
334 this->AddInternalFileName(this->InternalFileName);
338 //stack of 2D dicom case:
339 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
341 this->ComputeInternalFileName(idx);
342 vtkDebugMacro("Adding file " << this->InternalFileName);
343 this->AddInternalFileName(this->InternalFileName);
349 * When more than one filename is specified (i.e. we expect loading
350 * a stack or volume) we need to check that the corresponding images/volumes
351 * to be loaded are coherent i.e. to make sure:
352 * - they all share the same X dimensions
353 * - they all share the same Y dimensions
354 * - they all share the same ImageType ( 8 bit signed, or unsigned...)
356 * Eventually, we emit a warning when all the files do NOT share the
357 * Z dimension, since we can still build a stack but the
358 * files are not coherent in Z, which is probably a source a trouble...
359 * When files are not readable (either the file cannot be opened or
360 * because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".
361 * This method returns the total number of planar images to be loaded
362 * (i.e. an image represents one plane, but a volume represents many planes)
364 int vtkGdcmReader::CheckFileCoherence()
366 int ReturnedTotalNumberOfPlanes = 0; // The returned value.
368 this->BuildFileListFromPattern();
369 if (this->InternalFileNameList.empty())
371 vtkErrorMacro("FileNames are not set.");
375 bool FoundReferenceFile = false;
378 // Loop on the filenames:
379 // - check for their existence and gdcm "parsability"
380 // - get the coherence check done:
381 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
382 FileName != InternalFileNameList.end();
385 // The file is always added in the number of planes
386 // - If file doesn't exist, it will be replaced by a black plane in the
387 // ExecuteData method
388 // - If file has more than 1 plane, other planes will be added later to
389 // to the ReturnedTotalNumberOfPlanes variable counter
390 ReturnedTotalNumberOfPlanes += 1;
392 /////// Stage 0: check for file name:
393 if(*FileName==std::string("GDCM_UNREADABLE"))
396 /////// Stage 1: check for file readability:
397 // Stage 1.1: check for file existence.
399 fp = fopen(FileName->c_str(),"rb");
402 vtkErrorMacro("Unable to open file " << FileName->c_str());
403 vtkErrorMacro("Removing this file from readed files "
404 << FileName->c_str());
405 *FileName = "GDCM_UNREADABLE";
410 // Stage 1.2: check for Gdcm parsability
411 gdcmHeaderHelper GdcmHeader(FileName->c_str());
412 if (!GdcmHeader.IsReadable())
414 vtkErrorMacro("Gdcm cannot parse file " << FileName->c_str());
415 vtkErrorMacro("Removing this file from readed files "
416 << FileName->c_str());
417 *FileName = "GDCM_UNREADABLE";
421 // Stage 1.3: further gdcm compatibility on PixelType
422 std::string type = GdcmHeader.GetPixelType();
423 if ( (type != "8U") && (type != "8S")
424 && (type != "16U") && (type != "16S")
425 && (type != "32U") && (type != "32S") )
427 vtkErrorMacro("Bad File Type for file" << FileName->c_str());
428 vtkErrorMacro(" " << type.c_str());
429 vtkErrorMacro("Removing this file from readed files "
430 << FileName->c_str());
431 *FileName = "GDCM_UNREADABLE";
435 // Stage 2: check coherence of the set of files
436 int NX = GdcmHeader.GetXSize();
437 int NY = GdcmHeader.GetYSize();
438 int NZ = GdcmHeader.GetZSize();
439 if (FoundReferenceFile)
441 // Stage 2.1: mandatory coherence stage:
442 if ( ( NX != this->NumColumns )
443 || ( NY != this->NumLines )
444 || ( type != this->ImageType ) )
446 vtkErrorMacro("This file is not coherent with previous ones"
447 << FileName->c_str());
448 vtkErrorMacro("Removing this file from readed files "
449 << FileName->c_str());
450 *FileName = "GDCM_UNREADABLE";
454 // Stage 2.2: optional coherence stage
455 if ( NZ != ReferenceNZ )
457 vtkErrorMacro("File is not coherent in Z with previous ones"
458 << FileName->c_str());
462 vtkDebugMacro("File is coherent with previous ones"
463 << FileName->c_str());
466 // Stage 2.3: when the file contains a volume (as opposed to an image),
467 // notify the caller.
470 vtkErrorMacro("This file contains multiple planes (images)"
471 << FileName->c_str());
474 // Eventually, this file can be added on the stack. Update the
475 // full size of the stack
476 vtkDebugMacro("Number of planes added to the stack: " << NZ);
477 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
483 // We didn't have a workable reference file yet. Set this one
485 FoundReferenceFile = true;
486 vtkDebugMacro("This file taken as coherence reference:"
487 << FileName->c_str());
488 vtkDebugMacro("Image dimension of reference file as read from Gdcm:"
489 << NX << " " << NY << " " << NZ);
490 vtkDebugMacro("Number of planes added to the stack: " << NZ);
491 // Set aside the size of the image
492 this->NumColumns = NX;
495 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
496 this->ImageType = type;
497 this->PixelSize = GdcmHeader.GetPixelSize();
499 if( GdcmHeader.HasLUT() )
501 this->NumComponents = GdcmHeader.GetNumberOfScalarComponentsRaw();
505 this->NumComponents = GdcmHeader.GetNumberOfScalarComponents(); //rgb or mono
509 this->DataSpacing[0] = GdcmHeader.GetXSpacing();
510 this->DataSpacing[1] = GdcmHeader.GetYSpacing();
511 this->DataSpacing[2] = GdcmHeader.GetZSpacing();
514 this->DataOrigin[0] = GdcmHeader.GetXOrigin();
515 this->DataOrigin[1] = GdcmHeader.GetYOrigin();
516 this->DataOrigin[2] = GdcmHeader.GetZOrigin();
519 } // End of loop on FileName
521 ///////// The files we CANNOT load are flaged. On debugging purposes
522 // count the loadable number of files and display their number:
523 int NumberCoherentFiles = 0;
524 for (std::list<std::string>::iterator Filename = InternalFileNameList.begin();
525 Filename != InternalFileNameList.end();
528 if (*Filename != "GDCM_UNREADABLE")
529 NumberCoherentFiles++;
531 vtkDebugMacro("Number of coherent files: " << NumberCoherentFiles);
533 if (ReturnedTotalNumberOfPlanes == 0)
535 vtkErrorMacro("No loadable file.");
538 vtkDebugMacro("Total number of planes on the stack: "
539 << ReturnedTotalNumberOfPlanes);
541 return ReturnedTotalNumberOfPlanes;
544 //-----------------------------------------------------------------------------
547 * Remove all file names to the internal list of images to read.
549 void vtkGdcmReader::RemoveAllInternalFileName(void)
551 this->InternalFileNameList.clear();
555 * Adds a file name to the internal list of images to read.
557 void vtkGdcmReader::AddInternalFileName(const char* name)
559 char * LocalName = new char[strlen(name) + 1];
560 strcpy(LocalName, name);
561 this->InternalFileNameList.push_back(LocalName);
566 * Loads the contents of the image/volume contained by Filename at
567 * the Dest memory address. Returns the size of the data loaded.
569 size_t vtkGdcmReader::LoadImageInMemory(
570 std::string FileName,
571 unsigned char * Dest,
572 const unsigned long UpdateProgressTarget,
573 unsigned long & UpdateProgressCount)
575 vtkDebugMacro("Copying to memory image" << FileName.c_str());
576 gdcmFile GdcmFile(FileName.c_str());
579 // If the data structure of vtk for image/volume representation
580 // were straigthforwards the following would suffice:
581 // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
582 // But vtk chooses to invert the lines of an image, that is the last
583 // line comes first (for some axis related reasons?). Hence we need
584 // to load the image line by line, starting from the end.
586 int NumColumns = GdcmFile.GetHeader()->GetXSize();
587 int NumLines = GdcmFile.GetHeader()->GetYSize();
588 int NumPlanes = GdcmFile.GetHeader()->GetZSize();
589 int LineSize = NumComponents * NumColumns * GdcmFile.GetHeader()->GetPixelSize();
591 unsigned char * Source;
592 if( GdcmFile.GetHeader()->HasLUT() )
594 size = GdcmFile.GetImageDataSizeRaw();
595 Source = (unsigned char*) GdcmFile.GetImageDataRaw();
596 unsigned char *Lut = GdcmFile.GetHeader()->GetLUTRGBA();
598 if(!this->LookupTable)
599 this->LookupTable = vtkLookupTable::New();
601 this->LookupTable->SetNumberOfTableValues(256);
602 for (int tmp=0; tmp<256; tmp++)
604 this->LookupTable->SetTableValue(tmp,
605 (float)Lut[4*tmp+0]/255.0,
606 (float)Lut[4*tmp+1]/255.0,
607 (float)Lut[4*tmp+2]/255.0,
610 this->LookupTable->SetRange(0,255);
611 vtkDataSetAttributes *a=this->GetOutput()->GetPointData();
612 a->GetScalars()->SetLookupTable(this->LookupTable);
617 size = GdcmFile.GetImageDataSize();
618 Source = (unsigned char*)GdcmFile.GetImageData();
620 unsigned char * pSource = Source; //pointer for later deletion
621 unsigned char * Destination = Dest + size - LineSize;
623 for (int plane = 0; plane < NumPlanes; plane++)
625 for (int line = 0; line < NumLines; line++)
627 // Copy one line at proper destination:
628 memcpy((void*)Destination, (void*)Source, LineSize);
630 Destination -= LineSize;
631 // Update progress related:
632 if (!(UpdateProgressCount%UpdateProgressTarget))
634 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
636 UpdateProgressCount++;
639 //GetImageData allocate a (void*)malloc, remove it:
645 //-----------------------------------------------------------------------------