mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-07-17 22:04:35 +00:00
logging and improvements
This commit is contained in:
8
internal/builtins/bmp.go
Normal file
8
internal/builtins/bmp.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build bmp || full
|
||||
// +build bmp full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/bmp"
|
||||
)
|
3
internal/builtins/builtins.go
Normal file
3
internal/builtins/builtins.go
Normal file
@ -0,0 +1,3 @@
|
||||
package builtins
|
||||
|
||||
// does nothing :)
|
8
internal/builtins/gif.go
Normal file
8
internal/builtins/gif.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build gif || core || full
|
||||
// +build gif core full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/gif"
|
||||
)
|
8
internal/builtins/jpeg.go
Normal file
8
internal/builtins/jpeg.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build jpeg || core || full
|
||||
// +build jpeg core full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/jpeg"
|
||||
)
|
8
internal/builtins/png.go
Normal file
8
internal/builtins/png.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build png || core || full
|
||||
// +build png core full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/png"
|
||||
)
|
8
internal/builtins/tiff.go
Normal file
8
internal/builtins/tiff.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build tiff || full
|
||||
// +build tiff full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/tiff"
|
||||
)
|
@ -18,7 +18,7 @@ func init() {
|
||||
|
||||
type impl struct{}
|
||||
|
||||
func (impl) Name() string {
|
||||
func (impl) String() string {
|
||||
return "bmp"
|
||||
}
|
||||
|
||||
@ -30,20 +30,20 @@ func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
return flags
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magic := []byte{0x42, 0x4D}
|
||||
|
||||
buf := make([]byte, 2)
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magic) {
|
||||
return 100, nil
|
||||
return 100, magic, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
|
@ -9,12 +9,12 @@ import (
|
||||
)
|
||||
|
||||
type Codec interface {
|
||||
Name() string
|
||||
String() string
|
||||
|
||||
Flags([]cli.Flag) []cli.Flag
|
||||
Extensions() []string
|
||||
|
||||
Sniff(io.ReaderAt) (int, error)
|
||||
Sniff(io.ReaderAt) (int, []byte, error)
|
||||
Decode(io.Reader) (image.Image, error)
|
||||
Encode(io.Writer, image.Image, opts.Common) error
|
||||
}
|
||||
@ -22,7 +22,7 @@ type Codec interface {
|
||||
var codecs = map[string]Codec{}
|
||||
|
||||
func Register(c Codec) {
|
||||
codecs[c.Name()] = c
|
||||
codecs[c.String()] = c
|
||||
}
|
||||
|
||||
func Flags(flags []cli.Flag) []cli.Flag {
|
||||
|
@ -9,7 +9,27 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Sniff(reader io.Reader) (Codec, io.Reader, error) {
|
||||
type Sniffed struct {
|
||||
Header []byte
|
||||
Confidence int
|
||||
Codec Codec
|
||||
}
|
||||
|
||||
func (s *Sniffed) String() string {
|
||||
var builder strings.Builder
|
||||
|
||||
for _, b := range s.Header {
|
||||
if b >= 32 && b <= 126 {
|
||||
builder.WriteByte(b)
|
||||
} else {
|
||||
builder.WriteRune('.')
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func Sniff(reader io.Reader) (*Sniffed, io.Reader, error) {
|
||||
buf, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@ -18,18 +38,20 @@ func Sniff(reader io.Reader) (Codec, io.Reader, error) {
|
||||
ra := bytes.NewReader(buf)
|
||||
|
||||
var (
|
||||
guess Codec
|
||||
best int
|
||||
magic []byte
|
||||
guess Codec
|
||||
)
|
||||
|
||||
for _, codec := range codecs {
|
||||
confidence, err := codec.Sniff(ra)
|
||||
confidence, header, err := codec.Sniff(ra)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if confidence > best {
|
||||
best = confidence
|
||||
magic = header
|
||||
guess = codec
|
||||
}
|
||||
}
|
||||
@ -38,7 +60,11 @@ func Sniff(reader io.Reader) (Codec, io.Reader, error) {
|
||||
return nil, nil, errors.New("unknown format")
|
||||
}
|
||||
|
||||
return guess, bytes.NewReader(buf), nil
|
||||
return &Sniffed{
|
||||
Header: magic,
|
||||
Confidence: best,
|
||||
Codec: guess,
|
||||
}, bytes.NewReader(buf), nil
|
||||
}
|
||||
|
||||
func Detect(output, override string) (Codec, error) {
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/coalaura/ffwebp/internal/codec"
|
||||
"github.com/coalaura/ffwebp/internal/logx"
|
||||
"github.com/coalaura/ffwebp/internal/opts"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
@ -20,7 +21,7 @@ func init() {
|
||||
|
||||
type impl struct{}
|
||||
|
||||
func (impl) Name() string {
|
||||
func (impl) String() string {
|
||||
return "gif"
|
||||
}
|
||||
|
||||
@ -44,21 +45,25 @@ func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
})
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magic7a := []byte("GIF87a")
|
||||
magic9a := []byte("GIF89a")
|
||||
|
||||
buf := make([]byte, 6)
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magic7a) || bytes.Equal(buf, magic9a) {
|
||||
return 100, nil
|
||||
if bytes.Equal(buf, magic7a) {
|
||||
return 100, magic7a, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
if bytes.Equal(buf, magic9a) {
|
||||
return 100, magic9a, nil
|
||||
}
|
||||
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
@ -66,6 +71,8 @@ func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
}
|
||||
|
||||
func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error {
|
||||
logx.Printf("gif: colors=%d\n", numColors)
|
||||
|
||||
return gif.Encode(writer, img, &gif.Options{
|
||||
NumColors: numColors,
|
||||
})
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/coalaura/ffwebp/internal/codec"
|
||||
"github.com/coalaura/ffwebp/internal/logx"
|
||||
"github.com/coalaura/ffwebp/internal/opts"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
@ -17,7 +18,7 @@ func init() {
|
||||
codec.Register(impl{})
|
||||
}
|
||||
|
||||
func (impl) Name() string {
|
||||
func (impl) String() string {
|
||||
return "jpeg"
|
||||
}
|
||||
|
||||
@ -29,20 +30,20 @@ func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
return flags
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
marker := []byte{0xFF, 0xD8, 0xFF}
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magic := []byte{0xFF, 0xD8, 0xFF}
|
||||
|
||||
buf := make([]byte, 3)
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, marker) {
|
||||
return 100, nil
|
||||
if bytes.Equal(buf, magic) {
|
||||
return 100, magic, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
@ -50,6 +51,8 @@ func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
}
|
||||
|
||||
func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error {
|
||||
logx.Printf("jpeg: quality=%d\n", options.Quality)
|
||||
|
||||
return jpeg.Encode(writer, img, &jpeg.Options{
|
||||
Quality: options.Quality,
|
||||
})
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/coalaura/ffwebp/internal/codec"
|
||||
"github.com/coalaura/ffwebp/internal/logx"
|
||||
"github.com/coalaura/ffwebp/internal/opts"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
@ -22,7 +23,7 @@ func init() {
|
||||
codec.Register(impl{})
|
||||
}
|
||||
|
||||
func (impl) Name() string {
|
||||
func (impl) String() string {
|
||||
return "png"
|
||||
}
|
||||
|
||||
@ -46,20 +47,20 @@ func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
})
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magic := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
|
||||
|
||||
buf := make([]byte, len(magic))
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magic) {
|
||||
return 100, nil
|
||||
return 100, magic, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
@ -67,6 +68,8 @@ func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
}
|
||||
|
||||
func (impl) Encode(writer io.Writer, img image.Image, _ opts.Common) error {
|
||||
logx.Printf("png: compression=%d\n", compression)
|
||||
|
||||
encoder := png.Encoder{
|
||||
CompressionLevel: compressionLevel(compression),
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"golang.org/x/image/tiff"
|
||||
|
||||
"github.com/coalaura/ffwebp/internal/codec"
|
||||
"github.com/coalaura/ffwebp/internal/logx"
|
||||
"github.com/coalaura/ffwebp/internal/opts"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
@ -24,7 +25,7 @@ func init() {
|
||||
|
||||
type impl struct{}
|
||||
|
||||
func (impl) Name() string {
|
||||
func (impl) String() string {
|
||||
return "tiff"
|
||||
}
|
||||
|
||||
@ -56,21 +57,25 @@ func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
)
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, error) {
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magicLE := []byte{0x49, 0x49, 0x2A, 0x00}
|
||||
magicBE := []byte{0x4D, 0x4D, 0x00, 0x2A}
|
||||
|
||||
buf := make([]byte, 4)
|
||||
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magicLE) || bytes.Equal(buf, magicBE) {
|
||||
return 100, nil
|
||||
if bytes.Equal(buf, magicLE) {
|
||||
return 100, magicLE, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
if bytes.Equal(buf, magicBE) {
|
||||
return 100, magicBE, nil
|
||||
}
|
||||
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
@ -78,6 +83,8 @@ func (impl) Decode(reader io.Reader) (image.Image, error) {
|
||||
}
|
||||
|
||||
func (impl) Encode(writer io.Writer, img image.Image, options opts.Common) error {
|
||||
logx.Printf("tiff: compression=%d predictor=%t\n", compression, predictor)
|
||||
|
||||
return tiff.Encode(writer, img, &tiff.Options{
|
||||
Compression: compressionType(compression),
|
||||
Predictor: predictor,
|
||||
|
54
internal/logx/logx.go
Normal file
54
internal/logx/logx.go
Normal file
@ -0,0 +1,54 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var enabled atomic.Bool
|
||||
|
||||
func init() {
|
||||
enabled.Store(true)
|
||||
}
|
||||
|
||||
func SetSilent() {
|
||||
enabled.Store(false)
|
||||
}
|
||||
|
||||
func Printf(format string, a ...any) {
|
||||
if !enabled.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
for i, v := range a {
|
||||
switch r := v.(type) {
|
||||
case time.Time:
|
||||
a[i] = time.Since(r)
|
||||
case image.Image:
|
||||
b := r.Bounds()
|
||||
|
||||
a[i] = fmt.Sprintf("%dx%dx", b.Dx(), b.Dy())
|
||||
default:
|
||||
a[i] = v
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, format, a...)
|
||||
}
|
||||
|
||||
func PrintKV(codec, key string, val any) {
|
||||
if !enabled.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
Printf("%s: %s=%v\n", codec, key, val)
|
||||
}
|
||||
|
||||
func Errorf(f string, a ...any) {
|
||||
fmt.Fprintf(os.Stderr, f, a...)
|
||||
|
||||
os.Exit(1)
|
||||
}
|
Reference in New Issue
Block a user