comparison OrthancFramework/Sources/DicomParsing/ParsedDicomDir.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/DicomParsing/ParsedDicomDir.cpp@2a170a8f1faf
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 "ParsedDicomDir.h"
36
37 #include "../Compatibility.h"
38 #include "../OrthancException.h"
39 #include "ParsedDicomFile.h"
40 #include "FromDcmtkBridge.h"
41
42 #include <dcmtk/dcmdata/dcdeftag.h>
43
44
45 namespace Orthanc
46 {
47 void ParsedDicomDir::Clear()
48 {
49 for (size_t i = 0; i < content_.size(); i++)
50 {
51 assert(content_[i] != NULL);
52 delete content_[i];
53 }
54 }
55
56
57 bool ParsedDicomDir::LookupIndexOfOffset(size_t& target,
58 unsigned int offset) const
59 {
60 if (offset == 0)
61 {
62 return false;
63 }
64
65 OffsetToIndex::const_iterator found = offsetToIndex_.find(offset);
66 if (found == offsetToIndex_.end())
67 {
68 // Error in the algorithm that computes the offsets
69 throw OrthancException(ErrorCode_InternalError);
70 }
71 else
72 {
73 target = found->second;
74 return true;
75 }
76 }
77
78
79 ParsedDicomDir::ParsedDicomDir(const std::string content)
80 {
81 ParsedDicomFile dicom(content);
82
83 DcmSequenceOfItems* sequence = NULL;
84 if (dicom.GetDcmtkObject().getDataset() == NULL ||
85 !dicom.GetDcmtkObject().getDataset()->findAndGetSequence(DCM_DirectoryRecordSequence, sequence).good() ||
86 sequence == NULL)
87 {
88 throw OrthancException(ErrorCode_BadFileFormat, "Not a DICOMDIR");
89 }
90
91 content_.resize(sequence->card());
92 nextOffsets_.resize(content_.size());
93 lowerOffsets_.resize(content_.size());
94
95 // Manually reconstruct the list of all the available offsets of
96 // "DcmItem", as "fStartPosition" is a protected member in DCMTK
97 // API
98 std::set<uint32_t> availableOffsets;
99 availableOffsets.insert(0);
100
101
102 for (unsigned long i = 0; i < sequence->card(); i++)
103 {
104 DcmItem* item = sequence->getItem(i);
105 if (item == NULL)
106 {
107 Clear();
108 throw OrthancException(ErrorCode_InternalError);
109 }
110
111 Uint32 next, lower;
112 if (!item->findAndGetUint32(DCM_OffsetOfTheNextDirectoryRecord, next).good() ||
113 !item->findAndGetUint32(DCM_OffsetOfReferencedLowerLevelDirectoryEntity, lower).good())
114 {
115 item->writeXML(std::cout);
116 throw OrthancException(ErrorCode_BadFileFormat,
117 "Missing offsets in DICOMDIR");
118 }
119
120 nextOffsets_[i] = next;
121 lowerOffsets_[i] = lower;
122
123 std::unique_ptr<DicomMap> entry(new DicomMap);
124 FromDcmtkBridge::ExtractDicomSummary(*entry, *item);
125
126 if (next != 0)
127 {
128 availableOffsets.insert(next);
129 }
130
131 if (lower != 0)
132 {
133 availableOffsets.insert(lower);
134 }
135
136 content_[i] = entry.release();
137 }
138
139 if (content_.size() != availableOffsets.size())
140 {
141 throw OrthancException(ErrorCode_BadFileFormat,
142 "Inconsistent offsets in DICOMDIR");
143 }
144
145 unsigned int index = 0;
146 for (std::set<uint32_t>::const_iterator it = availableOffsets.begin();
147 it != availableOffsets.end(); ++it)
148 {
149 offsetToIndex_[*it] = index;
150 index ++;
151 }
152 }
153
154
155 const DicomMap& ParsedDicomDir::GetItem(size_t i) const
156 {
157 if (i >= content_.size())
158 {
159 throw OrthancException(ErrorCode_ParameterOutOfRange);
160 }
161 else
162 {
163 assert(content_[i] != NULL);
164 return *content_[i];
165 }
166 }
167
168
169 bool ParsedDicomDir::LookupNext(size_t& target,
170 size_t index) const
171 {
172 if (index >= nextOffsets_.size())
173 {
174 throw OrthancException(ErrorCode_ParameterOutOfRange);
175 }
176 else
177 {
178 return LookupIndexOfOffset(target, nextOffsets_[index]);
179 }
180 }
181
182
183 bool ParsedDicomDir::LookupLower(size_t& target,
184 size_t index) const
185 {
186 if (index >= lowerOffsets_.size())
187 {
188 throw OrthancException(ErrorCode_ParameterOutOfRange);
189 }
190 else
191 {
192 return LookupIndexOfOffset(target, lowerOffsets_[index]);
193 }
194 }
195 }