From 129d51a1e6e24126f4b6296565faef4cbf8a267b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Fri, 28 Apr 2023 18:26:25 -0300 Subject: [PATCH] fix gif pallete --- lib/xgif/xgif.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/xgif/xgif.go b/lib/xgif/xgif.go index 967b446fc..a0d21695a 100644 --- a/lib/xgif/xgif.go +++ b/lib/xgif/xgif.go @@ -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 {