-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
288 lines (265 loc) · 6.2 KB
/
Copy pathencode.go
File metadata and controls
288 lines (265 loc) · 6.2 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package termlatex
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/png"
"io"
"math"
"sort"
)
// renderHalfBlock renders img using Unicode half-block characters (▀ U+2580).
// Each cell encodes two vertically stacked pixels via 24-bit foreground (top)
// and background (bottom) ANSI color. Works on any UTF-8 truecolor terminal.
func renderHalfBlock(w io.Writer, img image.Image) error {
bw := bufio.NewWriter(w)
b := img.Bounds()
height := b.Dy()
for y := b.Min.Y; y < b.Max.Y; y += 2 {
for x := b.Min.X; x < b.Max.X; x++ {
top := toRGB(img.At(x, y))
var bot [3]uint8
if y+1 < height+b.Min.Y {
bot = toRGB(img.At(x, y+1))
}
if _, err := fmt.Fprintf(bw, "\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm▀",
top[0], top[1], top[2],
bot[0], bot[1], bot[2],
); err != nil {
return err
}
}
if _, err := bw.WriteString("\x1b[0m\n"); err != nil {
return err
}
}
return bw.Flush()
}
func toRGB(c color.Color) [3]uint8 {
r, g, b, _ := c.RGBA()
return [3]uint8{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
}
const kittyChunk = 4096
// renderKitty encodes img as a Kitty graphics protocol sequence (PNG, f=100)
// and writes it chunked to w.
func renderKitty(w io.Writer, img image.Image) error {
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
return fmt.Errorf("kitty: png encode: %w", err)
}
enc := base64.StdEncoding.EncodeToString(buf.Bytes())
for i := 0; i < len(enc); i += kittyChunk {
end := i + kittyChunk
if end > len(enc) {
end = len(enc)
}
chunk := enc[i:end]
more := 1
if end == len(enc) {
more = 0
}
var ctrl string
if i == 0 {
// a=T: transmit & display. f=100: PNG. q=2: suppress ACK.
ctrl = fmt.Sprintf("a=T,f=100,q=2,m=%d", more)
} else {
ctrl = fmt.Sprintf("q=2,m=%d", more)
}
if _, err := fmt.Fprintf(w, "\x1b_G%s;%s\x1b\\", ctrl, chunk); err != nil {
return err
}
}
_, err := fmt.Fprintln(w)
return err
}
// renderSixel encodes img as a DEC Sixel sequence, quantizing to 256 colors
// via median cut.
func renderSixel(w io.Writer, img image.Image) error {
bw := bufio.NewWriter(w)
b := img.Bounds()
width, height := b.Dx(), b.Dy()
palette := medianCut(img, 256)
if _, err := bw.WriteString("\x1bPq"); err != nil {
return err
}
for i, c := range palette {
r, g, bl, _ := c.RGBA()
// Sixel color values are percentages (0-100).
if _, err := fmt.Fprintf(bw, "#%d;2;%d;%d;%d", i,
int(r>>8)*100/255, int(g>>8)*100/255, int(bl>>8)*100/255,
); err != nil {
return err
}
}
for bandY := 0; bandY < height; bandY += 6 {
bands := make([][]byte, len(palette))
for i := range bands {
bands[i] = make([]byte, width)
}
for x := 0; x < width; x++ {
for bit := 0; bit < 6; bit++ {
py := bandY + bit
if py >= height {
break
}
idx := nearestColor(palette, img.At(b.Min.X+x, b.Min.Y+py))
bands[idx][x] |= 1 << uint(bit)
}
}
for i, band := range bands {
if allZero(band) {
continue
}
if _, err := fmt.Fprintf(bw, "#%d", i); err != nil {
return err
}
for _, v := range band {
if err := bw.WriteByte(v + 63); err != nil {
return err
}
}
if err := bw.WriteByte('$'); err != nil { // carriage return within row
return err
}
}
if err := bw.WriteByte('-'); err != nil { // next sixel row
return err
}
}
if _, err := bw.WriteString("\x1b\\\n"); err != nil {
return err
}
return bw.Flush()
}
func allZero(b []byte) bool {
for _, v := range b {
if v != 0 {
return false
}
}
return true
}
// medianCut quantizes img to at most n colors using the median cut algorithm.
func medianCut(img image.Image, n int) []color.Color {
b := img.Bounds()
pixels := make([]color.RGBA, 0, b.Dx()*b.Dy())
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
r, g, bl, a := img.At(x, y).RGBA()
if a < 0x8000 {
continue
}
pixels = append(pixels, color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(bl >> 8), A: 255})
}
}
buckets := [][]color.RGBA{pixels}
for len(buckets) < n && anyBucketSplittable(buckets) {
buckets = splitLargest(buckets)
}
palette := make([]color.Color, len(buckets))
for i, bucket := range buckets {
palette[i] = average(bucket)
}
return palette
}
func anyBucketSplittable(bs [][]color.RGBA) bool {
for _, b := range bs {
if len(b) > 1 {
return true
}
}
return false
}
func splitLargest(buckets [][]color.RGBA) [][]color.RGBA {
idx := 0
for i, b := range buckets {
if len(b) > len(buckets[idx]) {
idx = i
}
}
bucket := buckets[idx]
ch := dominantChannel(bucket)
sort.Slice(bucket, func(i, j int) bool {
switch ch {
case 0:
return bucket[i].R < bucket[j].R
case 1:
return bucket[i].G < bucket[j].G
default:
return bucket[i].B < bucket[j].B
}
})
mid := len(bucket) / 2
result := make([][]color.RGBA, 0, len(buckets)+1)
result = append(result, buckets[:idx]...)
result = append(result, bucket[:mid], bucket[mid:])
result = append(result, buckets[idx+1:]...)
return result
}
func dominantChannel(pixels []color.RGBA) int {
var minR, minG, minB uint8 = 255, 255, 255
var maxR, maxG, maxB uint8
for _, p := range pixels {
if p.R < minR {
minR = p.R
}
if p.R > maxR {
maxR = p.R
}
if p.G < minG {
minG = p.G
}
if p.G > maxG {
maxG = p.G
}
if p.B < minB {
minB = p.B
}
if p.B > maxB {
maxB = p.B
}
}
rr := int(maxR) - int(minR)
gg := int(maxG) - int(minG)
bb := int(maxB) - int(minB)
if rr >= gg && rr >= bb {
return 0
}
if gg >= bb {
return 1
}
return 2
}
func average(pixels []color.RGBA) color.Color {
if len(pixels) == 0 {
return color.RGBA{}
}
var r, g, b int64
for _, p := range pixels {
r += int64(p.R)
g += int64(p.G)
b += int64(p.B)
}
n := int64(len(pixels))
return color.RGBA{R: uint8(r / n), G: uint8(g / n), B: uint8(b / n), A: 255}
}
func nearestColor(palette []color.Color, c color.Color) int {
r0, g0, b0, _ := c.RGBA()
best := 0
bestDist := math.MaxFloat64
for i, p := range palette {
r1, g1, b1, _ := p.RGBA()
dr := float64(int(r0>>8) - int(r1>>8))
dg := float64(int(g0>>8) - int(g1>>8))
db := float64(int(b0>>8) - int(b1>>8))
d := dr*dr + dg*dg + db*db
if d < bestDist {
bestDist = d
best = i
}
}
return best
}