| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package portable |
| 6 | |
| 7 | import ( |
| 8 | "image" |
| 9 | "image/color" |
| 10 | "image/draw" |
| 11 | "image/png" |
| 12 | "math" |
| 13 | "os" |
| 14 | "runtime" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/mobile/event/size" |
| 18 | "golang.org/x/mobile/exp/f32" |
| 19 | "golang.org/x/mobile/geom" |
| 20 | ) |
| 21 | |
| 22 | func TestAffine(t *testing.T) { |
| 23 | if runtime.GOOS == "android" || runtime.GOOS == "ios" { |
| 24 | t.Skipf("testdata not available on %s", runtime.GOOS) |
| 25 | } |
| 26 | f, err := os.Open("../../../testdata/testpattern.png") |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | defer f.Close() |
| 31 | srcOrig, _, err := image.Decode(f) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | src := image.NewRGBA(srcOrig.Bounds()) |
| 36 | draw.Draw(src, src.Rect, srcOrig, srcOrig.Bounds().Min, draw.Src) |
| 37 | |
| 38 | const ( |
| 39 | pixW = 100 |
| 40 | pixH = 100 |
| 41 | ptW = geom.Pt(50) |
| 42 | ptH = geom.Pt(50) |
| 43 | ) |
| 44 | sz := size.Event{ |
| 45 | WidthPx: pixW, |
| 46 | HeightPx: pixH, |
| 47 | WidthPt: ptW, |
| 48 | HeightPt: ptH, |
| 49 | PixelsPerPt: float32(pixW) / float32(ptW), |
| 50 | } |
| 51 | |
| 52 | got := image.NewRGBA(image.Rect(0, 0, pixW, pixH)) |
| 53 | blue := image.NewUniform(color.RGBA{B: 0xff, A: 0xff}) |
| 54 | draw.Draw(got, got.Bounds(), blue, image.Point{}, draw.Src) |
| 55 | |
| 56 | b := src.Bounds() |
| 57 | b.Min.X += 10 |
| 58 | b.Max.Y /= 2 |
| 59 | |
| 60 | var a f32.Affine |
| 61 | a.Identity() |
| 62 | a.Scale(&a, sz.PixelsPerPt, sz.PixelsPerPt) |
| 63 | a.Translate(&a, 0, 24) |
| 64 | a.Rotate(&a, float32(math.Asin(12./20))) |
| 65 | // See commentary in the render method defined in portable.go. |
| 66 | a.Scale(&a, 40/float32(b.Dx()), 20/float32(b.Dy())) |
| 67 | a.Inverse(&a) |
| 68 | |
| 69 | affine(got, src, b, nil, &a, draw.Over) |
| 70 | |
| 71 | ptTopLeft := geom.Point{0, 24} |
| 72 | ptBottomRight := geom.Point{12 + 32, 16} |
| 73 | |
| 74 | drawCross(got, 0, 0) |
| 75 | drawCross(got, int(ptTopLeft.X.Px(sz.PixelsPerPt)), int(ptTopLeft.Y.Px(sz.PixelsPerPt))) |
| 76 | drawCross(got, int(ptBottomRight.X.Px(sz.PixelsPerPt)), int(ptBottomRight.Y.Px(sz.PixelsPerPt))) |
| 77 | drawCross(got, pixW-1, pixH-1) |
| 78 | |
| 79 | const wantPath = "../../../testdata/testpattern-window.png" |
| 80 | f, err = os.Open(wantPath) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | defer f.Close() |
| 85 | wantSrc, _, err := image.Decode(f) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | want, ok := wantSrc.(*image.RGBA) |
| 90 | if !ok { |
| 91 | b := wantSrc.Bounds() |
| 92 | want = image.NewRGBA(b) |
| 93 | draw.Draw(want, b, wantSrc, b.Min, draw.Src) |
| 94 | } |
| 95 | |
| 96 | if !imageEq(got, want) { |
| 97 | gotPath, err := writeTempPNG("testpattern-window-got", got) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | t.Errorf("got\n%s\nwant\n%s", gotPath, wantPath) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestAffineMask(t *testing.T) { |
| 106 | if runtime.GOOS == "android" || runtime.GOOS == "ios" { |
| 107 | t.Skipf("testdata not available on %s", runtime.GOOS) |
| 108 | } |
| 109 | f, err := os.Open("../../../testdata/testpattern.png") |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | defer f.Close() |
| 114 | srcOrig, _, err := image.Decode(f) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | b := srcOrig.Bounds() |
| 119 | src := image.NewRGBA(b) |
| 120 | draw.Draw(src, src.Rect, srcOrig, b.Min, draw.Src) |
| 121 | mask := image.NewAlpha(b) |
| 122 | for y := b.Min.Y; y < b.Max.Y; y++ { |
| 123 | for x := b.Min.X; x < b.Max.X; x++ { |
| 124 | mask.Set(x, y, color.Alpha{A: uint8(x - b.Min.X)}) |
| 125 | } |
| 126 | } |
| 127 | want := image.NewRGBA(b) |
| 128 | draw.DrawMask(want, want.Rect, src, b.Min, mask, b.Min, draw.Src) |
| 129 | |
| 130 | a := new(f32.Affine) |
| 131 | a.Identity() |
| 132 | got := image.NewRGBA(b) |
| 133 | affine(got, src, b, mask, a, draw.Src) |
| 134 | |
| 135 | if !imageEq(got, want) { |
| 136 | gotPath, err := writeTempPNG("testpattern-mask-got", got) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | wantPath, err := writeTempPNG("testpattern-mask-want", want) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | t.Errorf("got\n%s\nwant\n%s", gotPath, wantPath) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func writeTempPNG(prefix string, m image.Image) (string, error) { |
| 149 | f, err := os.CreateTemp("", prefix+"-") |
| 150 | if err != nil { |
| 151 | return "", err |
| 152 | } |
| 153 | f.Close() |
| 154 | path := f.Name() + ".png" |
| 155 | f, err = os.Create(path) |
| 156 | if err != nil { |
| 157 | return "", err |
| 158 | } |
| 159 | if err := png.Encode(f, m); err != nil { |
| 160 | f.Close() |
| 161 | return "", err |
| 162 | } |
| 163 | return path, f.Close() |
| 164 | } |
| 165 | |
| 166 | func drawCross(m *image.RGBA, x, y int) { |
| 167 | c := color.RGBA{0xff, 0, 0, 0xff} // red |
| 168 | m.SetRGBA(x+0, y-2, c) |
| 169 | m.SetRGBA(x+0, y-1, c) |
| 170 | m.SetRGBA(x-2, y+0, c) |
| 171 | m.SetRGBA(x-1, y+0, c) |
| 172 | m.SetRGBA(x+0, y+0, c) |
| 173 | m.SetRGBA(x+1, y+0, c) |
| 174 | m.SetRGBA(x+2, y+0, c) |
| 175 | m.SetRGBA(x+0, y+1, c) |
| 176 | m.SetRGBA(x+0, y+2, c) |
| 177 | } |
| 178 | |
| 179 | func eqEpsilon(x, y uint8) bool { |
| 180 | const epsilon = 9 |
| 181 | return x-y < epsilon || y-x < epsilon |
| 182 | } |
| 183 | |
| 184 | func colorEq(c0, c1 color.RGBA) bool { |
| 185 | return eqEpsilon(c0.R, c1.R) && eqEpsilon(c0.G, c1.G) && eqEpsilon(c0.B, c1.B) && eqEpsilon(c0.A, c1.A) |
| 186 | } |
| 187 | |
| 188 | func imageEq(m0, m1 *image.RGBA) bool { |
| 189 | b0 := m0.Bounds() |
| 190 | b1 := m1.Bounds() |
| 191 | if b0 != b1 { |
| 192 | return false |
| 193 | } |
| 194 | badPx := 0 |
| 195 | for y := b0.Min.Y; y < b0.Max.Y; y++ { |
| 196 | for x := b0.Min.X; x < b0.Max.X; x++ { |
| 197 | c0, c1 := m0.At(x, y).(color.RGBA), m1.At(x, y).(color.RGBA) |
| 198 | if !colorEq(c0, c1) { |
| 199 | badPx++ |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | badFrac := float64(badPx) / float64(b0.Dx()*b0.Dy()) |
| 204 | return badFrac < 0.01 |
| 205 | } |