fix gif pallete

This commit is contained in:
Júlio César Batista 2023-04-28 18:26:25 -03:00
parent c00cd8dbc7
commit 129d51a1e6
No known key found for this signature in database
GPG key ID: 10C4B861BF314878

View file

@ -25,7 +25,6 @@ import (
) )
const INFINITE_LOOP = 0 const INFINITE_LOOP = 0
const BG_INDEX uint8 = 255
var BG_COLOR = color.White 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 left := (width - bounds.Dx()) / 2
right := left + bounds.Dx() 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) frame := image.NewPaletted(image.Rect(0, 0, width, height), palettedImg.Palette)
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
if x <= left || y <= top || x >= right || y >= bottom { if x <= left || y <= top || x >= right || y >= bottom {
frame.SetColorIndex(x, y, BG_INDEX) frame.SetColorIndex(x, y, uint8(bgIndex))
} else { } else {
frame.SetColorIndex(x, y, palettedImg.ColorIndexAt(x-left, y-top)) 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 return err
} }
if anim.LoopCount != INFINITE_LOOP { if nFrames > 1 && anim.LoopCount != INFINITE_LOOP {
return fmt.Errorf("expected infinite loop, got=%d", anim.LoopCount) 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 { if len(anim.Image) != nFrames {