-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonochrome.go
57 lines (50 loc) · 1.06 KB
/
monochrome.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
package monochrome
import (
"fmt"
"image"
"image/color"
"strings"
)
type Monochrome struct {
val []byte
width int
height int
}
func (m *Monochrome) ToCpp() string {
s := "const uint8_t image[] = {"
s += fmt.Sprintf("0x%02x, 0x%02x, ", m.width, m.height)
for _, b := range m.val {
s += fmt.Sprintf("0x%02x, ", b)
}
s = strings.TrimRight(s, ", ")
s += "};"
return s
}
func (m *Monochrome) ToPy() string {
s := "image = bytearray(["
s += fmt.Sprintf("0x%02x, 0x%02x, ", m.width, m.height)
for _, b := range m.val {
s += fmt.Sprintf("0x%02x, ", b)
}
s = strings.TrimRight(s, ", ")
s += "])"
return s
}
func Convert(img image.Image) *Monochrome {
w, h := img.Bounds().Dx(), img.Bounds().Dy()
conv := make([]byte, 0, w*h/8)
for y := 0; y < h; y += 8 {
for x := 0; x < w; x++ {
var col byte
for j := 7; j >= 0; j-- {
pixel := 1
if color.Gray16Model.Convert(img.At(x, y+j)) == color.Black {
pixel = 0
}
col = (col << 1) | byte(pixel)
}
conv = append(conv, col)
}
}
return &Monochrome{val: conv, width: w, height: h}
}