1package ports
2
3import (
4 "image"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
10// ToBuffer matches wl_output.transform: the buffer holds the surface rotated
11// counter-clockwise by the transform (after a flip around the vertical axis
12// for flipped values). Checked on the surface's top-left corner of a 3×2
13// surface.
14func TestBufferTransformToBuffer(t *testing.T) {
15 for _, tc := range []struct {
16 t BufferTransform
17 rotated bool
18 bx, by float64 // buffer point of surface (0.5, 0.5)
19 bufW, bufH int
20 }{
21 {0, false, 0.5, 0.5, 3, 2},
22 {1, true, 0.5, 2.5, 2, 3},
23 {2, false, 2.5, 1.5, 3, 2},
24 {3, true, 1.5, 0.5, 2, 3},
25 {4, false, 2.5, 0.5, 3, 2},
26 {5, true, 0.5, 0.5, 2, 3},
27 {6, false, 0.5, 1.5, 3, 2},
28 {7, true, 1.5, 2.5, 2, 3},
29 } {
30 if tc.t.Rotated() != tc.rotated {
31 t.Errorf("%d: rotated %v", tc.t, tc.t.Rotated())
32 }
33 x, y := tc.t.ToBuffer(0.5, 0.5, 3, 2)
34 if x != tc.bx || y != tc.by {
35 t.Errorf("%d: (0.5,0.5) -> (%v,%v), want (%v,%v)", tc.t, x, y, tc.bx, tc.by)
36 }
37 if x < 0 || y < 0 || x > float64(tc.bufW) || y > float64(tc.bufH) {
38 t.Errorf("%d: (%v,%v) outside the %dx%d buffer", tc.t, x, y, tc.bufW, tc.bufH)
39 }
40 }
41}
42
43func TestBufferTransformInvert(t *testing.T) {
44 const w, h = 7.0, 3.0
45 for tr := BufferTransform(0); tr < 8; tr++ {
46 assert.Equal(t, tr, tr.Invert().Invert(), "transform %d", tr)
47 bw, bh := w, h
48 if tr.Rotated() {
49 bw, bh = h, w
50 }
51 for _, p := range [][2]float64{{0, 0}, {1.5, 1}, {6.25, 2.5}} {
52 bx, by := tr.ToBuffer(p[0], p[1], w, h)
53 x, y := tr.Invert().ToBuffer(bx, by, bw, bh)
54 assert.InDelta(t, p[0], x, 1e-9, "transform %d point %v", tr, p)
55 assert.InDelta(t, p[1], y, 1e-9, "transform %d point %v", tr, p)
56 }
57 }
58}
59
60// RectToBuffer is the bounding box of the rect's corners under ToBuffer.
61func TestRectToBuffer(t *testing.T) {
62 const w, h = 7, 3
63 r := image.Rect(1, 0, 3, 2)
64 assert.Equal(t, image.Rect(0, 5, 1, 7), BufferTransform(1).RectToBuffer(image.Rect(0, 0, 2, 1), w, h))
65 for tr := BufferTransform(0); tr < 8; tr++ {
66 got := tr.RectToBuffer(r, w, h)
67 x0, y0 := tr.ToBuffer(float64(r.Min.X), float64(r.Min.Y), w, h)
68 x1, y1 := tr.ToBuffer(float64(r.Max.X), float64(r.Max.Y), w, h)
69 x2, y2 := tr.ToBuffer(float64(r.Min.X), float64(r.Max.Y), w, h)
70 x3, y3 := tr.ToBuffer(float64(r.Max.X), float64(r.Min.Y), w, h)
71 want := image.Rect(int(min(x0, x1, x2, x3)), int(min(y0, y1, y2, y3)), int(max(x0, x1, x2, x3)), int(max(y0, y1, y2, y3)))
72 assert.Equal(t, want, got, "transform %d", tr)
73 assert.Equal(t, r.Dx()*r.Dy(), got.Dx()*got.Dy(), "transform %d area", tr)
74 }
75}
76
77func TestBufferTransformSize(t *testing.T) {
78 for tr := BufferTransform(0); tr < 8; tr++ {
79 w, h := tr.Size(7, 3)
80 if tr.Rotated() {
81 assert.Equal(t, [2]int{3, 7}, [2]int{w, h}, "transform %d", tr)
82 } else {
83 assert.Equal(t, [2]int{7, 3}, [2]int{w, h}, "transform %d", tr)
84 }
85 }
86}
87
88func TestOutputPlacementToTarget(t *testing.T) {
89 o := OutputPlacement{Info: OutputInfo{Width: 2560, Height: 1440}, Scale: 1, X: 100, Transform: 1}
90 x, y := o.ToTarget(100, 0)
91 wx, wy := BufferTransform(1).ToBuffer(0, 0, 1440, 2560)
92 assert.Equal(t, [2]float64{wx, wy}, [2]float64{x, y})
93 assert.Equal(t, [2]float64{0, 1440}, [2]float64{x, y})
94
95 o.Transform = 0
96 x, y = o.ToTarget(150, 20)
97 assert.Equal(t, [2]float64{50, 20}, [2]float64{x, y})
98}