forked from stroborobo/aimg
/
main.go
131 lines (111 loc) · 2.5 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
package main
import (
"fmt"
flag "github.com/ogier/pflag"
"os"
"path/filepath"
"strings"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/Knorkebrot/ansirgb"
"github.com/olekukonko/ts"
)
type Block struct {
top *ansirgb.Color
bottom *ansirgb.Color
}
func (b *Block) String() string {
ret := b.bottom.Bg()
if b.top != nil {
ret += b.top.Fg()
// If it's not a UTF-8 terminal, fall back to '#'
if strings.Contains(os.Getenv("LC_ALL"), "UTF-8") ||
strings.Contains(os.Getenv("LANG"), "UTF-8") {
ret += "\u2580"
} else {
ret += "#"
}
} else {
ret += " "
}
return ret
}
func cursorUp(count int) {
fmt.Printf("\033[%dA", count)
}
func reset() {
// add a space to prevent artifacts after resizing
fmt.Printf("\033[0m ")
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s file [...]\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
var width int
flag.IntVarP(&width, "width", "w", 0, "Output width, use 0 for terminal width")
flag.Usage = usage
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
if width == 0 {
size, err := ts.GetSize()
if err != nil {
fmt.Fprintln(os.Stderr, err, "\nYou may need to "+
"set width manually using -w num")
os.Exit(2)
}
width = size.Col() - 1 // -1 for the reset column
}
for _, fpath := range flag.Args() {
fh, err := os.Open(fpath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(10)
}
img, _, err := image.Decode(fh)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fh.Close()
os.Exit(20)
}
fh.Close()
imgWidth := img.Bounds().Dx()
imgHeight := img.Bounds().Dy()
if imgWidth < width {
width = imgWidth
}
ratio := float64(imgWidth) / float64(width)
rows := int(float64(imgHeight) / ratio)
for i := 1; i < rows; i += 2 {
fmt.Println("")
}
cursorUp(rows / 2)
for i := 1; i < rows; i += 2 {
for j := 0; j < width; j++ {
// TODO: get average color of the area instead
// of one pixel?
x := int(ratio * float64(j))
yTop := int(ratio * float64(i - 1))
yBottom := int(ratio * float64(i))
top := ansirgb.Convert(img.At(x, yTop))
bottom := ansirgb.Convert(img.At(x, yBottom))
b := &Block{}
b.bottom = bottom
// Foreground colors are lighter in some terminals.
// Ignore top (FG) if it's the same color anyway
if top.Code != bottom.Code {
b.top = top
}
fmt.Printf("%s", b)
}
reset()
fmt.Printf("\n")
}
fmt.Println("File:", filepath.Base(fpath), "size:", imgWidth, "x", imgHeight)
}
}