changeset 3903:d1273d7cc200 transcoding

avoid unnecessary dicom serialization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 May 2020 16:43:08 +0200
parents 7459fcb1bdf7
children c62f84c7eda9
files OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h
diffstat 3 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Thu May 07 15:29:39 2020 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Thu May 07 16:43:08 2020 +0200
@@ -130,14 +130,13 @@
 
     if (transcode)
     {
-      std::string saved;
-      modified->SaveToMemoryBuffer(saved);
+      std::set<DicomTransferSyntax> ts;
+      ts.insert(targetSyntax);
 
       std::string transcoded;
       bool hasSopInstanceUidChanged;
-      std::set<DicomTransferSyntax> ts;
-      ts.insert(targetSyntax);
-      if (context.TranscodeMemoryBuffer(transcoded, hasSopInstanceUidChanged, saved, ts, true))
+
+      if (context.Transcode(transcoded, hasSopInstanceUidChanged, *modified, ts, true))
       {      
         call.GetOutput().AnswerBuffer(transcoded, MimeType_Dicom);
       }
--- a/OrthancServer/ServerContext.cpp	Thu May 07 15:29:39 2020 +0200
+++ b/OrthancServer/ServerContext.cpp	Thu May 07 16:43:08 2020 +0200
@@ -1155,24 +1155,48 @@
   }
 
 
-  bool ServerContext::TranscodeMemoryBuffer(std::string& target,
-                                            bool& hasSopInstanceUidChanged,
-                                            const std::string& source,
-                                            const std::set<DicomTransferSyntax>& allowedSyntaxes,
-                                            bool allowNewSopInstanceUid)
+  bool ServerContext::Transcode(std::string& target,
+                                bool& hasSopInstanceUidChanged,
+                                ParsedDicomFile& dicom, // Possibly modified
+                                const std::set<DicomTransferSyntax>& allowedSyntaxes,
+                                bool allowNewSopInstanceUid)
   {
-    const char* data = source.empty() ? NULL : source.c_str();
+    IDicomTranscoder* transcoder = dcmtkTranscoder_.get();
     
 #if ORTHANC_ENABLE_PLUGINS == 1
     if (HasPlugins())
     {
-      return GetPlugins().TranscodeToBuffer(
-        target, hasSopInstanceUidChanged, data, source.size(), allowedSyntaxes, allowNewSopInstanceUid);
+      transcoder = &GetPlugins();
     }
 #endif
 
-    assert(dcmtkTranscoder_.get() != NULL);
-    return dcmtkTranscoder_->TranscodeToBuffer(
-      target, hasSopInstanceUidChanged, data, source.size(), allowedSyntaxes, allowNewSopInstanceUid);
+    if (transcoder == NULL)
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+    else if (transcoder->HasInplaceTranscode())
+    {
+      if (transcoder->InplaceTranscode(hasSopInstanceUidChanged, dicom.GetDcmtkObject(),
+                                       allowedSyntaxes, allowNewSopInstanceUid))
+      {
+        // In-place transcoding is supported and has succeeded
+        dicom.SaveToMemoryBuffer(target);
+        return true;
+      }
+      else
+      {
+        return false;
+      }
+    }
+    else
+    {
+      std::string source;
+      dicom.SaveToMemoryBuffer(source);
+      
+      const char* data = source.empty() ? NULL : source.c_str();
+    
+      return transcoder->TranscodeToBuffer(
+        target, hasSopInstanceUidChanged, data, source.size(), allowedSyntaxes, allowNewSopInstanceUid);
+    }
   }
 }
--- a/OrthancServer/ServerContext.h	Thu May 07 15:29:39 2020 +0200
+++ b/OrthancServer/ServerContext.h	Thu May 07 16:43:08 2020 +0200
@@ -464,10 +464,10 @@
                               uint16_t moveOriginatorId);
 
     // This method can be used even if "TranscodingEnabled" is set to "false"
-    bool TranscodeMemoryBuffer(std::string& target,
-                               bool& hasSopInstanceUidChanged,
-                               const std::string& source,
-                               const std::set<DicomTransferSyntax>& allowedSyntaxes,
-                               bool allowNewSopInstanceUid);
+    bool Transcode(std::string& target,
+                   bool& hasSopInstanceUidChanged,
+                   ParsedDicomFile& dicom, // Possibly modified
+                   const std::set<DicomTransferSyntax>& allowedSyntaxes,
+                   bool allowNewSopInstanceUid);
   };
 }