This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
dicomPipes.go
166 lines (130 loc) · 2.84 KB
/
dicomPipes.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
package dicom
import (
"fmt"
"io/ioutil"
"log"
"os"
fp "path/filepath"
"sync"
)
type DicomMessage struct {
msg *DicomElement
wait chan bool
}
const (
JPEG_2000 = "1.2.840.10008.1.2.4.91"
JPEG_BASELINE_1 = "1.2.840.10008.1.2.4.50"
)
// generator
func (di *DicomFile) Parse(buff []byte) <-chan DicomMessage {
parser, err := NewParser()
if err != nil {
panic(err)
}
_, c := parser.Parse(buff)
if err != nil {
panic(err)
}
return c
}
// Discard messages
func (di *DicomFile) Discard(in <-chan DicomMessage, done *sync.WaitGroup) {
done.Add(1)
go func() {
for dcmMsg := range in {
dcmMsg.wait <- true
}
done.Done()
}()
}
func fileName(folder string, i int, ext string) string {
basename := fp.Base(folder)
filename := basename + "_" + fmt.Sprintf("%03d\n", i)
return fp.Join(folder, filename) + "." + ext
}
// Writes pixel data to folder
func (di *DicomFile) WriteImagesToFolder(in <-chan DicomMessage, done *sync.WaitGroup, folder string) <-chan DicomMessage {
out := make(chan DicomMessage)
waitMsg := make(chan bool)
done.Add(1)
go func() {
var inImg bool = false
var idx int
var txUID, fext string
for dcmMsg := range in {
switch dcmMsg.msg.Name {
case "TransferSyntaxUID":
txUID = dcmMsg.msg.Value[0].(string)
case "PixelData":
inImg = true
switch txUID {
// JPEG baseline 1
case JPEG_BASELINE_1:
fext = "jpg"
// JPEG 2000 Part 1
case JPEG_2000:
fext = "jp2"
// not implemented
default:
//panic("Non implemented Transfer Syntax: \"" + txUID + "\"")
}
case "Item":
if inImg == true {
if idx > 0 {
pb := dcmMsg.msg.Value[0].([]byte)
err := ioutil.WriteFile(fileName(folder, idx, fext), pb, 0644)
if err != nil {
panic(err)
}
}
idx++
}
}
out <- DicomMessage{dcmMsg.msg, waitMsg}
<-waitMsg
dcmMsg.wait <- true
}
close(out)
done.Done()
}()
return out
}
// Writes dicom elements to file
func (di *DicomFile) WriteToFile(in <-chan DicomMessage, done *sync.WaitGroup, file *os.File) <-chan DicomMessage {
out := make(chan DicomMessage)
waitMsg := make(chan bool)
done.Add(1)
go func() {
for dcmMsg := range in {
_, err := file.WriteString(fmt.Sprintln(dcmMsg.msg))
if err != nil {
panic(err)
}
out <- DicomMessage{dcmMsg.msg, waitMsg}
<-waitMsg
dcmMsg.wait <- true
}
file.Close()
close(out)
done.Done()
}()
return out
}
// Logs dicom elements
func (di *DicomFile) Log(in <-chan DicomMessage, done *sync.WaitGroup) <-chan DicomMessage {
logger := log.New(os.Stdout, "", 0)
out := make(chan DicomMessage)
waitMsg := make(chan bool)
done.Add(1)
go func() {
for dcmMsg := range in {
logger.Println(dcmMsg.msg)
out <- DicomMessage{dcmMsg.msg, waitMsg}
<-waitMsg
dcmMsg.wait <- true
}
close(out)
done.Done()
}()
return out
}