This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
/
writer.go
290 lines (249 loc) · 8.77 KB
/
writer.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dicom
import (
"fmt"
"io"
"math"
)
// DataElementWriter writes DataElements one at a time
type DataElementWriter interface {
WriteElement(element *DataElement) error
}
var errExpectedMetaHeader = fmt.Errorf("expected header to only contain file meta elements, " +
"use DataSet.MetaElements to filter DataSet")
// NewDataElementWriter writes the DICOM preamble, signature, and meta header to w and returns a
// DataElementWriter that writes DataElements in the transfer syntax specified by the header.
// The options are applied in the order given to all DataElements including File Meta Elements
// before being written to w.
func NewDataElementWriter(w io.Writer, header *DataSet, opts ...ConstructOption) (DataElementWriter, error) {
if !header.isMetaHeader() {
return nil, errExpectedMetaHeader
}
syntax, err := header.transferSyntax()
if err != nil {
return nil, fmt.Errorf("getting transfer syntax from header: %v", err)
}
if syntax == deflatedExplicitVRLittleEndian {
return nil, fmt.Errorf("writing in the deflated syntax is not supported yet")
}
dw := &dcmWriter{w}
if err := writeDicomSignature(dw); err != nil {
return nil, err
}
// Process meta header elements before re-calculating the FileMetaInformationGroupLength in case
// an option modifies the length of a DataElement.
for tag, element := range header.Elements {
element, err := processElementForConstruct(element, explicitVRLittleEndian, opts...)
if err != nil {
return nil, fmt.Errorf("processing element: %v", err)
}
header.Elements[tag] = element
}
// The FileMetaInformationGroupLength element is a critical component of the Meta Header. It
// stores how long the meta header is. Thus, we need to re-calculate it properly.
metaGroupLengthElement, err := createMetaGroupLengthElement(header)
if err != nil {
return nil, fmt.Errorf("creating meta group length element: %v", err)
}
header.Elements[FileMetaInformationGroupLengthTag] = metaGroupLengthElement
// Meta elements are always written in the Explicit VR Little Endian syntax in ascending order.
for _, element := range header.SortedElements() {
if err := writeDataElement(dw, explicitVRLittleEndian, element); err != nil {
return nil, fmt.Errorf("writing data element: %v", err)
}
}
return &dataElementWriter{dw, syntax, opts}, nil
}
type dataElementWriter struct {
dw *dcmWriter
syntax transferSyntax
opts []ConstructOption
}
func (dew *dataElementWriter) WriteElement(element *DataElement) error {
element, err := processElementForConstruct(element, dew.syntax, dew.opts...)
if err != nil {
return err
}
return writeDataElement(dew.dw, dew.syntax, element)
}
func writeDicomSignature(dw *dcmWriter) error {
if err := dw.Bytes(make([]byte, 128)); err != nil {
return fmt.Errorf("writing DICOM preamble: %v", err)
}
if err := dw.String("DICM"); err != nil {
return fmt.Errorf("writing DICOM signature: %v", err)
}
return nil
}
func createMetaGroupLengthElement(header *DataSet) (*DataElement, error) {
// Please refer to the DICOM Standard Part 10 for information on the File Meta Information Group
// Length. http://dicom.nema.org/medical/dicom/current/output/html/part10.html#sect_7.1
size := uint32(0)
for _, element := range header.Elements {
if element.Tag == FileMetaInformationGroupLengthTag {
// The FileMetaGroupLength byte count excludes itself from the calculation.
continue
}
size += explicitVRLittleEndian.elementSize(element.VR, element.ValueLength)
}
return &DataElement{
Tag: FileMetaInformationGroupLengthTag,
VR: FileMetaInformationGroupLengthTag.DictionaryVR(),
ValueField: []uint32{size},
ValueLength: 4, // 4bytes = sizeof uint32
}, nil
}
func processElementForConstruct(element *DataElement, syntax transferSyntax, opts ...ConstructOption) (*DataElement, error) {
element, err := applyConstructOptions(element, syntax, opts...)
if err != nil {
return nil, fmt.Errorf("applying construct options: %v", err)
}
if seq, ok := element.ValueField.(*Sequence); ok {
processedSeq, err := processSequenceForConstruct(seq, syntax, opts...)
if err != nil {
return nil, fmt.Errorf("processing sequence: %v", err)
}
element.ValueField = processedSeq
}
return element, nil
}
func applyConstructOptions(element *DataElement, syntax transferSyntax, opts ...ConstructOption) (*DataElement, error) {
var err error
for i, opt := range opts {
element, err = opt.transform(element)
if err != nil {
return nil, fmt.Errorf("applying option %v: %v", i, err)
}
}
// As documented in NewConstructOption, after a transforms are applied, the length is
// re-calculated and VRs added from the Data Dictionary if nil
vr := element.VR
if vr == nil {
vr = element.Tag.DictionaryVR()
}
length, err := calculateElementLength(element, syntax)
if err != nil {
return nil, fmt.Errorf("calculating element length: %v", err)
}
return &DataElement{element.Tag, vr, element.ValueField, length}, nil
}
func calculateElementLength(element *DataElement, syntax transferSyntax) (uint32, error) {
if element.ValueLength == UndefinedLength {
return UndefinedLength, nil
}
numBytes := int64(0)
switch v := element.ValueField.(type) {
case []string:
for _, s := range v {
numBytes += int64(len(s))
}
if len(v) > 0 { // requires "/" delimiter
numBytes += int64(len(v)) - 1
}
case []int16:
numBytes = int64(len(v)) * 2
case []uint16:
numBytes = int64(len(v)) * 2
case []int32:
numBytes = int64(len(v)) * 4
case []uint32:
numBytes = int64(len(v)) * 4
case []float32:
numBytes = int64(len(v)) * 4
case []float64:
numBytes = int64(len(v)) * 8
case *Sequence:
seqLen, err := calculateSequenceLength(v, syntax)
if err != nil {
return 0, fmt.Errorf("calculating sequence length: %v", err)
}
numBytes = int64(seqLen)
case SequenceIterator:
numBytes = UndefinedLength // TODO support explicit length sequence construction
case BulkDataBuffer:
numBytes = v.Length()
if numBytes < 0 {
return 0, fmt.Errorf("explicit length must be provided to write BulkDataBuffer")
}
case BulkDataIterator:
numBytes = v.Length()
if numBytes < 0 {
return 0, fmt.Errorf("explicit length must be provided to write BulkDataIterator")
}
default:
return 0, fmt.Errorf("unexpected ValueField type %T", element.ValueField)
}
if numBytes >= math.MaxUint32 {
return UndefinedLength, nil
}
if numBytes%2 != 0 {
numBytes++
}
return uint32(numBytes), nil
}
func calculateSequenceLength(seq *Sequence, syntax transferSyntax) (uint32, error) {
size := int64(0)
for _, item := range seq.Items {
itemLen, err := calculateDataSetLength(item, syntax)
if err != nil {
return 0, fmt.Errorf("calculating sequence item length: %v", err)
}
item.Length = itemLen
size += tagSize + 4 /*32 bit length*/ + int64(itemLen)
}
if size > math.MaxUint32 {
return UndefinedLength, nil
}
return uint32(size), nil
}
func calculateDataSetLength(item *DataSet, syntax transferSyntax) (uint32, error) {
if item.Length >= UndefinedLength {
return UndefinedLength, nil
}
size := int64(0)
for _, elem := range item.Elements {
elemLength, err := calculateElementLength(elem, syntax)
if err != nil {
return 0, fmt.Errorf("calculating data set element length: %v", err)
}
size += int64(syntax.elementSize(elem.VR, elemLength))
}
if size > math.MaxUint32 {
return UndefinedLength, nil
}
return uint32(size), nil
}
func processSequenceForConstruct(sequence *Sequence, syntax transferSyntax, opts ...ConstructOption) (*Sequence, error) {
ret := &Sequence{Items: []*DataSet{}}
for _, item := range sequence.Items {
processedItem, err := processItemForConstruct(item, syntax, opts...)
if err != nil {
return nil, fmt.Errorf("processing sequence item: %v", err)
}
ret.append(processedItem)
}
return ret, nil
}
func processItemForConstruct(dataSet *DataSet, syntax transferSyntax, opts ...ConstructOption) (*DataSet, error) {
ret := &DataSet{Elements: map[DataElementTag]*DataElement{}, Length: dataSet.Length}
for _, element := range dataSet.SortedElements() {
processedElement, err := processElementForConstruct(element, syntax, opts...)
if err != nil {
return nil, fmt.Errorf("processing element %s", element.Tag)
}
ret.Elements[processedElement.Tag] = processedElement
}
return ret, nil
}