/
main.go
156 lines (139 loc) · 3.33 KB
/
main.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
package main
import (
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"math"
"os"
termbox "github.com/nsf/termbox-go"
)
var palette color.Palette = []color.Color{
attrColor(termbox.ColorBlack),
attrColor(termbox.ColorRed),
attrColor(termbox.ColorGreen),
attrColor(termbox.ColorYellow),
attrColor(termbox.ColorBlue),
attrColor(termbox.ColorMagenta),
attrColor(termbox.ColorCyan),
attrColor(termbox.ColorWhite),
}
func upgrade(palette color.Palette) color.Palette {
for i := 16; i <= 255; i++ {
palette = append(palette, attrColor(i))
}
return palette
}
type attrColor termbox.Attribute
var base = [...]uint8{0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff}
func (c attrColor) RGBA() (r, g, b, a uint32) {
switch termbox.Attribute(c) {
case termbox.ColorBlack:
return 0, 0, 0, math.MaxUint16
case termbox.ColorRed:
return math.MaxUint16, 0, 0, math.MaxUint16
case termbox.ColorGreen:
return 0, math.MaxUint16, 0, math.MaxUint16
case termbox.ColorYellow:
return math.MaxUint16, math.MaxUint16, 0, math.MaxUint16
case termbox.ColorBlue:
return 0, 0, math.MaxUint16, math.MaxUint16
case termbox.ColorMagenta:
return math.MaxUint16, 0, math.MaxUint16, math.MaxUint16
case termbox.ColorCyan:
return 0, math.MaxUint16, math.MaxUint16, math.MaxUint16
case termbox.ColorWhite:
return math.MaxUint16, math.MaxUint16, math.MaxUint16, math.MaxUint16
}
switch {
case c >= 16 && c <= 231:
c -= 16
rgba := color.RGBA{R: base[(c/36)%6], G: base[(c/6)%6], B: base[c%6], A: 0xff}
return rgba.RGBA()
case c >= 232 && c <= 255:
x := uint8(0x08 + 0xa*(c-232))
rgba := color.RGBA{R: x, G: x, B: x, A: 0xff}
return rgba.RGBA()
}
panic("not found")
}
func fit(w, h, iw, ih int) (sw, sh int) {
if w >= iw && h >= ih {
// image is smaller than screen
return iw, ih
} else if w >= iw {
// image is taller than the screen
return iw * h / ih, h
} else if h >= ih {
// image is skinnier than the screen
return w, ih * w / iw
} else {
sw, sh = iw*h/ih, h
if sw <= w {
return sw, sh
}
return w, ih * w / iw
}
}
func draw(img image.Image) {
w, h := termbox.Size()
iw, ih := img.Bounds().Max.X, img.Bounds().Max.Y
sw, sh := fit(w, h, iw*2, ih)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
if x >= sw || y >= sh {
termbox.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
continue
}
xi := x * img.Bounds().Max.X / sw
yi := y * img.Bounds().Max.Y / sh
a := palette.Convert(img.At(xi, yi))
if at, ok := a.(attrColor); ok {
termbox.SetCell(x, y, ' ', termbox.ColorDefault,
termbox.Attribute(at))
}
}
}
termbox.Flush()
}
func main() {
fname := os.Args[1]
f, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatal(err)
}
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
if termbox.SetOutputMode(termbox.Output256) == termbox.Output256 {
palette = upgrade(palette)
}
lw, lh := termbox.Size()
draw(img)
loop:
for {
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
break loop
}
case termbox.EventResize:
termbox.Flush()
if w, h := termbox.Size(); lw != w || lh != h {
lw, lh = w, h
draw(img)
}
}
}
}