Clip tag optimizations

This commit is contained in:
DataHoarder 2024-03-08 18:19:48 +01:00
parent 4478b7310a
commit 232062ce19
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 31 additions and 25 deletions

View file

@ -2,12 +2,12 @@ package tag
import (
"fmt"
swftypes "git.gammaspectra.live/WeebDataHoarder/swf-go/types"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/ass/time"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/settings"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/math"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/records"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/shapes"
"github.com/ctessum/polyclip-go"
)
type ClipTag struct {
@ -16,27 +16,18 @@ type ClipTag struct {
IsNull bool
}
func NewClipTag(clip *shapes.ClipPath, scale int) *ClipTag {
if clip == nil {
func NewClipTag(clip types.Option[shapes.Shape], scale int) *ClipTag {
if c, ok := clip.Some(); ok && len(c) > 0 {
return &ClipTag{
Scale: scale,
BaseDrawingTag: BaseDrawingTag(c),
IsNull: len(c) == 0,
}
} else {
return &ClipTag{
IsNull: true,
Scale: scale,
}
} else {
shape := clip.GetShape()
if len(shape) == 0 { //full clip
shape = shapes.Shape{
records.LineRecord{
//TODO: ??? why TwipFactor here???
To: math.NewVector2[float64](0, swftypes.Twip(swftypes.TwipFactor).Float64()),
Start: math.NewVector2[float64](0, 0),
},
}
}
return &ClipTag{
Scale: scale,
BaseDrawingTag: BaseDrawingTag(shape),
}
}
}
@ -73,6 +64,18 @@ func (t *ClipTag) Encode(event time.EventTime) string {
if t.IsNull {
return ""
}
shape := t.AsShape()
bb := shape.BoundingBox()
//uses pixel coords
if bb.TopLeft.Int64().Float64().Equals(bb.TopLeft) && bb.BottomRight.Int64().Float64().Equals(bb.BottomRight) {
diffPol := shapes.NewPolygonFromShape(bb.Draw()).Construct(polyclip.DIFFERENCE, shapes.NewPolygonFromShape(shape))
if len(diffPol) == 0 { //it's the same!
//we can use square clip!
return fmt.Sprintf("\\clip(%d,%d,%d,%d)", bb.TopLeft.Int64().X, bb.TopLeft.Int64().Y, bb.BottomRight.Int64().X, bb.BottomRight.Int64().Y)
}
}
scaleMultiplier := 1 << (t.Scale - 1)
precision := settings.GlobalSettings.ASSDrawingPrecision
if t.Scale >= 5 {

View file

@ -287,8 +287,8 @@ func ContainerTagFromPathEntry(path shapes.DrawPath, clip types.Option[shapes.Cl
}
}
if clip, ok := clip.Some(); ok {
if settings.GlobalSettings.ASSBakeClips {
if settings.GlobalSettings.ASSBakeClips {
clip.With(func(clip shapes.ClipPath) {
//Clip is given in absolute coordinates. path is relative to translation
//TODO: is this true for ClipPath???
//TODO: this is broken
@ -297,11 +297,13 @@ func ContainerTagFromPathEntry(path shapes.DrawPath, clip types.Option[shapes.Cl
Style: path.Style, //TODO: apply transform to Style?
Shape: clip.ApplyMatrixTransform(translationTransform, true).ClipShape(path.Shape, true),
}
} else {
container.TryAppend(NewClipTag(&clip, settings.GlobalSettings.ASSDrawingScale))
}
})
} else {
container.TryAppend(NewClipTag(nil, settings.GlobalSettings.ASSDrawingScale))
if clip, ok := clip.Some(); ok {
container.TryAppend(NewClipTag(types.Some(clip.GetShape()), settings.GlobalSettings.ASSDrawingScale))
} else {
container.TryAppend(NewClipTag(types.None[shapes.Shape](), settings.GlobalSettings.ASSDrawingScale))
}
}
/*

View file

@ -38,6 +38,7 @@ type Settings struct {
// FlushInterval Flush a shape after N frames of no transitions. Basically a buffer. Set to 0 to disable.
// This more or less controls how far frames are delayed before being flushed, sometimes these can be reused later on some loops.
// TODO: this has issues with layer re-stacking
FlushInterval int
FlushCountLimit int