Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Toolbox/OsiriX/CollectionOfAnnotations.cpp @ 1584:bd180f97c734
parsing osirix annotations
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 21 Oct 2020 17:33:17 +0200 |
parents | |
children | b782f78aed42 |
comparison
equal
deleted
inserted
replaced
1583:c8644706e78b | 1584:bd180f97c734 |
---|---|
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 "CollectionOfAnnotations.h" | |
23 | |
24 #include "ArrayValue.h" | |
25 #include "IntegerValue.h" | |
26 | |
27 #include <OrthancException.h> | |
28 | |
29 #include <cassert> | |
30 | |
31 namespace OrthancStone | |
32 { | |
33 namespace OsiriX | |
34 { | |
35 static void GetAttributes(std::map<std::string, std::string>& target, | |
36 const pugi::xml_node& node) | |
37 { | |
38 for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) | |
39 { | |
40 target[attr.name()] = attr.value(); | |
41 } | |
42 } | |
43 | |
44 | |
45 CollectionOfAnnotations::~CollectionOfAnnotations() | |
46 { | |
47 for (size_t i = 0; i < annotations_.size(); i++) | |
48 { | |
49 assert(annotations_[i] != NULL); | |
50 delete annotations_[i]; | |
51 } | |
52 } | |
53 | |
54 | |
55 const Annotation& CollectionOfAnnotations::GetAnnotation(size_t i) const | |
56 { | |
57 if (i >= annotations_.size()) | |
58 { | |
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
60 } | |
61 else | |
62 { | |
63 assert(annotations_[i] != NULL); | |
64 return *annotations_[i]; | |
65 } | |
66 } | |
67 | |
68 | |
69 void CollectionOfAnnotations::AddAnnotation(Annotation* annotation) | |
70 { | |
71 if (annotation == NULL) | |
72 { | |
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
74 } | |
75 else | |
76 { | |
77 annotations_.push_back(annotation); | |
78 } | |
79 } | |
80 | |
81 void CollectionOfAnnotations::ParseXml(const std::string& xml) | |
82 { | |
83 pugi::xml_document doc; | |
84 pugi::xml_parse_result result = doc.load_buffer(xml.empty() ? NULL : xml.c_str(), xml.size()); | |
85 if (!result) | |
86 { | |
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
88 } | |
89 | |
90 const pugi::xml_node& root = doc.document_element(); | |
91 if (std::string(root.name()) != "plist" || | |
92 !root.first_child() || | |
93 root.first_child() != root.last_child()) | |
94 { | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
96 } | |
97 | |
98 std::map<std::string, std::string> attributes; | |
99 GetAttributes(attributes, root); | |
100 | |
101 std::map<std::string, std::string>::const_iterator version = attributes.find("version"); | |
102 if (version == attributes.end() || | |
103 version->second != "1.0") | |
104 { | |
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
106 } | |
107 | |
108 std::unique_ptr<IValue> value(IValue::Parse(root.first_child())); | |
109 | |
110 const DictionaryValue& dict = dynamic_cast<const DictionaryValue&>(*value); | |
111 | |
112 std::set<std::string> annotations; | |
113 dict.GetMembers(annotations); | |
114 | |
115 for (std::set<std::string>::const_iterator | |
116 it = annotations.begin(); it != annotations.end(); ++it) | |
117 { | |
118 const ArrayValue& images = dynamic_cast<const ArrayValue&>(dict.GetValue(*it)); | |
119 | |
120 for (size_t i = 0; i < images.GetSize(); i++) | |
121 { | |
122 const DictionaryValue& image = dynamic_cast<const DictionaryValue&>(images.GetValue(i)); | |
123 const IntegerValue& number = dynamic_cast<const IntegerValue&>(image.GetValue("NumberOfROIs")); | |
124 const ArrayValue& rois = dynamic_cast<const ArrayValue&>(image.GetValue("ROIs")); | |
125 | |
126 if (static_cast<int64_t>(rois.GetSize()) != number.GetValue()) | |
127 { | |
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
129 } | |
130 | |
131 for (size_t j = 0; j < rois.GetSize(); j++) | |
132 { | |
133 const DictionaryValue& roi = dynamic_cast<const DictionaryValue&>(rois.GetValue(i)); | |
134 | |
135 std::unique_ptr<Annotation> annotation(Annotation::Create(roi)); | |
136 if (annotation.get() != NULL) | |
137 { | |
138 AddAnnotation(annotation.release()); | |
139 } | |
140 } | |
141 } | |
142 } | |
143 } | |
144 } | |
145 } |