Allowed to not draw big picture

This commit is contained in:
Tnze
2021-03-14 13:36:22 +08:00
parent 91e7ffa464
commit 0acecb936d
2 changed files with 34 additions and 15 deletions

View File

@ -3,19 +3,19 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/Tnze/go-mc/data/block"
"github.com/Tnze/go-mc/save"
"github.com/Tnze/go-mc/save/region"
"image" "image"
"image/color" "image/color"
"image/draw" "image/draw"
"log" "log"
"math"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"unsafe" "unsafe"
"github.com/Tnze/go-mc/data/block"
"github.com/Tnze/go-mc/save"
"github.com/Tnze/go-mc/save/region"
) )
var colors []color.RGBA64 var colors []color.RGBA64
@ -24,6 +24,7 @@ var sectionWorkerNum = 1
var ( var (
regionsFold = flag.String("region", filepath.Join(os.Getenv("AppData"), ".minecraft", "saves", "World", "region"), "region directory path") regionsFold = flag.String("region", filepath.Join(os.Getenv("AppData"), ".minecraft", "saves", "World", "region"), "region directory path")
drawBigPicture = flag.Bool("bigmap", true, "draw the bit map")
) )
func main() { func main() {
@ -43,6 +44,18 @@ func main() {
mkmin(&min[1], &pos[1]) mkmin(&min[1], &pos[1])
} }
if *drawBigPicture {
for _, dir := range de {
name := dir.Name()
var pos [2]int // {x, z}
if _, err := fmt.Sscanf(name, "r.%d.%d.mca", &pos[0], &pos[1]); err != nil {
log.Printf("Error parsing file name of %s: %v, ignoring", name, err)
continue
}
updateMinMax(pos)
}
}
type regions struct { type regions struct {
pos [2]int pos [2]int
*region.Region *region.Region
@ -58,7 +71,6 @@ func main() {
log.Printf("Error parsing file name of %s: %v, ignoring", name, err) log.Printf("Error parsing file name of %s: %v, ignoring", name, err)
continue continue
} }
updateMinMax(pos)
r, err := region.Open(path) r, err := region.Open(path)
if err != nil { if err != nil {
@ -69,7 +81,10 @@ func main() {
} }
close(rs) close(rs)
}() }()
bigPicture := image.NewRGBA(image.Rect(min[0]*512, min[1]*512, max[0]*512+512, max[1]*512+512)) var bigPicture *image.RGBA
if *drawBigPicture {
bigPicture = image.NewRGBA(image.Rect(min[0]*512, min[1]*512, max[0]*512+512, max[1]*512+512))
}
var bigWg sync.WaitGroup var bigWg sync.WaitGroup
// draw columns // draw columns
for r := range rs { for r := range rs {
@ -121,10 +136,12 @@ func main() {
log.Print("Draw: ", r.pos) log.Print("Draw: ", r.pos)
go func(img image.Image, pos [2]int) { go func(img image.Image, pos [2]int) {
savePng(img, fmt.Sprintf("r.%d.%d.png", pos[0], pos[1])) savePng(img, fmt.Sprintf("r.%d.%d.png", pos[0], pos[1]))
if *drawBigPicture {
draw.Draw( draw.Draw(
bigPicture, image.Rect(pos[0]*512, pos[1]*512, pos[0]*512+512, pos[1]*512+512), bigPicture, image.Rect(pos[0]*512, pos[1]*512, pos[0]*512+512, pos[1]*512+512),
img, image.Pt(0, 0), draw.Src, img, image.Pt(0, 0), draw.Src,
) )
}
bigWg.Done() bigWg.Done()
}(img, r.pos) }(img, r.pos)
// To close mca files // To close mca files
@ -133,8 +150,10 @@ func main() {
} }
} }
bigWg.Wait() bigWg.Wait()
if *drawBigPicture {
savePng(bigPicture, "maps.png") savePng(bigPicture, "maps.png")
} }
}
func drawColumn(column *save.Column) (img *image.RGBA) { func drawColumn(column *save.Column) (img *image.RGBA) {
img = image.NewRGBA(image.Rect(0, 0, 16, 16)) img = image.NewRGBA(image.Rect(0, 0, 16, 16))
@ -162,13 +181,13 @@ func drawColumn(column *save.Column) (img *image.RGBA) {
func drawSection(s *save.Chunk, img *image.RGBA) { func drawSection(s *save.Chunk, img *image.RGBA) {
// calculate bits per block // calculate bits per block
//bpb := len(s.BlockStates) * 64 / (16 * 16 * 16) bpb := len(s.BlockStates) * 64 / (16 * 16 * 16)
// skip empty // skip empty
if len(s.BlockStates) == 0 { if len(s.BlockStates) == 0 {
return return
} }
// decode section // decode section
bpb := int(math.Max(4, math.Ceil(math.Log2(float64(len(s.Palette)))))) //bpb := int(math.Max(4, math.Ceil(math.Log2(float64(len(s.Palette))))))
// decode status // decode status
data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64 data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64

View File

@ -102,7 +102,7 @@ func (r *Region) Close() error {
return r.f.Close() return r.f.Close()
} }
func sectorLoc(offset int32) (o, s int32) { func sectorLoc(offset int32) (sec, num int32) {
return offset >> 8, offset & 0xFF return offset >> 8, offset & 0xFF
} }