1
0
mirror of https://github.com/coalaura/ffwebp.git synced 2025-09-08 22:09:55 +00:00
Files
ffwebp/internal/codec/xbm/xbm.go

72 lines
1.2 KiB
Go
Raw Normal View History

2025-08-11 04:29:51 +02:00
package xbm
import (
"bytes"
"image"
"io"
2025-08-13 15:23:53 +02:00
"github.com/coalaura/xbm"
2025-08-11 04:33:23 +02:00
2025-08-11 04:29:51 +02:00
"github.com/coalaura/ffwebp/internal/codec"
"github.com/coalaura/ffwebp/internal/opts"
"github.com/urfave/cli/v3"
)
2025-08-13 15:23:53 +02:00
var name string
2025-08-11 04:29:51 +02:00
func init() {
codec.Register(impl{})
}
type impl struct{}
func (impl) String() string {
return "xbm"
}
func (impl) Extensions() []string {
return []string{"xbm"}
}
func (impl) CanEncode() bool {
return true
}
func (impl) Flags(flags []cli.Flag) []cli.Flag {
2025-08-13 15:23:53 +02:00
return append(flags,
&cli.StringFlag{
Name: "xbm.name",
Usage: "XBM: name of the image definition",
Value: "image",
Destination: &name,
},
)
2025-08-11 04:29:51 +02:00
}
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
2025-08-13 15:23:53 +02:00
buf := make([]byte, 128)
2025-08-11 04:29:51 +02:00
n, err := reader.ReadAt(buf, 0)
if err != nil && err != io.EOF {
return 0, nil, err
}
buf = buf[:n]
2025-08-13 15:23:53 +02:00
if bytes.Contains(buf, []byte("#define")) && bytes.Contains(buf, []byte("bits[]")) {
2025-08-11 04:29:51 +02:00
return 90, buf, nil
}
return 0, nil, nil
}
func (impl) Decode(r io.Reader) (image.Image, error) {
2025-08-13 15:23:53 +02:00
return xbm.Decode(r)
2025-08-11 04:29:51 +02:00
}
2025-08-11 04:39:57 +02:00
func (impl) Encode(w io.Writer, img image.Image, _ opts.Common) error {
2025-08-13 15:23:53 +02:00
return xbm.Encode(w, img, xbm.XBMOptions{
Name: name,
})
2025-08-11 04:29:51 +02:00
}