-
Notifications
You must be signed in to change notification settings - Fork 1
/
img2ansi.go
137 lines (123 loc) · 3.36 KB
/
img2ansi.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
package img2ansi
import (
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/johnmccabe/img2ansi/palette"
)
const alphaThreshold = 30
// ansiBlock
type ansiBlock struct {
FG string
BG string
Glyph string
}
// RenderANSI16 TODO
func RenderANSI16(i image.Image) (string, error) {
return renderANSI(i, palette.Xterm16)
}
// RenderANSI256 TODO
func RenderANSI256(i image.Image) (string, error) {
return renderANSI(i, palette.Xterm256)
}
// RenderTrueColor TODO
func RenderTrueColor(i image.Image) (string, error) {
return renderANSI(i, nil)
}
func renderANSI(i image.Image, p color.Palette) (string, error) {
bounds := i.Bounds()
rowbuffer := make([][]ansiBlock, (bounds.Max.Y+1)/2)
for row := 0; row < bounds.Max.Y; row += 2 {
rowbuffer[row/2] = make([]ansiBlock, bounds.Max.X)
for x := 0; x < bounds.Max.X; x++ {
top := i.At(x, row)
bottom := i.At(x, row+1)
if p == nil {
rowbuffer[row/2] = append(rowbuffer[row/2], rgbToTrueColor(top, bottom))
} else {
rowbuffer[row/2] = append(rowbuffer[row/2], rgbToXterm(top, bottom, p))
}
}
}
return bufferToString(rowbuffer), nil
}
func bufferToString(rowbuffer [][]ansiBlock) string {
var s string
for y := 0; y < len(rowbuffer); y++ {
for x := 0; x < len(rowbuffer[0]); x++ {
if x == 0 {
s += rowbuffer[y][x].printCompact(ansiBlock{})
}
if x > 0 {
s += rowbuffer[y][x].printCompact(rowbuffer[y][x-1])
}
}
// Reset escape codes and add a newline
s += "\033[0m"
s += "\n"
}
return s
}
func (b ansiBlock) printCompact(previous ansiBlock) (out string) {
if (b.FG == previous.FG) && (b.BG == previous.BG) {
out = fmt.Sprintf("%s", b.Glyph)
} else if b.FG == previous.FG {
out = fmt.Sprintf("\033[%sm%s", b.BG, b.Glyph)
} else if b.BG == previous.BG {
out = fmt.Sprintf("\033[%sm%s", b.FG, b.Glyph)
} else {
out = fmt.Sprintf("\033[%s;%sm%s", b.BG, b.FG, b.Glyph)
}
return out
}
func rgbToTrueColor(top, bottom color.Color) ansiBlock {
var fg, bg string
rt, gt, bt, at := top.RGBA()
rb, gb, bb, ab := bottom.RGBA()
var symbol string
if uint8(ab) >= alphaThreshold && uint8(at) >= alphaThreshold {
symbol = "▀"
fg = fmt.Sprintf("38;2;%d;%d;%d", uint8(rt), uint8(gt), uint8(bt))
bg = fmt.Sprintf("48;2;%d;%d;%d", uint8(rb), uint8(gb), uint8(bb))
} else if uint8(ab) >= alphaThreshold {
symbol = "▄"
fg = fmt.Sprintf("38;2;%d;%d;%d", uint8(rb), uint8(gb), uint8(bb))
bg = fmt.Sprintf("49")
} else if uint8(at) >= alphaThreshold {
symbol = "▀"
fg = fmt.Sprintf("38;2;%d;%d;%d", uint8(rt), uint8(gt), uint8(bt))
bg = fmt.Sprintf("49")
} else {
symbol = " "
fg = "39"
bg = "49"
}
return ansiBlock{FG: fg, BG: bg, Glyph: symbol}
}
func rgbToXterm(top, bottom color.Color, p color.Palette) ansiBlock {
var fg, bg string
_, _, _, at := top.RGBA()
_, _, _, ab := bottom.RGBA()
var symbol string
if uint8(ab) >= alphaThreshold && uint8(at) >= alphaThreshold {
symbol = "▀"
fg = fmt.Sprintf("38;5;%d", p.Index(top))
bg = fmt.Sprintf("48;5;%d", p.Index(bottom))
} else if uint8(ab) >= alphaThreshold {
symbol = "▄"
fg = fmt.Sprintf("38;5;%d", p.Index(bottom))
bg = fmt.Sprintf("49")
} else if uint8(at) >= alphaThreshold {
symbol = "▀"
fg = fmt.Sprintf("38;5;%d", p.Index(top))
bg = fmt.Sprintf("49")
} else {
symbol = " "
fg = "39"
bg = "49"
}
return ansiBlock{FG: fg, BG: bg, Glyph: symbol}
}