mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-09-08 05:49:54 +00:00
ico
This commit is contained in:
2
go.mod
2
go.mod
@@ -9,10 +9,12 @@ require (
|
|||||||
github.com/gen2brain/jpegxl v0.4.5
|
github.com/gen2brain/jpegxl v0.4.5
|
||||||
github.com/gen2brain/webp v0.5.5
|
github.com/gen2brain/webp v0.5.5
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||||
|
github.com/sergeymakinen/go-ico v1.0.0-beta.0
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ebitengine/purego v0.8.3 // indirect
|
github.com/ebitengine/purego v0.8.3 // indirect
|
||||||
|
github.com/sergeymakinen/go-bmp v1.0.0 // indirect
|
||||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@@ -12,6 +12,10 @@ github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6
|
|||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sergeymakinen/go-bmp v1.0.0 h1:SdGTzp9WvCV0A1V0mBeaS7kQAwNLdVJbmHlqNWq0R+M=
|
||||||
|
github.com/sergeymakinen/go-bmp v1.0.0/go.mod h1:/mxlAQZRLxSvJFNIEGGLBE/m40f3ZnUifpgVDlcUIEY=
|
||||||
|
github.com/sergeymakinen/go-ico v1.0.0-beta.0 h1:m5qKH7uPKLdrygMWxbamVn+tl2HfiA3K6MFJw4GfZvQ=
|
||||||
|
github.com/sergeymakinen/go-ico v1.0.0-beta.0/go.mod h1:wQ47mTczswBO5F0NoDt7O0IXgnV4Xy3ojrroMQzyhUk=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
|
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
|
||||||
|
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