あああああああああああああああああああああああああああ

This commit is contained in:
Raymonf 2022-03-26 02:52:59 -04:00
parent ec465535d7
commit 1375ab7a1a
15 changed files with 268 additions and 80 deletions

View File

@ -5,6 +5,9 @@ img_tag = local
cory: clean cory: clean
go build -o ./cory ./_cmd/cory-bin go build -o ./cory ./_cmd/cory-bin
cory_win: clean
go build -o ./cory.exe ./_cmd/cory-bin
docker: docker:
DOCKER_BUILDKIT=1 docker build -f server.Dockerfile -t $(img_user)/$(img_name):$(img_tag) . DOCKER_BUILDKIT=1 docker build -f server.Dockerfile -t $(img_user)/$(img_name):$(img_tag) .
@ -12,4 +15,4 @@ server-run: docker
docker run -p 3333:3333 $(img_user)/$(img_name):$(img_tag) --serve docker run -p 3333:3333 $(img_user)/$(img_name):$(img_tag) --serve
clean: clean:
rm -rf cory rm -rf cory cory.exe

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"git.tuxpa.in/a/nori/renderer"
"net/http" "net/http"
"os" "os"
"time" "time"
@ -57,7 +58,7 @@ func dump(filename string, output string) {
} }
out := new(bytes.Buffer) out := new(bytes.Buffer)
printIf("rendering %d animation(s)\n", n.AnimationCount) printIf("rendering %d animation(s)\n", n.AnimationCount)
animations, err := nori.RenderAnimations(n) animations, err := renderer.RenderAnimationsApng(n)
if err != nil { if err != nil {
log.Panicln("animation: %s", err) log.Panicln("animation: %s", err)
} }
@ -93,7 +94,7 @@ func serve() {
if err := n.Decode(r.Body); err != nil { if err := n.Decode(r.Body); err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
} }
frames, err := nori.RenderAnimations(n) frames, err := renderer.RenderAnimationsApng(n)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
} }

View File

