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 // This is fixed by adding a test at the beginning of ExecuteInformation
22 // on the modification of the object instance. If a modification have been
23 // made (method Modified() ), the MTime value is increased. The fileTime
24 // is compared to this new value to find a modification in the class
26 // 2b/ the core of ExecuteData then needs gdcmFile (which in turns
27 // initialises gdcmHeader in the constructor) in order to access
31 // maintain a list of gdcmFiles (created by say ExecuteInformation) created
32 // once and for all accross the life of vtkGdcmHeader (it would only load
33 // new gdcmFile if the user changes the list). ExecuteData would then use
34 // those gdcmFile and hence avoid calling the construtor:
35 // - advantage: the header of the files would only be parser once.
36 // - drawback: once execute information is called (i.e. on creation of
37 // a vtkGdcmHeader) the gdcmFile structure is loaded in memory.
38 // The average size of a gdcmHeader being of 100Ko, is one
39 // loads 10 stacks of images with say 200 images each, you
40 // end-up with a loss of 200Mo...
42 // /////// Never unallocated memory:
43 // ExecuteData allocates space for the pixel data [which will get pointed
44 // by the vtkPointData() through the call
45 // data->GetPointData()->GetScalars()->SetVoidArray(mem, StackNumPixels, 0);]
46 // This data is never "freed" neither in the destructor nor when the
47 // filename list is extended, ExecuteData is called a second (or third)
49 // //////////////////////////////////////////////////////////////
52 #include "gdcmHeaderHelper.h"
53 #include "vtkGdcmReader.h"
56 #include <vtkObjectFactory.h>
57 #include <vtkImageData.h>
58 #include <vtkPointData.h>
59 #include <vtkLookupTable.h>
61 //-----------------------------------------------------------------------------
62 // Constructor / Destructor
63 vtkGdcmReader::vtkGdcmReader()
65 this->LookupTable = NULL;
66 this->AllowLookupTable = 0;
69 vtkGdcmReader::~vtkGdcmReader()
71 this->RemoveAllFileName();
72 this->InternalFileNameList.clear();
74 this->LookupTable->Delete();
77 //-----------------------------------------------------------------------------
79 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
81 vtkImageReader::PrintSelf(os,indent);
82 os << indent << "Filenames : " << endl;
83 vtkIndent nextIndent = indent.GetNextIndent();
84 for (std::list<std::string>::iterator FileName = FileNameList.begin();
85 FileName != FileNameList.end();
88 os << nextIndent << FileName->c_str() << endl ;
92 //-----------------------------------------------------------------------------
95 * Remove all files from the list of images to read.
97 void vtkGdcmReader::RemoveAllFileName(void)
99 this->FileNameList.clear();
104 * Adds a file name to the list of images to read.
106 void vtkGdcmReader::AddFileName(const char* name)
108 // We need to bypass the const pointer [since list<>.push_bash() only
109 // takes a char* (but not a const char*)] by making a local copy:
110 char * LocalName = new char[strlen(name) + 1];
111 strcpy(LocalName, name);
112 this->FileNameList.push_back(LocalName);
118 * Sets up a filename to be read.
120 void vtkGdcmReader::SetFileName(const char *name)
122 vtkImageReader2::SetFileName(name);
123 // Since we maintain a list of filenames, when building a volume,
124 // (see vtkGdcmReader::AddFileName), we additionaly need to purge
125 // this list when we manually positionate the filename.
126 vtkDebugMacro("Clearing all files given with AddFileName");
127 this->FileNameList.clear();
131 //-----------------------------------------------------------------------------
134 * Configure the output e.g. WholeExtent, spacing, origin, scalar type...
136 void vtkGdcmReader::ExecuteInformation()
138 if(this->MTime>this->fileTime)
140 this->TotalNumberOfPlanes = this->CheckFileCoherence();
141 if ( this->TotalNumberOfPlanes == 0)
143 vtkErrorMacro("File set is not coherent. Exiting...");
147 // if the user has not set the extent, but has set the VOI
148 // set the z axis extent to the VOI z axis
149 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
150 (this->DataVOI[4] || this->DataVOI[5]))
152 this->DataExtent[4] = this->DataVOI[4];
153 this->DataExtent[5] = this->DataVOI[5];
156 // When the user has set the VOI, check it's coherence with the file content.
157 if (this->DataVOI[0] || this->DataVOI[1] ||
158 this->DataVOI[2] || this->DataVOI[3] ||
159 this->DataVOI[4] || this->DataVOI[5])
161 if ((this->DataVOI[0] < 0) ||
162 (this->DataVOI[1] >= this->NumColumns) ||
163 (this->DataVOI[2] < 0) ||
164 (this->DataVOI[3] >= this->NumLines) ||
165 (this->DataVOI[4] < 0) ||
166 (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
168 vtkWarningMacro("The requested VOI is larger than expected extent.");
169 this->DataVOI[0] = 0;
170 this->DataVOI[1] = this->NumColumns - 1;
171 this->DataVOI[2] = 0;
172 this->DataVOI[3] = this->NumLines - 1;
173 this->DataVOI[4] = 0;
174 this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
178 // Positionate the Extent.
179 this->DataExtent[0] = 0;
180 this->DataExtent[1] = this->NumColumns - 1;
181 this->DataExtent[2] = 0;
182 this->DataExtent[3] = this->NumLines - 1;
183 this->DataExtent[4] = 0;
184 this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
186 // We don't need to positionate the Endian related stuff (by using
187 // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
188 // since the reading of the file is done by gdcm.
189 // But we do need to set up the data type for downstream filters:
190 if ( ImageType == "8U" )
192 vtkDebugMacro("8 bits unsigned image");
193 this->SetDataScalarTypeToUnsignedChar();
195 else if ( ImageType == "8S" )
197 vtkErrorMacro("Cannot handle 8 bit signed files");
200 else if ( ImageType == "16U" )
202 vtkDebugMacro("16 bits unsigned image");
203 this->SetDataScalarTypeToUnsignedShort();
205 else if ( ImageType == "16S" )
207 vtkDebugMacro("16 bits signed image");
208 this->SetDataScalarTypeToShort();
209 //vtkErrorMacro("Cannot handle 16 bit signed files");
211 else if ( ImageType == "32U" )
213 vtkDebugMacro("32 bits unsigned image");
214 vtkDebugMacro("WARNING: forced to signed int !");
215 this->SetDataScalarTypeToInt();
217 else if ( ImageType == "32S" )
219 vtkDebugMacro("32 bits signed image");
220 this->SetDataScalarTypeToInt();
222 else if ( ImageType == "FD" )
224 vtkDebugMacro("64 bits Double image");
225 this->SetDataScalarTypeToDouble();
227 //Set number of scalar components:
228 this->SetNumberOfScalarComponents(this->NumComponents);
230 this->fileTime=this->MTime;
233 this->Superclass::ExecuteInformation();
237 * Update => ouput->Update => UpdateData => Execute => ExecuteData
238 * (see vtkSource.cxx for last step).
239 * This function (redefinition of vtkImageReader::ExecuteData, see
240 * VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
241 * extent/axes are assumed to be the same as the file extent/order.
243 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
245 if (this->InternalFileNameList.empty())
247 vtkErrorMacro("A least a valid FileName must be specified.");
251 // FIXME : extraneous parsing of header is made when allocating OuputData
252 vtkImageData *data = this->AllocateOutputData(output);
253 data->SetExtent(this->DataExtent);
254 data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
256 // Test if output has valid extent
257 // Prevent memory errors
258 if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
259 (this->DataExtent[3]-this->DataExtent[2]>=0) &&
260 (this->DataExtent[5]-this->DataExtent[4]>=0))
262 // The memory size for a full stack of images of course depends
263 // on the number of planes and the size of each image:
264 //size_t StackNumPixels = this->NumColumns * this->NumLines
265 // * this->TotalNumberOfPlanes * this->NumComponents;
266 //size_t stack_size = StackNumPixels * this->PixelSize; //not used
267 // Allocate pixel data space itself.
269 // Variables for the UpdateProgress. We shall use 50 steps to signify
270 // the advance of the process:
271 unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
272 * this->TotalNumberOfPlanes
274 // The actual advance measure:
275 unsigned long UpdateProgressCount = 0;
277 // Feeling the allocated memory space with each image/volume:
278 unsigned char *Dest = (unsigned char *)data->GetPointData()->GetScalars()->GetVoidPointer(0);
279 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
280 FileName != InternalFileNameList.end();
283 // Images that were tagged as unreadable in CheckFileCoherence()
284 // are substituted with a black image to let the caller visually
285 // notice something wrong is going on:
286 if (*FileName != "GDCM_UNREADABLE")
288 // Update progress related for good files is made in LoadImageInMemory
289 Dest += this->LoadImageInMemory(*FileName, Dest,
290 UpdateProgressTarget,
291 UpdateProgressCount);
295 // We insert a black image in the stack for the user to be aware that
296 // this image/volume couldn't be loaded. We simply skip one image
298 Dest += this->NumColumns * this->NumLines * this->PixelSize;
300 // Update progress related for bad files:
301 UpdateProgressCount += this->NumLines;
302 if (UpdateProgressTarget > 0)
304 if (!(UpdateProgressCount%UpdateProgressTarget))
306 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
309 } // Else, file not loadable
315 * vtkGdcmReader can have the file names specified through two ways:
316 * (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
318 * (2) By successive calls to vtkGdcmReader::AddFileName()
319 * When the first method was used by caller we need to update the local
322 void vtkGdcmReader::BuildFileListFromPattern()
324 this->RemoveAllInternalFileName();
326 if ((! this->FileNameList.empty()) && this->FileName )
328 vtkErrorMacro("Both AddFileName and SetFileName schemes were used");
329 vtkErrorMacro("No images loaded ! ");
333 if ((! this->FileNameList.empty()) && this->FilePrefix )
335 vtkErrorMacro("Both AddFileName and SetFilePrefix schemes were used");
336 vtkErrorMacro("No images loaded ! ");
340 if (this->FileName && this->FilePrefix)
342 vtkErrorMacro("Both SetFileName and SetFilePrefix schemes were used");
343 vtkErrorMacro("No images loaded ! ");
347 if (! this->FileNameList.empty() )
349 vtkDebugMacro("Using the AddFileName specified files");
350 this->InternalFileNameList=this->FileNameList;
354 if (!this->FileName && !this->FilePrefix)
356 vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
357 vtkErrorMacro("specify a FileName or FilePrefix.");
363 // Single file loading (as given with ::SetFileName()):
364 // Case of multi-frame file considered here
365 this->ComputeInternalFileName(this->DataExtent[4]);
366 vtkDebugMacro("Adding file " << this->InternalFileName);
367 this->AddInternalFileName(this->InternalFileName);
371 // Multi file loading (as given with ::SetFilePattern()):
372 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
374 this->ComputeInternalFileName(idx);
375 vtkDebugMacro("Adding file " << this->InternalFileName);
376 this->AddInternalFileName(this->InternalFileName);
382 * When more than one filename is specified (i.e. we expect loading
383 * a stack or volume) we need to check that the corresponding images/volumes
384 * to be loaded are coherent i.e. to make sure:
385 * - they all share the same X dimensions
386 * - they all share the same Y dimensions
387 * - they all share the same ImageType ( 8 bit signed, or unsigned...)
389 * Eventually, we emit a warning when all the files do NOT share the
390 * Z dimension, since we can still build a stack but the
391 * files are not coherent in Z, which is probably a source a trouble...
392 * When files are not readable (either the file cannot be opened or
393 * because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".
394 * This method returns the total number of planar images to be loaded
395 * (i.e. an image represents one plane, but a volume represents many planes)
397 int vtkGdcmReader::CheckFileCoherence()
399 int ReturnedTotalNumberOfPlanes = 0; // The returned value.
401 this->BuildFileListFromPattern();
402 if (this->InternalFileNameList.empty())
404 vtkErrorMacro("FileNames are not set.");
408 bool FoundReferenceFile = false;
411 // Loop on the filenames:
412 // - check for their existence and gdcm "parsability"
413 // - get the coherence check done:
414 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
415 FileName != InternalFileNameList.end();
418 // The file is always added in the number of planes
419 // - If file doesn't exist, it will be replaced by a black plane in the
420 // ExecuteData method
421 // - If file has more than 1 plane, other planes will be added later to
422 // to the ReturnedTotalNumberOfPlanes variable counter
423 ReturnedTotalNumberOfPlanes += 1;
425 /////// Stage 0: check for file name:
426 if(*FileName==std::string("GDCM_UNREADABLE"))
429 /////// Stage 1: check for file readability:
430 // Stage 1.1: check for file existence.
432 fp = fopen(FileName->c_str(),"rb");
435 vtkErrorMacro("Unable to open file " << FileName->c_str());
436 vtkErrorMacro("Removing this file from readed files "
437 << FileName->c_str());
438 *FileName = "GDCM_UNREADABLE";
443 // Stage 1.2: check for Gdcm parsability
444 gdcmHeader GdcmHeader(FileName->c_str(), false, true);
445 // true : for enableSequences
446 if (!GdcmHeader.IsReadable())
448 vtkErrorMacro("Gdcm cannot parse file " << FileName->c_str());
449 vtkErrorMacro("Removing this file from readed files "
450 << FileName->c_str());
451 *FileName = "GDCM_UNREADABLE";
455 // Stage 1.3: further gdcm compatibility on PixelType
456 std::string type = GdcmHeader.GetPixelType();
457 if ( (type != "8U") && (type != "8S")
458 && (type != "16U") && (type != "16S")
459 && (type != "32U") && (type != "32S") )
461 vtkErrorMacro("Bad File Type for file" << FileName->c_str());
462 vtkErrorMacro(" " << type.c_str());
463 vtkErrorMacro("Removing this file from readed files "
464 << FileName->c_str());
465 *FileName = "GDCM_UNREADABLE";
469 // Stage 2: check coherence of the set of files
470 int NX = GdcmHeader.GetXSize();
471 int NY = GdcmHeader.GetYSize();
472 int NZ = GdcmHeader.GetZSize();
473 if (FoundReferenceFile)
475 // Stage 2.1: mandatory coherence stage:
476 if ( ( NX != this->NumColumns )
477 || ( NY != this->NumLines )
478 || ( type != this->ImageType ) )
480 vtkErrorMacro("This file is not coherent with previous ones"
481 << FileName->c_str());
482 vtkErrorMacro("Removing this file from readed files "
483 << FileName->c_str());
484 *FileName = "GDCM_UNREADABLE";
488 // Stage 2.2: optional coherence stage
489 if ( NZ != ReferenceNZ )
491 vtkErrorMacro("File is not coherent in Z with previous ones"
492 << FileName->c_str());
496 vtkDebugMacro("File is coherent with previous ones"
497 << FileName->c_str());
500 // Stage 2.3: when the file contains a volume (as opposed to an image),
501 // notify the caller.
504 vtkErrorMacro("This file contains multiple planes (images)"
505 << FileName->c_str());
508 // Eventually, this file can be added on the stack. Update the
509 // full size of the stack
510 vtkDebugMacro("Number of planes added to the stack: " << NZ);
511 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
517 // We didn't have a workable reference file yet. Set this one
519 FoundReferenceFile = true;
520 vtkDebugMacro("This file taken as coherence reference:"
521 << FileName->c_str());
522 vtkDebugMacro("Image dimension of reference file as read from Gdcm:"
523 << NX << " " << NY << " " << NZ);
524 vtkDebugMacro("Number of planes added to the stack: " << NZ);
525 // Set aside the size of the image
526 this->NumColumns = NX;
529 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
530 this->ImageType = type;
531 this->PixelSize = GdcmHeader.GetPixelSize();
533 if( GdcmHeader.HasLUT() && this->AllowLookupTable )
535 // I could raise an error is AllowLookupTable is on and HasLUT() off
536 this->NumComponents = GdcmHeader.GetNumberOfScalarComponentsRaw();
540 this->NumComponents = GdcmHeader.GetNumberOfScalarComponents(); //rgb or mono
544 this->DataSpacing[0] = GdcmHeader.GetXSpacing();
545 this->DataSpacing[1] = GdcmHeader.GetYSpacing();
546 this->DataSpacing[2] = GdcmHeader.GetZSpacing();
549 this->DataOrigin[0] = GdcmHeader.GetXOrigin();
550 this->DataOrigin[1] = GdcmHeader.GetYOrigin();
551 this->DataOrigin[2] = GdcmHeader.GetZOrigin();
554 } // End of loop on FileName
556 ///////// The files we CANNOT load are flaged. On debugging purposes
557 // count the loadable number of files and display their number:
558 int NumberCoherentFiles = 0;
559 for (std::list<std::string>::iterator Filename = InternalFileNameList.begin();
560 Filename != InternalFileNameList.end();
563 if (*Filename != "GDCM_UNREADABLE")
564 NumberCoherentFiles++;
566 vtkDebugMacro("Number of coherent files: " << NumberCoherentFiles);
568 if (ReturnedTotalNumberOfPlanes == 0)
570 vtkErrorMacro("No loadable file.");
573 vtkDebugMacro("Total number of planes on the stack: "
574 << ReturnedTotalNumberOfPlanes);
576 return ReturnedTotalNumberOfPlanes;
579 //-----------------------------------------------------------------------------
582 * Remove all file names to the internal list of images to read.
584 void vtkGdcmReader::RemoveAllInternalFileName(void)
586 this->InternalFileNameList.clear();
590 * Adds a file name to the internal list of images to read.
592 void vtkGdcmReader::AddInternalFileName(const char* name)
594 char * LocalName = new char[strlen(name) + 1];
595 strcpy(LocalName, name);
596 this->InternalFileNameList.push_back(LocalName);
601 * Loads the contents of the image/volume contained by Filename at
602 * the Dest memory address. Returns the size of the data loaded.
604 size_t vtkGdcmReader::LoadImageInMemory(
605 std::string FileName,
606 unsigned char * Dest,
607 const unsigned long UpdateProgressTarget,
608 unsigned long & UpdateProgressCount)
610 vtkDebugMacro("Copying to memory image [" << FileName.c_str() << "]");
611 gdcmFile GdcmFile(FileName.c_str(),false,true);
612 // true : to enable SeQuences
615 // If the data structure of vtk for image/volume representation
616 // were straigthforwards the following would be enough:
617 // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
618 // But vtk chooses to invert the lines of an image, that is the last
619 // line comes first (for some axis related reasons?). Hence we need
620 // to load the image line by line, starting from the end.
622 int NumColumns = GdcmFile.GetHeader()->GetXSize();
623 int NumLines = GdcmFile.GetHeader()->GetYSize();
624 int NumPlanes = GdcmFile.GetHeader()->GetZSize();
625 int LineSize = NumComponents * NumColumns * GdcmFile.GetHeader()->GetPixelSize();
627 unsigned char * Source;
629 if( GdcmFile.GetHeader()->HasLUT() && this->AllowLookupTable )
631 size = GdcmFile.GetImageDataSizeRaw();
632 Source = (unsigned char*) GdcmFile.GetImageDataRaw();
633 unsigned char *Lut = GdcmFile.GetHeader()->GetLUTRGBA();
635 if(!this->LookupTable)
636 this->LookupTable = vtkLookupTable::New();
638 this->LookupTable->SetNumberOfTableValues(256);
639 for (int tmp=0; tmp<256; tmp++)
641 this->LookupTable->SetTableValue(tmp,
642 (float)Lut[4*tmp+0]/255.0,
643 (float)Lut[4*tmp+1]/255.0,
644 (float)Lut[4*tmp+2]/255.0,
647 this->LookupTable->SetRange(0,255);
648 vtkDataSetAttributes *a=this->GetOutput()->GetPointData();
649 a->GetScalars()->SetLookupTable(this->LookupTable);
654 size = GdcmFile.GetImageDataSize();
655 Source = (unsigned char*)GdcmFile.GetImageData();
658 unsigned char * pSource = Source; //pointer for later deletion
659 unsigned char * Destination = Dest + size - LineSize;
661 for (int plane = 0; plane < NumPlanes; plane++)
663 for (int line = 0; line < NumLines; line++)
665 // Copy one line at proper destination:
666 memcpy((void*)Destination, (void*)Source, LineSize);
668 Destination -= LineSize;
669 // Update progress related:
670 if (!(UpdateProgressCount%UpdateProgressTarget))
672 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
674 UpdateProgressCount++;
678 // DO NOT remove this commented out code .
679 // Nobody knows what's expecting you ...
680 // Just to 'see' what was actually read on disk :-(
683 // f2 = fopen("SpuriousFile.RAW","wb");
684 // fwrite(Dest,size,1,f2);
687 //GetImageData allocate a (void*)malloc, remove it:
692 //-----------------------------------------------------------------------------