diff --git a/common/nori/export.go b/common/nori/export.go index f1eca4a..f874973 100644 --- a/common/nori/export.go +++ b/common/nori/export.go @@ -26,17 +26,17 @@ func (n *Nori) ShowAnimation() *apng.APNG { return a } -func (n *Nori) ExportAnimation() (*apng.APNG, error) { +func (n *Nori) ExportAnimation(num int) (*apng.APNG, error) { g := n.Gawi a := &apng.APNG{ Frames: make([]apng.Frame, 0, len(g.Images)), } - images := make([]*image.NRGBA64, 0, len(n.Animations)) - for i := 0; i < len(n.Animations); i++ { - if len(n.Animations[i].Frames) < 1 { - continue - } - planes := n.Animations[i].Frames[0].Planes + 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 _, plane := range planes { if int(plane.BitmapId) >= len(n.Gawi.Images) { @@ -85,16 +85,15 @@ func (n *Nori) ExportAnimation() (*apng.APNG, error) { draw.Over, ) } - if len(planes) > 0 { - images = append(images, img) - - } + images = append(images, img) } or := images[0].Bounds() for _, realFrame := range images { 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 { big := image.NewNRGBA64(or) draw.Draw( diff --git a/common/nori/nori_test.go b/common/nori/nori_test.go index d48ed38..fea4e17 100644 --- a/common/nori/nori_test.go +++ b/common/nori/nori_test.go @@ -2,7 +2,9 @@ package nori import ( "bytes" + "fmt" "os" + "strings" "testing" "gitlab.com/gfxlabs/gfximg/apng" @@ -17,13 +19,12 @@ func TestParseFile1(t *testing.T) { if err != nil { t.Errorf("decode: %s", err) } - t.Logf("\n nori: %+v", nori) - t.Logf("\n gawi: %+v", nori.Gawi) - a, err := nori.ExportAnimation() + t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi) + + err = writeApng(nori, "test1") if err != nil { t.Errorf("export: %s", err) } - writeApng(a, "./nori_test/test1.apng") } func TestParseFile2(t *testing.T) { @@ -36,12 +37,10 @@ func TestParseFile2(t *testing.T) { t.Errorf("decode: %s", err) } t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi) - a, err := nori.ExportAnimation() + err = writeApng(nori, "test2") if err != nil { t.Errorf("export: %s", err) } - - writeApng(a, "./nori_test/test2.apng") } func TestParsePalette(t *testing.T) { @@ -54,25 +53,31 @@ func TestParsePalette(t *testing.T) { t.Errorf("decode: %s", err) } t.Logf("\n nori: %+v\n gawi: %+v\n palette: %+v\n", nori, nori.Gawi, nori.Gawi.Palette) - a, err := nori.ExportAnimation() - if err != nil { - t.Errorf("export: %s", err) - } - err = writeApng(a, "./nori_test/palette.apng") + err = writeApng(nori, "palette") if err != nil { t.Errorf("export: %s", err) } } -func writeApng(a *apng.APNG, fp string) error { +func writeApng(nori *Nori, name string) error { out := new(bytes.Buffer) - err := apng.Encode(out, *a) - if err != nil { - return err - } - err = os.WriteFile(fp, out.Bytes(), 0740) - if err != nil { - return err + 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 { + return err + } + err = os.WriteFile(fmt.Sprintf("./nori_test/%s.out/animation_%d.png", name, i), out.Bytes(), 0740) + if err != nil { + return err + } } return nil } diff --git a/common/nori/nori_test/palette.apng b/common/nori/nori_test/palette.apng deleted file mode 100755 index bbd7a8a..0000000 Binary files a/common/nori/nori_test/palette.apng and /dev/null differ diff --git a/common/nori/nori_test/test1.apng b/common/nori/nori_test/test1.apng deleted file mode 100755 index 0ee8cdb..0000000 Binary files a/common/nori/nori_test/test1.apng and /dev/null differ diff --git a/common/nori/nori_test/test2.apng b/common/nori/nori_test/test2.apng deleted file mode 100755 index 99bf08d..0000000 Binary files a/common/nori/nori_test/test2.apng and /dev/null differ