annotate OrthancFramework/Sources/DicomFormat/DicomStreamReader.cpp @ 4275:d7a50b7b8466

Dynamically access and/or change the verbosity of logging categories with the REST API
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Nov 2020 14:41:27 +0100
parents 7461f98c23a0
children c5ca798b158a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Lesser General Public License for more details.
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * License along with this program. If not, see
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * <http://www.gnu.org/licenses/>.
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../PrecompiledHeaders.h"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "DicomStreamReader.h"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 namespace Orthanc
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
4221
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
30 static bool IsNormalizationNeeded(const std::string& source,
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
31 ValueRepresentation vr)
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
32 {
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
33 return (!source.empty() &&
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
34 (source[source.size() - 1] == ' ' ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
35 source[source.size() - 1] == '\0') &&
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
36 // Normalization only applies to string-based VR
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
37 (vr == ValueRepresentation_ApplicationEntity ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
38 vr == ValueRepresentation_AgeString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
39 vr == ValueRepresentation_CodeString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
40 vr == ValueRepresentation_DecimalString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
41 vr == ValueRepresentation_IntegerString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
42 vr == ValueRepresentation_LongString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
43 vr == ValueRepresentation_LongText ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
44 vr == ValueRepresentation_PersonName ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
45 vr == ValueRepresentation_ShortString ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
46 vr == ValueRepresentation_ShortText ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
47 vr == ValueRepresentation_UniqueIdentifier ||
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
48 vr == ValueRepresentation_UnlimitedText));
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
49 }
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
50
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
51
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
52 static void NormalizeValue(std::string& inplace,
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
53 ValueRepresentation vr)
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
54 {
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
55 if (IsNormalizationNeeded(inplace, vr))
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
56 {
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
57 assert(!inplace.empty());
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
58 inplace.resize(inplace.size() - 1);
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
59 }
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
60 }
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
61
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
62
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 static uint16_t ReadUnsignedInteger16(const char* dicom,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 bool littleEndian)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 const uint8_t* p = reinterpret_cast<const uint8_t*>(dicom);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if (littleEndian)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 return (static_cast<uint16_t>(p[0]) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 (static_cast<uint16_t>(p[1]) << 8));
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 return (static_cast<uint16_t>(p[1]) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 (static_cast<uint16_t>(p[0]) << 8));
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 static uint32_t ReadUnsignedInteger32(const char* dicom,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 bool littleEndian)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 const uint8_t* p = reinterpret_cast<const uint8_t*>(dicom);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if (littleEndian)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 return (static_cast<uint32_t>(p[0]) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 (static_cast<uint32_t>(p[1]) << 8) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 (static_cast<uint32_t>(p[2]) << 16) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 (static_cast<uint32_t>(p[3]) << 24));
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 return (static_cast<uint32_t>(p[3]) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 (static_cast<uint32_t>(p[2]) << 8) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 (static_cast<uint32_t>(p[1]) << 16) |
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 (static_cast<uint32_t>(p[0]) << 24));
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 static DicomTag ReadTag(const char* dicom,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 bool littleEndian)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 return DicomTag(ReadUnsignedInteger16(dicom, littleEndian),
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 ReadUnsignedInteger16(dicom + 2, littleEndian));
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 static bool IsShortExplicitTag(ValueRepresentation vr)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 /**
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 * Are we in the case of Table 7.1-2? "Data Element with
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 * Explicit VR of AE, AS, AT, CS, DA, DS, DT, FL, FD, IS, LO,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 * LT, PN, SH, SL, SS, ST, TM, UI, UL and US"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 * http://dicom.nema.org/medical/dicom/current/output/chtml/part05/chapter_7.html#sect_7.1.2
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 **/
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 return (vr == ValueRepresentation_ApplicationEntity /* AE */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 vr == ValueRepresentation_AgeString /* AS */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 vr == ValueRepresentation_AttributeTag /* AT */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 vr == ValueRepresentation_CodeString /* CS */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 vr == ValueRepresentation_Date /* DA */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 vr == ValueRepresentation_DecimalString /* DS */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 vr == ValueRepresentation_DateTime /* DT */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 vr == ValueRepresentation_FloatingPointSingle /* FL */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 vr == ValueRepresentation_FloatingPointDouble /* FD */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 vr == ValueRepresentation_IntegerString /* IS */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 vr == ValueRepresentation_LongString /* LO */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 vr == ValueRepresentation_LongText /* LT */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 vr == ValueRepresentation_PersonName /* PN */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 vr == ValueRepresentation_ShortString /* SH */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 vr == ValueRepresentation_SignedLong /* SL */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 vr == ValueRepresentation_SignedShort /* SS */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 vr == ValueRepresentation_ShortText /* ST */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 vr == ValueRepresentation_Time /* TM */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 vr == ValueRepresentation_UniqueIdentifier /* UI */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 vr == ValueRepresentation_UnsignedLong /* UL */ ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 vr == ValueRepresentation_UnsignedShort /* US */);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 bool DicomStreamReader::IsLittleEndian() const
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 return (transferSyntax_ != DicomTransferSyntax_BigEndianExplicit);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 void DicomStreamReader::HandlePreamble(IVisitor& visitor,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 const std::string& block)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 assert(block.size() == 144u);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 assert(reader_.GetProcessedBytes() == 144u);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 /**
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 * The "DICOM file meta information" is always encoded using
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 * "Explicit VR Little Endian Transfer Syntax"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 * http://dicom.nema.org/medical/dicom/current/output/chtml/part10/chapter_7.html
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 **/
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 if (block[128] != 'D' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 block[129] != 'I' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 block[130] != 'C' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 block[131] != 'M' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 ReadTag(block.c_str() + 132, true) != DicomTag(0x0002, 0x0000) ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 block[136] != 'U' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 block[137] != 'L' ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 ReadUnsignedInteger16(block.c_str() + 138, true) != 4)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 throw OrthancException(ErrorCode_BadFileFormat);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 uint32_t length = ReadUnsignedInteger32(block.c_str() + 140, true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 reader_.Schedule(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 state_ = State_MetaHeader;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
4221
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
178
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 void DicomStreamReader::HandleMetaHeader(IVisitor& visitor,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 const std::string& block)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 size_t pos = 0;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 const char* p = block.c_str();
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 bool hasTransferSyntax = false;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 while (pos + 8 <= block.size())
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 DicomTag tag = ReadTag(p + pos, true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 ValueRepresentation vr = StringToValueRepresentation(std::string(p + pos + 4, 2), true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if (IsShortExplicitTag(vr))
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 uint16_t length = ReadUnsignedInteger16(p + pos + 6, true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 std::string value;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 value.assign(p + pos + 8, length);
4221
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
199 NormalizeValue(value, vr);
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 if (tag.GetGroup() == 0x0002)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 visitor.VisitMetaHeaderTag(tag, vr, value);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 if (tag == DICOM_TAG_TRANSFER_SYNTAX_UID)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 if (LookupTransferSyntax(transferSyntax_, value))
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 hasTransferSyntax = true;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 throw OrthancException(ErrorCode_NotImplemented, "Unsupported transfer syntax: " + value);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 pos += length + 8;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 else if (pos + 12 <= block.size())
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 uint16_t reserved = ReadUnsignedInteger16(p + pos + 6, true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 if (reserved != 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 uint32_t length = ReadUnsignedInteger32(p + pos + 8, true);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 if (tag.GetGroup() == 0x0002)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
4221
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
232 std::string value;
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
233 value.assign(p + pos + 12, length);
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
234 NormalizeValue(value, vr);
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 visitor.VisitMetaHeaderTag(tag, vr, value);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 pos += length + 12;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 if (pos != block.size())
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 throw OrthancException(ErrorCode_BadFileFormat);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 if (!hasTransferSyntax)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 throw OrthancException(ErrorCode_BadFileFormat, "DICOM file meta-header without transfer syntax UID");
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 visitor.VisitTransferSyntax(transferSyntax_);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 void DicomStreamReader::HandleDatasetTag(const std::string& block,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 const DicomTag& untilTag)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 static const DicomTag DICOM_TAG_SEQUENCE_ITEM(0xfffe, 0xe000);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 static const DicomTag DICOM_TAG_SEQUENCE_DELIMITATION_ITEM(0xfffe, 0xe00d);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 static const DicomTag DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE(0xfffe, 0xe0dd);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 assert(block.size() == 8u);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 const bool littleEndian = IsLittleEndian();
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 DicomTag tag = ReadTag(block.c_str(), littleEndian);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 if (sequenceDepth_ == 0 &&
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 tag >= untilTag)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 state_ = State_Done;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 return;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 if (tag == DICOM_TAG_SEQUENCE_ITEM ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 // The special sequence items are encoded like "Implicit VR"
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 uint32_t length = ReadUnsignedInteger32(block.c_str() + 4, littleEndian);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 if (tag == DICOM_TAG_SEQUENCE_ITEM)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 if (length == 0xffffffffu)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 // Undefined length: Need to loop over the tags of the nested dataset
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 // Explicit length: Can skip the full sequence at once
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 reader_.Schedule(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 state_ = State_DatasetValue;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 else if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 if (length != 0 ||
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 sequenceDepth_ == 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 throw OrthancException(ErrorCode_BadFileFormat);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 sequenceDepth_ --;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 throw OrthancException(ErrorCode_InternalError);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 ValueRepresentation vr = ValueRepresentation_Unknown;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 if (transferSyntax_ == DicomTransferSyntax_LittleEndianImplicit)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 if (sequenceDepth_ == 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 danglingTag_ = tag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 danglingVR_ = vr;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 uint32_t length = ReadUnsignedInteger32(block.c_str() + 4, true /* little endian */);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 HandleDatasetExplicitLength(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 // This in an explicit transfer syntax
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 vr = StringToValueRepresentation(
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 std::string(block.c_str() + 4, 2), false /* ignore unknown VR */);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 if (vr == ValueRepresentation_Sequence)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 sequenceDepth_ ++;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 reader_.Schedule(4);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 state_ = State_SequenceExplicitLength;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 else if (IsShortExplicitTag(vr))
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 uint16_t length = ReadUnsignedInteger16(block.c_str() + 6, littleEndian);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 reader_.Schedule(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 state_ = State_DatasetValue;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 uint16_t reserved = ReadUnsignedInteger16(block.c_str() + 6, littleEndian);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 if (reserved != 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 throw OrthancException(ErrorCode_BadFileFormat);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 reader_.Schedule(4);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 state_ = State_DatasetExplicitLength;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 if (sequenceDepth_ == 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 danglingTag_ = tag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 danglingVR_ = vr;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 void DicomStreamReader::HandleDatasetExplicitLength(uint32_t length)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 if (length == 0xffffffffu)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 /**
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 * This is the case of pixel data with compressed transfer
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 * syntaxes. Schedule the reading of the first tag of the
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 * nested dataset.
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 * http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_7.5.html
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 **/
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 sequenceDepth_ ++;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 reader_.Schedule(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 state_ = State_DatasetValue;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 void DicomStreamReader::HandleDatasetExplicitLength(const std::string& block)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 assert(block.size() == 4);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian());
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 HandleDatasetExplicitLength(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 void DicomStreamReader::HandleSequenceExplicitLength(const std::string& block)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 assert(block.size() == 4);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian());
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 if (length == 0xffffffffu)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 reader_.Schedule(length);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 state_ = State_SequenceExplicitValue;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 void DicomStreamReader::HandleSequenceExplicitValue()
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 if (sequenceDepth_ == 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 throw OrthancException(ErrorCode_InternalError);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 sequenceDepth_ --;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 void DicomStreamReader::HandleDatasetValue(IVisitor& visitor,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 const std::string& block)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 if (sequenceDepth_ == 0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 bool c;
4221
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
448
e4c0218b6b23 better normalization in stream reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4220
diff changeset
449 if (IsNormalizationNeeded(block, danglingVR_))
4220
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 std::string s(block.begin(), block.end() - 1);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 c = visitor.VisitDatasetTag(danglingTag_, danglingVR_, s, IsLittleEndian());
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 c = visitor.VisitDatasetTag(danglingTag_, danglingVR_, block, IsLittleEndian());
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 if (!c)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 state_ = State_Done;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 return;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 reader_.Schedule(8);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 state_ = State_DatasetTag;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 DicomStreamReader::DicomStreamReader(std::istream& stream) :
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 reader_(stream),
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 state_(State_Preamble),
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 transferSyntax_(DicomTransferSyntax_LittleEndianImplicit), // Dummy
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 danglingTag_(0x0000, 0x0000), // Dummy
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 danglingVR_(ValueRepresentation_Unknown), // Dummy
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 sequenceDepth_(0)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 reader_.Schedule(128 /* empty header */ +
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 4 /* "DICM" magic value */ +
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 4 /* (0x0002, 0x0000) tag */ +
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 2 /* value representation of (0x0002, 0x0000) == "UL" */ +
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 2 /* length of "UL" value == 4 */ +
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 4 /* actual length of the meta-header */);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 void DicomStreamReader::Consume(IVisitor& visitor,
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 const DicomTag& untilTag)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 while (state_ != State_Done)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 std::string block;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 if (reader_.Read(block))
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 switch (state_)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 case State_Preamble:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 HandlePreamble(visitor, block);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 case State_MetaHeader:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 HandleMetaHeader(visitor, block);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 case State_DatasetTag:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 HandleDatasetTag(block, untilTag);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 case State_DatasetExplicitLength:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 HandleDatasetExplicitLength(block);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 case State_SequenceExplicitLength:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 HandleSequenceExplicitLength(block);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 case State_SequenceExplicitValue:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 HandleSequenceExplicitValue();
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 case State_DatasetValue:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 HandleDatasetValue(visitor, block);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 break;
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 default:
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 throw OrthancException(ErrorCode_InternalError);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 else
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 return; // No more data in the stream
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 void DicomStreamReader::Consume(IVisitor& visitor)
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 {
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 DicomTag untilTag(0xffff, 0xffff);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 Consume(visitor, untilTag);
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 }
92a21efa5c96 reorganization of DicomStreamReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 }