Skip to content

Commit

Permalink
Merge pull request #2 from mikegleasonjr/stdlib-registration
Browse files Browse the repository at this point in the history
Now registering heic decoder in stdlib during library import
  • Loading branch information
jdeng authored Mar 28, 2019
2 parents 31f49d3 + 72d5d4e commit 0715d98
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
38 changes: 33 additions & 5 deletions goheif.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package goheif

import (
"bytes"
"fmt"
"github.com/jdeng/goheif/heif"
"github.com/jdeng/goheif/libde265"
"image"
"image/color"
"io"
"io/ioutil"

"github.com/jdeng/goheif/heif"
"github.com/jdeng/goheif/libde265"
)

type gridBox struct {
Expand Down Expand Up @@ -75,7 +78,12 @@ func ExtractExif(ra io.ReaderAt) ([]byte, error) {
return hf.EXIF()
}

func DecodeImage(ra io.ReaderAt) (image.Image, error) {
func Decode(r io.Reader) (image.Image, error) {
ra, err := asReaderAt(r)
if err != nil {
return nil, err
}

hf := heif.Open(ra)

it, err := hf.PrimaryItem()
Expand Down Expand Up @@ -170,9 +178,14 @@ func DecodeImage(ra io.ReaderAt) (image.Image, error) {
return out, nil
}

func DecodeConfig(ra io.ReaderAt) (image.Config, error) {
func DecodeConfig(r io.Reader) (image.Config, error) {
var config image.Config

ra, err := asReaderAt(r)
if err != nil {
return config, err
}

hf := heif.Open(ra)

it, err := hf.PrimaryItem()
Expand All @@ -193,7 +206,22 @@ func DecodeConfig(ra io.ReaderAt) (image.Config, error) {
return config, nil
}

func asReaderAt(r io.Reader) (io.ReaderAt, error) {
if ra, ok := r.(io.ReaderAt); ok {
return ra, nil
}

b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

return bytes.NewReader(b), nil
}

func init() {
libde265.Init()
//image.RegisterFormat("heif", "????ftypheic", decodeImage, decodeConfig)
// they check for "ftyp" at the 5th bytes, let's do the same...
// https://github.com/strukturag/libheif/blob/master/libheif/heif.cc#L94
image.RegisterFormat("heic", "????ftyp", Decode, DecodeConfig)
}
28 changes: 28 additions & 0 deletions goheif_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package goheif

import (
"bytes"
"image"
"io/ioutil"
"testing"
)

func TestFormatRegistered(t *testing.T) {
b, err := ioutil.ReadFile("testdata/camel.heic")
if err != nil {
t.Fatal(err)
}

img, dec, err := image.Decode(bytes.NewReader(b))
if err != nil {
t.Fatalf("unable to decode heic image: %s", err)
}

if got, want := dec, "heic"; got != want {
t.Errorf("unexpected decoder: got %s, want %s", got, want)
}

if w, h := img.Bounds().Dx(), img.Bounds().Dy(); w != 1596 || h != 1064 {
t.Errorf("unexpected decoded image size: got %dx%d, want 1596x1064", w, h)
}
}
Binary file added testdata/camel.heic
Binary file not shown.

0 comments on commit 0715d98

Please sign in to comment.