mirror of
https://github.com/mr0xb/spritestitch.git
synced 2026-08-27 20:44:55 -04:00
initial commit
This commit is contained in:
commit
2f396d314b
2 changed files with 116 additions and 0 deletions
113
main.go
Normal file
113
main.go
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
outputPath := flag.String("o", "output.png", "output spritesheet path")
|
||||
direction := flag.String("direction", "h", "join direction: h (horizontal) or v (vertical)")
|
||||
flag.Parse()
|
||||
|
||||
inputPaths := flag.Args()
|
||||
if len(inputPaths) == 0 {
|
||||
fmt.Println("Usage: spritestitch -o output.png [-direction h|v] sheet1.png sheet2.png ...")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *direction != "h" && *direction != "v" {
|
||||
fmt.Println("direction must be 'h' (horizontal) or 'v' (vertical)")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var images []image.Image
|
||||
var bounds []image.Rectangle
|
||||
|
||||
for _, path := range inputPaths {
|
||||
imgFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open %s: %v\n", path, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(imgFile)
|
||||
imgFile.Close()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to decode %s: %v\n", path, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
images = append(images, img)
|
||||
bounds = append(bounds, img.Bounds())
|
||||
}
|
||||
|
||||
var totalWidth, totalHeight int
|
||||
if *direction == "h" {
|
||||
maxHeight := 0
|
||||
sumWidth := 0
|
||||
for _, b := range bounds {
|
||||
w := b.Dx()
|
||||
h := b.Dy()
|
||||
sumWidth += w
|
||||
if h > maxHeight {
|
||||
maxHeight = h
|
||||
}
|
||||
}
|
||||
totalWidth = sumWidth
|
||||
totalHeight = maxHeight
|
||||
} else {
|
||||
maxWidth := 0
|
||||
sumHeight := 0
|
||||
for _, b := range bounds {
|
||||
w := b.Dx()
|
||||
h := b.Dy()
|
||||
sumHeight += h
|
||||
if w > maxWidth {
|
||||
maxWidth = w
|
||||
}
|
||||
}
|
||||
totalWidth = maxWidth
|
||||
totalHeight = sumHeight
|
||||
}
|
||||
|
||||
outImg := image.NewRGBA(image.Rect(0, 0, totalWidth, totalHeight))
|
||||
|
||||
offsetX, offsetY := 0, 0
|
||||
for i, img := range images {
|
||||
b := bounds[i]
|
||||
w := b.Dx()
|
||||
h := b.Dy()
|
||||
|
||||
var dstRect image.Rectangle
|
||||
if *direction == "h" {
|
||||
dstRect = image.Rect(offsetX, 0, offsetX+w, h)
|
||||
offsetX += w
|
||||
} else {
|
||||
dstRect = image.Rect(0, offsetY, w, offsetY+h)
|
||||
offsetY += h
|
||||
}
|
||||
|
||||
draw.Draw(outImg, dstRect, img, b.Min, draw.Over)
|
||||
}
|
||||
|
||||
outFile, err := os.Create(*outputPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to create output file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
if err := png.Encode(outFile, outImg); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to encode PNG: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Spritesheet written to %s\n", *outputPath)
|
||||
}
|
||||
Loading…
Reference in a new issue