mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-09-08 05:49:54 +00:00
ico
This commit is contained in:
8
internal/builtins/ico.go
Normal file
8
internal/builtins/ico.go
Normal 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
80
internal/codec/ico/ico.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user