comparison OrthancStone/Sources/Fonts/GlyphTextureAlphabet.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Fonts/GlyphTextureAlphabet.cpp@30deba7bc8e2
children 85e117739eca
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "GlyphTextureAlphabet.h"
23
24 #include "TextBoundingBox.h"
25 #include "../Toolbox/DynamicBitmap.h"
26
27 #include <Images/Image.h>
28 #include <Images/ImageProcessing.h>
29 #include <OrthancException.h>
30
31 #if defined(__EMSCRIPTEN__)
32 /*
33 Avoid this error:
34 .../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]
35 .../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
36 .../orthanc-stone/Framework/Fonts/GlyphTextureAlphabet.cpp:92:28: note: in instantiation of function template specialization 'boost::math::iround<float>' requested here
37 */
38 #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion"
39 #endif
40
41 #include <boost/math/special_functions/round.hpp>
42
43 namespace OrthancStone
44 {
45 class GlyphTextureAlphabet::GlyphSizeVisitor : public GlyphAlphabet::IGlyphVisitor
46 {
47 private:
48 unsigned int maxWidth_;
49 unsigned int maxHeight_;
50
51 public:
52 GlyphSizeVisitor() :
53 maxWidth_(0),
54 maxHeight_(0)
55 {
56 }
57
58 virtual void Visit(uint32_t unicode,
59 const Glyph& glyph)
60 {
61 maxWidth_ = std::max(maxWidth_, glyph.GetWidth());
62 maxHeight_ = std::max(maxHeight_, glyph.GetHeight());
63 }
64
65 unsigned int GetMaxWidth() const
66 {
67 return maxWidth_;
68 }
69
70 unsigned int GetMaxHeight() const
71 {
72 return maxHeight_;
73 }
74 };
75
76
77 class GlyphTextureAlphabet::TextureGenerator : public GlyphAlphabet::IGlyphVisitor
78 {
79 private:
80 std::unique_ptr<Orthanc::ImageAccessor> texture_;
81
82 unsigned int countColumns_;
83 unsigned int countRows_;
84 GlyphAlphabet& targetAlphabet_;
85 unsigned int glyphMaxWidth_;
86 unsigned int glyphMaxHeight_;
87 unsigned int column_;
88 unsigned int row_;
89
90 public:
91 TextureGenerator(GlyphAlphabet& targetAlphabet,
92 unsigned int countGlyphs,
93 unsigned int glyphMaxWidth,
94 unsigned int glyphMaxHeight) :
95 targetAlphabet_(targetAlphabet),
96 glyphMaxWidth_(glyphMaxWidth),
97 glyphMaxHeight_(glyphMaxHeight),
98 column_(0),
99 row_(0)
100 {
101 int c = boost::math::iround<float>(sqrt(static_cast<float>(countGlyphs)));
102
103 if (c <= 0)
104 {
105 countColumns_ = 1;
106 }
107 else
108 {
109 countColumns_ = static_cast<unsigned int>(c);
110 }
111
112 countRows_ = countGlyphs / countColumns_;
113 if (countGlyphs % countColumns_ != 0)
114 {
115 countRows_++;
116 }
117
118 texture_.reset(new Orthanc::Image(Orthanc::PixelFormat_RGBA32,
119 countColumns_ * glyphMaxWidth_,
120 countRows_ * glyphMaxHeight_,
121 true /* force minimal pitch */));
122
123 Orthanc::ImageProcessing::Set(*texture_, 0, 0, 0, 0);
124 }
125
126
127 virtual void Visit(uint32_t unicode,
128 const Glyph& glyph)
129 {
130 if (!glyph.HasPayload())
131 {
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
133 }
134
135 if (column_ >= countColumns_ ||
136 row_ >= countRows_)
137 {
138 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
139 }
140
141 unsigned int x = column_ * glyphMaxWidth_;
142 unsigned int y = row_ * glyphMaxHeight_;
143
144 const Orthanc::ImageAccessor& source = dynamic_cast<const DynamicBitmap&>(glyph.GetPayload()).GetBitmap();
145
146 if (source.GetFormat() != Orthanc::PixelFormat_Grayscale8)
147 {
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
149 }
150
151 targetAlphabet_.Register(unicode, glyph, new TextureLocation(x, y));
152
153 Orthanc::ImageAccessor target;
154 texture_->GetRegion(target, x, y, source.GetWidth(), source.GetHeight());
155
156 //Orthanc::ImageProcessing::Copy(target, bitmap->GetBitmap());
157
158 for (unsigned int y = 0; y < source.GetHeight(); y++)
159 {
160 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
161 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
162
163 for (unsigned int x = 0; x < source.GetWidth(); x++)
164 {
165 // Premultiplied alpha
166 q[0] = 0;
167 q[1] = 0;
168 q[2] = 0;
169 q[3] = *p;
170
171 p++;
172 q += 4;
173 }
174 }
175
176 column_++;
177 if (column_ == countColumns_)
178 {
179 column_ = 0;
180 row_++;
181 }
182 }
183
184
185 Orthanc::ImageAccessor* ReleaseTexture()
186 {
187 return texture_.release();
188 }
189 };
190
191
192 class GlyphTextureAlphabet::RenderTextVisitor : public GlyphAlphabet::ITextVisitor
193 {
194 private:
195 Orthanc::ImageAccessor& target_;
196 const Orthanc::ImageAccessor& texture_;
197 int offsetX_;
198 int offsetY_;
199
200 public:
201 RenderTextVisitor(Orthanc::ImageAccessor& target,
202 const GlyphTextureAlphabet& that,
203 int offsetX,
204 int offsetY) :
205 target_(target),
206 texture_(that.GetTexture()),
207 offsetX_(offsetX),
208 offsetY_(offsetY)
209 {
210 }
211
212 virtual void Visit(uint32_t unicode,
213 int x,
214 int y,
215 unsigned int width,
216 unsigned int height,
217 const Orthanc::IDynamicObject* payload)
218 {
219 int left = x + offsetX_;
220 int top = y + offsetY_;
221
222 assert(payload != NULL);
223 const TextureLocation& location = *dynamic_cast<const TextureLocation*>(payload);
224
225 assert(left >= 0 &&
226 top >= 0 &&
227 static_cast<unsigned int>(left) + width <= target_.GetWidth() &&
228 static_cast<unsigned int>(top) + height <= target_.GetHeight());
229
230 {
231 Orthanc::ImageAccessor to;
232 target_.GetRegion(to, left, top, width, height);
233
234 Orthanc::ImageAccessor from;
235 texture_.GetRegion(from, location.GetX(), location.GetY(), width, height);
236
237 Orthanc::ImageProcessing::Copy(to, from);
238 }
239 }
240 };
241
242
243 GlyphTextureAlphabet::GlyphTextureAlphabet(const GlyphBitmapAlphabet& sourceAlphabet) :
244 textureWidth_(0),
245 textureHeight_(0)
246 {
247 GlyphSizeVisitor size;
248 sourceAlphabet.GetAlphabet().Apply(size);
249
250 TextureGenerator generator(alphabet_,
251 static_cast<unsigned int>(sourceAlphabet.GetAlphabet().GetSize()),
252 size.GetMaxWidth(),
253 size.GetMaxHeight());
254 sourceAlphabet.GetAlphabet().Apply(generator);
255
256 texture_.reset(generator.ReleaseTexture());
257 textureWidth_ = texture_->GetWidth();
258 textureHeight_ = texture_->GetHeight();
259 }
260
261
262 const Orthanc::ImageAccessor& GlyphTextureAlphabet::GetTexture() const
263 {
264 if (texture_.get() == NULL)
265 {
266 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
267 }
268 else
269 {
270 return *texture_;
271 }
272 }
273
274
275 Orthanc::ImageAccessor* GlyphTextureAlphabet::ReleaseTexture()
276 {
277 if (texture_.get() == NULL)
278 {
279 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
280 }
281 else
282 {
283 return texture_.release();
284 }
285 }
286
287
288 Orthanc::ImageAccessor* GlyphTextureAlphabet::RenderText(const std::string& utf8)
289 {
290 TextBoundingBox box(alphabet_, utf8);
291
292 std::unique_ptr<Orthanc::ImageAccessor> bitmap(
293 new Orthanc::Image(Orthanc::PixelFormat_RGBA32,
294 box.GetWidth(), box.GetHeight(),
295 true /* force minimal pitch */));
296
297 Orthanc::ImageProcessing::Set(*bitmap, 0, 0, 0, 0);
298
299 RenderTextVisitor visitor(*bitmap, *this, -box.GetLeft(), -box.GetTop());
300 alphabet_.Apply(visitor, utf8);
301
302 return bitmap.release();
303 }
304 }