Skip to content
Snippets Groups Projects
Unverified Commit 2d8f5a52 authored by Sorunome's avatar Sorunome
Browse files

better handle frames

parent f8aa489e
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"image" "image"
"image/color"
"image/draw" "image/draw"
"image/gif" "image/gif"
"io" "io"
...@@ -240,23 +239,29 @@ func GenerateThumbnail(media *types.Media, width int, height int, method string, ...@@ -240,23 +239,29 @@ func GenerateThumbnail(media *types.Media, width int, height int, method string,
// Prepare a blank frame to use as swap space // Prepare a blank frame to use as swap space
frameImg := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{g.Config.Width, g.Config.Height}}) frameImg := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{g.Config.Width, g.Config.Height}})
// make sure the image is transparent at the start
draw.Draw(frameImg, frameImg.Bounds(), image.Transparent, image.ZP, draw.Src)
for i := range g.Image { for i := range g.Image {
img := g.Image[i] img := g.Image[i]
var disposal byte var disposal byte
if g.Disposal == nil { if g.Disposal == nil {
// go 1.14
disposal = 0 disposal = 0
} else { } else {
disposal = g.Disposal[i] disposal = g.Disposal[i]
} }
var previousImg draw.Image // if disposal type is 1 (preserve previous frame) we can get artifacts from re-scaling
// as such, we re-render those frames to disposal type 0 (start with a transparent frame)
// see https://www.w3.org/Graphics/GIF/spec-gif89a.txt
if disposal == 1 {
g.Disposal[i] = 0
} else {
draw.Draw(frameImg, frameImg.Bounds(), image.Transparent, image.ZP, draw.Src)
}
var previousFrameImg draw.Image
if disposal == 3 { if disposal == 3 {
previousImg = image.NewRGBA(g.Image[0].Bounds()) previousFrameImg = image.NewRGBA(frameImg.Bounds())
draw.Draw(previousImg, frameImg.Bounds(), frameImg, image.ZP, draw.Over) draw.Draw(previousFrameImg, frameImg.Bounds(), frameImg, image.ZP, draw.Over)
} }
// Copy the frame to a new image and use that // Copy the frame to a new image and use that
...@@ -274,24 +279,6 @@ func GenerateThumbnail(media *types.Media, width int, height int, method string, ...@@ -274,24 +279,6 @@ func GenerateThumbnail(media *types.Media, width int, height int, method string,
targetImg := image.NewPaletted(frameThumb.Bounds(), img.Palette) targetImg := image.NewPaletted(frameThumb.Bounds(), img.Palette)
draw.FloydSteinberg.Draw(targetImg, frameThumb.Bounds(), frameThumb, image.ZP) draw.FloydSteinberg.Draw(targetImg, frameThumb.Bounds(), frameThumb, image.ZP)
g.Image[i] = targetImg g.Image[i] = targetImg
// determine how to dispose the frame
// see https://www.w3.org/Graphics/GIF/spec-gif89a.txt
if disposal == 0 {
// Clear the transparency of the previous frame
draw.Draw(frameImg, frameImg.Bounds(), image.Transparent, image.ZP, draw.Src)
} else if disposal == 1 {
// do nothing
} else if disposal == 2 && g.Config.ColorModel != nil {
// restore background color
background := g.Config.ColorModel.(color.Palette)[g.BackgroundIndex]
if background != nil {
draw.Draw(frameImg, frameImg.Bounds(), image.NewUniform(background), image.ZP, draw.Src)
}
} else if disposal == 3 && previousImg != nil {
// restore previous frame
draw.Draw(frameImg, frameImg.Bounds(), previousImg, image.ZP, draw.Over)
}
} }
// Set the image size to the first frame's size // Set the image size to the first frame's size
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment