From 9d1ae8c95a3867cae5560ea484645c93ae223f7d Mon Sep 17 00:00:00 2001 From: Ryo Ota Date: Wed, 31 Jul 2019 01:27:57 +0900 Subject: [PATCH] [refactor] Make generation faster using .Pix --- gif_progress.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/gif_progress.go b/gif_progress.go index f117d53..effe5b5 100644 --- a/gif_progress.go +++ b/gif_progress.go @@ -6,6 +6,12 @@ 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 @@ -13,19 +19,14 @@ func AddProgressBar(inOutGif *gif.GIF, barTop bool, barHeight int, barColor colo 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++ { @@ -33,7 +34,7 @@ func AddProgressBar(inOutGif *gif.GIF, barTop bool, barHeight int, barColor colo } } // Save as previous - previousImage = newPaletted.SubImage(imageSize) + previousPix = clonePix(newPaletted.Pix) // Attach progress bar w := int(float32(width) * ((float32(i)+1)/float32(imageLen)))