annotate Applications/Resources/Graveyard/Toolbox/DicomDataset.cpp @ 2048:a05b03f45289 deep-learning

added ORTHANC_WEBGL2_HEAP_COMPAT
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 08 Mar 2023 17:46:04 +0100
parents 7053b8a0aaec
children 07964689cb0b
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
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * 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
9 * 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
10 * as published by the Free Software Foundation, either version 3 of
28956ed68280 agpl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
11 * the License, or (at your option) any later version.
39
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
47
28956ed68280 agpl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28956ed68280 agpl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
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
28956ed68280 agpl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
18 * You should have received a copy of the GNU Affero General Public License
39
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "DicomDataset.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "../../Resources/Orthanc/Core/OrthancException.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../../Resources/Orthanc/Core/Logging.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "../../Resources/Orthanc/Core/Toolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <boost/lexical_cast.hpp>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <json/value.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <json/reader.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace OrthancStone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 static uint16_t GetCharValue(char c)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 if (c >= '0' && c <= '9')
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 return c - '0';
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 else if (c >= 'a' && c <= 'f')
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 return c - 'a' + 10;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 else if (c >= 'A' && c <= 'F')
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 return c - 'A' + 10;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 return 0;
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 static uint16_t GetHexadecimalValue(const char* c)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 return ((GetCharValue(c[0]) << 12) +
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 (GetCharValue(c[1]) << 8) +
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 (GetCharValue(c[2]) << 4) +
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 GetCharValue(c[3]));
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 static DicomDataset::Tag ParseTag(const std::string& tag)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 if (tag.size() == 9 &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 isxdigit(tag[0]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 isxdigit(tag[1]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 isxdigit(tag[2]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 isxdigit(tag[3]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 (tag[4] == '-' || tag[4] == ',') &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 isxdigit(tag[5]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 isxdigit(tag[6]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 isxdigit(tag[7]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 isxdigit(tag[8]))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 uint16_t group = GetHexadecimalValue(tag.c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 uint16_t element = GetHexadecimalValue(tag.c_str() + 5);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 return std::make_pair(group, element);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 else if (tag.size() == 8 &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 isxdigit(tag[0]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 isxdigit(tag[1]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 isxdigit(tag[2]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 isxdigit(tag[3]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 isxdigit(tag[4]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 isxdigit(tag[5]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 isxdigit(tag[6]) &&
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 isxdigit(tag[7]))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 uint16_t group = GetHexadecimalValue(tag.c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 uint16_t element = GetHexadecimalValue(tag.c_str() + 4);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 return std::make_pair(group, element);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 void DicomDataset::Parse(const std::string& content)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 Json::Value json;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 Json::Reader reader;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 if (!reader.parse(content, json))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 Parse(json);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 void DicomDataset::Parse(const Json::Value& content)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if (content.type() != Json::objectValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 Json::Value::Members members = content.getMemberNames();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 for (size_t i = 0; i < members.size(); i++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 Tag tag = ParseTag(members[i]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 const Json::Value& item = content[members[i]];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 if (item.type() != Json::objectValue ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 !item.isMember("Type") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 !item.isMember("Value") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 !item.isMember("Name") ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 item["Type"].type() != Json::stringValue ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 item["Name"].type() != Json::stringValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 if (item["Type"].asString() == "String")
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 if (item["Value"].type() == Json::stringValue)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 values_[tag] = item["Value"].asString();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 DicomDataset::DicomDataset(OrthancPlugins::IOrthancConnection& orthanc,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 const std::string& instanceId)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 std::string content;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 orthanc.RestApiGet(content, "/instances/" + instanceId + "/tags");
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 Parse(content);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 std::string DicomDataset::GetStringValue(const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 Values::const_iterator it = values_.find(tag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 if (it == values_.end())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 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
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 return it->second;
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 std::string DicomDataset::GetStringValue(const Tag& tag,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 const std::string& defaultValue) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 Values::const_iterator it = values_.find(tag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if (it == values_.end())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 return defaultValue;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return it->second;
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 float DicomDataset::GetFloatValue(const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 return boost::lexical_cast<float>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag)));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 catch (boost::bad_lexical_cast&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 LOG(ERROR) << "Trying to access a DICOM tag that is not a float";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 double DicomDataset::GetDoubleValue(const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 return boost::lexical_cast<double>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag)));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 catch (boost::bad_lexical_cast&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 LOG(ERROR) << "Trying to access a DICOM tag that is not a float";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 int DicomDataset::GetIntegerValue(const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return boost::lexical_cast<int>(Orthanc::Toolbox::StripSpaces(GetStringValue(tag)));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 catch (boost::bad_lexical_cast&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 LOG(ERROR) << "Trying to access a DICOM tag that is not an integer";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 unsigned int DicomDataset::GetUnsignedIntegerValue(const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 int v = GetIntegerValue(tag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 if (v < 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 return static_cast<unsigned int>(v);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 void DicomDataset::GetVectorValue(Vector& vector,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 const Tag& tag) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 if (!GeometryToolbox::ParseVector(vector, Orthanc::Toolbox::StripSpaces(GetStringValue(tag))))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 LOG(ERROR) << "Trying to access a DICOM tag that is not a vector";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 void DicomDataset::GetVectorValue(Vector& vector,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 const Tag& tag,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 size_t expectedSize) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 GetVectorValue(vector, tag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 if (vector.size() != expectedSize)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 LOG(ERROR) << "A vector in a DICOM tag has a bad size";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 void DicomDataset::Print() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 for (Values::const_iterator it = values_.begin(); it != values_.end(); ++it)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 printf("%04x,%04x = [%s]\n", it->first.first, it->first.second, it->second.c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 printf("\n");
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 bool DicomDataset::IsGrayscale() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 std::string photometric = Orthanc::Toolbox::StripSpaces(GetStringValue(DICOM_TAG_PHOTOMETRIC_INTERPRETATION));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 return (photometric == "MONOCHROME1" ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 photometric == "MONOCHROME2");
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 void DicomDataset::GetPixelSpacing(double& spacingX,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 double& spacingY) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 if (HasTag(DICOM_TAG_PIXEL_SPACING))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 Vector spacing;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 GetVectorValue(spacing, DICOM_TAG_PIXEL_SPACING, 2);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 spacingX = spacing[0];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 spacingY = spacing[1];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 spacingX = 1.0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 spacingY = 1.0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 }