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 04:18:47 +02:00
parent 86cc9e33de
commit c97cabf0ad
8 changed files with 103 additions and 3 deletions

View File

@@ -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`)

View File

@@ -112,7 +112,7 @@ func run(_ context.Context, cmd *cli.Command) error {
logx.Printf("reading input from <stdin>\n")
}
sniffed, reader, err := codec.Sniff(reader)
sniffed, reader, err := codec.Sniff(reader, input)
if err != nil {
return err
}

1
go.mod
View File

@@ -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

2
go.sum
View File

@@ -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=

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

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

View File

@@ -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

77
internal/codec/tga/tga.go Normal file
View File

@@ -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)
}

BIN
test/image.tga Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB