forked from grailbio/go-dicom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_elements.go
executable file
·194 lines (177 loc) · 4.6 KB
/
print_elements.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
package main
// Print the contents of a file in a format used by pydicom. Used to ensure
// that pydicom and go-dicom parses files in the same way.
import (
"flag"
"fmt"
"io/ioutil"
"log"
"sort"
"strings"
"github.com/BTsykaniuk/go-dicom"
"github.com/BTsykaniuk/go-dicom/dicomtag"
"github.com/BTsykaniuk/go-dicom/dicomuid"
)
var (
printMetadata = flag.Bool("print-metadata", true, "Print image metadata")
)
// Sorter
type elemSorter struct {
elems []*dicom.Element
}
func (e *elemSorter) Len() int {
return len(e.elems)
}
func (e *elemSorter) Swap(i, j int) {
tmp := e.elems[i]
e.elems[i] = e.elems[j]
e.elems[j] = tmp
}
func (e *elemSorter) Less(i, j int) bool {
elemi := e.elems[i]
elemj := e.elems[j]
if elemi.Tag.Group < elemj.Tag.Group {
return true
}
if elemi.Tag.Group > elemj.Tag.Group {
return false
}
return elemi.Tag.Element < elemj.Tag.Element
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
log.Panic("print_elements_test <dicomfile>")
}
path := flag.Arg(0)
bytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
data, err := dicom.ReadDataSetInBytes(bytes, dicom.ReadOptions{})
if err != nil {
panic(err)
}
printElements(data.Elements, 0)
}
func printScalar(vr string, i interface{}, indent int) string {
var s string
switch v := i.(type) {
case float32:
s = fmt.Sprintf("%.4f", v)
case float64:
s = fmt.Sprintf("%.4f", v)
case string:
if vr == "UI" {
// Resolve UID
e, err := dicomuid.Lookup(v)
if err == nil {
v = e.Name
}
}
s = fmt.Sprintf("%s", v)
case dicomtag.Tag:
return v.String()
default:
s = fmt.Sprintf("%v", i)
}
return s
}
func printTag(tag dicomtag.Tag) string {
return fmt.Sprintf("(%04x, %04x)", tag.Group, tag.Element)
}
func printElement(elem *dicom.Element, indent int) {
if elem.Tag.Group == 2 {
// Don't print the meta elements found in the beginning of the
// file. Pydicom doesn't for some reason.
return
}
fmt.Printf("%s%s %s:", strings.Repeat(" ", indent*2), printTag(elem.Tag), elem.VR)
if elem.Tag == dicomtag.PixelData {
// PixelData encoding differs between godicom and pydicom. Skip
// for now. TODO(saito) fix.
fmt.Print(" [omitted]\n")
} else if elem.VR == "OW" || elem.VR == "OB" || elem.VR == "OD" || elem.VR == "OF" {
if len(elem.Value) != 1 {
fmt.Printf(" [%d values]\n", len(elem.Value))
} else if v, ok := elem.Value[0].([]byte); ok {
fmt.Printf(" %dB\n", len(v))
} else {
v := elem.Value[0].(string)
fmt.Printf(" %dB\n", len(v))
}
} else if elem.VR == "LT" {
// pydicom trims one (but not more) trailing space from the
// string. Whereas go-dicom doesn't trim anything. The spec
// says an impl "*may* trim trailing space*s* from the string".
// So the behavior of pydicom is a bit strange, but follows the
// wording.
v := elem.Value[0].(string)
n := len(v)
if strings.HasSuffix(v, " ") {
n--
}
fmt.Printf(" %dB\n", n)
} else if elem.VR == "SQ" {
var childElems []*dicom.Element
if len(elem.Value) == 1 {
// If SQ contains one Item, unwrap the item.
items := elem.Value[0].(*dicom.Element)
if items.Tag != dicomtag.Item {
log.Panicf("A SQ item must be of type Item, but found %v", items)
}
for _, item := range items.Value {
childElems = append(childElems, item.(*dicom.Element))
}
} else {
for _, v := range elem.Value {
child := v.(*dicom.Element)
if child.Tag != dicomtag.Item {
log.Panicf("A SQ item must be of type Item, but found %v", child)
}
childElems = append(childElems, child)
}
}
fmt.Print("\n")
printElements(childElems, indent+1)
} else if elem.VR == "NA" {
var childElems []*dicom.Element
if len(elem.Value) == 1 {
items := elem.Value[0].(*dicom.Element)
for _, item := range items.Value {
childElems = append(childElems, item.(*dicom.Element))
}
} else {
for _, v := range elem.Value {
child := v.(*dicom.Element)
childElems = append(childElems, child)
}
}
fmt.Print("\n")
printElements(childElems, indent+1)
} else { // Other scalar value
if len(elem.Value) == 0 {
fmt.Print("\n")
} else if len(elem.Value) == 1 {
fmt.Printf(" %s\n", printScalar(elem.VR, elem.Value[0], 1))
} else {
fmt.Print(" [")
for i, value := range elem.Value {
if i > 0 {
fmt.Print(", ")
}
// Follow the pydicom's printing format. It
// encloses the value in quotes only at the
// toplevel.
fmt.Print(printScalar(elem.VR, value, indent))
}
fmt.Print("]\n")
}
}
}
func printElements(elems []*dicom.Element, indent int) {
sort.Sort(&elemSorter{elems: elems})
for _, elem := range elems {
printElement(elem, indent)
}
}