Skip to content

Commit

Permalink
Merge pull request #23 from fabiomarini/master
Browse files Browse the repository at this point in the history
Added compatibility with image.Decode
  • Loading branch information
lEx0 authored Mar 2, 2023
2 parents 630f42c + b1b8eed commit 66059de
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
3 changes: 3 additions & 0 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewDecoder(r io.Reader, options *Options) (d *Decoder, err error) {
return nil, errors.New("data is empty")
}

if options == nil {
options = &Options{}
}
d = &Decoder{data: data, options: options}

if d.config, err = d.options.GetConfig(); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package decoder

import (
"github.com/kolesa-team/go-webp/utils"
"github.com/stretchr/testify/require"
"image"
"os"
"testing"
Expand Down Expand Up @@ -104,3 +105,11 @@ func TestDecoder_GetFeatures(t *testing.T) {
t.Fatal("file has_animation is invalid")
}
}

func TestDecoder_NilOptions(t *testing.T) {
file, err := os.Open("../test_data/images/m4_q75.webp")
require.NoError(t, err)

_, err = NewDecoder(file, nil)
require.NoError(t, err)
}
3 changes: 3 additions & 0 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Encoder struct {
func NewEncoder(src image.Image, options *Options) (e *Encoder, err error) {
var config *C.WebPConfig

if options == nil {
options, _ = NewLossyEncoderOptions(PresetDefault, 75)
}
if config, err = options.GetConfig(); err != nil {
return nil, err
}
Expand Down
24 changes: 21 additions & 3 deletions encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"image"
"io/ioutil"
"os"
"testing"
)

Expand All @@ -28,7 +28,7 @@ func TestNewEncoder(t *testing.T) {
err = e.Encode(expected)
require.NoError(t, err)

actual, err := ioutil.ReadFile("../test_data/images/100x150_lossy.webp")
actual, err := os.ReadFile("../test_data/images/100x150_lossy.webp")
require.NoError(t, err)

assert.Equal(t, actual, expected.Bytes())
Expand All @@ -51,9 +51,27 @@ func TestNewEncoder(t *testing.T) {
err = e.Encode(actuall)
require.NoError(t, err)

expected, err := ioutil.ReadFile("../test_data/images/100x150_lossless.webp")
expected, err := os.ReadFile("../test_data/images/100x150_lossless.webp")
require.NoError(t, err)

assert.Equal(t, expected, actuall.Bytes())
})
}

func TestNewEncoder_NilOptions(t *testing.T) {
t.Run("encode image without passing options should not panic", func(t *testing.T) {
expected := &bytes.Buffer{}
img := image.NewNRGBA(image.Rectangle{
Max: image.Point{
X: 100,
Y: 150,
},
})

e, err := NewEncoder(img, nil)
require.NoError(t, err)

err = e.Encode(expected)
require.NoError(t, err)
})
}
27 changes: 26 additions & 1 deletion webp/webp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ import (
"github.com/kolesa-team/go-webp/decoder"
"github.com/kolesa-team/go-webp/encoder"
"image"
"image/color"
"io"
)

func init() {
image.RegisterFormat("webp", "RIFF????WEBPVP8", quickDecode, quickDecodeConfig)
}

func quickDecode(r io.Reader) (image.Image, error) {
return Decode(r, &decoder.Options{})
}

func quickDecodeConfig(r io.Reader) (image.Config, error) {
return DecodeConfig(r, &decoder.Options{})
}

