-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (105 loc) · 2.8 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
package main
import (
"fmt"
"image"
"image/color"
"image/gif"
"log"
"os"
"strconv"
"time"
)
const (
CHARS = " .:-=+*#%@"
// chars add more detail
//CHARS = " .':-~=+*#%@"
)
// reduce the size of the gif to 100x100
func resizeFrames(frames []*image.Paletted, width, height int) []*image.Paletted {
hScale := float64(height) / float64(frames[0].Bounds().Dy())
wScale := float64(width) / float64(frames[0].Bounds().Dx())
resizedFrames := make([]*image.Paletted, len(frames))
for i, frame := range frames {
newWidth := int(float64(frame.Bounds().Dx()) * wScale)
newHeight := int(float64(frame.Bounds().Dy()) * hScale)
resizedFrames[i] = image.NewPaletted(image.Rect(0, 0, newWidth, newHeight), frame.Palette)
for x := 0; x < newWidth; x++ {
for y := 0; y < newHeight; y++ {
resizedFrames[i].Set(x, y, frame.At(int(float64(x)/wScale), int(float64(y)/hScale)))
}
}
}
return resizedFrames
}
func readGif(fileName string) *gif.GIF {
file, err := os.Open("gifs/tom.gif")
if err != nil {
log.Fatalf("Error opening file: %s", err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalf("Error closing file: %s", err)
}
}(file)
gifImage, err := gif.DecodeAll(file)
if err != nil {
log.Fatalf("Error decoding file: %s", err)
}
return gifImage
}
func readFrames(fileName string, reqSize int) []*image.Paletted {
gifImage := readGif(fileName)
// resize the gif
gifImage.Image = resizeFrames(gifImage.Image, reqSize, reqSize)
return gifImage.Image
}
// convert color to grayscale
func grayscaleChar(color color.Color) int {
r, g, b, _ := color.RGBA()
val := int(0.299*float64(r)+0.587*float64(g)+0.114*float64(b)) / 256
val = val * (len(CHARS) - 1) / 255
return val
}
func clearConsole() {
fmt.Print("\033[H\033[2J")
}
func main() {
reqSize := 64
args := os.Args[1:]
if len(args) > 0 {
newReqSize := 0
for _, arg := range args {
val, _ := strconv.Atoi(arg)
newReqSize += val
}
reqSize = newReqSize
}
fileName := "gifs/tom.gif"
gifImage := readFrames(fileName, reqSize)
frameValues := make([][]int, len(gifImage))
for i, frame := range gifImage {
frameValues[i] = make([]int, frame.Bounds().Dx()*frame.Bounds().Dy())
for x := 0; x < frame.Bounds().Dx(); x++ {
for y := 0; y < frame.Bounds().Dy(); y++ {
frameValues[i][x*frame.Bounds().Dy()+y] = grayscaleChar(frame.At(x, y))
}
}
}
for {
for i, frame := range frameValues {
// roate the image 90 degrees
for y := 0; y < gifImage[i].Bounds().Dy(); y++ {
for x := 0; x < gifImage[i].Bounds().Dx(); x++ {
char := CHARS[frame[x*gifImage[i].Bounds().Dy()+y]]
fmt.Printf("%c%c%c", char, char, char)
}
fmt.Printf("\n")
}
fmt.Printf("\n\n")
time.Sleep(40 * time.Millisecond)
clearConsole()
}
time.Sleep(100 * time.Millisecond)
}
}