mirror of
https://github.com/coalaura/ffwebp.git
synced 2025-09-08 13:59:54 +00:00
xcf (decode)
This commit is contained in:
@@ -6,7 +6,7 @@ FFWebP is a small, single-binary CLI for converting images between formats, thin
|
||||
|
||||
- Single binary: no external tools required
|
||||
- Auto-detects input codec and infers output from the file extension
|
||||
- Supports AVIF, BMP, Farbfeld, GIF, HEIF/HEIC (decode-only), ICO/CUR, JPEG, JPEG XL, PCX, PNG, PNM (PBM/PGM/PPM/PAM), PSD (decode-only), QOI, SVG (decode-only), TGA, TIFF, WebP, XPM and XBM
|
||||
- Supports AVIF, BMP, Farbfeld, GIF, HEIF/HEIC (decode-only), ICO/CUR, JPEG, JPEG XL, PCX, PNG, PNM (PBM/PGM/PPM/PAM), PSD (decode-only), QOI, SVG (decode-only), TGA, TIFF, WebP, XBM, XCF (decode-only) and XPM
|
||||
- Lossy or lossless output with configurable quality
|
||||
- Thumbnail generation via Lanczos3 resampling
|
||||
- Per-codec flags for fine-grained control (see `ffwebp --help`)
|
||||
|
1
go.mod
1
go.mod
@@ -11,6 +11,7 @@ require (
|
||||
github.com/gen2brain/heic v0.4.5
|
||||
github.com/gen2brain/jpegxl v0.4.5
|
||||
github.com/gen2brain/webp v0.5.5
|
||||
github.com/gonutz/xcf v0.0.0-20180404091035-c002b9533d97
|
||||
github.com/hullerob/go.farbfeld v0.0.0-20181222022525-3661193c725f
|
||||
github.com/kriticalflare/qoi v0.0.0-20240815192827-34f66f23bcef
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
|
2
go.sum
2
go.sum
@@ -18,6 +18,8 @@ github.com/gen2brain/jpegxl v0.4.5 h1:TWpVEn5xkIfsswzkjHBArd0Cc9AE0tbjBSoa0jDsrb
|
||||
github.com/gen2brain/jpegxl v0.4.5/go.mod h1:4kWYJ18xCEuO2vzocYdGpeqNJ990/Gjy3uLMg5TBN6I=
|
||||
github.com/gen2brain/webp v0.5.5 h1:MvQR75yIPU/9nSqYT5h13k4URaJK3gf9tgz/ksRbyEg=
|
||||
github.com/gen2brain/webp v0.5.5/go.mod h1:xOSMzp4aROt2KFW++9qcK/RBTOVC2S9tJG66ip/9Oc0=
|
||||
github.com/gonutz/xcf v0.0.0-20180404091035-c002b9533d97 h1:DdKf/zTPiJP9GyxSJPRW3UpsKjIXrpEQOOc+N2mXbuE=
|
||||
github.com/gonutz/xcf v0.0.0-20180404091035-c002b9533d97/go.mod h1:1YB2Y6fvddxaSsiTvGp6+WWg17jt1MKhTwJWnGD1zN4=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
|
||||
|
BIN
integration/images/image.xcf
Normal file
BIN
integration/images/image.xcf
Normal file
Binary file not shown.
8
internal/builtins/xcf.go
Normal file
8
internal/builtins/xcf.go
Normal file
@@ -0,0 +1,8 @@
|
||||
//go:build xcf || full
|
||||
// +build xcf full
|
||||
|
||||
package builtins
|
||||
|
||||
import (
|
||||
_ "github.com/coalaura/ffwebp/internal/codec/xcf"
|
||||
)
|
119
internal/codec/xcf/xcf.go
Normal file
119
internal/codec/xcf/xcf.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package xcf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"io"
|
||||
|
||||
"github.com/gonutz/xcf"
|
||||
|
||||
"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 "xcf"
|
||||
}
|
||||
|
||||
func (impl) Extensions() []string {
|
||||
return []string{"xcf"}
|
||||
}
|
||||
|
||||
func (impl) CanEncode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (impl) Flags(flags []cli.Flag) []cli.Flag {
|
||||
return flags
|
||||
}
|
||||
|
||||
func (impl) Sniff(reader io.ReaderAt) (int, []byte, error) {
|
||||
magic := []byte("gimp xcf ")
|
||||
|
||||
buf := make([]byte, len(magic))
|
||||
if _, err := reader.ReadAt(buf, 0); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if bytes.Equal(buf, magic) {
|
||||
return 100, magic, nil
|
||||
}
|
||||
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (impl) Decode(r io.Reader) (image.Image, error) {
|
||||
buf, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reader := bytes.NewReader(buf)
|
||||
|
||||
canvas, err := xcf.Decode(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dst := image.NewNRGBA(image.Rect(0, 0, int(canvas.Width), int(canvas.Height)))
|
||||
|
||||
for i := len(canvas.Layers) - 1; i >= 0; i-- {
|
||||
layer := canvas.Layers[i]
|
||||
if !layer.Visible {
|
||||
continue
|
||||
}
|
||||
|
||||
var src image.Image = layer.RGBA
|
||||
|
||||
if layer.Opacity < 255 {
|
||||
src = applyOpacity(src, layer.Opacity)
|
||||
}
|
||||
|
||||
dr := src.Bounds().Intersect(dst.Bounds())
|
||||
if dr.Empty() {
|
||||
continue
|
||||
}
|
||||
|
||||
draw.Draw(dst, dr, src, dr.Min, draw.Over)
|
||||
}
|
||||
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func (impl) Encode(w io.Writer, img image.Image, _ opts.Common) error {
|
||||
return fmt.Errorf("xcf: encoding not supported")
|
||||
}
|
||||
|
||||
func applyOpacity(img image.Image, opacity uint8) *image.NRGBA {
|
||||
bounds := img.Bounds()
|
||||
out := image.NewNRGBA(bounds)
|
||||
|
||||
opa := int(opacity)
|
||||
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
r16, g16, b16, a16 := img.At(x, y).RGBA()
|
||||
|
||||
r := uint8(r16 >> 8)
|
||||
g := uint8(g16 >> 8)
|
||||
b := uint8(b16 >> 8)
|
||||
a := int(uint8(a16 >> 8))
|
||||
|
||||
a = (a*opa + 127) / 255
|
||||
|
||||
out.Set(x, y, color.NRGBA{R: r, G: g, B: b, A: uint8(a)})
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
Reference in New Issue
Block a user