diff --git a/README.md b/README.md index 7a680dc..007904f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ FFWebP is a small, single-binary CLI for converting images between formats, thin - Single binary: no external tools required - Auto-detects input codec and infers output from the file extension -- Supports AVIF, BMP, Farbfeld, GIF, ICO, JPEG, JPEG XL, PCX, PNG, PNM (PBM/PGM/PPM/PAM), PSD (decode-only), QOI, SVG (decode-only), TGA, TIFF, WebP and XBM +- Supports AVIF, BMP, Farbfeld, GIF, HEIF/HEIC, ICO/CUR, JPEG, JPEG XL, PCX, PNG, PNM (PBM/PGM/PPM/PAM), PSD (decode-only), QOI, SVG (decode-only), TGA, TIFF, WebP and XBM - Lossy or lossless output with configurable quality - Thumbnail generation via Lanczos3 resampling - Per-codec flags for fine-grained control (see `ffwebp --help`) diff --git a/go.mod b/go.mod index 03c50c0..9354e14 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/anthonynsimon/bild v0.14.0 github.com/ftrvxmtrx/tga v0.0.0-20150524081124-bd8e8d5be13a github.com/gen2brain/avif v0.4.4 + github.com/gen2brain/heic v0.4.5 github.com/gen2brain/jpegxl v0.4.5 github.com/gen2brain/webp v0.5.5 github.com/hullerob/go.farbfeld v0.0.0-20181222022525-3661193c725f diff --git a/go.sum b/go.sum index bf24473..e0aa853 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/ftrvxmtrx/tga v0.0.0-20150524081124-bd8e8d5be13a h1:eSqaRmdlZ9JsJ7JuW github.com/ftrvxmtrx/tga v0.0.0-20150524081124-bd8e8d5be13a/go.mod h1:US5WvgEHtG+BvWNNs6gk937h0QL2g2x+r7RH8m3g80Y= github.com/gen2brain/avif v0.4.4 h1:Ga/ss7qcWWQm2bxFpnjYjhJsNfZrWs5RsyklgFjKRSE= github.com/gen2brain/avif v0.4.4/go.mod h1:/XCaJcjZraQwKVhpu9aEd9aLOssYOawLvhMBtmHVGqk= +github.com/gen2brain/heic v0.4.5 h1:Cq3hPu6wwlTJNv2t48ro3oWje54h82Q5pALeCBNgaSk= +github.com/gen2brain/heic v0.4.5/go.mod h1:ECnpqbqLu0qSje4KSNWUUDK47UPXPzl80T27GWGEL5I= github.com/gen2brain/jpegxl v0.4.5 h1:TWpVEn5xkIfsswzkjHBArd0Cc9AE0tbjBSoa0jDsrbo= github.com/gen2brain/jpegxl v0.4.5/go.mod h1:4kWYJ18xCEuO2vzocYdGpeqNJ990/Gjy3uLMg5TBN6I= github.com/gen2brain/webp v0.5.5 h1:MvQR75yIPU/9nSqYT5h13k4URaJK3gf9tgz/ksRbyEg= diff --git a/internal/builtins/heif.go b/internal/builtins/heif.go new file mode 100644 index 0000000..59a525b --- /dev/null +++ b/internal/builtins/heif.go @@ -0,0 +1,8 @@ +//go:build heif || full +// +build heif full + +package builtins + +import ( + _ "github.com/coalaura/ffwebp/internal/codec/heif" +) diff --git a/internal/codec/heif/heif.go b/internal/codec/heif/heif.go new file mode 100644 index 0000000..8923aba --- /dev/null +++ b/internal/codec/heif/heif.go @@ -0,0 +1,80 @@ +package heif + +import ( + "bytes" + "fmt" + "image" + "io" + + "github.com/gen2brain/heic" + + "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 "heif" +} + +func (impl) Extensions() []string { + return []string{"heic", "heif"} +} + +func (impl) CanEncode() bool { + return false +} + +func (impl) Flags(flags []cli.Flag) []cli.Flag { + return flags +} + +func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) { + buf := make([]byte, 32) + + n, err := reader.ReadAt(buf, 0) + if err != nil && err != io.EOF { + return 0, nil, err + } + + buf = buf[:n] + + if len(buf) < 12 { + return 0, nil, nil + } + + if !bytes.Equal(buf[4:8], []byte("ftyp")) { + return 0, nil, nil + } + + brands := [][]byte{ + []byte("heic"), + []byte("heix"), + []byte("hevc"), + []byte("hevx"), + []byte("mif1"), + []byte("msf1"), + } + + for _, b := range brands { + if bytes.Contains(buf[8:], b) { + return 90, buf[:n], nil + } + } + + return 0, nil, nil +} + +func (impl) Decode(reader io.Reader) (image.Image, error) { + return heic.Decode(reader) +} + +func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error { + return fmt.Errorf("heif: encoding not supported") +} diff --git a/test/image.cur b/test/image.cur new file mode 100644 index 0000000..8f85153 Binary files /dev/null and b/test/image.cur differ