summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Benau/tgsconverter/libtgsconverter/apng.go
blob: 78541533729fdd15cc6d801f811e9550930e315d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()
}