Mercurial > hg > orthanc-stone
annotate Applications/Resources/Graveyard/Toolbox/DicomDataset.cpp @ 2039:e0906b7c67b9 deep-learning
const
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 28 Jan 2023 15:48:01 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
rev | line source |
---|---|
39 | 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 |
39 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
47 | 9 * modify it under the terms of the GNU Affero General Public License |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
39 | 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 | |
47 | 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 * Affero General Public License for more details. | |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1586
diff
changeset
|
17 * |
47 | 18 * You should have received a copy of the GNU Affero General Public License |
39 | 19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 **/ | |
21 | |
22 | |
23 #include "DicomDataset.h" | |
24 | |
25 #include "../../Resources/Orthanc/Core/OrthancException.h" | |
26 #include "../../Resources/Orthanc/Core/Logging.h" | |
27 #include "../../Resources/Orthanc/Core/Toolbox.h" | |
28 | |
29 #include <boost/lexical_cast.hpp> | |
30 #include <json/value.h> | |
31 #include <json/reader.h> | |
32 | |
33 namespace OrthancStone | |
34 { | |
35 static uint16_t GetCharValue(char c) | |
36 { | |
37 if (c >= '0' && c <= '9') | |
38 return c - '0'; | |
39 else if (c >= 'a' && c <= 'f') | |
40 return c - 'a' + 10; | |
41 else if (c >= 'A' && c <= 'F') | |
42 return c - 'A' + 10; | |
43 else | |
44 return 0; | |
45 } | |
46 | |
47 | |
48 static uint16_t GetHexadecimalValue(const char* c) | |
49 { | |
50 return ((GetCharValue(c[0]) << 12) + | |
51 (GetCharValue(c[1]) << 8) + | |
52 (GetCharValue(c[2]) << 4) + | |
53 GetCharValue(c[3])); | |
54 } | |
55 | |
56 | |
57 static DicomDataset::Tag ParseTag(const std::string& tag) | |
58 { | |
59 if (tag.size() == 9 && | |
60 isxdigit(tag[0]) && | |
61 isxdigit(tag[1]) && | |
62 isxdigit(tag[2]) && | |
63 isxdigit(tag[3]) && | |
64 (tag[4] == '-' || tag[4] == ',') && | |
65 isxdigit(tag[5]) && | |
66 isxdigit(tag[6]) && | |
67 isxdigit(tag[7]) && | |
68 isxdigit(tag[8])) | |
69 { | |
70 uint16_t group = GetHexadecimalValue(tag.c_str()); | |
71 uint16_t element = GetHexadecimalValue(tag.c_str() + 5); | |
72 return std::make_pair(group, element); | |
73 } | |
74 else if (tag.size() == 8 && | |
75 isxdigit(tag[0]) && | |
76 isxdigit(tag[1]) && | |
77 isxdigit(tag[2]) && | |
78 isxdigit(tag[3]) && | |
79 isxdigit(tag[4]) && | |
80 isxdigit(tag[5]) && | |
81 isxdigit(tag[6]) && | |
82 isxdigit(tag[7])) | |
83 { | |
84 uint16_t group = GetHexadecimalValue(tag.c_str()); | |
85 uint16_t element = GetHexadecimalValue(tag.c_str() + 4); | |
86 return std::make_pair(group, element); | |
87 } | |
88 else | |
89 { | |
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
91 } | |
92 } | |
93 | |
94 void DicomDataset::Parse(const std::string& content) | |
95 { | |
96 Json::Value json; | |
97 Json::Reader reader; | |
98 if (!reader.parse(content, json)) | |
99 { | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
101 } | |
102 | |
103 Parse(json); | |
104 } | |
105 | |
106 | |
107 void DicomDataset::Parse(const Json::Value& content) | |
108 { | |
109 if (content.type() != Json::objectValue) | |
110 { | |
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
112 } | |
113 | |
114 Json::Value::Members members = content.getMemberNames(); | |
115 for (size_t i = 0; i < members.size(); i++) | |
116 { | |
117 Tag tag = ParseTag(members[i]); | |
118 | |
119 const Json::Value& item = content[members[i]]; | |
120 | |
121 if (item.type() != Json::objectValue || | |
122 !item.isMember("Type") || | |
123 !item.isMember("Value") || | |
124 !item.isMember("Name") || | |
125 item["Type"].type() != Json::stringValue || | |
126 item["Name"].type() != Json::stringValue) | |
127 { | |
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
129 } | |
130 | |
131 if (item["Type"].asString() == "String") | |
132 { | |
133 if (item["Value"].type() == Json::stringValue) | |
134 { | |
135 values_[tag] = item["Value"].asString(); | |
136 } | |
137 else | |
138 { | |
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
140 } | |
141 } | |
142 } | |
143 } | |
144 | |
145 | |
146 DicomDataset::DicomDataset(OrthancPlugins::IOrthancConnection& orthanc, | |
147 const std::string& instanceId) | |
148 { | |
149 std::string content; | |
150 orthanc.RestApiGet(content, "/instances/" + instanceId + "/tags"); | |
151 | |
152 Parse(content); | |
153 } | |
154 | |
155 | |
156 std::string DicomDataset::GetStringValue(const Tag& tag) const | |
157 { | |
158 Values::const_iterator it = values_.find(tag); | |
159 | |
160 if (it == values_.end()) | |
161 { | |
162 LOG(ERROR) << "Trying to access a DICOM tag that is not set in a DICOM dataset"; | |
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); | |
164 } | |
165 else | |
166 { | |
167 return it->second; | |
168 } | |
169 } | |
170 | |
171 | |
172 std::string DicomDataset::GetStringValue(const Tag& tag, | |
173 const std::string& defaultValue) const | |
174 { | |
175 Values::const_iterator it = values_.find(tag); | |
176 | |
177 if (it == values_.end()) | |
178 { | |
179 return defaultValue; | |
180 } | |
181 else | |
182 { | |
183 return it->second; | |
184 } | |
185 } | |
186 | |
187 | |
188 float DicomDataset::GetFloatValue(const Tag& tag) const | |
189 { | |
190 try | |
191 { | |
192 return boost::lexical_cast<float>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag))); | |
193 } | |
194 catch (boost::bad_lexical_cast&) | |
195 { | |
196 LOG(ERROR) << "Trying to access a DICOM tag that is not a float"; | |
197 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
198 } | |
199 } | |
200 | |
201 | |
202 double DicomDataset::GetDoubleValue(const Tag& tag) const | |
203 { | |
204 try | |
205 { | |
206 return boost::lexical_cast<double>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag))); | |
207 } | |
208 catch (boost::bad_lexical_cast&) | |
209 { | |
210 LOG(ERROR) << "Trying to access a DICOM tag that is not a float"; | |
211 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
212 } | |
213 } | |
214 | |
215 | |
216 int DicomDataset::GetIntegerValue(const Tag& tag) const | |
217 { | |
218 try | |
219 { | |
220 return boost::lexical_cast<int>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag))); | |
221 } | |
222 catch (boost::bad_lexical_cast&) | |
223 { | |
224 LOG(ERROR) << "Trying to access a DICOM tag that is not an integer"; | |
225 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
226 } | |
227 } | |
228 | |
229 | |
230 unsigned int DicomDataset::GetUnsignedIntegerValue(const Tag& tag) const | |
231 { | |
232 int v = GetIntegerValue(tag); | |
233 | |
234 if (v < 0) | |
235 { | |
236 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
237 } | |
238 else | |
239 { | |
240 return static_cast<unsigned int>(v); | |
241 } | |
242 } | |
243 | |
244 | |
245 void DicomDataset::GetVectorValue(Vector& vector, | |
246 const Tag& tag) const | |
247 { | |
248 if (!GeometryToolbox::ParseVector(vector, Orthanc::Toolbox::StripSpaces(GetStringValue(tag)))) | |
249 { | |
250 LOG(ERROR) << "Trying to access a DICOM tag that is not a vector"; | |
251 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
252 } | |
253 } | |
254 | |
255 | |
256 void DicomDataset::GetVectorValue(Vector& vector, | |
257 const Tag& tag, | |
258 size_t expectedSize) const | |
259 { | |
260 GetVectorValue(vector, tag); | |
261 | |
262 if (vector.size() != expectedSize) | |
263 { | |
264 LOG(ERROR) << "A vector in a DICOM tag has a bad size"; | |
265 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
266 } | |
267 } | |
268 | |
269 | |
270 void DicomDataset::Print() const | |
271 { | |
272 for (Values::const_iterator it = values_.begin(); it != values_.end(); ++it) | |
273 { | |
274 printf("%04x,%04x = [%s]\n", it->first.first, it->first.second, it->second.c_str()); | |
275 } | |
276 printf("\n"); | |
277 } | |
278 | |
279 | |
280 bool DicomDataset::IsGrayscale() const | |
281 { | |
282 std::string photometric = Orthanc::Toolbox::StripSpaces(GetStringValue(DICOM_TAG_PHOTOMETRIC_INTERPRETATION)); | |
283 | |
284 return (photometric == "MONOCHROME1" || | |
285 photometric == "MONOCHROME2"); | |
286 } | |
287 | |
288 | |
289 void DicomDataset::GetPixelSpacing(double& spacingX, | |
290 double& spacingY) const | |
291 { | |
292 if (HasTag(DICOM_TAG_PIXEL_SPACING)) | |
293 { | |
294 Vector spacing; | |
295 GetVectorValue(spacing, DICOM_TAG_PIXEL_SPACING, 2); | |
296 spacingX = spacing[0]; | |
297 spacingY = spacing[1]; | |
298 } | |
299 else | |
300 { | |
301 spacingX = 1.0; | |
302 spacingY = 1.0; | |
303 } | |
304 } | |
305 } |