Skip to content

Commit

Permalink
media/heif: add rotations and visual dimensions accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Apr 17, 2018
1 parent a2a4794 commit 9599cf2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
24 changes: 22 additions & 2 deletions media/heif/heif.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,28 @@ func (it *Item) SpatialExtents() (width, height int, ok bool) {
return
}

// TODO: add HEIF irot rotation accessor, like Image.SpatialExtents.
// And imir (mirroring).
// Rotations returns the number of 90 degree rotations counter-clockwise that this
// image should be rendered at, in the range [0,3].
func (it *Item) Rotations() int {
for _, p := range it.Properties {
if p, ok := p.(*bmff.ImageRotation); ok {
return int(p.Angle)
}
}
return 0
}

// VisualDimensions returns the item's width and height after correcting
// for any rotations.
func (it *Item) VisualDimensions() (width, height int, ok bool) {
width, height, ok = it.SpatialExtents()
for i := 0; i < it.Rotations(); i++ {
width, height = height, width
}
return
}

// TODO: add HEIF imir (mirroring) accessor, like Image.SpatialExtents.

// Open returns a handle to access a HEIF file.
func Open(f io.ReaderAt) *File {
Expand Down
27 changes: 27 additions & 0 deletions media/heif/heif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ func TestAll(t *testing.T) {
}
}

func TestRotations(t *testing.T) {
f, err := os.Open("testdata/rotate.heic")
if err != nil {
t.Fatal(err)
}
defer f.Close()
h := Open(f)
it, err := h.PrimaryItem()
if err != nil {
t.Fatalf("PrimaryItem: %v", err)
}
if r := it.Rotations(); r != 3 {
t.Errorf("Rotations = %v; want %v", r, 3)
}
sw, sh, ok := it.SpatialExtents()
if !ok {
t.Fatalf("expected spatial extents")
}
vw, vh, ok := it.VisualDimensions()
if !ok {
t.Fatalf("expected visual dimensions")
}
if vw != sh || vh != sw {
t.Errorf("visual dimensions = %v, %v; want %v, %v", vw, vh, sh, sw)
}
}

type walkFunc func(exif.FieldName, *tiff.Tag) error

func (f walkFunc) Walk(name exif.FieldName, tag *tiff.Tag) error {
Expand Down
Binary file added media/heif/testdata/rotate.heic
Binary file not shown.

0 comments on commit 9599cf2

Please sign in to comment.