/
native.go
50 lines (43 loc) · 1.45 KB
/
native.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
package frame
import (
"image"
"image/color"
"math"
)
// NativeFrame represents a native image frame
type NativeFrame struct {
// Data is a slice of pixels, where each pixel can have multiple values
Data [][]int
Rows int
Cols int
BitsPerSample int
}
// IsEncapsulated indicates if the frame is encapsulated or not.
func (n *NativeFrame) IsEncapsulated() bool { return false }
// GetNativeFrame returns a NativeFrame from this frame. If the underlying frame
// is not a NativeFrame, ErrorFrameTypeNotPresent will be returned.
func (n *NativeFrame) GetNativeFrame() (*NativeFrame, error) {
return n, nil
}
// GetEncapsulatedFrame returns ErrorFrameTypeNotPresent, because this struct
// does not hold encapsulated frame data.
func (n *NativeFrame) GetEncapsulatedFrame() (*EncapsulatedFrame, error) {
return nil, ErrorFrameTypeNotPresent
}
// GetImage returns an image.Image representation the frame, using default
// processing. This default processing is basic at the moment, and does not
// autoscale pixel values or use window width or level info.
func (n *NativeFrame) GetImage() (image.Image, error) {
i := image.NewGray16(image.Rect(0, 0, n.Cols, n.Rows))
max := 0
for j := 0; j < len(n.Data); j++ {
if n.Data[j][0] > max {
max = n.Data[j][0]
}
}
mu := (math.MaxUint16 + 1) / (max + 1)
for j := 0; j < len(n.Data); j++ {
i.SetGray16(j%n.Cols, j/n.Cols, color.Gray16{Y: uint16(n.Data[j][0]) * uint16(mu)})
}
return i, nil
}