-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathrenderers_formats.go
61 lines (56 loc) · 1.39 KB
/
renderers_formats.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
//go:build formats
package renderers
import (
"fmt"
"io"
"github.com/Kagami/go-avif"
webp "github.com/kolesa-team/go-webp/encoder"
"github.com/tdewolff/canvas"
"github.com/tdewolff/canvas/renderers/rasterizer"
)
func WebP(opts ...interface{}) canvas.Writer {
resolution := canvas.DPMM(1.0)
colorSpace := canvas.DefaultColorSpace
var options *webp.Options
for _, opt := range opts {
switch o := opt.(type) {
case canvas.Resolution:
resolution = o
case canvas.ColorSpace:
colorSpace = o
case *webp.Options:
options = o
default:
return errorWriter(fmt.Errorf("unknown WebP option: %T(%v)", opt, opt))
}
}
return func(w io.Writer, c *canvas.Canvas) error {
img := rasterizer.Draw(c, resolution, colorSpace)
enc, err := webp.NewEncoder(img, options)
if err != nil {
return err
}
return enc.Encode(w)
}
}
func AVIF(opts ...interface{}) canvas.Writer {
resolution := canvas.DPMM(1.0)
colorSpace := canvas.DefaultColorSpace
var options *avif.Options
for _, opt := range opts {
switch o := opt.(type) {
case canvas.Resolution:
resolution = o
case canvas.ColorSpace:
colorSpace = o
case *avif.Options:
options = o
default:
return errorWriter(fmt.Errorf("unknown AVIF option: %T(%v)", opt, opt))
}
}
return func(w io.Writer, c *canvas.Canvas) error {
img := rasterizer.Draw(c, resolution, colorSpace)
return avif.Encode(w, img, options)
}
}