Fix animation timing

The timing of the final animation frame wasn't being recorded correctly.
master
Rohan Singh 6 years ago
parent 1c4bc9fe39
commit 7430f2cbfa

@ -21,8 +21,8 @@ import "C"
import ( import (
"errors" "errors"
"fmt" "fmt"
"time"
"image" "image"
"time"
"unsafe" "unsafe"
) )
@ -82,7 +82,9 @@ func (ae *AnimationEncoder) AddFrame(img image.Image, duration time.Duration) er
return errors.New("unsupported image type") return errors.New("unsupported image type")
} }
timestamp := C.int((duration + ae.duration) / time.Millisecond) timestamp := C.int(ae.duration / time.Millisecond)
ae.duration += duration
if C.WebPAnimEncoderAdd(ae.c, pic, timestamp, nil) == 0 { if C.WebPAnimEncoderAdd(ae.c, pic, timestamp, nil) == 0 {
return fmt.Errorf( return fmt.Errorf(
"Encoding error: %d - %s", "Encoding error: %d - %s",
@ -91,14 +93,13 @@ func (ae *AnimationEncoder) AddFrame(img image.Image, duration time.Duration) er
) )
} }
ae.duration += duration
return nil return nil
} }
// Assemble assembles all frames into animated WebP. // Assemble assembles all frames into animated WebP.
func (ae *AnimationEncoder) Assemble() ([]byte, error) { func (ae *AnimationEncoder) Assemble() ([]byte, error) {
// add final empty frame // add final empty frame
if C.WebPAnimEncoderAdd(ae.c, nil, C.int(ae.duration / time.Millisecond), nil) == 0 { if C.WebPAnimEncoderAdd(ae.c, nil, C.int(ae.duration/time.Millisecond), nil) == 0 {
return nil, errors.New("Couldn't add final empty frame") return nil, errors.New("Couldn't add final empty frame")
} }

Loading…
Cancel
Save