@ -81,23 +81,25 @@ func (f *Frame) Decode(rd io.Reader) error {
if err := discardN(rd, 18); err != nil { if err := discardN(rd, 18); err != nil {
return err return err
} }
var cast uint32 if f.Version >= 303 {
if err := binary.Read(rd, end, &cast); err != nil { var cast uint32
return err if err := binary.Read(rd, end, &cast); err != nil {
}
// camp
f.HasCamp = (f.Version >= 303 && (cast > 0))
if f.HasCamp {
if err := binary.Read(rd, end, f.Camp.Params[:]); err != nil {
return err return err
} }
sz := int(f.Camp.Params[1]) * int(f.Camp.Params[2]) // camp
f.Camp.Array = make([]byte, sz) f.HasCamp = cast > 0
if _, err := io.ReadFull(rd, f.Camp.Array); err != nil { if f.HasCamp {
return err if err := binary.Read(rd, end, f.Camp.Params[:]); err != nil {
} return err
if err := discardN(rd, 20); err != nil { }
return err sz := int(f.Camp.Params[1]) * int(f.Camp.Params[2])
f.Camp.Array = make([]byte, sz)
if _, err := io.ReadFull(rd, f.Camp.Array); err != nil {
return err
}
if err := discardN(rd, 20); err != nil {
return err
}
} }
} }

6
go.mod
View File

@ -4,14 +4,16 @@ go 1.18
require ( require (
git.tuxpa.in/a/zlog v1.32.0 git.tuxpa.in/a/zlog v1.32.0
github.com/go-chi/chi/v5 v5.0.7 github.com/disintegration/imaging v1.6.2
github.com/phrozen/blend v0.0.0-20210220204729-f26b6cf7a28e github.com/phrozen/blend v0.0.0-20210220204729-f26b6cf7a28e
gitlab.com/gfxlabs/gfximg v0.0.5 gitlab.com/gfxlabs/gfximg v0.0.5
) )
require ( require (
github.com/chai2010/webp v1.1.1 // indirect
github.com/go-chi/chi/v5 v5.0.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
gitlab.com/gfxlabs/goutil v1.5.0 // indirect golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
) )

13
go.sum
View File

@ -1,6 +1,10 @@
git.tuxpa.in/a/zlog v1.32.0 h1:KKXbRF1x8kJDSzUoGz/pivo+4TVY6xT5sVtdFZ6traY= git.tuxpa.in/a/zlog v1.32.0 h1:KKXbRF1x8kJDSzUoGz/pivo+4TVY6xT5sVtdFZ6traY=
git.tuxpa.in/a/zlog v1.32.0/go.mod h1:vUa2Qhu6DLPLqmfRy99FiPqaY2eb6/KQjtMekW3UNnA= git.tuxpa.in/a/zlog v1.32.0/go.mod h1:vUa2Qhu6DLPLqmfRy99FiPqaY2eb6/KQjtMekW3UNnA=
github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@ -14,8 +18,13 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
gitlab.com/gfxlabs/gfximg v0.0.5 h1:jtHE6In6axz0zGPy7YnLIZV9RFdcFEZzpdqoXb52K9s= gitlab.com/gfxlabs/gfximg v0.0.5 h1:jtHE6In6axz0zGPy7YnLIZV9RFdcFEZzpdqoXb52K9s=
gitlab.com/gfxlabs/gfximg v0.0.5/go.mod h1:IAYZwCoqy3JFKwkKafragpfecp5Up6FFIzI7VvK2Zzo= gitlab.com/gfxlabs/gfximg v0.0.5/go.mod h1:IAYZwCoqy3JFKwkKafragpfecp5Up6FFIzI7VvK2Zzo=
gitlab.com/gfxlabs/goutil v1.5.0 h1:tYjMbDRtK93MPrWrZE4Ub67Ng8rYwTahtb+Bfpf0Spk= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
gitlab.com/gfxlabs/goutil v1.5.0/go.mod h1:NGvdG6oTLV2cEX4HKtewdzx/ZIvC+eAS7EbjKy4sXsA= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@ -103,17 +103,19 @@ func (i *Image) Decode(rd io.Reader, palette *PaletteSection) error {
return err return err
} }
alpha := uint8(0xff) alpha := uint8(0xff)
if rgb[2] == 0x00 && rgb[1] == 0xff && rgb[0] == 0x00 {
alpha = 0x00
}
if rgb[2] == 0xff && rgb[1] == 0x00 && rgb[0] == 0xff {
alpha = 0x00
}
col := &color.RGBA{ col := &color.RGBA{
rgb[0], rgb[1], rgb[2], rgb[2], rgb[1], rgb[0],
alpha, alpha,
} }
if rgb[0] == 0x00 && rgb[1] == 0xff && rgb[2] == 0x00 {
alpha = 0x00
}
if rgb[0] == 0xff && rgb[1] == 0x00 && rgb[2] == 0xff {
alpha = 0x00
}
cf.Set(idx%int(i.Width), idx/int(i.Width), col) cf.Set(idx%int(i.Width), idx/int(i.Width), col)
} }
return nil return nil

View File

@ -18,7 +18,7 @@ func main() {
log.Panicln("decode: %s", err) log.Panicln("decode: %s", err)
} }
// uses apng encoder/decoder from https://gitlab.com/gfxlabs/gfximg // uses apng encoder/decoder from https://gitlab.com/gfxlabs/gfximg
animations, err := n.RenderAnimations(n) animations, err := n.RenderAnimationsApng(n)
if err != nil { if err != nil {
log.Panicln("render: %s", err) log.Panicln("render: %s", err)
} }

View File

