fix gif pallete
This commit is contained in:
parent
c00cd8dbc7
commit
129d51a1e6
1 changed files with 12 additions and 4 deletions
|
|
@ -25,7 +25,6 @@ import (
|
|||
)
|
||||
|
||||
const INFINITE_LOOP = 0
|
||||
const BG_INDEX uint8 = 255
|
||||
|
||||
var BG_COLOR = color.White
|
||||
|
||||
|
|
@ -85,12 +84,19 @@ func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, er
|
|||
left := (width - bounds.Dx()) / 2
|
||||
right := left + bounds.Dx()
|
||||
|
||||
palettedImg.Palette[BG_INDEX] = BG_COLOR
|
||||
var bgIndex int
|
||||
if len(palettedImg.Palette) == 256 {
|
||||
bgIndex = 255
|
||||
palettedImg.Palette[255] = BG_COLOR
|
||||
} else {
|
||||
bgIndex = len(palettedImg.Palette)
|
||||
palettedImg.Palette = append(palettedImg.Palette, BG_COLOR)
|
||||
}
|
||||
frame := image.NewPaletted(image.Rect(0, 0, width, height), palettedImg.Palette)
|
||||
for x := 0; x < width; x++ {
|
||||
for y := 0; y < height; y++ {
|
||||
if x <= left || y <= top || x >= right || y >= bottom {
|
||||
frame.SetColorIndex(x, y, BG_INDEX)
|
||||
frame.SetColorIndex(x, y, uint8(bgIndex))
|
||||
} else {
|
||||
frame.SetColorIndex(x, y, palettedImg.ColorIndexAt(x-left, y-top))
|
||||
}
|
||||
|
|
@ -115,8 +121,10 @@ func Validate(gifBytes []byte, nFrames int, intervalMS int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if anim.LoopCount != INFINITE_LOOP {
|
||||
if nFrames > 1 && anim.LoopCount != INFINITE_LOOP {
|
||||
return fmt.Errorf("expected infinite loop, got=%d", anim.LoopCount)
|
||||
} else if nFrames == 1 && anim.LoopCount != -1 {
|
||||
return fmt.Errorf("wrong loop count for single frame gif, got=%d", anim.LoopCount)
|
||||
}
|
||||
|
||||
if len(anim.Image) != nFrames {
|
||||
|
|
|
|||
Loading…
Reference in a new issue