Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Fonts/GlyphTextureAlphabet.cpp @ 1808:797633f48a9c
display series description if hovering pdf or video
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 20 May 2021 17:28:16 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
rev | line source |
---|---|
577 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1624
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
577 | 6 * |
7 * 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
|
8 * modify it under the terms of the GNU Lesser General Public License |
577 | 9 * as published by the Free Software Foundation, either version 3 of |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * 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
|
14 * 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
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
577 | 20 **/ |
21 | |
22 | |
23 #include "GlyphTextureAlphabet.h" | |
24 | |
25 #include "TextBoundingBox.h" | |
26 #include "../Toolbox/DynamicBitmap.h" | |
27 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1300
diff
changeset
|
28 #include <Images/Image.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1300
diff
changeset
|
29 #include <Images/ImageProcessing.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1300
diff
changeset
|
30 #include <OrthancException.h> |
577 | 31 |
1242
b9b5d4378874
working of WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
958
diff
changeset
|
32 #if defined(__EMSCRIPTEN__) |
958
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
33 /* |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
34 Avoid this error: |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
35 .../boost/math/special_functions/round.hpp:86:12: warning: implicit conversion from 'std::__2::numeric_limits<int>::type' (aka 'int') to 'float' changes value from 2147483647 to 2147483648 [-Wimplicit-int-float-conversion] |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
36 .../boost/math/special_functions/round.hpp:93:11: note: in instantiation of function template specialization 'boost::math::iround<float, boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy> >' requested here |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
37 .../orthanc-stone/Framework/Fonts/GlyphTextureAlphabet.cpp:92:28: note: in instantiation of function template specialization 'boost::math::iround<float>' requested here |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
38 */ |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
39 #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion" |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
40 #endif |
769249e1f3b4
added guards to prevent calling methods on a dead controller (see comment in diff for expl.) + build fix
Benjamin Golinvaux <bgo@osimis.io>
parents:
693
diff
changeset
|
41 |
577 | 42 #include <boost/math/special_functions/round.hpp> |
43 | |
44 namespace OrthancStone | |
45 { | |
46 class GlyphTextureAlphabet::GlyphSizeVisitor : public GlyphAlphabet::IGlyphVisitor | |
47 { | |
48 private: | |
49 unsigned int maxWidth_; | |
50 unsigned int maxHeight_; | |
51 | |
52 public: | |
53 GlyphSizeVisitor() : | |
54 maxWidth_(0), | |
55 maxHeight_(0) | |
56 { | |
57 } | |
58 | |
59 virtual void Visit(uint32_t unicode, | |
1571 | 60 const Glyph& glyph) ORTHANC_OVERRIDE |
577 | 61 { |
62 maxWidth_ = std::max(maxWidth_, glyph.GetWidth()); | |
63 maxHeight_ = std::max(maxHeight_, glyph.GetHeight()); | |
64 } | |
65 | |
66 unsigned int GetMaxWidth() const | |
67 { | |
68 return maxWidth_; | |
69 } | |
70 | |
71 unsigned int GetMaxHeight() const | |
72 { | |
73 return maxHeight_; | |
74 } | |
75 }; | |
76 | |
77 | |
78 class GlyphTextureAlphabet::TextureGenerator : public GlyphAlphabet::IGlyphVisitor | |
79 { | |
80 private: | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
81 std::unique_ptr<Orthanc::ImageAccessor> texture_; |
577 | 82 |
83 unsigned int countColumns_; | |
84 unsigned int countRows_; | |
85 GlyphAlphabet& targetAlphabet_; | |
86 unsigned int glyphMaxWidth_; | |
87 unsigned int glyphMaxHeight_; | |
88 unsigned int column_; | |
89 unsigned int row_; | |
90 | |
91 public: | |
92 TextureGenerator(GlyphAlphabet& targetAlphabet, | |
93 unsigned int countGlyphs, | |
94 unsigned int glyphMaxWidth, | |
95 unsigned int glyphMaxHeight) : | |
96 targetAlphabet_(targetAlphabet), | |
97 glyphMaxWidth_(glyphMaxWidth), | |
98 glyphMaxHeight_(glyphMaxHeight), | |
99 column_(0), | |
100 row_(0) | |
101 { | |
693
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
577
diff
changeset
|
102 int c = boost::math::iround<float>(sqrt(static_cast<float>(countGlyphs))); |
577 | 103 |
104 if (c <= 0) | |
105 { | |
106 countColumns_ = 1; | |
107 } | |
108 else | |
109 { | |
110 countColumns_ = static_cast<unsigned int>(c); | |
111 } | |
112 | |
113 countRows_ = countGlyphs / countColumns_; | |
114 if (countGlyphs % countColumns_ != 0) | |
115 { | |
116 countRows_++; | |
117 } | |
118 | |
119 texture_.reset(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, | |
120 countColumns_ * glyphMaxWidth_, | |
121 countRows_ * glyphMaxHeight_, | |
122 true /* force minimal pitch */)); | |
123 | |
124 Orthanc::ImageProcessing::Set(*texture_, 0, 0, 0, 0); | |
125 } | |
126 | |
127 | |
128 virtual void Visit(uint32_t unicode, | |
1571 | 129 const Glyph& glyph) ORTHANC_OVERRIDE |
577 | 130 { |
131 if (!glyph.HasPayload()) | |
132 { | |
133 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
134 } | |
135 | |
136 if (column_ >= countColumns_ || | |
137 row_ >= countRows_) | |
138 { | |
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
140 } | |
141 | |
142 const Orthanc::ImageAccessor& source = dynamic_cast<const DynamicBitmap&>(glyph.GetPayload()).GetBitmap(); | |
143 | |
144 if (source.GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
145 { | |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
147 } | |
148 | |
1571 | 149 Orthanc::ImageAccessor target; |
577 | 150 |
1571 | 151 { |
152 unsigned int x = column_ * glyphMaxWidth_; | |
153 unsigned int y = row_ * glyphMaxHeight_; | |
154 targetAlphabet_.Register(unicode, glyph, new TextureLocation(x, y)); | |
155 texture_->GetRegion(target, x, y, source.GetWidth(), source.GetHeight()); | |
156 } | |
577 | 157 |
158 //Orthanc::ImageProcessing::Copy(target, bitmap->GetBitmap()); | |
159 | |
1624 | 160 const unsigned int width = source.GetWidth(); |
161 const unsigned int height = source.GetHeight(); | |
162 | |
163 for (unsigned int y = 0; y < height; y++) | |
577 | 164 { |
165 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | |
166 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
167 | |
1624 | 168 for (unsigned int x = 0; x < width; x++) |
577 | 169 { |
170 // Premultiplied alpha | |
171 q[0] = 0; | |
172 q[1] = 0; | |
173 q[2] = 0; | |
174 q[3] = *p; | |
175 | |
176 p++; | |
177 q += 4; | |
178 } | |
179 } | |
180 | |
181 column_++; | |
182 if (column_ == countColumns_) | |
183 { | |
184 column_ = 0; | |
185 row_++; | |
186 } | |
187 } | |
188 | |
189 | |
190 Orthanc::ImageAccessor* ReleaseTexture() | |
191 { | |
192 return texture_.release(); | |
193 } | |
194 }; | |
195 | |
196 | |
197 class GlyphTextureAlphabet::RenderTextVisitor : public GlyphAlphabet::ITextVisitor | |
198 { | |
199 private: | |
200 Orthanc::ImageAccessor& target_; | |
201 const Orthanc::ImageAccessor& texture_; | |
202 int offsetX_; | |
203 int offsetY_; | |
204 | |
205 public: | |
206 RenderTextVisitor(Orthanc::ImageAccessor& target, | |
207 const GlyphTextureAlphabet& that, | |
208 int offsetX, | |
209 int offsetY) : | |
210 target_(target), | |
211 texture_(that.GetTexture()), | |
212 offsetX_(offsetX), | |
213 offsetY_(offsetY) | |
214 { | |
215 } | |
216 | |
217 virtual void Visit(uint32_t unicode, | |
218 int x, | |
219 int y, | |
220 unsigned int width, | |
221 unsigned int height, | |
1571 | 222 const Orthanc::IDynamicObject* payload) ORTHANC_OVERRIDE |
577 | 223 { |
224 int left = x + offsetX_; | |
225 int top = y + offsetY_; | |
226 | |
227 assert(payload != NULL); | |
228 const TextureLocation& location = *dynamic_cast<const TextureLocation*>(payload); | |
229 | |
230 assert(left >= 0 && | |
231 top >= 0 && | |
232 static_cast<unsigned int>(left) + width <= target_.GetWidth() && | |
233 static_cast<unsigned int>(top) + height <= target_.GetHeight()); | |
234 | |
235 { | |
236 Orthanc::ImageAccessor to; | |
237 target_.GetRegion(to, left, top, width, height); | |
238 | |
239 Orthanc::ImageAccessor from; | |
240 texture_.GetRegion(from, location.GetX(), location.GetY(), width, height); | |
241 | |
242 Orthanc::ImageProcessing::Copy(to, from); | |
243 } | |
244 } | |
245 }; | |
246 | |
247 | |
248 GlyphTextureAlphabet::GlyphTextureAlphabet(const GlyphBitmapAlphabet& sourceAlphabet) : | |
249 textureWidth_(0), | |
250 textureHeight_(0) | |
251 { | |
252 GlyphSizeVisitor size; | |
253 sourceAlphabet.GetAlphabet().Apply(size); | |
254 | |
255 TextureGenerator generator(alphabet_, | |
693
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
577
diff
changeset
|
256 static_cast<unsigned int>(sourceAlphabet.GetAlphabet().GetSize()), |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
577
diff
changeset
|
257 size.GetMaxWidth(), |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
577
diff
changeset
|
258 size.GetMaxHeight()); |
577 | 259 sourceAlphabet.GetAlphabet().Apply(generator); |
260 | |
261 texture_.reset(generator.ReleaseTexture()); | |
262 textureWidth_ = texture_->GetWidth(); | |
263 textureHeight_ = texture_->GetHeight(); | |
264 } | |
265 | |
266 | |
267 const Orthanc::ImageAccessor& GlyphTextureAlphabet::GetTexture() const | |
268 { | |
269 if (texture_.get() == NULL) | |
270 { | |
271 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
272 } | |
273 else | |
274 { | |
275 return *texture_; | |
276 } | |
277 } | |
278 | |
279 | |
280 Orthanc::ImageAccessor* GlyphTextureAlphabet::ReleaseTexture() | |
281 { | |
282 if (texture_.get() == NULL) | |
283 { | |
284 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
285 } | |
286 else | |
287 { | |
288 return texture_.release(); | |
289 } | |
290 } | |
291 | |
292 | |
293 Orthanc::ImageAccessor* GlyphTextureAlphabet::RenderText(const std::string& utf8) | |
294 { | |
295 TextBoundingBox box(alphabet_, utf8); | |
296 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
297 std::unique_ptr<Orthanc::ImageAccessor> bitmap( |
577 | 298 new Orthanc::Image(Orthanc::PixelFormat_RGBA32, |
299 box.GetWidth(), box.GetHeight(), | |
300 true /* force minimal pitch */)); | |
301 | |
302 Orthanc::ImageProcessing::Set(*bitmap, 0, 0, 0, 0); | |
303 | |
304 RenderTextVisitor visitor(*bitmap, *this, -box.GetLeft(), -box.GetTop()); | |
305 alphabet_.Apply(visitor, utf8); | |
306 | |
307 return bitmap.release(); | |
308 } | |
309 } |