-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for WebP and AVIF only when CGO is enabled, fixes #304
- Loading branch information
Showing
6 changed files
with
100 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//go:build cgo | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !cgo | ||
|
||
package renderers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tdewolff/canvas" | ||
) | ||
|
||
// WebP returns a Webp writer that uses libwebp and accepts the following options: canvas.Resolution, canvas.Colorspace, github.com/kolesa-team/go-webp/encoder.*Options | ||
func WebP(opts ...interface{}) canvas.Writer { | ||
return errorWriter(fmt.Errorf("unsupported WebP: CGO must be enabled")) | ||
} | ||
|
||
// AVIF returns a AVIF writer that uses libaom and accepts the following options: canvas.Resolution, canvas.Colorspace, github.com/Kagami/go-avif.*Options | ||
func AVIF(opts ...interface{}) canvas.Writer { | ||
return errorWriter(fmt.Errorf("unsupported AVIF: CGO must be enabled")) | ||
} |