comparison OrthancStone/Sources/Toolbox/OrthancDatasets/OrthancNativeDataset.cpp @ 2174:2410a171ebfb

refactoring using DicomWebDataset and OrthancNativeDataset
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Oct 2024 21:52:34 +0200
parents
children
comparison
equal deleted inserted replaced
2172:239fb2c893c1 2174:2410a171ebfb
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-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 **/
22
23
24 #include "OrthancNativeDataset.h"
25
26 #include <OrthancException.h>
27
28
29 static const char* const NAME = "Name";
30 static const char* const TYPE = "Type";
31 static const char* const VALUE = "Value";
32
33
34 namespace OrthancStone
35 {
36 const Json::Value* OrthancNativeDataset::LookupValue(std::string& type,
37 const Orthanc::DicomPath& path) const
38 {
39 if (path.IsPrefixUniversal(0))
40 {
41 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
42 }
43
44 const Orthanc::DicomValue* rootSequence = dicom_->TestAndGetValue(path.GetPrefixTag(0));
45 if (rootSequence == NULL ||
46 !rootSequence->IsSequence())
47 {
48 return NULL;
49 }
50
51 Json::ArrayIndex index = path.GetPrefixIndex(0);
52
53 if (rootSequence->GetSequenceContent().type() != Json::arrayValue)
54 {
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
56 }
57
58 if (index >= rootSequence->GetSequenceContent().size())
59 {
60 return NULL;
61 }
62
63 const Json::Value* current = &(rootSequence->GetSequenceContent() [index]);
64
65 for (size_t i = 1; i < path.GetPrefixLength(); i++)
66 {
67 if (path.IsPrefixUniversal(i))
68 {
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
70 }
71
72 index = path.GetPrefixIndex(i);
73 std::string tag = path.GetPrefixTag(i).Format();
74
75 if (current->type() != Json::objectValue)
76 {
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
78 }
79
80 if (!current->isMember(tag))
81 {
82 return NULL;
83 }
84
85 if ((*current) [tag].type() != Json::objectValue ||
86 !(*current) [tag].isMember(NAME) ||
87 !(*current) [tag].isMember(TYPE) ||
88 !(*current) [tag].isMember(VALUE) ||
89 (*current) [tag][NAME].type() != Json::stringValue ||
90 (*current) [tag][TYPE].type() != Json::stringValue)
91 {
92 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
93 }
94
95 if ((*current) [tag][TYPE].asString() != "Sequence" ||
96 (*current) [tag][VALUE].type() != Json::arrayValue ||
97 index >= (*current) [tag][VALUE].size())
98 {
99 return NULL;
100 }
101
102 current = &(*current) [tag][VALUE][index];
103 }
104
105 std::string tag = path.GetFinalTag().Format();
106
107 if (current->type() != Json::objectValue)
108 {
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
110 }
111
112 if (!current->isMember(tag))
113 {
114 return NULL;
115 }
116
117 if ((*current) [tag].type() != Json::objectValue ||
118 !(*current) [tag].isMember(NAME) ||
119 !(*current) [tag].isMember(TYPE) ||
120 !(*current) [tag].isMember(VALUE) ||
121 (*current) [tag][NAME].type() != Json::stringValue ||
122 (*current) [tag][TYPE].type() != Json::stringValue)
123 {
124 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
125 }
126 else
127 {
128 type = (*current) [tag][TYPE].asString();
129 return &((*current) [tag][VALUE]);
130 }
131 }
132
133
134 OrthancNativeDataset::OrthancNativeDataset(const Json::Value& dicom) :
135 dicom_(new Orthanc::DicomMap)
136 {
137 dicom_->FromDicomAsJson(dicom, false, true /* parse sequences */);
138 }
139
140
141 bool OrthancNativeDataset::GetStringValue(std::string& result,
142 const Orthanc::DicomPath& path) const
143 {
144 if (path.GetPrefixLength() == 0)
145 {
146 return dicom_->LookupStringValue(result, path.GetFinalTag(), false);
147 }
148 else
149 {
150 std::string type;
151 const Json::Value* value = LookupValue(type, path);
152
153 if (value == NULL)
154 {
155 return false;
156 }
157 else if (type == "String" &&
158 value->type() == Json::stringValue)
159 {
160 result = value->asString();
161 return true;
162 }
163 else
164 {
165 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
166 }
167 }
168 }
169
170
171 bool OrthancNativeDataset::GetSequenceSize(size_t& size,
172 const Orthanc::DicomPath& path) const
173 {
174 if (path.GetPrefixLength() == 0)
175 {
176 const Orthanc::DicomValue* value = dicom_->TestAndGetValue(path.GetFinalTag());
177 if (value == NULL ||
178 !value->IsSequence())
179 {
180 return false;
181 }
182 else if (value->GetSequenceContent().type() != Json::arrayValue)
183 {
184 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
185 }
186 else
187 {
188 size = value->GetSequenceContent().size();
189 return true;
190 }
191 }
192 else
193 {
194 std::string type;
195 const Json::Value* value = LookupValue(type, path);
196
197 if (value == NULL ||
198 type != "Sequence")
199 {
200 return false;
201 }
202 else if (value->type() != Json::arrayValue)
203 {
204 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
205 }
206 else
207 {
208 size = value->size();
209 return true;
210 }
211 }
212 }
213 }