2018-12-17 20:28:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-12-17 23:09:30 +00:00
|
|
|
"image"
|
2018-12-31 18:54:33 +00:00
|
|
|
"image/color"
|
2018-12-17 23:09:30 +00:00
|
|
|
"image/png"
|
2018-12-17 20:28:16 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2018-12-17 23:09:30 +00:00
|
|
|
"path/filepath"
|
2018-12-17 20:28:16 +00:00
|
|
|
|
|
|
|
"github.com/FooSoft/lazarus/formats/dat"
|
|
|
|
"github.com/FooSoft/lazarus/formats/dc6"
|
|
|
|
)
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
func loadPalette(path string) (*dat.Palette, error) {
|
2018-12-17 20:28:16 +00:00
|
|
|
fp, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
return dat.NewFromReader(fp)
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
func loadSprite(path string) (*dc6.Animation, error) {
|
2018-12-17 20:28:16 +00:00
|
|
|
fp, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
return dc6.NewFromReader(fp)
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
func extractSprite(spritePath string, palette *dat.Palette, targetDir string) error {
|
2018-12-17 23:09:30 +00:00
|
|
|
sprite, err := loadSprite(spritePath)
|
2018-12-17 20:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
for di, direction := range sprite.Directions {
|
|
|
|
for fi, frame := range direction.Frames {
|
2018-12-31 18:54:33 +00:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, frame.Size.X, frame.Size.Y))
|
|
|
|
for y := 0; y < frame.Size.Y; y++ {
|
|
|
|
for x := 0; x < frame.Size.X; x++ {
|
|
|
|
colorSrc := palette.Colors[frame.Data[y*frame.Size.X+x]]
|
|
|
|
colorDst := color.RGBA{colorSrc.R, colorSrc.G, colorSrc.B, 0xff}
|
|
|
|
img.Set(x, y, colorDst)
|
2018-12-17 23:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
basePath := filepath.Base(spritePath)
|
|
|
|
targetPath := fmt.Sprintf("%s_%d_%d.png", filepath.Join(targetDir, basePath), di, fi)
|
|
|
|
|
|
|
|
fp, err := os.Create(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
if err := png.Encode(fp, img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 20:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-12-17 23:09:30 +00:00
|
|
|
targetDir := flag.String("target", ".", "target directory")
|
2018-12-17 20:28:16 +00:00
|
|
|
flag.Usage = func() {
|
2018-12-17 23:09:30 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] palette_file [dc6_files]\n", path.Base(os.Args[0]))
|
2018-12-17 20:28:16 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Parameters:\n\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
if flag.NArg() < 1 {
|
2018-12-17 20:28:16 +00:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
palette, err := loadPalette(flag.Arg(0))
|
|
|
|
if err != nil {
|
2018-12-17 20:28:16 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-17 23:09:30 +00:00
|
|
|
|
|
|
|
for i := 1; i < flag.NArg(); i++ {
|
|
|
|
if err := extractSprite(flag.Arg(1), palette, *targetDir); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 20:28:16 +00:00
|
|
|
}
|