00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include "gdcmFileHelper.h"
00061 #include "gdcmFile.h"
00062
00063 #include "vtkGdcmReader.h"
00064 #include "gdcmDebug.h"
00065 #include "gdcmCommon.h"
00066
00067 #include <vtkObjectFactory.h>
00068 #include <vtkImageData.h>
00069 #include <vtkPointData.h>
00070 #include <vtkLookupTable.h>
00071
00072 vtkCxxRevisionMacro(vtkGdcmReader, "$Revision: 1.85 $")
00073 vtkStandardNewMacro(vtkGdcmReader)
00074
00075
00076
00077 vtkGdcmReader::vtkGdcmReader()
00078 {
00079 this->LookupTable = NULL;
00080 this->AllowLookupTable = false;
00081 this->AllowLightChecking = false;
00082 this->LoadMode = gdcm::LD_ALL;
00083
00084
00085
00086 this->CoherentFileList = 0;
00087 this->UserFunction = 0;
00088
00089 this->OwnFile=true;
00090 this->Execution=false;
00091 }
00092
00093 vtkGdcmReader::~vtkGdcmReader()
00094 {
00095 this->RemoveAllFileName();
00096 this->InternalFileNameList.clear();
00097 if(this->LookupTable)
00098 this->LookupTable->Delete();
00099 }
00100
00101
00102
00103 void vtkGdcmReader::PrintSelf(ostream &os, vtkIndent indent)
00104 {
00105 this->Superclass::PrintSelf(os,indent);
00106 os << indent << "Filenames : " << endl;
00107 vtkIndent nextIndent = indent.GetNextIndent();
00108 for (std::list<std::string>::iterator it = FileNameList.begin();
00109 it != FileNameList.end();
00110 ++it)
00111 {
00112 os << nextIndent << it->c_str() << endl ;
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121 void vtkGdcmReader::RemoveAllFileName(void)
00122 {
00123 this->FileNameList.clear();
00124 this->Modified();
00125 }
00126
00127
00128
00129
00130 void vtkGdcmReader::AddFileName(const char* name)
00131 {
00132
00133
00134 this->FileNameList.push_back(name);
00135 this->Modified();
00136 }
00137
00138
00139
00140
00141 void vtkGdcmReader::SetFileName(const char *name)
00142 {
00143 vtkImageReader2::SetFileName(name);
00144
00145
00146
00147 vtkDebugMacro(<< "Clearing all files given with AddFileName");
00148 this->FileNameList.clear();
00149 this->Modified();
00150 }
00151
00152
00153
00154
00155
00156
00157 void vtkGdcmReader::ExecuteInformation()
00158 {
00159 if(this->Execution)
00160 return;
00161
00162 this->Execution=true;
00163 this->RemoveAllInternalFile();
00164 if(this->MTime>this->fileTime)
00165 {
00166 this->TotalNumberOfPlanes = 0;
00167
00168 if ( this->CoherentFileList != 0 )
00169 {
00170 this->UpdateFileInformation();
00171 }
00172 else
00173 {
00174 this->BuildFileListFromPattern();
00175 this->LoadFileInformation();
00176 }
00177
00178 if ( this->TotalNumberOfPlanes == 0)
00179 {
00180 vtkErrorMacro(<< "File set is not coherent. Exiting...");
00181 return;
00182 }
00183
00184
00185
00186 if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
00187 (this->DataVOI[4] || this->DataVOI[5]))
00188 {
00189 this->DataExtent[4] = this->DataVOI[4];
00190 this->DataExtent[5] = this->DataVOI[5];
00191 }
00192
00193
00194 if (this->DataVOI[0] || this->DataVOI[1] ||
00195 this->DataVOI[2] || this->DataVOI[3] ||
00196 this->DataVOI[4] || this->DataVOI[5])
00197 {
00198 if ((this->DataVOI[0] < 0) ||
00199 (this->DataVOI[1] >= this->NumColumns) ||
00200 (this->DataVOI[2] < 0) ||
00201 (this->DataVOI[3] >= this->NumLines) ||
00202 (this->DataVOI[4] < 0) ||
00203 (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
00204 {
00205 vtkWarningMacro(<< "The requested VOI is larger than expected extent.");
00206 this->DataVOI[0] = 0;
00207 this->DataVOI[1] = this->NumColumns - 1;
00208 this->DataVOI[2] = 0;
00209 this->DataVOI[3] = this->NumLines - 1;
00210 this->DataVOI[4] = 0;
00211 this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
00212 }
00213 }
00214
00215
00216 this->DataExtent[0] = 0;
00217 this->DataExtent[1] = this->NumColumns - 1;
00218 this->DataExtent[2] = 0;
00219 this->DataExtent[3] = this->NumLines - 1;
00220 this->DataExtent[4] = 0;
00221 this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
00222
00223
00224
00225
00226
00227 if ( ImageType == "8U" )
00228 {
00229 vtkDebugMacro(<< "8 bits unsigned image");
00230 this->SetDataScalarTypeToUnsignedChar();
00231 }
00232 else if ( ImageType == "8S" )
00233 {
00234 vtkErrorMacro(<< "Cannot handle 8 bit signed files");
00235 return;
00236 }
00237 else if ( ImageType == "16U" )
00238 {
00239 vtkDebugMacro(<< "16 bits unsigned image");
00240 this->SetDataScalarTypeToUnsignedShort();
00241 }
00242 else if ( ImageType == "16S" )
00243 {
00244 vtkDebugMacro(<< "16 bits signed image");
00245 this->SetDataScalarTypeToShort();
00246 }
00247 else if ( ImageType == "32U" )
00248 {
00249 vtkDebugMacro(<< "32 bits unsigned image");
00250 vtkDebugMacro(<< "WARNING: forced to signed int !");
00251 this->SetDataScalarTypeToInt();
00252 }
00253 else if ( ImageType == "32S" )
00254 {
00255 vtkDebugMacro(<< "32 bits signed image");
00256 this->SetDataScalarTypeToInt();
00257 }
00258 else if ( ImageType == "FD" )
00259 {
00260 vtkDebugMacro(<< "64 bits Double image");
00261 this->SetDataScalarTypeToDouble();
00262 }
00263
00264 this->SetNumberOfScalarComponents(this->NumComponents);
00265
00266 this->fileTime=this->MTime;
00267 }
00268
00269 this->Superclass::ExecuteInformation();
00270
00271 this->GetOutput()->SetUpdateExtentToWholeExtent();
00272 this->BuildData(this->GetOutput());
00273
00274 this->Execution=false;
00275 this->RemoveAllInternalFile();
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
00286 {
00287 vtkImageData *data=vtkImageData::SafeDownCast(output);
00288 data->SetExtent(this->DataExtent);
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 }
00305
00306 void vtkGdcmReader::BuildData(vtkDataObject *output)
00307 {
00308 vtkImageData *data = this->AllocateOutputData(output);
00309
00310 data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
00311
00312
00313
00314 if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
00315 (this->DataExtent[3]-this->DataExtent[2]>=0) &&
00316 (this->DataExtent[5]-this->DataExtent[4]>=0))
00317 {
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
00328 * this->TotalNumberOfPlanes
00329 / 50.0);
00330
00331 unsigned long UpdateProgressCount = 0;
00332
00333
00334
00335 size_t size = this->NumColumns * this->NumLines * this->NumPlanes
00336 * data->GetScalarSize() * this->NumComponents;
00337 unsigned char *Dest = (unsigned char *)data->GetScalarPointer();
00338 for (std::vector<gdcm::File* >::iterator it = InternalFileList.begin();
00339 it != InternalFileList.end();
00340 ++it)
00341 {
00342 this->LoadImageInMemory(*it, Dest,
00343 UpdateProgressTarget,
00344 UpdateProgressCount);
00345 Dest += size;
00346 }
00347 }
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 void vtkGdcmReader::BuildFileListFromPattern()
00359 {
00360 this->RemoveAllInternalFileName();
00361
00362
00363 if ((! this->FileNameList.empty()) && this->FileName )
00364 {
00365 vtkErrorMacro(<< "Both AddFileName and SetFileName schemes were used");
00366 vtkErrorMacro(<< "No images loaded ! ");
00367 return;
00368 }
00369
00370 if ((! this->FileNameList.empty()) && this->FilePrefix )
00371 {
00372 vtkErrorMacro(<< "Both AddFileName and SetFilePrefix schemes were used");
00373 vtkErrorMacro(<< "No images loaded ! ");
00374 return;
00375 }
00376
00377 if (this->FileName && this->FilePrefix)
00378 {
00379 vtkErrorMacro(<< "Both SetFileName and SetFilePrefix schemes were used");
00380 vtkErrorMacro(<< "No images loaded ! ");
00381 return;
00382 }
00383
00384
00385 if (! this->FileNameList.empty() )
00386 {
00387 vtkDebugMacro(<< "Using the AddFileName specified files");
00388 this->InternalFileNameList=this->FileNameList;
00389 return;
00390 }
00391
00392 if (!this->FileName && !this->FilePrefix)
00393 {
00394 vtkErrorMacro(<< "FileNames are not set. Either use AddFileName() or");
00395 vtkErrorMacro(<< "specify a FileName or FilePrefix.");
00396 return;
00397 }
00398
00399 if( this->FileName )
00400 {
00401
00402
00403 this->ComputeInternalFileName(this->DataExtent[4]);
00404 vtkDebugMacro(<< "Adding file " << this->InternalFileName);
00405 this->AddInternalFileName(this->InternalFileName);
00406 }
00407 else
00408 {
00409
00410 for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
00411 {
00412 this->ComputeInternalFileName(idx);
00413 vtkDebugMacro(<< "Adding file " << this->InternalFileName);
00414 this->AddInternalFileName(this->InternalFileName);
00415 }
00416 }
00417 }
00418
00429 void vtkGdcmReader::LoadFileInformation()
00430 {
00431 gdcm::File *file;
00432 bool foundReference=false;
00433 std::string type;
00434
00435 this->OwnFile=true;
00436 for (std::list<std::string>::iterator filename = InternalFileNameList.begin();
00437 filename != InternalFileNameList.end();
00438 ++filename)
00439 {
00440
00441 FILE *fp;
00442 fp = fopen(filename->c_str(),"rb");
00443 if (!fp)
00444 {
00445 vtkErrorMacro(<< "Unable to open file " << filename->c_str());
00446 vtkErrorMacro(<< "Removing this file from read files: "
00447 << filename->c_str());
00448 file = NULL;
00449 InternalFileList.push_back(file);
00450 continue;
00451 }
00452 fclose(fp);
00453
00454
00455 file=gdcm::File::New();
00456 file->SetLoadMode( LoadMode );
00457 file->SetFileName(filename->c_str() );
00458 file->Load();
00459
00460
00461 if(!file->IsReadable())
00462 {
00463 vtkErrorMacro(<< "Gdcm cannot parse file " << filename->c_str());
00464 vtkErrorMacro(<< "Removing this file from read files: "
00465 << filename->c_str());
00466 file->Delete();
00467 file=NULL;
00468 InternalFileList.push_back(file);
00469 continue;
00470 }
00471
00472
00473 type = file->GetPixelType();
00474 if ( (type != "8U") && (type != "8S")
00475 && (type != "16U") && (type != "16S")
00476 && (type != "32U") && (type != "32S") )
00477 {
00478 vtkErrorMacro(<< "Bad File Type for file " << filename->c_str() << "\n"
00479 << " File type found : " << type.c_str()
00480 << " (might be 8U, 8S, 16U, 16S, 32U, 32S) \n"
00481 << " Removing this file from read files");
00482 file->Delete();
00483 file=NULL;
00484 InternalFileList.push_back(file);
00485 continue;
00486 }
00487
00488
00489 if(!foundReference)
00490 {
00491 foundReference = true;
00492 GetFileInformation(file);
00493
00494 vtkDebugMacro(<< "This file taken as coherence reference:"
00495 << filename->c_str());
00496 vtkDebugMacro(<< "Image dimensions of reference file as read from Gdcm:"
00497 << this->NumColumns << " " << this->NumLines << " "
00498 << this->NumPlanes);
00499 }
00500 else if(!TestFileInformation(file))
00501 {
00502 file->Delete();
00503 file=NULL;
00504 }
00505
00506 InternalFileList.push_back(file);
00507 }
00508 }
00509
00515 void vtkGdcmReader::UpdateFileInformation()
00516 {
00517 this->InternalFileList=*(this->CoherentFileList);
00518 this->OwnFile=false;
00519
00520 for(gdcmFileList::iterator it=InternalFileList.begin();
00521 it!=InternalFileList.end();
00522 ++it)
00523 {
00524 if( *it != NULL)
00525 {
00526 GetFileInformation(*it);
00527 break;
00528 }
00529 }
00530 }
00531
00537 void vtkGdcmReader::GetFileInformation(gdcm::File *file)
00538 {
00539
00540 this->NumColumns = file->GetXSize();
00541 this->NumLines = file->GetYSize();
00542 this->NumPlanes = file->GetZSize();
00543
00544 if (CoherentFileList == 0)
00545 this->TotalNumberOfPlanes = this->NumPlanes*InternalFileNameList.size();
00546 else
00547 this->TotalNumberOfPlanes = this->NumPlanes*CoherentFileList->size();
00548
00549 this->ImageType = file->GetPixelType();
00550 this->PixelSize = file->GetPixelSize();
00551
00552 this->DataSpacing[0] = file->GetXSpacing();
00553 this->DataSpacing[1] = file->GetYSpacing();
00554 this->DataSpacing[2] = file->GetZSpacing();
00555
00556
00557 if( file->HasLUT() && this->AllowLookupTable )
00558 {
00559
00560 this->NumComponents = file->GetNumberOfScalarComponentsRaw();
00561 }
00562 else
00563 {
00564 this->NumComponents = file->GetNumberOfScalarComponents();
00565 }
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00595 bool vtkGdcmReader::TestFileInformation(gdcm::File *file)
00596 {
00597 int numColumns = file->GetXSize();
00598 int numLines = file->GetYSize();
00599 int numPlanes = file->GetZSize();
00600 int numComponents;
00601 unsigned int pixelSize = file->GetPixelSize();
00602
00603 if( file->HasLUT() && this->AllowLookupTable )
00604 numComponents = file->GetNumberOfScalarComponentsRaw();
00605 else
00606 numComponents = file->GetNumberOfScalarComponents();
00607
00608 if( numColumns != this->NumColumns )
00609 {
00610 vtkErrorMacro(<< "File X value doesn't match with the previous ones: "
00611 << file->GetFileName().c_str()
00612 << ". Found " << numColumns << ", must be "
00613 << this->NumColumns);
00614 return false;
00615 }
00616 if( numLines != this->NumLines )
00617 {
00618 vtkErrorMacro(<< "File Y value doesn't match with the previous ones: "
00619 << file->GetFileName().c_str()
00620 << ". Found " << numLines << ", must be "
00621 << this->NumLines);
00622 return false;
00623 }
00624 if( numPlanes != this->NumPlanes )
00625 {
00626 vtkErrorMacro(<< "File Z value doesn't match with the previous ones: "
00627 << file->GetFileName().c_str()
00628 << ". Found " << numPlanes << ", must be "
00629 << this->NumPlanes);
00630 return false;
00631 }
00632 if( numComponents != this->NumComponents )
00633 {
00634 vtkErrorMacro(<< "File Components count doesn't match with the previous ones: "
00635 << file->GetFileName().c_str()
00636 << ". Found " << numComponents << ", must be "
00637 << this->NumComponents);
00638 return false;
00639 }
00640 if( pixelSize != this->PixelSize )
00641 {
00642 vtkErrorMacro(<< "File pixel size doesn't match with the previous ones: "
00643 << file->GetFileName().c_str()
00644 << ". Found " << pixelSize << ", must be "
00645 << this->PixelSize);
00646 return false;
00647 }
00648
00649 return true;
00650 }
00651
00652
00653
00654
00655
00656
00657 void vtkGdcmReader::RemoveAllInternalFileName(void)
00658 {
00659 this->InternalFileNameList.clear();
00660 }
00661
00662
00663
00664
00665 void vtkGdcmReader::AddInternalFileName(const char *name)
00666 {
00667 char *LocalName = new char[strlen(name) + 1];
00668 strcpy(LocalName, name);
00669 this->InternalFileNameList.push_back(LocalName);
00670 delete[] LocalName;
00671 }
00672
00673
00674
00675
00676 void vtkGdcmReader::RemoveAllInternalFile(void)
00677 {
00678 if(this->OwnFile)
00679 {
00680 for(gdcmFileList::iterator it=InternalFileList.begin();
00681 it!=InternalFileList.end();
00682 ++it)
00683 {
00684 (*it)->Delete();
00685 }
00686 }
00687 this->InternalFileList.clear();
00688 }
00689
00690 void vtkGdcmReader::IncrementProgress(const unsigned long updateProgressTarget,
00691 unsigned long &updateProgressCount)
00692 {
00693
00694 updateProgressCount += this->NumLines;
00695 if (updateProgressTarget > 0)
00696 {
00697 if (!(updateProgressCount%updateProgressTarget))
00698 {
00699 this->UpdateProgress(updateProgressCount/(50.0*updateProgressTarget));
00700 }
00701 }
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735 void vtkGdcmReader::LoadImageInMemory(
00736 gdcm::File *f,
00737 unsigned char *dest,
00738 const unsigned long updateProgressTarget,
00739 unsigned long &updateProgressCount)
00740 {
00741 if(!f)
00742 return;
00743
00744 gdcm::FileHelper *fileH = gdcm::FileHelper::New( f );
00745 fileH->SetUserFunction( UserFunction );
00746
00747 int numColumns = f->GetXSize();
00748 int numLines = f->GetYSize();
00749 int numPlanes = f->GetZSize();
00750 int numComponents;
00751
00752 if( f->HasLUT() && this->AllowLookupTable )
00753 numComponents = f->GetNumberOfScalarComponentsRaw();
00754 else
00755 numComponents = f->GetNumberOfScalarComponents();
00756 vtkDebugMacro(<< "numComponents:" << numComponents);
00757 vtkDebugMacro(<< "Copying to memory image [" << f->GetFileName().c_str() << "]");
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 int lineSize = NumComponents * numColumns * f->GetPixelSize();
00768 int planeSize = lineSize * numLines;
00769
00770 unsigned char *src;
00771
00772 if( fileH->GetFile()->HasLUT() && AllowLookupTable )
00773 {
00774
00775 fileH->GetImageDataSize();
00776 src = (unsigned char*) fileH->GetImageDataRaw();
00777 unsigned char *lut = (unsigned char*) fileH->GetLutRGBA();
00778
00779 if(!this->LookupTable)
00780 {
00781 this->LookupTable = vtkLookupTable::New();
00782 }
00783
00784 this->LookupTable->SetNumberOfTableValues(256);
00785 for (int tmp=0; tmp<256; tmp++)
00786 {
00787 this->LookupTable->SetTableValue(tmp,
00788 (float)lut[4*tmp+0]/255.0,
00789 (float)lut[4*tmp+1]/255.0,
00790 (float)lut[4*tmp+2]/255.0,
00791 1);
00792 }
00793 this->LookupTable->SetRange(0,255);
00794 vtkDataSetAttributes *a = this->GetOutput()->GetPointData();
00795 a->GetScalars()->SetLookupTable(this->LookupTable);
00796 free(lut);
00797 }
00798 else
00799 {
00800
00801
00802 src = (unsigned char*)fileH->GetImageData();
00803 }
00804
00805 unsigned char *dst = dest + planeSize - lineSize;
00806 for (int plane = 0; plane < numPlanes; plane++)
00807 {
00808 for (int line = 0; line < numLines; line++)
00809 {
00810
00811 memcpy((void*)dst, (void*)src, lineSize);
00812 src += lineSize;
00813 dst -= lineSize;
00814
00815 if (!(updateProgressCount%updateProgressTarget))
00816 {
00817 this->UpdateProgress(updateProgressCount/(50.0*updateProgressTarget));
00818 }
00819 updateProgressCount++;
00820 }
00821 dst += 2 * planeSize;
00822 }
00823
00824 fileH->Delete();
00825 }
00826
00827