Prepare stitcher for multiple output formats

This commit is contained in:
David Vogel 2023-12-22 10:07:02 +01:00
parent f5a3bad396
commit a70a5a4d1a
2 changed files with 38 additions and 24 deletions

26
bin/stitch/export-png.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"image/png"
"log"
"os"
)
func exportPNG(stitchedImage *StitchedImage) error {
log.Printf("Creating output file %q.", *flagOutputPath)
f, err := os.Create(*flagOutputPath)
if err != nil {
log.Panic(err)
}
defer f.Close()
encoder := png.Encoder{
CompressionLevel: png.DefaultCompression,
}
if err := encoder.Encode(f, stitchedImage); err != nil {
log.Panic(err)
}
return nil
}

View File

@ -9,10 +9,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"image" "image"
"image/png"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"time" "time"
@ -240,11 +239,11 @@ func main() {
BlendTileLimit: *flagBlendTileLimit, // Limit median blending to the n newest tiles by file modification time. BlendTileLimit: *flagBlendTileLimit, // Limit median blending to the n newest tiles by file modification time.
} }
outputImage, err := NewStitchedImage(tiles, outputRect, blendMethod, 64, overlays) stitchedImage, err := NewStitchedImage(tiles, outputRect, blendMethod, 64, overlays)
if err != nil { if err != nil {
log.Panicf("NewStitchedImage() failed: %v.", err) log.Panicf("NewStitchedImage() failed: %v.", err)
} }
_, max := outputImage.Progress() _, max := stitchedImage.Progress()
bar.SetTotal(int64(max)).Start().SetRefreshRate(250 * time.Millisecond) bar.SetTotal(int64(max)).Start().SetRefreshRate(250 * time.Millisecond)
// Query progress and draw progress bar. // Query progress and draw progress bar.
@ -256,39 +255,28 @@ func main() {
for { for {
select { select {
case <-done: case <-done:
value, _ := outputImage.Progress() value, _ := stitchedImage.Progress()
bar.SetCurrent(int64(value)) bar.SetCurrent(int64(value))
bar.Finish() bar.Finish()
return return
case <-ticker.C: case <-ticker.C:
value, _ := outputImage.Progress() value, _ := stitchedImage.Progress()
bar.SetCurrent(int64(value)) bar.SetCurrent(int64(value))
} }
} }
}() }()
log.Printf("Creating output file %q.", *flagOutputPath) fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath))
f, err := os.Create(*flagOutputPath) switch fileExtension {
if err != nil { case ".png":
log.Panic(err) exportPNG(stitchedImage)
} default:
log.Panicf("Unknown output format %q.", fileExtension)
encoder := png.Encoder{
CompressionLevel: png.DefaultCompression,
}
if err := encoder.Encode(f, outputImage); err != nil {
f.Close()
log.Panic(err)
} }
done <- struct{}{} done <- struct{}{}
wg.Wait() wg.Wait()
log.Printf("Created output in %v.", time.Since(startTime))
if err := f.Close(); err != nil {
log.Panic(err)
}
log.Printf("Created output file %q in %v.", *flagOutputPath, time.Since(startTime))
//fmt.Println("Press the enter key to terminate the console screen!") //fmt.Println("Press the enter key to terminate the console screen!")
//fmt.Scanln() //fmt.Scanln()