2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-09-14 21:56:41 +02:00
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2016-04-23 16:45:33 +02:00
|
|
|
"image/color"
|
2014-09-14 21:56:41 +02:00
|
|
|
"image/draw"
|
2024-01-06 10:34:37 +01:00
|
|
|
"io/fs"
|
2014-09-14 21:56:41 +02:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2014-10-03 12:07:53 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-09-16 01:13:12 +02:00
|
|
|
|
2015-12-25 22:07:54 +01:00
|
|
|
"github.com/bamiaux/rez"
|
2014-09-14 21:56:41 +02:00
|
|
|
)
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
type pyramidCreator struct {
|
|
|
|
numWorkers int
|
|
|
|
outDir string
|
|
|
|
bg color.RGBA
|
|
|
|
}
|
|
|
|
|
2024-01-06 10:34:37 +01:00
|
|
|
func findMaxDir(files []os.DirEntry) (min, max int) {
|
2014-09-14 21:56:41 +02:00
|
|
|
min, max = math.MaxInt32, math.MinInt32
|
|
|
|
for _, file := range files {
|
2024-01-06 10:34:37 +01:00
|
|
|
if !file.IsDir() {
|
2014-09-14 21:56:41 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if x, err := strconv.Atoi(file.Name()); err == nil {
|
|
|
|
if x > max {
|
|
|
|
max = x
|
|
|
|
}
|
|
|
|
if x < min {
|
|
|
|
min = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-06 10:34:37 +01:00
|
|
|
func findMaxFile(files []os.DirEntry) (min, max int) {
|
2014-09-14 21:56:41 +02:00
|
|
|
min, max = math.MaxInt32, math.MinInt32
|
|
|
|
for _, file := range files {
|
2024-01-06 10:34:37 +01:00
|
|
|
if !file.Type().IsRegular() {
|
2014-09-14 21:56:41 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := file.Name()
|
|
|
|
name = strings.TrimSuffix(name, filepath.Ext(name))
|
|
|
|
if x, err := strconv.Atoi(name); err == nil {
|
|
|
|
if x > max {
|
|
|
|
max = x
|
|
|
|
}
|
|
|
|
if x < min {
|
|
|
|
min = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type pyramidJob struct {
|
|
|
|
src [4]string
|
|
|
|
dst string
|
|
|
|
}
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
func (pc *pyramidCreator) createParentLevel(
|
|
|
|
oldDir string,
|
|
|
|
jobs chan pyramidJob) (newDir string, err error) {
|
|
|
|
|
2014-09-14 21:56:41 +02:00
|
|
|
oldName := filepath.Base(oldDir)
|
|
|
|
|
|
|
|
var oldLevel int
|
|
|
|
if oldLevel, err = strconv.Atoi(oldName); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldLevel <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-06 10:34:37 +01:00
|
|
|
var files []fs.DirEntry
|
|
|
|
if files, err = os.ReadDir(oldDir); err != nil {
|
2014-09-14 21:56:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
xMin, xMax := findMaxDir(files)
|
|
|
|
if xMax == math.MinInt32 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newLevel := oldLevel - 1
|
|
|
|
|
2015-06-21 18:36:26 +02:00
|
|
|
log.Printf("Generating tiles of level %d\n", newLevel)
|
2014-09-14 21:56:41 +02:00
|
|
|
|
|
|
|
parentDir := filepath.Dir(oldDir)
|
|
|
|
newDir = filepath.Join(parentDir, strconv.Itoa(newLevel))
|
|
|
|
|
|
|
|
if err = os.MkdirAll(newDir, os.ModePerm); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for ox, nx := xMin, xMin; ox <= xMax; ox += 2 {
|
|
|
|
|
|
|
|
ox1Dir := filepath.Join(oldDir, strconv.Itoa(ox))
|
|
|
|
ox2Dir := filepath.Join(oldDir, strconv.Itoa(ox+1))
|
|
|
|
|
2024-01-06 10:34:37 +01:00
|
|
|
if files, err = os.ReadDir(ox1Dir); err != nil {
|
2014-09-14 21:56:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
zMin, zMax := findMaxFile(files)
|
|
|
|
if zMax == math.MinInt32 {
|
|
|
|
nx++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nxDir := filepath.Join(newDir, strconv.Itoa(nx))
|
|
|
|
if err = os.MkdirAll(nxDir, os.ModePerm); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for oz, nz := zMin, zMin; oz <= zMax; oz += 2 {
|
2015-06-21 18:36:26 +02:00
|
|
|
oz1 := strconv.Itoa(oz) + ".png"
|
|
|
|
oz2 := strconv.Itoa(oz+1) + ".png"
|
2014-09-14 21:56:41 +02:00
|
|
|
s1 := filepath.Join(ox1Dir, oz1)
|
|
|
|
s2 := filepath.Join(ox1Dir, oz2)
|
|
|
|
s3 := filepath.Join(ox2Dir, oz1)
|
|
|
|
s4 := filepath.Join(ox2Dir, oz2)
|
2015-06-21 18:36:26 +02:00
|
|
|
d := filepath.Join(nxDir, strconv.Itoa(nz)+".png")
|
2014-09-14 21:56:41 +02:00
|
|
|
jobs <- pyramidJob{src: [4]string{s1, s2, s3, s4}, dst: d}
|
|
|
|
nz++
|
|
|
|
}
|
|
|
|
|
|
|
|
nx++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-21 09:04:59 +02:00
|
|
|
func clip8(x int) int {
|
|
|
|
switch {
|
|
|
|
case x < 0:
|
|
|
|
return 0
|
|
|
|
case x > 256:
|
|
|
|
return 256
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func clipRect(r image.Rectangle) image.Rectangle {
|
|
|
|
return image.Rectangle{
|
|
|
|
Min: image.Point{X: clip8(r.Min.X), Y: clip8(r.Min.Y)},
|
|
|
|
Max: image.Point{X: clip8(r.Max.X), Y: clip8(r.Max.Y)}}
|
|
|
|
}
|
|
|
|
|
2014-09-14 23:10:54 +02:00
|
|
|
var dps = [4]image.Point{
|
|
|
|
image.Pt(0, 256),
|
|
|
|
image.Pt(0, 0),
|
|
|
|
image.Pt(256, 256),
|
|
|
|
image.Pt(256, 0)}
|
2014-09-14 21:56:41 +02:00
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
func (pc *pyramidCreator) fuseTile(
|
|
|
|
scratch, resized *image.RGBA,
|
|
|
|
conv rez.Converter,
|
|
|
|
job *pyramidJob) error {
|
2014-09-14 21:56:41 +02:00
|
|
|
|
|
|
|
for i, path := range job.src {
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
img := common.LoadPNG(path, pc.bg)
|
2014-09-14 21:56:41 +02:00
|
|
|
|
2014-09-21 09:04:59 +02:00
|
|
|
sr := clipRect(img.Bounds())
|
2014-09-14 23:10:54 +02:00
|
|
|
r := sr.Sub(sr.Min).Add(dps[i])
|
|
|
|
|
|
|
|
draw.Draw(scratch, r, img, sr.Min, draw.Src)
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-25 22:07:54 +01:00
|
|
|
if err := conv.Convert(resized, scratch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-14 21:56:41 +02:00
|
|
|
|
2015-08-10 17:43:52 +02:00
|
|
|
log.Printf("Writing pyramid tile '%s'.\n", job.dst)
|
|
|
|
|
2014-09-21 17:30:19 +02:00
|
|
|
return common.SaveAsPNG(job.dst, resized)
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
func (pc *pyramidCreator) fuseTiles(jobs chan pyramidJob, done *sync.WaitGroup) {
|
2014-09-14 21:56:41 +02:00
|
|
|
defer done.Done()
|
|
|
|
scratch := image.NewRGBA(image.Rect(0, 0, 512, 512))
|
2015-12-25 22:07:54 +01:00
|
|
|
resized := image.NewRGBA(image.Rect(0, 0, 256, 256))
|
|
|
|
|
|
|
|
cfg, err := rez.PrepareConversion(resized, scratch)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WARN: cannot prepare rescaling: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conv, err := rez.NewConverter(cfg, common.ResizeFilter)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WARN: Cannot create image converter: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-14 21:56:41 +02:00
|
|
|
|
|
|
|
for job := range jobs {
|
2016-04-23 16:45:33 +02:00
|
|
|
if err := pc.fuseTile(scratch, resized, conv, &job); err != nil {
|
2015-06-21 18:36:26 +02:00
|
|
|
log.Printf("WARN: Writing image failed: %s\n", err)
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
func (pc *pyramidCreator) create() (err error) {
|
2014-09-14 21:56:41 +02:00
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
for oldDir := filepath.Join(pc.outDir, baseLevelDir); oldDir != ""; {
|
|
|
|
if oldDir, err = pc.createLevel(oldDir); err != nil {
|
2014-09-14 21:56:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
func (pc *pyramidCreator) createLevel(oldDir string) (string, error) {
|
2014-09-14 21:56:41 +02:00
|
|
|
|
|
|
|
jobs := make(chan pyramidJob)
|
|
|
|
|
|
|
|
var done sync.WaitGroup
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
for i := 0; i < pc.numWorkers; i++ {
|
2014-09-14 21:56:41 +02:00
|
|
|
done.Add(1)
|
2016-04-23 16:45:33 +02:00
|
|
|
go pc.fuseTiles(jobs, &done)
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
newDir, err := pc.createParentLevel(oldDir, jobs)
|
2014-09-14 21:56:41 +02:00
|
|
|
close(jobs)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-04-23 16:45:33 +02:00
|
|
|
return newDir, err
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done.Wait()
|
|
|
|
|
2016-04-23 16:45:33 +02:00
|
|
|
return newDir, err
|
2014-09-14 21:56:41 +02:00
|
|
|
}
|