diff Core/DicomParsing/ParsedDicomFile.cpp @ 3944:aae045f802f4 transcoding

preparing simplified interface for IDicomTranscoder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 19 May 2020 10:17:06 +0200
parents 7610af1532c3
children 3d2fc1b5cc8c
line wrap: on
line diff
--- a/Core/DicomParsing/ParsedDicomFile.cpp	Mon May 18 21:37:31 2020 +0200
+++ b/Core/DicomParsing/ParsedDicomFile.cpp	Tue May 19 10:17:06 2020 +0200
@@ -455,8 +455,8 @@
   void ParsedDicomFile::SendPathValue(RestApiOutput& output,
                                       const UriComponents& uri)
   {
-    DcmItem* dicom = pimpl_->file_->getDataset();
-    E_TransferSyntax transferSyntax = pimpl_->file_->getDataset()->getCurrentXfer();
+    DcmItem* dicom = GetDcmtkObject().getDataset();
+    E_TransferSyntax transferSyntax = GetDcmtkObject().getDataset()->getCurrentXfer();
 
     // Special case: Accessing the pixel data
     if (uri.size() == 1 || 
@@ -516,7 +516,7 @@
     InvalidateCache();
 
     DcmTagKey key(tag.GetGroup(), tag.GetElement());
-    DcmElement* element = pimpl_->file_->getDataset()->remove(key);
+    DcmElement* element = GetDcmtkObject().getDataset()->remove(key);
     if (element != NULL)
     {
       delete element;
@@ -536,7 +536,7 @@
 
     InvalidateCache();
 
-    DcmItem* dicom = pimpl_->file_->getDataset();
+    DcmItem* dicom = GetDcmtkObject().getDataset();
     DcmTagKey key(tag.GetGroup(), tag.GetElement());
 
     if (onlyIfExists &&
@@ -558,7 +558,7 @@
   {
     InvalidateCache();
 
-    DcmDataset& dataset = *pimpl_->file_->getDataset();
+    DcmDataset& dataset = *GetDcmtkObject().getDataset();
 
     // Loop over the dataset to detect its private tags
     typedef std::list<DcmElement*> Tags;
@@ -629,7 +629,7 @@
       return;
     }
 
-    if (pimpl_->file_->getDataset()->tagExists(ToDcmtkBridge::Convert(tag)))
+    if (GetDcmtkObject().getDataset()->tagExists(ToDcmtkBridge::Convert(tag)))
     {
       throw OrthancException(ErrorCode_AlreadyExistingTag);
     }
@@ -650,7 +650,7 @@
     bool hasCodeExtensions;
     Encoding encoding = DetectEncoding(hasCodeExtensions);
     std::unique_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, encoding, privateCreator));
-    InsertInternal(*pimpl_->file_->getDataset(), element.release());
+    InsertInternal(*GetDcmtkObject().getDataset(), element.release());
   }
 
 
@@ -782,7 +782,7 @@
 
     InvalidateCache();
 
-    DcmDataset& dicom = *pimpl_->file_->getDataset();
+    DcmDataset& dicom = *GetDcmtkObject().getDataset();
     if (CanReplaceProceed(dicom, ToDcmtkBridge::Convert(tag), mode))
     {
       // Either the tag was previously existing (and now removed), or
@@ -828,7 +828,7 @@
 
     InvalidateCache();
 
-    DcmDataset& dicom = *pimpl_->file_->getDataset();
+    DcmDataset& dicom = *GetDcmtkObject().getDataset();
     if (CanReplaceProceed(dicom, ToDcmtkBridge::Convert(tag), mode))
     {
       // Either the tag was previously existing (and now removed), or
@@ -867,7 +867,7 @@
   void ParsedDicomFile::Answer(RestApiOutput& output)
   {
     std::string serialized;
-    if (FromDcmtkBridge::SaveToMemoryBuffer(serialized, *pimpl_->file_->getDataset()))
+    if (FromDcmtkBridge::SaveToMemoryBuffer(serialized, *GetDcmtkObject().getDataset()))
     {
       output.AnswerBuffer(serialized, MimeType_Dicom);
     }
@@ -879,7 +879,7 @@
                                     const DicomTag& tag)
   {
     DcmTagKey k(tag.GetGroup(), tag.GetElement());
-    DcmDataset& dataset = *pimpl_->file_->getDataset();
+    DcmDataset& dataset = *GetDcmtkObject().getDataset();
 
     if (tag.IsPrivate() ||
         FromDcmtkBridge::IsUnknownTag(tag) ||
@@ -970,7 +970,7 @@
 
   void ParsedDicomFile::SaveToMemoryBuffer(std::string& buffer)
   {
-    FromDcmtkBridge::SaveToMemoryBuffer(buffer, *pimpl_->file_->getDataset());
+    FromDcmtkBridge::SaveToMemoryBuffer(buffer, *GetDcmtkObject().getDataset());
   }
 
 
@@ -1004,6 +1004,7 @@
                                            bool permissive)
   {
     pimpl_->file_.reset(new DcmFileFormat);
+    pimpl_->frameIndex_.reset(NULL);
 
     const DicomValue* tmp = source.TestAndGetValue(DICOM_TAG_SPECIFIC_CHARACTER_SET);
 
@@ -1091,7 +1092,7 @@
                                    bool keepSopInstanceUid) : 
     pimpl_(new PImpl)
   {
-    pimpl_->file_.reset(dynamic_cast<DcmFileFormat*>(other.pimpl_->file_->clone()));
+    pimpl_->file_.reset(dynamic_cast<DcmFileFormat*>(other.GetDcmtkObject().clone()));
 
     if (!keepSopInstanceUid)
     {
@@ -1121,7 +1122,30 @@
 
   DcmFileFormat& ParsedDicomFile::GetDcmtkObject() const
   {
-    return *pimpl_->file_.get();
+    if (pimpl_->file_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls,
+                             "ReleaseDcmtkObject() was called");
+    }
+    else
+    {
+      return *pimpl_->file_;
+    }
+  }
+
+
+  DcmFileFormat* ParsedDicomFile::ReleaseDcmtkObject()
+  {
+    if (pimpl_->file_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls,
+                             "ReleaseDcmtkObject() was called");
+    }
+    else
+    {
+      pimpl_->frameIndex_.reset(NULL);
+      return pimpl_->file_.release();
+    }
   }
 
 
@@ -1354,7 +1378,7 @@
       }
     }
 
-    if (!pimpl_->file_->getDataset()->insert(pixels.release(), false, false).good())
+    if (!GetDcmtkObject().getDataset()->insert(pixels.release(), false, false).good())
     {
       throw OrthancException(ErrorCode_InternalError);
     }    
@@ -1364,7 +1388,7 @@
   Encoding ParsedDicomFile::DetectEncoding(bool& hasCodeExtensions) const
   {
     return FromDcmtkBridge::DetectEncoding(hasCodeExtensions,
-                                           *pimpl_->file_->getDataset(),
+                                           *GetDcmtkObject().getDataset(),
                                            GetDefaultDicomEncoding());
   }
 
@@ -1388,7 +1412,7 @@
                                       unsigned int maxStringLength)
   {
     std::set<DicomTag> ignoreTagLength;
-    FromDcmtkBridge::ExtractDicomAsJson(target, *pimpl_->file_->getDataset(),
+    FromDcmtkBridge::ExtractDicomAsJson(target, *GetDcmtkObject().getDataset(),
                                         format, flags, maxStringLength,
                                         GetDefaultDicomEncoding(), ignoreTagLength);
   }
@@ -1400,7 +1424,7 @@
                                       unsigned int maxStringLength,
                                       const std::set<DicomTag>& ignoreTagLength)
   {
-    FromDcmtkBridge::ExtractDicomAsJson(target, *pimpl_->file_->getDataset(),
+    FromDcmtkBridge::ExtractDicomAsJson(target, *GetDcmtkObject().getDataset(),
                                         format, flags, maxStringLength,
                                         GetDefaultDicomEncoding(), ignoreTagLength);
   }
@@ -1409,28 +1433,28 @@
   void ParsedDicomFile::DatasetToJson(Json::Value& target,
                                       const std::set<DicomTag>& ignoreTagLength)
   {
-    FromDcmtkBridge::ExtractDicomAsJson(target, *pimpl_->file_->getDataset(), ignoreTagLength);
+    FromDcmtkBridge::ExtractDicomAsJson(target, *GetDcmtkObject().getDataset(), ignoreTagLength);
   }
 
 
   void ParsedDicomFile::DatasetToJson(Json::Value& target)
   {
     const std::set<DicomTag> ignoreTagLength;
-    FromDcmtkBridge::ExtractDicomAsJson(target, *pimpl_->file_->getDataset(), ignoreTagLength);
+    FromDcmtkBridge::ExtractDicomAsJson(target, *GetDcmtkObject().getDataset(), ignoreTagLength);
   }
 
 
   void ParsedDicomFile::HeaderToJson(Json::Value& target, 
                                      DicomToJsonFormat format)
   {
-    FromDcmtkBridge::ExtractHeaderAsJson(target, *pimpl_->file_->getMetaInfo(), format, DicomToJsonFlags_None, 0);
+    FromDcmtkBridge::ExtractHeaderAsJson(target, *GetDcmtkObject().getMetaInfo(), format, DicomToJsonFlags_None, 0);
   }
 
 
   bool ParsedDicomFile::HasTag(const DicomTag& tag) const
   {
     DcmTag key(tag.GetGroup(), tag.GetElement());
-    return pimpl_->file_->getDataset()->tagExists(key);
+    return GetDcmtkObject().getDataset()->tagExists(key);
   }
 
 
@@ -1472,7 +1496,7 @@
     memcpy(bytes, pdf.c_str(), pdf.size());
       
     DcmPolymorphOBOW* obj = element.release();
-    result = pimpl_->file_->getDataset()->insert(obj);
+    result = GetDcmtkObject().getDataset()->insert(obj);
 
     if (!result.good())
     {
@@ -1564,13 +1588,13 @@
     if (pimpl_->frameIndex_.get() == NULL)
     {
       assert(pimpl_->file_ != NULL &&
-             pimpl_->file_->getDataset() != NULL);
-      pimpl_->frameIndex_.reset(new DicomFrameIndex(*pimpl_->file_->getDataset()));
+             GetDcmtkObject().getDataset() != NULL);
+      pimpl_->frameIndex_.reset(new DicomFrameIndex(*GetDcmtkObject().getDataset()));
     }
 
     pimpl_->frameIndex_->GetRawFrame(target, frameId);
 
-    E_TransferSyntax transferSyntax = pimpl_->file_->getDataset()->getCurrentXfer();
+    E_TransferSyntax transferSyntax = GetDcmtkObject().getDataset()->getCurrentXfer();
     switch (transferSyntax)
     {
       case EXS_JPEGProcess1:
@@ -1598,8 +1622,8 @@
   unsigned int ParsedDicomFile::GetFramesCount() const
   {
     assert(pimpl_->file_ != NULL &&
-           pimpl_->file_->getDataset() != NULL);
-    return DicomFrameIndex::GetFramesCount(*pimpl_->file_->getDataset());
+           GetDcmtkObject().getDataset() != NULL);
+    return DicomFrameIndex::GetFramesCount(*GetDcmtkObject().getDataset());
   }
 
 
@@ -1611,21 +1635,21 @@
     if (source != target)  // Avoid unnecessary conversion
     {
       ReplacePlainString(DICOM_TAG_SPECIFIC_CHARACTER_SET, GetDicomSpecificCharacterSet(target));
-      FromDcmtkBridge::ChangeStringEncoding(*pimpl_->file_->getDataset(), source, hasCodeExtensions, target);
+      FromDcmtkBridge::ChangeStringEncoding(*GetDcmtkObject().getDataset(), source, hasCodeExtensions, target);
     }
   }
 
 
   void ParsedDicomFile::ExtractDicomSummary(DicomMap& target) const
   {
-    FromDcmtkBridge::ExtractDicomSummary(target, *pimpl_->file_->getDataset());
+    FromDcmtkBridge::ExtractDicomSummary(target, *GetDcmtkObject().getDataset());
   }
 
 
   void ParsedDicomFile::ExtractDicomSummary(DicomMap& target,
                                             const std::set<DicomTag>& ignoreTagLength) const
   {
-    FromDcmtkBridge::ExtractDicomSummary(target, *pimpl_->file_->getDataset(), ignoreTagLength);
+    FromDcmtkBridge::ExtractDicomSummary(target, *GetDcmtkObject().getDataset(), ignoreTagLength);
   }
 
 
@@ -1635,9 +1659,8 @@
     // using the meta header?
     const char* value = NULL;
 
-    assert(pimpl_->file_ != NULL);
-    if (pimpl_->file_->getMetaInfo() != NULL &&
-        pimpl_->file_->getMetaInfo()->findAndGetString(DCM_TransferSyntaxUID, value).good() &&
+    if (GetDcmtkObject().getMetaInfo() != NULL &&
+        GetDcmtkObject().getMetaInfo()->findAndGetString(DCM_TransferSyntaxUID, value).good() &&
         value != NULL)
     {
       result.assign(value);
@@ -1655,7 +1678,7 @@
     DcmTagKey k(DICOM_TAG_PHOTOMETRIC_INTERPRETATION.GetGroup(),
                 DICOM_TAG_PHOTOMETRIC_INTERPRETATION.GetElement());
 
-    DcmDataset& dataset = *pimpl_->file_->getDataset();
+    DcmDataset& dataset = *GetDcmtkObject().getDataset();
 
     const char *c = NULL;
     if (dataset.findAndGetString(k, c).good() &&
@@ -1673,6 +1696,6 @@
 
   void ParsedDicomFile::Apply(ITagVisitor& visitor)
   {
-    FromDcmtkBridge::Apply(*pimpl_->file_->getDataset(), visitor, GetDefaultDicomEncoding());
+    FromDcmtkBridge::Apply(*GetDcmtkObject().getDataset(), visitor, GetDefaultDicomEncoding());
   }
 }