Fix up some bugs and update to modern go
This commit is contained in:
parent
f740dc806b
commit
85dafe4120
7
go.mod
Normal file
7
go.mod
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module github.com/tardisx/slicerdicer
|
||||||
|
|
||||||
|
go 1.22.1
|
||||||
|
|
||||||
|
require github.com/disintegration/imaging v1.6.2
|
||||||
|
|
||||||
|
require golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
5
go.sum
Normal file
5
go.sum
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
|
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
51
main.go
51
main.go
@ -1,12 +1,14 @@
|
|||||||
package main;
|
package main
|
||||||
|
|
||||||
import "image"
|
import (
|
||||||
import "image/png"
|
"flag"
|
||||||
import "github.com/disintegration/imaging"
|
"fmt"
|
||||||
import "runtime"
|
"image"
|
||||||
import "flag"
|
"image/png"
|
||||||
import "fmt"
|
"os"
|
||||||
import "os"
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
)
|
||||||
|
|
||||||
const currentVersion = "0.01"
|
const currentVersion = "0.01"
|
||||||
|
|
||||||
@ -15,19 +17,20 @@ func main() {
|
|||||||
filenamePtr := flag.String("filename", "", "filename to open")
|
filenamePtr := flag.String("filename", "", "filename to open")
|
||||||
tileSizePtr := flag.Int("tile-size", 256, "tile size, in pixels")
|
tileSizePtr := flag.Int("tile-size", 256, "tile size, in pixels")
|
||||||
concurrencyPtr := flag.Int("concurrency", 5, "how many tiles to generate concurrently (threads)")
|
concurrencyPtr := flag.Int("concurrency", 5, "how many tiles to generate concurrently (threads)")
|
||||||
|
baseName := flag.String("basename", "tile", "base of the output files")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if (*filenamePtr == "") {
|
if *filenamePtr == "" {
|
||||||
fmt.Println("Error: You must specify a filename with --filename");
|
fmt.Println("Error: You must specify a filename with --filename")
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("opening file:", *filenamePtr)
|
fmt.Println("opening file:", *filenamePtr)
|
||||||
src, err := imaging.Open(*filenamePtr)
|
src, err := imaging.Open(*filenamePtr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error: Could not open file:", err)
|
fmt.Println("Error: Could not open file:", err)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
size := src.Bounds().Max
|
size := src.Bounds().Max
|
||||||
@ -40,7 +43,7 @@ func main() {
|
|||||||
zoom_test_size_x := size.X
|
zoom_test_size_x := size.X
|
||||||
zoom_test_size_y := size.Y
|
zoom_test_size_y := size.Y
|
||||||
for max_zoom = 0; ; max_zoom++ {
|
for max_zoom = 0; ; max_zoom++ {
|
||||||
if zoom_test_size_x < tile_size_x && zoom_test_size_y < tile_size_y {
|
if zoom_test_size_x <= tile_size_x && zoom_test_size_y <= tile_size_y {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
zoom_test_size_x = zoom_test_size_x >> 1
|
zoom_test_size_x = zoom_test_size_x >> 1
|
||||||
@ -57,34 +60,33 @@ func main() {
|
|||||||
|
|
||||||
// outer loop for zoom
|
// outer loop for zoom
|
||||||
for {
|
for {
|
||||||
if (z == max_zoom) {
|
if z == max_zoom {
|
||||||
// do nothing
|
// do nothing
|
||||||
} else {
|
} else {
|
||||||
// halve image size
|
// halve image size
|
||||||
src = imaging.Resize(src, size.X/2, 0, imaging.NearestNeighbor)
|
src = imaging.Resize(src, size.X/2, 0, imaging.NearestNeighbor)
|
||||||
runtime.GC()
|
|
||||||
// recalculate size
|
// recalculate size
|
||||||
size = src.Bounds().Max
|
size = src.Bounds().Max
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(fmt.Sprintf("zoom level: %d (%d x %d)\n", z, size.X, size.Y))
|
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 y := 0; y < (size.Y / tile_size_y); y++ {
|
||||||
for x := 0 ; x <= (size.X / tile_size_x) ; x++ {
|
for x := 0; x < (size.X / tile_size_x); x++ {
|
||||||
sem <- true
|
sem <- true
|
||||||
go tile(src, z, x, y, tile_size_x, tile_size_y, sem)
|
go tile(*baseName, src, z, x, y, tile_size_x, tile_size_y, sem)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
z--
|
z--
|
||||||
if (z < 0) {
|
if z < 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// drain at the end of each zoom level
|
// drain at the end of each zoom level
|
||||||
// since we are about to modify the source image
|
// since we are about to modify the source image in memory
|
||||||
for i := 0; i < cap(sem); i++ {
|
for i := 0; i < cap(sem); i++ {
|
||||||
sem <- true
|
sem <- true
|
||||||
}
|
}
|
||||||
@ -92,10 +94,10 @@ func main() {
|
|||||||
fmt.Println("done")
|
fmt.Println("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func tile (src image.Image, z, x, y int, tile_size_x, tile_size_y int, sem chan bool) {
|
func tile(basename string, src image.Image, z, x, y int, tile_size_x, tile_size_y int, sem chan bool) {
|
||||||
defer func() { <-sem }()
|
defer func() { <-sem }()
|
||||||
output_filename := fmt.Sprintf("tile-%d-%d-%d.png", z, x, y)
|
output_filename := fmt.Sprintf("%s-%d-%d-%d.png", basename, 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));
|
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))
|
||||||
|
|
||||||
writer, _ := os.Create(output_filename)
|
writer, _ := os.Create(output_filename)
|
||||||
err := png.Encode(writer, cropped)
|
err := png.Encode(writer, cropped)
|
||||||
@ -104,6 +106,5 @@ func tile (src image.Image, z, x, y int, tile_size_x, tile_size_y int, sem chan
|
|||||||
}
|
}
|
||||||
writer.Close()
|
writer.Close()
|
||||||
|
|
||||||
runtime.GC()
|
return
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user