Mercurial > hg > orthanc
comparison OrthancFramework/Sources/DicomFormat/DicomValue.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/DicomFormat/DicomValue.cpp@a9ce35d67c3c |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
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-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 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 "DicomValue.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../SerializationToolbox.h" | |
39 #include "../Toolbox.h" | |
40 | |
41 #include <boost/lexical_cast.hpp> | |
42 | |
43 namespace Orthanc | |
44 { | |
45 DicomValue::DicomValue(const DicomValue& other) : | |
46 type_(other.type_), | |
47 content_(other.content_) | |
48 { | |
49 } | |
50 | |
51 | |
52 DicomValue::DicomValue(const std::string& content, | |
53 bool isBinary) : | |
54 type_(isBinary ? Type_Binary : Type_String), | |
55 content_(content) | |
56 { | |
57 } | |
58 | |
59 | |
60 DicomValue::DicomValue(const char* data, | |
61 size_t size, | |
62 bool isBinary) : | |
63 type_(isBinary ? Type_Binary : Type_String) | |
64 { | |
65 content_.assign(data, size); | |
66 } | |
67 | |
68 | |
69 const std::string& DicomValue::GetContent() const | |
70 { | |
71 if (type_ == Type_Null) | |
72 { | |
73 throw OrthancException(ErrorCode_BadParameterType); | |
74 } | |
75 else | |
76 { | |
77 return content_; | |
78 } | |
79 } | |
80 | |
81 | |
82 DicomValue* DicomValue::Clone() const | |
83 { | |
84 return new DicomValue(*this); | |
85 } | |
86 | |
87 | |
88 #if ORTHANC_ENABLE_BASE64 == 1 | |
89 void DicomValue::FormatDataUriScheme(std::string& target, | |
90 const std::string& mime) const | |
91 { | |
92 Toolbox::EncodeBase64(target, GetContent()); | |
93 target.insert(0, "data:" + mime + ";base64,"); | |
94 } | |
95 #endif | |
96 | |
97 // same as ParseValue but in case the value actually contains a sequence, | |
98 // it will return the first value | |
99 // this has been introduced to support invalid "width/height" DICOM tags in some US | |
100 // images where the width is stored as "800\0" ! | |
101 template <typename T, | |
102 bool allowSigned> | |
103 static bool ParseFirstValue(T& result, | |
104 const DicomValue& source) | |
105 { | |
106 if (source.IsBinary() || | |
107 source.IsNull()) | |
108 { | |
109 return false; | |
110 } | |
111 | |
112 try | |
113 { | |
114 std::string value = Toolbox::StripSpaces(source.GetContent()); | |
115 if (value.empty()) | |
116 { | |
117 return false; | |
118 } | |
119 | |
120 if (!allowSigned && | |
121 value[0] == '-') | |
122 { | |
123 return false; | |
124 } | |
125 | |
126 if (value.find("\\") == std::string::npos) | |
127 { | |
128 result = boost::lexical_cast<T>(value); | |
129 return true; | |
130 } | |
131 else | |
132 { | |
133 std::vector<std::string> tokens; | |
134 Toolbox::TokenizeString(tokens, value, '\\'); | |
135 | |
136 if (tokens.size() >= 1) | |
137 { | |
138 result = boost::lexical_cast<T>(tokens[0]); | |
139 return true; | |
140 } | |
141 | |
142 return false; | |
143 } | |
144 } | |
145 catch (boost::bad_lexical_cast&) | |
146 { | |
147 return false; | |
148 } | |
149 } | |
150 | |
151 | |
152 template <typename T, | |
153 bool allowSigned> | |
154 static bool ParseValue(T& result, | |
155 const DicomValue& source) | |
156 { | |
157 if (source.IsBinary() || | |
158 source.IsNull()) | |
159 { | |
160 return false; | |
161 } | |
162 | |
163 try | |
164 { | |
165 std::string value = Toolbox::StripSpaces(source.GetContent()); | |
166 if (value.empty()) | |
167 { | |
168 return false; | |
169 } | |
170 | |
171 if (!allowSigned && | |
172 value[0] == '-') | |
173 { | |
174 return false; | |
175 } | |
176 | |
177 result = boost::lexical_cast<T>(value); | |
178 return true; | |
179 } | |
180 catch (boost::bad_lexical_cast&) | |
181 { | |
182 return false; | |
183 } | |
184 } | |
185 | |
186 bool DicomValue::ParseInteger32(int32_t& result) const | |
187 { | |
188 int64_t tmp; | |
189 if (ParseValue<int64_t, true>(tmp, *this)) | |
190 { | |
191 result = static_cast<int32_t>(tmp); | |
192 return (tmp == static_cast<int64_t>(result)); // Check no overflow occurs | |
193 } | |
194 else | |
195 { | |
196 return false; | |
197 } | |
198 } | |
199 | |
200 bool DicomValue::ParseInteger64(int64_t& result) const | |
201 { | |
202 return ParseValue<int64_t, true>(result, *this); | |
203 } | |
204 | |
205 bool DicomValue::ParseUnsignedInteger32(uint32_t& result) const | |
206 { | |
207 uint64_t tmp; | |
208 if (ParseValue<uint64_t, false>(tmp, *this)) | |
209 { | |
210 result = static_cast<uint32_t>(tmp); | |
211 return (tmp == static_cast<uint64_t>(result)); // Check no overflow occurs | |
212 } | |
213 else | |
214 { | |
215 return false; | |
216 } | |
217 } | |
218 | |
219 bool DicomValue::ParseUnsignedInteger64(uint64_t& result) const | |
220 { | |
221 return ParseValue<uint64_t, false>(result, *this); | |
222 } | |
223 | |
224 bool DicomValue::ParseFloat(float& result) const | |
225 { | |
226 return ParseValue<float, true>(result, *this); | |
227 } | |
228 | |
229 bool DicomValue::ParseDouble(double& result) const | |
230 { | |
231 return ParseValue<double, true>(result, *this); | |
232 } | |
233 | |
234 bool DicomValue::ParseFirstFloat(float& result) const | |
235 { | |
236 return ParseFirstValue<float, true>(result, *this); | |
237 } | |
238 | |
239 bool DicomValue::ParseFirstUnsignedInteger(unsigned int& result) const | |
240 { | |
241 return ParseFirstValue<unsigned int, true>(result, *this); | |
242 } | |
243 | |
244 bool DicomValue::CopyToString(std::string& result, | |
245 bool allowBinary) const | |
246 { | |
247 if (IsNull()) | |
248 { | |
249 return false; | |
250 } | |
251 else if (IsBinary() && !allowBinary) | |
252 { | |
253 return false; | |
254 } | |
255 else | |
256 { | |
257 result.assign(content_); | |
258 return true; | |
259 } | |
260 } | |
261 | |
262 | |
263 static const char* KEY_TYPE = "Type"; | |
264 static const char* KEY_CONTENT = "Content"; | |
265 | |
266 void DicomValue::Serialize(Json::Value& target) const | |
267 { | |
268 target = Json::objectValue; | |
269 | |
270 switch (type_) | |
271 { | |
272 case Type_Null: | |
273 target[KEY_TYPE] = "Null"; | |
274 break; | |
275 | |
276 case Type_String: | |
277 target[KEY_TYPE] = "String"; | |
278 target[KEY_CONTENT] = content_; | |
279 break; | |
280 | |
281 case Type_Binary: | |
282 { | |
283 target[KEY_TYPE] = "Binary"; | |
284 | |
285 std::string base64; | |
286 Toolbox::EncodeBase64(base64, content_); | |
287 target[KEY_CONTENT] = base64; | |
288 break; | |
289 } | |
290 | |
291 default: | |
292 throw OrthancException(ErrorCode_InternalError); | |
293 } | |
294 } | |
295 | |
296 void DicomValue::Unserialize(const Json::Value& source) | |
297 { | |
298 std::string type = SerializationToolbox::ReadString(source, KEY_TYPE); | |
299 | |
300 if (type == "Null") | |
301 { | |
302 type_ = Type_Null; | |
303 content_.clear(); | |
304 } | |
305 else if (type == "String") | |
306 { | |
307 type_ = Type_String; | |
308 content_ = SerializationToolbox::ReadString(source, KEY_CONTENT); | |
309 } | |
310 else if (type == "Binary") | |
311 { | |
312 type_ = Type_Binary; | |
313 | |
314 const std::string base64 =SerializationToolbox::ReadString(source, KEY_CONTENT); | |
315 Toolbox::DecodeBase64(content_, base64); | |
316 } | |
317 else | |
318 { | |
319 throw OrthancException(ErrorCode_BadFileFormat); | |
320 } | |
321 } | |
322 } |