From 5cb8b7c5e8b1080b2becf335f02ff2cbfd6206a4 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Wed, 1 Nov 2023 03:12:30 +1300 Subject: [PATCH] Feature: resize and output single frames with single_sprites bool and sprite_resize flag. --- main.go | 5 +-- src/gontage.go | 98 +++++++++++++++++++++++++++++--------------------- 2 files changed, 61 insertions(+), 42 deletions(-) diff --git a/main.go b/main.go index 3575aee..aae6f6f 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ func main() { sprite_source_folder := flag.String("f", "", "Folder name that contains sprites.") hframes := flag.Int("hframes", 8, "Amount of horizontal sprites you want in your spritesheet: default 8.") sprite_resize_px := flag.Int("sprite_resize", 0, "Resize each tile/sprite in spritesheet to the pixel value provided.") + single_sprites := flag.Bool("single_sprites", false, "Resize each tile/sprite in spritesheet to the pixel value provided.") 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") @@ -55,7 +56,7 @@ func main() { } if *sprite_source_folder != "" { - gontage.Gontage(*sprite_source_folder, hframes, *sprite_resize_px) + gontage.Gontage(*sprite_source_folder, hframes, *sprite_resize_px, *single_sprites) } else { var wg sync.WaitGroup if parent_folder_path != nil { @@ -120,7 +121,7 @@ func call_gontage_or_montage(i int, spritesheet spritesheet, folder folderInfo, } fmt.Println(string(out), filepath.Join(folder.sub_folder_path_gontage, folder.folder_name)+"/*", sprite_name) } else { - gontage.Gontage(filepath.Join(folder.sub_folder_path_gontage, folder.folder_name), &spritesheet.hframes, spritesheet.sprite_resize_px) + gontage.Gontage(filepath.Join(folder.sub_folder_path_gontage, folder.folder_name), &spritesheet.hframes, spritesheet.sprite_resize_px, false) } } diff --git a/src/gontage.go b/src/gontage.go index 2dd0c8d..a9a5f05 100644 --- a/src/gontage.go +++ b/src/gontage.go @@ -26,7 +26,7 @@ type drawingInfo struct { spritesheet draw.Image } -func Gontage(sprite_source_folder string, hframes *int, sprite_resize_px int) { +func Gontage(sprite_source_folder string, hframes *int, sprite_resize_px int, single_sprites bool) { start := time.Now() pwd, err := os.Getwd() if err != nil { @@ -73,49 +73,67 @@ func Gontage(sprite_source_folder string, hframes *int, sprite_resize_px int) { } chunk_images_waitgroup.Wait() - spritesheet_width, spritesheet_height, vframes := calcSheetDimensions(*hframes, all_decoded_images) - - spritesheet := image.NewNRGBA(image.Rect(0, 0, spritesheet_width, spritesheet_height)) - draw.Draw(spritesheet, spritesheet.Bounds(), spritesheet, image.Point{}, draw.Src) - decoded_images_to_draw_chunked := sliceChunk(all_decoded_images, *hframes) - - var make_spritesheet_wg sync.WaitGroup - for count_vertical_frames, sprite_chunk := range decoded_images_to_draw_chunked { - drawing := drawingInfo{ - sprites: sprite_chunk, - hframes: *hframes, - vframes: int(vframes), - spritesheet: spritesheet, - } - make_spritesheet_wg.Add(1) - go func(vertical_frames_count int, sprite_chunk []image.Image) { - drawing.vertical_frames_count = vertical_frames_count - defer make_spritesheet_wg.Done() - drawSpritesheet(drawing) - }(count_vertical_frames, sprite_chunk) - } - make_spritesheet_wg.Wait() - spritesheet_name := fmt.Sprintf("%v_f%v_v%v.png", sprite_source_folder, len(all_decoded_images), vframes) - f, err := os.Create(spritesheet_name) - if err != nil { - panic(err) - } - - encoder := png.Encoder{CompressionLevel: png.BestSpeed} - if sprite_resize_px != 0 { - resized_spritesheet := resize.Resize(uint(*hframes*sprite_resize_px), uint(int(vframes)*sprite_resize_px), - spritesheet, resize.Lanczos3) - if err = encoder.Encode(f, resized_spritesheet); err != nil { - log.Printf("failed to encode: %v", err) + // TODO: refactor we use similiar code in other places as well... + if single_sprites && sprite_resize_px != 0 { + sprite_source_folder_resized_name := fmt.Sprintf("%v_resized_%vpx", sprite_source_folder, sprite_resize_px) + 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_%v.png", i, "resized") + f, err := os.Create(sprite_source_folder_resized_name + resized_sprite_name) + if err != nil { + panic(err) + } + resized_image := resize.Resize(uint(sprite_resize_px), uint(sprite_resize_px), decoded_image, resize.Lanczos3) + if err = encoder.Encode(f, resized_image); err != nil { + log.Printf("failed to encode: %v", err) + } + fmt.Println(sprite_source_folder_resized_name + resized_sprite_name) + f.Close() } + fmt.Println(time.Since(start)) } else { - if err = encoder.Encode(f, spritesheet); err != nil { - log.Printf("failed to encode: %v", err) + spritesheet_width, spritesheet_height, vframes := calcSheetDimensions(*hframes, all_decoded_images) + spritesheet := image.NewNRGBA(image.Rect(0, 0, spritesheet_width, spritesheet_height)) + draw.Draw(spritesheet, spritesheet.Bounds(), spritesheet, image.Point{}, draw.Src) + decoded_images_to_draw_chunked := sliceChunk(all_decoded_images, *hframes) + var make_spritesheet_wg sync.WaitGroup + for count_vertical_frames, sprite_chunk := range decoded_images_to_draw_chunked { + drawing := drawingInfo{ + sprites: sprite_chunk, + hframes: *hframes, + vframes: int(vframes), + spritesheet: spritesheet, + } + make_spritesheet_wg.Add(1) + go func(vertical_frames_count int, sprite_chunk []image.Image) { + drawing.vertical_frames_count = vertical_frames_count + defer make_spritesheet_wg.Done() + drawSpritesheet(drawing) + }(count_vertical_frames, sprite_chunk) + } + make_spritesheet_wg.Wait() + spritesheet_name := fmt.Sprintf("%v_f%v_v%v.png", sprite_source_folder, len(all_decoded_images), vframes) + f, err := os.Create(spritesheet_name) + if err != nil { + panic(err) + } + encoder := png.Encoder{CompressionLevel: png.BestSpeed} + if sprite_resize_px != 0 { + resized_spritesheet := resize.Resize(uint(*hframes*sprite_resize_px), uint(int(vframes)*sprite_resize_px), + spritesheet, resize.Lanczos3) + if err = encoder.Encode(f, resized_spritesheet); err != nil { + log.Printf("failed to encode: %v", err) + } + } else { + if err = encoder.Encode(f, spritesheet); err != nil { + log.Printf("failed to encode: %v", err) + } } - } - f.Close() - fmt.Println(spritesheet_name, ": ", time.Since(start)) + f.Close() + fmt.Println(spritesheet_name, ": ", time.Since(start)) + } } }