From 76afabb1d817f22c9366b78474624f22cf546615 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Thu, 20 Jun 2019 21:45:48 +0930 Subject: [PATCH] Handle zoom levels --- README.md | 8 ++++++++ main.go | 54 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 916c714..10ac65f 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ # slicerdicer + +Slice and dice an image, turning it into many equal sized tiles. + +## Usage + +slicerdicer -help + +slicerdicer --filename foo.png --tile-size 256 diff --git a/main.go b/main.go index 2da5baa..ba2217f 100644 --- a/main.go +++ b/main.go @@ -31,27 +31,49 @@ func main() { fmt.Println("starting tiling") - for y := 0 ; y <= (size.Y / tile_size_y) ; y++ { + z := 0 - for x := 0 ; x <= (size.X / tile_size_x) ; x++ { - - output_filename := fmt.Sprintf("tile-0-%d-%d.png", x, y) - cropped := imaging.Crop(src, image.Rect(tile_size_x*x, tile_size_y*y, tile_size_x*x+tile_size_x, tile_size_y*y+tile_size_y)); - - fmt.Print("writing to: ", output_filename, " "); - fmt.Print("\r") - - writer, _ := os.Create(output_filename) - err = png.Encode(writer, cropped) - writer.Close() - runtime.GC() - if err != nil { - fmt.Println(err) + // outer loop for zoom + for { + if (z == 0) { + // do nothing + } else { + // halve image size + src = imaging.Resize(src, size.X/2, 0, imaging.Linear) + // recalculate size + size = src.Bounds().Max + // we are done if we are now smaller then the tile + if (size.X < tile_size_x || size.Y < tile_size_y) { + break; } } + + fmt.Print(fmt.Sprintf("zoom level: %d (%d x %d)\n", z, size.X, size.Y)) + + for y := 0 ; y <= (size.Y / tile_size_y) ; y++ { + + for x := 0 ; x <= (size.X / tile_size_x) ; x++ { + + output_filename := fmt.Sprintf("tile-%d-%d-%d.png", z, x, y) + cropped := imaging.Crop(src, image.Rect(tile_size_x*x, tile_size_y*y, tile_size_x*x+tile_size_x, tile_size_y*y+tile_size_y)); + + fmt.Print("writing to: ", output_filename, " "); + fmt.Print("\r") + + writer, _ := os.Create(output_filename) + err = png.Encode(writer, cropped) + writer.Close() + runtime.GC() + if err != nil { + fmt.Println(err) + } + } + } + + fmt.Println() + z++ } - fmt.Println() fmt.Println("done") }