changeset 518:5ce456812a51 annotations

removed redundancies
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 21 Aug 2026 18:20:53 +0200
parents eefdef0a313e
children d7bad7647bdf
files Framework/BackgroundColor.cpp Framework/BackgroundColor.h ViewerPlugin/Annotations/AnnotationsRestApi.cpp ViewerPlugin/Plugin.cpp ViewerPlugin/ViewerToolbox.cpp ViewerPlugin/ViewerToolbox.h
diffstat 6 files changed, 51 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/BackgroundColor.cpp	Fri Aug 21 17:21:41 2026 +0200
+++ b/Framework/BackgroundColor.cpp	Fri Aug 21 18:20:53 2026 +0200
@@ -25,6 +25,8 @@
 
 #include "ImageToolbox.h"
 
+#include <iostream>
+
 #include <OrthancException.h>
 
 #include <boost/lexical_cast.hpp>
@@ -106,6 +108,21 @@
   }
 
 
+  std::string BackgroundColor::ToHexadecimalString() const
+  {
+    if (present_)
+    {
+      char tmp[32];
+      sprintf(tmp, "#%02x%02x%02x", red_, green_, blue_);
+      return tmp;
+    }
+    else
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);  // Should have called "IsPresent()"
+    }
+  }
+
+
   std::string BackgroundColor::ToHexadecimalString(uint8_t defaultRed,
                                                    uint8_t defaultGreen,
                                                    uint8_t defaultBlue) const
--- a/Framework/BackgroundColor.h	Fri Aug 21 17:21:41 2026 +0200
+++ b/Framework/BackgroundColor.h	Fri Aug 21 18:20:53 2026 +0200
@@ -74,6 +74,8 @@
 
     std::string Format() const;
 
+    std::string ToHexadecimalString() const;  // Will throw if absent
+
     std::string ToHexadecimalString(uint8_t defaultRed,
                                     uint8_t defaultGreen,
                                     uint8_t defaultBlue) const;
--- a/ViewerPlugin/Annotations/AnnotationsRestApi.cpp	Fri Aug 21 17:21:41 2026 +0200
+++ b/ViewerPlugin/Annotations/AnnotationsRestApi.cpp	Fri Aug 21 18:20:53 2026 +0200
@@ -24,6 +24,7 @@
 #include "../../Framework/PrecompiledHeadersWSI.h"
 #include "AnnotationsRestApi.h"
 
+#include "../../Framework/BackgroundColor.h"
 #include "../ViewerToolbox.h"
 #include "IAuthenticatedUser.h"
 
@@ -369,14 +370,14 @@
   {
   private:
     bool              isVisible_;
-    RGBColor          color_;
+    BackgroundColor   color_;
     std::string       id_;
     std::string       name_;
     std::set<UserId>  sharedWith_;
     bool              isPublic_;
 
   public:
-    UserLayer(const RGBColor& color,
+    UserLayer(const BackgroundColor& color,
               const std::string& name) :
       isVisible_(true),
       color_(color),
@@ -386,35 +387,30 @@
     {
     }
 
-    UserLayer(const Json::Value& source) :
-      color_(ViewerToolbox::ParseColor(Orthanc::SerializationToolbox::ReadString(source, KEY_COLOR)))
+    UserLayer(const Json::Value& source)
     {
+      if (!source.isObject() ||
+          !source.isMember(KEY_SHARED_WITH))
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
+      }
+
       isVisible_ = Orthanc::SerializationToolbox::ReadBoolean(source, KEY_VISIBLE);
+      color_ = BackgroundColor::FromHexadecimalString(Orthanc::SerializationToolbox::ReadString(source, KEY_COLOR));
       id_ = Orthanc::SerializationToolbox::ReadString(source, KEY_ID);
       name_ = Orthanc::SerializationToolbox::ReadString(source, KEY_NAME);
+      isPublic_ = Orthanc::SerializationToolbox::ReadBoolean(source, KEY_PUBLIC);
 
-      if (source.isMember(KEY_PUBLIC))
+      const Json::Value& sharedWith = source[KEY_SHARED_WITH];
+
+      if (!sharedWith.isArray())
       {
-        isPublic_ = Orthanc::SerializationToolbox::ReadBoolean(source, KEY_PUBLIC);
-      }
-      else
-      {
-        isPublic_ = false;
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
       }
 
-      if (source.isMember(KEY_SHARED_WITH))
+      for (Json::Value::ArrayIndex i = 0; i < sharedWith.size(); i++)
       {
-        const Json::Value& sharedWith = source[KEY_SHARED_WITH];
-
-        if (!sharedWith.isArray())
-        {
-          throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-        }
-
-        for (Json::Value::ArrayIndex i = 0; i < sharedWith.size(); i++)
-        {
-          sharedWith_.insert(UserId(sharedWith[i]));
-        }
+        sharedWith_.insert(UserId(sharedWith[i]));
       }
     }
 
@@ -428,7 +424,7 @@
       return isVisible_;
     }
 
-    const RGBColor& GetColor() const
+    const BackgroundColor& GetColor() const
     {
       return color_;
     }
@@ -465,7 +461,7 @@
 
       target = Json::objectValue;
       target[KEY_VISIBLE] = isVisible_;
-      target[KEY_COLOR] = ViewerToolbox::SerializeColor(color_);
+      target[KEY_COLOR] = color_.ToHexadecimalString();
       target[KEY_ID] = id_;
       target[KEY_NAME] = name_;
       target[KEY_PUBLIC] = isPublic_;
