-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_test.go
161 lines (145 loc) · 4.02 KB
/
render_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2016 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 ivg_test
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"strings"
"testing"
"github.com/reactivego/ivg/decode"
"github.com/reactivego/ivg/raster/vec"
"github.com/reactivego/ivg/render"
)
// overwriteTestdataFiles is temporarily set to true when adding new
// testdataTestCases.
const overwriteTestdataFiles = false
func encodePNG(dstFilename string, src image.Image) error {
f, err := os.Create(dstFilename)
if err != nil {
return err
}
encErr := png.Encode(f, src)
closeErr := f.Close()
if encErr != nil {
return encErr
}
return closeErr
}
func decodePNG(srcFilename string) (image.Image, error) {
f, err := os.Open(srcFilename)
if err != nil {
return nil, err
}
defer f.Close()
return png.Decode(f)
}
func checkApproxEqual(m0, m1 image.Image) error {
diff := func(a, b uint32) uint32 {
if a < b {
return b - a
}
return a - b
}
bounds0 := m0.Bounds()
bounds1 := m1.Bounds()
if bounds0 != bounds1 {
return fmt.Errorf("bounds differ: got %v, want %v", bounds0, bounds1)
}
for y := bounds0.Min.Y; y < bounds0.Max.Y; y++ {
for x := bounds0.Min.X; x < bounds0.Max.X; x++ {
r0, g0, b0, a0 := m0.At(x, y).RGBA()
r1, g1, b1, a1 := m1.At(x, y).RGBA()
// TODO: be more principled in picking this magic threshold, other
// than what the difference is, in practice, in x/image/vector's
// fixed and floating point rasterizer?
const D = 0xffff * 12 / 100 // Diff threshold of 12%.
if diff(r0, r1) > D || diff(g0, g1) > D || diff(b0, b1) > D || diff(a0, a1) > D {
return fmt.Errorf("at (%d, %d):\n"+
"got RGBA %#04x, %#04x, %#04x, %#04x\n"+
"want RGBA %#04x, %#04x, %#04x, %#04x",
x, y, r0, g0, b0, a0, r1, g1, b1, a1)
}
}
}
return nil
}
var testdataTestCases = []struct {
filename string
variants string
}{
{"testdata/action-info.lores", ""},
{"testdata/action-info.hires", ""},
{"testdata/arcs", ""},
{"testdata/blank", ""},
{"testdata/cowbell", ""},
{"testdata/elliptical", ""},
{"testdata/favicon", ";pink"},
{"testdata/gradient", ""},
{"testdata/lod-polygon", ";64"},
{"testdata/video-005.primitive", ""},
}
func TestRenderer(t *testing.T) {
for _, tc := range testdataTestCases {
ivgData, err := os.ReadFile(filepath.FromSlash(tc.filename) + ".ivg")
if err != nil {
t.Errorf("%s: ReadFile: %v", tc.filename, err)
continue
}
vb, err := decode.DecodeViewBox(ivgData)
if err != nil {
t.Errorf("%s: DecodeViewBox: %v", tc.filename, err)
continue
}
for _, variant := range strings.Split(tc.variants, ";") {
length := 256
if variant == "64" {
length = 64
}
width, height := length, length
if dx, dy := vb.Size(); dx < dy {
width = int(float32(length) * dx / dy)
} else {
height = int(float32(length) * dy / dx)
}
opts := []decode.DecodeOption{}
if variant == "pink" {
pink := color.RGBA{0xfe, 0x76, 0xea, 0xff}
opts = append(opts, decode.WithColorAt(0, pink))
}
bounds := image.Rect(0, 0, width, height)
got := image.NewRGBA(bounds)
var z render.Renderer
z.SetRasterizer(&vec.Rasterizer{Dst: got, DrawOp: draw.Src}, got.Bounds())
if err := decode.Decode(&z, ivgData, opts...); err != nil {
t.Errorf("%s %q variant: Decode: %v", tc.filename, variant, err)
continue
}
wantFilename := filepath.FromSlash(tc.filename)
if variant != "" {
wantFilename += "." + variant
}
wantFilename += ".png"
if overwriteTestdataFiles {
if err := encodePNG(filepath.FromSlash(wantFilename), got); err != nil {
t.Errorf("%s %q variant: encodePNG: %v", tc.filename, variant, err)
}
continue
}
want, err := decodePNG(wantFilename)
if err != nil {
t.Errorf("%s %q variant: decodePNG: %v", tc.filename, variant, err)
continue
}
if err := checkApproxEqual(got, want); err != nil {
t.Errorf("%s %q variant: %v", tc.filename, variant, err)
continue
}
}
}
}