forked from rainycape/magick
/
gif.go
74 lines (68 loc) · 1.68 KB
/
gif.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
package magick
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
)
var (
gifsicleCmd string
convertCmd string
errNoConvert = errors.New("error decoding GIF image: Corrupt data. Install imagemagick (convert) to try to fix it.")
errNoGifsicle = errors.New("error decoding GIF image: Corrupt data. Install gifsicle to try to fix it.")
maxGifTries = 2
)
func looksLikeGif(data []byte) bool {
return bytes.HasPrefix(data, []byte{'G', 'I', 'F'})
}
func runGifsicle(data []byte, args []string) ([]byte, error) {
cmd := exec.Command(gifsicleCmd, args...)
cmd.Stdin = bytes.NewReader(data)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
// Workaround bug in gifsicle 1.71
if strings.Contains(err.Error(), "segmentation fault") {
return runGifsicle(data, append(args, "--colors=256"))
}
return nil, fmt.Errorf("error running gifsicle: %s", err)
}
return out.Bytes(), nil
}
func fixAndDecodeGif(data []byte, try int) (*Image, error) {
if gifsicleCmd == "" {
return nil, errNoGifsicle
}
args := []string{"--careful"}
if try > 0 {
args = append(args, "--unoptimize")
}
data, err := runGifsicle(data, args)
if err != nil {
return nil, err
}
if try > 1 {
if convertCmd == "" {
return nil, errNoConvert
}
cmd := exec.Command(convertCmd, "-", "-")
cmd.Stdin = bytes.NewReader(data)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("error running convert: %s", err)
}
data = out.Bytes()
}
return decodeData(data, try+1)
}
func init() {
gifsicleCmd, _ = exec.LookPath("gifsicle")
if Backend() == "GraphicsMagick" {
maxGifTries = 3
convertCmd, _ = exec.LookPath("convert")
}
}