2015-02-09 06:09:14 +01:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package draw
|
|
|
|
|
|
|
|
import (
|
2015-02-24 10:05:58 +01:00
|
|
|
"bytes"
|
2015-02-09 06:09:14 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
2015-02-24 10:05:58 +01:00
|
|
|
"image/color"
|
2015-02-09 06:09:14 +01:00
|
|
|
"image/png"
|
2015-02-24 10:05:58 +01:00
|
|
|
"math/rand"
|
2015-02-09 06:09:14 +01:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
"golang.org/x/image/math/f64"
|
|
|
|
|
2015-02-09 06:09:14 +01:00
|
|
|
_ "image/jpeg"
|
|
|
|
)
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
var genGoldenFiles = flag.Bool("gen_golden_files", false, "whether to generate the TestXxx golden files.")
|
2015-02-09 06:09:14 +01:00
|
|
|
|
2015-07-27 07:58:18 +02:00
|
|
|
var transformMatrix = func(scale, tx, ty float64) f64.Aff3 {
|
2015-04-10 11:58:30 +02:00
|
|
|
const cos30, sin30 = 0.866025404, 0.5
|
2015-07-27 07:58:18 +02:00
|
|
|
return f64.Aff3{
|
2015-03-17 08:46:52 +01:00
|
|
|
+scale * cos30, -scale * sin30, tx,
|
|
|
|
+scale * sin30, +scale * cos30, ty,
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func encode(filename string, m image.Image) error {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Create: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := png.Encode(f, m); err != nil {
|
|
|
|
return fmt.Errorf("Encode: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
|
|
|
|
// testInterp tests that interpolating the source image gives the exact
|
|
|
|
// destination image. This is to ensure that any refactoring or optimization of
|
|
|
|
// the interpolation code doesn't change the behavior. Changing the actual
|
|
|
|
// algorithm or kernel used by any particular quality setting will obviously
|
|
|
|
// change the resultant pixels. In such a case, use the gen_golden_files flag
|
|
|
|
// to regenerate the golden files.
|
2015-04-10 11:58:30 +02:00
|
|
|
func testInterp(t *testing.T, w int, h int, direction, prefix, suffix string) {
|
|
|
|
f, err := os.Open("../testdata/" + prefix + suffix)
|
2015-02-09 06:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
src, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Decode: %v", err)
|
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
op, scale := Src, 3.75
|
|
|
|
if prefix == "tux" {
|
|
|
|
op, scale = Over, 0.125
|
|
|
|
}
|
|
|
|
green := image.NewUniform(color.RGBA{0x00, 0x22, 0x11, 0xff})
|
|
|
|
|
2015-02-09 06:09:14 +01:00
|
|
|
testCases := map[string]Interpolator{
|
|
|
|
"nn": NearestNeighbor,
|
|
|
|
"ab": ApproxBiLinear,
|
|
|
|
"bl": BiLinear,
|
|
|
|
"cr": CatmullRom,
|
|
|
|
}
|
|
|
|
for name, q := range testCases {
|
2015-04-10 11:58:30 +02:00
|
|
|
goldenFilename := fmt.Sprintf("../testdata/%s-%s-%s.png", prefix, direction, name)
|
2015-02-09 06:09:14 +01:00
|
|
|
|
|
|
|
got := image.NewRGBA(image.Rect(0, 0, w, h))
|
2015-04-29 09:05:36 +02:00
|
|
|
Copy(got, image.Point{}, green, got.Bounds(), Src, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
if direction == "rotate" {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Transform(got, transformMatrix(scale, 40, 10), src, src.Bounds(), op, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
} else {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Scale(got, got.Bounds(), src, src.Bounds(), op, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if *genGoldenFiles {
|
2015-03-17 08:46:52 +01:00
|
|
|
if err := encode(goldenFilename, got); err != nil {
|
|
|
|
t.Error(err)
|
2015-02-09 06:09:14 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
g, err := os.Open(goldenFilename)
|
2015-02-09 06:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Open: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer g.Close()
|
2015-03-13 07:44:34 +01:00
|
|
|
wantRaw, err := png.Decode(g)
|
2015-02-09 06:09:14 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Decode: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
// convert wantRaw to RGBA.
|
|
|
|
want, ok := wantRaw.(*image.RGBA)
|
|
|
|
if !ok {
|
|
|
|
b := wantRaw.Bounds()
|
|
|
|
want = image.NewRGBA(b)
|
|
|
|
Draw(want, b, wantRaw, b.Min, Src)
|
|
|
|
}
|
2015-02-09 06:09:14 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
2015-03-13 07:44:34 +01:00
|
|
|
t.Errorf("%s: actual image differs from golden image", goldenFilename)
|
2015-02-09 06:09:14 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func TestScaleDown(t *testing.T) { testInterp(t, 100, 100, "down", "go-turns-two", "-280x360.jpeg") }
|
|
|
|
func TestScaleUp(t *testing.T) { testInterp(t, 75, 100, "up", "go-turns-two", "-14x18.png") }
|
|
|
|
func TestTformSrc(t *testing.T) { testInterp(t, 100, 100, "rotate", "go-turns-two", "-14x18.png") }
|
|
|
|
func TestTformOver(t *testing.T) { testInterp(t, 100, 100, "rotate", "tux", ".png") }
|
|
|
|
|
|
|
|
func TestOps(t *testing.T) {
|
|
|
|
blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
|
|
|
|
testCases := map[Op]color.RGBA{
|
|
|
|
Over: color.RGBA{0x7f, 0x00, 0x80, 0xff},
|
|
|
|
Src: color.RGBA{0x7f, 0x00, 0x00, 0x7f},
|
|
|
|
}
|
|
|
|
for op, want := range testCases {
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
2015-04-29 09:05:36 +02:00
|
|
|
Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
src := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
src.SetRGBA(0, 0, color.RGBA{0x7f, 0x00, 0x00, 0x7f})
|
|
|
|
|
2015-04-29 09:05:36 +02:00
|
|
|
NearestNeighbor.Scale(dst, dst.Bounds(), src, src.Bounds(), op, nil)
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
if got := dst.RGBAAt(0, 0); got != want {
|
|
|
|
t.Errorf("op=%v: got %v, want %v", op, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 06:09:14 +01:00
|
|
|
|
2015-04-10 05:05:20 +02:00
|
|
|
// TestNegativeWeights tests that scaling by a kernel that produces negative
|
|
|
|
// weights, such as the Catmull-Rom kernel, doesn't produce an invalid color
|
|
|
|
// according to Go's alpha-premultiplied model.
|
|
|
|
func TestNegativeWeights(t *testing.T) {
|
|
|
|
check := func(m *image.RGBA) error {
|
|
|
|
b := m.Bounds()
|
|
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
|
|
if c := m.RGBAAt(x, y); c.R > c.A || c.G > c.A || c.B > c.A {
|
|
|
|
return fmt.Errorf("invalid color.RGBA at (%d, %d): %v", x, y, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
src := image.NewRGBA(image.Rect(0, 0, 16, 16))
|
|
|
|
for y := 0; y < 16; y++ {
|
|
|
|
for x := 0; x < 16; x++ {
|
|
|
|
a := y * 0x11
|
|
|
|
src.Set(x, y, color.RGBA{
|
|
|
|
R: uint8(x * 0x11 * a / 0xff),
|
|
|
|
A: uint8(a),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := check(src); err != nil {
|
|
|
|
t.Fatalf("src image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 32, 32))
|
2015-04-29 09:05:36 +02:00
|
|
|
CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), Over, nil)
|
2015-04-10 05:05:20 +02:00
|
|
|
if err := check(dst); err != nil {
|
|
|
|
t.Fatalf("dst image: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 06:20:04 +01:00
|
|
|
func fillPix(r *rand.Rand, pixs ...[]byte) {
|
|
|
|
for _, pix := range pixs {
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = uint8(r.Intn(256))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
func TestInterpClipCommute(t *testing.T) {
|
2015-02-26 06:20:04 +01:00
|
|
|
src := image.NewNRGBA(image.Rect(0, 0, 20, 20))
|
|
|
|
fillPix(rand.New(rand.NewSource(0)), src.Pix)
|
|
|
|
|
|
|
|
outer := image.Rect(1, 1, 8, 5)
|
|
|
|
inner := image.Rect(2, 3, 6, 5)
|
|
|
|
qs := []Interpolator{
|
|
|
|
NearestNeighbor,
|
|
|
|
ApproxBiLinear,
|
|
|
|
CatmullRom,
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
for _, transform := range []bool{false, true} {
|
|
|
|
for _, q := range qs {
|
|
|
|
dst0 := image.NewRGBA(image.Rect(1, 1, 10, 10))
|
|
|
|
dst1 := image.NewRGBA(image.Rect(1, 1, 10, 10))
|
|
|
|
for i := range dst0.Pix {
|
|
|
|
dst0.Pix[i] = uint8(i / 4)
|
|
|
|
dst1.Pix[i] = uint8(i / 4)
|
|
|
|
}
|
2015-02-26 06:20:04 +01:00
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
var interp func(dst *image.RGBA)
|
|
|
|
if transform {
|
|
|
|
interp = func(dst *image.RGBA) {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Transform(dst, transformMatrix(3.75, 2, 1), src, src.Bounds(), Over, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
interp = func(dst *image.RGBA) {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Scale(dst, outer, src, src.Bounds(), Over, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 06:20:04 +01:00
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
// Interpolate then clip.
|
|
|
|
interp(dst0)
|
|
|
|
dst0 = dst0.SubImage(inner).(*image.RGBA)
|
2015-02-26 06:20:04 +01:00
|
|
|
|
2015-03-13 07:44:34 +01:00
|
|
|
// Clip then interpolate.
|
|
|
|
dst1 = dst1.SubImage(inner).(*image.RGBA)
|
|
|
|
interp(dst1)
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for y := inner.Min.Y; y < inner.Max.Y; y++ {
|
|
|
|
for x := inner.Min.X; x < inner.Max.X; x++ {
|
|
|
|
if c0, c1 := dst0.RGBAAt(x, y), dst1.RGBAAt(x, y); c0 != c1 {
|
|
|
|
t.Errorf("q=%T: at (%d, %d): c0=%v, c1=%v", q, x, y, c0, c1)
|
|
|
|
break loop
|
|
|
|
}
|
2015-02-26 06:20:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 07:50:07 +01:00
|
|
|
// translatedImage is an image m translated by t.
|
|
|
|
type translatedImage struct {
|
|
|
|
m image.Image
|
|
|
|
t image.Point
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *translatedImage) At(x, y int) color.Color { return t.m.At(x-t.t.X, y-t.t.Y) }
|
|
|
|
func (t *translatedImage) Bounds() image.Rectangle { return t.m.Bounds().Add(t.t) }
|
|
|
|
func (t *translatedImage) ColorModel() color.Model { return t.m.ColorModel() }
|
|
|
|
|
|
|
|
// TestSrcTranslationInvariance tests that Scale and Transform are invariant
|
|
|
|
// under src translations. Specifically, when some source pixels are not in the
|
|
|
|
// bottom-right quadrant of src coordinate space, we consistently round down,
|
|
|
|
// not round towards zero.
|
|
|
|
func TestSrcTranslationInvariance(t *testing.T) {
|
|
|
|
f, err := os.Open("../testdata/testpattern.png")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
src, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Decode: %v", err)
|
|
|
|
}
|
2015-03-18 04:50:27 +01:00
|
|
|
sr := image.Rect(2, 3, 16, 12)
|
|
|
|
if !sr.In(src.Bounds()) {
|
|
|
|
t.Fatalf("src bounds too small: got %v", src.Bounds())
|
|
|
|
}
|
2015-03-12 07:50:07 +01:00
|
|
|
qs := []Interpolator{
|
|
|
|
NearestNeighbor,
|
|
|
|
ApproxBiLinear,
|
|
|
|
CatmullRom,
|
|
|
|
}
|
|
|
|
deltas := []image.Point{
|
|
|
|
{+0, +0},
|
|
|
|
{+0, +5},
|
|
|
|
{+0, -5},
|
|
|
|
{+5, +0},
|
|
|
|
{-5, +0},
|
|
|
|
{+8, +8},
|
|
|
|
{+8, -8},
|
|
|
|
{-8, +8},
|
|
|
|
{-8, -8},
|
|
|
|
}
|
2015-04-10 11:58:30 +02:00
|
|
|
m00 := transformMatrix(3.75, 0, 0)
|
2015-03-12 07:50:07 +01:00
|
|
|
|
2015-03-17 08:46:52 +01:00
|
|
|
for _, transform := range []bool{false, true} {
|
|
|
|
for _, q := range qs {
|
2015-03-18 04:50:27 +01:00
|
|
|
want := image.NewRGBA(image.Rect(0, 0, 20, 20))
|
2015-03-17 08:46:52 +01:00
|
|
|
if transform {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Transform(want, m00, src, sr, Over, nil)
|
2015-03-17 08:46:52 +01:00
|
|
|
} else {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Scale(want, want.Bounds(), src, sr, Over, nil)
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
|
|
|
for _, delta := range deltas {
|
|
|
|
tsrc := &translatedImage{src, delta}
|
2015-03-18 04:50:27 +01:00
|
|
|
got := image.NewRGBA(image.Rect(0, 0, 20, 20))
|
2015-03-17 08:46:52 +01:00
|
|
|
if transform {
|
2015-07-27 07:58:18 +02:00
|
|
|
m := matMul(&m00, &f64.Aff3{
|
2015-03-17 08:46:52 +01:00
|
|
|
1, 0, -float64(delta.X),
|
|
|
|
0, 1, -float64(delta.Y),
|
|
|
|
})
|
2015-07-27 07:58:18 +02:00
|
|
|
q.Transform(got, m, tsrc, sr.Add(delta), Over, nil)
|
2015-03-17 08:46:52 +01:00
|
|
|
} else {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Scale(got, got.Bounds(), tsrc, sr.Add(delta), Over, nil)
|
2015-03-17 08:46:52 +01:00
|
|
|
}
|
|
|
|
if !bytes.Equal(got.Pix, want.Pix) {
|
|
|
|
t.Errorf("pix differ for delta=%v, transform=%t, q=%T", delta, transform, q)
|
|
|
|
}
|
2015-03-12 07:50:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 06:45:24 +02:00
|
|
|
func TestSrcMask(t *testing.T) {
|
|
|
|
srcMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
|
|
|
|
srcMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
|
|
|
|
srcMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
|
|
|
|
srcMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
|
|
|
|
srcMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
|
|
|
|
red := image.NewUniform(color.RGBA{0xff, 0x00, 0x00, 0xff})
|
|
|
|
blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 6, 1))
|
2015-04-29 09:05:36 +02:00
|
|
|
Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
|
|
|
|
NearestNeighbor.Scale(dst, dst.Bounds(), red, image.Rect(0, 0, 3, 1), Over, &Options{
|
2015-04-24 06:45:24 +02:00
|
|
|
SrcMask: srcMask,
|
|
|
|
SrcMaskP: image.Point{20, 0},
|
|
|
|
})
|
|
|
|
got := [6]color.RGBA{
|
|
|
|
dst.RGBAAt(0, 0),
|
|
|
|
dst.RGBAAt(1, 0),
|
|
|
|
dst.RGBAAt(2, 0),
|
|
|
|
dst.RGBAAt(3, 0),
|
|
|
|
dst.RGBAAt(4, 0),
|
|
|
|
dst.RGBAAt(5, 0),
|
|
|
|
}
|
|
|
|
want := [6]color.RGBA{
|
|
|
|
{0xff, 0x00, 0x00, 0xff},
|
|
|
|
{0xff, 0x00, 0x00, 0xff},
|
|
|
|
{0x3f, 0x00, 0xc0, 0xff},
|
|
|
|
{0x3f, 0x00, 0xc0, 0xff},
|
|
|
|
{0x00, 0x00, 0xff, 0xff},
|
|
|
|
{0x00, 0x00, 0xff, 0xff},
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("\ngot %v\nwant %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 06:26:44 +02:00
|
|
|
func TestDstMask(t *testing.T) {
|
|
|
|
dstMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
|
|
|
|
dstMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
|
|
|
|
dstMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
|
|
|
|
dstMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
|
|
|
|
dstMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
|
|
|
|
red := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
red.SetRGBA(0, 0, color.RGBA{0xff, 0x00, 0x00, 0xff})
|
|
|
|
blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
|
|
|
|
qs := []Interpolator{
|
|
|
|
NearestNeighbor,
|
|
|
|
ApproxBiLinear,
|
|
|
|
CatmullRom,
|
|
|
|
}
|
|
|
|
for _, q := range qs {
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 3, 1))
|
2015-04-29 09:05:36 +02:00
|
|
|
Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
|
|
|
|
q.Scale(dst, dst.Bounds(), red, red.Bounds(), Over, &Options{
|
2015-04-21 06:26:44 +02:00
|
|
|
DstMask: dstMask,
|
|
|
|
DstMaskP: image.Point{20, 0},
|
|
|
|
})
|
|
|
|
got := [3]color.RGBA{
|
|
|
|
dst.RGBAAt(0, 0),
|
|
|
|
dst.RGBAAt(1, 0),
|
|
|
|
dst.RGBAAt(2, 0),
|
|
|
|
}
|
|
|
|
want := [3]color.RGBA{
|
|
|
|
{0xff, 0x00, 0x00, 0xff},
|
|
|
|
{0x3f, 0x00, 0xc0, 0xff},
|
|
|
|
{0x00, 0x00, 0xff, 0xff},
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("q=%T:\ngot %v\nwant %v", q, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRectDstMask(t *testing.T) {
|
|
|
|
f, err := os.Open("../testdata/testpattern.png")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
src, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Decode: %v", err)
|
|
|
|
}
|
|
|
|
m00 := transformMatrix(1, 0, 0)
|
|
|
|
|
|
|
|
bounds := image.Rect(0, 0, 50, 50)
|
|
|
|
dstOutside := image.NewRGBA(bounds)
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
|
|
dstOutside.SetRGBA(x, y, color.RGBA{uint8(5 * x), uint8(5 * y), 0x00, 0xff})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:24:07 +02:00
|
|
|
mk := func(q Transformer, dstMask image.Image, dstMaskP image.Point) *image.RGBA {
|
2015-04-21 06:26:44 +02:00
|
|
|
m := image.NewRGBA(bounds)
|
2015-04-29 09:05:36 +02:00
|
|
|
Copy(m, bounds.Min, dstOutside, bounds, Src, nil)
|
|
|
|
q.Transform(m, m00, src, src.Bounds(), Over, &Options{
|
2015-04-22 07:24:07 +02:00
|
|
|
DstMask: dstMask,
|
|
|
|
DstMaskP: dstMaskP,
|
|
|
|
})
|
2015-04-21 06:26:44 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
qs := []Interpolator{
|
|
|
|
NearestNeighbor,
|
|
|
|
ApproxBiLinear,
|
|
|
|
CatmullRom,
|
|
|
|
}
|
2015-04-22 07:24:07 +02:00
|
|
|
dstMaskPs := []image.Point{
|
|
|
|
{0, 0},
|
|
|
|
{5, 7},
|
|
|
|
{-3, 0},
|
|
|
|
}
|
|
|
|
rect := image.Rect(10, 10, 30, 40)
|
2015-04-21 06:26:44 +02:00
|
|
|
for _, q := range qs {
|
2015-04-22 07:24:07 +02:00
|
|
|
for _, dstMaskP := range dstMaskPs {
|
|
|
|
dstInside := mk(q, nil, image.Point{})
|
|
|
|
for _, wrap := range []bool{false, true} {
|
2015-04-29 02:46:24 +02:00
|
|
|
// TODO: replace "rectImage(rect)" with "rect" once Go 1.5 is
|
|
|
|
// released, where an image.Rectangle implements image.Image.
|
|
|
|
dstMask := image.Image(rectImage(rect))
|
2015-04-22 07:24:07 +02:00
|
|
|
if wrap {
|
|
|
|
dstMask = srcWrapper{dstMask}
|
2015-04-21 06:26:44 +02:00
|
|
|
}
|
2015-04-22 07:24:07 +02:00
|
|
|
dst := mk(q, dstMask, dstMaskP)
|
|
|
|
|
|
|
|
nError := 0
|
|
|
|
loop:
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
|
|
which := dstOutside
|
|
|
|
if (image.Point{x, y}).Add(dstMaskP).In(rect) {
|
|
|
|
which = dstInside
|
|
|
|
}
|
|
|
|
if got, want := dst.RGBAAt(x, y), which.RGBAAt(x, y); got != want {
|
|
|
|
if nError == 10 {
|
|
|
|
t.Errorf("q=%T dmp=%v wrap=%v: ...and more errors", q, dstMaskP, wrap)
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
nError++
|
|
|
|
t.Errorf("q=%T dmp=%v wrap=%v: x=%3d y=%3d: got %v, want %v",
|
|
|
|
q, dstMaskP, wrap, x, y, got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 06:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:46:24 +02:00
|
|
|
// TODO: delete this wrapper type once Go 1.5 is released, where an
|
|
|
|
// image.Rectangle implements image.Image.
|
|
|
|
type rectImage image.Rectangle
|
|
|
|
|
|
|
|
func (r rectImage) ColorModel() color.Model { return color.Alpha16Model }
|
|
|
|
func (r rectImage) Bounds() image.Rectangle { return image.Rectangle(r) }
|
|
|
|
func (r rectImage) At(x, y int) color.Color {
|
|
|
|
if (image.Point{x, y}).In(image.Rectangle(r)) {
|
|
|
|
return color.Opaque
|
|
|
|
}
|
|
|
|
return color.Transparent
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:05:58 +01:00
|
|
|
// The fooWrapper types wrap the dst or src image to avoid triggering the
|
|
|
|
// type-specific fast path implementations.
|
|
|
|
type (
|
|
|
|
dstWrapper struct{ Image }
|
|
|
|
srcWrapper struct{ image.Image }
|
|
|
|
)
|
2015-02-24 05:28:16 +01:00
|
|
|
|
2015-02-28 06:05:58 +01:00
|
|
|
func srcGray(boundsHint image.Rectangle) (image.Image, error) {
|
|
|
|
m := image.NewGray(boundsHint)
|
|
|
|
fillPix(rand.New(rand.NewSource(0)), m.Pix)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:05:58 +01:00
|
|
|
func srcNRGBA(boundsHint image.Rectangle) (image.Image, error) {
|
|
|
|
m := image.NewNRGBA(boundsHint)
|
2015-02-26 06:20:04 +01:00
|
|
|
fillPix(rand.New(rand.NewSource(1)), m.Pix)
|
2015-02-24 10:05:58 +01:00
|
|
|
return m, nil
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 10:05:58 +01:00
|
|
|
func srcRGBA(boundsHint image.Rectangle) (image.Image, error) {
|
|
|
|
m := image.NewRGBA(boundsHint)
|
2015-02-26 06:20:04 +01:00
|
|
|
fillPix(rand.New(rand.NewSource(2)), m.Pix)
|
2015-02-24 10:05:58 +01:00
|
|
|
// RGBA is alpha-premultiplied, so the R, G and B values should
|
|
|
|
// be <= the A values.
|
|
|
|
for i := 0; i < len(m.Pix); i += 4 {
|
|
|
|
m.Pix[i+0] = uint8(uint32(m.Pix[i+0]) * uint32(m.Pix[i+3]) / 0xff)
|
|
|
|
m.Pix[i+1] = uint8(uint32(m.Pix[i+1]) * uint32(m.Pix[i+3]) / 0xff)
|
|
|
|
m.Pix[i+2] = uint8(uint32(m.Pix[i+2]) * uint32(m.Pix[i+3]) / 0xff)
|
|
|
|
}
|
|
|
|
return m, nil
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func srcUnif(boundsHint image.Rectangle) (image.Image, error) {
|
2015-02-24 10:05:58 +01:00
|
|
|
return image.NewUniform(color.RGBA64{0x1234, 0x5555, 0x9181, 0xbeef}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func srcYCbCr(boundsHint image.Rectangle) (image.Image, error) {
|
|
|
|
m := image.NewYCbCr(boundsHint, image.YCbCrSubsampleRatio420)
|
2015-02-26 06:20:04 +01:00
|
|
|
fillPix(rand.New(rand.NewSource(3)), m.Y, m.Cb, m.Cr)
|
2015-02-24 10:05:58 +01:00
|
|
|
return m, nil
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func srcLarge(boundsHint image.Rectangle) (image.Image, error) {
|
2015-02-24 05:28:16 +01:00
|
|
|
// 3072 x 2304 is over 7 million pixels at 4:3, comparable to a
|
|
|
|
// 2015 smart-phone camera's output.
|
2015-02-24 10:05:58 +01:00
|
|
|
return srcYCbCr(image.Rect(0, 0, 3072, 2304))
|
2015-02-24 05:28:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 10:05:58 +01:00
|
|
|
func srcTux(boundsHint image.Rectangle) (image.Image, error) {
|
2015-02-24 05:28:16 +01:00
|
|
|
// tux.png is a 386 x 395 image.
|
|
|
|
f, err := os.Open("../testdata/tux.png")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Open: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
src, err := png.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Decode: %v", err)
|
2015-02-09 06:09:14 +01:00
|
|
|
}
|
2015-02-24 05:28:16 +01:00
|
|
|
return src, nil
|
|
|
|
}
|
2015-02-09 06:09:14 +01:00
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func benchScale(b *testing.B, w int, h int, op Op, srcf func(image.Rectangle) (image.Image, error), q Interpolator) {
|
2015-02-09 06:09:14 +01:00
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, w, h))
|
2015-02-24 10:05:58 +01:00
|
|
|
src, err := srcf(image.Rect(0, 0, 1024, 768))
|
2015-02-24 05:28:16 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2015-02-09 06:09:14 +01:00
|
|
|
dr, sr := dst.Bounds(), src.Bounds()
|
2015-03-03 06:54:53 +01:00
|
|
|
scaler := Scaler(q)
|
|
|
|
if n, ok := q.(interface {
|
|
|
|
NewScaler(int, int, int, int) Scaler
|
|
|
|
}); ok {
|
|
|
|
scaler = n.NewScaler(dr.Dx(), dr.Dy(), sr.Dx(), sr.Dy())
|
|
|
|
}
|
2015-02-09 06:09:14 +01:00
|
|
|
|
2015-03-27 01:21:16 +01:00
|
|
|
b.ReportAllocs()
|
2015-02-09 06:09:14 +01:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-04-29 09:05:36 +02:00
|
|
|
scaler.Scale(dst, dr, src, sr, op, nil)
|
2015-02-09 06:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func benchTform(b *testing.B, w int, h int, op Op, srcf func(image.Rectangle) (image.Image, error), q Interpolator) {
|
2015-03-13 07:44:34 +01:00
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
|
|
src, err := srcf(image.Rect(0, 0, 1024, 768))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
sr := src.Bounds()
|
2015-04-10 11:58:30 +02:00
|
|
|
m := transformMatrix(3.75, 40, 10)
|
2015-03-13 07:44:34 +01:00
|
|
|
|
2015-03-27 01:21:16 +01:00
|
|
|
b.ReportAllocs()
|
2015-03-13 07:44:34 +01:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-04-29 09:05:36 +02:00
|
|
|
q.Transform(dst, m, src, sr, op, nil)
|
2015-03-13 07:44:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func BenchmarkScaleNNLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, NearestNeighbor) }
|
|
|
|
func BenchmarkScaleABLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleBLLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, BiLinear) }
|
|
|
|
func BenchmarkScaleCRLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, CatmullRom) }
|
|
|
|
|
|
|
|
func BenchmarkScaleNNDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, NearestNeighbor) }
|
|
|
|
func BenchmarkScaleABDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleBLDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, BiLinear) }
|
|
|
|
func BenchmarkScaleCRDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, CatmullRom) }
|
|
|
|
|
|
|
|
func BenchmarkScaleNNUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, NearestNeighbor) }
|
|
|
|
func BenchmarkScaleABUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleBLUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, BiLinear) }
|
|
|
|
func BenchmarkScaleCRUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, CatmullRom) }
|
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func BenchmarkScaleNNSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, NearestNeighbor) }
|
|
|
|
func BenchmarkScaleNNSrcUnif(b *testing.B) { benchScale(b, 200, 150, Src, srcUnif, NearestNeighbor) }
|
|
|
|
|
|
|
|
func BenchmarkScaleNNOverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, NearestNeighbor) }
|
|
|
|
func BenchmarkScaleNNOverUnif(b *testing.B) { benchScale(b, 200, 150, Over, srcUnif, NearestNeighbor) }
|
2015-04-07 03:22:25 +02:00
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func BenchmarkTformNNSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, NearestNeighbor) }
|
|
|
|
func BenchmarkTformNNSrcUnif(b *testing.B) { benchTform(b, 200, 150, Src, srcUnif, NearestNeighbor) }
|
|
|
|
|
|
|
|
func BenchmarkTformNNOverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, NearestNeighbor) }
|
|
|
|
func BenchmarkTformNNOverUnif(b *testing.B) { benchTform(b, 200, 150, Over, srcUnif, NearestNeighbor) }
|
2015-04-07 03:22:25 +02:00
|
|
|
|
|
|
|
func BenchmarkScaleABSrcGray(b *testing.B) { benchScale(b, 200, 150, Src, srcGray, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABSrcNRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcNRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABSrcYCbCr(b *testing.B) { benchScale(b, 200, 150, Src, srcYCbCr, ApproxBiLinear) }
|
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func BenchmarkScaleABOverGray(b *testing.B) { benchScale(b, 200, 150, Over, srcGray, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABOverNRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcNRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABOverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkScaleABOverYCbCr(b *testing.B) { benchScale(b, 200, 150, Over, srcYCbCr, ApproxBiLinear) }
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func BenchmarkTformABSrcGray(b *testing.B) { benchTform(b, 200, 150, Src, srcGray, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABSrcNRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcNRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABSrcYCbCr(b *testing.B) { benchTform(b, 200, 150, Src, srcYCbCr, ApproxBiLinear) }
|
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func BenchmarkTformABOverGray(b *testing.B) { benchTform(b, 200, 150, Over, srcGray, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABOverNRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcNRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABOverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, ApproxBiLinear) }
|
|
|
|
func BenchmarkTformABOverYCbCr(b *testing.B) { benchTform(b, 200, 150, Over, srcYCbCr, ApproxBiLinear) }
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func BenchmarkScaleCRSrcGray(b *testing.B) { benchScale(b, 200, 150, Src, srcGray, CatmullRom) }
|
|
|
|
func BenchmarkScaleCRSrcNRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcNRGBA, CatmullRom) }
|
|
|
|
func BenchmarkScaleCRSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, CatmullRom) }
|
|
|
|
func BenchmarkScaleCRSrcYCbCr(b *testing.B) { benchScale(b, 200, 150, Src, srcYCbCr, CatmullRom) }
|
|
|
|
|
2015-04-10 11:58:30 +02:00
|
|
|
func BenchmarkScaleCROverGray(b *testing.B) { benchScale(b, 200, 150, Over, srcGray, CatmullRom) }
|
|
|
|
func BenchmarkScaleCROverNRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcNRGBA, CatmullRom) }
|
|
|
|
func BenchmarkScaleCROverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, CatmullRom) }
|
|
|
|
func BenchmarkScaleCROverYCbCr(b *testing.B) { benchScale(b, 200, 150, Over, srcYCbCr, CatmullRom) }
|
|
|
|
|
2015-04-07 03:22:25 +02:00
|
|
|
func BenchmarkTformCRSrcGray(b *testing.B) { benchTform(b, 200, 150, Src, srcGray, CatmullRom) }
|
|
|
|
func BenchmarkTformCRSrcNRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcNRGBA, CatmullRom) }
|
|
|
|
func BenchmarkTformCRSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, CatmullRom) }
|
|
|
|
func BenchmarkTformCRSrcYCbCr(b *testing.B) { benchTform(b, 200, 150, Src, srcYCbCr, CatmullRom) }
|
2015-04-10 11:58:30 +02:00
|
|
|
|
|
|
|
func BenchmarkTformCROverGray(b *testing.B) { benchTform(b, 200, 150, Over, srcGray, CatmullRom) }
|
|
|
|
func BenchmarkTformCROverNRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcNRGBA, CatmullRom) }
|
|
|
|
func BenchmarkTformCROverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, CatmullRom) }
|
|
|
|
func BenchmarkTformCROverYCbCr(b *testing.B) { benchTform(b, 200, 150, Over, srcYCbCr, CatmullRom) }
|