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

feat: extend bimg with support for autorotate #181

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
11 changes: 11 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ func (i *Image) Rotate(a Angle) ([]byte, error) {
return i.Process(options)
}

// AutoRotate the image to be 'up' by default (setting Orientation=1) with vips.
func (i *Image) AutoRotate() ([]byte, error) {
options := Options{AutoRotate: true}
image, err := AutoRotate(i.buffer, options)
if err != nil {
return nil, err
}
i.buffer = image
return image, nil
}

// Flip flips the image about the vertical Y axis.
func (i *Image) Flip() ([]byte, error) {
options := Options{Flip: true}
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ type Options struct {
NoAutoRotate bool
NoProfile bool
Interlace bool
AutoRotate bool
Extend Extend
Rotate Angle
Background Color
Expand Down
24 changes: 24 additions & 0 deletions resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import (
"math"
)

// AutoRotate is a directly calling vips.
func AutoRotate(buf []byte, o Options) ([]byte, error) {
defer C.vips_thread_shutdown()

image, imageType, err := loadImage(buf)
if err != nil {
return nil, err
}

// Clone and define default options
o = applyDefaults(o, imageType)

if !IsTypeSupported(o.Type) {
return nil, errors.New("Unsupported image output type")
}

image, err = vipsAutoRotate(image)
if err != nil {
return nil, err
}

return saveImage(image, o)
}

// Resize is used to transform a given image as byte buffer
// with the passed options.
func Resize(buf []byte, o Options) ([]byte, error) {
Expand Down
10 changes: 6 additions & 4 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func TestDeterminateImageType(t *testing.T) {
buf, _ := ioutil.ReadAll(img)
defer img.Close()

if DetermineImageType(buf) != file.expected {
t.Fatal("Image type is not valid")
actual := DetermineImageType(buf)
if actual != file.expected {
t.Fatalf("Image type %#v does not equal the expected %#v", actual, file.expected)
}
}
}
Expand All @@ -51,8 +52,9 @@ func TestDeterminateImageTypeName(t *testing.T) {
buf, _ := ioutil.ReadAll(img)
defer img.Close()

if DetermineImageTypeName(buf) != file.expected {
t.Fatal("Image type is not valid")
actual := DetermineImageTypeName(buf)
if actual != file.expected {
t.Fatalf("Image type %#v does not equal the expected %#v", actual, file.expected)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ func vipsRotate(image *C.VipsImage, angle Angle) (*C.VipsImage, error) {
return out, nil
}

func vipsAutoRotate(image *C.VipsImage) (*C.VipsImage, error) {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(image))

err := C.vips_autorotate(image, &out)
if err != 0 {
return nil, catchVipsError()
}

return out, nil
}

func vipsFlip(image *C.VipsImage, direction Direction) (*C.VipsImage, error) {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(image))
Expand Down
5 changes: 5 additions & 0 deletions vips.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ vips_rotate(VipsImage *in, VipsImage **out, int angle) {
}
}

int
vips_autorotate(VipsImage *in, VipsImage **out) {
return vips_autorot(in, out, NULL);
}

int
vips_exif_orientation(VipsImage *image) {
int orientation = 0;
Expand Down