-
Notifications
You must be signed in to change notification settings - Fork 6
/
robot.go
195 lines (164 loc) · 4.95 KB
/
robot.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2018 Johannes Donath <[email protected]>
* and other copyright owners as documented in the project's IP log.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sstv
import (
"errors"
"github.com/go-audio/audio"
"image"
)
type RobotMode uint8
const (
Robot36 RobotMode = 8
Robot72 RobotMode = 12
)
const (
robotLineFrequency = 1200
robotLineLength = 9
robotSyncFrequency = 1500
robotSyncLength = 3
robotPorchFrequency = 1900
robotPorchLength = 1.5
robotEvenSeparatorFrequency = 1500
robotOddSeparatorFrequency = 2300
robotSeparatorLength = 4.5
)
const (
robot36YLength = 0.275
robot36Length = 0.1375
robot72YLength = 0.43125
robot72Length = 0.215625
)
// provides a Scottie implementation as designed by Eddie Murphy
//
// this implementation encodes RGB images into 240 lines
type robotEncoder struct {
mode RobotMode
format *audio.Format
}
// creates a new Martin compatible image encoder
func NewRobot(mode RobotMode, format *audio.Format) Encoder {
return &robotEncoder{
mode: mode,
format: format,
}
}
func (enc *robotEncoder) Vis() uint8 {
return uint8(enc.mode)
}
func (enc *robotEncoder) Resolution() image.Rectangle {
return image.Rect(0, 0, 320, 240)
}
func (enc *robotEncoder) Encode(img image.Image) *audio.FloatBuffer {
wr := newWriter(enc.format)
wr.writeHeader()
wr.writeVis(uint8(enc.mode))
// different from other encoders, Robot provides two completely different encoding types which
// share little to nothing and thus we 'll need two separate encoding methods
switch enc.mode {
case Robot36:
enc.encode36(wr, img)
case Robot72:
enc.encode72(wr, img)
default:
panic(errors.New("illegal encoding mode"))
}
return wr.buf
}
func (enc *robotEncoder) encode36(wr *audioWriter, img image.Image) {
size := img.Bounds().Size()
ymap, umap, vmap := enc.generateYUVMap(img)
for y := 0; y < size.Y; y++ {
wr.write(robotLineFrequency, robotLineLength)
wr.write(robotSyncFrequency, robotSyncLength)
even := y%2 == 0
for i := 0; i < 2; i++ {
for x := 0; x < size.X; x++ {
mapIndex := y*size.X + x
var val float64
l := robot36Length
if i == 0 {
val = float64(ymap[mapIndex]) / 255
l = robot36YLength
} else if even {
val = float64(umap[mapIndex]) / 255
} else {
val = float64(vmap[mapIndex]) / 255
}
wr.writeValue(val, l)
}
if i == 0 {
if even {
wr.write(robotEvenSeparatorFrequency, robotSeparatorLength)
} else {
wr.write(robotOddSeparatorFrequency, robotSeparatorLength)
}
wr.write(robotPorchFrequency, robotPorchLength)
}
}
}
}
func (enc *robotEncoder) encode72(wr *audioWriter, img image.Image) {
size := img.Bounds().Size()
for y := 0; y < size.Y; y++ {
wr.write(robotLineFrequency, robotLineLength)
wr.write(robotSyncFrequency, robotSyncLength)
for i := 0; i < 3; i++ {
for x := 0; x < size.X; x++ {
y, u, v := convertYUV(img.At(x, y))
var val float64
l := robot72Length
if i == 0 {
val = float64(y) / 255
l = robot72YLength
} else if i == 1 {
val = float64(u) / 255
} else {
val = float64(v) / 255
}
wr.writeValue(val, l)
}
if i != 2 {
if i % 2 == 0 {
wr.write(robotEvenSeparatorFrequency, robotSeparatorLength)
wr.write(robotPorchFrequency, robotPorchLength)
} else {
wr.write(robotOddSeparatorFrequency, robotSeparatorLength)
wr.write(robotSyncFrequency, robotPorchLength)
}
}
}
}
}
func (enc *robotEncoder) generateYUVMap(img image.Image) ([]byte, []byte, []byte) {
size := img.Bounds().Size()
y := make([]byte, size.X*size.Y)
u := make([]byte, size.X*size.Y)
v := make([]byte, size.X*size.Y)
for ycord := 0; ycord < size.Y; ycord++ {
for xcord := 0; xcord < size.X; xcord++ {
yv, uv00, vv00 := convertYUV(img.At(xcord, ycord))
_, uv01, vv01 := convertYUV(img.At(xcord, ycord+1))
_, uv10, vv10 := convertYUV(img.At(xcord+1, ycord))
_, uv11, vv11 := convertYUV(img.At(xcord+1, ycord+1))
i := ycord*size.X + xcord
y[i] = yv
u[i] = byte((int(uv00) + int(uv01) + int(uv10) + int(uv11)) / 4)
v[i] = byte((int(vv00) + int(vv01) + int(vv10) + int(vv11)) / 4)
}
}
return y, u, v
}