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

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