comparison OrthancFramework/Sources/DicomNetworking/DicomFindAnswers.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/DicomNetworking/DicomFindAnswers.cpp@cc6ed76bba27
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "DicomFindAnswers.h"
36
37 #include "../DicomParsing/FromDcmtkBridge.h"
38 #include "../OrthancException.h"
39
40 #include <memory>
41 #include <dcmtk/dcmdata/dcfilefo.h>
42 #include <boost/noncopyable.hpp>
43
44
45 namespace Orthanc
46 {
47 void DicomFindAnswers::AddAnswerInternal(ParsedDicomFile* answer)
48 {
49 std::unique_ptr<ParsedDicomFile> protection(answer);
50
51 if (isWorklist_)
52 {
53 // These lines are necessary when serving worklists, otherwise
54 // Orthanc does not behave as "wlmscpfs"
55 protection->Remove(DICOM_TAG_MEDIA_STORAGE_SOP_INSTANCE_UID);
56 protection->Remove(DICOM_TAG_SOP_INSTANCE_UID);
57 }
58
59 protection->ChangeEncoding(encoding_);
60
61 answers_.push_back(protection.release());
62 }
63
64
65 DicomFindAnswers::DicomFindAnswers(bool isWorklist) :
66 encoding_(GetDefaultDicomEncoding()),
67 isWorklist_(isWorklist),
68 complete_(true)
69 {
70 }
71
72
73 void DicomFindAnswers::SetEncoding(Encoding encoding)
74 {
75 for (size_t i = 0; i < answers_.size(); i++)
76 {
77 assert(answers_[i] != NULL);
78 answers_[i]->ChangeEncoding(encoding);
79 }
80
81 encoding_ = encoding;
82 }
83
84
85 void DicomFindAnswers::SetWorklist(bool isWorklist)
86 {
87 if (answers_.empty())
88 {
89 isWorklist_ = isWorklist;
90 }
91 else
92 {
93 // This set of answers is not empty anymore, cannot change its type
94 throw OrthancException(ErrorCode_BadSequenceOfCalls);
95 }
96 }
97
98
99 void DicomFindAnswers::Clear()
100 {
101 for (size_t i = 0; i < answers_.size(); i++)
102 {
103 assert(answers_[i] != NULL);
104 delete answers_[i];
105 }
106
107 answers_.clear();
108 }
109
110
111 void DicomFindAnswers::Reserve(size_t size)
112 {
113 if (size > answers_.size())
114 {
115 answers_.reserve(size);
116 }
117 }
118
119
120 void DicomFindAnswers::Add(const DicomMap& map)
121 {
122 // We use the permissive mode to be tolerant wrt. invalid DICOM
123 // files that contain some tags with out-of-range values (such
124 // tags are removed from the answers)
125 AddAnswerInternal(new ParsedDicomFile(map, encoding_, true /* permissive */));
126 //"" /* no private creator */));
127 }
128
129
130 void DicomFindAnswers::Add(ParsedDicomFile& dicom)
131 {
132 AddAnswerInternal(dicom.Clone(true));
133 }
134
135 void DicomFindAnswers::Add(const void* dicom,
136 size_t size)
137 {
138 AddAnswerInternal(new ParsedDicomFile(dicom, size));
139 }
140
141
142 ParsedDicomFile& DicomFindAnswers::GetAnswer(size_t index) const
143 {
144 if (index < answers_.size())
145 {
146 return *answers_[index];
147 }
148 else
149 {
150 throw OrthancException(ErrorCode_ParameterOutOfRange);
151 }
152 }
153
154
155 DcmDataset* DicomFindAnswers::ExtractDcmDataset(size_t index) const
156 {
157 // As "DicomFindAnswers" stores its content using class
158 // "ParsedDicomFile" (that internally uses "DcmFileFormat" from
159 // DCMTK), the dataset can contain tags that are reserved if
160 // storing the media on the disk, notably tag
161 // "MediaStorageSOPClassUID" (0002,0002). In this function, we
162 // remove all those tags whose group is below 0x0008. The
163 // resulting data set is clean for emission in the C-FIND SCP.
164
165 // http://dicom.nema.org/medical/dicom/current/output/chtml/part04/sect_C.4.html#sect_C.4.1.1.3
166 // https://groups.google.com/d/msg/orthanc-users/D3kpPuX8yV0/_zgHOzkMEQAJ
167
168 DcmDataset& source = *GetAnswer(index).GetDcmtkObject().getDataset();
169
170 std::unique_ptr<DcmDataset> target(new DcmDataset);
171
172 for (unsigned long i = 0; i < source.card(); i++)
173 {
174 const DcmElement* element = source.getElement(i);
175 assert(element != NULL);
176
177 if (element != NULL &&
178 element->getTag().getGroup() >= 0x0008 &&
179 element->getTag().getElement() != 0x0000)
180 {
181 target->insert(dynamic_cast<DcmElement*>(element->clone()));
182 }
183 }
184
185 return target.release();
186 }
187
188
189 void DicomFindAnswers::ToJson(Json::Value& target,
190 size_t index,
191 bool simplify) const
192 {
193 DicomToJsonFormat format = (simplify ? DicomToJsonFormat_Human : DicomToJsonFormat_Full);
194 GetAnswer(index).DatasetToJson(target, format, DicomToJsonFlags_None, 0);
195 }
196
197
198 void DicomFindAnswers::ToJson(Json::Value& target,
199 bool simplify) const
200 {
201 target = Json::arrayValue;
202
203 for (size_t i = 0; i < GetSize(); i++)
204 {
205 Json::Value answer;
206 ToJson(answer, i, simplify);
207 target.append(answer);
208 }
209 }
210 }