Mercurial > hg > orthanc
comparison OrthancServer/DicomModification.cpp @ 1698:d78b87f93bcf
DicomModification use Json::Value
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Oct 2015 12:29:21 +0200 |
parents | 18c02c6987d5 |
children | 1065401501fb |
comparison
equal
deleted
inserted
replaced
1697:21902c8ba95b | 1698:d78b87f93bcf |
---|---|
42 | 42 |
43 static const std::string ORTHANC_DEIDENTIFICATION_METHOD = "Orthanc " ORTHANC_VERSION " - PS 3.15-2008 Table E.1-1"; | 43 static const std::string ORTHANC_DEIDENTIFICATION_METHOD = "Orthanc " ORTHANC_VERSION " - PS 3.15-2008 Table E.1-1"; |
44 | 44 |
45 namespace Orthanc | 45 namespace Orthanc |
46 { | 46 { |
47 void DicomModification::RemoveInternal(const DicomTag& tag) | |
48 { | |
49 Replacements::iterator it = replacements_.find(tag); | |
50 | |
51 if (it != replacements_.end()) | |
52 { | |
53 delete it->second; | |
54 replacements_.erase(it); | |
55 } | |
56 } | |
57 | |
58 | |
59 void DicomModification::ReplaceInternal(const DicomTag& tag, | |
60 const Json::Value& value) | |
61 { | |
62 Replacements::iterator it = replacements_.find(tag); | |
63 | |
64 if (it != replacements_.end()) | |
65 { | |
66 delete it->second; | |
67 it->second = NULL; // In the case of an exception during the clone | |
68 it->second = new Json::Value(value); // Clone | |
69 } | |
70 else | |
71 { | |
72 replacements_[tag] = new Json::Value(value); // Clone | |
73 } | |
74 } | |
75 | |
76 | |
77 void DicomModification::ClearReplacements() | |
78 { | |
79 for (Replacements::iterator it = replacements_.begin(); | |
80 it != replacements_.end(); ++it) | |
81 { | |
82 delete it->second; | |
83 } | |
84 | |
85 replacements_.clear(); | |
86 } | |
87 | |
88 | |
47 void DicomModification::MarkNotOrthancAnonymization() | 89 void DicomModification::MarkNotOrthancAnonymization() |
48 { | 90 { |
49 Replacements::iterator it = replacements_.find(DICOM_TAG_DEIDENTIFICATION_METHOD); | 91 Replacements::iterator it = replacements_.find(DICOM_TAG_DEIDENTIFICATION_METHOD); |
50 | 92 |
51 if (it != replacements_.end() && | 93 if (it != replacements_.end() && |
52 it->second == ORTHANC_DEIDENTIFICATION_METHOD) | 94 it->second->asString() == ORTHANC_DEIDENTIFICATION_METHOD) |
53 { | 95 { |
96 delete it->second; | |
54 replacements_.erase(it); | 97 replacements_.erase(it); |
55 } | 98 } |
56 } | 99 } |
57 | 100 |
58 | 101 |
98 mapped = previous->second; | 141 mapped = previous->second; |
99 } | 142 } |
100 | 143 |
101 dicom.Replace(*tag, mapped); | 144 dicom.Replace(*tag, mapped); |
102 } | 145 } |
103 | 146 |
104 DicomModification::DicomModification() | 147 DicomModification::DicomModification() |
105 { | 148 { |
106 removePrivateTags_ = false; | 149 removePrivateTags_ = false; |
107 level_ = ResourceType_Instance; | 150 level_ = ResourceType_Instance; |
108 allowManualIdentifiers_ = true; | 151 allowManualIdentifiers_ = true; |
109 } | 152 } |
110 | 153 |
154 DicomModification::~DicomModification() | |
155 { | |
156 ClearReplacements(); | |
157 } | |
158 | |
111 void DicomModification::Keep(const DicomTag& tag) | 159 void DicomModification::Keep(const DicomTag& tag) |
112 { | 160 { |
113 removals_.erase(tag); | 161 removals_.erase(tag); |
114 replacements_.erase(tag); | 162 RemoveInternal(tag); |
115 | 163 |
116 if (FromDcmtkBridge::IsPrivateTag(tag)) | 164 if (FromDcmtkBridge::IsPrivateTag(tag)) |
117 { | 165 { |
118 privateTagsToKeep_.insert(tag); | 166 privateTagsToKeep_.insert(tag); |
119 } | 167 } |
122 } | 170 } |
123 | 171 |
124 void DicomModification::Remove(const DicomTag& tag) | 172 void DicomModification::Remove(const DicomTag& tag) |
125 { | 173 { |
126 removals_.insert(tag); | 174 removals_.insert(tag); |
127 replacements_.erase(tag); | 175 RemoveInternal(tag); |
128 privateTagsToKeep_.erase(tag); | 176 privateTagsToKeep_.erase(tag); |
129 | 177 |
130 MarkNotOrthancAnonymization(); | 178 MarkNotOrthancAnonymization(); |
131 } | 179 } |
132 | 180 |
134 { | 182 { |
135 return removals_.find(tag) != removals_.end(); | 183 return removals_.find(tag) != removals_.end(); |
136 } | 184 } |
137 | 185 |
138 void DicomModification::Replace(const DicomTag& tag, | 186 void DicomModification::Replace(const DicomTag& tag, |
139 const std::string& utf8Value, | 187 const Json::Value& value, |
140 bool safeForAnonymization) | 188 bool safeForAnonymization) |
141 { | 189 { |
142 removals_.erase(tag); | 190 removals_.erase(tag); |
143 privateTagsToKeep_.erase(tag); | 191 privateTagsToKeep_.erase(tag); |
144 replacements_[tag] = utf8Value; | 192 ReplaceInternal(tag, value); |
145 | 193 |
146 if (!safeForAnonymization) | 194 if (!safeForAnonymization) |
147 { | 195 { |
148 MarkNotOrthancAnonymization(); | 196 MarkNotOrthancAnonymization(); |
149 } | 197 } |
150 } | 198 } |
151 | 199 |
200 | |
152 bool DicomModification::IsReplaced(const DicomTag& tag) const | 201 bool DicomModification::IsReplaced(const DicomTag& tag) const |
153 { | 202 { |
154 return replacements_.find(tag) != replacements_.end(); | 203 return replacements_.find(tag) != replacements_.end(); |
155 } | 204 } |
156 | 205 |
157 const std::string& DicomModification::GetReplacement(const DicomTag& tag) const | 206 const Json::Value& DicomModification::GetReplacement(const DicomTag& tag) const |
158 { | 207 { |
159 Replacements::const_iterator it = replacements_.find(tag); | 208 Replacements::const_iterator it = replacements_.find(tag); |
160 | 209 |
161 if (it == replacements_.end()) | 210 if (it == replacements_.end()) |
162 { | 211 { |
163 throw OrthancException(ErrorCode_InexistentItem); | 212 throw OrthancException(ErrorCode_InexistentItem); |
164 } | 213 } |
165 else | 214 else |
166 { | 215 { |
167 return it->second; | 216 return *it->second; |
168 } | 217 } |
169 } | 218 } |
219 | |
220 | |
221 std::string DicomModification::GetReplacementAsString(const DicomTag& tag) const | |
222 { | |
223 const Json::Value& json = GetReplacement(tag); | |
224 | |
225 if (json.type() != Json::stringValue) | |
226 { | |
227 throw OrthancException(ErrorCode_BadParameterType); | |
228 } | |
229 else | |
230 { | |
231 return json.asString(); | |
232 } | |
233 } | |
234 | |
170 | 235 |
171 void DicomModification::SetRemovePrivateTags(bool removed) | 236 void DicomModification::SetRemovePrivateTags(bool removed) |
172 { | 237 { |
173 removePrivateTags_ = removed; | 238 removePrivateTags_ = removed; |
174 | 239 |
190 } | 255 } |
191 | 256 |
192 void DicomModification::SetupAnonymization() | 257 void DicomModification::SetupAnonymization() |
193 { | 258 { |
194 removals_.clear(); | 259 removals_.clear(); |
195 replacements_.clear(); | 260 ClearReplacements(); |
196 removePrivateTags_ = true; | 261 removePrivateTags_ = true; |
197 level_ = ResourceType_Patient; | 262 level_ = ResourceType_Patient; |
198 uidMap_.clear(); | 263 uidMap_.clear(); |
199 privateTagsToKeep_.clear(); | 264 privateTagsToKeep_.clear(); |
200 | 265 |
253 removals_.insert(DicomTag(0x0032, 0x1032)); // Requesting Physician | 318 removals_.insert(DicomTag(0x0032, 0x1032)); // Requesting Physician |
254 removals_.insert(DicomTag(0x0010, 0x2154)); // PatientTelephoneNumbers | 319 removals_.insert(DicomTag(0x0010, 0x2154)); // PatientTelephoneNumbers |
255 removals_.insert(DicomTag(0x0010, 0x2000)); // Medical Alerts | 320 removals_.insert(DicomTag(0x0010, 0x2000)); // Medical Alerts |
256 | 321 |
257 // Set the DeidentificationMethod tag | 322 // Set the DeidentificationMethod tag |
258 replacements_.insert(std::make_pair(DICOM_TAG_DEIDENTIFICATION_METHOD, ORTHANC_DEIDENTIFICATION_METHOD)); | 323 ReplaceInternal(DICOM_TAG_DEIDENTIFICATION_METHOD, ORTHANC_DEIDENTIFICATION_METHOD); |
259 | 324 |
260 // Set the PatientIdentityRemoved tag | 325 // Set the PatientIdentityRemoved tag |
261 replacements_.insert(std::make_pair(DicomTag(0x0012, 0x0062), "YES")); | 326 ReplaceInternal(DicomTag(0x0012, 0x0062), "YES"); |
262 | 327 |
263 // (*) Choose a random patient name and ID | 328 // (*) Choose a random patient name and ID |
264 std::string patientId = FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Patient); | 329 std::string patientId = FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Patient); |
265 replacements_[DICOM_TAG_PATIENT_ID] = patientId; | 330 ReplaceInternal(DICOM_TAG_PATIENT_ID, patientId); |
266 replacements_[DICOM_TAG_PATIENT_NAME] = patientId; | 331 ReplaceInternal(DICOM_TAG_PATIENT_NAME, patientId); |
267 } | 332 } |
268 | 333 |
269 void DicomModification::Apply(ParsedDicomFile& toModify) | 334 void DicomModification::Apply(ParsedDicomFile& toModify) |
270 { | 335 { |
271 // Check the request | 336 // Check the request |
392 | 457 |
393 // (3) Replace the tags | 458 // (3) Replace the tags |
394 for (Replacements::const_iterator it = replacements_.begin(); | 459 for (Replacements::const_iterator it = replacements_.begin(); |
395 it != replacements_.end(); ++it) | 460 it != replacements_.end(); ++it) |
396 { | 461 { |
397 toModify.Replace(it->first, it->second, DicomReplaceMode_InsertIfAbsent); | 462 toModify.Replace(it->first, *it->second, DicomReplaceMode_InsertIfAbsent); |
398 } | 463 } |
399 | 464 |
400 // (4) Update the DICOM identifiers | 465 // (4) Update the DICOM identifiers |
401 if (level_ <= ResourceType_Study && | 466 if (level_ <= ResourceType_Study && |
402 !IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID)) | 467 !IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID)) |