comparison Resources/Graveyard/Toolbox/DicomDataset.cpp @ 39:9ee7e2f5f1a3

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