forked from fiam/max7456tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar.go
201 lines (179 loc) · 4.24 KB
/
char.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
196
197
198
199
200
201
package main
import (
"fmt"
"image"
"image/color"
)
const (
charWidth = 12
charHeight = 18
// See type MCMChar
minCharBytes = 54
charBytes = 64
// all pixels = 01
mcmTransparentByte = 85
)
var (
blankCharacter = constantChar(mcmTransparentByte)
)
// MCMPixel represents a pixel in a character. Each pixel must be one
// of MCMPixelBlack, MCMPixelTransparent or MCMPixelWhite.
type MCMPixel byte
const (
// MCMPixelBlack represents a black pixel
MCMPixelBlack MCMPixel = 0
// MCMPixelTransparent represents a transparent/gray pixel,
// depending on the OSD mode.
MCMPixelTransparent = 1
// MCMPixelWhite represents a white pixel
MCMPixelWhite = 2
)
func (p MCMPixel) isTransparent() bool {
// Transparent pixels have the LSB
// set to 1 while MSB is ignored.
return (p & MCMPixelTransparent) == MCMPixelTransparent
}
// MCMChar represents a character in the character map.
// Each character has 12x18 lines, where each pixel is represented
// by 2 bits. Thus, each character requires ((12*18)*2)/8 = 54 bytes.
// However, MCM files use 64 bytes per character ignoring the rest
// (according to Maxim, to make adressing easier).
type MCMChar struct {
data []byte
}
// Data returns a copy of the raw pixel data.
func (c *MCMChar) Data() []byte {
data := make([]byte, len(c.data))
copy(data, c.data)
return data
}
// ForEachPixel calls f for each pixel in the character.
// 0 <= x <= 12 while y >= 0. Note that a character might
// have extra ignored pixels at the end. unused will be true
// for those ones. p will always be one
// of the constants defined for MCMPixel
func (c *MCMChar) ForEachPixel(f func(x, y int, unused bool, p MCMPixel)) {
x := 0
y := 0
for _, v := range c.data {
unused := y >= charHeight
f(x, y, unused, MCMPixel((0xC0&v)>>6))
f(x+1, y, unused, MCMPixel((0x30&v)>>4))
f(x+2, y, unused, MCMPixel((0xC&v)>>2))
f(x+3, y, unused, MCMPixel(0x03&v))
x += 4
if x == charWidth {
x = 0
y++
}
}
}
// Image returns a 12x18 image of the character
func (c *MCMChar) Image(transparent color.Color) image.Image {
if isNilColor(transparent) {
transparent = defaultTransparentColor
}
im := image.NewRGBA(image.Rect(0, 0, charWidth, charHeight))
c.ForEachPixel(func(x, y int, unused bool, p MCMPixel) {
if unused {
return
}
var c color.Color
switch p {
case MCMPixelTransparent:
c = transparent
case MCMPixelBlack:
c = blackColor
case MCMPixelWhite:
c = whiteColor
default:
// Should not happen
panic(fmt.Errorf("invalid pixel %v", p))
}
im.Set(x, y, c)
})
return im
}
func (c *MCMChar) isBlank() bool {
blank := true
c.ForEachPixel(func(x, y int, unused bool, p MCMPixel) {
if p != MCMPixelTransparent {
blank = false
}
})
return blank
}
func constantChar(b byte) *MCMChar {
data := make([]byte, charBytes)
for ii := range data {
data[ii] = b
}
return &MCMChar{data: data}
}
type charBuilder struct {
c *MCMChar
pixel int
}
func (b *charBuilder) Char() *MCMChar {
return b.c
}
func (b *charBuilder) Reset() {
b.c = new(MCMChar)
b.pixel = 0
}
func (b *charBuilder) IsEmpty() bool {
return len(b.c.data) == 0
}
func (b *charBuilder) IsComplete() bool {
return len(b.c.data) == charBytes && b.pixel == 0
}
func (b *charBuilder) AppendPixel(p MCMPixel) error {
if p.isTransparent() {
p = MCMPixelTransparent
}
if p > 3 {
return fmt.Errorf("invalid pixel %v > 3", p)
}
pb := byte(p)
switch b.pixel {
case 0:
// Append new byte
b.c.data = append(b.c.data, pb<<6)
case 1:
b.c.data[len(b.c.data)-1] |= pb << 4
case 2:
b.c.data[len(b.c.data)-1] |= pb << 2
case 3:
b.c.data[len(b.c.data)-1] |= pb
}
b.pixel++
if b.pixel == 4 {
b.pixel = 0
}
return nil
}
func (b *charBuilder) SetImage(im image.Image, x0, y0 int) error {
b.Reset()
bounds := im.Bounds()
for y := y0; y < y0+charHeight; y++ {
for x := x0; x < x0+charWidth; x++ {
px := bounds.Min.X + x
py := bounds.Min.Y + y
r, g, bl, a := im.At(px, py).RGBA()
var p MCMPixel
switch {
case r == 0 && g == 0 && bl == 0 && a == 65535:
p = MCMPixelBlack
case r == 65535 && g == 65535 && bl == 65535 && a == 65535:
p = MCMPixelWhite
default:
p = MCMPixelTransparent
}
b.AppendPixel(p)
}
}
for !b.IsComplete() {
b.AppendPixel(MCMPixelTransparent)
}
return nil
}