summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go
diff options
context:
space:
mode:
authorBenau <Benau@users.noreply.github.com>2021-08-25 04:32:50 +0800
committerGitHub <noreply@github.com>2021-08-24 22:32:50 +0200
commit53cafa9f3d0c8be33821fc7338b1da97e91d9cc6 (patch)
tree964a225219099a1a1c282e27913767da588191b4 /vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go
parentd4195deb3a6305c49c50ff30e8af978c7f1bdd92 (diff)
downloadmatterbridge-msglm-53cafa9f3d0c8be33821fc7338b1da97e91d9cc6.tar.gz
matterbridge-msglm-53cafa9f3d0c8be33821fc7338b1da97e91d9cc6.tar.bz2
matterbridge-msglm-53cafa9f3d0c8be33821fc7338b1da97e91d9cc6.zip
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>
Diffstat (limited to 'vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go')
-rw-r--r--vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go b/vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go
new file mode 100644
index 00000000..c4f5c5d9
--- /dev/null
+++ b/vendor/github.com/Benau/tgsconverter/libtgsconverter/gif.go
@@ -0,0 +1,81 @@
+package libtgsconverter
+
+import "bytes"
+
+import "image"
+import "image/color"
+import "image/gif"
+
+type togif struct {
+ gif gif.GIF
+ images []image.Image
+ prev_frame *image.RGBA
+}
+
+func(to_gif *togif) init(w uint, h uint, options ConverterOptions) {
+ to_gif.gif.Config.Width = int(w)
+ to_gif.gif.Config.Height = int(h)
+}
+
+func(to_gif *togif) SupportsAnimation() bool {
+ return true
+}
+
+func (to_gif *togif) AddFrame(image *image.RGBA, fps uint) error {
+ var fps_int = int(1.0 / float32(fps) * 100.)
+ if to_gif.prev_frame != nil && sameImage(to_gif.prev_frame, image) {
+ to_gif.gif.Delay[len(to_gif.gif.Delay) - 1] += fps_int
+ return nil
+ }
+ to_gif.gif.Image = append(to_gif.gif.Image, nil)
+ to_gif.gif.Delay = append(to_gif.gif.Delay, fps_int)
+ to_gif.gif.Disposal = append(to_gif.gif.Disposal, gif.DisposalBackground)
+ to_gif.images = append(to_gif.images, image)
+ to_gif.prev_frame = image
+ return nil
+}
+
+func (to_gif *togif) Result() []byte {
+ q := medianCutQuantizer{mode, nil, false}
+ p := q.quantizeMultiple(make([]color.Color, 0, 256), to_gif.images)
+ // Add transparent entry finally
+ var trans_idx uint8 = 0
+ if q.reserveTransparent {
+ trans_idx = uint8(len(p))
+ }
+ var id_map = make(map[uint32]uint8)
+ for i, img := range to_gif.images {
+ pi := image.NewPaletted(img.Bounds(), p)
+ for y := 0; y < img.Bounds().Dy(); y++ {
+ for x := 0; x < img.Bounds().Dx(); x++ {
+ c := img.At(x, y)
+ cr, cg, cb, ca := c.RGBA()
+ cid := (cr >> 8) << 16 | cg | (cb >> 8)
+ if q.reserveTransparent && ca == 0 {
+ pi.Pix[pi.PixOffset(x, y)] = trans_idx
+ } else if val, ok := id_map[cid]; ok {
+ pi.Pix[pi.PixOffset(x, y)] = val
+ } else {
+ val := uint8(p.Index(c))
+ pi.Pix[pi.PixOffset(x, y)] = val
+ id_map[cid] = val
+ }
+ }
+ }
+ to_gif.gif.Image[i] = pi
+ }
+ if q.reserveTransparent {
+ p = append(p, color.RGBA{0, 0, 0, 0})
+ }
+ for _, img := range to_gif.gif.Image {
+ img.Palette = p
+ }
+ to_gif.gif.Config.ColorModel = p
+ var data []byte
+ w := bytes.NewBuffer(data)
+ err := gif.EncodeAll(w, &to_gif.gif)
+ if err != nil {
+ return nil
+ }
+ return w.Bytes()
+}