1504
|
1 /**
|
|
2 * Stone of Orthanc
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
6 *
|
|
7 * This program is free software: you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Affero General Public License
|
|
9 * as published by the Free Software Foundation, either version 3 of
|
|
10 * the License, or (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful, but
|
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 * Affero General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Affero General Public License
|
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 **/
|
|
20
|
|
21
|
|
22 #include "DicomDatasetReader.h"
|
|
23
|
|
24 #include <OrthancException.h>
|
|
25 #include <Toolbox.h>
|
|
26
|
|
27 #include <boost/lexical_cast.hpp>
|
|
28
|
|
29 namespace OrthancStone
|
|
30 {
|
|
31 DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) :
|
|
32 dataset_(dataset)
|
|
33 {
|
|
34 }
|
|
35
|
|
36
|
|
37 std::string DicomDatasetReader::GetStringValue(const DicomPath& path,
|
|
38 const std::string& defaultValue) const
|
|
39 {
|
|
40 std::string s;
|
|
41 if (dataset_.GetStringValue(s, path))
|
|
42 {
|
|
43 return s;
|
|
44 }
|
|
45 else
|
|
46 {
|
|
47 return defaultValue;
|
|
48 }
|
|
49 }
|
|
50
|
|
51
|
|
52 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const
|
|
53 {
|
|
54 std::string s;
|
|
55 if (dataset_.GetStringValue(s, path))
|
|
56 {
|
|
57 return s;
|
|
58 }
|
|
59 else
|
|
60 {
|
|
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentTag);
|
|
62 }
|
|
63 }
|
|
64
|
|
65
|
|
66 template <typename T>
|
|
67 static bool GetValueInternal(T& target,
|
|
68 const IDicomDataset& dataset,
|
|
69 const DicomPath& path)
|
|
70 {
|
|
71 try
|
|
72 {
|
|
73 std::string s;
|
|
74
|
|
75 if (dataset.GetStringValue(s, path))
|
|
76 {
|
|
77 target = boost::lexical_cast<T>(Orthanc::Toolbox::StripSpaces(s));
|
|
78 return true;
|
|
79 }
|
|
80 else
|
|
81 {
|
|
82 return false;
|
|
83 }
|
|
84 }
|
|
85 catch (boost::bad_lexical_cast&)
|
|
86 {
|
|
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
88 }
|
|
89 }
|
|
90
|
|
91
|
|
92 bool DicomDatasetReader::GetIntegerValue(int& target,
|
|
93 const DicomPath& path) const
|
|
94 {
|
|
95 return GetValueInternal<int>(target, dataset_, path);
|
|
96 }
|
|
97
|
|
98
|
|
99 bool DicomDatasetReader::GetUnsignedIntegerValue(unsigned int& target,
|
|
100 const DicomPath& path) const
|
|
101 {
|
|
102 int value;
|
|
103
|
|
104 if (!GetIntegerValue(value, path))
|
|
105 {
|
|
106 return false;
|
|
107 }
|
|
108 else if (value >= 0)
|
|
109 {
|
|
110 target = static_cast<unsigned int>(value);
|
|
111 return true;
|
|
112 }
|
|
113 else
|
|
114 {
|
|
115 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
|
|
116 }
|
|
117 }
|
|
118
|
|
119
|
|
120 bool DicomDatasetReader::GetFloatValue(float& target,
|
|
121 const DicomPath& path) const
|
|
122 {
|
|
123 return GetValueInternal<float>(target, dataset_, path);
|
|
124 }
|
|
125
|
|
126
|
|
127 bool DicomDatasetReader::GetDoubleValue(double& target,
|
|
128 const DicomPath& path) const
|
|
129 {
|
|
130 return GetValueInternal<double>(target, dataset_, path);
|
|
131 }
|
|
132 }
|