1
0
mirror of https://github.com/coalaura/ffwebp.git synced 2025-07-18 06:14:34 +00:00
Files
ffwebp/image.go

103 lines
2.1 KiB
Go
Raw Normal View History

2024-09-08 00:32:52 +02:00
package main
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
2025-01-22 16:58:09 +01:00
"io"
2024-09-08 00:32:52 +02:00
2025-01-22 16:58:09 +01:00
ico "github.com/biessek/golang-ico"
2024-09-08 00:32:52 +02:00
"github.com/gen2brain/avif"
"github.com/gen2brain/jpegxl"
"github.com/gen2brain/webp"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)
2025-01-22 18:19:20 +01:00
type Encoder func(io.Writer, image.Image) error
2025-01-22 16:58:09 +01:00
func ReadImage(input io.ReadSeeker) (image.Image, error) {
2024-09-08 00:32:52 +02:00
decoder, err := GetDecoderFromContent(input)
if err != nil {
return nil, err
}
return decoder(input)
}
2025-01-22 18:19:20 +01:00
func ResolveImageEncoder() (Encoder, error) {
table := NewOptionsTable()
switch opts.Format {
2024-09-08 00:32:52 +02:00
case "webp":
options := GetWebPOptions()
2025-01-22 18:19:20 +01:00
table.AddWebPOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return webp.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "jpeg":
options := GetJpegOptions()
2025-01-22 18:19:20 +01:00
table.AddJpegOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return jpeg.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "png":
2025-01-22 16:58:09 +01:00
encoder := GetPNGOptions()
2025-01-22 18:19:20 +01:00
table.AddPNGOptions(encoder)
2025-01-22 16:58:09 +01:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return encoder.Encode(output, img)
}, nil
2024-09-08 00:32:52 +02:00
case "gif":
options := GetGifOptions()
2025-01-22 18:19:20 +01:00
table.AddGifOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return gif.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "bmp":
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return bmp.Encode(output, img)
}, nil
2024-09-08 00:32:52 +02:00
case "tiff":
options := GetTiffOptions()
2025-01-22 18:19:20 +01:00
table.AddTiffOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return tiff.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "avif":
options := GetAvifOptions()
2025-01-22 18:19:20 +01:00
table.AddAvifOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return avif.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "jxl":
options := GetJxlOptions()
2025-01-22 18:19:20 +01:00
table.AddJxlOptions(options)
2024-09-08 00:32:52 +02:00
2025-01-22 18:19:20 +01:00
return func(output io.Writer, img image.Image) error {
return jpegxl.Encode(output, img, options)
}, nil
2024-09-08 00:32:52 +02:00
case "ico":
2025-01-22 18:19:20 +01:00
table.Print()
return func(output io.Writer, img image.Image) error {
return ico.Encode(output, img)
}, nil
2024-09-08 00:32:52 +02:00
}
2025-01-22 18:19:20 +01:00
return nil, fmt.Errorf("unsupported output format: %s", opts.Format)
2024-09-08 00:32:52 +02:00
}