-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage.go
More file actions
163 lines (142 loc) · 3.74 KB
/
image.go
File metadata and controls
163 lines (142 loc) · 3.74 KB
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
package koebiten
import (
"image/color"
"io/fs"
"github.com/chewxy/math32"
"tinygo.org/x/drivers/image/png"
"tinygo.org/x/drivers/pixel"
)
var _ Displayer = (*Image)(nil)
// Image is a Wrapper for the pixel.Image type.
//
// Image implements the Displayer interface.
type Image struct {
img pixel.Image[pixel.Monochrome]
}
// Size returns the width and height of the image.
//
// It implements the Displayer interface.
func (i *Image) Size() (int16, int16) {
x, y := i.img.Size()
return int16(x), int16(y)
}
// SetPixel sets the pixel at the given x and y coordinates to the given color.
// The color is converted to a pixel.Monochrome color.
// If the x and y coordinates are outside the image, the function does nothing.
//
// It implements the Displayer interface.
func (i *Image) SetPixel(x, y int16, c color.RGBA) {
w, h := i.img.Size()
if x < 0 || x >= int16(w) || y < 0 || y >= int16(h) {
return
}
i.img.Set(int(x), int(y), pixel.NewMonochrome(c.R, c.G, c.B))
}
// Display does nothing.
//
// It implements the Displayer interface.
func (i *Image) Display() error { return nil }
// ClearDisplay clears the display.
//
// It implements the Displayer interface.
func (i *Image) ClearDisplay() {}
// ClearBuffer clears the buffer.
//
// It implements the Displayer interface.
func (i *Image) ClearBuffer() {}
// NewImage creates a new Image with the given width and height.
//
// It returns a pointer to the new Image.
func NewImage(width, height int16) *Image {
return &Image{
img: pixel.NewImage[pixel.Monochrome](int(width), int(height)),
}
}
// NewImageFromFS creates a new Image from the filesystem.
func NewImageFromFS(fsys fs.FS, path string) *Image {
img, err := loadImageFromFS(fsys, path)
if err != nil {
panic(err)
}
return &Image{img: img}
}
// loadImageFromFS loads an image from the filesystem.
func loadImageFromFS(fsys fs.FS, path string) (pixel.Image[pixel.Monochrome], error) {
var buffer [3 * 8 * 8 * 4]uint16
p, err := fsys.Open(path)
if err != nil {
return pixel.Image[pixel.Monochrome]{}, err
}
var img pixel.Image[pixel.Monochrome]
png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
if img.Len() == 0 {
img = pixel.NewImage[pixel.Monochrome](int(width), int(height))
}
for yy := int16(0); yy < h; yy++ {
for xx := int16(0); xx < w; xx++ {
c := C565toRGBA(data[yy*w+xx])
cnt := 0
if c.R < 0x80 {
cnt++
}
if c.G < 0x80 {
cnt++
}
if c.B < 0x80 {
cnt++
}
if cnt >= 2 {
img.Set(int(x+xx), int(y+yy), true)
}
}
}
})
if _, err = png.Decode(p); err != nil {
return pixel.Image[pixel.Monochrome]{}, err
}
return img, nil
}
// Fill fills the image with the given color.
func (i *Image) Fill(clr color.Color) {
r, g, b, _ := clr.RGBA()
w, h := i.img.Size()
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
i.img.Set(x, y, pixel.NewMonochrome(uint8(r), uint8(g), uint8(b)))
}
}
}
type DrawImageOptions struct {
GeoM GeoM
}
// DrawImage draws an image onto the display.
func (i *Image) DrawImage(dst Displayer, options DrawImageOptions) {
if isNil(dst) {
dst = display
}
geoM := options.GeoM
if !geoM.IsInvertible() {
return
}
w, h := i.img.Size()
if geoM.a_1 == 0 && geoM.b == 0 && geoM.c == 0 && geoM.d_1 == 0 {
tx, ty := geoM.Apply(0, 0)
ox, oy := int(math32.Round(tx)), int(math32.Round(ty))
for yy := 0; yy < h; yy++ {
for xx := 0; xx < w; xx++ {
if i.img.Get(xx, yy) == true {
dst.SetPixel(int16(xx+ox), int16(yy+oy), white)
}
}
}
} else {
for yy := 0; yy < h; yy++ {
for xx := 0; xx < w; xx++ {
if i.img.Get(xx, yy) == true {
xxf, yyf := geoM.Apply(float32(xx), float32(yy))
dst.SetPixel(int16(math32.Round(xxf)), int16(math32.Round(yyf)), white)
}
}
}
}
}