This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
forked from gillesdemey/go-dicom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdicom_test.go
executable file
·139 lines (128 loc) · 3.81 KB
/
dicom_test.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
package dicom_test
import (
"os"
"testing"
"github.com/yasushi-saito/go-dicom"
"github.com/yasushi-saito/go-dicom/dicomuid"
"v.io/x/lib/vlog"
)
func mustReadFile(path string, options dicom.ReadOptions) *dicom.DataSet {
data, err := dicom.ReadDataSetFromFile(path, options)
if err != nil {
vlog.Fatalf("%s: failed to read: %v", path, err)
}
return data
}
func TestAllFiles(t *testing.T) {
dir, err := os.Open("examples")
if err != nil {
panic(err)
}
names, err := dir.Readdirnames(0)
if err != nil {
panic(err)
}
for _, name := range names {
vlog.Infof("Reading %s", name)
_ = mustReadFile("examples/"+name, dicom.ReadOptions{})
}
}
func testWriteFile(t *testing.T, dcmPath, transferSyntaxUID string) {
data := mustReadFile(dcmPath, dicom.ReadOptions{})
dstPath := "/tmp/test.dcm"
out, err := os.Create(dstPath)
if err != nil {
t.Fatal(err)
}
for i := range data.Elements {
if data.Elements[i].Tag == dicom.TagTransferSyntaxUID {
newElem := dicom.MustNewElement(dicom.TagTransferSyntaxUID, transferSyntaxUID)
vlog.Infof("Setting transfer syntax UID from %v to %v",
data.Elements[i].MustGetString(), newElem.MustGetString())
data.Elements[i] = newElem
}
}
err = dicom.WriteDataSet(out, data)
if err != nil {
t.Fatal(err)
}
data2 := mustReadFile(dstPath, dicom.ReadOptions{})
if len(data.Elements) != len(data2.Elements) {
t.Errorf("Wrong # of elements: %v %v", len(data.Elements), len(data2.Elements))
for _, elem := range data.Elements {
if _, err := data2.FindElementByTag(elem.Tag); err != nil {
t.Errorf("Tag %v found in org, but not in new", dicom.TagString(elem.Tag))
}
}
for _, elem := range data2.Elements {
if _, err := data.FindElementByTag(elem.Tag); err != nil {
t.Errorf("Tag %v found in new, but not in org", dicom.TagString(elem.Tag))
}
}
}
for _, elem := range data.Elements {
elem2, err := data2.FindElementByTag(elem.Tag)
if err != nil {
t.Error(err)
continue
}
if elem.Tag == dicom.TagFileMetaInformationGroupLength {
// This element is expected to change when the file is transcoded.
continue
}
if elem.String() != elem2.String() {
t.Fatalf("Elem mismatch: %v %v", elem.String(), elem2.String())
}
}
// TODO(saito) Fix below.
//if !reflect.DeepEqual(data, data2) {
// t.Error("Files aren't equal")
//}
}
func TestWriteFile(t *testing.T) {
// path := "examples/IM-0001-0001.dcm"
//testWriteFile(t, "examples/CT-MONO2-16-ort.dcm", dicomuid.ExplicitVRBigEndian)
//testWriteFile(t, "examples/CT-MONO2-16-ort.dcm", dicomuid.ImplicitVRLittleEndian)
testWriteFile(t, "examples/CT-MONO2-16-ort.dcm", dicomuid.ExplicitVRLittleEndian)
}
func TestReadDataSet(t *testing.T) {
data := mustReadFile("examples/IM-0001-0001.dcm", dicom.ReadOptions{})
elem, err := data.FindElementByName("PatientName")
if err != nil {
t.Error(err)
}
if elem.MustGetString() != "TOUTATIX" {
t.Errorf("Incorrect patient name: %s", elem)
}
elem, err = data.FindElementByName("TransferSyntaxUID")
if err != nil {
t.Error(err)
}
if elem.MustGetString() != "1.2.840.10008.1.2.4.91" {
t.Errorf("Incorrect TransferSyntaxUID: %s", elem)
}
if l := len(data.Elements); l != 98 {
t.Errorf("Error parsing DICOM file, wrong number of elements: %d", l)
}
elem, err = data.FindElementByTag(dicom.TagPixelData)
if err != nil {
t.Error(err)
}
}
// Test ReadOptions.DropPixelData.
func TestDropPixelData(t *testing.T) {
data := mustReadFile("examples/IM-0001-0001.dcm", dicom.ReadOptions{DropPixelData: true})
_, err := data.FindElementByTag(dicom.TagPatientName)
if err != nil {
t.Error(err)
}
_, err = data.FindElementByTag(dicom.TagPixelData)
if err == nil {
t.Errorf("PixelData should not be present")
}
}
func BenchmarkParseSingle(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = mustReadFile("examples/IM-0001-0001.dcm", dicom.ReadOptions{})
}
}