-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
87 lines (70 loc) · 1.89 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
package main
import (
"fmt"
"image"
"os"
"time"
_ "image/jpeg"
_ "image/png"
"github.com/nsf/termbox-go"
)
func draw(img image.Image) {
// Get terminal size and cursor width/height ratio
width, height, whratio := canvasSize()
bounds := img.Bounds()
imgW, imgH := bounds.Dx(), bounds.Dy()
imgScale := scale(imgW, imgH, width, height, whratio)
// Resize canvas to fit scaled image
width, height = int(float64(imgW)/imgScale), int(float64(imgH)/(imgScale*whratio))
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// Calculate average color for the corresponding image rectangle
// fitting in this cell. We use a half-block trick, wherein the
// lower half of the cell displays the character ▄, effectively
// doubling the resolution of the canvas.
startX, startY, endX, endY := imgArea(x, y, imgScale, whratio)
r, g, b := avgRGB(img, startX, startY, endX, (startY+endY)/2)
colorUp := termbox.Attribute(termColor(r, g, b))
r, g, b = avgRGB(img, startX, (startY+endY)/2, endX, endY)
colorDown := termbox.Attribute(termColor(r, g, b))
termbox.SetCell(x, y, '▄', colorDown, colorUp)
}
}
termbox.Flush()
}
func display(image string) {
img, err := load(image)
if err != nil {
panic(err)
}
draw(img)
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc || ev.Ch == 'q' {
return
}
case termbox.EventResize:
draw(img)
default:
time.Sleep(10 * time.Millisecond)
}
}
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s <filename>...\n\n", os.Args[0])
fmt.Println("Close the image with <ESC> or by pressing 'q'.")
os.Exit(1)
}
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
for i := 1; i < len(os.Args); i++ {
display(os.Args[i])
}
}