2022-03-25 06:00:01 +00:00
|
|
|
package nori
|
|
|
|
|
2022-03-25 07:33:23 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/draw"
|
2022-03-25 22:19:32 +00:00
|
|
|
"strings"
|
2022-03-25 06:00:01 +00:00
|
|
|
|
2022-03-25 07:33:23 +00:00
|
|
|
"git.tuxpa.in/a/zlog/log"
|
2022-03-25 10:23:31 +00:00
|
|
|
"github.com/phrozen/blend"
|
2022-03-25 07:33:23 +00:00
|
|
|
"gitlab.com/gfxlabs/gfximg/apng"
|
|
|
|
)
|
|
|
|
|
2022-03-25 22:19:32 +00:00
|
|
|
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) {
|
2022-03-25 07:33:23 +00:00
|
|
|
g := n.Gawi
|
|
|
|
a := &apng.APNG{
|
|
|
|
Frames: make([]apng.Frame, 0, len(g.Images)),
|
|
|
|
}
|
2022-03-25 18:32:40 +00:00
|
|
|
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
|
2022-03-25 10:23:31 +00:00
|
|
|
var canvasRect image.Rectangle
|
2022-03-25 19:24:52 +00:00
|
|
|
for i, plane := range planes {
|
2022-03-25 10:23:31 +00:00
|
|
|
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
|
2022-03-25 19:24:52 +00:00
|
|
|
rc := bitmap.Img.Bounds().Add(pt) // translate rectangle to put in the global canvas
|
|
|
|
if i == 0 {
|
|
|
|
canvasRect = rc
|
|
|
|
} else {
|
|
|
|
canvasRect = canvasRect.Union(rc)
|
|
|
|
}
|
2022-03-25 10:23:31 +00:00
|
|
|
}
|
|
|
|
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 {
|
2022-03-25 07:33:23 +00:00
|
|
|
}
|
2022-03-25 10:23:31 +00:00
|
|
|
// is transparent
|
|
|
|
if plane.RenderFlag&0x20 != 0 {
|
|
|
|
transparent = true
|
2022-03-25 07:33:23 +00:00
|
|
|
}
|
2022-03-25 10:23:31 +00:00
|
|
|
_ = transparent
|
|
|
|
pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point
|
2022-03-25 19:24:52 +00:00
|
|
|
rc := bitmap.Img.Bounds().Add(pt)
|
2022-03-25 10:23:31 +00:00
|
|
|
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)
|
2022-03-25 07:33:23 +00:00
|
|
|
}
|
2022-03-25 19:24:52 +00:00
|
|
|
draw.Draw(
|
|
|
|
img,
|
|
|
|
rc,
|
2022-03-25 10:23:31 +00:00
|
|
|
src,
|
2022-03-25 19:24:52 +00:00
|
|
|
src.Bounds().Min,
|
2022-03-25 19:38:46 +00:00
|
|
|
draw.Over,
|
2022-03-25 10:23:31 +00:00
|
|
|
)
|
2022-03-25 07:33:23 +00:00
|
|
|
}
|
2022-03-25 18:32:40 +00:00
|
|
|
images = append(images, img)
|
2022-03-25 10:23:31 +00:00
|
|
|
}
|
|
|
|
or := images[0].Bounds()
|
|
|
|
for _, realFrame := range images {
|
|
|
|
or = realFrame.Bounds().Union(or)
|
|
|
|
}
|
2022-03-25 18:32:40 +00:00
|
|
|
if or.Size().X == 0 || or.Size().Y == 0 {
|
|
|
|
return nil, fmt.Errorf("no frames found for animation")
|
|
|
|
}
|
2022-03-25 10:23:31 +00:00
|
|
|
for i, realFrame := range images {
|
2022-03-25 19:38:46 +00:00
|
|
|
//log.Info().Int("frame", num).Interface("rect", or).Interface("frame", realFrame.Bounds()).Msg("")
|
2022-03-25 19:24:52 +00:00
|
|
|
src := realFrame
|
2022-03-25 10:23:31 +00:00
|
|
|
big := image.NewNRGBA64(or)
|
|
|
|
draw.Draw(
|
|
|
|
big,
|
2022-03-25 19:24:52 +00:00
|
|
|
src.Bounds(),
|
|
|
|
src,
|
|
|
|
src.Bounds().Min,
|
2022-03-25 10:23:31 +00:00
|
|
|
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)
|
2022-03-25 07:33:23 +00:00
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|