From 4f4cd5e63573da4d6edcc7d4213afaca67c19f88 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Mon, 23 Nov 2015 15:53:48 -0800 Subject: upgrading libs --- .../github.com/disintegration/imaging/README.md | 38 +- .../disintegration/imaging/adjust_test.go | 504 +++++++++++++++++ .../disintegration/imaging/effects_test.go | 128 +++++ .../github.com/disintegration/imaging/helpers.go | 24 +- .../disintegration/imaging/helpers_test.go | 361 ++++++++++++ .../github.com/disintegration/imaging/resize.go | 43 +- .../disintegration/imaging/resize_test.go | 455 ++++++++++++++++ .../disintegration/imaging/tools_test.go | 605 +++++++++++++++++++++ .../disintegration/imaging/transform_test.go | 261 +++++++++ .../disintegration/imaging/utils_test.go | 61 +++ 10 files changed, 2458 insertions(+), 22 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/adjust_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/effects_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/helpers_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/resize_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/tools_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/transform_test.go create mode 100644 Godeps/_workspace/src/github.com/disintegration/imaging/utils_test.go (limited to 'Godeps/_workspace/src/github.com/disintegration/imaging') diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/README.md b/Godeps/_workspace/src/github.com/disintegration/imaging/README.md index 16ac8cf6c..25d836cef 100644 --- a/Godeps/_workspace/src/github.com/disintegration/imaging/README.md +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/README.md @@ -32,8 +32,8 @@ dstImage800 := imaging.Resize(srcImage, 800, 0, imaging.Lanczos) // scale down srcImage to fit the 800x600px bounding box dstImageFit := imaging.Fit(srcImage, 800, 600, imaging.Lanczos) -// resize and crop the srcImage to make a 100x100px thumbnail -dstImageThumb := imaging.Thumbnail(srcImage, 100, 100, imaging.Lanczos) +// resize and crop the srcImage to fill the 100x100px area +dstImageFill := imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos) ``` Imaging supports image resizing using various resampling filters. The most notable ones: @@ -63,6 +63,40 @@ Filter | Resize result `imaging.Gaussian` | ![dstImage](http://disintegration.github.io/imaging/out_resize_down_gaussian.png) `imaging.Lanczos` | ![dstImage](http://disintegration.github.io/imaging/out_resize_down_lanczos.png) +**Resize functions comparison** + +Original image: + +![srcImage](http://disintegration.github.io/imaging/in.jpg) + +Resize the image to width=100px and height=100px: + +```go +dstImage := imaging.Resize(srcImage, 100, 100, imaging.Lanczos) +``` +![dstImage](http://disintegration.github.io/imaging/out-comp-resize.jpg) + +Resize the image to width=100px preserving the aspect ratio: + +```go +dstImage := imaging.Resize(srcImage, 100, 0, imaging.Lanczos) +``` +![dstImage](http://disintegration.github.io/imaging/out-comp-fit.jpg) + +Resize the image to fit the 100x100px boundng box preserving the aspect ratio: + +```go +dstImage := imaging.Fit(srcImage, 100, 100, imaging.Lanczos) +``` +![dstImage](http://disintegration.github.io/imaging/out-comp-fit.jpg) + +Resize and crop the image with a center anchor point to fill the 100x100px area: + +```go +dstImage := imaging.Fill(srcImage, 100, 100, imaging.Center, imaging.Lanczos) +``` +![dstImage](http://disintegration.github.io/imaging/out-comp-fill.jpg) + ### Gaussian Blur ```go dstImage := imaging.Blur(srcImage, 0.5) diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/adjust_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/adjust_test.go new file mode 100644 index 000000000..99898b0dc --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/adjust_test.go @@ -0,0 +1,504 @@ +package imaging + +import ( + "image" + "testing" +) + +func TestGrayscale(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Grayscale 3x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x3d, 0x3d, 0x3d, 0x01, 0x78, 0x78, 0x78, 0x02, 0x17, 0x17, 0x17, 0x03, + 0x1f, 0x1f, 0x1f, 0xff, 0x25, 0x25, 0x25, 0xff, 0x66, 0x66, 0x66, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Grayscale(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestInvert(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Invert 3x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x33, 0xff, 0xff, 0x01, 0xff, 0x33, 0xff, 0x02, 0xff, 0xff, 0x33, 0x03, + 0xee, 0xdd, 0xcc, 0xff, 0xcc, 0xdd, 0xee, 0xff, 0x55, 0xcc, 0x44, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0xff, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Invert(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestAdjustContrast(t *testing.T) { + td := []struct { + desc string + src image.Image + p float64 + want *image.NRGBA + }{ + { + "AdjustContrast 3x3 10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 10, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xd5, 0x00, 0x00, 0x01, 0x00, 0xd5, 0x00, 0x02, 0x00, 0x00, 0xd5, 0x03, + 0x05, 0x18, 0x2b, 0xff, 0x2b, 0x18, 0x05, 0xff, 0xaf, 0x2b, 0xc2, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x2b, 0x2b, 0x2b, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustContrast 3x3 100", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 100, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0x01, 0x00, 0xff, 0x00, 0x02, 0x00, 0x00, 0xff, 0x03, + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustContrast 3x3 -10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + -10, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xc4, 0x0d, 0x0d, 0x01, 0x0d, 0xc4, 0x0d, 0x02, 0x0d, 0x0d, 0xc4, 0x03, + 0x1c, 0x2b, 0x3b, 0xff, 0x3b, 0x2b, 0x1c, 0xff, 0xa6, 0x3b, 0xb5, 0xff, + 0x0d, 0x0d, 0x0d, 0xff, 0x3b, 0x3b, 0x3b, 0xff, 0xf2, 0xf2, 0xf2, 0xff, + }, + }, + }, + { + "AdjustContrast 3x3 -100", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + -100, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x02, 0x80, 0x80, 0x80, 0x03, + 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, + 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, + }, + }, + }, + { + "AdjustContrast 3x3 0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := AdjustContrast(d.src, d.p) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestAdjustBrightness(t *testing.T) { + td := []struct { + desc string + src image.Image + p float64 + want *image.NRGBA + }{ + { + "AdjustBrightness 3x3 10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 10, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xe6, 0x1a, 0x1a, 0x01, 0x1a, 0xe6, 0x1a, 0x02, 0x1a, 0x1a, 0xe6, 0x03, + 0x2b, 0x3c, 0x4d, 0xff, 0x4d, 0x3c, 0x2b, 0xff, 0xc4, 0x4d, 0xd5, 0xff, + 0x1a, 0x1a, 0x1a, 0xff, 0x4d, 0x4d, 0x4d, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustBrightness 3x3 100", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 100, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x02, 0xff, 0xff, 0xff, 0x03, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustBrightness 3x3 -10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + -10, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xb3, 0x00, 0x00, 0x01, 0x00, 0xb3, 0x00, 0x02, 0x00, 0x00, 0xb3, 0x03, + 0x00, 0x09, 0x1a, 0xff, 0x1a, 0x09, 0x00, 0xff, 0x91, 0x1a, 0xa2, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x1a, 0x1a, 0x1a, 0xff, 0xe6, 0xe6, 0xe6, 0xff, + }, + }, + }, + { + "AdjustBrightness 3x3 -100", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + -100, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + { + "AdjustBrightness 3x3 0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := AdjustBrightness(d.src, d.p) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestAdjustGamma(t *testing.T) { + td := []struct { + desc string + src image.Image + p float64 + want *image.NRGBA + }{ + { + "AdjustGamma 3x3 0.75", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0.75, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xbd, 0x00, 0x00, 0x01, 0x00, 0xbd, 0x00, 0x02, 0x00, 0x00, 0xbd, 0x03, + 0x07, 0x11, 0x1e, 0xff, 0x1e, 0x11, 0x07, 0xff, 0x95, 0x1e, 0xa9, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x1e, 0x1e, 0x1e, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustGamma 3x3 1.5", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 1.5, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xdc, 0x00, 0x00, 0x01, 0x00, 0xdc, 0x00, 0x02, 0x00, 0x00, 0xdc, 0x03, + 0x2a, 0x43, 0x57, 0xff, 0x57, 0x43, 0x2a, 0xff, 0xc3, 0x57, 0xcf, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x57, 0x57, 0x57, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustGamma 3x3 1.0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 1.0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := AdjustGamma(d.src, d.p) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestAdjustSigmoid(t *testing.T) { + td := []struct { + desc string + src image.Image + m float64 + p float64 + want *image.NRGBA + }{ + { + "AdjustSigmoid 3x3 0.5 3.0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0.5, + 3.0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xd4, 0x00, 0x00, 0x01, 0x00, 0xd4, 0x00, 0x02, 0x00, 0x00, 0xd4, 0x03, + 0x0d, 0x1b, 0x2b, 0xff, 0x2b, 0x1b, 0x0d, 0xff, 0xb1, 0x2b, 0xc3, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x2b, 0x2b, 0x2b, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustSigmoid 3x3 0.5 -3.0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0.5, + -3.0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xc4, 0x00, 0x00, 0x01, 0x00, 0xc4, 0x00, 0x02, 0x00, 0x00, 0xc4, 0x03, + 0x16, 0x2a, 0x3b, 0xff, 0x3b, 0x2a, 0x16, 0xff, 0xa4, 0x3b, 0xb3, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x3b, 0x3b, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + { + "AdjustSigmoid 3x3 0.5 0.0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 0.5, + 0.0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03, + 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff, + 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := AdjustSigmoid(d.src, d.m, d.p) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/effects_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/effects_test.go new file mode 100644 index 000000000..b7de0974c --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/effects_test.go @@ -0,0 +1,128 @@ +package imaging + +import ( + "image" + "testing" +) + +func TestBlur(t *testing.T) { + td := []struct { + desc string + src image.Image + sigma float64 + want *image.NRGBA + }{ + { + "Blur 3x3 0.5", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }, + }, + 0.5, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x01, 0x02, 0x04, 0x04, 0x0a, 0x10, 0x18, 0x18, 0x01, 0x02, 0x04, 0x04, + 0x09, 0x10, 0x18, 0x18, 0x3f, 0x69, 0x9e, 0x9e, 0x09, 0x10, 0x18, 0x18, + 0x01, 0x02, 0x04, 0x04, 0x0a, 0x10, 0x18, 0x18, 0x01, 0x02, 0x04, 0x04, + }, + }, + }, + { + + "Blur 3x3 10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }, + }, + 10, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, + 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, + 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, 0x0b, 0x13, 0x1c, 0x1c, + }, + }, + }, + } + for _, d := range td { + got := Blur(d.src, d.sigma) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestSharpen(t *testing.T) { + td := []struct { + desc string + src image.Image + sigma float64 + want *image.NRGBA + }{ + { + "Sharpen 3x3 0.5", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, + 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66, + 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, + }, + }, + 0.5, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66, + 0x64, 0x64, 0x64, 0x64, 0x7e, 0x7e, 0x7e, 0x7e, 0x64, 0x64, 0x64, 0x64, + 0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66}, + }, + }, + { + + "Sharpen 3x3 10", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, + 0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66, + 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}, + }, + 100, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 3), + Stride: 3 * 4, + Pix: []uint8{ + 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, + 0x64, 0x64, 0x64, 0x64, 0x86, 0x86, 0x86, 0x86, 0x64, 0x64, 0x64, 0x64, + 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, + }, + }, + }, + } + for _, d := range td { + got := Sharpen(d.src, d.sigma) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/helpers.go b/Godeps/_workspace/src/github.com/disintegration/imaging/helpers.go index 983b64d71..247962f2d 100644 --- a/Godeps/_workspace/src/github.com/disintegration/imaging/helpers.go +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/helpers.go @@ -20,8 +20,8 @@ import ( "path/filepath" "strings" - "golang.org/x/image/bmp" - "golang.org/x/image/tiff" + "github.com/mattermost/platform/Godeps/_workspace/src/golang.org/x/image/bmp" + "github.com/mattermost/platform/Godeps/_workspace/src/golang.org/x/image/tiff" ) type Format int @@ -226,9 +226,13 @@ func Clone(img image.Image) *image.NRGBA { dst.Pix[di+1] = src.Pix[si+1] dst.Pix[di+2] = src.Pix[si+2] default: - dst.Pix[di+0] = uint8(uint16(src.Pix[si+0]) * 0xff / uint16(a)) - dst.Pix[di+1] = uint8(uint16(src.Pix[si+1]) * 0xff / uint16(a)) - dst.Pix[di+2] = uint8(uint16(src.Pix[si+2]) * 0xff / uint16(a)) + var tmp uint16 + tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a) + dst.Pix[di+0] = uint8(tmp) + tmp = uint16(src.Pix[si+1]) * 0xff / uint16(a) + dst.Pix[di+1] = uint8(tmp) + tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a) + dst.Pix[di+2] = uint8(tmp) } di += 4 @@ -257,9 +261,13 @@ func Clone(img image.Image) *image.NRGBA { dst.Pix[di+1] = src.Pix[si+2] dst.Pix[di+2] = src.Pix[si+4] default: - dst.Pix[di+0] = uint8(uint16(src.Pix[si+0]) * 0xff / uint16(a)) - dst.Pix[di+1] = uint8(uint16(src.Pix[si+2]) * 0xff / uint16(a)) - dst.Pix[di+2] = uint8(uint16(src.Pix[si+4]) * 0xff / uint16(a)) + var tmp uint16 + tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a) + dst.Pix[di+0] = uint8(tmp) + tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a) + dst.Pix[di+1] = uint8(tmp) + tmp = uint16(src.Pix[si+4]) * 0xff / uint16(a) + dst.Pix[di+2] = uint8(tmp) } di += 4 diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/helpers_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/helpers_test.go new file mode 100644 index 000000000..2d611a008 --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/helpers_test.go @@ -0,0 +1,361 @@ +package imaging + +import ( + "bytes" + "image" + "image/color" + "testing" +) + +func compareNRGBA(img1, img2 *image.NRGBA, delta int) bool { + if !img1.Rect.Eq(img2.Rect) { + return false + } + + if len(img1.Pix) != len(img2.Pix) { + return false + } + + for i := 0; i < len(img1.Pix); i++ { + if absint(int(img1.Pix[i])-int(img2.Pix[i])) > delta { + return false + } + } + + return true +} + +func TestEncodeDecode(t *testing.T) { + imgWithAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3)) + imgWithAlpha.Pix = []uint8{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, + 244, 245, 246, 247, 248, 249, 250, 252, 252, 253, 254, 255, + } + + imgWithoutAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3)) + imgWithoutAlpha.Pix = []uint8{ + 0, 1, 2, 255, 4, 5, 6, 255, 8, 9, 10, 255, + 127, 128, 129, 255, 131, 132, 133, 255, 135, 136, 137, 255, + 244, 245, 246, 255, 248, 249, 250, 255, 252, 253, 254, 255, + } + + for _, format := range []Format{JPEG, PNG, GIF, BMP, TIFF} { + img := imgWithoutAlpha + if format == PNG { + img = imgWithAlpha + } + + buf := &bytes.Buffer{} + err := Encode(buf, img, format) + if err != nil { + t.Errorf("fail encoding format %s", format) + continue + } + + img2, err := Decode(buf) + if err != nil { + t.Errorf("fail decoding format %s", format) + continue + } + img2cloned := Clone(img2) + + delta := 0 + if format == JPEG { + delta = 3 + } else if format == GIF { + delta = 16 + } + + if !compareNRGBA(img, img2cloned, delta) { + t.Errorf("test [DecodeEncode %s] failed: %#v %#v", format, img, img2cloned) + continue + } + } + + buf := &bytes.Buffer{} + err := Encode(buf, imgWithAlpha, Format(100)) + if err != ErrUnsupportedFormat { + t.Errorf("expected ErrUnsupportedFormat") + } +} + +func TestNew(t *testing.T) { + td := []struct { + desc string + w, h int + c color.Color + dstBounds image.Rectangle + dstPix []uint8 + }{ + { + "New 1x1 black", + 1, 1, + color.NRGBA{0, 0, 0, 0}, + image.Rect(0, 0, 1, 1), + []uint8{0x00, 0x00, 0x00, 0x00}, + }, + { + "New 1x2 red", + 1, 2, + color.NRGBA{255, 0, 0, 255}, + image.Rect(0, 0, 1, 2), + []uint8{0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff}, + }, + { + "New 2x1 white", + 2, 1, + color.NRGBA{255, 255, 255, 255}, + image.Rect(0, 0, 2, 1), + []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, + }, + } + + for _, d := range td { + got := New(d.w, d.h, d.c) + want := image.NewNRGBA(d.dstBounds) + want.Pix = d.dstPix + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestClone(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Clone NRGBA", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + }, + { + "Clone NRGBA64", + &image.NRGBA64{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 8, + Pix: []uint8{ + 0x00, 0x00, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33, + 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0xff, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + }, + { + "Clone RGBA", + &image.RGBA{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + }, + { + "Clone RGBA64", + &image.RGBA64{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 8, + Pix: []uint8{ + 0x00, 0x00, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33, + 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0xff, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff}, + }, + }, + { + "Clone Gray", + &image.Gray{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 1, + Pix: []uint8{0x11, 0xee}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x11, 0x11, 0x11, 0xff, 0xee, 0xee, 0xee, 0xff}, + }, + }, + { + "Clone Gray16", + &image.Gray16{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 2, + Pix: []uint8{0x11, 0x11, 0xee, 0xee}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0x11, 0x11, 0x11, 0xff, 0xee, 0xee, 0xee, 0xff}, + }, + }, + { + "Clone Alpha", + &image.Alpha{ + Rect: image.Rect(-1, -1, 0, 1), + Stride: 1 * 1, + Pix: []uint8{0x11, 0xee}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 2), + Stride: 1 * 4, + Pix: []uint8{0xff, 0xff, 0xff, 0x11, 0xff, 0xff, 0xff, 0xee}, + }, + }, + { + "Clone YCbCr", + &image.YCbCr{ + Rect: image.Rect(-1, -1, 5, 0), + SubsampleRatio: image.YCbCrSubsampleRatio444, + YStride: 6, + CStride: 6, + Y: []uint8{0x00, 0xff, 0x7f, 0x26, 0x4b, 0x0e}, + Cb: []uint8{0x80, 0x80, 0x80, 0x6b, 0x56, 0xc0}, + Cr: []uint8{0x80, 0x80, 0x80, 0xc0, 0x4b, 0x76}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 6, 1), + Stride: 6 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0x7f, 0x7f, 0x7f, 0xff, + 0x7f, 0x00, 0x00, 0xff, + 0x00, 0x7f, 0x00, 0xff, + 0x00, 0x00, 0x7f, 0xff, + }, + }, + }, + { + "Clone YCbCr 444", + &image.YCbCr{ + Y: []uint8{0x4c, 0x69, 0x1d, 0xb1, 0x96, 0xe2, 0x26, 0x34, 0xe, 0x59, 0x4b, 0x71, 0x0, 0x4c, 0x99, 0xff}, + Cb: []uint8{0x55, 0xd4, 0xff, 0x8e, 0x2c, 0x01, 0x6b, 0xaa, 0xc0, 0x95, 0x56, 0x40, 0x80, 0x80, 0x80, 0x80}, + Cr: []uint8{0xff, 0xeb, 0x6b, 0x36, 0x15, 0x95, 0xc0, 0xb5, 0x76, 0x41, 0x4b, 0x8c, 0x80, 0x80, 0x80, 0x80}, + YStride: 4, + CStride: 4, + SubsampleRatio: image.YCbCrSubsampleRatio444, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + &image.NRGBA{ + Pix: []uint8{0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x49, 0xe1, 0xca, 0xff, 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, 0x0, 0xff, 0x7f, 0x0, 0x0, 0xff, 0x7f, 0x0, 0x7f, 0xff, 0x0, 0x0, 0x7f, 0xff, 0x0, 0x7f, 0x7f, 0xff, 0x0, 0x7f, 0x0, 0xff, 0x82, 0x7f, 0x0, 0xff, 0x0, 0x0, 0x0, 0xff, 0x4c, 0x4c, 0x4c, 0xff, 0x99, 0x99, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff}, + Stride: 16, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + }, + { + "Clone YCbCr 440", + &image.YCbCr{ + Y: []uint8{0x4c, 0x69, 0x1d, 0xb1, 0x96, 0xe2, 0x26, 0x34, 0xe, 0x59, 0x4b, 0x71, 0x0, 0x4c, 0x99, 0xff}, + Cb: []uint8{0x2c, 0x01, 0x6b, 0xaa, 0x80, 0x80, 0x80, 0x80}, + Cr: []uint8{0x15, 0x95, 0xc0, 0xb5, 0x80, 0x80, 0x80, 0x80}, + YStride: 4, + CStride: 4, + SubsampleRatio: image.YCbCrSubsampleRatio440, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + &image.NRGBA{ + Pix: []uint8{0x0, 0xb5, 0x0, 0xff, 0x86, 0x86, 0x0, 0xff, 0x77, 0x0, 0x0, 0xff, 0xfb, 0x7d, 0xfb, 0xff, 0x0, 0xff, 0x1, 0xff, 0xff, 0xff, 0x1, 0xff, 0x80, 0x0, 0x1, 0xff, 0x7e, 0x0, 0x7e, 0xff, 0xe, 0xe, 0xe, 0xff, 0x59, 0x59, 0x59, 0xff, 0x4b, 0x4b, 0x4b, 0xff, 0x71, 0x71, 0x71, 0xff, 0x0, 0x0, 0x0, 0xff, 0x4c, 0x4c, 0x4c, 0xff, 0x99, 0x99, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff}, + Stride: 16, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + }, + { + "Clone YCbCr 422", + &image.YCbCr{ + Y: []uint8{0x4c, 0x69, 0x1d, 0xb1, 0x96, 0xe2, 0x26, 0x34, 0xe, 0x59, 0x4b, 0x71, 0x0, 0x4c, 0x99, 0xff}, + Cb: []uint8{0xd4, 0x8e, 0x01, 0xaa, 0x95, 0x40, 0x80, 0x80}, + Cr: []uint8{0xeb, 0x36, 0x95, 0xb5, 0x41, 0x8c, 0x80, 0x80}, + YStride: 4, + CStride: 2, + SubsampleRatio: image.YCbCrSubsampleRatio422, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + &image.NRGBA{ + Pix: []uint8{0xe2, 0x0, 0xe1, 0xff, 0xff, 0x0, 0xfe, 0xff, 0x0, 0x4d, 0x36, 0xff, 0x49, 0xe1, 0xca, 0xff, 0xb3, 0xb3, 0x0, 0xff, 0xff, 0xff, 0x1, 0xff, 0x70, 0x0, 0x70, 0xff, 0x7e, 0x0, 0x7e, 0xff, 0x0, 0x34, 0x33, 0xff, 0x1, 0x7f, 0x7e, 0xff, 0x5c, 0x58, 0x0, 0xff, 0x82, 0x7e, 0x0, 0xff, 0x0, 0x0, 0x0, 0xff, 0x4c, 0x4c, 0x4c, 0xff, 0x99, 0x99, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff}, + Stride: 16, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + }, + { + "Clone YCbCr 420", + &image.YCbCr{ + Y: []uint8{0x4c, 0x69, 0x1d, 0xb1, 0x96, 0xe2, 0x26, 0x34, 0xe, 0x59, 0x4b, 0x71, 0x0, 0x4c, 0x99, 0xff}, + Cb: []uint8{0x01, 0xaa, 0x80, 0x80}, + Cr: []uint8{0x95, 0xb5, 0x80, 0x80}, + YStride: 4, CStride: 2, + SubsampleRatio: image.YCbCrSubsampleRatio420, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + &image.NRGBA{ + Pix: []uint8{0x69, 0x69, 0x0, 0xff, 0x86, 0x86, 0x0, 0xff, 0x67, 0x0, 0x67, 0xff, 0xfb, 0x7d, 0xfb, 0xff, 0xb3, 0xb3, 0x0, 0xff, 0xff, 0xff, 0x1, 0xff, 0x70, 0x0, 0x70, 0xff, 0x7e, 0x0, 0x7e, 0xff, 0xe, 0xe, 0xe, 0xff, 0x59, 0x59, 0x59, 0xff, 0x4b, 0x4b, 0x4b, 0xff, 0x71, 0x71, 0x71, 0xff, 0x0, 0x0, 0x0, 0xff, 0x4c, 0x4c, 0x4c, 0xff, 0x99, 0x99, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff}, + Stride: 16, + Rect: image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: 4, Y: 4}}, + }, + }, + { + "Clone Paletted", + &image.Paletted{ + Rect: image.Rect(-1, -1, 5, 0), + Stride: 6 * 1, + Palette: color.Palette{ + color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}, + color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}, + color.NRGBA{R: 0x7f, G: 0x7f, B: 0x7f, A: 0xff}, + color.NRGBA{R: 0x7f, G: 0x00, B: 0x00, A: 0xff}, + color.NRGBA{R: 0x00, G: 0x7f, B: 0x00, A: 0xff}, + color.NRGBA{R: 0x00, G: 0x00, B: 0x7f, A: 0xff}, + }, + Pix: []uint8{0x0, 0x1, 0x2, 0x3, 0x4, 0x5}, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 6, 1), + Stride: 6 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0x7f, 0x7f, 0x7f, 0xff, + 0x7f, 0x00, 0x00, 0xff, + 0x00, 0x7f, 0x00, 0xff, + 0x00, 0x00, 0x7f, 0xff, + }, + }, + }, + } + + for _, d := range td { + got := Clone(d.src) + want := d.want + + delta := 0 + if _, ok := d.src.(*image.YCbCr); ok { + delta = 1 + } + + if !compareNRGBA(got, want, delta) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/resize.go b/Godeps/_workspace/src/github.com/disintegration/imaging/resize.go index d2efd5c83..3c792e904 100644 --- a/Godeps/_workspace/src/github.com/disintegration/imaging/resize.go +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/resize.go @@ -267,20 +267,21 @@ func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA return Resize(img, newW, newH, filter) } -// Thumbnail scales the image up or down using the specified resample filter, crops it -// to the specified width and hight and returns the transformed image. +// Fill scales the image to the smallest possible size that will cover the specified dimensions, +// crops the resized image to the specified dimensions using the given anchor point and returns +// the transformed image. // // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine. // // Usage example: // -// dstImage := imaging.Thumbnail(srcImage, 100, 100, imaging.Lanczos) +// dstImage := imaging.Fill(srcImage, 800, 600, imaging.Center, imaging.Lanczos) // -func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA { - thumbW, thumbH := width, height +func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilter) *image.NRGBA { + minW, minH := width, height - if thumbW <= 0 || thumbH <= 0 { + if minW <= 0 || minH <= 0 { return &image.NRGBA{} } @@ -292,17 +293,35 @@ func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image return &image.NRGBA{} } + if srcW == minW && srcH == minH { + return Clone(img) + } + srcAspectRatio := float64(srcW) / float64(srcH) - thumbAspectRatio := float64(thumbW) / float64(thumbH) + minAspectRatio := float64(minW) / float64(minH) - var tmp image.Image - if srcAspectRatio > thumbAspectRatio { - tmp = Resize(img, 0, thumbH, filter) + var tmp *image.NRGBA + if srcAspectRatio < minAspectRatio { + tmp = Resize(img, minW, 0, filter) } else { - tmp = Resize(img, thumbW, 0, filter) + tmp = Resize(img, 0, minH, filter) } - return CropCenter(tmp, thumbW, thumbH) + return CropAnchor(tmp, minW, minH, anchor) +} + +// Thumbnail scales the image up or down using the specified resample filter, crops it +// to the specified width and hight and returns the transformed image. +// +// Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, +// CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine. +// +// Usage example: +// +// dstImage := imaging.Thumbnail(srcImage, 100, 100, imaging.Lanczos) +// +func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA { + return Fill(img, width, height, Center, filter) } // Resample filter struct. It can be used to make custom filters. diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/resize_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/resize_test.go new file mode 100644 index 000000000..bc575dcbf --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/resize_test.go @@ -0,0 +1,455 @@ +package imaging + +import ( + "image" + "testing" +) + +func TestResize(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + f ResampleFilter + want *image.NRGBA + }{ + { + "Resize 2x2 1x1 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 1, 1, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x40, 0x40, 0x40, 0xc0}, + }, + }, + { + "Resize 2x2 2x2 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 2, 2, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + }, + { + "Resize 3x1 1x1 nearest", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 2, 0), + Stride: 3 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 1, 1, + NearestNeighbor, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x00, 0xff, 0x00, 0xff}, + }, + }, + { + "Resize 2x2 0x4 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 0, 4, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 4, 4), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + }, + { + "Resize 2x2 4x0 linear", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 4, 0, + Linear, + &image.NRGBA{ + Rect: image.Rect(0, 0, 4, 4), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0xbf, 0x00, 0x00, 0xbf, 0xff, 0x00, 0x00, 0xff, + 0x00, 0x40, 0x00, 0x40, 0x30, 0x30, 0x10, 0x70, 0x8f, 0x10, 0x30, 0xcf, 0xbf, 0x00, 0x40, 0xff, + 0x00, 0xbf, 0x00, 0xbf, 0x10, 0x8f, 0x30, 0xcf, 0x30, 0x30, 0x8f, 0xef, 0x40, 0x00, 0xbf, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0xbf, 0x40, 0xff, 0x00, 0x40, 0xbf, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Resize(d.src, d.w, d.h, d.f) + want := d.want + if !compareNRGBA(got, want, 1) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestFit(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + f ResampleFilter + want *image.NRGBA + }{ + { + "Fit 2x2 1x10 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 1, 10, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x40, 0x40, 0x40, 0xc0}, + }, + }, + { + "Fit 2x2 10x1 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 10, 1, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x40, 0x40, 0x40, 0xc0}, + }, + }, + { + "Fit 2x2 10x10 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + 10, 10, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Fit(d.src, d.w, d.h, d.f) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestFill(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + a Anchor + f ResampleFilter + want *image.NRGBA + }{ + { + "Fill 4x4 2x2 Center Nearest", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Center, + NearestNeighbor, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d, 0x1e, 0x1f, + 0x34, 0x35, 0x36, 0x37, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "Fill 4x4 1x4 TopLeft Nearest", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 1, 4, + TopLeft, + NearestNeighbor, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 4), + Stride: 1 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, + 0x10, 0x11, 0x12, 0x13, + 0x20, 0x21, 0x22, 0x23, + 0x30, 0x31, 0x32, 0x33, + }, + }, + }, + { + "Fill 4x4 8x2 Bottom Nearest", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 8, 2, + Bottom, + NearestNeighbor, + &image.NRGBA{ + Rect: image.Rect(0, 0, 8, 2), + Stride: 8 * 4, + Pix: []uint8{ + 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f, + 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "Fill 4x4 2x8 Top Nearest", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 8, + Top, + NearestNeighbor, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 8), + Stride: 2 * 4, + Pix: []uint8{ + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, + }, + }, + }, + { + "Fill 4x4 4x4 TopRight Box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 4, 4, + TopRight, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 4, 4), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "Fill 4x4 0x4 Left Box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 0, 4, + Left, + Box, + &image.NRGBA{}, + }, + { + "Fill 0x0 4x4 Right Box", + &image.NRGBA{}, + 4, 4, + Right, + Box, + &image.NRGBA{}, + }, + } + for _, d := range td { + got := Fill(d.src, d.w, d.h, d.a, d.f) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestThumbnail(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + f ResampleFilter + want *image.NRGBA + }{ + { + "Thumbnail 6x2 1x1 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 5, 1), + Stride: 6 * 4, + Pix: []uint8{ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 1, 1, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x40, 0x40, 0x40, 0xc0}, + }, + }, + { + "Thumbnail 2x6 1x1 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 5), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, + 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + }, + }, + 1, 1, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 1), + Stride: 1 * 4, + Pix: []uint8{0x40, 0x40, 0x40, 0xc0}, + }, + }, + { + "Thumbnail 1x3 2x2 box", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 0, 2), + Stride: 1 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0x00, + 0xff, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, + }, + }, + 2, 2, + Box, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, + 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Thumbnail(d.src, d.w, d.h, d.f) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/tools_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/tools_test.go new file mode 100644 index 000000000..2dace6299 --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/tools_test.go @@ -0,0 +1,605 @@ +package imaging + +import ( + "image" + "testing" +) + +func TestCrop(t *testing.T) { + td := []struct { + desc string + src image.Image + r image.Rectangle + want *image.NRGBA + }{ + { + "Crop 2x3 2x1", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + image.Rect(-1, 0, 1, 1), + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + }, + }, + }, + } + for _, d := range td { + got := Crop(d.src, d.r) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestCropCenter(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + want *image.NRGBA + }{ + { + "CropCenter 2x3 2x1", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + 2, 1, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + }, + }, + }, + { + "CropCenter 2x3 0x1", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + 0, 1, + &image.NRGBA{ + Rect: image.Rect(0, 0, 0, 0), + Stride: 0, + Pix: []uint8{}, + }, + }, + { + "CropCenter 2x3 5x5", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + 5, 5, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := CropCenter(d.src, d.w, d.h) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestCropAnchor(t *testing.T) { + td := []struct { + desc string + src image.Image + w, h int + anchor Anchor + want *image.NRGBA + }{ + { + "CropAnchor 4x4 2x2 TopLeft", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + TopLeft, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + }, + }, + }, + { + "CropAnchor 4x4 2x2 Top", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Top, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + }, + }, + }, + { + "CropAnchor 4x4 2x2 TopRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + TopRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + }, + }, + }, + { + "CropAnchor 4x4 2x2 Left", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Left, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + }, + }, + }, + { + "CropAnchor 4x4 2x2 Center", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Center, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + }, + }, + }, + { + "CropAnchor 4x4 2x2 Right", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Right, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + }, + }, + }, + { + "CropAnchor 4x4 2x2 BottomLeft", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + BottomLeft, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + }, + }, + }, + { + "CropAnchor 4x4 2x2 Bottom", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + Bottom, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, + }, + }, + }, + { + "CropAnchor 4x4 2x2 BottomRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 2, 2, + BottomRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "CropAnchor 4x4 0x0 BottomRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 0, 0, + BottomRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 0, 0), + Stride: 0, + Pix: []uint8{}, + }, + }, + { + "CropAnchor 4x4 100x100 BottomRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 100, 100, + BottomRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 4, 4), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "CropAnchor 4x4 1x100 BottomRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 1, 100, + BottomRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 1, 4), + Stride: 1 * 4, + Pix: []uint8{ + 0x0c, 0x0d, 0x0e, 0x0f, + 0x1c, 0x1d, 0x1e, 0x1f, + 0x2c, 0x2d, 0x2e, 0x2f, + 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + }, + { + "CropAnchor 4x4 0x100 BottomRight", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 3, 3), + Stride: 4 * 4, + Pix: []uint8{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + }, + }, + 0, 100, + BottomRight, + &image.NRGBA{ + Rect: image.Rect(0, 0, 0, 0), + Stride: 0, + Pix: []uint8{}, + }, + }, + } + for _, d := range td { + got := CropAnchor(d.src, d.w, d.h, d.anchor) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestPaste(t *testing.T) { + td := []struct { + desc string + src1 image.Image + src2 image.Image + p image.Point + want *image.NRGBA + }{ + { + "Paste 2x3 2x1", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(1, 1, 3, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + }, + }, + image.Pt(-1, 0), + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Paste(d.src1, d.src2, d.p) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestPasteCenter(t *testing.T) { + td := []struct { + desc string + src1 image.Image + src2 image.Image + want *image.NRGBA + }{ + { + "PasteCenter 2x3 2x1", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(1, 1, 3, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := PasteCenter(d.src1, d.src2) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestOverlay(t *testing.T) { + td := []struct { + desc string + src1 image.Image + src2 image.Image + p image.Point + a float64 + want *image.NRGBA + }{ + { + "Overlay 2x3 2x1 1.0", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0x60, 0x00, 0x90, 0xff, 0xff, 0x00, 0x99, 0x7f, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(1, 1, 3, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x20, 0x40, 0x80, 0x7f, 0xaa, 0xbb, 0xcc, 0xff, + }, + }, + image.Pt(-1, 0), + 1.0, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0x40, 0x1f, 0x88, 0xff, 0xaa, 0xbb, 0xcc, 0xff, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + { + "Overlay 2x2 2x2 0.5", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, + 0x00, 0x00, 0xff, 0xff, 0x20, 0x20, 0x20, 0x00, + }, + }, + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 1), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0x00, 0xff, 0x20, 0x20, 0x20, 0xff, + }, + }, + image.Pt(-1, -1), + 0.5, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0xff, 0x7f, 0x7f, 0xff, 0x00, 0xff, 0x00, 0xff, + 0x7f, 0x7f, 0x7f, 0xff, 0x20, 0x20, 0x20, 0x7f, + }, + }, + }, + } + for _, d := range td { + got := Overlay(d.src1, d.src2, d.p, d.a) + want := d.want + if !compareNRGBA(got, want, 1) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/transform_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/transform_test.go new file mode 100644 index 000000000..6e64082f4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/transform_test.go @@ -0,0 +1,261 @@ +package imaging + +import ( + "image" + "testing" +) + +func TestRotate90(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Rotate90 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, + 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, + }, + }, + }, + } + for _, d := range td { + got := Rotate90(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestRotate180(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Rotate180 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, + 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, + }, + }, + }, + } + for _, d := range td { + got := Rotate180(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestRotate270(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Rotate270 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, + 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Rotate270(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestFlipV(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "FlipV 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + }, + }, + }, + } + for _, d := range td { + got := FlipV(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestFlipH(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "FlipH 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 2, 3), + Stride: 2 * 4, + Pix: []uint8{ + 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, + 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, + }, + }, + }, + } + for _, d := range td { + got := FlipH(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestTranspose(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Transpose 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, + 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + }, + } + for _, d := range td { + got := Transpose(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} + +func TestTransverse(t *testing.T) { + td := []struct { + desc string + src image.Image + want *image.NRGBA + }{ + { + "Transverse 2x3", + &image.NRGBA{ + Rect: image.Rect(-1, -1, 1, 2), + Stride: 2 * 4, + Pix: []uint8{ + 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + }, + &image.NRGBA{ + Rect: image.Rect(0, 0, 3, 2), + Stride: 3 * 4, + Pix: []uint8{ + 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff, + 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, + }, + }, + }, + } + for _, d := range td { + got := Transverse(d.src) + want := d.want + if !compareNRGBA(got, want, 0) { + t.Errorf("test [%s] failed: %#v", d.desc, got) + } + } +} diff --git a/Godeps/_workspace/src/github.com/disintegration/imaging/utils_test.go b/Godeps/_workspace/src/github.com/disintegration/imaging/utils_test.go new file mode 100644 index 000000000..c2384586b --- /dev/null +++ b/Godeps/_workspace/src/github.com/disintegration/imaging/utils_test.go @@ -0,0 +1,61 @@ +package imaging + +import ( + "runtime" + "testing" +) + +func testParallelN(enabled bool, n, procs int) bool { + data := make([]bool, n) + before := runtime.GOMAXPROCS(0) + runtime.GOMAXPROCS(procs) + parallel(n, func(start, end int) { + for i := start; i < end; i++ { + data[i] = true + } + }) + for i := 0; i < n; i++ { + if data[i] != true { + return false + } + } + runtime.GOMAXPROCS(before) + return true +} + +func TestParallel(t *testing.T) { + for _, e := range []bool{true, false} { + for _, n := range []int{1, 10, 100, 1000} { + for _, p := range []int{1, 2, 4, 8, 16, 100} { + if testParallelN(e, n, p) != true { + t.Errorf("test [parallel %v %d %d] failed", e, n, p) + } + } + } + } +} + +func TestClamp(t *testing.T) { + td := []struct { + f float64 + u uint8 + }{ + {0, 0}, + {255, 255}, + {128, 128}, + {0.49, 0}, + {0.50, 1}, + {254.9, 255}, + {254.0, 254}, + {256, 255}, + {2500, 255}, + {-10, 0}, + {127.6, 128}, + } + + for _, d := range td { + if clamp(d.f) != d.u { + t.Errorf("test [clamp %v %v] failed: %v", d.f, d.u, clamp(d.f)) + } + } +} -- cgit v1.2.3-1-g7c22