From 0548db24c747830d632a5f5215a882bb84f8897c Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 24 Jun 2019 14:44:35 +0930 Subject: [PATCH] Make zoom levels descend to zero - ie '0' is the most zoomed out, where a single tile encompasses the entire image --- main.go | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 8814c74..064ef09 100644 --- a/main.go +++ b/main.go @@ -8,18 +8,25 @@ import "flag" import "fmt" import "os" +const currentVersion = "0.01" + func main() { - filenamePtr := flag.String("filename", "screenshot.png", "filename to open") - tileSizePtr := flag.Int ("tile-size", 512, "tile size, in pixels") + filenamePtr := flag.String("filename", "", "filename to open") + tileSizePtr := flag.Int ("tile-size", 256, "tile size, in pixels") concurrencyPtr := flag.Int ("concurrency", 5, "how many tiles to generate concurrently (threads)") flag.Parse() + if (*filenamePtr == "") { + fmt.Println("Error: You must specify a filename with --filename"); + return; + } + fmt.Println("opening file:", *filenamePtr) src, err := imaging.Open(*filenamePtr) if err != nil { - fmt.Println("could not open file:", err) + fmt.Println("Error: Could not open file:", err) return; } @@ -28,7 +35,20 @@ func main() { tile_size_x := *tileSizePtr tile_size_y := *tileSizePtr - z := 0 + // work out maximum zoom + var max_zoom int + zoom_test_size_x := size.X + zoom_test_size_y := size.Y + for max_zoom = 0 ; ; max_zoom++ { + if zoom_test_size_x < tile_size_x && zoom_test_size_y < tile_size_y { + break + } + zoom_test_size_x = zoom_test_size_x >> 1 + zoom_test_size_y = zoom_test_size_y >> 1 + } + + z := max_zoom + fmt.Println("maximum zoom level is", max_zoom) concurrency := *concurrencyPtr sem := make(chan bool, concurrency) @@ -37,7 +57,7 @@ func main() { // outer loop for zoom for { - if (z == 0) { + if (z == max_zoom) { // do nothing } else { // halve image size @@ -45,10 +65,6 @@ func main() { runtime.GC() // 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)) @@ -61,7 +77,10 @@ func main() { } - z++ + z-- + if (z < 0) { + break + } } // drain at the end of each zoom level