From d774cf373d2934af365f332f1104725b11c30785 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Sun, 31 Dec 2023 17:44:07 +0100 Subject: [PATCH] Add parameters for DZI output --- bin/stitch/README.md | 4 +++ bin/stitch/dzi.go | 10 +++---- bin/stitch/export-dzi.go | 4 +-- bin/stitch/main.go | 61 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/bin/stitch/README.md b/bin/stitch/README.md index f58f35c..369fd0b 100644 --- a/bin/stitch/README.md +++ b/bin/stitch/README.md @@ -44,6 +44,10 @@ example list of files: - `output string` The path and filename of the resulting stitched image. Defaults to "output.png". Supported formats/file extensions: `.png`, `.jpg`, `.dzi`. + - `dzi-tile-size` + The size of the resulting deep zoom image (DZI) tiles in pixels. Defaults to 512. + - `dzi-tile-overlap` + TThe number of additional pixels around every deep zoom image (DZI) tile in pixels. Defaults to 2. - `xmax int` Right bound of the output rectangle. This coordinate is not included in the output. - `xmin int` diff --git a/bin/stitch/dzi.go b/bin/stitch/dzi.go index 86f068d..cf53230 100644 --- a/bin/stitch/dzi.go +++ b/bin/stitch/dzi.go @@ -17,11 +17,6 @@ import ( "time" ) -const ( - dziTileSize = 512 // The (maximum) width and height of a tile in pixels, not including the overlap. - dziOverlap = 2 // The amount of additional pixels on every side of every tile. The real (max) width/height of an image is `2*overlap + tileSize`. -) - type DZI struct { stitchedImage *StitchedImage @@ -33,7 +28,10 @@ type DZI struct { maxZoomLevel int // The maximum zoom level that is needed. } -func NewDZI(stitchedImage *StitchedImage) DZI { +// NewDZI creates a new DZI from the given StitchedImages. +// +// dziTileSize and dziOverlap define the size and overlap of the resulting DZI tiles. +func NewDZI(stitchedImage *StitchedImage, dziTileSize, dziOverlap int) DZI { dzi := DZI{ stitchedImage: stitchedImage, diff --git a/bin/stitch/export-dzi.go b/bin/stitch/export-dzi.go index 8b03d9e..8791c0a 100644 --- a/bin/stitch/export-dzi.go +++ b/bin/stitch/export-dzi.go @@ -12,12 +12,12 @@ import ( "strings" ) -func exportDZI(stitchedImage *StitchedImage, outputPath string) error { +func exportDZI(stitchedImage *StitchedImage, outputPath string, dziTileSize, dziOverlap int) error { descriptorPath := outputPath extension := filepath.Ext(outputPath) outputTilesPath := strings.TrimSuffix(outputPath, extension) + "_files" - dzi := NewDZI(stitchedImage) + dzi := NewDZI(stitchedImage, dziTileSize, dziOverlap) // Create base directory of all DZI files. if err := os.MkdirAll(outputTilesPath, 0755); err != nil { diff --git a/bin/stitch/main.go b/bin/stitch/main.go index 8acb182..a75aa96 100644 --- a/bin/stitch/main.go +++ b/bin/stitch/main.go @@ -25,6 +25,8 @@ var flagPlayerPathInputPath = flag.String("player-path", filepath.Join(".", ".." var flagOutputPath = flag.String("output", filepath.Join(".", "output.png"), "The path and filename of the resulting stitched image. Supported formats/file extensions: `.png`, `.jpg`, `.dzi`.") var flagScaleDivider = flag.Int("divide", 1, "A downscaling factor. 2 will produce an image with half the side lengths.") var flagBlendTileLimit = flag.Int("blend-tile-limit", 9, "Limits median blending to the n newest tiles by file modification time. If set to 0, all available tiles will be median blended.") +var flagDZITileSize = flag.Int("dzi-tile-size", 512, "The size of the resulting deep zoom image (DZI) tiles in pixels.") +var flagDZIOverlap = flag.Int("dzi-tile-overlap", 2, "The number of additional pixels around every deep zoom image (DZI) tile in pixels.") var flagXMin = flag.Int("xmin", 0, "Left bound of the output rectangle. This coordinate is included in the output.") var flagYMin = flag.Int("ymin", 0, "Upper bound of the output rectangle. This coordinate is included in the output.") var flagXMax = flag.Int("xmax", 0, "Right bound of the output rectangle. This coordinate is not included in the output.") @@ -229,6 +231,62 @@ func main() { *flagOutputPath = result } + fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath)) + + // Query the user, if there were no cmd arguments given. + if flag.NFlag() == 0 && fileExtension == ".dzi" { + prompt := promptui.Prompt{ + Label: "Enter DZI tile size:", + Default: fmt.Sprint(*flagDZITileSize), + AllowEdit: true, + Validate: func(s string) error { + var num int + _, err := fmt.Sscanf(s, "%d", &num) + if err != nil { + return err + } + if int(num) < 1 { + return fmt.Errorf("number must be at least 1") + } + + return nil + }, + } + + result, err := prompt.Run() + if err != nil { + log.Panicf("Error while getting user input: %v.", err) + } + fmt.Sscanf(result, "%d", flagDZITileSize) + } + + // Query the user, if there were no cmd arguments given. + if flag.NFlag() == 0 && fileExtension == ".dzi" { + prompt := promptui.Prompt{ + Label: "Enter DZI tile overlap:", + Default: fmt.Sprint(*flagDZIOverlap), + AllowEdit: true, + Validate: func(s string) error { + var num int + _, err := fmt.Sscanf(s, "%d", &num) + if err != nil { + return err + } + if int(num) < 0 { + return fmt.Errorf("number must be at least 0") + } + + return nil + }, + } + + result, err := prompt.Run() + if err != nil { + log.Panicf("Error while getting user input: %v.", err) + } + fmt.Sscanf(result, "%d", flagDZIOverlap) + } + startTime := time.Now() bar := pb.Full.New(0) @@ -266,7 +324,6 @@ func main() { } }() - fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath)) switch fileExtension { case ".png": if err := exportPNG(stitchedImage, *flagOutputPath); err != nil { @@ -277,7 +334,7 @@ func main() { log.Panicf("Export of JPEG file failed: %v", err) } case ".dzi": - if err := exportDZI(stitchedImage, *flagOutputPath); err != nil { + if err := exportDZI(stitchedImage, *flagOutputPath, *flagDZITileSize, *flagDZIOverlap); err != nil { log.Panicf("Export of DZI file failed: %v", err) } default: