-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage.go
More file actions
142 lines (125 loc) · 2.46 KB
/
image.go
File metadata and controls
142 lines (125 loc) · 2.46 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
package runal
import (
"image"
col "image/color"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/ansi"
"golang.org/x/image/webp"
"github.com/emprcl/runal/pkg/mosaic"
)
type Image interface {
Cell(x, y int) Cell
writable
}
type writable interface {
write(c *Canvas, x, y, w, h int)
}
type imageFile struct {
file image.Image
frame frame
}
func (i *imageFile) Cell(x, y int) Cell {
if i.frame.outOfBounds(x, y) {
return Cell{}
}
return i.frame[y][x].public()
}
func (i *imageFile) write(c *Canvas, x, y, w, h int) {
m := mosaic.New().Symbol(mosaic.Half)
if w > 0 {
m = m.Width(w)
}
if h > 0 {
m = m.Height(h)
}
imageBuffer := m.RenderCells(i.file)
i.frame = newFrame(w, h)
c.toggleFill()
for iy := range imageBuffer {
for ix := range imageBuffer[iy] {
if c.outOfBounds(x+ix, y+iy) {
continue
}
cell := cell{
char: imageBuffer[iy][ix].Char,
background: colorFromImage(imageBuffer[iy][ix].Background),
foreground: colorFromImage(imageBuffer[iy][ix].Foreground),
}
c.write(cell, x+ix, y+iy, 2)
i.frame[iy][ix] = cell
}
}
c.toggleFill()
}
type imageFrame struct {
frame frame
}
func (i *imageFrame) Cell(x, y int) Cell {
if i.frame.outOfBounds(x, y) {
return Cell{}
}
return i.frame[y][x].public()
}
func (i *imageFrame) write(c *Canvas, x, y, w, h int) {
if h == 0 {
_, h = i.frame.size()
}
if w == 0 {
w, _ = i.frame.size()
}
for iy := range i.frame {
for ix := range i.frame[iy] {
if c.outOfBounds(x+ix, y+iy) ||
x+ix >= x+w || y+iy >= y+h {
continue
}
c.write(i.frame[iy][ix], x+ix, y+iy, 1)
}
}
}
func (c *Canvas) LoadImage(path string) Image {
f, err := os.Open(path)
if err != nil {
log.Errorf("can't load image: %v", err)
c.DisableRendering()
return nil
}
defer f.Close()
var img image.Image
switch filepath.Ext(path) {
case ".jpg":
img, err = jpeg.Decode(f)
case ".png":
img, err = png.Decode(f)
case ".webp":
img, err = webp.Decode(f)
}
if err != nil {
log.Errorf("can't load image: %v", err)
c.DisableRendering()
return nil
}
return &imageFile{
file: img,
}
}
func (c *Canvas) Image(img Image, x, y, w, h int) {
if img == nil {
log.Errorf("can't load empty image")
c.DisableRendering()
return
}
img.write(c, x, y, w, h)
}
func colorFromImage(c col.Color) ansi.Color {
rgba := col.RGBAModel.Convert(c).(col.RGBA)
return ansi.RGBColor{
R: rgba.R,
G: rgba.G,
B: rgba.B,
}
}