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

auto rotation that supports all exif values #17

Merged
merged 1 commit into from
Dec 16, 2019
Merged
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
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