Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds compatibility with the std image lib #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions webp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package webp

// Importing this package allows decoding of webp image files using the standard library's image.Decode.
// It will clash with the golang.org/x/image/webp package

import (
"image"
"io"

"github.com/kolesa-team/go-webp/webp"
)

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

func quickDecode(r io.Reader) (image.Image, error) {
return webp.Decode(r, nil)
}

func quickDecodeConfig(r io.Reader) (image.Config, error) {
return webp.DecodeConfig(r, nil)
}
21 changes: 19 additions & 2 deletions webp/webp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
package webp

import (
"github.com/kolesa-team/go-webp/decoder"
"github.com/kolesa-team/go-webp/encoder"
"image"
"image/color"
"io"

"github.com/kolesa-team/go-webp/decoder"
"github.com/kolesa-team/go-webp/encoder"
)

// Decode picture from reader
Expand All @@ -37,6 +39,21 @@ func Decode(r io.Reader, options *decoder.Options) (image.Image, error) {
}
}

// DecodeConfig extracts simple metadata without decoding the image
func DecodeConfig(r io.Reader, options *decoder.Options) (image.Config, error) {
dec, err := decoder.NewDecoder(r, options)
if err != nil {
return image.Config{}, err
}

feat := dec.GetFeatures()
return image.Config{
ColorModel: color.NRGBAModel,
Width: feat.Width,
Height: feat.Height,
}, nil
}

// Encode 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 {
Expand Down