Skip to content

Commit

Permalink
flag to output the avatar data as a json file
Browse files Browse the repository at this point in the history
  • Loading branch information
balpha committed Sep 12, 2023
1 parent 682dabb commit 04f2576
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
27 changes: 24 additions & 3 deletions unicornify/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion unicornify/unicorndata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 04f2576

Please # to comment.