Skip to content

Commit

Permalink
auto rotation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
idanya authored and Elad Laufer committed Dec 16, 2019
1 parent 4268fb3 commit 9eb1d4e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/rotated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 43 additions & 7 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,50 @@ func (r *ImageRef) Linear1(a, b float64) error {
return nil
}

// Autorot executes the 'autorot' operation
func (r *ImageRef) AutoRotate() error {
out, err := vipsAutoRotate(r.image)
if err != nil {
return err
func getZeroedAngle(angle Angle) Angle {
switch angle {
case Angle0:
return Angle0
case Angle90:
return Angle270
case Angle180:
return Angle180
case Angle270:
return Angle90
}
r.setImage(out)
return nil
return Angle0
}

func GetRotationAngleFromExif(orientation int) (Angle, bool) {

switch orientation {
case 0, 1, 2:
return Angle0, orientation == 2
case 3, 4:
return Angle180, orientation == 4
case 5, 8:
return Angle90, orientation == 5
case 6, 7:
return Angle270, orientation == 7
}

return Angle0, false
}

// Autorot do auto rotation
func (r *ImageRef) AutoRotate() error {
// this is a full implementation of auto rotate as vips doesn't support auto rotating of mirrors exifs
// https://jcupitt.github.io/libvips/API/current/libvips-conversion.html#vips-autorot
angle, flipped := GetRotationAngleFromExif(r.GetOrientation())
if flipped {
err := r.Flip(DirectionHorizontal)
if err != nil {
return err
}
}

zeroAngle := getZeroedAngle(angle)
return r.Rotate(zeroAngle)
}

// ExtractArea executes the 'extract_area' operation
Expand Down
13 changes: 13 additions & 0 deletions vips/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,19 @@ func (t *Transform) Apply(image *ImageRef) (*ImageRef, error) {
return newBlackboard(image, t.transformParams).execute()
}

func (t *Transform) MatchOrientation(orientation int) *Transform {
angle, flipped := GetRotationAngleFromExif(orientation)
t.Rotate(angle)

if flipped {
t.Flip(FlipHorizontal)
}

t.SetOverwriteOrientation(orientation)

return t
}

// Return the formatted buffer of the transformed image, and its metadata
func (t *Transform) ApplyAndExport(image *ImageRef) ([]byte, *ImageMetadata, error) {
i, err := t.Apply(image)
Expand Down
12 changes: 12 additions & 0 deletions vips/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ func TestTransform_AutoRotate(t *testing.T) {
})
}

func TestTransform_AutoRotateReal(t *testing.T) {
goldenTest(t, resources+"rotate.jpg", func(tx *Transform) {
tx.AutoRotate()
})
}

func TestTransform_AutoRotateRealFlipped(t *testing.T) {
goldenTest(t, resources+"rotated.jpg", func(tx *Transform) {
tx.AutoRotate()
})
}

func TestTransform_Scale3x(t *testing.T) {
goldenTest(t, resources+"tomatoes.png", func(tx *Transform) {
tx.Scale(3.0)
Expand Down

0 comments on commit 9eb1d4e

Please sign in to comment.