summaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/code.google.com/p/graphics-go/graphics/interp/bilinear_test.go
blob: 242d70546f8b7e72dd98b9dfedc6d795956b02ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2012 The Graphics-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 interp

import (
	"image"
	"image/color"
	"testing"
)

type interpTest struct {
	desc     string
	src      []uint8
	srcWidth int
	x, y     float64
	expect   uint8
}

func (p *interpTest) newSrc() *image.RGBA {
	b := image.Rect(0, 0, p.srcWidth, len(p.src)/p.srcWidth)
	src := image.NewRGBA(b)
	i := 0
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			src.SetRGBA(x, y, color.RGBA{
				R: p.src[i],
				G: p.src[i],
				B: p.src[i],
				A: 0xff,
			})
			i++
		}
	}
	return src
}

var interpTests = []interpTest{
	{
		desc:     "center of a single white pixel should match that pixel",
		src:      []uint8{0x00},
		srcWidth: 1,
		x:        0.5,
		y:        0.5,
		expect:   0x00,
	},
	{
		desc: "middle of a square is equally weighted",
		src: []uint8{
			0x00, 0xff,
			0xff, 0x00,
		},
		srcWidth: 2,
		x:        1.0,
		y:        1.0,
		expect:   0x80,
	},
	{
		desc: "center of a pixel is just that pixel",
		src: []uint8{
			0x00, 0xff,
			0xff, 0x00,
		},
		srcWidth: 2,
		x:        1.5,
		y:        0.5,
		expect:   0xff,
	},
	{
		desc: "asymmetry abounds",
		src: []uint8{
			0xaa, 0x11, 0x55,
			0xff, 0x95, 0xdd,
		},
		srcWidth: 3,
		x:        2.0,
		y:        1.0,
		expect:   0x76, // (0x11 + 0x55 + 0x95 + 0xdd) / 4
	},
}

func TestBilinearRGBA(t *testing.T) {
	for _, p := range interpTests {
		src := p.newSrc()

		// Fast path.
		c := Bilinear.(RGBA).RGBA(src, p.x, p.y)
		if c.R != c.G || c.R != c.B || c.A != 0xff {
			t.Errorf("expect channels to match, got %v", c)
			continue
		}
		if c.R != p.expect {
			t.Errorf("%s: got 0x%02x want 0x%02x", p.desc, c.R, p.expect)
			continue
		}

		// Standard Interp should use the fast path.
		cStd := Bilinear.Interp(src, p.x, p.y)
		if cStd != c {
			t.Errorf("%s: standard mismatch got %v want %v", p.desc, cStd, c)
			continue
		}

		// General case should match the fast path.
		cGen := color.RGBAModel.Convert(bilinearGeneral(src, p.x, p.y))
		r0, g0, b0, a0 := c.RGBA()
		r1, g1, b1, a1 := cGen.RGBA()
		if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
			t.Errorf("%s: general case mismatch got %v want %v", p.desc, c, cGen)
			continue
		}
	}
}

func TestBilinearSubImage(t *testing.T) {
	b0 := image.Rect(0, 0, 4, 4)
	src0 := image.NewRGBA(b0)
	b1 := image.Rect(1, 1, 3, 3)
	src1 := src0.SubImage(b1).(*image.RGBA)
	src1.Set(1, 1, color.RGBA{0x11, 0, 0, 0xff})
	src1.Set(2, 1, color.RGBA{0x22, 0, 0, 0xff})
	src1.Set(1, 2, color.RGBA{0x33, 0, 0, 0xff})
	src1.Set(2, 2, color.RGBA{0x44, 0, 0, 0xff})

	tests := []struct {
		x, y float64
		want uint8
	}{
		{1, 1, 0x11},
		{3, 1, 0x22},
		{1, 3, 0x33},
		{3, 3, 0x44},
		{2, 2, 0x2b},
	}

	for _, p := range tests {
		c := Bilinear.(RGBA).RGBA(src1, p.x, p.y)
		if c.R != p.want {
			t.Errorf("(%.0f, %.0f): got 0x%02x want 0x%02x", p.x, p.y, c.R, p.want)
		}
	}
}