1
0
mirror of https://github.com/coalaura/ffwebp.git synced 2025-09-08 05:49:54 +00:00
This commit is contained in:
2025-08-11 03:02:30 +02:00
parent 6c75477d42
commit 506326bef1
4 changed files with 94 additions and 0 deletions

8
internal/builtins/ico.go Normal file
View File

@@ -0,0 +1,8 @@
//go:build ico || core || full
// +build ico core full
package builtins
import (
_ "github.com/coalaura/ffwebp/internal/codec/ico"
)

80
internal/codec/ico/ico.go Normal file
View File

@@ -0,0 +1,80 @@
package ico
import (
"bytes"
"image"
"io"
"github.com/sergeymakinen/go-ico"
"github.com/coalaura/ffwebp/internal/codec"
"github.com/coalaura/ffwebp/internal/opts"
"github.com/urfave/cli/v3"
)
func init() {
codec.Register(impl{})
}
type impl struct{}
func (impl) String() string {
return "ico"
}
func (impl) Extensions() []string {
return []string{"ico", "cur"}
}
func (impl) Flags(flags []cli.Flag) []cli.Flag {
return flags
}
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
magicICO := []byte{0x00, 0x00, 0x01, 0x00}
magicCUR := []byte{0x00, 0x00, 0x02, 0x00}
buf := make([]byte, 4)
if _, err := reader.ReadAt(buf, 0); err != nil {
return 0, nil, err
}
if bytes.Equal(buf, magicICO) || bytes.Equal(buf, magicCUR) {
return 100, buf, nil
}
return 0, nil, nil
}
func (impl) Decode(r io.Reader) (image.Image, error) {
images, err := ico.DecodeAll(r)
if err != nil {
return nil, err
}
var (
best image.Image
bestArea int
)
for _, img := range images {
bounds := img.Bounds()
area := bounds.Dx() * bounds.Dy()
if area > bestArea {
best = img
bestArea = area
}
}
if best == nil {
return nil, io.EOF
}
return best, nil
}
func (impl) Encode(w io.Writer, img image.Image, _ opts.Common) error {
return ico.Encode(w, img)
}