// Decode picture from reader
func Decode(r io.Reader, options *decoder.Options) (image.Image, error) {
if dec, err := decoder.NewDecoder(r, options); err != nil {
Expand All @@ -37,7 +50,19 @@ func Decode(r io.Reader, options *decoder.Options) (image.Image, error) {
}
}

// Encode encode picture and write to io.Writer
func DecodeConfig(r io.Reader, options *decoder.Options) (image.Config, error) {
if dec, err := decoder.NewDecoder(r, options); err != nil {
return image.Config{}, err
} else {
return image.Config{
ColorModel: color.RGBAModel,
Width: dec.GetFeatures().Width,
Height: dec.GetFeatures().Height,
}, nil
}
}

// Encode picture and write to io.Writer
func Encode(w io.Writer, src image.Image, options *encoder.Options) error {
if enc, err := encoder.NewEncoder(src, options); err != nil {
return err
Expand Down
43 changes: 31 additions & 12 deletions webp/webp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
"bytes"
"github.com/kolesa-team/go-webp/decoder"
"github.com/kolesa-team/go-webp/encoder"
"golang.org/x/image/webp"
"github.com/stretchr/testify/require"
"image"
"image/color"
"image/jpeg"
"io/ioutil"
"io"
"os"
"testing"
)
Expand All @@ -50,7 +51,7 @@ func TestEncode(t *testing.T) {
t.Fatal(err)
}

if err = Encode(ioutil.Discard, img, options); err != nil {
if err = Encode(io.Discard, img, options); err != nil {
t.Fatal(err)
}
})
Expand All @@ -70,7 +71,7 @@ func TestEncode(t *testing.T) {
t.Fatal(err)
}

if err = Encode(ioutil.Discard, img, options); err != nil {
if err = Encode(io.Discard, img, options); err != nil {
t.Fatal(err)
}
})
Expand All @@ -84,7 +85,7 @@ func TestEncode(t *testing.T) {
t.Fatal(err)
}

if err = Encode(ioutil.Discard, img, options); err == nil {
if err = Encode(io.Discard, img, options); err == nil {
t.Fatal("an error was expected")
}
})
Expand Down Expand Up @@ -143,10 +144,28 @@ func TestDecode(t *testing.T) {
t.Fatal("an error was expected")
}
})
t.Run("decode lossy image via standard image.Decode", func(t *testing.T) {
r, err := os.Open("../test_data/images/webp-logo-lossy.webp")
require.NoError(t, err)
img, format, err := image.Decode(r)
require.NoError(t, err)

require.Equal(t, "webp", format)
require.Equal(t, color.NRGBAModel, img.ColorModel())
})
t.Run("decode lossless image via standard image.Decode", func(t *testing.T) {
r, err := os.Open("../test_data/images/webp-logo-lossless.webp")
require.NoError(t, err)
img, format, err := image.Decode(r)
require.NoError(t, err)

require.Equal(t, "webp", format)
require.Equal(t, color.NRGBAModel, img.ColorModel())
})
}

func BenchmarkDecodeLossy(b *testing.B) {
data, err := ioutil.ReadFile("../test_data/images/webp-logo-lossy.webp")
data, err := os.ReadFile("../test_data/images/webp-logo-lossy.webp")
if err != nil {
b.Fatal(err)
}
Expand All @@ -164,21 +183,21 @@ func BenchmarkDecodeLossy(b *testing.B) {
}

func BenchmarkDecodeXImageLossy(b *testing.B) {
data, err := ioutil.ReadFile("../test_data/images/webp-logo-lossy.webp")
data, err := os.ReadFile("../test_data/images/webp-logo-lossy.webp")
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
_, err = webp.Decode(bytes.NewBuffer(data))
_, err = Decode(bytes.NewBuffer(data), &decoder.Options{})
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkDecodeLossless(b *testing.B) {
data, err := ioutil.ReadFile("../test_data/images/webp-logo-lossless.webp")
data, err := os.ReadFile("../test_data/images/webp-logo-lossless.webp")
if err != nil {
b.Fatal(err)
}
Expand All @@ -196,13 +215,13 @@ func BenchmarkDecodeLossless(b *testing.B) {
}

func BenchmarkDecodeXImageLossless(b *testing.B) {
data, err := ioutil.ReadFile("../test_data/images/webp-logo-lossless.webp")
data, err := os.ReadFile("../test_data/images/webp-logo-lossless.webp")
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
_, err = webp.Decode(bytes.NewBuffer(data))
_, err = Decode(bytes.NewBuffer(data), &decoder.Options{})
if err != nil {
b.Fatal(err)
}
Expand All @@ -226,7 +245,7 @@ func BenchmarkEncode(b *testing.B) {
b.Fatal(err)
}

if err = Encode(ioutil.Discard, img, options); err != nil {
if err = Encode(io.Discard, img, options); err != nil {
b.Fatal(err)
}
}
Expand Down

0 comments on commit 66059de

Please sign in to comment.