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;
68 vtkGdcmReader::~vtkGdcmReader()
70 this->RemoveAllFileName();
71 this->InternalFileNameList.clear();
73 this->LookupTable->Delete();
76 //-----------------------------------------------------------------------------
78 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
80 vtkImageReader::PrintSelf(os,indent);
81 os << indent << "Filenames : " << endl;
82 vtkIndent nextIndent = indent.GetNextIndent();
83 for (std::list<std::string>::iterator FileName = FileNameList.begin();
84 FileName != FileNameList.end();
87 os << nextIndent << FileName->c_str() << endl ;
91 //-----------------------------------------------------------------------------
94 * Remove all files from the list of images to read.
96 void vtkGdcmReader::RemoveAllFileName(void)
98 this->FileNameList.clear();
103 * Adds a file name to the list of images to read.
105 void vtkGdcmReader::AddFileName(const char* name)
107 // We need to bypass the const pointer [since list<>.push_bash() only
108 // takes a char* (but not a const char*)] by making a local copy:
109 char * LocalName = new char[strlen(name) + 1];
110 strcpy(LocalName, name);
111 this->FileNameList.push_back(LocalName);
117 * Sets up a filename to be read.
119 void vtkGdcmReader::SetFileName(const char *name)
121 vtkImageReader2::SetFileName(name);
122 // Since we maintain a list of filenames, when building a volume,
123 // (see vtkGdcmReader::AddFileName), we additionaly need to purge
124 // this list when we manually positionate the filename.
125 vtkDebugMacro("Clearing all files given with AddFileName");
126 this->FileNameList.clear();
130 //-----------------------------------------------------------------------------
133 * Configure the output e.g. WholeExtent, spacing, origin, scalar type...
135 void vtkGdcmReader::ExecuteInformation()
137 if(this->MTime>this->fileTime)
139 this->TotalNumberOfPlanes = this->CheckFileCoherence();
140 if ( this->TotalNumberOfPlanes == 0)
142 vtkErrorMacro("File set is not coherent. Exiting...");
146 // if the user has not set the extent, but has set the VOI
147 // set the z axis extent to the VOI z axis
148 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
149 (this->DataVOI[4] || this->DataVOI[5]))
151 this->DataExtent[4] = this->DataVOI[4];
152 this->DataExtent[5] = this->DataVOI[5];
155 // When the user has set the VOI, check it's coherence with the file content.
156 if (this->DataVOI[0] || this->DataVOI[1] ||
157 this->DataVOI[2] || this->DataVOI[3] ||
158 this->DataVOI[4] || this->DataVOI[5])
160 if ((this->DataVOI[0] < 0) ||
161 (this->DataVOI[1] >= this->NumColumns) ||
162 (this->DataVOI[2] < 0) ||
163 (this->DataVOI[3] >= this->NumLines) ||
164 (this->DataVOI[4] < 0) ||
165 (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
167 vtkWarningMacro("The requested VOI is larger than expected extent.");
168 this->DataVOI[0] = 0;
169 this->DataVOI[1] = this->NumColumns - 1;
170 this->DataVOI[2] = 0;
171 this->DataVOI[3] = this->NumLines - 1;
172 this->DataVOI[4] = 0;
173 this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
177 // Positionate the Extent.
178 this->DataExtent[0] = 0;
179 this->DataExtent[1] = this->NumColumns - 1;
180 this->DataExtent[2] = 0;
181 this->DataExtent[3] = this->NumLines - 1;
182 this->DataExtent[4] = 0;
183 this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
185 // We don't need to positionate the Endian related stuff (by using
186 // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
187 // since the reading of the file is done by gdcm.
188 // But we do need to set up the data type for downstream filters:
189 if ( ImageType == "8U" )
191 vtkDebugMacro("8 bits unsigned image");
192 this->SetDataScalarTypeToUnsignedChar();
194 else if ( ImageType == "8S" )
196 vtkErrorMacro("Cannot handle 8 bit signed files");
199 else if ( ImageType == "16U" )
201 vtkDebugMacro("16 bits unsigned image");
202 this->SetDataScalarTypeToUnsignedShort();
204 else if ( ImageType == "16S" )
206 vtkDebugMacro("16 bits signed image");
207 this->SetDataScalarTypeToShort();
208 //vtkErrorMacro("Cannot handle 16 bit signed files");
210 else if ( ImageType == "32U" )
212 vtkDebugMacro("32 bits unsigned image");
213 vtkDebugMacro("WARNING: forced to signed int !");
214 this->SetDataScalarTypeToInt();
216 else if ( ImageType == "32S" )
218 vtkDebugMacro("32 bits signed image");
219 this->SetDataScalarTypeToInt();
221 else if ( ImageType == "FD" )
223 vtkDebugMacro("64 bits Double image");
224 this->SetDataScalarTypeToDouble();
226 //Set number of scalar components:
227 this->SetNumberOfScalarComponents(this->NumComponents);
229 this->fileTime=this->MTime;
232 this->Superclass::ExecuteInformation();
236 * Update => ouput->Update => UpdateData => Execute => ExecuteData
237 * (see vtkSource.cxx for last step).
238 * This function (redefinition of vtkImageReader::ExecuteData, see
239 * VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
240 * extent/axes are assumed to be the same as the file extent/order.
242 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
244 if (this->InternalFileNameList.empty())
246 vtkErrorMacro("A least a valid FileName must be specified.");
250 // FIXME : extraneous parsing of header is made when allocating OuputData
251 vtkImageData *data = this->AllocateOutputData(output);
252 data->SetExtent(this->DataExtent);
253 data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
255 // Test if output has valid extent
256 // Prevent memory errors
257 if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
258 (this->DataExtent[3]-this->DataExtent[2]>=0) &&
259 (this->DataExtent[5]-this->DataExtent[4]>=0))
261 // The memory size for a full stack of images of course depends
262 // on the number of planes and the size of each image:
263 size_t StackNumPixels = this->NumColumns * this->NumLines
264 * this->TotalNumberOfPlanes * this->NumComponents;
265 size_t stack_size = StackNumPixels * this->PixelSize;
266 // Allocate pixel data space itself.
268 // Variables for the UpdateProgress. We shall use 50 steps to signify
269 // the advance of the process:
270 unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
271 * this->TotalNumberOfPlanes
273 // The actual advance measure:
274 unsigned long UpdateProgressCount = 0;
276 // Feeling the allocated memory space with each image/volume:
277 unsigned char *Dest = (unsigned char *)data->GetPointData()->GetScalars()->GetVoidPointer(0);
278 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
279 FileName != InternalFileNameList.end();
282 // Images that were tagged as unreadable in CheckFileCoherence()
283 // are substituted with a black image to let the caller visually
284 // notice something wrong is going on:
285 if (*FileName != "GDCM_UNREADABLE")
287 // Update progress related for good files is made in LoadImageInMemory
288 Dest += this->LoadImageInMemory(*FileName, Dest,
289 UpdateProgressTarget,
290 UpdateProgressCount);
294 // We insert a black image in the stack for the user to be aware that
295 // this image/volume couldn't be loaded. We simply skip one image
297 Dest += this->NumColumns * this->NumLines * this->PixelSize;
299 // Update progress related for bad files:
300 UpdateProgressCount += this->NumLines;
301 if (UpdateProgressTarget > 0)
303 if (!(UpdateProgressCount%UpdateProgressTarget))
305 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
308 } // Else, file not loadable
314 * vtkGdcmReader can have the file names specified through two ways:
315 * (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
317 * (2) By successive calls to vtkGdcmReader::AddFileName()
318 * When the first method was used by caller we need to update the local
321 void vtkGdcmReader::BuildFileListFromPattern()
323 this->RemoveAllInternalFileName();
325 if ((! this->FileNameList.empty()) && this->FileName )
327 vtkErrorMacro("Both AddFileName and SetFileName schemes were used");
328 vtkErrorMacro("No images loaded ! ");
332 if ((! this->FileNameList.empty()) && this->FilePrefix )
334 vtkErrorMacro("Both AddFileName and SetFilePrefix schemes were used");
335 vtkErrorMacro("No images loaded ! ");
339 if (this->FileName && this->FilePrefix)
341 vtkErrorMacro("Both SetFileName and SetFilePrefix schemes were used");
342 vtkErrorMacro("No images loaded ! ");
346 if (! this->FileNameList.empty() )
348 vtkDebugMacro("Using the AddFileName specified files");
349 this->InternalFileNameList=this->FileNameList;
353 if (!this->FileName && !this->FilePrefix)
355 vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
356 vtkErrorMacro("specify a FileName or FilePrefix.");
362 // Single file loading (as given with ::SetFileName()):
363 // Case of multi-frame file considered here
364 this->ComputeInternalFileName(this->DataExtent[4]);
365 vtkDebugMacro("Adding file " << this->InternalFileName);
366 this->AddInternalFileName(this->InternalFileName);
370 // Multi file loading (as given with ::SetFilePattern()):
371 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
373 this->ComputeInternalFileName(idx);
374 vtkDebugMacro("Adding file " << this->InternalFileName);
375 this->AddInternalFileName(this->InternalFileName);
381 * When more than one filename is specified (i.e. we expect loading
382 * a stack or volume) we need to check that the corresponding images/volumes
383 * to be loaded are coherent i.e. to make sure:
384 * - they all share the same X dimensions
385 * - they all share the same Y dimensions
386 * - they all share the same ImageType ( 8 bit signed, or unsigned...)
388 * Eventually, we emit a warning when all the files do NOT share the
389 * Z dimension, since we can still build a stack but the
390 * files are not coherent in Z, which is probably a source a trouble...
391 * When files are not readable (either the file cannot be opened or
392 * because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".
393 * This method returns the total number of planar images to be loaded
394 * (i.e. an image represents one plane, but a volume represents many planes)
396 int vtkGdcmReader::CheckFileCoherence()
398 int ReturnedTotalNumberOfPlanes = 0; // The returned value.
400 this->BuildFileListFromPattern();
401 if (this->InternalFileNameList.empty())
403 vtkErrorMacro("FileNames are not set.");
407 bool FoundReferenceFile = false;
410 // Loop on the filenames:
411 // - check for their existence and gdcm "parsability"
412 // - get the coherence check done:
413 for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
414 FileName != InternalFileNameList.end();
417 // The file is always added in the number of planes
418 // - If file doesn't exist, it will be replaced by a black plane in the
419 // ExecuteData method
420 // - If file has more than 1 plane, other planes will be added later to
421 // to the ReturnedTotalNumberOfPlanes variable counter
422 ReturnedTotalNumberOfPlanes += 1;
424 /////// Stage 0: check for file name:
425 if(*FileName==std::string("GDCM_UNREADABLE"))
428 /////// Stage 1: check for file readability:
429 // Stage 1.1: check for file existence.
431 fp = fopen(FileName->c_str(),"rb");
434 vtkErrorMacro("Unable to open file " << FileName->c_str());
435 vtkErrorMacro("Removing this file from readed files "
436 << FileName->c_str());
437 *FileName = "GDCM_UNREADABLE";
442 // Stage 1.2: check for Gdcm parsability
443 gdcmHeaderHelper GdcmHeader(FileName->c_str(), false, true);
444 // true : for enableSequences
445 if (!GdcmHeader.IsReadable())
447 vtkErrorMacro("Gdcm cannot parse file " << FileName->c_str());
448 vtkErrorMacro("Removing this file from readed files "
449 << FileName->c_str());
450 *FileName = "GDCM_UNREADABLE";
454 // Stage 1.3: further gdcm compatibility on PixelType
455 std::string type = GdcmHeader.GetPixelType();
456 if ( (type != "8U") && (type != "8S")
457 && (type != "16U") && (type != "16S")
458 && (type != "32U") && (type != "32S") )
460 vtkErrorMacro("Bad File Type for file" << FileName->c_str());
461 vtkErrorMacro(" " << type.c_str());
462 vtkErrorMacro("Removing this file from readed files "
463 << FileName->c_str());
464 *FileName = "GDCM_UNREADABLE";
468 // Stage 2: check coherence of the set of files
469 int NX = GdcmHeader.GetXSize();
470 int NY = GdcmHeader.GetYSize();
471 int NZ = GdcmHeader.GetZSize();
472 if (FoundReferenceFile)
474 // Stage 2.1: mandatory coherence stage:
475 if ( ( NX != this->NumColumns )
476 || ( NY != this->NumLines )
477 || ( type != this->ImageType ) )
479 vtkErrorMacro("This file is not coherent with previous ones"
480 << FileName->c_str());
481 vtkErrorMacro("Removing this file from readed files "
482 << FileName->c_str());
483 *FileName = "GDCM_UNREADABLE";
487 // Stage 2.2: optional coherence stage
488 if ( NZ != ReferenceNZ )
490 vtkErrorMacro("File is not coherent in Z with previous ones"
491 << FileName->c_str());
495 vtkDebugMacro("File is coherent with previous ones"
496 << FileName->c_str());
499 // Stage 2.3: when the file contains a volume (as opposed to an image),
500 // notify the caller.
503 vtkErrorMacro("This file contains multiple planes (images)"
504 << FileName->c_str());
507 // Eventually, this file can be added on the stack. Update the
508 // full size of the stack
509 vtkDebugMacro("Number of planes added to the stack: " << NZ);
510 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
516 // We didn't have a workable reference file yet. Set this one
518 FoundReferenceFile = true;
519 vtkDebugMacro("This file taken as coherence reference:"
520 << FileName->c_str());
521 vtkDebugMacro("Image dimension of reference file as read from Gdcm:"
522 << NX << " " << NY << " " << NZ);
523 vtkDebugMacro("Number of planes added to the stack: " << NZ);
524 // Set aside the size of the image
525 this->NumColumns = NX;
528 ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
529 this->ImageType = type;
530 this->PixelSize = GdcmHeader.GetPixelSize();
532 if( GdcmHeader.HasLUT() )
534 this->NumComponents = GdcmHeader.GetNumberOfScalarComponentsRaw();
538 this->NumComponents = GdcmHeader.GetNumberOfScalarComponents(); //rgb or mono
542 this->DataSpacing[0] = GdcmHeader.GetXSpacing();
543 this->DataSpacing[1] = GdcmHeader.GetYSpacing();
544 this->DataSpacing[2] = GdcmHeader.GetZSpacing();
547 this->DataOrigin[0] = GdcmHeader.GetXOrigin();
548 this->DataOrigin[1] = GdcmHeader.GetYOrigin();
549 this->DataOrigin[2] = GdcmHeader.GetZOrigin();
552 } // End of loop on FileName
554 ///////// The files we CANNOT load are flaged. On debugging purposes
555 // count the loadable number of files and display their number:
556 int NumberCoherentFiles = 0;
557 for (std::list<std::string>::iterator Filename = InternalFileNameList.begin();
558 Filename != InternalFileNameList.end();
561 if (*Filename != "GDCM_UNREADABLE")
562 NumberCoherentFiles++;
564 vtkDebugMacro("Number of coherent files: " << NumberCoherentFiles);
566 if (ReturnedTotalNumberOfPlanes == 0)
568 vtkErrorMacro("No loadable file.");
571 vtkDebugMacro("Total number of planes on the stack: "
572 << ReturnedTotalNumberOfPlanes);
574 return ReturnedTotalNumberOfPlanes;
577 //-----------------------------------------------------------------------------
580 * Remove all file names to the internal list of images to read.
582 void vtkGdcmReader::RemoveAllInternalFileName(void)
584 this->InternalFileNameList.clear();
588 * Adds a file name to the internal list of images to read.
590 void vtkGdcmReader::AddInternalFileName(const char* name)
592 char * LocalName = new char[strlen(name) + 1];
593 strcpy(LocalName, name);
594 this->InternalFileNameList.push_back(LocalName);
599 * Loads the contents of the image/volume contained by Filename at
600 * the Dest memory address. Returns the size of the data loaded.
602 size_t vtkGdcmReader::LoadImageInMemory(
603 std::string FileName,
604 unsigned char * Dest,
605 const unsigned long UpdateProgressTarget,
606 unsigned long & UpdateProgressCount)
608 vtkDebugMacro("Copying to memory image [" << FileName.c_str() << "]");
609 gdcmFile GdcmFile(FileName.c_str(),false,true);
610 // true : to enable SeQuences
613 // If the data structure of vtk for image/volume representation
614 // were straigthforwards the following would be enough:
615 // GdcmFile.GetImageDataIntoVector((void*)Dest, size);
616 // But vtk chooses to invert the lines of an image, that is the last
617 // line comes first (for some axis related reasons?). Hence we need
618 // to load the image line by line, starting from the end.
620 int NumColumns = GdcmFile.GetHeader()->GetXSize();
621 int NumLines = GdcmFile.GetHeader()->GetYSize();
622 int NumPlanes = GdcmFile.GetHeader()->GetZSize();
623 int LineSize = NumComponents * NumColumns * GdcmFile.GetHeader()->GetPixelSize();
625 unsigned char * Source;
627 if( GdcmFile.GetHeader()->HasLUT() )
629 size = GdcmFile.GetImageDataSizeRaw();
630 Source = (unsigned char*) GdcmFile.GetImageDataRaw();
631 unsigned char *Lut = GdcmFile.GetHeader()->GetLUTRGBA();
633 if(!this->LookupTable)
634 this->LookupTable = vtkLookupTable::New();
636 this->LookupTable->SetNumberOfTableValues(256);
637 for (int tmp=0; tmp<256; tmp++)
639 this->LookupTable->SetTableValue(tmp,
640 (float)Lut[4*tmp+0]/255.0,
641 (float)Lut[4*tmp+1]/255.0,
642 (float)Lut[4*tmp+2]/255.0,
645 this->LookupTable->SetRange(0,255);
646 vtkDataSetAttributes *a=this->GetOutput()->GetPointData();
647 a->GetScalars()->SetLookupTable(this->LookupTable);
652 size = GdcmFile.GetImageDataSize();
653 Source = (unsigned char*)GdcmFile.GetImageData();
656 unsigned char * pSource = Source; //pointer for later deletion
657 unsigned char * Destination = Dest + size - LineSize;
659 for (int plane = 0; plane < NumPlanes; plane++)
661 for (int line = 0; line < NumLines; line++)
663 // Copy one line at proper destination:
664 memcpy((void*)Destination, (void*)Source, LineSize);
666 Destination -= LineSize;
667 // Update progress related:
668 if (!(UpdateProgressCount%UpdateProgressTarget))
670 this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
672 UpdateProgressCount++;
676 // DO NOT remove this commented out code .
677 // Nobody knows what's expecting you ...
678 // Just to 'see' what was actually read on disk :-(
681 // f2 = fopen("SpuriousFile.RAW","wb");
682 // fwrite(Dest,size,1,f2);
685 //GetImageData allocate a (void*)malloc, remove it:
690 //-----------------------------------------------------------------------------