changeset 1085:63539e826355

Added TextRenderer
author Alain Mazy <alain@mazy.be>
date Tue, 22 Oct 2019 17:24:37 +0200
parents 8493f5fb6165
children 3dbdcecccf5d
files Applications/Samples/CMakeLists.txt Applications/Samples/SingleFrameEditorApplication.h Framework/Toolbox/TextRenderer.cpp Framework/Toolbox/TextRenderer.h Resources/CMake/OrthancStoneConfiguration.cmake
diffstat 5 files changed, 204 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Samples/CMakeLists.txt	Mon Oct 21 20:52:17 2019 +0200
+++ b/Applications/Samples/CMakeLists.txt	Tue Oct 22 17:24:37 2019 +0200
@@ -10,6 +10,15 @@
 
 set(ENABLE_STONE_DEPRECATED ON)  # Need deprecated classes for these samples
 
+include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
+DownloadPackage(
+  "a24b8136b8f3bb93f166baf97d9328de"
+  "http://orthanc.osimis.io/ThirdPartyDownloads/ubuntu-font-family-0.83.zip"
+  "${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83")
+
+set(ORTHANC_STONE_APPLICATION_RESOURCES
+  UBUNTU_FONT  ${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83/Ubuntu-R.ttf
+  )
 
 if (OPENSSL_NO_CAPIENG)
 add_definitions(-DOPENSSL_NO_CAPIENG=1)
--- a/Applications/Samples/SingleFrameEditorApplication.h	Mon Oct 21 20:52:17 2019 +0200
+++ b/Applications/Samples/SingleFrameEditorApplication.h	Tue Oct 22 17:24:37 2019 +0200
@@ -36,10 +36,14 @@
 #include "../../Framework/Radiography/RadiographySceneReader.h"
 #include "../../Framework/Radiography/RadiographyMaskLayer.h"
 
+#include <../../Framework/Toolbox/TextRenderer.h>
+
 #include <Core/HttpClient.h>
 #include <Core/Images/FontRegistry.h>
 #include <Core/Logging.h>
 #include <Core/OrthancException.h>
+#include <Core/Images/PngWriter.h>
+#include <Core/Images/PngReader.h>
 
 
 // Export using PAM is faster than using PNG, but requires Orthanc
@@ -117,12 +121,15 @@
           if (tool_ == Tool_Windowing)
           {
             return new RadiographyWindowingTracker(
-              undoRedoStack_, widget.GetScene(), widget, ImageInterpolation_Nearest,
-              viewportX, viewportY,
-              RadiographyWindowingTracker::Action_DecreaseWidth,
-              RadiographyWindowingTracker::Action_IncreaseWidth,
-              RadiographyWindowingTracker::Action_DecreaseCenter,
-              RadiographyWindowingTracker::Action_IncreaseCenter);
+                  undoRedoStack_,
+                  widget.GetScene(),
+                  widget,
+                  OrthancStone::ImageInterpolation_Nearest,
+                  viewportX, viewportY,
+                  RadiographyWindowingTracker::Action_DecreaseWidth,
+                  RadiographyWindowingTracker::Action_IncreaseWidth,
+                  RadiographyWindowingTracker::Action_DecreaseCenter,
+                  RadiographyWindowingTracker::Action_IncreaseCenter);
           }
           else if (!widget.LookupSelectedLayer(selected))
           {
@@ -511,6 +518,13 @@
         interactor_.SetMaskLayer(maskLayer_);
 
         {
+          std::auto_ptr<Orthanc::ImageAccessor> renderedTextAlpha(TextRenderer::RenderWhiteOnBlack(Orthanc::EmbeddedResources::UBUNTU_FONT, 100,
+                                                                                    "%öÇaA&#"));
+          RadiographyLayer& layer = scene_->LoadAlphaBitmap(renderedTextAlpha.release(), NULL);
+          dynamic_cast<RadiographyAlphaLayer&>(layer).SetForegroundValue(200);
+        }
+
+        {
           RadiographyLayer& layer = scene_->LoadText(fontRegistry_.GetFont(0), "Hello\nworld", NULL);
           layer.SetResizeable(true);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Toolbox/TextRenderer.cpp	Tue Oct 22 17:24:37 2019 +0200
@@ -0,0 +1,120 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2019 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "TextRenderer.h"
+
+#include <../../Framework/Scene2D/CairoCompositor.h>
+#include <../../Framework/Scene2D/ColorTextureSceneLayer.h>
+#include <../../Framework/Scene2D/FloatTextureSceneLayer.h>
+#include <../../Framework/Scene2D/TextSceneLayer.h>
+#include <../../Framework/Fonts/GlyphBitmapAlphabet.h>
+#include <../../Framework/Fonts/FontRenderer.h>
+#include <Core/Images/PngWriter.h>
+
+#include "Core/Images/Image.h"
+#include "Core/Images/ImageProcessing.h"
+
+namespace OrthancStone
+{
+  Orthanc::ImageAccessor* TextRenderer::Render(Orthanc::EmbeddedResources::FileResourceId font,
+                                               unsigned int fontSize,
+                                               const std::string& utf8String
+                                               )
+  {
+    FontRenderer renderer;
+    renderer.LoadFont(font, fontSize);
+
+    // add each char to be rendered to the alphabet
+    std::auto_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet);
+
+    size_t posInString = 0;
+    uint32_t unicode;
+    size_t utf8CharLength;
+
+    while (posInString < utf8String.size())
+    {
+      Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, utf8CharLength, utf8String, posInString);
+      alphabet->AddUnicodeCharacter(renderer, unicode);
+      posInString += utf8CharLength;
+    }
+
+    return alphabet->RenderText(utf8String);
+  }
+
+
+  Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
+                                                        unsigned int fontSize,
+                                                        const std::string& utf8String,
+                                                        uint8_t foreground)
+  {
+    std::auto_ptr<Orthanc::ImageAccessor> renderedText8(RenderWhiteOnBlack(resource, fontSize, utf8String));
+    std::auto_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true));
+
+    Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8);
+    return target.release();
+  }
+
+
+  // currently disabled because the background is actually not transparent once we use the Cairo Compositor !
+  //
+  //  // renders text in color + a border with alpha in a RGBA32 image
+  //  Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
+  //                                                        unsigned int fontSize,
+  //                                                        const std::string& utf8String,
+  //                                                        uint8_t foreground,
+  //                                                        uint8_t borderColor)
+  //  {
+  //    std::auto_ptr<Orthanc::ImageAccessor> renderedBorderAlpha(RenderWithAlpha(resource, fontSize, utf8String, borderColor));
+  //    std::auto_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(resource, fontSize, utf8String, foreground));
+
+  //    unsigned int textWidth = renderedBorderAlpha->GetWidth();
+  //    unsigned int textHeight = renderedBorderAlpha->GetHeight();
+
+  //    Scene2D targetScene;
+  //    std::auto_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha));
+  //    std::auto_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha));
+  //    std::auto_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha));
+  //    std::auto_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha));
+  //    std::auto_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha));
+
+  //    borderLayerLeft->SetOrigin(0, 1);
+  //    borderLayerRight->SetOrigin(2, 1);
+  //    borderLayerTop->SetOrigin(1, 0);
+  //    borderLayerBottom->SetOrigin(1, 2);
+  //    textLayerCenter->SetOrigin(1, 1);
+  //    targetScene.SetLayer(1, borderLayerLeft.release());
+  //    targetScene.SetLayer(2, borderLayerRight.release());
+  //    targetScene.SetLayer(3, borderLayerTop.release());
+  //    targetScene.SetLayer(4, borderLayerBottom.release());
+  //    targetScene.SetLayer(5, textLayerCenter.release());
+
+  //    targetScene.FitContent(textWidth + 2, textHeight + 2);
+  //    CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2);
+  //    compositor.Refresh();
+
+  //    Orthanc::ImageAccessor canvas;
+  //    compositor.GetCanvas().GetReadOnlyAccessor(canvas);
+
+  //    std::auto_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false));
+  //    Orthanc::ImageProcessing::Convert(*output, canvas);
+  //    return output.release();
+  //  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Toolbox/TextRenderer.h	Tue Oct 22 17:24:37 2019 +0200
@@ -0,0 +1,53 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2019 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "Core/Images/Image.h"
+#include <EmbeddedResources.h>
+
+namespace OrthancStone
+{
+  // Helpers methods to render text in bitmaps.
+  // Compared to the GlyphBitmapAlphabet::RenderText, these methods do not need a
+  // code page.
+  class TextRenderer : public boost::noncopyable
+  {
+  public:
+    // simply renders text in GrayScale8 with a black background and a white text
+    static Orthanc::ImageAccessor* Render(Orthanc::EmbeddedResources::FileResourceId resource,
+                                          unsigned int fontSize,
+                                          const std::string& utf8String);
+
+    // renders text in "color" with alpha in a RGBA32 image
+    static Orthanc::ImageAccessor* RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
+                                                   unsigned int fontSize,
+                                                   const std::string& utf8String,
+                                                   uint8_t foreground);
+
+    //    // renders text in color + a border with alpha in a RGBA32 image
+    //    static Orthanc::ImageAccessor* RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
+    //                                                   unsigned int fontSize,
+    //                                                   const std::string& utf8String,
+    //                                                   uint8_t foreground,
+    //                                                   uint8_t borderColor);
+  };
+}
--- a/Resources/CMake/OrthancStoneConfiguration.cmake	Mon Oct 21 20:52:17 2019 +0200
+++ b/Resources/CMake/OrthancStoneConfiguration.cmake	Tue Oct 22 17:24:37 2019 +0200
@@ -569,6 +569,8 @@
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SlicesSorter.h
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SubpixelReader.h
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SubvoxelReader.h
+  ${ORTHANC_STONE_ROOT}/Framework/Toolbox/TextRenderer.cpp
+  ${ORTHANC_STONE_ROOT}/Framework/Toolbox/TextRenderer.h
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/UndoRedoStack.cpp
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/UndoRedoStack.h
   ${ORTHANC_STONE_ROOT}/Framework/Toolbox/GenericToolbox.cpp