forked from campoy/mandelbrot
/
main.go
52 lines (45 loc) · 942 Bytes
/
main.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
package main
import (
"errors"
"flag"
"image/gif"
"image/jpeg"
"image/png"
"log"
"os"
"path/filepath"
"runtime"
"github.com/campoy/mandelbrot/mandelbrot"
)
var (
output = flag.String("out", "mandelbrot.png", "name of the output image file")
height = flag.Int("h", 1024, "height of the output image in pixels")
width = flag.Int("w", 1024, "width of the output image in pixels")
)
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
// open a new file
f, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
// create the image
img := mandelbrot.Create(*height, *width)
// and encoding it
fmt := filepath.Ext(*output)
switch fmt {
case ".png":
err = png.Encode(f, img)
case ".jpg", ".jpeg":
err = jpeg.Encode(f, img, nil)
case ".gif":
err = gif.Encode(f, img, nil)
default:
err = errors.New("unkwnown format " + fmt)
}
// unless you can't
if err != nil {
log.Fatal(err)
}
}