-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsixel.go
235 lines (181 loc) · 4.69 KB
/
sixel.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
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
package rasterm
import (
"fmt"
"image"
"io"
"strconv"
)
// NOTE: valid sixel encodeds are in range 0x3F (?) TO 0x7E (~)
const (
SIXEL_MIN byte = 0x3f
SIXEL_MAX byte = 0x7e
)
func IsSixelCapable() (bool, error) {
sATT, E := RequestTermAttributes()
if E != nil {
return false, E
}
for ix := range sATT {
// IGNORE `4` @ 1ST INDEX -- THAT IS TERMINAL ID RATHER THAN SIXEL SUPPORT
if (ix > 0) && (sATT[ix] == 4) {
return true, nil
}
}
return false, nil
}
/*
Encodes a paletted image into DECSIXEL format.
Forked & heavily modified from https://github.com/mattn/go-sixel/
Since SIXEL does not support alpha transparency, any alpha > 0
will be treated as fully opaque.
SIXEL is a paletted format. To keep dependencies to a minimum, this only
supports paletted images. Palette entries beyond index 255 are ignored.
To handle non-paletted images, please pre-dither from the caller.
For more information on DECSIXEL format:
https://www.vt100.net/docs/vt3xx-gp/chapter14.html
https://saitoha.github.io/libsixel/
*/
func SixelWriteImage(out io.Writer, pI *image.Paletted) (E error) {
width, height := pI.Bounds().Dx(), pI.Bounds().Dy()
if (width <= 0) || (height <= 0) {
return
}
if len(pI.Palette) == 0 {
return
}
OSC_OPEN, OSC_CLOSE := "\x1b", "\x1b\\"
// CAPTURE WRITE ERROR FOR SIMPLIFIED CHECKING
fnWri := func(v []byte) error {
_, E = out.Write(v)
return E
}
// INTRODUCER = <ESC>P0;1q
// 0; rely on RASTER ATTRIBUTES to set aspect ratio
// 1; palette[0] as opaque
// RASTER ATTRIBUTES (1:1 aspect ratio) = "1;1;width;height
_, E = fmt.Fprintf(out, "%sP0;1q\"1;1;%d;%d", OSC_OPEN, width, height)
if E != nil {
return
}
// CONVERT uint32 [0..0xFFFF] COLOR COMPONENT TO WHOLE PERCENTAGE
P := func(v uint32) uint8 {
return uint8(((v + 1) * 100) >> 16)
}
// SEND PALETTE
for ix_color, v := range pI.Palette {
// SIXEL ONLY SUPPORTS 256 COLORS
if ix_color > 255 {
break
}
// R,G,B AS WHOLE PERCENTAGES
r, g, b, a := v.RGBA()
// OMIT FULLY-TRANSPARENT COLORS FROM GCI PALETTE
if a == 0 {
continue
}
// DECGCI (#): Graphics Color Introducer
// SEE: https://www.vt100.net/docs/vt3xx-gp/chapter14.html
_, E = fmt.Fprintf(out, "#%d;2;%d;%d;%d", ix_color, P(r), P(g), P(b))
if E != nil {
return
}
}
nColors := len(pI.Palette)
color_used := make([]bool, nColors)
color_used_blank := make([]bool, nColors)
buf := make([]byte, width*nColors)
buf_blank := make([]byte, width*nColors)
// WALK IMAGE HEIGHT IN SIXEL ROWS
sixel_rows := (height + 5) / 6
for ix_srow := 0; ix_srow < sixel_rows; ix_srow++ {
// GRAPHICS NL (start a new sixel line)
if ix_srow > 0 {
if fnWri([]byte(`-`)) != nil {
return
}
}
// RESET COLOR USAGE FLAGS & SIXEL LINE BUFFER
copy(color_used, color_used_blank)
copy(buf, buf_blank)
// BUFFER SIXEL ROW, TRACK USED COLORS
for p := 0; p < 6; p++ {
y := (ix_srow * 6) + p
if y >= height {
break
}
for x := 0; x < width; x++ {
color_ix := pI.ColorIndexAt(x, y)
// SKIP FULLY-TRANSPARENT PIXELS
_, _, _, a := pI.Palette[color_ix].RGBA()
if a == 0 {
continue
}
color_used[color_ix] = true
buf[(width*int(color_ix))+x] |= 1 << uint(p)
}
}
// RENDER SIXEL ROW FOR EACH PALETTE ENTRY
bFirstColorWritten := false
for n := 0; n < nColors; n++ {
if !color_used[n] {
continue
}
// GRAPHICS CR (overwrite last line w/ new color)
if bFirstColorWritten {
if fnWri([]byte(`$`)) != nil {
return
}
}
// COLOR INTRODUCER (#)
tmpCI := make([]byte, 1, 4)
tmpCI[0] = byte('#')
tmpCI = strconv.AppendInt(tmpCI, int64(n), 10)
if fnWri(tmpCI) != nil {
return
}
rleCt := 0
cPrev := byte(255)
for x := 0; x < width; x++ {
// GET BUFFERED SIXEL
cNext := buf[(n*width)+x]
// RLE ENCODE, WRITE ON VALUE CHANGE
// USE 255 AS SENTINEL FOR INITIAL RUN
if (cPrev != 255) && (cNext != cPrev) {
if fnWri(encodeGRI(rleCt, cPrev)) != nil {
return
}
rleCt = 0
}
cPrev = cNext
rleCt++
}
// WRITE LAST SIXEL IN LINE
if fnWri(encodeGRI(rleCt, cPrev)) != nil {
return
}
bFirstColorWritten = true
}
}
// SIXEL TERMINATOR
fnWri([]byte(OSC_CLOSE))
return
}
func encodeGRI(rleCt int, sixl byte) []byte {
if rleCt <= 0 {
return nil
}
// MASK WITH VALID SIXEL BITS, APPLY OFFSET
sixl = SIXEL_MIN + (sixl & 0b111111)
tmpGRI := make([]byte, 0, 6)
if rleCt > 3 {
// GRAPHICS REPEAT INTRODUCER (!<repeat count><sixel>)
tmpGRI = append(tmpGRI, byte('!'))
tmpGRI = strconv.AppendInt(tmpGRI, int64(rleCt), 10)
tmpGRI = append(tmpGRI, sixl)
} else if rleCt > 0 {
for ix := 0; ix < rleCt; ix++ {
tmpGRI = append(tmpGRI, sixl)
}
}
return tmpGRI
}