@ -2,47 +2,38 @@ package nori
import ( import (
"fmt" "fmt"
"image"
"image/draw"
"strings"
"git.tuxpa.in/a/zlog/log" "git.tuxpa.in/a/zlog/log"
"github.com/disintegration/imaging"
"github.com/phrozen/blend" "github.com/phrozen/blend"
"gitlab.com/gfxlabs/gfximg/apng" "gitlab.com/gfxlabs/gfximg/apng"
"image"
"image/color"
"image/draw"
) )
func RenderAnimations(n *Nori) ([]*apng.APNG, error) { func copyImage(img image.Image) image.Image {
apngs := make([]*apng.APNG, 0, len(n.Animations)) copied := image.NewNRGBA(img.Bounds())
for i := range n.Animations { draw.Draw(copied, img.Bounds(), img, img.Bounds().Min, draw.Over)
a, err := RenderAnimation(n, i) return copied
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) { type RenderFunc = func(img image.Image, delay int)
func RenderAnimation(n *Nori, num int, f RenderFunc) error {
g := n.Gawi g := n.Gawi
a := &apng.APNG{
Frames: make([]apng.Frame, 0, len(g.Images)),
}
if len(n.Animations[num].Frames) == 0 { if len(n.Animations[num].Frames) == 0 {
return nil, fmt.Errorf("no frames found for animation") return fmt.Errorf("no frames found for animation")
} }
images := make([]*image.NRGBA64, 0, len(n.Animations[num].Frames)) images := make([]image.Image, 0, len(n.Animations[num].Frames))
durations := make([]uint32, 0, len(n.Animations[num].Frames))
for _, frame := range n.Animations[num].Frames { for _, frame := range n.Animations[num].Frames {
planes := frame.Planes planes := frame.Planes
var canvasRect image.Rectangle var canvasRect image.Rectangle
for i, plane := range planes { for i, plane := range planes {
if int(plane.BitmapId) >= len(n.Gawi.Images) { if int(plane.BitmapId) >= len(g.Images) {
log.Printf("could not find bitmap %d, only have %d", plane.BitmapId, len(n.Gawi.Images)) log.Printf("could not find bitmap %d, only have %d", plane.BitmapId, len(g.Images))
} }
bitmap := n.Gawi.Images[plane.BitmapId] bitmap := g.Images[plane.BitmapId]
pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point 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 rc := bitmap.Img.Bounds().Add(pt) // translate rectangle to put in the global canvas
if i == 0 { if i == 0 {
@ -51,15 +42,24 @@ func RenderAnimation(n *Nori, num int) (*apng.APNG, error) {
canvasRect = canvasRect.Union(rc) canvasRect = canvasRect.Union(rc)
} }
} }
img := image.NewNRGBA64(canvasRect) img := image.NewNRGBA(canvasRect)
for _, plane := range planes { for _, plane := range planes {
bitmap := n.Gawi.Images[plane.BitmapId] bitmap := g.Images[plane.BitmapId]
drawBitmap := bitmap.Img
transparent := false transparent := false
//flipx //flipx
if plane.RenderFlag&1 != 0 { if plane.RenderFlag&1 != 0 {
if bitmap.Img == drawBitmap {
drawBitmap = copyImage(bitmap.Img)
}
drawBitmap = imaging.FlipH(drawBitmap)
} }
//flipy //flipy
if plane.RenderFlag&2 != 0 { if plane.RenderFlag&2 != 0 {
if bitmap.Img == drawBitmap {
drawBitmap = copyImage(bitmap.Img)
}
drawBitmap = imaging.FlipV(drawBitmap)
} }
// is transparent // is transparent
if plane.RenderFlag&0x20 != 0 { if plane.RenderFlag&0x20 != 0 {
@ -67,36 +67,46 @@ func RenderAnimation(n *Nori, num int) (*apng.APNG, error) {
} }
_ = transparent _ = transparent
pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point pt := image.Pt(int(plane.PlaneX), int(plane.PlaneY)) // where to put the point
rc := bitmap.Img.Bounds().Add(pt) rc := drawBitmap.Bounds().Add(pt)
src := bitmap.Img src := drawBitmap
switch plane.Blend { switch plane.Blend {
case BlendMode_InvertMul, BlendMode_InvertMul5:
img = imaging.Invert(img)
fallthrough
case BlendMode_Alpha: case BlendMode_Alpha:
case BlendMode_Mul, BlendMode_Mul7: case BlendMode_Mul, BlendMode_Mul7:
blend.BlendImage(img, bitmap.Img, blend.Multiply) blend.BlendImage(img, drawBitmap, blend.Multiply)
case BlendMode_Add, BlendMode_Add8: case BlendMode_Add, BlendMode_Add8:
blend.BlendImage(img, bitmap.Img, blend.Add) blend.BlendImage(img, drawBitmap, blend.Add)
case BlendMode_InvertMul, BlendMode_InvertMul5:
blend.BlendImage(img, bitmap.Img, blend.Multiply)
case BlendMode_None: case BlendMode_None:
default: default:
return nil, fmt.Errorf("unknown blend mode: %d", plane.Blend) return fmt.Errorf("unknown blend mode: %d", plane.Blend)
} }
draw.Draw(
mask := (image.Image)(nil)
if transparent {
mask = &image.Uniform{C: color.RGBA{A: 160}}
}
draw.DrawMask(
img, img,
rc, rc,
src, src,
src.Bounds().Min, src.Bounds().Min,
mask,
src.Bounds().Min,
draw.Over, draw.Over,
) )
} }
images = append(images, img) images = append(images, img)
durations = append(durations, frame.Duration)
} }
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)
} }
if or.Size().X == 0 || or.Size().Y == 0 { if or.Size().X == 0 || or.Size().Y == 0 {
return nil, fmt.Errorf("no frames found for animation") return fmt.Errorf("no frames found for animation")
} }
for i, realFrame := range images { for i, realFrame := range images {
//log.Info().Int("frame", num).Interface("rect", or).Interface("frame", realFrame.Bounds()).Msg("") //log.Info().Int("frame", num).Interface("rect", or).Interface("frame", realFrame.Bounds()).Msg("")
@ -112,11 +122,10 @@ func RenderAnimation(n *Nori, num int) (*apng.APNG, error) {
fr := apng.Frame{ fr := apng.Frame{
Image: big, Image: big,
DelayDenominator: 1000, DelayDenominator: 1000,
DelayNumerator: uint16(durations[i]),
} }
if g.Images[i].Delay != 0 { f(big, int(fr.DelayNumerator))
fr.DelayNumerator = uint16(g.Images[i].Delay)
}
a.Frames = append(a.Frames, fr)
} }
return a, nil return nil
} }

View File

@ -1,8 +1,10 @@
package nori package nori_test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"git.tuxpa.in/a/nori"
"git.tuxpa.in/a/nori/renderer"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -11,7 +13,7 @@ import (
) )
func TestParseFile1(t *testing.T) { func TestParseFile1(t *testing.T) {
nori, err := FromFile("./render_test/test1.nri") nori, err := nori.FromFile("./render_test/test1.nri")
if err != nil { if err != nil {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
@ -24,7 +26,7 @@ func TestParseFile1(t *testing.T) {
} }
func TestParseFile2(t *testing.T) { func TestParseFile2(t *testing.T) {
nori, err := FromFile("./render_test/test2.nri") nori, err := nori.FromFile("./render_test/test2.nri")
if err != nil { if err != nil {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
@ -35,8 +37,34 @@ func TestParseFile2(t *testing.T) {
} }
} }
func TestParseFile3(t *testing.T) {
nori, err := nori.FromFile("./render_test/CharCreateNSelectUI.bac")
if err != nil {
t.Errorf("decode: %s", err)
}
t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi)
err = writeApng(nori, "charcreate")
if err != nil {
t.Errorf("export: %s", err)
}
}
func TestParseFile4(t *testing.T) {
nori, err := nori.FromFile("./render_test/LoadingUI.bac")
if err != nil {
t.Errorf("decode: %s", err)
}
t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi)
err = writeApng(nori, "LoadingUI")
if err != nil {
t.Errorf("export: %s", err)
}
}
func TestParsePalette(t *testing.T) { func TestParsePalette(t *testing.T) {
nori, err := FromFile("./render_test/palette.nri") nori, err := nori.FromFile("./render_test/palette.nri")
if err != nil { if err != nil {
t.Errorf("decode: %s", err) t.Errorf("decode: %s", err)
} }
@ -47,12 +75,12 @@ func TestParsePalette(t *testing.T) {
} }
} }
func writeApng(nori *Nori, name string) error { func writeApng(n *nori.Nori, name string) error {
postfix := ".example" postfix := ".example"
out := new(bytes.Buffer) out := new(bytes.Buffer)
os.MkdirAll(fmt.Sprintf("./render_test/%s%s", name, postfix), 0740) os.MkdirAll(fmt.Sprintf("./render_test/%s%s", name, postfix), 0740)
for i := range nori.Animations { for i := range n.Animations {
a, err := RenderAnimation(nori, i) a, err := renderer.RenderAnimationApng(n, i)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "no frame") { if strings.Contains(err.Error(), "no frame") {
continue continue

Binary file not shown.

BIN
render_test/LoadingUI.bac Normal file

Binary file not shown.

47
renderer/apng.go Normal file
View File

@ -0,0 +1,47 @@
package renderer
import (
"git.tuxpa.in/a/nori"
"gitlab.com/gfxlabs/gfximg/apng"
"image"
"strings"
)
func newRenderApngFunc(a *apng.APNG) nori.RenderFunc {
return func(img image.Image, delay int) {
fr := apng.Frame{
Image: img,
DelayDenominator: 1000,
}
if delay != 0 {
fr.DelayNumerator = uint16(delay)
}
a.Frames = append(a.Frames, fr)
}
}
func RenderAnimationsApng(n *nori.Nori) ([]*apng.APNG, error) {
apngs := make([]*apng.APNG, 0, len(n.Animations))
for i := range n.Animations {
a, err := RenderAnimationApng(n, i)
if err != nil {
if strings.Contains(err.Error(), "no frame") {
continue
}
return nil, err
}
apngs = append(apngs, a)
}
return apngs, nil
}
func RenderAnimationApng(n *nori.Nori, i int) (*apng.APNG, error) {
a := &apng.APNG{
Frames: make([]apng.Frame, 0, len(n.Gawi.Images)),
}
err := nori.RenderAnimation(n, i, newRenderApngFunc(a))
if err != nil {
return nil, err
}
return a, nil
}

32
renderer/gif.go Normal file
View File

@ -0,0 +1,32 @@
package renderer
import (
"git.tuxpa.in/a/nori"
"image"
"image/color/palette"
"image/draw"
"image/gif"
)
func newRenderGifFunc(gif *gif.GIF) nori.RenderFunc {
return func(img image.Image, delay int) {
i := image.NewPaletted(img.Bounds(), palette.Plan9)
draw.Draw(i, img.Bounds(), img, img.Bounds().Min, draw.Over)
gif.Image = append(gif.Image, i)
gif.Delay = append(gif.Delay, delay)
}
}
func RenderAnimationGif(n *nori.Nori, i int) (*gif.GIF, error) {
gif := &gif.GIF{
Image: make([]*image.Paletted, 0),
Delay: make([]int, 0),
LoopCount: 0,
}
err := nori.RenderAnimation(n, i, newRenderGifFunc(gif))
if err != nil {
return nil, err
}
return gif, nil
}

52
renderer/gif_test.go Normal file
View File

@ -0,0 +1,52 @@
package renderer_test
import (
"bytes"
"fmt"
"git.tuxpa.in/a/nori"
"git.tuxpa.in/a/nori/renderer"
"git.tuxpa.in/a/zlog/log"
"image/gif"
"os"
"strings"
"testing"
)
func TestMakeGif1(t *testing.T) {
nori, err := nori.FromFile("../render_test/CharCreateNSelectUI.bac")
if err != nil {
t.Errorf("decode: %s", err)
}
t.Logf("\n nori: %+v\n gawi: %+v", nori, nori.Gawi)
err = writeGif(nori, "charcreate")
if err != nil {
t.Errorf("export: %s", err)
}
}
func writeGif(n *nori.Nori, name string) error {
postfix := ".example"
out := new(bytes.Buffer)
os.MkdirAll(fmt.Sprintf("../render_test/%s%s", name, postfix), 0740)
for i := range n.Animations {
g, err := renderer.RenderAnimationGif(n, i)
if err != nil {
if strings.Contains(err.Error(), "no frame") {
continue
}
return err
}
err = gif.EncodeAll(out, g)
if err != nil {
log.Errorf("EncodeAll failed for frame %d, skipping: %s", i, err)
continue
}
err = os.WriteFile(fmt.Sprintf("../render_test/%s%s/animation_%d.gif", name, postfix, i), out.Bytes(), 0740)
if err != nil {
return err
}
out.Reset()
}
return nil
}

View File

@ -3,6 +3,7 @@ package utils
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"git.tuxpa.in/a/nori/renderer"
"os" "os"
"testing" "testing"
@ -48,7 +49,7 @@ func TestParsePalette(t *testing.T) {
func writeZip(n *nori.Nori, name string) error { func writeZip(n *nori.Nori, name string) error {
out := new(bytes.Buffer) out := new(bytes.Buffer)
frames, err := nori.RenderAnimations(n) frames, err := renderer.RenderAnimationsApng(n)
if err != nil { if err != nil {
return err return err
} }