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 03:38:30 +02:00
parent 881b3455cb
commit de88ed5358
4 changed files with 34 additions and 26 deletions

View File

@@ -139,11 +139,13 @@ func run(_ context.Context, cmd *cli.Command) error {
common.FillDefaults() common.FillDefaults()
oCodec, err := codec.Detect(output, cmd.String("codec")) oCodec, oExt, err := codec.Detect(output, cmd.String("codec"))
if err != nil { if err != nil {
return err return err
} }
common.OutputExtension = oExt
logx.Printf("output codec: %s (forced=%v)\n", oCodec, cmd.IsSet("codec")) logx.Printf("output codec: %s (forced=%v)\n", oCodec, cmd.IsSet("codec"))
t0 := time.Now() t0 := time.Now()

View File

@@ -71,34 +71,39 @@ func Sniff(reader io.Reader) (*Sniffed, io.Reader, error) {
}, bytes.NewReader(buf), nil }, bytes.NewReader(buf), nil
} }
func Detect(output, override string) (Codec, error) { func Detect(output, override string) (Codec, string, error) {
if override != "" { ext := override
codec, ok := codecs[override]
if !ok {
return nil, fmt.Errorf("unsupported output codec: %q", override)
}
if !codec.CanEncode() {
return nil, fmt.Errorf("decode-only output codec: %q", override)
}
return codec, nil
}
if output == "-" {
return nil, errors.New("missing codec for output")
}
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(output), "."))
if ext == "" { if ext == "" {
return nil, fmt.Errorf("output filename %q has no extension", output) ext = strings.ToLower(strings.TrimPrefix(filepath.Ext(output), "."))
if ext == "" {
return nil, "", fmt.Errorf("output filename %q has no extension", output)
}
}
codec, err := FindCodec(ext)
if err != nil {
return nil, "", err
}
if codec == nil {
return nil, "", fmt.Errorf("unsupported output codec: %q", ext)
}
return codec, ext, nil
}
func FindCodec(ext string) (Codec, error) {
codec, ok := codecs[ext]
if ok {
return codec, nil
} }
for _, codec := range codecs { for _, codec := range codecs {
for _, alias := range codec.Extensions() { for _, alias := range codec.Extensions() {
if ext == strings.ToLower(alias) { if ext == alias {
if !codec.CanEncode() { if !codec.CanEncode() {
return nil, fmt.Errorf("decode-only output codec: %q", override) return nil, fmt.Errorf("decode-only output codec: %q", ext)
} }
return codec, nil return codec, nil
@@ -106,5 +111,5 @@ func Detect(output, override string) (Codec, error) {
} }
} }
return nil, fmt.Errorf("unsupported or unknown file extension: %q", ext) return nil, nil
} }

View File

@@ -86,7 +86,7 @@ func (impl) Decode(reader io.Reader) (image.Image, error) {
return tiff.Decode(reader) return tiff.Decode(reader)
} }
func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error { func (impl) Encode(writer io.Writer, img image.Image, _ opts.Common) error {
logx.Printf("tiff: compression=%d predictor=%t\n", compression, predictor) logx.Printf("tiff: compression=%d predictor=%t\n", compression, predictor)
return tiff.Encode(writer, img, &tiff.Options{ return tiff.Encode(writer, img, &tiff.Options{

View File

@@ -1,8 +1,9 @@
package opts package opts
type Common struct { type Common struct {
Quality int Quality int
Lossless bool Lossless bool
OutputExtension string
} }
func (c *Common) FillDefaults() { func (c *Common) FillDefaults() {