go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

commit b410cf6d30e70ae5057fc0698422c1f0b5e53858
parent 69259af52c815d64657b5c38782f08681c57c6e6
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Wed, 26 Jan 2022 00:47:23 +0800

webp: add flags to control quality

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>

Diffstat:
Mmain.go | 28++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/main.go b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "image" "os" @@ -21,10 +22,17 @@ func main() { } func run(args []string) error { - if len(args) < 1 { + var ( + quality float64 + lossless bool + ) + flag.BoolVar(&lossless, "lossless", false, "lossless quality, ignores quality flag") + flag.Float64Var(&quality, "quality", 1.0, "quality from [0,1]") + flag.Parse() + if len(flag.Args()) < 1 { return fmt.Errorf("provide image to encode as webp, try megopher.png") } - input := args[0] + input := flag.Args()[0] srcf, err := os.Open(input) if err != nil { return fmt.Errorf("opening input file: %w", err) @@ -42,8 +50,6 @@ func run(args []string) error { } } var ( - quality = float32(100.0) - lossless = int32(0) // out buffer to contain webp data. out *byte = nil ) @@ -55,8 +61,6 @@ func run(args []string) error { int32(rgbaImg.Stride), // Function pointers are generated by taking a pointer to // a struct who's first field is that function. - // That being said, this could be nil because we don't actually - // invoke it in our bsearch implementation. *(*uintptr)(unsafe.Pointer( &struct { f func(tls *libc.TLS, picture uintptr, rgba uintptr, rgba_stride int32) int32 @@ -64,8 +68,9 @@ func run(args []string) error { WebPPictureImportRGBA, }, )), - quality, - lossless, + // quality for libwebp is [0,100]. + float32(quality*100), + boolToInt32(lossless), uintptr(unsafe.Pointer(&out)), ) if size == 0 { @@ -79,3 +84,10 @@ func run(args []string) error { } return nil } + +func boolToInt32(b bool) int32 { + if b { + return 1 + } + return 0 +}