/
main.go
111 lines (93 loc) · 2.44 KB
/
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
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
package main
import (
"flag"
"fmt"
"image/png"
"log"
"os"
"runtime/trace"
"github.com/TomasCrhonek/gshader/colormap"
"github.com/TomasCrhonek/gshader/shader"
)
const (
DEFAULT_IMAGEFILENAME = "image.png"
DEFAULT_WIDTH = 1024
DEFAULT_HEIGHT = 1024
DEFAULT_TRACEFILENAME = "main.trace"
)
const help string = `
Usage: gshader [-h] [-t] [-x pixels] [-y pixels] [-n filename]
-x width Image width in pixels.
-y height Image height in pixels.
-n name Image file name.
-l List avaiable shaders and colormaps.
-s shader Select shader. List shaders with -l.
-c color Select colormap. List colormaps with -l.
-t Generate trace information about performance. Use with 'go tool trace main.trace'.
-h This help page.
`
func main() {
traceQ := flag.Bool("t", false, "")
helpQ := flag.Bool("h", false, "")
imageFile := flag.String("n", DEFAULT_IMAGEFILENAME, "")
width := flag.Int("x", DEFAULT_WIDTH, "")
height := flag.Int("y", DEFAULT_HEIGHT, "")
list := flag.Bool("l", false, "")
shd := flag.String("s", "", "")
clr := flag.String("c", "", "")
flag.Usage = func() { fmt.Print(help) }
flag.Parse()
if *helpQ {
flag.Usage()
return
}
registerShader(shader.Mandelbrot, "mandel")
registerShader(shader.Gradient, "gradient")
registerShader(shader.Julia1, "julia1")
registerShader(shader.Julia2, "julia2")
registerShader(shader.RandomNoise, "randomnoise")
registerColormap(colormap.Red, "red")
registerColormap(colormap.Rainbow, "rainbow")
if *list {
listShadersColormaps()
return
}
var shaderFunc shader.Shader
if len(*shd) > 0 {
shaderFunc = getShader(*shd)
if shaderFunc == nil {
fmt.Fprintf(os.Stderr, "Shader '%s' not found.\n", *shd)
os.Exit(1)
}
} else {
shaderFunc = shaderList[0].shader
}
var colorFunc colormap.ColorMap
if len(*clr) > 0 {
colorFunc = getColormap(*clr)
if shaderFunc == nil {
fmt.Fprintf(os.Stderr, "ColorMap '%s' not found.\n", *clr)
os.Exit(1)
}
} else {
colorFunc = colorList[0].color
}
if *traceQ {
traceFile, err := os.Create(DEFAULT_TRACEFILENAME)
if err != nil {
log.Fatal(err)
}
defer traceFile.Close()
trace.Start(traceFile)
defer trace.Stop()
}
f, err := os.Create(*imageFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
encoder := png.Encoder{CompressionLevel: png.NoCompression}
if err := encoder.Encode(f, computeImage(*width, *height, shaderFunc, colorFunc)); err != nil {
log.Fatal(err)
}
}