Mercurial > hg > orthanc
annotate OrthancServer/DicomModification.cpp @ 841:89a593f66104 jpeg
fix windows build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Jun 2014 20:37:35 +0200 |
parents | 84513f2ee1f3 |
children | b4b46e3e6017 |
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 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
33 #include "PrecompiledHeadersServer.h" |
786 | 34 #include "DicomModification.h" |
35 | |
36 #include "../Core/OrthancException.h" | |
790 | 37 #include "FromDcmtkBridge.h" |
786 | 38 |
795 | 39 #include <memory> // For std::auto_ptr |
40 | |
786 | 41 namespace Orthanc |
42 { | |
43 void DicomModification::MapDicomIdentifier(ParsedDicomFile& dicom, | |
788 | 44 ResourceType level) |
786 | 45 { |
46 std::auto_ptr<DicomTag> tag; | |
47 | |
48 switch (level) | |
49 { | |
788 | 50 case ResourceType_Study: |
786 | 51 tag.reset(new DicomTag(DICOM_TAG_STUDY_INSTANCE_UID)); |
52 break; | |
53 | |
788 | 54 case ResourceType_Series: |
786 | 55 tag.reset(new DicomTag(DICOM_TAG_SERIES_INSTANCE_UID)); |
56 break; | |
57 | |
788 | 58 case ResourceType_Instance: |
786 | 59 tag.reset(new DicomTag(DICOM_TAG_SOP_INSTANCE_UID)); |
60 break; | |
61 | |
62 default: | |
63 throw OrthancException(ErrorCode_InternalError); | |
64 } | |
65 | |
66 std::string original; | |
67 if (!dicom.GetTagValue(original, *tag)) | |
68 { | |
69 original = ""; | |
70 } | |
71 | |
72 std::string mapped; | |
73 | |
74 UidMap::const_iterator previous = uidMap_.find(std::make_pair(level, original)); | |
75 if (previous == uidMap_.end()) | |
76 { | |
77 mapped = FromDcmtkBridge::GenerateUniqueIdentifier(level); | |
78 uidMap_.insert(std::make_pair(std::make_pair(level, original), mapped)); | |
79 } | |
80 else | |
81 { | |
82 mapped = previous->second; | |
83 } | |
84 | |
85 dicom.Replace(*tag, mapped); | |
86 } | |
87 | |
88 DicomModification::DicomModification() | |
89 { | |
90 removePrivateTags_ = false; | |
788 | 91 level_ = ResourceType_Instance; |
786 | 92 } |
93 | |
787
ac18946afa74
refactoring of anonymization/modification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
786
diff
changeset
|
94 void DicomModification::Keep(const DicomTag& tag) |
786 | 95 { |
96 removals_.erase(tag); | |
97 replacements_.erase(tag); | |
98 } | |
99 | |
100 void DicomModification::Remove(const DicomTag& tag) | |
101 { | |
102 removals_.insert(tag); | |
103 replacements_.erase(tag); | |
104 } | |
105 | |
106 bool DicomModification::IsRemoved(const DicomTag& tag) const | |
107 { | |
108 return removals_.find(tag) != removals_.end(); | |
109 } | |
110 | |
111 void DicomModification::Replace(const DicomTag& tag, | |
112 const std::string& value) | |
113 { | |
114 removals_.erase(tag); | |
115 replacements_[tag] = value; | |
116 } | |
117 | |
118 bool DicomModification::IsReplaced(const DicomTag& tag) const | |
119 { | |
120 return replacements_.find(tag) != replacements_.end(); | |
121 } | |
122 | |
123 const std::string& DicomModification::GetReplacement(const DicomTag& tag) const | |
124 { | |
125 Replacements::const_iterator it = replacements_.find(tag); | |
126 | |
127 if (it == replacements_.end()) | |
128 { | |
129 throw OrthancException(ErrorCode_InexistentItem); | |
130 } | |
131 else | |
132 { | |
133 return it->second; | |
134 } | |
135 } | |
136 | |
137 void DicomModification::SetRemovePrivateTags(bool removed) | |
138 { | |
139 removePrivateTags_ = removed; | |
140 } | |
141 | |
788 | 142 void DicomModification::SetLevel(ResourceType level) |
786 | 143 { |
144 uidMap_.clear(); | |
145 level_ = level; | |
146 } | |
147 | |
148 void DicomModification::SetupAnonymization() | |
149 { | |
150 removals_.clear(); | |
151 replacements_.clear(); | |
152 removePrivateTags_ = true; | |
788 | 153 level_ = ResourceType_Patient; |
786 | 154 uidMap_.clear(); |
155 | |
156 // This is Table E.1-1 from PS 3.15-2008 - DICOM Part 15: Security and System Management Profiles | |
157 removals_.insert(DicomTag(0x0008, 0x0014)); // Instance Creator UID | |
158 //removals_.insert(DicomTag(0x0008, 0x0018)); // SOP Instance UID => set in Apply() | |
159 removals_.insert(DicomTag(0x0008, 0x0050)); // Accession Number | |
160 removals_.insert(DicomTag(0x0008, 0x0080)); // Institution Name | |
161 removals_.insert(DicomTag(0x0008, 0x0081)); // Institution Address | |
162 removals_.insert(DicomTag(0x0008, 0x0090)); // Referring Physician's Name | |
163 removals_.insert(DicomTag(0x0008, 0x0092)); // Referring Physician's Address | |
164 removals_.insert(DicomTag(0x0008, 0x0094)); // Referring Physician's Telephone Numbers | |
165 removals_.insert(DicomTag(0x0008, 0x1010)); // Station Name | |
166 removals_.insert(DicomTag(0x0008, 0x1030)); // Study Description | |
167 removals_.insert(DicomTag(0x0008, 0x103e)); // Series Description | |
168 removals_.insert(DicomTag(0x0008, 0x1040)); // Institutional Department Name | |
169 removals_.insert(DicomTag(0x0008, 0x1048)); // Physician(s) of Record | |
170 removals_.insert(DicomTag(0x0008, 0x1050)); // Performing Physicians' Name | |
171 removals_.insert(DicomTag(0x0008, 0x1060)); // Name of Physician(s) Reading Study | |
172 removals_.insert(DicomTag(0x0008, 0x1070)); // Operators' Name | |
173 removals_.insert(DicomTag(0x0008, 0x1080)); // Admitting Diagnoses Description | |
174 removals_.insert(DicomTag(0x0008, 0x1155)); // Referenced SOP Instance UID | |
175 removals_.insert(DicomTag(0x0008, 0x2111)); // Derivation Description | |
176 //removals_.insert(DicomTag(0x0010, 0x0010)); // Patient's Name => cf. below (*) | |
177 //removals_.insert(DicomTag(0x0010, 0x0020)); // Patient ID => cf. below (*) | |
178 removals_.insert(DicomTag(0x0010, 0x0030)); // Patient's Birth Date | |
179 removals_.insert(DicomTag(0x0010, 0x0032)); // Patient's Birth Time | |
180 removals_.insert(DicomTag(0x0010, 0x0040)); // Patient's Sex | |
181 removals_.insert(DicomTag(0x0010, 0x1000)); // Other Patient Ids | |
182 removals_.insert(DicomTag(0x0010, 0x1001)); // Other Patient Names | |
183 removals_.insert(DicomTag(0x0010, 0x1010)); // Patient's Age | |
184 removals_.insert(DicomTag(0x0010, 0x1020)); // Patient's Size | |
185 removals_.insert(DicomTag(0x0010, 0x1030)); // Patient's Weight | |
186 removals_.insert(DicomTag(0x0010, 0x1090)); // Medical Record Locator | |
187 removals_.insert(DicomTag(0x0010, 0x2160)); // Ethnic Group | |
188 removals_.insert(DicomTag(0x0010, 0x2180)); // Occupation | |
189 removals_.insert(DicomTag(0x0010, 0x21b0)); // Additional Patient's History | |
190 removals_.insert(DicomTag(0x0010, 0x4000)); // Patient Comments | |
191 removals_.insert(DicomTag(0x0018, 0x1000)); // Device Serial Number | |
192 removals_.insert(DicomTag(0x0018, 0x1030)); // Protocol Name | |
193 //removals_.insert(DicomTag(0x0020, 0x000d)); // Study Instance UID => set in Apply() | |
194 //removals_.insert(DicomTag(0x0020, 0x000e)); // Series Instance UID => set in Apply() | |
195 removals_.insert(DicomTag(0x0020, 0x0010)); // Study ID | |
196 removals_.insert(DicomTag(0x0020, 0x0052)); // Frame of Reference UID | |
197 removals_.insert(DicomTag(0x0020, 0x0200)); // Synchronization Frame of Reference UID | |
198 removals_.insert(DicomTag(0x0020, 0x4000)); // Image Comments | |
199 removals_.insert(DicomTag(0x0040, 0x0275)); // Request Attributes Sequence | |
200 removals_.insert(DicomTag(0x0040, 0xa124)); // UID | |
201 removals_.insert(DicomTag(0x0040, 0xa730)); // Content Sequence | |
202 removals_.insert(DicomTag(0x0088, 0x0140)); // Storage Media File-set UID | |
203 removals_.insert(DicomTag(0x3006, 0x0024)); // Referenced Frame of Reference UID | |
204 removals_.insert(DicomTag(0x3006, 0x00c2)); // Related Frame of Reference UID | |
205 | |
206 // Some more removals (from the experience of DICOM files at the CHU of Liege) | |
207 removals_.insert(DicomTag(0x0010, 0x1040)); // Patient's Address | |
208 removals_.insert(DicomTag(0x0032, 0x1032)); // Requesting Physician | |
209 removals_.insert(DicomTag(0x0010, 0x2154)); // PatientTelephoneNumbers | |
210 removals_.insert(DicomTag(0x0010, 0x2000)); // Medical Alerts | |
211 | |
212 // Set the DeidentificationMethod tag | |
213 replacements_.insert(std::make_pair(DicomTag(0x0012, 0x0063), "Orthanc " ORTHANC_VERSION " - PS 3.15-2008 Table E.1-1")); | |
214 | |
215 // Set the PatientIdentityRemoved tag | |
216 replacements_.insert(std::make_pair(DicomTag(0x0012, 0x0062), "YES")); | |
217 | |
218 // (*) Choose a random patient name and ID | |
788 | 219 std::string patientId = FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Patient); |
786 | 220 replacements_[DICOM_TAG_PATIENT_ID] = patientId; |
221 replacements_[DICOM_TAG_PATIENT_NAME] = patientId; | |
222 } | |
223 | |
224 void DicomModification::Apply(ParsedDicomFile& toModify) | |
225 { | |
226 // Check the request | |
788 | 227 assert(ResourceType_Patient + 1 == ResourceType_Study && |
228 ResourceType_Study + 1 == ResourceType_Series && | |
229 ResourceType_Series + 1 == ResourceType_Instance); | |
786 | 230 |
231 if (IsRemoved(DICOM_TAG_PATIENT_ID) || | |
232 IsRemoved(DICOM_TAG_STUDY_INSTANCE_UID) || | |
233 IsRemoved(DICOM_TAG_SERIES_INSTANCE_UID) || | |
234 IsRemoved(DICOM_TAG_SOP_INSTANCE_UID)) | |
235 { | |
236 throw OrthancException(ErrorCode_BadRequest); | |
237 } | |
238 | |
788 | 239 if (level_ == ResourceType_Patient && !IsReplaced(DICOM_TAG_PATIENT_ID)) |
786 | 240 { |
241 throw OrthancException(ErrorCode_BadRequest); | |
242 } | |
243 | |
788 | 244 if (level_ > ResourceType_Patient && IsReplaced(DICOM_TAG_PATIENT_ID)) |
786 | 245 { |
246 throw OrthancException(ErrorCode_BadRequest); | |
247 } | |
248 | |
788 | 249 if (level_ > ResourceType_Study && IsReplaced(DICOM_TAG_STUDY_INSTANCE_UID)) |
786 | 250 { |
251 throw OrthancException(ErrorCode_BadRequest); | |
252 } | |
253 | |
788 | 254 if (level_ > ResourceType_Series && IsReplaced(DICOM_TAG_SERIES_INSTANCE_UID)) |
786 | 255 { |
256 throw OrthancException(ErrorCode_BadRequest); | |
257 } | |
258 | |
259 // (1) Remove the private tags, if need be | |
260 if (removePrivateTags_) | |
261 { | |
262 toModify.RemovePrivateTags(); | |
263 } | |
264 | |
265 // (2) Remove the tags specified by the user | |
266 for (Removals::const_iterator it = removals_.begin(); | |
267 it != removals_.end(); ++it) | |
268 { | |
269 toModify.Remove(*it); | |
270 } | |
271 | |
272 // (3) Replace the tags | |
273 for (Replacements::const_iterator it = replacements_.begin(); | |
274 it != replacements_.end(); ++it) | |
275 { | |
789 | 276 toModify.Replace(it->first, it->second, DicomReplaceMode_InsertIfAbsent); |
786 | 277 } |
278 | |
279 // (4) Update the DICOM identifiers | |
788 | 280 if (level_ <= ResourceType_Study) |
786 | 281 { |
788 | 282 MapDicomIdentifier(toModify, ResourceType_Study); |
786 | 283 } |
284 | |
788 | 285 if (level_ <= ResourceType_Series) |
786 | 286 { |
788 | 287 MapDicomIdentifier(toModify, ResourceType_Series); |
786 | 288 } |
289 | |
788 | 290 if (level_ <= ResourceType_Instance) // Always true |
786 | 291 { |
788 | 292 MapDicomIdentifier(toModify, ResourceType_Instance); |
786 | 293 } |
294 } | |
295 } |