-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
288 lines (230 loc) · 5.98 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
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 main
import (
"bytes"
"flag"
"fmt"
"image"
"image/draw"
"image/gif"
"io"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/creack/pty"
"github.com/disintegration/imaging"
"github.com/gabriel-vasile/mimetype"
"github.com/mat/besticon/ico"
"github.com/mattn/go-isatty"
)
const RESIZE_OFFSET_Y = 8
const RESIZE_FACTOR_Y = 2
const RESIZE_FACTOR_X = 1
const DEFAULT_TERM_COLS = 80
const DEFAULT_TERM_ROWS = 24
const FPS = 15
const ANSI_CURSOR_UP = "\x1B[%dA"
const ANSI_CURSOR_HIDE = "\x1B[?25l"
const ANSI_CURSOR_SHOW = "\x1B[?25h"
const ANSI_BG_TRANSPARENT_COLOR = "\x1b[0;39;49m"
const ANSI_BG_RGB_COLOR = "\x1b[48;2;%d;%d;%dm"
const ANSI_FG_TRANSPARENT_COLOR = "\x1b[0m "
const ANSI_FG_RGB_COLOR = "\x1b[38;2;%d;%d;%dm▄"
const ANSI_RESET = "\x1b[0m"
var InterpolationType = imaging.Lanczos
func read(input string) []byte {
var err error
var buf []byte
if input == "" {
if buf, err = io.ReadAll(os.Stdin); err != nil {
log.Panicf("failed to read the stdin: %v", err)
}
} else {
if buf, err = os.ReadFile(input); err != nil {
log.Panicf("failed to read the input file: %v", err)
}
}
return buf
}
func decode(buf []byte) []image.Image {
mime, err := mimetype.DetectReader(bytes.NewReader(buf))
if err != nil {
log.Panicf("failed to detect the mime type: %v", err)
}
allowed := []string{"image/gif", "image/png", "image/jpeg", "image/bmp", "image/x-icon"}
if !mimetype.EqualsAny(mime.String(), allowed...) {
log.Fatalf("invalid MIME type: %s", mime.String())
}
frames := make([]image.Image, 0)
if mime.Is("image/gif") {
gifImage, err := gif.DecodeAll(bytes.NewReader(buf))
if err != nil {
log.Panicf("failed to decode the gif: %v", err)
}
var lowestX int
var lowestY int
var highestX int
var highestY int
for _, img := range gifImage.Image {
if img.Rect.Min.X < lowestX {
lowestX = img.Rect.Min.X
}
if img.Rect.Min.Y < lowestY {
lowestY = img.Rect.Min.Y
}
if img.Rect.Max.X > highestX {
highestX = img.Rect.Max.X
}
if img.Rect.Max.Y > highestY {
highestY = img.Rect.Max.Y
}
}
imgWidth := highestX - lowestX
imgHeight := highestY - lowestY
overPaintImage := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
draw.Draw(overPaintImage, overPaintImage.Bounds(), gifImage.Image[0], image.Point{}, draw.Src)
for _, srcImg := range gifImage.Image {
draw.Draw(overPaintImage, overPaintImage.Bounds(), srcImg, image.Point{}, draw.Over)
frame := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
draw.Draw(frame, frame.Bounds(), overPaintImage, image.Point{}, draw.Over)
frames = append(frames, frame)
}
return frames
} else {
var frame image.Image
var err error
if mime.Is("image/x-icon") {
frame, err = ico.Decode(bytes.NewReader(buf))
} else {
frame, _, err = image.Decode(bytes.NewReader(buf))
}
if err != nil {
log.Panicf("failed to decode the image: %v", err)
}
imb := frame.Bounds()
if imb.Max.X < 2 || imb.Max.Y < 2 {
log.Fatal("the input image is to small")
}
frames = append(frames, frame)
}
return frames
}
func scale(frames []image.Image) []image.Image {
type data struct {
i int
im image.Image
}
var err error
cols := DEFAULT_TERM_COLS
rows := DEFAULT_TERM_ROWS
if isatty.IsTerminal(os.Stdout.Fd()) {
if rows, cols, err = pty.Getsize(os.Stdout); err != nil {
log.Panicf("failed to get the terminal size: %v", err)
}
}
w := cols * RESIZE_FACTOR_X
h := (rows - RESIZE_OFFSET_Y) * RESIZE_FACTOR_Y
l := len(frames)
r := make([]image.Image, l)
c := make(chan *data, l)
for i, f := range frames {
go func(i int, f image.Image) {
c <- &data{i, imaging.Fit(f, w, h, InterpolationType)}
}(i, f)
}
for range r {
d := <-c
r[d.i] = d.im
}
return r
}
func escape(frames []image.Image) [][]string {
type data struct {
i int
str string
}
escaped := make([][]string, 0)
for _, f := range frames {
imb := f.Bounds()
maxY := imb.Max.Y - imb.Max.Y%2
maxX := imb.Max.X
c := make(chan *data, maxY/2)
lines := make([]string, maxY/2)
for y := 0; y < maxY; y += 2 {
go func(y int) {
var sb strings.Builder
for x := 0; x < maxX; x++ {
r, g, b, a := f.At(x, y).RGBA()
if a>>8 < 128 {
sb.WriteString(ANSI_BG_TRANSPARENT_COLOR)
} else {
sb.WriteString(fmt.Sprintf(ANSI_BG_RGB_COLOR, r>>8, g>>8, b>>8))
}
r, g, b, a = f.At(x, y+1).RGBA()
if a>>8 < 128 {
sb.WriteString(ANSI_FG_TRANSPARENT_COLOR)
} else {
sb.WriteString(fmt.Sprintf(ANSI_FG_RGB_COLOR, r>>8, g>>8, b>>8))
}
}
sb.WriteString(ANSI_RESET)
sb.WriteString("\n")
c <- &data{y / 2, sb.String()}
}(y)
}
for range lines {
line := <-c
lines[line.i] = line.str
}
escaped = append(escaped, lines)
}
return escaped
}
func print(frames [][]string) {
if isatty.IsTerminal(os.Stdout.Fd()) {
defer enableEcho(disableEcho())
}
os.Stdout.WriteString(ANSI_CURSOR_HIDE)
os.Stdout.WriteString("\n")
frameCount := len(frames)
if frameCount == 1 {
os.Stdout.WriteString(strings.Join(frames[0], ""))
} else {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
tick := time.Tick(time.Second / time.Duration(FPS))
h := len(frames[0]) + 2 // two extra lines for the exit msg
playing := true
go func() {
<-c
playing = false
}()
for i := 0; playing; i++ {
if i != 0 {
os.Stdout.WriteString(fmt.Sprintf(ANSI_CURSOR_UP, h))
}
os.Stdout.WriteString(strings.Join(frames[i%frameCount], ""))
os.Stdout.WriteString("\npress `ctrl c` to exit\n")
<-tick
}
}
os.Stdout.WriteString(ANSI_RESET)
os.Stdout.WriteString(ANSI_CURSOR_SHOW)
}
func main() {
interpolation := flag.String("interpolation", "lanczos", "Interpolation method. Options: lanczos, nearest")
ParseFlags()
input := ""
if len(flag.Args()) > 0 {
args := flag.Args()
input = args[0]
}
switch *interpolation {
case "nearest":
InterpolationType = imaging.NearestNeighbor
default:
InterpolationType = imaging.Lanczos
}
print(escape(scale(decode(read(input)))))
}