Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Loaders/LoadedDicomResources.cpp @ 1822:0489fe25ce48
support of pixel spacing in ultrasound images from tag SequenceOfUltrasoundRegions
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 May 2021 18:13:35 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
comparison
equal
deleted
inserted
replaced
1821:36430d73e36c | 1822:0489fe25ce48 |
---|---|
27 #include <cassert> | 27 #include <cassert> |
28 | 28 |
29 | 29 |
30 namespace OrthancStone | 30 namespace OrthancStone |
31 { | 31 { |
32 void LoadedDicomResources::Flatten() | 32 LoadedDicomResources::Resource::Resource(const Orthanc::DicomMap& dicom) : |
33 dicom_(dicom.Clone()) | |
34 { | |
35 if (dicom_.get() == NULL) | |
36 { | |
37 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
38 } | |
39 } | |
40 | |
41 | |
42 LoadedDicomResources::Resource* LoadedDicomResources::Resource::Clone() const | |
43 { | |
44 assert(dicom_.get() != NULL); | |
45 return new Resource(*dicom_); | |
46 } | |
47 | |
48 | |
49 const Json::Value& LoadedDicomResources::Resource::GetSourceJson() const | |
50 { | |
51 if (sourceJson_.get() == NULL) | |
52 { | |
53 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
54 } | |
55 else | |
56 { | |
57 return *sourceJson_; | |
58 } | |
59 } | |
60 | |
61 | |
62 void LoadedDicomResources::Resource::SetSourceJson(const Json::Value& json) | |
63 { | |
64 sourceJson_.reset(new Json::Value(json)); | |
65 } | |
66 | |
67 | |
68 void LoadedDicomResources::AddResourceInternal(Resource* resource) | |
69 { | |
70 std::unique_ptr<Resource> protection(resource); | |
71 | |
72 std::string id; | |
73 | |
74 if (protection->GetDicom().LookupStringValue(id, indexedTag_, false /* no binary value */) && | |
75 resources_.find(id) == resources_.end() /* Don't index twice the same resource */) | |
76 { | |
77 resources_[id] = protection.release(); | |
78 flattened_.clear(); // Invalidate the flattened version | |
79 } | |
80 } | |
81 | |
82 | |
83 const LoadedDicomResources::Resource& LoadedDicomResources::GetResourceInternal(size_t index) | |
33 { | 84 { |
34 // Lazy generation of a "std::vector" from the "std::map" | 85 // Lazy generation of a "std::vector" from the "std::map" |
35 if (flattened_.empty()) | 86 if (flattened_.empty()) |
36 { | 87 { |
37 flattened_.resize(resources_.size()); | 88 flattened_.resize(resources_.size()); |
45 } | 96 } |
46 else | 97 else |
47 { | 98 { |
48 // No need to flatten | 99 // No need to flatten |
49 assert(flattened_.size() == resources_.size()); | 100 assert(flattened_.size() == resources_.size()); |
101 } | |
102 | |
103 if (index >= flattened_.size()) | |
104 { | |
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
106 } | |
107 else | |
108 { | |
109 assert(flattened_[index] != NULL); | |
110 return *flattened_[index]; | |
50 } | 111 } |
51 } | 112 } |
52 | 113 |
53 | 114 |
54 void LoadedDicomResources::AddFromDicomWebInternal(const Json::Value& dicomweb) | 115 void LoadedDicomResources::AddFromDicomWebInternal(const Json::Value& dicomweb) |
55 { | 116 { |
56 assert(dicomweb.type() == Json::objectValue); | 117 assert(dicomweb.type() == Json::objectValue); |
57 Orthanc::DicomMap dicom; | 118 Orthanc::DicomMap dicom; |
58 dicom.FromDicomWeb(dicomweb); | 119 dicom.FromDicomWeb(dicomweb); |
59 AddResource(dicom); | 120 |
121 std::unique_ptr<Resource> resource(new Resource(dicom)); | |
122 resource->SetSourceJson(dicomweb); | |
123 AddResourceInternal(resource.release()); | |
60 } | 124 } |
61 | 125 |
62 | 126 |
63 LoadedDicomResources::LoadedDicomResources(const LoadedDicomResources& other, | 127 LoadedDicomResources::LoadedDicomResources(const LoadedDicomResources& other, |
64 const Orthanc::DicomTag& indexedTag) : | 128 const Orthanc::DicomTag& indexedTag) : |
66 { | 130 { |
67 for (Resources::const_iterator it = other.resources_.begin(); | 131 for (Resources::const_iterator it = other.resources_.begin(); |
68 it != other.resources_.end(); ++it) | 132 it != other.resources_.end(); ++it) |
69 { | 133 { |
70 assert(it->second != NULL); | 134 assert(it->second != NULL); |
71 AddResource(*it->second); | 135 AddResourceInternal(it->second->Clone()); |
72 } | 136 } |
73 } | 137 } |
74 | 138 |
75 void LoadedDicomResources::Clear() | 139 void LoadedDicomResources::Clear() |
76 { | 140 { |
83 resources_.clear(); | 147 resources_.clear(); |
84 flattened_.clear(); | 148 flattened_.clear(); |
85 } | 149 } |
86 | 150 |
87 | 151 |
88 Orthanc::DicomMap& LoadedDicomResources::GetResource(size_t index) | |
89 { | |
90 Flatten(); | |
91 | |
92 if (index >= flattened_.size()) | |
93 { | |
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
95 } | |
96 else | |
97 { | |
98 assert(flattened_[index] != NULL); | |
99 return *flattened_[index]; | |
100 } | |
101 } | |
102 | |
103 | |
104 void LoadedDicomResources::MergeResource(Orthanc::DicomMap& target, | 152 void LoadedDicomResources::MergeResource(Orthanc::DicomMap& target, |
105 const std::string& id) const | 153 const std::string& id) const |
106 { | 154 { |
107 Resources::const_iterator it = resources_.find(id); | 155 Resources::const_iterator it = resources_.find(id); |
108 | 156 |
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); | 159 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); |
112 } | 160 } |
113 else | 161 else |
114 { | 162 { |
115 assert(it->second != NULL); | 163 assert(it->second != NULL); |
116 target.Merge(*it->second); | 164 target.Merge(it->second->GetDicom()); |
117 } | 165 } |
118 } | 166 } |
119 | 167 |
120 | 168 |
121 bool LoadedDicomResources::LookupStringValue(std::string& target, | 169 bool LoadedDicomResources::LookupStringValue(std::string& target, |
129 return false; | 177 return false; |
130 } | 178 } |
131 else | 179 else |
132 { | 180 { |
133 assert(found->second != NULL); | 181 assert(found->second != NULL); |
134 return found->second->LookupStringValue(target, tag, false); | 182 return found->second->GetDicom().LookupStringValue(target, tag, false); |
135 } | 183 } |
136 } | 184 } |
137 | 185 |
138 | 186 |
139 void LoadedDicomResources::AddResource(const Orthanc::DicomMap& dicom) | 187 void LoadedDicomResources::AddResource(const Orthanc::DicomMap& dicom) |
140 { | 188 { |
141 std::string id; | 189 AddResourceInternal(new Resource(dicom)); |
142 | |
143 if (dicom.LookupStringValue(id, indexedTag_, false /* no binary value */) && | |
144 resources_.find(id) == resources_.end() /* Don't index twice the same resource */) | |
145 { | |
146 resources_[id] = dicom.Clone(); | |
147 flattened_.clear(); // Invalidate the flattened version | |
148 } | |
149 } | 190 } |
150 | 191 |
151 | 192 |
152 void LoadedDicomResources::AddFromOrthanc(const Json::Value& tags) | 193 void LoadedDicomResources::AddFromOrthanc(const Json::Value& tags) |
153 { | 194 { |
154 Orthanc::DicomMap dicom; | 195 Orthanc::DicomMap dicom; |
155 dicom.FromDicomAsJson(tags); | 196 dicom.FromDicomAsJson(tags); |
156 AddResource(dicom); | 197 |
198 std::unique_ptr<Resource> resource(new Resource(dicom)); | |
199 resource->SetSourceJson(tags); | |
200 AddResourceInternal(resource.release()); | |
157 } | 201 } |
158 | 202 |
159 | 203 |
160 void LoadedDicomResources::AddFromDicomWeb(const Json::Value& dicomweb) | 204 void LoadedDicomResources::AddFromDicomWeb(const Json::Value& dicomweb) |
161 { | 205 { |
194 for (Resources::const_iterator it = resources_.begin(); it != resources_.end(); ++it) | 238 for (Resources::const_iterator it = resources_.begin(); it != resources_.end(); ++it) |
195 { | 239 { |
196 assert(it->second != NULL); | 240 assert(it->second != NULL); |
197 | 241 |
198 std::string value; | 242 std::string value; |
199 if (it->second->LookupStringValue(value, tag, false)) | 243 if (it->second->GetDicom().LookupStringValue(value, tag, false)) |
200 { | 244 { |
201 Counter::iterator found = counter.find(value); | 245 Counter::iterator found = counter.find(value); |
202 if (found == counter.end()) | 246 if (found == counter.end()) |
203 { | 247 { |
204 counter[value] = 1; | 248 counter[value] = 1; |