Mercurial > hg > orthanc-stone
changeset 807:c237e0625065
deprecating CairoFont
line wrap: on
line diff
--- a/Applications/Sdl/SdlCairoSurface.h Tue May 28 16:01:13 2019 +0200 +++ b/Applications/Sdl/SdlCairoSurface.h Tue May 28 17:44:53 2019 +0200 @@ -24,7 +24,7 @@ #if ORTHANC_ENABLE_SDL == 1 #include "SdlWindow.h" -#include "../../Framework/Viewport/CairoSurface.h" +#include "../../Framework/Wrappers/CairoSurface.h" #include "../../Framework/Deprecated/Viewport/IViewport.h" #include <boost/thread/mutex.hpp>
--- a/Framework/Deprecated/Layers/ILayerRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Deprecated/Layers/ILayerRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoContext.h" +#include "../../Wrappers/CairoContext.h" #include "../../Toolbox/CoordinateSystem3D.h" #include "../Toolbox/ViewportGeometry.h" #include "RenderStyle.h"
--- a/Framework/Deprecated/Toolbox/ViewportGeometry.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Deprecated/Toolbox/ViewportGeometry.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoContext.h" +#include "../../Wrappers/CairoContext.h" #include "../../Toolbox/Extent2D.h" #include "../../Toolbox/LinearAlgebra.h" #include "../Viewport/IMouseTracker.h" // to include "Touch" definition
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Deprecated/Viewport/CairoFont.cpp Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,65 @@ +/** + * 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 "CairoFont.h" + +#include <Core/Logging.h> +#include <Core/OrthancException.h> + +namespace Deprecated +{ + CairoFont::CairoFont(const char* family, + cairo_font_slant_t slant, + cairo_font_weight_t weight) + { + font_ = cairo_toy_font_face_create(family, slant, weight); + if (font_ == NULL) + { + LOG(ERROR) << "Unknown font: " << family; + throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); + } + } + + + CairoFont::~CairoFont() + { + if (font_ != NULL) + { + cairo_font_face_destroy(font_); + } + } + + + void CairoFont::Draw(OrthancStone::CairoContext& context, + const std::string& text, + double size) + { + if (size <= 0) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + + cairo_t* cr = context.GetObject(); + cairo_set_font_face(cr, font_); + cairo_set_font_size(cr, size); + cairo_show_text(cr, text.c_str()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Deprecated/Viewport/CairoFont.h Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,52 @@ +/** + * 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 + +#if !defined(ORTHANC_SANDBOXED) +# error The macro ORTHANC_SANDBOXED must be defined +#endif + +#if ORTHANC_SANDBOXED == 1 +# error The class CairoFont cannot be used in sandboxed environments +#endif + +#include "../../Wrappers/CairoContext.h" + +namespace Deprecated +{ + class CairoFont : public boost::noncopyable + { + private: + cairo_font_face_t* font_; + + public: + CairoFont(const char* family, + cairo_font_slant_t slant, + cairo_font_weight_t weight); + + ~CairoFont(); + + void Draw(OrthancStone::CairoContext& context, + const std::string& text, + double size); + }; +}
--- a/Framework/Deprecated/Viewport/IMouseTracker.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Deprecated/Viewport/IMouseTracker.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include <vector> namespace Deprecated
--- a/Framework/Deprecated/Widgets/IWorldSceneMouseTracker.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Deprecated/Widgets/IWorldSceneMouseTracker.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoContext.h" +#include "../../Wrappers/CairoContext.h" #include "../Viewport/IMouseTracker.h" // only to get the "Touch" definition namespace Deprecated
--- a/Framework/Deprecated/Widgets/WidgetBase.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Deprecated/Widgets/WidgetBase.h Tue May 28 17:44:53 2019 +0200 @@ -23,7 +23,7 @@ #include "IWidget.h" -#include "../../Viewport/CairoContext.h" +#include "../../Wrappers/CairoContext.h" #include "../Viewport/WidgetViewport.h" namespace Deprecated
--- a/Framework/Radiography/RadiographyLayer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Radiography/RadiographyLayer.h Tue May 28 17:44:53 2019 +0200 @@ -23,7 +23,7 @@ #include "../Toolbox/AffineTransform2D.h" #include "../Toolbox/Extent2D.h" -#include "../Viewport/CairoContext.h" +#include "../Wrappers/CairoContext.h" #include "../Messages/IMessage.h" #include "../Messages/IObservable.h"
--- a/Framework/Scene2D/CairoCompositor.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/CairoCompositor.h Tue May 28 17:44:53 2019 +0200 @@ -22,7 +22,7 @@ #pragma once #include "../Fonts/GlyphBitmapAlphabet.h" -#include "../Viewport/CairoContext.h" +#include "../Wrappers/CairoContext.h" #include "Internals/CompositorHelper.h" #include "Internals/ICairoContextProvider.h"
--- a/Framework/Scene2D/Internals/CairoColorTextureRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/Internals/CairoColorTextureRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include "CompositorHelper.h" #include "ICairoContextProvider.h"
--- a/Framework/Scene2D/Internals/CairoFloatTextureRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/Internals/CairoFloatTextureRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include "CompositorHelper.h" #include "ICairoContextProvider.h"
--- a/Framework/Scene2D/Internals/CairoInfoPanelRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/Internals/CairoInfoPanelRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include "CompositorHelper.h" #include "ICairoContextProvider.h"
--- a/Framework/Scene2D/Internals/CairoLookupTableTextureRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/Internals/CairoLookupTableTextureRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -21,7 +21,7 @@ #pragma once -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include "CompositorHelper.h" #include "ICairoContextProvider.h"
--- a/Framework/Scene2D/Internals/CairoTextRenderer.h Tue May 28 16:01:13 2019 +0200 +++ b/Framework/Scene2D/Internals/CairoTextRenderer.h Tue May 28 17:44:53 2019 +0200 @@ -22,7 +22,7 @@ #pragma once #include "../../Fonts/GlyphBitmapAlphabet.h" -#include "../../Viewport/CairoSurface.h" +#include "../../Wrappers/CairoSurface.h" #include "../TextSceneLayer.h" #include "CairoBaseRenderer.h"
--- a/Framework/Viewport/CairoContext.cpp Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,146 +0,0 @@ -/** - * 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 "CairoContext.h" - -#include <Core/Logging.h> -#include <Core/OrthancException.h> - - -namespace OrthancStone -{ - CairoContext::CairoContext(CairoSurface& surface) : - width_(surface.GetWidth()), - height_(surface.GetHeight()) - { - context_ = cairo_create(surface.GetObject()); - if (!context_) - { - LOG(ERROR) << "Cannot create Cairo drawing context"; - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - } - - - CairoContext::~CairoContext() - { - if (context_ != NULL) - { - cairo_destroy(context_); - context_ = NULL; - } - } - - - void CairoContext::SetSourceColor(uint8_t red, - uint8_t green, - uint8_t blue) - { - cairo_set_source_rgb(context_, - static_cast<float>(red) / 255.0f, - static_cast<float>(green) / 255.0f, - static_cast<float>(blue) / 255.0f); - } - - - class CairoContext::AlphaSurface : public boost::noncopyable - { - private: - cairo_surface_t *surface_; - - public: - AlphaSurface(unsigned int width, - unsigned int height) - { - surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height); - - if (!surface_) - { - // Should never occur - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - - if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) - { - LOG(ERROR) << "Cannot create a Cairo surface"; - cairo_surface_destroy(surface_); - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - } - - ~AlphaSurface() - { - cairo_surface_destroy(surface_); - } - - void GetAccessor(Orthanc::ImageAccessor& target) - { - target.AssignWritable(Orthanc::PixelFormat_Grayscale8, - cairo_image_surface_get_width(surface_), - cairo_image_surface_get_height(surface_), - cairo_image_surface_get_stride(surface_), - cairo_image_surface_get_data(surface_)); - } - - void Blit(cairo_t* cr, - double x, - double y) - { - cairo_surface_mark_dirty(surface_); - cairo_mask_surface(cr, surface_, x, y); - cairo_fill(cr); - } - }; - - - void CairoContext::DrawText(const Orthanc::Font& font, - const std::string& text, - double x, - double y, - BitmapAnchor anchor) - { - // Render a bitmap containing the text - unsigned int width, height; - font.ComputeTextExtent(width, height, text); - - AlphaSurface surface(width, height); - - Orthanc::ImageAccessor accessor; - surface.GetAccessor(accessor); - font.Draw(accessor, text, 0, 0, 255); - - // Correct the text location given the anchor location - double deltaX, deltaY; - ComputeAnchorTranslation(deltaX, deltaY, anchor, width, height); - - // Cancel zoom/rotation before blitting the text onto the surface - double pixelX = x; - double pixelY = y; - cairo_user_to_device(context_, &pixelX, &pixelY); - - cairo_save(context_); - cairo_identity_matrix(context_); - - // Blit the text bitmap - surface.Blit(context_, pixelX + deltaX, pixelY + deltaY); - cairo_restore(context_); - } -}
--- a/Framework/Viewport/CairoContext.h Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/** - * 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 "CairoSurface.h" -#include "../StoneEnumerations.h" - -#include <Core/Images/Font.h> - -namespace OrthancStone -{ - // This is a RAII wrapper around the Cairo drawing context - class CairoContext : public boost::noncopyable - { - private: - class AlphaSurface; - - cairo_t* context_; - unsigned int width_; - unsigned int height_; - - public: - CairoContext(CairoSurface& surface); - - ~CairoContext(); - - cairo_t* GetObject() - { - return context_; - } - - unsigned int GetWidth() const - { - return width_; - } - - unsigned int GetHeight() const - { - return height_; - } - - void SetSourceColor(uint8_t red, - uint8_t green, - uint8_t blue); - - void SetSourceColor(const uint8_t color[3]) - { - SetSourceColor(color[0], color[1], color[2]); - } - - void DrawText(const Orthanc::Font& font, - const std::string& text, - double x, - double y, - BitmapAnchor anchor); - }; -}
--- a/Framework/Viewport/CairoFont.cpp Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -/** - * 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 "CairoFont.h" - -#include <Core/Logging.h> -#include <Core/OrthancException.h> - -namespace OrthancStone -{ - CairoFont::CairoFont(const char* family, - cairo_font_slant_t slant, - cairo_font_weight_t weight) - { - font_ = cairo_toy_font_face_create(family, slant, weight); - if (font_ == NULL) - { - LOG(ERROR) << "Unknown font: " << family; - throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); - } - } - - - CairoFont::~CairoFont() - { - if (font_ != NULL) - { - cairo_font_face_destroy(font_); - } - } - - - void CairoFont::Draw(CairoContext& context, - const std::string& text, - double size) - { - if (size <= 0) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); - } - - cairo_t* cr = context.GetObject(); - cairo_set_font_face(cr, font_); - cairo_set_font_size(cr, size); - cairo_show_text(cr, text.c_str()); - } -}
--- a/Framework/Viewport/CairoFont.h Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/** - * 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 - -#if !defined(ORTHANC_SANDBOXED) -# error The macro ORTHANC_SANDBOXED must be defined -#endif - -#if ORTHANC_SANDBOXED == 1 -# error The class CairoFont cannot be used in sandboxed environments -#endif - -#include "CairoContext.h" - -namespace OrthancStone -{ - class CairoFont : public boost::noncopyable - { - private: - cairo_font_face_t* font_; - - public: - CairoFont(const char* family, - cairo_font_slant_t slant, - cairo_font_weight_t weight); - - ~CairoFont(); - - void Draw(CairoContext& context, - const std::string& text, - double size); - }; -}
--- a/Framework/Viewport/CairoSurface.cpp Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,155 +0,0 @@ -/** - * 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 "CairoSurface.h" - -#include <Core/Logging.h> -#include <Core/OrthancException.h> -#include <Core/Images/ImageProcessing.h> - -namespace OrthancStone -{ - void CairoSurface::Release() - { - if (surface_) - { - cairo_surface_destroy(surface_); - surface_ = NULL; - } - } - - - void CairoSurface::Allocate(unsigned int width, - unsigned int height, - bool hasAlpha) - { - Release(); - - hasAlpha_ = hasAlpha; - - surface_ = cairo_image_surface_create - (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height); - if (!surface_) - { - // Should never occur - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - - if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) - { - LOG(ERROR) << "Cannot create a Cairo surface"; - cairo_surface_destroy(surface_); - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - - width_ = width; - height_ = height; - pitch_ = cairo_image_surface_get_stride(surface_); - buffer_ = cairo_image_surface_get_data(surface_); - } - - - CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor, - bool hasAlpha) : - hasAlpha_(hasAlpha) - { - if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); - } - - width_ = accessor.GetWidth(); - height_ = accessor.GetHeight(); - pitch_ = accessor.GetPitch(); - buffer_ = accessor.GetBuffer(); - - surface_ = cairo_image_surface_create_for_data - (reinterpret_cast<unsigned char*>(buffer_), - hasAlpha_ ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, - width_, height_, pitch_); - if (!surface_) - { - // Should never occur - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - - if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) - { - LOG(ERROR) << "Bad pitch for a Cairo surface"; - cairo_surface_destroy(surface_); - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); - } - } - - - void CairoSurface::SetSize(unsigned int width, - unsigned int height, - bool hasAlpha) - { - if (hasAlpha_ != hasAlpha || - width_ != width || - height_ != height) - { - Allocate(width, height, hasAlpha); - } - } - - - void CairoSurface::Copy(const CairoSurface& other) - { - SetSize(other.GetWidth(), other.GetHeight(), other.HasAlpha()); - - Orthanc::ImageAccessor source, target; - - other.GetReadOnlyAccessor(source); - GetWriteableAccessor(target); - - Orthanc::ImageProcessing::Copy(target, source); - - cairo_surface_mark_dirty(surface_); - } - - - void CairoSurface::Copy(const Orthanc::ImageAccessor& source, - bool hasAlpha) - { - SetSize(source.GetWidth(), source.GetHeight(), hasAlpha); - - Orthanc::ImageAccessor target; - GetWriteableAccessor(target); - - Orthanc::ImageProcessing::Convert(target, source); - - cairo_surface_mark_dirty(surface_); - } - - - void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const - { - target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); - } - - - void CairoSurface::GetWriteableAccessor(Orthanc::ImageAccessor& target) - { - target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); - } -}
--- a/Framework/Viewport/CairoSurface.h Tue May 28 16:01:13 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,118 +0,0 @@ -/** - * 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/ImageAccessor.h> - -#include <boost/noncopyable.hpp> -#include <cairo.h> - -namespace OrthancStone -{ - class CairoSurface : public boost::noncopyable - { - private: - cairo_surface_t* surface_; - unsigned int width_; - unsigned int height_; - unsigned int pitch_; - void* buffer_; - bool hasAlpha_; - - void Release(); - - void Allocate(unsigned int width, - unsigned int height, - bool hasAlpha); - - public: - CairoSurface() : - surface_(NULL) - { - Allocate(0, 0, false); - } - - CairoSurface(unsigned int width, - unsigned int height, - bool hasAlpha) : - surface_(NULL) - { - Allocate(width, height, hasAlpha); - } - - CairoSurface(Orthanc::ImageAccessor& accessor, - bool hasAlpha); - - ~CairoSurface() - { - Release(); - } - - void SetSize(unsigned int width, - unsigned int height, - bool hasAlpha); - - void Copy(const CairoSurface& other); - - void Copy(const Orthanc::ImageAccessor& source, - bool hasAlpha); - - unsigned int GetWidth() const - { - return width_; - } - - unsigned int GetHeight() const - { - return height_; - } - - unsigned int GetPitch() const - { - return pitch_; - } - - const void* GetBuffer() const - { - return buffer_; - } - - void* GetBuffer() - { - return buffer_; - } - - cairo_surface_t* GetObject() - { - return surface_; - } - - bool HasAlpha() const - { - return hasAlpha_; - } - - void GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const; - - void GetWriteableAccessor(Orthanc::ImageAccessor& target); - }; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Wrappers/CairoContext.cpp Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,146 @@ +/** + * 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 "CairoContext.h" + +#include <Core/Logging.h> +#include <Core/OrthancException.h> + + +namespace OrthancStone +{ + CairoContext::CairoContext(CairoSurface& surface) : + width_(surface.GetWidth()), + height_(surface.GetHeight()) + { + context_ = cairo_create(surface.GetObject()); + if (!context_) + { + LOG(ERROR) << "Cannot create Cairo drawing context"; + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + } + + + CairoContext::~CairoContext() + { + if (context_ != NULL) + { + cairo_destroy(context_); + context_ = NULL; + } + } + + + void CairoContext::SetSourceColor(uint8_t red, + uint8_t green, + uint8_t blue) + { + cairo_set_source_rgb(context_, + static_cast<float>(red) / 255.0f, + static_cast<float>(green) / 255.0f, + static_cast<float>(blue) / 255.0f); + } + + + class CairoContext::AlphaSurface : public boost::noncopyable + { + private: + cairo_surface_t *surface_; + + public: + AlphaSurface(unsigned int width, + unsigned int height) + { + surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height); + + if (!surface_) + { + // Should never occur + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) + { + LOG(ERROR) << "Cannot create a Cairo surface"; + cairo_surface_destroy(surface_); + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + } + + ~AlphaSurface() + { + cairo_surface_destroy(surface_); + } + + void GetAccessor(Orthanc::ImageAccessor& target) + { + target.AssignWritable(Orthanc::PixelFormat_Grayscale8, + cairo_image_surface_get_width(surface_), + cairo_image_surface_get_height(surface_), + cairo_image_surface_get_stride(surface_), + cairo_image_surface_get_data(surface_)); + } + + void Blit(cairo_t* cr, + double x, + double y) + { + cairo_surface_mark_dirty(surface_); + cairo_mask_surface(cr, surface_, x, y); + cairo_fill(cr); + } + }; + + + void CairoContext::DrawText(const Orthanc::Font& font, + const std::string& text, + double x, + double y, + BitmapAnchor anchor) + { + // Render a bitmap containing the text + unsigned int width, height; + font.ComputeTextExtent(width, height, text); + + AlphaSurface surface(width, height); + + Orthanc::ImageAccessor accessor; + surface.GetAccessor(accessor); + font.Draw(accessor, text, 0, 0, 255); + + // Correct the text location given the anchor location + double deltaX, deltaY; + ComputeAnchorTranslation(deltaX, deltaY, anchor, width, height); + + // Cancel zoom/rotation before blitting the text onto the surface + double pixelX = x; + double pixelY = y; + cairo_user_to_device(context_, &pixelX, &pixelY); + + cairo_save(context_); + cairo_identity_matrix(context_); + + // Blit the text bitmap + surface.Blit(context_, pixelX + deltaX, pixelY + deltaY); + cairo_restore(context_); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Wrappers/CairoContext.h Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,76 @@ +/** + * 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 "CairoSurface.h" +#include "../StoneEnumerations.h" + +#include <Core/Images/Font.h> + +namespace OrthancStone +{ + // This is a RAII wrapper around the Cairo drawing context + class CairoContext : public boost::noncopyable + { + private: + class AlphaSurface; + + cairo_t* context_; + unsigned int width_; + unsigned int height_; + + public: + CairoContext(CairoSurface& surface); + + ~CairoContext(); + + cairo_t* GetObject() + { + return context_; + } + + unsigned int GetWidth() const + { + return width_; + } + + unsigned int GetHeight() const + { + return height_; + } + + void SetSourceColor(uint8_t red, + uint8_t green, + uint8_t blue); + + void SetSourceColor(const uint8_t color[3]) + { + SetSourceColor(color[0], color[1], color[2]); + } + + void DrawText(const Orthanc::Font& font, + const std::string& text, + double x, + double y, + BitmapAnchor anchor); + }; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Wrappers/CairoSurface.cpp Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,155 @@ +/** + * 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 "CairoSurface.h" + +#include <Core/Logging.h> +#include <Core/OrthancException.h> +#include <Core/Images/ImageProcessing.h> + +namespace OrthancStone +{ + void CairoSurface::Release() + { + if (surface_) + { + cairo_surface_destroy(surface_); + surface_ = NULL; + } + } + + + void CairoSurface::Allocate(unsigned int width, + unsigned int height, + bool hasAlpha) + { + Release(); + + hasAlpha_ = hasAlpha; + + surface_ = cairo_image_surface_create + (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height); + if (!surface_) + { + // Should never occur + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) + { + LOG(ERROR) << "Cannot create a Cairo surface"; + cairo_surface_destroy(surface_); + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + width_ = width; + height_ = height; + pitch_ = cairo_image_surface_get_stride(surface_); + buffer_ = cairo_image_surface_get_data(surface_); + } + + + CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor, + bool hasAlpha) : + hasAlpha_(hasAlpha) + { + if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); + } + + width_ = accessor.GetWidth(); + height_ = accessor.GetHeight(); + pitch_ = accessor.GetPitch(); + buffer_ = accessor.GetBuffer(); + + surface_ = cairo_image_surface_create_for_data + (reinterpret_cast<unsigned char*>(buffer_), + hasAlpha_ ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, + width_, height_, pitch_); + if (!surface_) + { + // Should never occur + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) + { + LOG(ERROR) << "Bad pitch for a Cairo surface"; + cairo_surface_destroy(surface_); + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + } + + + void CairoSurface::SetSize(unsigned int width, + unsigned int height, + bool hasAlpha) + { + if (hasAlpha_ != hasAlpha || + width_ != width || + height_ != height) + { + Allocate(width, height, hasAlpha); + } + } + + + void CairoSurface::Copy(const CairoSurface& other) + { + SetSize(other.GetWidth(), other.GetHeight(), other.HasAlpha()); + + Orthanc::ImageAccessor source, target; + + other.GetReadOnlyAccessor(source); + GetWriteableAccessor(target); + + Orthanc::ImageProcessing::Copy(target, source); + + cairo_surface_mark_dirty(surface_); + } + + + void CairoSurface::Copy(const Orthanc::ImageAccessor& source, + bool hasAlpha) + { + SetSize(source.GetWidth(), source.GetHeight(), hasAlpha); + + Orthanc::ImageAccessor target; + GetWriteableAccessor(target); + + Orthanc::ImageProcessing::Convert(target, source); + + cairo_surface_mark_dirty(surface_); + } + + + void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const + { + target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); + } + + + void CairoSurface::GetWriteableAccessor(Orthanc::ImageAccessor& target) + { + target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Wrappers/CairoSurface.h Tue May 28 17:44:53 2019 +0200 @@ -0,0 +1,118 @@ +/** + * 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/ImageAccessor.h> + +#include <boost/noncopyable.hpp> +#include <cairo.h> + +namespace OrthancStone +{ + class CairoSurface : public boost::noncopyable + { + private: + cairo_surface_t* surface_; + unsigned int width_; + unsigned int height_; + unsigned int pitch_; + void* buffer_; + bool hasAlpha_; + + void Release(); + + void Allocate(unsigned int width, + unsigned int height, + bool hasAlpha); + + public: + CairoSurface() : + surface_(NULL) + { + Allocate(0, 0, false); + } + + CairoSurface(unsigned int width, + unsigned int height, + bool hasAlpha) : + surface_(NULL) + { + Allocate(width, height, hasAlpha); + } + + CairoSurface(Orthanc::ImageAccessor& accessor, + bool hasAlpha); + + ~CairoSurface() + { + Release(); + } + + void SetSize(unsigned int width, + unsigned int height, + bool hasAlpha); + + void Copy(const CairoSurface& other); + + void Copy(const Orthanc::ImageAccessor& source, + bool hasAlpha); + + unsigned int GetWidth() const + { + return width_; + } + + unsigned int GetHeight() const + { + return height_; + } + + unsigned int GetPitch() const + { + return pitch_; + } + + const void* GetBuffer() const + { + return buffer_; + } + + void* GetBuffer() + { + return buffer_; + } + + cairo_surface_t* GetObject() + { + return surface_; + } + + bool HasAlpha() const + { + return hasAlpha_; + } + + void GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const; + + void GetWriteableAccessor(Orthanc::ImageAccessor& target); + }; +}
--- a/Resources/CMake/OrthancStoneConfiguration.cmake Tue May 28 16:01:13 2019 +0200 +++ b/Resources/CMake/OrthancStoneConfiguration.cmake Tue May 28 17:44:53 2019 +0200 @@ -252,7 +252,6 @@ if (NOT ORTHANC_SANDBOXED) set(PLATFORM_SOURCES - ${ORTHANC_STONE_ROOT}/Framework/Viewport/CairoFont.cpp ${ORTHANC_STONE_ROOT}/Platforms/Generic/WebServiceCommandBase.cpp ${ORTHANC_STONE_ROOT}/Platforms/Generic/WebServiceGetCommand.cpp ${ORTHANC_STONE_ROOT}/Platforms/Generic/WebServicePostCommand.cpp @@ -263,6 +262,12 @@ ${ORTHANC_STONE_ROOT}/Platforms/Generic/OracleDelayedCallExecutor.h ) + if (ENABLE_STONE_DEPRECATED) + list(APPEND PLATFORM_SOURCES + ${ORTHANC_STONE_ROOT}/Framework/Deprecated/Viewport/CairoFont.cpp + ) + endif() + if (ENABLE_SDL OR ENABLE_QT) list(APPEND APPLICATIONS_SOURCES ${ORTHANC_STONE_ROOT}/Applications/Generic/NativeStoneApplicationRunner.cpp @@ -484,8 +489,8 @@ ${ORTHANC_STONE_ROOT}/Framework/Toolbox/ShearWarpProjectiveTransform.cpp ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SlicesSorter.cpp ${ORTHANC_STONE_ROOT}/Framework/Toolbox/UndoRedoStack.cpp - ${ORTHANC_STONE_ROOT}/Framework/Viewport/CairoContext.cpp - ${ORTHANC_STONE_ROOT}/Framework/Viewport/CairoSurface.cpp + ${ORTHANC_STONE_ROOT}/Framework/Wrappers/CairoContext.cpp + ${ORTHANC_STONE_ROOT}/Framework/Wrappers/CairoSurface.cpp ${ORTHANC_STONE_ROOT}/Framework/Volumes/ImageBuffer3D.cpp ${ORTHANC_STONE_ROOT}/Framework/Volumes/OrientedVolumeBoundingBox.cpp ${ORTHANC_STONE_ROOT}/Framework/Volumes/VolumeImageGeometry.cpp