/
cli.go
119 lines (115 loc) · 3.35 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"github.com/codegangsta/cli"
"github.com/jaredly/goflam3"
"github.com/kless/term"
"image"
"image/png"
"io"
"os"
"strconv"
)
func cliRender(c *cli.Context) {
var config flame.Config
config.Dims.Width = 400
config.Dims.Height = 400
config.Iterations = 10 * 1000 * 1000
config.Functions = []flame.FunConfig{
{5},
{7},
}
config.DataOut = c.String("dataout")
config.DataIn = c.String("datain")
config.NoImage = c.Bool("noimage")
if c.String("config") != "" {
flame.ReadConfig(c.String("config"), &config)
}
if c.Int("width") != 0 {
config.Dims.Width = c.Int("width")
}
if c.Int("height") != 0 {
config.Dims.Height = c.Int("height")
}
config.Dims.X, _ = strconv.ParseFloat(c.String("x"), 64)
config.Dims.Y, _ = strconv.ParseFloat(c.String("y"), 64)
config.Dims.Xscale, _ = strconv.ParseFloat(c.String("xscale"), 64)
config.Dims.Yscale, _ = strconv.ParseFloat(c.String("yscale"), 64)
if c.Int("iterations") != 0 {
config.Iterations = c.Int("iterations")
}
if len(c.IntSlice("f")) != 0 {
config.Functions = make([]flame.FunConfig, len(c.IntSlice("f")))
for i, v := range c.IntSlice("f") {
config.Functions[i] = flame.FunConfig{v}
}
}
outfile := "-"
if c.String("outfile") != "" {
outfile = c.String("outfile")
}
image, _ := flame.Flame(config)
if image != nil {
write(outfile, image)
}
}
func main() {
app := cli.NewApp()
app.Name = "Goflam3'"
app.Usage = "Render fractal flames using go"
var nofuncs cli.IntSlice
app.Commands = []cli.Command{
{
Name: "serve",
ShortName: "s",
Usage: "Webserver",
Flags: []cli.Flag{
cli.IntFlag{"port", 4000, "The port to run on (defaults to 3000)"},
},
Action: cliWebserver,
},
{
Name: "render",
ShortName: "r",
Usage: "Render a file",
Flags: []cli.Flag{
cli.StringFlag{"dataout, d", "", "Output generated points to the given path"},
cli.StringFlag{"datain", "", "Don't generate points; read them from the given path"},
cli.StringFlag{"config, c", "", "File containing config. Defaults to stdin if no datain is given"},
cli.StringFlag{"outfile, o", "", "The name of the output image. Defaults to stdout if -noimage is not set"},
cli.BoolFlag{"noimage, n", "Don't output an image; only dataout"},
// these can also be set by the config file
cli.IntFlag{"width, w", 0, "Width of the image in px"},
cli.IntFlag{"height", 0, "Height of the image in px"},
cli.StringFlag{"x", "0", "X translation"},
cli.StringFlag{"y", "0", "Y translation"},
cli.StringFlag{"xscale", "1", "X scale"},
cli.StringFlag{"yscale", "1", "Y scale"},
cli.IntFlag{"iterations, i", 0, "Number of iterations to execute"},
cli.IntSliceFlag{"function, f", &nofuncs, "Functions to use"},
},
Action: cliRender,
},
}
app.Action = cliRender
app.Run(os.Args)
}
func write(outfile string, image *image.RGBA) {
var out io.Writer
if outfile == "-" && term.IsTerminal(int(os.Stdout.Fd())) {
println("You're on a terminal, and I assume you don't want a face full of PNG binary. " +
"Specify -outfile if you want some name other than flame-image.png")
outfile = "flame-image.png"
}
if outfile != "-" {
outimg, err := os.Create(outfile)
if err != nil {
println("Failed to open file for writing", outfile)
return
}
out = outimg
defer outimg.Close()
} else {
out = os.Stdout
}
png.Encode(out, image)
}