-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrenderer.go
94 lines (83 loc) · 2.3 KB
/
renderer.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
package main
import (
"image"
"image/color"
"image/draw"
"github.com/bmatsuo/img2ansi/gif"
)
type gifrenderer struct {
GIF *gif.GIF
Draw func(bounds image.Rectangle) draw.Image
Frames []image.Image
frame draw.Image
index int
}
func newGIFRenderer(g *gif.GIF, draw func(image.Rectangle) draw.Image) *gifrenderer {
return &gifrenderer{
GIF: g,
Draw: draw,
index: -1,
}
}
func (r *gifrenderer) Frame() image.Image {
return r.Frames[r.index]
}
func (r *gifrenderer) RenderAll() {
for r.RenderNext() {
}
}
func (r *gifrenderer) RenderNext() (ok bool) {
i := (r.index + 1) % len(r.GIF.Image)
r.index = i
if len(r.Frames) <= i {
r.renderFrame(i)
return true
}
return false
}
func (r *gifrenderer) renderFrame(i int) {
m := r.GIF.Image[i]
bounds := image.Rect(0, 0, r.GIF.Config.Width, r.GIF.Config.Height)
if i == 0 {
disposal := r.GIF.Disposal[len(r.GIF.Image)-1]
r.frame = r.Draw(bounds)
var fill image.Image
if disposal == gif.DisposalBackground {
fill = image.NewUniform(r.GIF.Config.ColorModel.(color.Palette)[r.GIF.BackgroundIndex])
} else {
fill = image.NewUniform(color.Transparent)
}
draw.Draw(r.frame, bounds, fill, bounds.Min, draw.Src)
} else {
disposal := r.GIF.Disposal[i-1]
// disposal unspecified and DisposalNone are handled the same way, leave the frame as it is
if disposal == gif.DisposalBackground {
c := r.GIF.Config.ColorModel.(color.Palette)[r.GIF.BackgroundIndex]
img := image.NewUniform(c)
draw.Draw(r.frame, bounds, img, bounds.Min, draw.Src)
} else if disposal == gif.DisposalPrevious {
fill := image.NewUniform(color.Transparent)
draw.Draw(r.frame, bounds, fill, bounds.Min, draw.Src)
}
}
// draw the image over the virtual screen. transparency is checked
// directly instead of blending because GIF89a has binary
// transparency (no alpha channel).
for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
color := m.ColorIndexAt(x, y)
if r.GIF.HasTransparent[i] && color == r.GIF.Transparent[i] {
continue
}
r.frame.Set(x, y, m.Palette[color])
}
}
framecp := r.Draw(bounds)
draw.Draw(framecp, bounds, r.frame, bounds.Min, draw.Over)
r.Frames = append(r.Frames, framecp)
}
func (r *gifrenderer) RenderFrames() {
for i := range r.GIF.Image {
r.renderFrame(i)
}
}