forked from suyashkumar/dicom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
116 lines (101 loc) · 3.11 KB
/
parse_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
package dicom_test
import (
"bytes"
"encoding/json"
"fmt"
"image/jpeg"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/suyashkumar/dicom/pkg/tag"
"github.com/suyashkumar/dicom/pkg/frame"
"github.com/suyashkumar/dicom"
)
// TestParse is an end-to-end sanity check over DICOMs in testdata/. Currently it only checks that no error is returned
// when parsing the files.
func TestParse(t *testing.T) {
files, err := ioutil.ReadDir("./testdata")
if err != nil {
t.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
t.Run(f.Name(), func(t *testing.T) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
t.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
info, err := dcm.Stat()
if err != nil {
t.Errorf("Unable to stat %s. Error: %v", f.Name(), err)
}
_, err = dicom.Parse(dcm, info.Size(), nil)
if err != nil {
t.Errorf("dicom.Parse(%s) unexpected error: %v", f.Name(), err)
}
})
}
}
}
// BenchmarkParse runs sanity benchmarks over the sample files in testdata.
func BenchmarkParse(b *testing.B) {
files, err := ioutil.ReadDir("./testdata")
if err != nil {
b.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
b.Run(f.Name(), func(b *testing.B) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
b.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
data, err := ioutil.ReadAll(dcm)
if err != nil {
b.Errorf("Unable to read file into memory for benchmark: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = dicom.Parse(bytes.NewBuffer(data), int64(len(data)), nil)
}
})
}
}
}
func Example_readFile() {
// See also: dicom.Parse, which uses a more generic io.Reader API.
dataset, _ := dicom.ParseFile("testdata/1.dcm", nil)
// Dataset will nicely print the DICOM dataset data out of the box.
fmt.Println(dataset)
// Dataset is also JSON serializable out of the box.
j, _ := json.Marshal(dataset)
fmt.Println(j)
}
func Example_streamingFrames() {
frameChan := make(chan *frame.Frame)
// Go routine to handle streaming frames as they are parsed. This may be
// useful when parsing a large DICOM with many frames from a network source,
// where image frames can start to be processed before the entire DICOM
// is parsed (or even read from storage).
go func() {
for fr := range frameChan {
fmt.Println(fr)
}
}()
dataset, _ := dicom.ParseFile("testdata/1.dcm", frameChan)
fmt.Println(dataset) // The full dataset
}
func Example_getImageFrames() {
dataset, _ := dicom.ParseFile("testdata/1.dcm", nil)
pixelDataElement, _ := dataset.FindElementByTag(tag.PixelData)
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelDataElement.Value)
for i, fr := range pixelDataInfo.Frames {
img, _ := fr.GetImage() // The Go image.Image for this frame
f, _ := os.Create(fmt.Sprintf("image_%d.jpg", i))
_ = jpeg.Encode(f, img, &jpeg.Options{Quality: 100})
_ = f.Close()
}
}