changeset 1641:4e56b5a206b7

Support of binary tags encoded using data URI scheme
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Sep 2015 12:43:05 +0200
parents 48224db51ee7
children 0669d05b6de1
files Core/Toolbox.cpp NEWS OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp OrthancServer/ParsedDicomFile.cpp UnitTestsSources/FromDcmtkTests.cpp
diffstat 5 files changed, 35 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Wed Sep 23 12:02:39 2015 +0200
+++ b/Core/Toolbox.cpp	Wed Sep 23 12:43:05 2015 +0200
@@ -1021,7 +1021,7 @@
     if (regex_match(source.c_str(), what, pattern))
     {
       mime = what[1];
-      content = what[2];
+      DecodeBase64(content, what[2]);
     }
     else
     {
--- a/NEWS	Wed Sep 23 12:02:39 2015 +0200
+++ b/NEWS	Wed Sep 23 12:43:05 2015 +0200
@@ -2,6 +2,7 @@
 ===============================
 
 * Add ".dcm" suffix to files in ZIP archives (cf. URI ".../archive")
+* "/tools/create-dicom": Support of binary tags encoded using data URI scheme
 
 Maintenance
 -----------
--- a/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Wed Sep 23 12:02:39 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Wed Sep 23 12:43:05 2015 +0200
@@ -40,6 +40,7 @@
 #include "../OrthancInitialization.h"
 
 #include <boost/lexical_cast.hpp>
+#include <boost/algorithm/string/predicate.hpp>
 
 namespace Orthanc
 {
@@ -485,7 +486,8 @@
 
 
   static void InjectTags(ParsedDicomFile& dicom,
-                         const Json::Value& tags)
+                         const Json::Value& tags,
+                         bool interpretBinaryTags)
   {
     if (tags.type() != Json::objectValue)
     {
@@ -503,8 +505,8 @@
       }
 
       std::string value = tags[name].asString();
+      DicomTag tag = FromDcmtkBridge::ParseTag(name);
 
-      DicomTag tag = FromDcmtkBridge::ParseTag(name);
       if (tag != DICOM_TAG_SPECIFIC_CHARACTER_SET)
       {
         if (tag != DICOM_TAG_PATIENT_ID &&
@@ -527,6 +529,13 @@
         {
           throw OrthancException(ErrorCode_CreateDicomUseContent);
         }
+        else if (interpretBinaryTags &&
+                 boost::starts_with(value, "data:application/octet-stream;base64,"))
+        {
+          std::string mime, binary;
+          Toolbox::DecodeDataUriScheme(mime, binary, value);
+          dicom.Replace(tag, binary);
+        }
         else
         {
           dicom.Replace(tag, Toolbox::ConvertFromUtf8(value, dicom.GetEncoding()));
@@ -538,7 +547,8 @@
 
   static void CreateSeries(RestApiPostCall& call,
                            ParsedDicomFile& base /* in */,
-                           const Json::Value& content)
+                           const Json::Value& content,
+                           bool interpretBinaryTags)
   {
     assert(content.isArray());
     assert(content.size() > 0);
@@ -571,7 +581,7 @@
 
           if (content[i].isMember("Tags"))
           {
-            InjectTags(*dicom, content[i]["Tags"]);
+            InjectTags(*dicom, content[i]["Tags"], interpretBinaryTags);
           }
         }
 
@@ -744,6 +754,19 @@
       }
     }
 
+
+    bool interpretBinaryTags = true;
+    if (request.isMember("InterpretBinaryTags"))
+    {
+      const Json::Value& v = request["InterpretBinaryTags"];
+      if (v.type() != Json::booleanValue)
+      {
+        throw OrthancException(ErrorCode_BadRequest);
+      }
+
+      interpretBinaryTags = v.asBool();
+    }
+
     
     // Inject time-related information
     std::string date, time;
@@ -771,7 +794,7 @@
     }
 
 
-    InjectTags(dicom, request["Tags"]);
+    InjectTags(dicom, request["Tags"], interpretBinaryTags);
 
 
     // Inject the content (either an image, or a PDF file)
@@ -789,7 +812,7 @@
         if (content.size() > 0)
         {
           // Let's create a series instead of a single instance
-          CreateSeries(call, dicom, content);
+          CreateSeries(call, dicom, content, interpretBinaryTags);
           return;
         }
       }
--- a/OrthancServer/ParsedDicomFile.cpp	Wed Sep 23 12:02:39 2015 +0200
+++ b/OrthancServer/ParsedDicomFile.cpp	Wed Sep 23 12:43:05 2015 +0200
@@ -1139,13 +1139,10 @@
 
   void ParsedDicomFile::EmbedContent(const std::string& dataUriScheme)
   {
-    std::string mime, base64;
-    Toolbox::DecodeDataUriScheme(mime, base64, dataUriScheme);
+    std::string mime, content;
+    Toolbox::DecodeDataUriScheme(mime, content, dataUriScheme);
     Toolbox::ToLowerCase(mime);
 
-    std::string content;
-    Toolbox::DecodeBase64(content, base64);
-
     if (mime == "image/png")
     {
       EmbedImage(mime, content);
--- a/UnitTestsSources/FromDcmtkTests.cpp	Wed Sep 23 12:02:39 2015 +0200
+++ b/UnitTestsSources/FromDcmtkTests.cpp	Wed Sep 23 12:43:05 2015 +0200
@@ -145,14 +145,11 @@
   // Red dot in http://en.wikipedia.org/wiki/Data_URI_scheme (RGBA image)
   std::string s = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
 
-  std::string m, c;
-  Toolbox::DecodeDataUriScheme(m, c, s);
+  std::string m, cc;
+  Toolbox::DecodeDataUriScheme(m, cc, s);
 
   ASSERT_EQ("image/png", m);
-  ASSERT_EQ(116u, c.size());
 
-  std::string cc;
-  Toolbox::DecodeBase64(cc, c);
   PngReader reader;
   reader.ReadFromMemory(cc);