-
Notifications
You must be signed in to change notification settings - Fork 103
/
image.go
197 lines (161 loc) · 4.83 KB
/
image.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package render
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
// register image formats
_ "image/jpeg"
_ "image/png"
"github.com/nfnt/resize"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"github.com/tidbyt/gg"
)
// Image renders the binary image data passed via `src`. Supported
// formats include PNG, JPEG, GIF, and SVG.
//
// If `width` or `height` are set, the image will be scaled
// accordingly, with nearest neighbor interpolation. Otherwise the
// image's original dimensions are used.
//
// If the image data encodes an animated GIF, the Image instance will
// also be animated. Frame delay (in milliseconds) can be read from
// the `delay` attribute.
//
// DOC(Src): Binary image data or SVG text
// DOC(Width): Scale image to this width
// DOC(Height): Scale image to this height
// DOC(Delay): (Read-only) Frame delay in ms, for animated GIFs
type Image struct {
Widget
Src string `starlark:"src,required"`
Width, Height int
Delay int `starlark:"delay,readonly"`
imgs []image.Image
}
func (p *Image) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
return p.imgs[ModInt(frameIdx, len(p.imgs))].Bounds()
}
func (p *Image) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
dc.DrawImage(p.imgs[ModInt(frameIdx, len(p.imgs))], 0, 0)
}
func (p *Image) Size() (int, int) {
return p.imgs[0].Bounds().Dx(), p.imgs[0].Bounds().Dy()
}
func (p *Image) FrameCount() int {
return len(p.imgs)
}
func (p *Image) InitFromGIF(data []byte) error {
// Consider using WebP instead.
img, err := gif.DecodeAll(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("decoding image data: %v", err)
}
p.Delay = img.Delay[0] * 10
var prev_src *image.Paletted
disposal_length := len(img.Disposal)
compositing_op := draw.Src
last := image.NewRGBA(image.Rect(0, 0, img.Config.Width, img.Config.Height))
for index, src := range img.Image {
bounds := img.Image[index].Bounds()
disposal_method := img.Disposal[index]
is_disposal_previous := disposal_method == gif.DisposalPrevious
// if the frame is DisposalPrevious
// reset to the last non-DisposalPrevious frame
if is_disposal_previous && prev_src != nil {
draw.Draw(last, last.Bounds(), prev_src, image.ZP, draw.Over)
}
// if this is a non-DisposalPrevious frame
// and the next frame is DisposalPrevious
// store the src to reset before the next frame draws
if !is_disposal_previous && index+1 < disposal_length && img.Disposal[index+1] == gif.DisposalPrevious {
prev_src = src
}
draw.Draw(last, bounds, img.Image[index], image.Point{bounds.Min.X, bounds.Min.Y}, compositing_op)
frame := *last
frame.Pix = make([]uint8, len(last.Pix))
copy(frame.Pix, last.Pix)
// if this is a non-DisposalPrevious frame
// set the compositing operation to Over
if !is_disposal_previous {
compositing_op = draw.Over
}
// if the frame is DisposalBackground
// remove the frame pixels
if disposal_method == gif.DisposalBackground {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
last.Set(x, y, color.Transparent)
}
}
}
p.imgs = append(p.imgs, &frame)
}
return nil
}
func (p *Image) InitFromImage(data []byte) error {
im, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("decoding image data: %v", err)
}
p.imgs = []image.Image{im}
return nil
}
func (p *Image) InitFromSVG(data []byte) error {
svgData, _ := oksvg.ReadIconStream(bytes.NewReader(data), oksvg.StrictErrorMode)
w := int(svgData.ViewBox.W)
h := int(svgData.ViewBox.H)
if w == 0 && h == 0 {
return errors.New("decoding svg data failed")
}
svgData.SetTarget(0, 0, float64(w), float64(h))
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
svgData.Draw(rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())), 1)
buf := new(bytes.Buffer)
err := jpeg.Encode(buf, rgba, nil)
if err != nil {
return err
}
err = p.InitFromImage([]byte(buf.Bytes()))
if err != nil {
return err
}
return nil
}
func (p *Image) Init() error {
err := p.InitFromWebP([]byte(p.Src))
if err != nil {
err = p.InitFromGIF([]byte(p.Src))
if err != nil {
err = p.InitFromSVG([]byte(p.Src))
if err != nil {
err = p.InitFromImage([]byte(p.Src))
}
}
}
if err != nil {
return err
}
w := p.imgs[0].Bounds().Dx()
h := p.imgs[0].Bounds().Dy()
if p.Width != 0 || p.Height != 0 {
nw, nh := p.Width, p.Height
if nw == 0 {
// scale width, maintaining original aspect ratio
nw = int(float64(nh) * (float64(w) / float64(h)))
}
if nh == 0 {
// scale height, maintaining original aspect ratio
nh = int(float64(nw) * (float64(h) / float64(w)))
}
for i := 0; i < len(p.imgs); i++ {
p.imgs[i] = resize.Resize(uint(nw), uint(nh), p.imgs[i], resize.NearestNeighbor)
}
}
return nil
}