comparison Core/DicomParsing/ParsedDicomDir.cpp @ 3554:8fe89c2ea593

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