@@ -478,7 +474,7 @@
   {
   private:
     bool         isVisible_;
-    RGBColor     color_;
+    BackgroundColor     color_;
     UserId       author_;
     std::string  id_;
     std::string  name_;
@@ -494,21 +490,19 @@
     {
     }
 
-    SharedLayer(const Json::Value& source) :
-      color_(0, 0, 0)
+    SharedLayer(const Json::Value& source)
     {
-      if (!source.isMember(KEY_AUTHOR))
+      if (!source.isObject() ||
+          !source.isMember(KEY_AUTHOR))
       {
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
       }
 
       isVisible_ = Orthanc::SerializationToolbox::ReadBoolean(source, KEY_VISIBLE);
       author_ = UserId(source[KEY_AUTHOR]);
       id_ = Orthanc::SerializationToolbox::ReadString(source, KEY_ID);
       name_ = Orthanc::SerializationToolbox::ReadString(source, KEY_NAME);
-
-      std::string color = Orthanc::SerializationToolbox::ReadString(source, KEY_COLOR);
-      color_ = ViewerToolbox::ParseColor(color);
+      color_ = BackgroundColor::FromHexadecimalString(Orthanc::SerializationToolbox::ReadString(source, KEY_COLOR));
     }
 
     virtual std::string GetId() const ORTHANC_OVERRIDE
@@ -521,7 +515,7 @@
       return isVisible_;
     }
 
-    const RGBColor& GetColor() const
+    const BackgroundColor& GetColor() const
     {
       return color_;
     }
@@ -540,7 +534,7 @@
     {
       target = Json::objectValue;
       target[KEY_VISIBLE] = isVisible_;
-      target[KEY_COLOR] = ViewerToolbox::SerializeColor(color_);
+      target[KEY_COLOR] = color_.ToHexadecimalString();
       target[KEY_ID] = id_;
       target[KEY_NAME] = name_;
 
@@ -609,9 +603,9 @@
 
       size_t item = userLayers_.GetSize() % PALETTE_SIZE;
 
-      RGBColor color(PALETTE[3 * item],
-                     PALETTE[3 * item + 1],
-                     PALETTE[3 * item + 2]);
+      BackgroundColor color(PALETTE[3 * item],
+                            PALETTE[3 * item + 1],
+                            PALETTE[3 * item + 2]);
 
       std::string name;
       if (userLayers_.GetSize() == 0)
--- a/ViewerPlugin/Plugin.cpp	Fri Aug 21 17:21:41 2026 +0200
+++ b/ViewerPlugin/Plugin.cpp	Fri Aug 21 18:20:53 2026 +0200
@@ -23,6 +23,7 @@
 
 #include "../Framework/PrecompiledHeadersWSI.h"
 
+#include "../Framework/ColorSpaces.h"
 #include "../Framework/ImageToolbox.h"
 #include "Annotations/AnnotationsRestApi.h"
 #include "DicomPyramidCache.h"
--- a/ViewerPlugin/ViewerToolbox.cpp	Fri Aug 21 17:21:41 2026 +0200
+++ b/ViewerPlugin/ViewerToolbox.cpp	Fri Aug 21 18:20:53 2026 +0200
@@ -24,33 +24,11 @@
 #include "../Framework/PrecompiledHeadersWSI.h"
 #include "ViewerToolbox.h"
 
-#include <OrthancException.h>
 #include <Toolbox.h>
 
 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
 
 
-static unsigned int GetHex(char c)
-{
-  if (c >= '0' && c <= '9')
-  {
-    return c - '0';
-  }
-  else if (c >= 'a' && c <= 'f')
-  {
-    return c - 'a' + 10;
-  }
-  else if (c >= 'A' && c <= 'F')
-  {
-    return c - 'A' + 10;
-  }
-  else
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-  }
-}
-
-
 namespace OrthancWSI
 {
   namespace ViewerToolbox
@@ -69,30 +47,5 @@
       Json::Value answer = Json::objectValue;
       AnswerJson(output, answer);
     }
-
-
-    RGBColor ParseColor(const std::string& color)
-    {
-      if (color.size() != 7 ||
-          color[0] != '#')
-      {
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
-      }
-      else
-      {
-        unsigned int r = GetHex(color[1]) * 16 + GetHex(color[2]);
-        unsigned int g = GetHex(color[3]) * 16 + GetHex(color[4]);
-        unsigned int b = GetHex(color[5]) * 16 + GetHex(color[6]);
-        return RGBColor(r, g, b);
-      }
-    }
-
-
-    std::string SerializeColor(const RGBColor& color)
-    {
-      char buf[16];
-      sprintf(buf, "#%02x%02x%02x", color.GetR(), color.GetG(), color.GetB());
-      return buf;
-    }
   }
 }
--- a/ViewerPlugin/ViewerToolbox.h	Fri Aug 21 17:21:41 2026 +0200
+++ b/ViewerPlugin/ViewerToolbox.h	Fri Aug 21 18:20:53 2026 +0200
@@ -23,8 +23,6 @@
 
 #pragma once
 
-#include "../Framework/ColorSpaces.h"
-
 #include <json/value.h>
 #include <orthanc/OrthancCPlugin.h>
 
@@ -37,9 +35,5 @@
                     const Json::Value& value);
 
     void AnswerEmpty(OrthancPluginRestOutput* output);
-
-    RGBColor ParseColor(const std::string& color);
-
-    std::string SerializeColor(const RGBColor& color);
   }
 }