annotate Applications/Resources/Graveyard/Toolbox/DicomDataset.cpp @ 1630:78509230f0d7

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