5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 03:31:37 +00:00
matterbridge/vendor/github.com/Benau/tgsconverter/libtgsconverter/apng.go
Benau 53cafa9f3d
Convert .tgs with go libraries (and cgo) (telegram) (#1569)
This commit adds support for go/cgo tgs conversion when building with the -tags `cgo`
The default binaries are still "pure" go and uses the old way of converting.

* Move lottie_convert.py conversion code to its own file

* Add optional libtgsconverter

* Update vendor

* Apply suggestions from code review

* Update bridge/helper/libtgsconverter.go

Co-authored-by: Wim <wim@42.be>
2021-08-24 22:32:50 +02:00

52 lines
1.3 KiB
Go

package libtgsconverter
import "bytes"
import "image"
import "github.com/kettek/apng"
import "github.com/av-elier/go-decimal-to-rational"
type toapng struct {
apng apng.APNG
prev_frame *image.RGBA
}
func(to_apng *toapng) init(w uint, h uint, options ConverterOptions) {
}
func(to_apng *toapng) SupportsAnimation() bool {
return true
}
func (to_apng *toapng) AddFrame(image *image.RGBA, fps uint) error {
if to_apng.prev_frame != nil && sameImage(to_apng.prev_frame, image) {
var idx = len(to_apng.apng.Frames) - 1
var prev_fps = float64(to_apng.apng.Frames[idx].DelayNumerator) / float64(to_apng.apng.Frames[idx].DelayDenominator)
prev_fps += 1.0 / float64(fps)
rat := dectofrac.NewRatP(prev_fps, 0.001)
to_apng.apng.Frames[idx].DelayNumerator = uint16(rat.Num().Int64())
to_apng.apng.Frames[idx].DelayDenominator = uint16(rat.Denom().Int64())
return nil
}
f := apng.Frame{}
f.Image = image
f.DelayNumerator = 1
f.DelayDenominator = uint16(fps)
f.DisposeOp = apng.DISPOSE_OP_BACKGROUND
f.BlendOp = apng.BLEND_OP_SOURCE
f.IsDefault = false
to_apng.apng.Frames = append(to_apng.apng.Frames, f)
to_apng.prev_frame = image
return nil
}
func (to_apng *toapng) Result() []byte {
var data []byte
w := bytes.NewBuffer(data)
err := apng.Encode(w, to_apng.apng)
if err != nil {
return nil
}
return w.Bytes()
}