Skip to content

Commit

Permalink
[refactor] Make generation faster using .Pix
Browse files Browse the repository at this point in the history
  • Loading branch information
nwtgck committed Jul 30, 2019
1 parent e3d714c commit 9d1ae8c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions gif_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ import (
"image/gif"
)

func clonePix(pix []uint8) []uint8 {
p := make([]uint8, len(pix))
copy(p, pix)
return p
}

func AddProgressBar(inOutGif *gif.GIF, barTop bool, barHeight int, barColor color.RGBA) {
// NOTE: inOutGif is changed destructively
width := inOutGif.Config.Width
height := inOutGif.Config.Height
imageLen := len(inOutGif.Image)
// Image size
imageSize := image.Rect(0, 0, width, height)
// Previous frame
previousImage := inOutGif.Image[0].SubImage(imageSize)
firstPalette := inOutGif.Image[0].Palette
// Previous pixels
previousPix := inOutGif.Image[0].Pix
for i, paletted := range inOutGif.Image {
// Create new empty paletted
newPaletted := image.NewPaletted(imageSize, firstPalette)
newPaletted := image.NewPaletted(imageSize, paletted.Palette)
// Copy previous frame to the new paletted
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
newPaletted.Set(x, y, previousImage.At(x, y))
}
}
// Copy whole image to the new paletted
newPaletted.Pix = previousPix
// Copy diff to the new paletted
rect := paletted.Rect
for x := rect.Min.X; x < rect.Max.X; x++ {
for y := rect.Min.Y; y < rect.Max.Y; y++ {
newPaletted.Set(x, y, paletted.At(x, y))
}
}
// Save as previous
previousImage = newPaletted.SubImage(imageSize)
previousPix = clonePix(newPaletted.Pix)

// Attach progress bar
w := int(float32(width) * ((float32(i)+1)/float32(imageLen)))
Expand Down

0 comments on commit 9d1ae8c

Please sign in to comment.