From 414fe40c919ecba841fef54971632f2ecad6457d Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Wed, 8 Apr 2015 00:44:44 +0200 Subject: [PATCH] feat(#20): support flop operation (interface broken, sorry im still beta) --- README.md | 3 ++- image.go | 4 ++-- image_test.go | 8 ++++++++ options.go | 3 ++- resize.go | 14 +++++++++++--- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f50c44e..fdde77f 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ The [install script](https://github.com/lovell/sharp/blob/master/preinstall.sh) - Crop - Rotate - Flip +- Flop - Thumbnail - Extract area - Format conversion @@ -56,7 +57,7 @@ Here you can see some performance test comparisons for multiple scenarios: - [libvips speed and memory usage](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use) - [sharp performance tests](https://github.com/lovell/sharp#the-task) -#### bimg performance tests +#### Benchmarks Tested using Go 1.4 and libvips-7.42.3 in OSX i7 2.7Ghz ``` diff --git a/image.go b/image.go index 3c59e4d..c1bea8a 100644 --- a/image.go +++ b/image.go @@ -82,13 +82,13 @@ func (i *Image) Rotate(a Angle) ([]byte, error) { // Flip the image about the vertical Y axis func (i *Image) Flip() ([]byte, error) { - options := Options{Flip: VERTICAL} + options := Options{Flip: true} return i.Process(options) } // Flop the image about the horizontal X axis func (i *Image) Flop() ([]byte, error) { - options := Options{Flip: VERTICAL} + options := Options{Flop: true} return i.Process(options) } diff --git a/image_test.go b/image_test.go index 698143e..a152180 100644 --- a/image_test.go +++ b/image_test.go @@ -112,6 +112,14 @@ func TestImageFlip(t *testing.T) { Write("fixtures/test_flip_out.jpg", buf) } +func TestImageFlop(t *testing.T) { + buf, err := initImage("test.jpg").Flop() + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + Write("fixtures/test_flop_out.jpg", buf) +} + func TestImageRotate(t *testing.T) { buf, err := initImage("test_flip_out.jpg").Rotate(90) if err != nil { diff --git a/options.go b/options.go index cc8b32c..ea1b3b5 100644 --- a/options.go +++ b/options.go @@ -68,9 +68,10 @@ type Options struct { Crop bool Enlarge bool Embed bool + Flip bool + Flop bool Type ImageType Rotate Angle - Flip Direction Gravity Gravity Interpolator Interpolator } diff --git a/resize.go b/resize.go index 77cfab9..5af81ab 100644 --- a/resize.go +++ b/resize.go @@ -160,10 +160,11 @@ func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, func rotateImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, error) { var err error + var direction Direction = -1 rotation, flip := calculateRotationAndFlip(image, o.Rotate) if flip { - o.Flip = HORIZONTAL + o.Flip = flip } if rotation > D0 && o.Rotate == 0 { o.Rotate = rotation @@ -172,8 +173,15 @@ func rotateImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, e if o.Rotate > 0 { image, err = vipsRotate(image, getAngle(o.Rotate)) } - if o.Flip > 0 { - image, err = vipsFlip(image, o.Flip) + + if o.Flip { + direction = HORIZONTAL + } else if o.Flop { + direction = VERTICAL + } + + if direction != -1 { + image, err = vipsFlip(image, direction) } return image, err