-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.go
67 lines (51 loc) · 1.27 KB
/
image.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
package dicomgraphics
import (
"github.com/suyashkumar/dicom/pkg/frame"
"image"
"image/color"
)
type DICOMImage struct {
level int16
width int16
frame *frame.NativeFrame
}
func (d *DICOMImage) SetFrame(frame *frame.NativeFrame) {
d.frame = frame
}
func (d *DICOMImage) WindowLevel() int16 {
return d.level
}
func (d *DICOMImage) SetWindowLevel(level int16) {
d.level = level
}
func (d *DICOMImage) WindowWidth() int16 {
return d.width
}
func (d *DICOMImage) SetWindowWidth(width int16) {
d.width = width
}
func (d *DICOMImage) ColorModel() color.Model {
return color.Gray16Model
}
func (d *DICOMImage) Bounds() image.Rectangle {
return image.Rect(0, 0, d.frame.Cols, d.frame.Rows)
}
func (d *DICOMImage) At(x, y int) color.Color {
if d.frame == nil {
return color.Gray16{Y: 0}
}
windowMin := d.level - d.width/2
windowMax := windowMin + d.width
i := y*d.frame.Rows + x
raw := int16(d.frame.Data[i][0])
if raw < windowMin {
return color.Gray16{Y: 0}
} else if raw >= windowMax {
return color.Gray16{Y: 0xffff}
}
val := float32(raw-windowMin) / float32(d.width)
return color.Gray16{Y: uint16(float32(0xffff) * val)}
}
func NewDICOMImage(frame *frame.NativeFrame, level, width int16) *DICOMImage {
return &DICOMImage{frame: frame, width: width, level: level}
}