Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/Internals/CairoTextRenderer.cpp @ 1883:fba6e550e0ee
apply rescale slope/intercept in rendering plugin
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Jan 2022 18:26:05 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
rev | line source |
---|---|
597 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
597 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
597 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
597 | 21 **/ |
22 | |
23 | |
24 #include "CairoTextRenderer.h" | |
25 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1298
diff
changeset
|
26 #include <OrthancException.h> |
597 | 27 |
28 namespace OrthancStone | |
29 { | |
30 namespace Internals | |
31 { | |
32 CairoTextRenderer::CairoTextRenderer(ICairoContextProvider& target, | |
33 const GlyphBitmapAlphabet& alphabet, | |
34 const TextSceneLayer& layer) : | |
35 CairoBaseRenderer(target, layer) | |
36 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
37 std::unique_ptr<Orthanc::ImageAccessor> source(alphabet.RenderText(layer.GetText())); |
597 | 38 |
39 if (source.get() != NULL) | |
40 { | |
41 text_.SetSize(source->GetWidth(), source->GetHeight(), true); | |
42 | |
1571 | 43 Orthanc::ImageAccessor accessor; |
44 text_.GetWriteableAccessor(accessor); | |
597 | 45 |
46 if (source->GetFormat() != Orthanc::PixelFormat_Grayscale8 || | |
1571 | 47 accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) |
597 | 48 { |
49 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
50 } | |
51 | |
1625
4a2c63ae8f99
minor optimization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
52 const unsigned int height = source->GetHeight(); |
597 | 53 const unsigned int width = source->GetWidth(); |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
54 const Color& color = layer.GetColor(); |
1625
4a2c63ae8f99
minor optimization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
55 |
4a2c63ae8f99
minor optimization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
56 for (unsigned int y = 0; y < height; y++) |
597 | 57 { |
58 const uint8_t* p = reinterpret_cast<const uint8_t*>(source->GetConstRow(y)); | |
1571 | 59 uint8_t* q = reinterpret_cast<uint8_t*>(accessor.GetRow(y)); |
597 | 60 |
61 for (unsigned int x = 0; x < width; x++) | |
62 { | |
63 unsigned int alpha = *p; | |
64 | |
65 // Premultiplied alpha | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
66 q[0] = static_cast<uint8_t>((color.GetBlue() * alpha) / 255); |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
67 q[1] = static_cast<uint8_t>((color.GetGreen() * alpha) / 255); |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
68 q[2] = static_cast<uint8_t>((color.GetRed() * alpha) / 255); |
597 | 69 q[3] = *p; |
70 | |
71 p++; | |
72 q += 4; | |
73 } | |
74 } | |
75 | |
76 cairo_surface_mark_dirty(text_.GetObject()); | |
77 } | |
78 } | |
79 | |
80 | |
888
6e888cf6a48b
renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
804
diff
changeset
|
81 void CairoTextRenderer::Render(const AffineTransform2D& transform, |
6e888cf6a48b
renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
804
diff
changeset
|
82 unsigned int canvasWidth, |
6e888cf6a48b
renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
804
diff
changeset
|
83 unsigned int canvasHeight) |
597 | 84 { |
85 if (text_.GetWidth() != 0 && | |
86 text_.GetHeight() != 0) | |
87 { | |
88 const TextSceneLayer& layer = GetLayer<TextSceneLayer>(); | |
89 | |
90 cairo_t* cr = GetCairoContext(); | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
91 cairo_set_source_rgb(cr, layer.GetColor().GetRedAsFloat(), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
92 layer.GetColor().GetGreenAsFloat(), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
597
diff
changeset
|
93 layer.GetColor().GetBlueAsFloat()); |
597 | 94 |
95 double dx, dy; // In pixels | |
96 ComputeAnchorTranslation(dx, dy, layer.GetAnchor(), text_.GetWidth(), | |
97 text_.GetHeight(), layer.GetBorder()); | |
98 | |
99 double x = layer.GetX(); | |
100 double y = layer.GetY(); | |
101 transform.Apply(x, y); | |
102 | |
103 cairo_save(cr); | |
104 | |
105 cairo_matrix_t t; | |
106 cairo_matrix_init_identity(&t); | |
107 cairo_matrix_translate(&t, x + dx, y + dy); | |
108 cairo_transform(cr, &t); | |
109 | |
110 cairo_set_operator(cr, CAIRO_OPERATOR_OVER); | |
111 cairo_set_source_surface(cr, text_.GetObject(), 0, 0); | |
112 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR); | |
113 cairo_paint(cr); | |
114 | |
115 cairo_restore(cr); | |
116 } | |
117 } | |
118 } | |
119 } |