-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoint.go
104 lines (90 loc) · 2.39 KB
/
point.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
/*
# Copyright 2016 Tim Greiser
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package etherdream
import (
"encoding/binary"
"fmt"
"image/color"
"io"
"github.com/tgreiser/ln/ln"
)
// PointStream is the interface clients should implement to
// generate points
type PointStream func(w io.WriteCloser)
// Point is a step in the laser stream, X, Y, RGB, Intensity and
// some other fields.
type Point struct {
X int16
Y int16
R uint16
G uint16
B uint16
I uint16
U1 uint16
U2 uint16
Flags uint16
}
// NewPoint wil instantiate a point from the basic attributes.
func NewPoint(x, y int, c color.Color) *Point {
r, g, b, a := c.RGBA()
return &Point{
X: int16(x),
Y: int16(y),
R: uint16(r),
G: uint16(g),
B: uint16(b),
I: uint16(a),
}
}
// Encode color values into a 18 byte struct Point
//
// Values must be specified for x, y, r, g, and b. If a value is not
// passed in for the other fields, i will default to max(r, g, b); the
// rest default to zero.
func (p Point) Encode() []byte {
mut.Lock()
if p.I <= 0 {
p.I = p.R
if p.G > p.I {
p.I = p.G
}
if p.B > p.I {
p.I = p.B
}
}
var enc = make([]byte, 18)
binary.LittleEndian.PutUint16(enc[0:2], p.Flags)
// X and Y are actualy int16
binary.LittleEndian.PutUint16(enc[2:4], uint16(p.X))
binary.LittleEndian.PutUint16(enc[4:6], uint16(p.Y))
binary.LittleEndian.PutUint16(enc[6:8], p.R)
binary.LittleEndian.PutUint16(enc[8:10], p.G)
binary.LittleEndian.PutUint16(enc[10:12], p.B)
binary.LittleEndian.PutUint16(enc[12:14], p.I)
binary.LittleEndian.PutUint16(enc[14:16], p.U1)
binary.LittleEndian.PutUint16(enc[16:18], p.U2)
if *Dump {
fmt.Printf("%v\t%v\t%v\t%v\t%v\n", p.X, p.Y, p.R, p.G, p.B)
}
mut.Unlock()
return enc
}
func (p Point) ToVector() ln.Vector {
return ln.Vector{float64(p.X), float64(p.Y), 0.0}
}
// Points - Point list
type Points struct {
Points []Point
}