-
Notifications
You must be signed in to change notification settings - Fork 0
/
img.go
108 lines (94 loc) · 2.03 KB
/
img.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
package insta
import (
"image"
"image/draw"
"image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/nfnt/resize"
)
func ShowImage(c Client, fname string) {
if strings.HasSuffix(fname, ".gif") {
showGif(c, fname, 0)
return
}
r, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer r.Close()
img, _, err := image.Decode(r)
if err != nil {
log.Fatal(err)
}
img = resize.Resize(ScreenWidth, ScreenHeight, img, resize.Bilinear)
scr := NewScreen()
bnds := image.Rect(0, 0, ScreenWidth, ScreenHeight)
draw.Draw(scr, bnds, img, image.ZP, draw.Over)
c.SetScreen(scr)
}
func showGif(c Client, fname string, d time.Duration) {
r, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer r.Close()
g, err := gif.DecodeAll(r)
if err != nil {
log.Fatal(err)
return
}
start := time.Now()
bnds := image.Rect(0, 0, ScreenWidth, ScreenHeight)
for {
scr := NewScreen()
for i := range g.Image {
img := resize.Resize(ScreenWidth, ScreenHeight, g.Image[i], resize.Bilinear)
op := draw.Over
if g.Disposal[i] == gif.DisposalBackground {
op = draw.Src
}
draw.Draw(scr, bnds, img, image.ZP, op)
c.SetScreenImmediate(scr)
time.Sleep(time.Duration(g.Delay[i]) * time.Millisecond)
}
if d == 0 {
return
} else if time.Since(start) > d {
return
}
}
}
func RandomGif(c Client, dir string, d time.Duration) {
gifs, _ := filepath.Glob(filepath.Join(dir, "*.gif"))
if len(gifs) == 0 {
return
}
i := rand.Intn(len(gifs))
showGif(c, gifs[i], d)
}
func ScrollImage(c Client, fname string) {
r, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer r.Close()
img, _, err := image.Decode(r)
if err != nil {
log.Fatal(err)
}
img = resize.Resize(0, ScreenHeight, img, resize.Bilinear)
steps := img.Bounds().Dx() + ScreenWidth + 1
for i := 0; i < steps; i++ {
scr := NewScreen()
bnds := image.Rect(ScreenWidth-i, 0, ScreenWidth, ScreenHeight)
draw.Draw(scr, bnds, img, image.ZP, draw.Over)
c.SetScreen(scr)
}
}