commit fe80f51a22e3ccdf2b85f859fa603f0ecc41cb95
parent 254ea970859cbc90d79c6d7814217bfa22952ed7
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date: Tue, 25 Jan 2022 21:50:45 +0800
webp: accept generic color spaces
Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Diffstat:
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/main.go b/main.go
@@ -35,12 +35,16 @@ func run(args []string) error {
if err != nil {
return fmt.Errorf("decoding src image: %w", err)
}
- rgbaImg, ok := img.(*image.NRGBA)
- if !ok {
- return fmt.Errorf("image is not NRGBA format: %T", img)
+ rgbaImg := image.NewNRGBA(img.Bounds())
+ rect := img.Bounds()
+ for y := rect.Min.Y; y < rect.Max.Y; y++ {
+ for x := rect.Min.X; x < rect.Max.X; x++ {
+ rgbaImg.Set(x, y, img.At(x, y))
+ }
}
var (
- quality = float32(1.0)
+ quality = float32(1.0)
+ // BUG: turning this off leads to divide by zero.
lossless = int32(1)
// out buffer to contain webp data.
out *byte = nil
@@ -69,6 +73,9 @@ func run(args []string) error {
if size == 0 {
return fmt.Errorf("encoding webp image: size %d", size)
}
+ if out == nil {
+ return fmt.Errorf("failed to allocate memory; probably errored")
+ }
if err := os.WriteFile("out.webp", libc.GoBytes(uintptr(unsafe.Pointer(out)), int(size)), 0644); err != nil {
return fmt.Errorf("writing out webp image: %w", err)
}