Mercurial > hg > orthanc
annotate OrthancServer/DicomModification.cpp @ 822:7ce875531950
fix mainline version
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 May 2014 17:16:10 +0200 |
parents | d466b3606aca |
children | a811bdf8b8eb |
rev | line source |
---|---|
786 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "DicomModification.h" | |
34 | |
35 #include "../Core/OrthancException.h" | |
790 | 36 #include "FromDcmtkBridge.h" |
786 | 37 |
795 | 38 #include <memory> // For std::auto_ptr |
39 | |
786 | 40 namespace Orthanc |
41 { | |
42 void DicomModification::MapDicomIdentifier(ParsedDicomFile& dicom, | |
788 | 43 ResourceType level) |
786 | 44 { |
45 std::auto_ptr<DicomTag> tag; | |
46 | |
47 switch (level) | |
48 { | |
788 | 49 case ResourceType_Study: |
786 | 50 tag.reset(new DicomTag(DICOM_TAG_STUDY_INSTANCE_UID)); |
51 break; | |
52 | |
788 | 53 case ResourceType_Series: |
786 | 54 tag.reset(new DicomTag(DICOM_TAG_SERIES_INSTANCE_UID)); |
55 break; | |
56 | |
788 | 57 case ResourceType_Instance: |
786 | 58 tag.reset(new DicomTag(DICOM_TAG_SOP_INSTANCE_UID)); |
59 break; | |
60 | |
61 default: | |
62 throw OrthancException(ErrorCode_InternalError); | |
63 } | |
64 | |
65 std::string original; | |
66 if (!dicom.GetTagValue(original, *tag)) | |
67 { | |
68 original = ""; | |
69 } | |
70 | |
71 std::string mapped; | |
72 | |
73 UidMap::const_iterator previous = uidMap_.find(std::make_pair(level, original)); | |
74 if (previous == uidMap_.end()) | |
75 { | |
76 mapped = FromDcmtkBridge::GenerateUniqueIdentifier(level); | |
77 uidMap_.insert(std::make_pair(std::make_pair(level, original), mapped)); | |
78 } | |
79 else | |
80 { | |
81 mapped = previous->second; | |
82 } | |
83 | |
84 dicom.Replace(*tag, mapped); | |
85 } | |
86 | |
87 DicomModification::DicomModification() | |
88 { | |
89 removePrivateTags_ = false; | |
788 | 90 level_ = ResourceType_Instance; |
786 | 91 } |
92 | |
787
ac18946afa74
refactoring of anonymization/modification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
786
diff
changeset
|
93 void DicomModification::Keep(const DicomTag& tag) |
786 | 94 { |
95 removals_.erase(tag); | |
96 replacements_.erase(tag); | |
97 } | |
98 | |
99 void DicomModification::Remove(const DicomTag& tag) | |
100 { | |
101 removals_.insert(tag); | |
102 replacements_.erase(tag); | |
103 } | |
104 | |
105 bool DicomModification::IsRemoved(const DicomTag& tag) const | |
106 { | |
107 return removals_.find(tag) != removals_.end(); | |
108 } | |
109 | |
110 void DicomModification::Replace(const DicomTag& tag, | |
111 const std::string& value) | |
112 { | |
113 removals_.erase(tag); | |
114 replacements_[tag] = value; | |
115 } | |
116 | |
117 bool DicomModification::IsReplaced(const DicomTag& tag) const | |
118 { | |
119 return replacements_.find(tag) != replacements_.end(); | |
120 } | |
121 | |
122 const std::string& DicomModification::GetReplacement(const DicomTag& tag) const | |
123 { | |
124 Replacements::const_iterator it = replacements_.find(tag); | |
125 | |
126 if (it == replacements_.end()) | |
127 { | |
128 throw OrthancException(ErrorCode_InexistentItem); | |
129 } | |
130 else | |
131 { | |
132 return it->second; | |
133 } | |
134 } | |
135 | |
136 void DicomModification::SetRemovePrivateTags(bool removed) | |
137 { | |
138 removePrivateTags_ = removed; | |
139 } | |
140 | |
788 | 141 void DicomModification::SetLevel(ResourceType level) |
786 | 142 { |
143 uidMap_.clear(); | |
144 level_ = level; | |
145 } | |
146 | |
147 void DicomModification::SetupAnonymization() | |
148 { | |
149 removals_.clear(); | |
150 replacements_.clear(); | |
151 removePrivateTags_ = true; | |
788 | 152 level_ = ResourceType_Patient; |
786 | 153 uidMap_.clear(); |
154 | |
155 // This is Table E.1-1 from PS 3.15-2008 - DICOM Part 15: Security and System Management Profiles | |
156 removals_.insert(DicomTag(0x0008, 0x0014)); // Instance Creator UID | |
157 //removals_.insert(DicomTag(0x0008, 0x0018)); // SOP Instance UID => set in Apply() | |
158 removals_.insert(DicomTag(0x0008, 0x0050)); // Accession Number | |
159 removals_.insert(DicomTag(0x0008, 0x0080)); // Institution Name | |
160 removals_.insert(DicomTag(0x0008, 0x0081)); // Institution Address | |
161 removals_.insert(DicomTag(0x0008, 0x0090)); // Referring Physician's Name | |
162 removals_.insert(DicomTag(0x0008, 0x0092)); // Referring Physician's Address | |
163 removals_.insert(DicomTag(0x0008, 0x0094)); // Referring Physician's Telephone Numbers | |
164 removals_.insert(DicomTag(0x0008, 0x1010)); // Station Name | |
165 removals_.insert(DicomTag(0x0008, 0x1030)); // Study Description | |
166 removals_.insert(DicomTag(0x0008, 0x103e)); // Series Description | |
167 removals_.insert(DicomTag(0x0008, 0x1040)); // Institutional Department Name | |
168 removals_.insert(DicomTag(0x0008, 0x1048)); // Physician(s) of Record | |
169 removals_.insert(DicomTag(0x0008, 0x1050)); // Performing Physicians' Name | |
170 removals_.insert(DicomTag(0x0008, 0x1060)); // Name of Physician(s) Reading Study | |
171 removals_.insert(DicomTag(0x0008, 0x1070)); // Operators' Name | |
172 removals_.insert(DicomTag(0x0008, 0x1080)); // Admitting Diagnoses Description | |
173 removals_.insert(DicomTag(0x0008, 0x1155)); // Referenced SOP Instance UID | |
174 removals_.insert(DicomTag(0x0008, 0x2111)); // Derivation Description | |
175 //removals_.insert(DicomTag(0x0010, 0x0010)); // Patient's Name => cf. below (*) | |
176 //removals_.insert(DicomTag(0x0010, 0x0020)); // Patient ID => cf. below (*) | |
177 removals_.insert(DicomTag(0x0010, 0x0030)); // Patient's Birth Date | |
178 removals_.insert(DicomTag(0x0010, 0x0032)); // Patient's Birth Time | |
179 removals_.insert(DicomTag(0x0010, 0x0040)); // Patient's Sex | |
180 removals_.insert(DicomTag(0x0010, 0x1000)); // Other Patient Ids | |
181 removals_.insert(DicomTag(0x0010, 0x1001)); // Other Patient Names | |
182 removals_.insert(DicomTag(0x0010, 0x1010)); // Patient's Age | |
183 removals_.insert(DicomTag(0x0010, 0x1020)); // Patient's Size | |
184 removals_.insert(DicomTag(0x0010, 0x1030)); // Patient's Weight | |
185 removals_.insert(DicomTag(0x0010, 0x1090)); // Medical Record Locator | |
186 removals_.insert(DicomTag(0x0010, 0x2160)); // Ethnic Group | |
187 removals_.insert(DicomTag(0x0010, 0x2180)); // Occupation | |
188 removals_.insert(DicomTag(0x0010, 0x21b0)); // Additional Patient's History | |
189 removals_.insert(DicomTag(0x0010, 0x4000)); // Patient Comments | |
190 removals_.insert(DicomTag(0x0018, 0x1000)); // Device Serial Number | |
191 removals_.insert(DicomTag(0x0018, 0x1030)); // Protocol Name | |
192 //removals_.insert(DicomTag(0x0020, 0x000d)); // Study Instance UID => set in Apply() | |
193 //removals_.insert(DicomTag(0x0020, 0x000e)); // Series Instance UID => set in Apply() | |
194 removals_.insert(DicomTag(0x0020, 0x0010)); // Study ID | |
195 removals_.insert(DicomTag(0x0020, 0x0052)); // Frame of Reference UID | |
196 removals_.insert(DicomTag(0x0020, 0x0200)); // Synchronization Frame of Reference UID | |
197 removals_.insert(DicomTag(0x0020, 0x4000)); // Image Comments | |
198 removals_.insert(DicomTag(0x0040, 0x0275)); // Request Attributes Sequence | |
199 removals_.insert(DicomTag(0x0040, 0xa124)); // UID | |
200 removals_.insert(DicomTag(0x0040, 0xa730)); // Content Sequence | |
201 removals_.insert(DicomTag(0x0088, 0x0140)); // Storage Media File-set UID | |
202 removals_.insert(DicomTag(0x3006, 0x0024)); // Referenced Frame of Reference UID | |
203 removals_.insert(DicomTag(0x3006, 0x00c2)); // Related Frame of Reference UID | |
204 | |
205 // Some more removals (from the experience of DICOM files at the CHU of Liege) | |
206 removals_.insert(DicomTag(0x0010, 0x1040)); // Patient's Address | |
207 removals_.insert(DicomTag(0x0032, 0x1032)); // Requesting Physician | |
208 removals_.insert(DicomTag(0x0010, 0x2154)); // PatientTelephoneNumbers | |
209 removals_.insert(DicomTag(0x0010, 0x2000)); // Medical Alerts | |
210 | |
211 // Set the DeidentificationMethod tag | |
212 replacements_.insert(std::make_pair(DicomTag(0x0012, 0x0063), "Orthanc " ORTHANC_VERSION " - PS 3.15-2008 Table E.1-1")); | |
213 | |
214 // Set the PatientIdentityRemoved tag | |
215 replacements_.insert(std::make_pair(DicomTag(0x0012, 0x0062), "YES")); | |
216 | |
217 // (*) Choose a random patient name and ID | |
788 | 218 std::string patientId = FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Patient); |
786 | 219 replacements_[DICOM_TAG_PATIENT_ID] = patientId; |
220 replacements_[DICOM_TAG_PATIENT_NAME] = patientId; | |
221 } | |
222 | |
223 void DicomModification::Apply(ParsedDicomFile& toModify) | |
224 { | |
225 // Check the request | |
788 | 226 assert(ResourceType_Patient + 1 == ResourceType_Study && |
227 ResourceType_Study + 1 == ResourceType_Series && | |
228 ResourceType_Series + 1 == ResourceType_Instance); | |
786 | 229 |
230 if (IsRemoved(DICOM_TAG_PATIENT_ID) || | |
231 IsRemoved(DICOM_TAG_STUDY_INSTANCE_UID) || | |
232 IsRemoved(DICOM_TAG_SERIES_INSTANCE_UID) || | |
233 IsRemoved(DICOM_TAG_SOP_INSTANCE_UID)) | |
234 { | |
235 throw OrthancException(ErrorCode_BadRequest); | |
236 } | |
237 | |
788 | 238 if (level_ == ResourceType_Patient && !IsReplaced(DICOM_TAG_PATIENT_ID)) |
786 | 239 { |
240 throw OrthancException(ErrorCode_BadRequest); | |
241 } | |
242 | |
788 | 243 if (level_ > ResourceType_Patient && IsReplaced(DICOM_TAG_PATIENT_ID)) |
786 | 244 { |
245 throw OrthancException(ErrorCode_BadRequest); | |
246 } | |
247 | |
788 | 248 if (level_ > ResourceType_Study && IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID)) |
786 | 249 { |
250 throw OrthancException(ErrorCode_BadRequest); | |
251 } | |
252 | |
788 | 253 if (level_ > ResourceType_Series && IsReplaced(DICOM_TAG_SERIES_INSTANCE_UID)) |
786 | 254 { |
255 throw OrthancException(ErrorCode_BadRequest); | |
256 } | |
257 | |
258 // (1) Remove the private tags, if need be | |
259 if (removePrivateTags_) | |
260 { | |
261 toModify.RemovePrivateTags(); | |
262 } | |
263 | |
264 // (2) Remove the tags specified by the user | |
265 for (Removals::const_iterator it = removals_.begin(); | |
266 it != removals_.end(); ++it) | |
267 { | |
268 toModify.Remove(*it); | |
269 } | |
270 | |
271 // (3) Replace the tags | |
272 for (Replacements::const_iterator it = replacements_.begin(); | |
273 it != replacements_.end(); ++it) | |
274 { | |
789 | 275 toModify.Replace(it->first, it->second, DicomReplaceMode_InsertIfAbsent); |
786 | 276 } |
277 | |
278 // (4) Update the DICOM identifiers | |
788 | 279 if (level_ <= ResourceType_Study) |
786 | 280 { |
788 | 281 MapDicomIdentifier(toModify, ResourceType_Study); |
786 | 282 } |
283 | |
788 | 284 if (level_ <= ResourceType_Series) |
786 | 285 { |
788 | 286 MapDicomIdentifier(toModify, ResourceType_Series); |
786 | 287 } |
288 | |
788 | 289 if (level_ <= ResourceType_Instance) // Always true |
786 | 290 { |
788 | 291 MapDicomIdentifier(toModify, ResourceType_Instance); |
786 | 292 } |
293 } | |
294 } |