forked from andybons/gogif
/
crop.go
71 lines (59 loc) · 1.47 KB
/
crop.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
package main
// This demonstrates a problem with the encoder where pixels are only written
// to half of the cropped image. I believe that it is related to images where
// the bounds do not start at (0, 0).
//
// Usage something like this:
// go run examples/crop.go && open blob.out.gif shapes.out.gif
import (
"image"
"image/draw"
"image/gif"
"log"
"os"
"github.com/andybons/gogif"
)
func main() {
crop("shapes")
crop("blob")
}
func crop(filename string) {
f, err := os.Open("testdata/" + filename + ".gif")
if err != nil {
log.Fatal(err.Error())
}
defer f.Close()
im, err := gif.DecodeAll(f)
if err != nil {
log.Fatal(err.Error())
}
firstFrame := im.Image[0]
imgBounds := firstFrame.Bounds()
img := image.NewRGBA(imgBounds)
for index, frame := range im.Image {
bounds := frame.Bounds()
draw.Draw(img, bounds, frame, bounds.Min, draw.Src)
im.Image[index] = ImageToPaletted(img.SubImage(image.Rect(
imgBounds.Min.X,
imgBounds.Min.Y+imgBounds.Dy()/4,
imgBounds.Max.X,
imgBounds.Max.Y-imgBounds.Dy()/4)))
}
out, err := os.Create(filename + ".out.gif")
if err != nil {
log.Fatal(err.Error())
}
defer out.Close()
gogif.EncodeAll(out, im)
}
// Converts an image to an image.Paletted with 256 colors.
func ImageToPaletted(img image.Image) *image.Paletted {
pm, ok := img.(*image.Paletted)
if !ok {
b := img.Bounds()
pm = image.NewPaletted(b, nil)
q := &gogif.MedianCutQuantizer{NumColor: 256}
q.Quantize(pm, b, img, image.ZP)
}
return pm
}