/
image.go
60 lines (51 loc) · 1.35 KB
/
image.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
package cli
import (
"image/png"
"os"
"runtime"
"github.com/rs/zerolog/log"
"github.com/schollz/progressbar/v3"
"github.com/teadove/awesome-fractals/internal/brot"
"github.com/teadove/awesome-fractals/internal/palette"
"github.com/urfave/cli/v2"
)
func drawImage(cCtx *cli.Context) error {
runtime.GOMAXPROCS(cCtx.Int(maxprocsFlag.Name))
colors, ok := palette.ColorPalettes[cCtx.String(paletteFlag.Name)]
if !ok {
return cli.Exit("Palette not found", 1)
}
done := make(chan struct{})
buildInput := brot.BuilderInput{
Colors: colors,
ColorStep: cCtx.Int(stepFlag.Name),
XPos: cCtx.Float64(xposFlag.Name),
YPos: cCtx.Float64(yposFlag.Name),
Width: cCtx.Int(widthFlag.Name),
Height: cCtx.Int(heightFlag.Name),
MaxIteration: cCtx.Int(iterationFlag.Name),
EscapeRadius: cCtx.Float64(radiusFlag.Name),
Done: done,
}
input, iterations := brot.NewInputFromBuilderInput(&buildInput)
go func() {
bar := progressbar.Default(int64(iterations))
for i := 0; i < iterations; i++ {
<-done
err := bar.Add(1)
if err != nil {
log.Error().Err(err).Str("status", "bar.add.error")
}
}
}()
img := brot.Render(input)
output, err := os.Create(cCtx.String(imageFilenameFlag.Name))
if err != nil {
return err
}
err = png.Encode(output, img)
if err != nil {
return err
}
return nil
}