animations

This commit is contained in:
elee 2022-03-25 13:32:40 -05:00
parent 1bcae21244
commit 27aba01292
5 changed files with 36 additions and 32 deletions

View File

@ -26,17 +26,17 @@ func (n *Nori) ShowAnimation() *apng.APNG {
return a return a
} }
func (n *Nori) ExportAnimation() (*apng.APNG, error) { func (n *Nori) ExportAnimation(num int) (*apng.APNG, error) {
g := n.Gawi g := n.Gawi
a := &apng.APNG{ a := &apng.APNG{
Frames: make([]apng.Frame, 0, len(g.Images)), Frames: make([]apng.Frame, 0, len(g.Images)),
} }
images := make([]*image.NRGBA64, 0, len(n.Animations)) if len(n.Animations[num].Frames) == 0 {
for i := 0; i < len(n.Animations); i++ { return nil, fmt.Errorf("no frames found for animation")
if len(n.Animations[i].Frames) < 1 {
continue
} }
planes := n.Animations[i].Frames[0].Planes images := make([]*image.NRGBA64, 0, len(n.Animations[num].Frames))
for _, frame := range n.Animations[num].Frames {
planes := frame.Planes
var canvasRect image.Rectangle var canvasRect image.Rectangle
for _, plane := range planes { for _, plane := range planes {
if int(plane.BitmapId) >= len(n.Gawi.Images) { if int(plane.BitmapId) >= len(n.Gawi.Images) {
@ -85,16 +85,15 @@ func (n *Nori) ExportAnimation() (*apng.APNG, error) {
draw.Over, draw.Over,
) )
} }
if len(planes) > 0 {
images = append(images, img) images = append(images, img)
}
} }
or := images[0].Bounds() or := images[0].Bounds()
for _, realFrame := range images { for _, realFrame := range images {
or = realFrame.Bounds().Union(or) or = realFrame.Bounds().Union(or)
} }
// TODO: this is bugged if or.Size().X == 0 || or.Size().Y == 0 {
return nil, fmt.Errorf("no frames found for animation")
}
for i, realFrame := range images { for i, realFrame := range images {
big := image.NewNRGBA64(or) big := image.NewNRGBA64(or)
draw.Draw( draw.Draw(

View File

@ -2,7 +2,9 @@ package nori
import ( import (
"bytes" "bytes"
"fmt"
"os" "os"
"strings"
"testing" "testing"
"gitlab.com/gfxlabs/gfximg/apng" "gitlab.com/gfxlabs/gfximg/apng"
@ -17,13 +19,12 @@ func TestParseFile1(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
t.Logf("\n nori: %+v", nori) t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi)
t.Logf("\n gawi: %+v", nori.Gawi)
a, err := nori.ExportAnimation() err = writeApng(nori, "test1")
if err != nil { if err != nil {
t.Errorf("export: %s", err) t.Errorf("export: %s", err)
} }
writeApng(a, "./nori_test/test1.apng")
} }
func TestParseFile2(t *testing.T) { func TestParseFile2(t *testing.T) {
@ -36,12 +37,10 @@ func TestParseFile2(t *testing.T) {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi) t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi)
a, err := nori.ExportAnimation() err = writeApng(nori, "test2")
if err != nil { if err != nil {
t.Errorf("export: %s", err) t.Errorf("export: %s", err)
} }
writeApng(a, "./nori_test/test2.apng")
} }
func TestParsePalette(t *testing.T) { func TestParsePalette(t *testing.T) {
@ -54,25 +53,31 @@ func TestParsePalette(t *testing.T) {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
t.Logf("\n nori: %+v\n gawi: %+v\n palette: %+v\n", nori, nori.Gawi, nori.Gawi.Palette) t.Logf("\n nori: %+v\n gawi: %+v\n palette: %+v\n", nori, nori.Gawi, nori.Gawi.Palette)
a, err := nori.ExportAnimation() err = writeApng(nori, "palette")
if err != nil {
t.Errorf("export: %s", err)
}
err = writeApng(a, "./nori_test/palette.apng")
if err != nil { if err != nil {
t.Errorf("export: %s", err) t.Errorf("export: %s", err)
} }
} }
func writeApng(a *apng.APNG, fp string) error { func writeApng(nori *Nori, name string) error {
out := new(bytes.Buffer) out := new(bytes.Buffer)
err := apng.Encode(out, *a) os.MkdirAll("./nori_test/"+name+".out", 0740)
for i := range nori.Animations {
a, err := nori.ExportAnimation(i)
if err != nil {
if strings.Contains(err.Error(), "no frame") {
continue
}
return err
}
err = apng.Encode(out, *a)
if err != nil { if err != nil {
return err return err
} }
err = os.WriteFile(fp, out.Bytes(), 0740) err = os.WriteFile(fmt.Sprintf("./nori_test/%s.out/animation_%d.png", name, i), out.Bytes(), 0740)
if err != nil { if err != nil {
return err return err
} }
}
return nil return nil
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 580 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB