PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
vr_type.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#ifndef PACS_ENCODING_VR_TYPE_HPP
12#define PACS_ENCODING_VR_TYPE_HPP
13
14#include <cstdint>
15#include <optional>
16#include <string_view>
17
18namespace kcenon::pacs::encoding {
19
29enum class vr_type : uint16_t {
30 // String VRs
31 AE = 0x4145,
32 AS = 0x4153,
33 CS = 0x4353,
34 DA = 0x4441,
35 DS = 0x4453,
36 DT = 0x4454,
37 IS = 0x4953,
38 LO = 0x4C4F,
39 LT = 0x4C54,
40 PN = 0x504E,
41 SH = 0x5348,
42 ST = 0x5354,
43 TM = 0x544D,
44 UC = 0x5543,
45 UI = 0x5549,
46 UR = 0x5552,
47 UT = 0x5554,
48
49 // Numeric VRs (binary encoded)
50 FL = 0x464C,
51 FD = 0x4644,
52 SL = 0x534C,
53 SS = 0x5353,
54 UL = 0x554C,
55 US = 0x5553,
56
57 // Binary VRs (raw bytes)
58 OB = 0x4F42,
59 OD = 0x4F44,
60 OF = 0x4F46,
61 OL = 0x4F4C,
62 OV = 0x4F56,
63 OW = 0x4F57,
64 UN = 0x554E,
65
66 // Special VRs
67 AT = 0x4154,
68 SQ = 0x5351,
69 SV = 0x5356,
70 UV = 0x5556,
71};
72
75
83[[nodiscard]] constexpr std::string_view to_string(vr_type vr) noexcept {
84 switch (vr) {
85 // String VRs
86 case vr_type::AE: return "AE";
87 case vr_type::AS: return "AS";
88 case vr_type::CS: return "CS";
89 case vr_type::DA: return "DA";
90 case vr_type::DS: return "DS";
91 case vr_type::DT: return "DT";
92 case vr_type::IS: return "IS";
93 case vr_type::LO: return "LO";
94 case vr_type::LT: return "LT";
95 case vr_type::PN: return "PN";
96 case vr_type::SH: return "SH";
97 case vr_type::ST: return "ST";
98 case vr_type::TM: return "TM";
99 case vr_type::UC: return "UC";
100 case vr_type::UI: return "UI";
101 case vr_type::UR: return "UR";
102 case vr_type::UT: return "UT";
103 // Numeric VRs
104 case vr_type::FL: return "FL";
105 case vr_type::FD: return "FD";
106 case vr_type::SL: return "SL";
107 case vr_type::SS: return "SS";
108 case vr_type::UL: return "UL";
109 case vr_type::US: return "US";
110 // Binary VRs
111 case vr_type::OB: return "OB";
112 case vr_type::OD: return "OD";
113 case vr_type::OF: return "OF";
114 case vr_type::OL: return "OL";
115 case vr_type::OV: return "OV";
116 case vr_type::OW: return "OW";
117 case vr_type::UN: return "UN";
118 // Special VRs
119 case vr_type::AT: return "AT";
120 case vr_type::SQ: return "SQ";
121 case vr_type::SV: return "SV";
122 case vr_type::UV: return "UV";
123 default: return "??";
124 }
125}
126
132[[nodiscard]] constexpr std::optional<vr_type> from_string(std::string_view str) noexcept {
133 if (str.size() != 2) {
134 return std::nullopt;
135 }
136
137 // Convert two ASCII chars to uint16_t (big-endian: first char is high byte)
138 const auto code = static_cast<uint16_t>(
139 (static_cast<uint16_t>(static_cast<unsigned char>(str[0])) << 8) |
140 static_cast<uint16_t>(static_cast<unsigned char>(str[1]))
141 );
142
143 // Validate by checking if the code corresponds to a known VR
144 switch (static_cast<vr_type>(code)) {
145 case vr_type::AE: case vr_type::AS: case vr_type::AT:
146 case vr_type::CS: case vr_type::DA: case vr_type::DS:
147 case vr_type::DT: case vr_type::FD: case vr_type::FL:
148 case vr_type::IS: case vr_type::LO: case vr_type::LT:
149 case vr_type::OB: case vr_type::OD: case vr_type::OF:
150 case vr_type::OL: case vr_type::OV: case vr_type::OW:
151 case vr_type::PN: case vr_type::SH: case vr_type::SL:
152 case vr_type::SQ: case vr_type::SS: case vr_type::ST:
153 case vr_type::SV: case vr_type::TM: case vr_type::UC:
154 case vr_type::UI: case vr_type::UL: case vr_type::UN:
155 case vr_type::UR: case vr_type::US: case vr_type::UT:
156 case vr_type::UV:
157 return static_cast<vr_type>(code);
158 default:
159 return std::nullopt;
160 }
161}
162
164
167
175[[nodiscard]] constexpr bool is_string_vr(vr_type vr) noexcept {
176 switch (vr) {
177 case vr_type::AE: case vr_type::AS: case vr_type::CS:
178 case vr_type::DA: case vr_type::DS: case vr_type::DT:
179 case vr_type::IS: case vr_type::LO: case vr_type::LT:
180 case vr_type::PN: case vr_type::SH: case vr_type::ST:
181 case vr_type::TM: case vr_type::UC: case vr_type::UI:
182 case vr_type::UR: case vr_type::UT:
183 return true;
184 default:
185 return false;
186 }
187}
188
196[[nodiscard]] constexpr bool is_binary_vr(vr_type vr) noexcept {
197 switch (vr) {
198 case vr_type::OB: case vr_type::OD: case vr_type::OF:
199 case vr_type::OL: case vr_type::OV: case vr_type::OW:
200 case vr_type::UN:
201 return true;
202 default:
203 return false;
204 }
205}
206
214[[nodiscard]] constexpr bool is_numeric_vr(vr_type vr) noexcept {
215 switch (vr) {
216 case vr_type::FL: case vr_type::FD:
217 case vr_type::SL: case vr_type::SS: case vr_type::SV:
218 case vr_type::UL: case vr_type::US: case vr_type::UV:
219 return true;
220 default:
221 return false;
222 }
223}
224
235[[nodiscard]] constexpr bool has_explicit_32bit_length(vr_type vr) noexcept {
236 switch (vr) {
237 case vr_type::OB: case vr_type::OD: case vr_type::OF:
238 case vr_type::OL: case vr_type::OV: case vr_type::OW:
239 case vr_type::SQ: case vr_type::SV: case vr_type::UC:
240 case vr_type::UN: case vr_type::UR: case vr_type::UT:
241 case vr_type::UV:
242 return true;
243 default:
244 return false;
245 }
246}
247
249
252
260[[nodiscard]] constexpr std::size_t fixed_length(vr_type vr) noexcept {
261 switch (vr) {
262 case vr_type::AT: return 4; // Attribute Tag
263 case vr_type::FL: return 4; // Float
264 case vr_type::FD: return 8; // Double
265 case vr_type::SL: return 4; // Signed Long
266 case vr_type::SS: return 2; // Signed Short
267 case vr_type::SV: return 8; // Signed 64-bit
268 case vr_type::UL: return 4; // Unsigned Long
269 case vr_type::US: return 2; // Unsigned Short
270 case vr_type::UV: return 8; // Unsigned 64-bit
271 default: return 0; // Variable length
272 }
273}
274
280[[nodiscard]] constexpr bool is_fixed_length(vr_type vr) noexcept {
281 return fixed_length(vr) > 0;
282}
283
292[[nodiscard]] constexpr char padding_char(vr_type vr) noexcept {
293 if (vr == vr_type::UI) {
294 return '\0'; // UI uses null padding
295 }
296 if (is_string_vr(vr)) {
297 return ' '; // Other string VRs use space padding
298 }
299 return '\0'; // Binary VRs use null padding if needed
300}
301
303
304} // namespace kcenon::pacs::encoding
305
306#endif // PACS_ENCODING_VR_TYPE_HPP
constexpr std::size_t fixed_length(vr_type vr) noexcept
Gets the fixed size of a VR if applicable.
Definition vr_type.h:260
constexpr char padding_char(vr_type vr) noexcept
Gets the padding character for a VR.
Definition vr_type.h:292
constexpr bool is_numeric_vr(vr_type vr) noexcept
Checks if a VR is a numeric type.
Definition vr_type.h:214
constexpr bool is_binary_vr(vr_type vr) noexcept
Checks if a VR is a binary/raw byte type.
Definition vr_type.h:196
vr_type
DICOM Value Representation (VR) types.
Definition vr_type.h:29
@ OB
Other Byte (variable length)
@ DA
Date (8 chars, format: YYYYMMDD)
@ UN
Unknown (variable length)
@ IS
Integer String (12 chars max)
@ SQ
Sequence of Items (undefined length)
@ UR
Universal Resource Identifier (2^32-2 max)
@ OV
Other 64-bit Very Long (variable length)
@ LO
Long String (64 chars max)
@ DS
Decimal String (16 chars max)
@ DT
Date Time (26 chars max)
@ UL
Unsigned Long (4 bytes)
@ UC
Unlimited Characters (2^32-2 max)
@ UI
Unique Identifier (64 chars max)
@ SL
Signed Long (4 bytes)
@ US
Unsigned Short (2 bytes)
@ OD
Other Double (variable length)
@ PN
Person Name (64 chars max per component group)
@ OF
Other Float (variable length)
@ UT
Unlimited Text (2^32-2 max)
@ CS
Code String (16 chars max, uppercase + digits + space + underscore)
@ OW
Other Word (variable length)
@ AS
Age String (4 chars, format: nnnD/W/M/Y)
@ FD
Floating Point Double (8 bytes)
@ FL
Floating Point Single (4 bytes)
@ TM
Time (14 chars max, format: HHMMSS.FFFFFF)
@ OL
Other Long (variable length)
@ LT
Long Text (10240 chars max)
@ SV
Signed 64-bit Very Long (8 bytes)
@ SS
Signed Short (2 bytes)
@ UV
Unsigned 64-bit Very Long (8 bytes)
@ AE
Application Entity (16 chars max)
@ SH
Short String (16 chars max)
@ ST
Short Text (1024 chars max)
@ AT
Attribute Tag (4 bytes)
constexpr bool is_string_vr(vr_type vr) noexcept
Checks if a VR is a string type.
Definition vr_type.h:175
constexpr std::optional< vr_type > from_string(std::string_view str) noexcept
Parses a two-character string to a vr_type.
Definition vr_type.h:132
constexpr bool has_explicit_32bit_length(vr_type vr) noexcept
Checks if a VR requires 32-bit length field in Explicit VR encoding.
Definition vr_type.h:235
constexpr std::string_view to_string(vr_type vr) noexcept
Converts a vr_type to its two-character string representation.
Definition vr_type.h:83
constexpr bool is_fixed_length(vr_type vr) noexcept
Checks if a VR has a fixed length.
Definition vr_type.h:280
std::string_view code
vr_encoding vr