annotate OrthancStone/Sources/Toolbox/OrthancDatasets/DicomDatasetReader.cpp @ 1515:b750b6eab453

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 20:52:35 +0200
parents 244ad1e4e76a
children 4fb8fdf03314
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1504
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
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 "DicomDatasetReader.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <OrthancException.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <Toolbox.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <boost/lexical_cast.hpp>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancStone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) :
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 dataset_(dataset)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 std::string DicomDatasetReader::GetStringValue(const DicomPath& path,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 const std::string& defaultValue) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 std::string s;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 if (dataset_.GetStringValue(s, path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 return s;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 return defaultValue;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 std::string s;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if (dataset_.GetStringValue(s, path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 return s;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentTag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 template <typename T>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 static bool GetValueInternal(T& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 const IDicomDataset& dataset,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const DicomPath& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 std::string s;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (dataset.GetStringValue(s, path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 target = boost::lexical_cast<T>(Orthanc::Toolbox::StripSpaces(s));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 catch (boost::bad_lexical_cast&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
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 bool DicomDatasetReader::GetIntegerValue(int& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 return GetValueInternal<int>(target, dataset_, path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 bool DicomDatasetReader::GetUnsignedIntegerValue(unsigned int& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 int value;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 if (!GetIntegerValue(value, path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 else if (value >= 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 target = static_cast<unsigned int>(value);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 bool DicomDatasetReader::GetFloatValue(float& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 return GetValueInternal<float>(target, dataset_, path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 bool DicomDatasetReader::GetDoubleValue(double& target,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 const DicomPath& path) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 return GetValueInternal<double>(target, dataset_, path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }