Skip to content

Commit

Permalink
Make zoom levels descend to zero - ie '0' is the most zoomed out, whe…
Browse files Browse the repository at this point in the history
…re a single tile encompasses the entire image
  • Loading branch information
tardisx committed Jun 24, 2019
1 parent e2ee7ae commit 0548db2
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -37,18 +57,14 @@ func main() {

// outer loop for zoom
for {
if (z == 0) {
if (z == max_zoom) {
// do nothing
} else {
// halve image size
src = imaging.Resize(src, size.X/2, 0, imaging.NearestNeighbor)
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))
Expand All @@ -61,7 +77,10 @@ func main() {

}

z++
z--
if (z < 0) {
break
}
}

// drain at the end of each zoom level
Expand Down

0 comments on commit 0548db2

Please sign in to comment.