-
Notifications
You must be signed in to change notification settings - Fork 3
/
gotermimg.go
113 lines (93 loc) · 2.61 KB
/
gotermimg.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
// Package gotermimg provides functions to convert image.Image structs
// to 256 color terminal compatible []string.
// It also provides functions to convert and "play" gif animations.
package gotermimg
import (
"fmt"
"image"
"github.com/moshen/gotermimg/terminal"
)
type Converter func(image.Image, Transformer) []string
const (
space = " "
spaces = " "
top = "▀"
bottom = "▄"
)
// Converts image.Image img to a []string of 256 color terminal compatbile
// ANSI using 2 spaces as a "pixel". Applies Transformer trans to img
// before conversion
func ANSI(img image.Image, trans Transformer) []string {
if trans != nil {
img = trans(img)
}
bounds := img.Bounds()
termimg := make([]string, bounds.Dy(), bounds.Dy())
for i := 0; i < bounds.Dy(); i++ {
row := ""
local := ""
curcolor := terminal.ColorCode(0)
for j := 0; j < bounds.Dx(); j++ {
newcolor := terminal.FindColorCode(img.At(j, i))
if newcolor != curcolor {
if curcolor > 0 {
row += terminal.Bg(curcolor, local)
} else {
row += terminal.Reset + local
}
curcolor = newcolor
local = ""
}
local += spaces
}
if curcolor > 0 {
row += terminal.Bg(curcolor, local)
} else {
row += terminal.Reset + local
}
termimg[i] = row + terminal.Reset
}
return termimg
}
// Converts image.Image img to a []string of 256 color terminal compatbile
// UTF8 using UTF8 1/2 blocks as a "pixel". Applies Transformer trans to img
// before conversion
func UTF8(img image.Image, trans Transformer) []string {
if trans != nil {
img = trans(img)
}
bounds := img.Bounds()
termimg := make([]string, (bounds.Dy()/2)+1, (bounds.Dy()/2)+1)
rownum := 0
for i := 0; i < bounds.Dy(); i += 2 {
row := ""
for j := 0; j < bounds.Dx(); j++ {
colortop := terminal.FindColorCode(img.At(j, i))
colorbottom := terminal.FindColorCode(img.At(j, i+1))
if colortop == colorbottom {
if colortop > 0 {
row += terminal.Bg(colortop, space)
} else {
row += terminal.Reset + space
}
} else {
if colortop > 0 && colorbottom > 0 {
row += terminal.Bg(colorbottom, terminal.Fg(colortop, top))
} else if colortop > 0 && colorbottom == 0 {
row += terminal.Reset + terminal.Fg(colortop, top)
} else if colortop == 0 && colorbottom > 0 {
row += terminal.Reset + terminal.Fg(colorbottom, bottom)
}
}
}
termimg[rownum] = row + terminal.Reset
rownum++
}
return termimg
}
// Prints image.Image img to os.Stdout using Converter with Transformer trans
func PrintImage(img image.Image, conv Converter, trans Transformer) {
for _, v := range conv(img, trans) {
fmt.Println(v)
}
}