forked from dcmjs-org/dcmjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag.js
150 lines (125 loc) · 4.05 KB
/
Tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { WriteBufferStream } from "./BufferStream.js";
import {
EXPLICIT_LITTLE_ENDIAN,
IMPLICIT_LITTLE_ENDIAN
} from "./constants/dicom";
import { DicomMessage } from "./DicomMessage.js";
import { ValueRepresentation } from "./ValueRepresentation.js";
function paddingLeft(paddingValue, string) {
return String(paddingValue + string).slice(-paddingValue.length);
}
class Tag {
constructor(value) {
this.value = value;
}
toString() {
return (
"(" +
paddingLeft("0000", this.group().toString(16).toUpperCase()) +
"," +
paddingLeft("0000", this.element().toString(16).toUpperCase()) +
")"
);
}
toCleanString() {
return (
paddingLeft("0000", this.group().toString(16).toUpperCase()) +
paddingLeft("0000", this.element().toString(16).toUpperCase())
);
}
is(t) {
return this.value == t;
}
group() {
return this.value >>> 16;
}
element() {
return this.value & 0xffff;
}
isPixelDataTag() {
return this.is(0x7fe00010);
}
isPrivateCreator() {
const group = this.group();
const element = this.element();
return group % 2 === 1 && element < 0x100 && element > 0x00;
}
static fromString(str) {
var group = parseInt(str.substring(0, 4), 16),
element = parseInt(str.substring(4), 16);
return Tag.fromNumbers(group, element);
}
static fromPString(str) {
var group = parseInt(str.substring(1, 5), 16),
element = parseInt(str.substring(6, 10), 16);
return Tag.fromNumbers(group, element);
}
static fromNumbers(group, element) {
return new Tag(((group << 16) | element) >>> 0);
}
static readTag(stream) {
var group = stream.readUint16(),
element = stream.readUint16();
return Tag.fromNumbers(group, element);
}
write(stream, vrType, values, syntax, writeOptions) {
var vr = ValueRepresentation.createByTypeString(vrType),
useSyntax = DicomMessage._normalizeSyntax(syntax);
var implicit = useSyntax == IMPLICIT_LITTLE_ENDIAN ? true : false,
isLittleEndian =
useSyntax == IMPLICIT_LITTLE_ENDIAN ||
useSyntax == EXPLICIT_LITTLE_ENDIAN
? true
: false,
isEncapsulated =
this.isPixelDataTag() && DicomMessage.isEncapsulated(syntax);
var oldEndian = stream.isLittleEndian;
stream.setEndian(isLittleEndian);
stream.writeUint16(this.group());
stream.writeUint16(this.element());
var tagStream = new WriteBufferStream(256),
valueLength;
tagStream.setEndian(isLittleEndian);
if (vrType == "OW" || vrType == "OB" || vrType == "UN") {
valueLength = vr.writeBytes(
tagStream,
values,
useSyntax,
isEncapsulated,
writeOptions
);
} else if (vrType == "SQ") {
valueLength = vr.writeBytes(
tagStream,
values,
useSyntax,
writeOptions
);
} else {
valueLength = vr.writeBytes(tagStream, values, writeOptions);
}
if (vrType == "SQ") {
valueLength = 0xffffffff;
}
var written = tagStream.size + 4;
if (implicit) {
stream.writeUint32(valueLength);
written += 4;
} else {
if (vr.isExplicit()) {
stream.writeAsciiString(vr.type);
stream.writeUint16(0);
stream.writeUint32(valueLength);
written += 8;
} else {
stream.writeAsciiString(vr.type);
stream.writeUint16(valueLength);
written += 4;
}
}
stream.concat(tagStream);
stream.setEndian(oldEndian);
return written;
}
}
export { Tag };