123 lines
3.1 KiB
Go
123 lines
3.1 KiB
Go
package nori
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/draw"
|
|
"strings"
|
|
|
|
"git.tuxpa.in/a/zlog/log"
|
|
"github.com/phrozen/blend"
|
|
"gitlab.com/gfxlabs/gfximg/apng"
|
|
)
|
|
|
|
func RenderAnimations(n *Nori) ([]*apng.APNG, error) {
|
|
apngs := make([]*apng.APNG, 0, len(n.Animations))
|
|
for i := range n.Animations {
|
|
a, err := RenderAnimation(n, i)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no frame") {
|
|
continue
|
|
}
|
|
return nil, err
|
|
}
|
|
apngs = append(apngs, a)
|
|
}
|
|
return apngs, nil
|
|
}
|
|
|
|
func RenderAnimation(n *Nori, num int) (*apng.APNG, error) {
|
|
g := n.Gawi
|
|
a := &apng.APNG{
|
|
Frames: make([]apng.Frame, 0, len(g.Images)),
|
|
}
|
|
if len(n.Animations[num].Frames) == 0 {
|
|
return nil, fmt.Errorf("no frames found for animation")
|
|
}
|
|
images := make([]*image.NRGBA64, 0, len(n.Animations[num].Frames))
|
|
for _, frame := range n.Animations[num].Frames {
|
|
planes := frame.Planes
|
|
var canvasRect image.Rectangle
|
|
for i, plane := range planes {
|
|
if int(plane.BitmapId) >= len(n.Gawi.Images) {
|
|
log.Printf("could not find bitmap %d, only have %d", plane.BitmapId, len(n.Gawi.Images))
|
|
}
|
|
bitmap := n.Gawi.Images[plane.BitmapId]
|
|
pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point
|
|
rc := bitmap.Img.Bounds().Add(pt) // translate rectangle to put in the global canvas
|
|
if i == 0 {
|
|
canvasRect = rc
|
|
} else {
|
|
canvasRect = canvasRect.Union(rc)
|
|
}
|
|
}
|
|
img := image.NewNRGBA64(canvasRect)
|
|
for _, plane := range planes {
|
|
bitmap := n.Gawi.Images[plane.BitmapId]
|
|
transparent := false
|
|
//flipx
|
|
if plane.RenderFlag&1 != 0 {
|
|
}
|
|
//flipy
|
|
if plane.RenderFlag&2 != 0 {
|
|
}
|
|
// is transparent
|
|
if plane.RenderFlag&0x20 != 0 {
|
|
transparent = true
|
|
}
|
|
_ = transparent
|
|
pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point
|
|
rc := bitmap.Img.Bounds().Add(pt)
|
|
src := bitmap.Img
|
|
switch plane.Blend {
|
|
case BlendMode_Alpha:
|
|
case BlendMode_Mul, BlendMode_Mul7:
|
|
blend.BlendImage(img, bitmap.Img, blend.Multiply)
|
|
case BlendMode_Add, BlendMode_Add8:
|
|
blend.BlendImage(img, bitmap.Img, blend.Add)
|
|
case BlendMode_InvertMul, BlendMode_InvertMul5:
|
|
blend.BlendImage(img, bitmap.Img, blend.Multiply)
|
|
case BlendMode_None:
|
|
default:
|
|
return nil, fmt.Errorf("unknown blend mode: %d", plane.Blend)
|
|
}
|
|
draw.Draw(
|
|
img,
|
|
rc,
|
|
src,
|
|
src.Bounds().Min,
|
|
draw.Over,
|
|
)
|
|
}
|
|
images = append(images, img)
|
|
}
|
|
or := images[0].Bounds()
|
|
for _, realFrame := range images {
|
|
or = realFrame.Bounds().Union(or)
|
|
}
|
|
if or.Size().X == 0 || or.Size().Y == 0 {
|
|
return nil, fmt.Errorf("no frames found for animation")
|
|
}
|
|
for i, realFrame := range images {
|
|
//log.Info().Int("frame", num).Interface("rect", or).Interface("frame", realFrame.Bounds()).Msg("")
|
|
src := realFrame
|
|
big := image.NewNRGBA64(or)
|
|
draw.Draw(
|
|
big,
|
|
src.Bounds(),
|
|
src,
|
|
src.Bounds().Min,
|
|
draw.Src,
|
|
)
|
|
fr := apng.Frame{
|
|
Image: big,
|
|
DelayDenominator: 1000,
|
|
}
|
|
if g.Images[i].Delay != 0 {
|
|
fr.DelayNumerator = uint16(g.Images[i].Delay)
|
|
}
|
|
a.Frames = append(a.Frames, fr)
|
|
}
|
|
return a, nil
|
|
}
|