From 6873e695718a1673408cc69275c860bc99266579 Mon Sep 17 00:00:00 2001 From: Krad Date: Tue, 20 Sep 2022 15:44:13 +0800 Subject: [PATCH] Add some comment. --- src/src/IO/DICOM/DICOMHeaderHelper.cpp | 42 ++++++++++-------- src/src/IO/DICOM/DICOMPixelDataHelper.cpp | 6 +-- src/src/IO/DICOM/DICOMPixelDataHelper.h | 43 ++++++++++++++++--- src/src/IO/DICOM/DicomLoader.h | 4 +- .../IO/DICOM/ExtendMedicalImageProperties.h | 4 +- src/src/IO/DICOM/vtkDCMTKImageReader.cpp | 24 +++++------ src/src/IO/DICOM/vtkDCMTKImageReader.h | 4 +- 7 files changed, 86 insertions(+), 41 deletions(-) diff --git a/src/src/IO/DICOM/DICOMHeaderHelper.cpp b/src/src/IO/DICOM/DICOMHeaderHelper.cpp index b9c9e52..961b050 100644 --- a/src/src/IO/DICOM/DICOMHeaderHelper.cpp +++ b/src/src/IO/DICOM/DICOMHeaderHelper.cpp @@ -187,10 +187,12 @@ void DICOMHeaderHelper::readHeaderFromFile(const char * filePath){ frameHeader.FrameIndex = i; DcmItem *perFrameGroup = nullptr; - if (dataset->findAndGetSequenceItem(DCM_PerFrameFunctionalGroupsSequence, perFrameGroup, i).good()) { + if (dataset->findAndGetSequenceItem(DCM_PerFrameFunctionalGroupsSequence, + perFrameGroup, i).good()) { //calc instance number for frame DcmItem *frameContent = nullptr; - if (perFrameGroup->findAndGetSequenceItem(DCM_FrameContentSequence, frameContent, 0).good()) { + if (perFrameGroup->findAndGetSequenceItem(DCM_FrameContentSequence, + frameContent, 0).good()) { unsigned long frameNumber = 0; frameContent->findAndGetUint32(DCM_InStackPositionNumber, frameNumber); frameHeader.InstanceNumber = frameNumber + fileHeader.InstanceNumber; @@ -198,10 +200,14 @@ void DICOMHeaderHelper::readHeaderFromFile(const char * filePath){ // try to get frame position DcmItem *framePosition = nullptr; - if (perFrameGroup->findAndGetSequenceItem(DCM_PlanePositionSequence, framePosition, 0).good()) { - if(framePosition->findAndGetFloat64(DCM_ImagePositionPatient, frameHeader.Position[0], 0).good()){ - framePosition->findAndGetFloat64(DCM_ImagePositionPatient, frameHeader.Position[1], 1); - framePosition->findAndGetFloat64(DCM_ImagePositionPatient, frameHeader.Position[2], 2); + if (perFrameGroup->findAndGetSequenceItem(DCM_PlanePositionSequence, + framePosition, 0).good()) { + if(framePosition->findAndGetFloat64(DCM_ImagePositionPatient, + frameHeader.Position[0], 0).good()){ + framePosition->findAndGetFloat64(DCM_ImagePositionPatient, + frameHeader.Position[1], 1); + framePosition->findAndGetFloat64(DCM_ImagePositionPatient, + frameHeader.Position[2], 2); }else{ //restore value frameHeader.Position[0] = fileHeader.Position[0]; @@ -214,16 +220,16 @@ void DICOMHeaderHelper::readHeaderFromFile(const char * filePath){ 0).good()) { if (frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[0], 0).good()) { - frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[1], - 1); - frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[2], - 2); - frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[3], - 3); - frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[4], - 4); - frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, frameHeader.Orientation[5], - 5); + frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, + frameHeader.Orientation[1],1); + frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, + frameHeader.Orientation[2],2); + frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, + frameHeader.Orientation[3],3); + frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, + frameHeader.Orientation[4],4); + frameOrientation->findAndGetFloat64(DCM_ImageOrientationPatient, + frameHeader.Orientation[5],5); }else{ //restore value frameHeader.Orientation[0] = fileHeader.Orientation[0]; @@ -317,7 +323,9 @@ void DICOMHeaderHelper::ArrangeSeriesProperty() { } } -ExtendMedicalImageProperties* DICOMHeaderHelper::createProperty(const std::vector& headerList, int splitIndex, int beginOffset) { +ExtendMedicalImageProperties* DICOMHeaderHelper::createProperty(const std::vector& headerList, + int splitIndex, int beginOffset) { + const DICOMFileHeader& header = headerList[beginOffset]; // create ExtendMedicalImageProperties and set values diff --git a/src/src/IO/DICOM/DICOMPixelDataHelper.cpp b/src/src/IO/DICOM/DICOMPixelDataHelper.cpp index 42449f8..3d676ba 100644 --- a/src/src/IO/DICOM/DICOMPixelDataHelper.cpp +++ b/src/src/IO/DICOM/DICOMPixelDataHelper.cpp @@ -151,7 +151,7 @@ void DICOMPixelDataHelper::GetRGBPixelData(const char *path, void *data, unsigne delete fileFormat; } -int DICOMPixelDataHelper::GetOverLayCount(const char *path) { +int DICOMPixelDataHelper::GetOverlayCount(const char *path) { int ret =0; DcmFileFormat fileFormat; if (fileFormat.loadFile(path).good()) { @@ -230,7 +230,7 @@ void DICOMPixelDataHelper::GetFramePixelData(const char *path, void *data, unsig } } -bool DICOMPixelDataHelper::StoreFileObject(const char *path) { +bool DICOMPixelDataHelper::CacheFileObject(const char *path) { DcmFileFormat *fileFormat = new DcmFileFormat();//读取文件获取传输语法 if (fileFormat->loadFile(path).good() && fileFormat->loadAllDataIntoMemory().good()) { DICOMFileObjectCache::getInstance()->store(path,fileFormat); @@ -241,7 +241,7 @@ bool DICOMPixelDataHelper::StoreFileObject(const char *path) { } } -void DICOMPixelDataHelper::ClearFileObject() { +void DICOMPixelDataHelper::ClearFileObjectCache() { DICOMFileObjectCache::getInstance()->clear(); } diff --git a/src/src/IO/DICOM/DICOMPixelDataHelper.h b/src/src/IO/DICOM/DICOMPixelDataHelper.h index 6c37141..9bd8b55 100644 --- a/src/src/IO/DICOM/DICOMPixelDataHelper.h +++ b/src/src/IO/DICOM/DICOMPixelDataHelper.h @@ -26,18 +26,39 @@ public: */ static void GetPixelData(const char * path, void* data, unsigned long& length); + /** + * Check whether FileObject with path in cache + * @param path dcm file absolute path, and the cache key + * @return is in cache + */ static bool CheckFileObjectCache(const char * path); - static bool StoreFileObject(const char * path); + /** + * Create a DcmFileFormat object with input path, and + * store it into the cache map. + * @param path dcm file absolute path, and the cache key + * @return cache success + */ + static bool CacheFileObject(const char * path); - static void ClearFileObject(); + /** + * Clear all object cache in the store map + */ + static void ClearFileObjectCache(); + /** + * Get multi-frame format dicom file Pixel Data + * @param path dcm file absolute path + * @param data output data pointer, must have been allocated. + * @param length output data length + * @param frameIndex frame index in pixel data + */ static void GetFramePixelData(const char * path, void* data, unsigned long& length, const long& frameIndex); /** * Get Pixel Data with Rescaled, result is calculate as Float, and arrange as Float. * @param path dcm file absolute path - * @param data output data buffer pointer + * @param data output data buffer pointer, must have been allocated. * @param length output data length */ static void GetPixelDataFloat(const char * path, void* data, unsigned long& length); @@ -45,13 +66,25 @@ public: /** * Get colorful Pixel Data as three component R,G,B color value. * @param path dcm file absolute path - * @param data output data buffer pointer + * @param data output data buffer pointer, must have been allocated. * @param length output data length */ static void GetRGBPixelData(const char * path, void* data, unsigned long& length); - static int GetOverLayCount(const char *path); + /** + * Get Overlay data count + * @param path dcm file absolute path + * @return Overlay data count + */ + static int GetOverlayCount(const char *path); + /** + * Get Overlay data + * @param path dcm file absolute path + * @param data output data buffer pointer, must have been allocated. + * @param width output overlay width, usually same as image columns + * @param height output overlay height, usually same as image rows + */ static void GetOverlayData(const char * path, void* data, unsigned int& width, unsigned int& height); diff --git a/src/src/IO/DICOM/DicomLoader.h b/src/src/IO/DICOM/DicomLoader.h index ae46e9a..09b2c54 100644 --- a/src/src/IO/DICOM/DicomLoader.h +++ b/src/src/IO/DICOM/DicomLoader.h @@ -5,10 +5,12 @@ #include #include +#include "IO/Defines/DIDKitExport.h" + class ExtendMedicalImageProperties; class vtkImageData; -class DicomLoader { +class DIDKIT_EXPORT DicomLoader { public: static void InitCodecs(); diff --git a/src/src/IO/DICOM/ExtendMedicalImageProperties.h b/src/src/IO/DICOM/ExtendMedicalImageProperties.h index 2e55314..dadb0a2 100644 --- a/src/src/IO/DICOM/ExtendMedicalImageProperties.h +++ b/src/src/IO/DICOM/ExtendMedicalImageProperties.h @@ -10,7 +10,9 @@ #include #include -class ExtendMedicalImageProperties: public vtkMedicalImageProperties{ +#include "IO/Defines/DIDKitExport.h" + +class DIDKIT_EXPORT ExtendMedicalImageProperties: public vtkMedicalImageProperties{ public: static ExtendMedicalImageProperties *New(); vtkTypeMacro(ExtendMedicalImageProperties,vtkMedicalImageProperties); diff --git a/src/src/IO/DICOM/vtkDCMTKImageReader.cpp b/src/src/IO/DICOM/vtkDCMTKImageReader.cpp index ce83afd..562c943 100644 --- a/src/src/IO/DICOM/vtkDCMTKImageReader.cpp +++ b/src/src/IO/DICOM/vtkDCMTKImageReader.cpp @@ -98,25 +98,23 @@ void vtkDCMTKImageReader::ExecuteDataWithInformation(vtkDataObject *output, // init codes to support compressed file DICOMPixelDataHelper::InitCodecs(); + if (properties->GetHasOverlay()){ + HasOverlay = true; + OverLayData = vtkImageData::New(); + OverLayData->SetExtent(data->GetExtent()); + OverLayData->SetSpacing(data->GetSpacing()); + OverLayData->SetOrigin(data->GetOrigin()); + OverLayData->AllocateScalars(VTK_UNSIGNED_CHAR,1); + } for (int i = 0; i < properties->GetFileNames().size(); ++i) { const std::string &path = properties->GetFileNames()[i].first; const long &frameIndex = properties->GetFileNames()[i].second; if (properties->GetHasOverlay()){ - if (!OverLayData){ - HasOverlay = true; - OverLayData = vtkImageData::New(); - OverLayData->SetExtent(data->GetExtent()); - OverLayData->SetSpacing(data->GetSpacing()); - OverLayData->SetOrigin(data->GetOrigin()); - OverLayData->AllocateScalars(VTK_UNSIGNED_CHAR,1); - } unsigned int width = 0; unsigned int height = 0; void* overlayBuffer = OverLayData->GetScalarPointer(0, 0, i); DICOMPixelDataHelper::GetOverlayData(path.c_str(), overlayBuffer, width, height); - - } unsigned long imageDataLength; @@ -132,9 +130,9 @@ void vtkDCMTKImageReader::ExecuteDataWithInformation(vtkDataObject *output, } else { if (frameIndex>=0){ if (!DICOMPixelDataHelper::CheckFileObjectCache(path.c_str())){ - if(!DICOMPixelDataHelper::StoreFileObject(path.c_str())){ + if(!DICOMPixelDataHelper::CacheFileObject(path.c_str())){ vtkErrorMacro(<< "Fail to read file:"< #include +#include "IO/Defines/DIDKitExport.h" + class ExtendMedicalImageProperties; -class vtkDCMTKImageReader: public vtkImageReader2 { +class DIDKIT_EXPORT vtkDCMTKImageReader: public vtkImageReader2 { public: //@{ /**