comparison Resources/Orthanc/Core/Images/Font.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "Font.h"
36
37 #if !defined(ORTHANC_ENABLE_LOCALE)
38 # error ORTHANC_ENABLE_LOCALE must be defined to use this file
39 #endif
40
41 #if ORTHANC_SANDBOXED == 0
42 # include "../SystemToolbox.h"
43 #endif
44
45 #include "../Toolbox.h"
46 #include "../OrthancException.h"
47
48 #include <stdio.h>
49 #include <memory>
50 #include <boost/lexical_cast.hpp>
51
52 namespace Orthanc
53 {
54 Font::~Font()
55 {
56 for (Characters::iterator it = characters_.begin();
57 it != characters_.end(); ++it)
58 {
59 delete it->second;
60 }
61 }
62
63
64 void Font::LoadFromMemory(const std::string& font)
65 {
66 Json::Value v;
67 Json::Reader reader;
68 if (!reader.parse(font, v) ||
69 v.type() != Json::objectValue ||
70 !v.isMember("Name") ||
71 !v.isMember("Size") ||
72 !v.isMember("Characters") ||
73 v["Name"].type() != Json::stringValue ||
74 v["Size"].type() != Json::intValue ||
75 v["Characters"].type() != Json::objectValue)
76 {
77 throw OrthancException(ErrorCode_BadFont);
78 }
79
80 name_ = v["Name"].asString();
81 size_ = v["Size"].asUInt();
82 maxHeight_ = 0;
83
84 Json::Value::Members characters = v["Characters"].getMemberNames();
85
86 for (size_t i = 0; i < characters.size(); i++)
87 {
88 const Json::Value& info = v["Characters"][characters[i]];
89 if (info.type() != Json::objectValue ||
90 !info.isMember("Advance") ||
91 !info.isMember("Bitmap") ||
92 !info.isMember("Height") ||
93 !info.isMember("Top") ||
94 !info.isMember("Width") ||
95 info["Advance"].type() != Json::intValue ||
96 info["Bitmap"].type() != Json::arrayValue ||
97 info["Height"].type() != Json::intValue ||
98 info["Top"].type() != Json::intValue ||
99 info["Width"].type() != Json::intValue)
100 {
101 throw OrthancException(ErrorCode_BadFont);
102 }
103
104 std::auto_ptr<Character> c(new Character);
105
106 c->advance_ = info["Advance"].asUInt();
107 c->height_ = info["Height"].asUInt();
108 c->top_ = info["Top"].asUInt();
109 c->width_ = info["Width"].asUInt();
110 c->bitmap_.resize(info["Bitmap"].size());
111
112 if (c->height_ > maxHeight_)
113 {
114 maxHeight_ = c->height_;
115 }
116
117 for (Json::Value::ArrayIndex j = 0; j < info["Bitmap"].size(); j++)
118 {
119 if (info["Bitmap"][j].type() != Json::intValue)
120 {
121 throw OrthancException(ErrorCode_BadFont);
122 }
123
124 int value = info["Bitmap"][j].asInt();
125 if (value < 0 || value > 255)
126 {
127 throw OrthancException(ErrorCode_BadFont);
128 }
129
130 c->bitmap_[j] = static_cast<uint8_t>(value);
131 }
132
133 int index = boost::lexical_cast<int>(characters[i]);
134 if (index < 0 || index > 255)
135 {
136 throw OrthancException(ErrorCode_BadFont);
137 }
138
139 characters_[static_cast<char>(index)] = c.release();
140 }
141 }
142
143
144 #if ORTHANC_SANDBOXED == 0
145 void Font::LoadFromFile(const std::string& path)
146 {
147 std::string font;
148 SystemToolbox::ReadFile(font, path);
149 LoadFromMemory(font);
150 }
151 #endif
152
153
154 static unsigned int MyMin(unsigned int a,
155 unsigned int b)
156 {
157 return a < b ? a : b;
158 }
159
160
161 void Font::DrawCharacter(ImageAccessor& target,
162 const Character& character,
163 int x,
164 int y,
165 const uint8_t color[4]) const
166 {
167 // Compute the bounds of the character
168 if (x >= static_cast<int>(target.GetWidth()) ||
169 y >= static_cast<int>(target.GetHeight()))
170 {
171 // The character is out of the image
172 return;
173 }
174
175 unsigned int left = x < 0 ? -x : 0;
176 unsigned int top = y < 0 ? -y : 0;
177 unsigned int width = MyMin(character.width_, target.GetWidth() - x);
178 unsigned int height = MyMin(character.height_, target.GetHeight() - y);
179
180 unsigned int bpp = target.GetBytesPerPixel();
181
182 // Blit the font bitmap OVER the target image
183 // https://en.wikipedia.org/wiki/Alpha_compositing
184
185 for (unsigned int cy = top; cy < height; cy++)
186 {
187 uint8_t* p = reinterpret_cast<uint8_t*>(target.GetRow(y + cy)) + (x + left) * bpp;
188 unsigned int pos = cy * character.width_ + left;
189
190 switch (target.GetFormat())
191 {
192 case PixelFormat_Grayscale8:
193 {
194 assert(bpp == 1);
195 for (unsigned int cx = left; cx < width; cx++, pos++, p++)
196 {
197 uint16_t alpha = character.bitmap_[pos];
198 uint16_t value = alpha * static_cast<uint16_t>(color[0]) + (255 - alpha) * static_cast<uint16_t>(*p);
199 *p = static_cast<uint8_t>(value >> 8);
200 }
201
202 break;
203 }
204
205 case PixelFormat_RGB24:
206 {
207 assert(bpp == 3);
208 for (unsigned int cx = left; cx < width; cx++, pos++, p += 3)
209 {
210 uint16_t alpha = character.bitmap_[pos];
211 for (uint8_t i = 0; i < 3; i++)
212 {
213 uint16_t value = alpha * static_cast<uint16_t>(color[i]) + (255 - alpha) * static_cast<uint16_t>(p[i]);
214 p[i] = static_cast<uint8_t>(value >> 8);
215 }
216 }
217
218 break;
219 }
220
221 case PixelFormat_RGBA32:
222 {
223 assert(bpp == 4);
224
225 for (unsigned int cx = left; cx < width; cx++, pos++, p += 4)
226 {
227 float alpha = static_cast<float>(character.bitmap_[pos]) / 255.0f;
228 float beta = (1.0f - alpha) * static_cast<float>(p[3]) / 255.0f;
229 float denom = 1.0f / (alpha + beta);
230
231 for (uint8_t i = 0; i < 3; i++)
232 {
233 p[i] = static_cast<uint8_t>((alpha * static_cast<float>(color[i]) +
234 beta * static_cast<float>(p[i])) * denom);
235 }
236
237 p[3] = static_cast<uint8_t>(255.0f * (alpha + beta));
238 }
239
240 break;
241 }
242
243 default:
244 throw OrthancException(ErrorCode_NotImplemented);
245 }
246 }
247
248 }
249
250
251 void Font::DrawInternal(ImageAccessor& target,
252 const std::string& utf8,
253 int x,
254 int y,
255 const uint8_t color[4]) const
256 {
257 if (target.GetFormat() != PixelFormat_Grayscale8 &&
258 target.GetFormat() != PixelFormat_RGB24 &&
259 target.GetFormat() != PixelFormat_RGBA32)
260 {
261 throw OrthancException(ErrorCode_NotImplemented);
262 }
263
264 int a = x;
265
266 #if ORTHANC_ENABLE_LOCALE == 1
267 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1);
268 #else
269 // If the locale support is disabled, simply drop non-ASCII
270 // characters from the source UTF-8 string
271 std::string s = Toolbox::ConvertToAscii(utf8);
272 #endif
273
274 for (size_t i = 0; i < s.size(); i++)
275 {
276 if (s[i] == '\n')
277 {
278 // Go to the next line
279 a = x;
280 y += maxHeight_ + 1;
281 }
282 else
283 {
284 Characters::const_iterator c = characters_.find(s[i]);
285 if (c != characters_.end())
286 {
287 DrawCharacter(target, *c->second, a, y + static_cast<int>(c->second->top_), color);
288 a += c->second->advance_;
289 }
290 }
291 }
292 }
293
294
295 void Font::Draw(ImageAccessor& target,
296 const std::string& utf8,
297 int x,
298 int y,
299 uint8_t grayscale) const
300 {
301 uint8_t color[4] = { grayscale, grayscale, grayscale, 255 };
302 DrawInternal(target, utf8, x, y, color);
303 }
304
305
306 void Font::Draw(ImageAccessor& target,
307 const std::string& utf8,
308 int x,
309 int y,
310 uint8_t r,
311 uint8_t g,
312 uint8_t b) const
313 {
314 uint8_t color[4] = { r, g, b, 255 };
315 DrawInternal(target, utf8, x, y, color);
316 }
317
318 }