From 04f2576832b14d6886897bac6ea573046da09bd1 Mon Sep 17 00:00:00 2001 From: Benjamin Dumke-von der Ehe Date: Tue, 12 Sep 2023 08:12:33 +0200 Subject: [PATCH] flag to output the avatar data as a json file --- README.md | 4 ++++ main.go | 24 +++++++++++++++++++++--- unicornify/avatar.go | 27 ++++++++++++++++++++++++--- unicornify/unicorndata.go | 2 +- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cb7f26b..5ca005b 100644 --- a/README.md +++ b/README.md @@ -98,3 +98,7 @@ The left image was generated normally, the right image with disabled anti-aliasi ## Disable parallelization The drawing operation is parallelized by default to make use of multiple processor cores. You can disable this with the `-serial` switch. + +## Save avatar data to a JSON file + +To save all the data (colors, angles, sizes etc.) to a JSON file, pass `-dataout filename.json`. diff --git a/main.go b/main.go index 5d79037..f02e8f3 100644 --- a/main.go +++ b/main.go @@ -4,22 +4,24 @@ import ( "bufio" "crypto/md5" "encoding/hex" + "encoding/json" "flag" "fmt" - "github.com/balpha/go-unicornify/unicornify" "image" "image/png" "math/rand" "os" "strings" "time" + + "github.com/balpha/go-unicornify/unicornify" ) func main() { var mail, hash string var random, free, zoomOut, nodouble, noshading, nograss, serial bool var size int - var outfile string + var outfile, datafile string flag.StringVar(&mail, "m", "", "the email address for which a unicorn avatar should be generated") flag.StringVar(&hash, "h", "", "the hash for which a unicorn avatar should be generated") @@ -32,6 +34,7 @@ func main() { flag.BoolVar(&noshading, "noshading", false, "do not add shading, this will make unicorns look flatter") flag.BoolVar(&nograss, "nograss", false, "do not add grass to the ground") flag.BoolVar(&serial, "serial", false, "do not parallelize the drawing") + flag.StringVar(&datafile, "dataout", "", "if given, a JSON file of this name will be created with all the unicorn data") flag.Parse() inputs := 0 @@ -77,7 +80,7 @@ func main() { fmt.Printf("\r%v%% ", perc) } - err, img := unicornify.MakeAvatar(hash, actualSize, !free, zoomOut, !noshading, !nograss && !free, !serial, yCallback) + err, img, allData := unicornify.MakeAvatar(hash, actualSize, !free, zoomOut, !noshading, !nograss && !free, !serial, yCallback) fmt.Print("\r \r") if err != nil { os.Stderr.WriteString("Not a valid hexadecimal number: " + hash + "\n") @@ -102,6 +105,21 @@ func main() { os.Stderr.WriteString("Error writing to output file\n") os.Exit(1) } + + if datafile != "" { + json, err := json.MarshalIndent(allData, "", " ") + if err != nil { + os.Stderr.WriteString("Error creating content for data file\n") + os.Stderr.WriteString(err.Error()) + + os.Exit(1) + } + err = os.WriteFile(datafile, json, 0o644) + if err != nil { + os.Stderr.WriteString("Error writing data file\n") + os.Exit(1) + } + } } func mail2hash(mail string) string { diff --git a/unicornify/avatar.go b/unicornify/avatar.go index 330e23b..82e5908 100644 --- a/unicornify/avatar.go +++ b/unicornify/avatar.go @@ -10,11 +10,21 @@ import ( pyrand "github.com/balpha/gopyrand" ) -func MakeAvatar(hash string, size int, withBackground bool, zoomOut bool, shading bool, grass bool, parallelize bool, yCallback func(int)) (error, *image.RGBA) { +type AllData struct { + UnicornData UnicornData + BackgroundData BackgroundData + GrassData GrassData + Scale float64 + XAngle float64 + YAngle float64 + FocalLength float64 +} + +func MakeAvatar(hash string, size int, withBackground bool, zoomOut bool, shading bool, grass bool, parallelize bool, yCallback func(int)) (error, *image.RGBA, AllData) { rand := pyrand.NewRandom() err := rand.SeedFromHexString(hash) if err != nil { - return err, nil + return err, nil, AllData{} } data := UnicornData{} @@ -36,6 +46,7 @@ func MakeAvatar(hash string, size int, withBackground bool, zoomOut bool, shadin abs := rand.RandInt(10, 75) yAngle := float64(90+sign*abs) * DEGREE xAngle := float64(rand.RandInt(-20, 20)) * DEGREE + originalXAngle := xAngle data.Randomize2(rand) bgdata.Randomize2(rand) @@ -170,5 +181,15 @@ func MakeAvatar(hash string, size int, withBackground bool, zoomOut bool, shadin DrawTracer(tracer, wv, img, yCallback) } - return nil, img + allData := AllData{ + UnicornData: data, + BackgroundData: bgdata, + GrassData: grassdata, + Scale: unicornScaleFactor, + XAngle: originalXAngle, + YAngle: yAngle, + FocalLength: focalLength, + } + + return nil, img, allData } diff --git a/unicornify/unicorndata.go b/unicornify/unicorndata.go index c4e3d17..6124244 100644 --- a/unicornify/unicorndata.go +++ b/unicornify/unicorndata.go @@ -41,7 +41,7 @@ type UnicornData struct { BrowLength float64 BrowMood float64 // from -1 (angry) to 1 (astonished) - PoseKind func(*Unicorn, float64) + PoseKind func(*Unicorn, float64) `json:"-"` PoseKindIndex int PosePhase float64