forked from tidbyt/pixlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
147 lines (126 loc) · 2.79 KB
/
render.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"image"
"io/ioutil"
"log"
"os"
"strings"
"github.com/spf13/cobra"
"tidbyt.dev/pixlet/encode"
"tidbyt.dev/pixlet/runtime"
)
var (
output string
magnify int
renderGif bool
)
func init() {
rootCmd.AddCommand(renderCmd)
renderCmd.Flags().StringVarP(&output, "output", "o", "", "Path for rendered image")
renderCmd.Flags().BoolVarP(&renderGif, "gif", "", false, "Generate GIF instead of WebP")
renderCmd.Flags().IntVarP(
&magnify,
"magnify",
"m",
1,
"Increase image dimension by a factor (useful for debugging)",
)
}
var renderCmd = &cobra.Command{
Use: "render [script] [<key>=value>]...",
Short: "Runs script with provided config parameters.",
Args: cobra.MinimumNArgs(1),
Run: render,
}
func render(cmd *cobra.Command, args []string) {
script := args[0]
if !strings.HasSuffix(script, ".star") {
fmt.Printf("script file must have suffix .star: %s\n", script)
os.Exit(1)
}
outPath := strings.TrimSuffix(script, ".star")
if renderGif {
outPath += ".gif"
} else {
outPath += ".webp"
}
if output != "" {
outPath = output
}
config := map[string]string{}
for _, param := range args[1:] {
split := strings.Split(param, "=")
if len(split) != 2 {
fmt.Printf("parameters must be on form <key>=<value>, found %s\n", param)
os.Exit(1)
}
config[split[0]] = split[1]
}
src, err := ioutil.ReadFile(script)
if err != nil {
fmt.Printf("failed to read file %s: %v\n", script, err)
os.Exit(1)
}
runtime.InitCache(runtime.NewInMemoryCache())
applet := runtime.Applet{}
err = applet.Load(script, src, nil)
if err != nil {
fmt.Printf("failed to load applet: %v\n", err)
os.Exit(1)
}
roots, err := applet.Run(config)
if err != nil {
log.Printf("Error running script: %s\n", err)
os.Exit(1)
}
screens := encode.ScreensFromRoots(roots)
filter := func(input image.Image) (image.Image, error) {
if magnify <= 1 {
return input, nil
}
in, ok := input.(*image.RGBA)
if !ok {
return nil, fmt.Errorf("image not RGBA, very weird")
}
out := image.NewRGBA(
image.Rect(
0, 0,
in.Bounds().Dx()*magnify,
in.Bounds().Dy()*magnify),
)
for x := 0; x < in.Bounds().Dx(); x++ {
for y := 0; y < in.Bounds().Dy(); y++ {
for xx := 0; xx < 10; xx++ {
for yy := 0; yy < 10; yy++ {
out.SetRGBA(
x*magnify+xx,
y*magnify+yy,
in.RGBAAt(x, y),
)
}
}
}
}
return out, nil
}
var buf []byte
if renderGif {
buf, err = screens.EncodeGIF(filter)
} else {
buf, err = screens.EncodeWebP(filter)
}
if err != nil {
fmt.Printf("Error rendering: %s\n", err)
os.Exit(1)
}
if outPath == "-" {
_, err = os.Stdout.Write(buf)
} else {
err = ioutil.WriteFile(outPath, buf, 0644)
}
if err != nil {
fmt.Printf("Writing %s: %s", outPath, err)
os.Exit(1)
}
}