diff --git a/README.md b/README.md index 67ecfe0..bb5daa1 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ FFWebP is a command line utility for converting images between multiple formats. ## Features - Pure Go implementation with no external runtime dependencies -- Supports AVIF, BMP, GIF, ICO, JPEG, JPEGXL, PNG, PNM (PBM/PGM/PPM/PAM), PSD (no encoding), TIFF and WebP +- Supports AVIF, BMP, GIF, ICO, JPEG, JPEGXL, PNG, PNM (PBM/PGM/PPM/PAM), PSD (no encoding), TGA, TIFF and WebP - Lossy or lossless output with configurable quality - Output codec selected from the output file extension when `--codec` is omitted - Full set of format-specific flags for every supported format (see `ffwebp --help`) diff --git a/cmd/ffwebp/main.go b/cmd/ffwebp/main.go index 3f11e54..11775c6 100644 --- a/cmd/ffwebp/main.go +++ b/cmd/ffwebp/main.go @@ -112,7 +112,7 @@ func run(_ context.Context, cmd *cli.Command) error { logx.Printf("reading input from \n") } - sniffed, reader, err := codec.Sniff(reader) + sniffed, reader, err := codec.Sniff(reader, input) if err != nil { return err } diff --git a/go.mod b/go.mod index 91a8b94..589743c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.5 require github.com/urfave/cli/v3 v3.3.8 require ( + github.com/ftrvxmtrx/tga v0.0.0-20150524081124-bd8e8d5be13a github.com/gen2brain/avif v0.4.4 github.com/gen2brain/jpegxl v0.4.5 github.com/gen2brain/webp v0.5.5 diff --git a/go.sum b/go.sum index 84fdc0f..1864386 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/ebitengine/purego v0.8.3 h1:K+0AjQp63JEZTEMZiwsI9g0+hAMNohwUOtY0RPGexmc= github.com/ebitengine/purego v0.8.3/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/ftrvxmtrx/tga v0.0.0-20150524081124-bd8e8d5be13a h1:eSqaRmdlZ9JsJ7JuWfDr3ym3monToXRczohBOL+heVQ= +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/jpegxl v0.4.5 h1:TWpVEn5xkIfsswzkjHBArd0Cc9AE0tbjBSoa0jDsrbo= diff --git a/internal/builtins/tga.go b/internal/builtins/tga.go new file mode 100644 index 0000000..e5f1364 --- /dev/null +++ b/internal/builtins/tga.go @@ -0,0 +1,8 @@ +//go:build tga || full +// +build tga full + +package builtins + +import ( + _ "github.com/coalaura/ffwebp/internal/codec/tga" +) diff --git a/internal/codec/detect.go b/internal/codec/detect.go index 6d7c862..3262a9b 100644 --- a/internal/codec/detect.go +++ b/internal/codec/detect.go @@ -29,7 +29,19 @@ func (s *Sniffed) String() string { return builder.String() } -func Sniff(reader io.Reader) (*Sniffed, io.Reader, error) { +func Sniff(reader io.Reader, input string) (*Sniffed, io.Reader, error) { + ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(input), ".")) + if ext != "" { + codec, _ := FindCodec(ext) + if codec != nil { + return &Sniffed{ + Header: []byte("." + ext), + Confidence: 100, + Codec: codec, + }, reader, nil + } + } + buf, err := io.ReadAll(reader) if err != nil { return nil, nil, err diff --git a/internal/codec/tga/tga.go b/internal/codec/tga/tga.go new file mode 100644 index 0000000..781a01a --- /dev/null +++ b/internal/codec/tga/tga.go @@ -0,0 +1,77 @@ +package tga + +import ( + "image" + "io" + + "github.com/ftrvxmtrx/tga" + + "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 "tga" +} + +func (impl) Extensions() []string { + return []string{"tga"} +} + +func (impl) CanEncode() bool { + return true +} + +func (impl) Flags(flags []cli.Flag) []cli.Flag { + return flags +} + +func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) { + buf := make([]byte, 3) + + if _, err := reader.ReadAt(buf, 0); err != nil { + return 0, nil, err + } + + colorMapType := buf[1] + + if colorMapType > 1 { + return 0, nil, nil + } + + validImageTypes := map[byte]bool{ + 0: true, // no image data + 1: true, // colormapped, uncompressed + 2: true, // truecolor, uncompressed + 3: true, // grayscale, uncompressed + 9: true, // colormapped, RLE + 10: true, // truecolor, RLE + 11: true, // grayscale, RLE + } + + imageType := buf[2] + + if !validImageTypes[imageType] { + return 0, nil, nil + } + + header := make([]byte, 3) + copy(header, buf) + + return 100, header, nil +} + +func (impl) Decode(reader io.Reader) (image.Image, error) { + return tga.Decode(reader) +} + +func (impl) Encode(writer io.Writer, img image.Image, _ opts.Common) error { + return tga.Encode(writer, img) +} diff --git a/test/image.tga b/test/image.tga new file mode 100644 index 0000000..88dadb9 Binary files /dev/null and b/test/image.tga differ