Skip to content

Commit

Permalink
Add support for decoding .tga files.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-wannacott committed Nov 12, 2024
1 parent bb7ba65 commit c67bb20
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Create spritesheets from multiple folders of sprites/images up to ~48x+ faster t
## Features
* Images to Spritesheet: flags(-f or -mf)
* Images to Resized images: flags (-f -ss -sr)
* Spritesheet cut into images: flags (-f -c)
* Spritesheet cut into images: flags (-f -x)

## Help:
`gontage -h`
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/LeeWannacott/gontage
go 1.23

require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // direct

require github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017 h1:awJfkE1xXsPK+yOi1JfFBYCrSBkZXWbOgEFL6dmYeUA=
github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017/go.mod h1:47yJHzYP/+2SCHY45B0eyR1QaecoOhkTTpS7UasE0DY=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
sprite_resize_px := flag.Int("sr", 0, "Sprite Resize: Resize each sprite to the pixel value provided.")
single_sprites := flag.Bool("ss", false, "Single Sprites: Output sprites rather than spritesheet use with -sr flag")
cpu_threads := flag.Int("t", 0, "CPU threads available (default max available)")
cut_spritesheet := flag.String("c", "", "Example: -c 128x128. Cut spritesheet into size individual sprites. ")
cut_spritesheet := flag.String("x", "", "Example: -x 128x128. Cut spritesheet into size individual sprites. ")
parent_folder_path := flag.String("mf", "", "Multiple Folders: path should be parent folder containing sub folders that contain folders with sprites/images in them. Refer to test_multi for example structure.")
useMontage := flag.Bool("montage", false, "Use montage with -mf instead of gontage (if installed)")
help := flag.Bool("h", false, "Display help")
Expand Down Expand Up @@ -73,7 +73,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
for i, sub_folder := range parent_folder {
for _, sub_folder := range parent_folder {
if err != nil {
fmt.Println(err)
}
Expand All @@ -86,7 +86,7 @@ func main() {
sprite_source_folder: *sprite_source_folder,
}
amount_of_sprites, folder_names, sprite_height, sprite_width :=
iterate_folder(sub_folder_path, i)
iterate_folder(sub_folder_path)
spritesheet := spritesheet{
sprite_height: sprite_height,
sprite_width: sprite_width,
Expand Down Expand Up @@ -144,7 +144,7 @@ func call_gontage_or_montage(i int, spritesheet spritesheet, folder folderInfo,
}
}

func iterate_folder(file_path_to_walk string, index int) ([]int, []string, int, int) {
func iterate_folder(file_path_to_walk string) ([]int, []string, int, int) {
is_first_sprite_in_directory := true
folder_names := []string{}
amount_of_sprites := []int{}
Expand Down
Binary file added sprite_cut/barrel_red_f18_v3_f24_v3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 22 additions & 8 deletions src/gontage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sync"
"time"

"github.com/dblezek/tga"
"github.com/nfnt/resize"
)

Expand All @@ -34,6 +35,7 @@ type GontageArgs struct {
Sprite_resize_px_resize int
Single_sprites bool
Cut_spritesheet string
Convert_sprites string
Cpu_threads int
}

Expand Down Expand Up @@ -91,7 +93,7 @@ func Gontage(gargs GontageArgs) {
}
chunk_images_waitgroup.Wait()

if gargs.Single_sprites && gargs.Sprite_resize_px_resize != 0 {
if gargs.Single_sprites {
spritesToResizedSprites(gargs, all_decoded_images, all_decoded_images_names, start)
} else if gargs.Cut_spritesheet != "" {
cutSpritesheetIntoSprites(gargs, all_decoded_images, all_decoded_images_names, start)
Expand All @@ -106,14 +108,25 @@ func decodeImages(sprites_folder []fs.DirEntry, targetFolder string, pwd string,
var sprites_array []image.Image
var sprites_names []string
for _, sprite := range sprites_folder {
if !sprite.IsDir() && filepath.Ext(sprite.Name()) == ".png" {
if !sprite.IsDir() {
if reader, err := os.Open(filepath.Join(pwd, targetFolder, sprite.Name())); err == nil {
m, _, err := image.Decode(reader)
if err != nil {
log.Fatalln(err)
switch filepath.Ext(sprite.Name()) {
// TODO: refactor cases with duplicated logic
case ".tga":
s, err := tga.Decode(reader)
if err != nil {
log.Fatalln(err)
}
sprites_array = append(sprites_array, s)
sprites_names = append(sprites_names, sprite.Name())
default:
s, t, err := image.Decode(reader)
if err != nil {
log.Fatalln(err, t)
}
sprites_array = append(sprites_array, s)
sprites_names = append(sprites_names, sprite.Name())
}
sprites_array = append(sprites_array, m)
sprites_names = append(sprites_names, sprite.Name())
reader.Close()
}
}
Expand Down Expand Up @@ -162,7 +175,8 @@ func spritesToResizedSprites(gargs GontageArgs, all_decoded_images []image.Image
os.Mkdir(sprite_source_folder_resized_name, 0755)
encoder := png.Encoder{CompressionLevel: png.BestSpeed}
for i, decoded_image := range all_decoded_images {
resized_sprite_name := fmt.Sprintf("/%v", all_decoded_images_names[i])
sprite_name := strings.Split(all_decoded_images_names[i], ".")
resized_sprite_name := fmt.Sprintf("/%v.png", sprite_name[0])
f, err := os.Create(sprite_source_folder_resized_name + resized_sprite_name)
if err != nil {
panic(err)
Expand Down

0 comments on commit c67bb20

Please sign in to comment.