Debian Codegraph @

libwebp/encode_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package libwebp
2
3 import (
4 "bytes"
5 "flag"
6 "image"
7 "image/draw"
8 "image/jpeg"
9 _ "image/png"
10 "io"
11 "os"
12 "path/filepath"
13 "runtime"
14 "strings"
15 "testing"
16 "time"
17
18 "github.com/bep/gowebp/internal/libwebp"
19
20 "github.com/bep/gowebp/libwebp/webpoptions"
21 "golang.org/x/image/webp"
22 )
23
24 type testCase struct {
25 name string
26 inputFile string
27 opts webpoptions.EncodingOptions
28 }
29
30 var testCases = []testCase{
•31 // {"lossy", "sunset.jpg", webpoptions.EncodingOptions{Quality: 75, EncodingPreset: webpoptions.EncodingPresetPhoto, UseSharpYuv: true}},
32 // {"lossless", "source.jpg", webpoptions.EncodingOptions{}},
33 {"bw", "bw-gopher.png", webpoptions.EncodingOptions{Quality: 75}},
34 {"transparent-32", "fuzzy-cirlcle.png", webpoptions.EncodingOptions{Quality: 75}},
35 }
36
37 func TestEncode(t *testing.T) {
38 writeGolden := false
39 for _, test := range testCases {
40 t.Run(test.name, func(t *testing.T) {
41 r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 img, _, err := image.Decode(r)
47 if err != nil {
48 t.Fatal(err)
49 }
50
51 targetName := strings.TrimSuffix(test.inputFile, filepath.Ext(test.inputFile)) + "-" + test.name + ".webp"
52
53 targetFilename := filepath.Join("../test_data/images/golden", targetName)
54 b := &bytes.Buffer{}
55 var w io.Writer = b
56
57 if writeGolden {
58 f, err := os.Create(targetFilename)
59 if err != nil {
60 t.Fatal(err)
61 }
62 w = f
63 defer f.Close()
64 }
65
66 if err = Encode(w, img, test.opts); err != nil {
67 t.Fatal(err)
68 }
69
70 if !writeGolden {
71 f, err := os.Open(targetFilename)
72 if err != nil {
73 t.Fatal(err)
74 }
75 defer f.Close()
76
77 img1 := decodeWebp(t, f)
78 img2 := decodeWebp(t, b)
79
80 if !goldenEqual(img1, img2) {
81 t.Fatal("images are different")
82 }
83
84 }
85 })
86 }
87 }
88
89 var longrunning = flag.Bool("longrunning", false, "Enable long running tests.")
90
91 // go test -v ./libwebp --longrunning
92 func TestEncodeLongRunning(t *testing.T) {
93 if !(*longrunning) {
94 t.Skip("Skip long running test...")
95 }
96 t.Log("Start...")
97 for i := 0; i < 60; i++ {
98 for _, test := range testCases {
99 r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
100 if err != nil {
101 t.Fatal(err)
102 }
103
104 img, _, err := image.Decode(r)
105 if err != nil {
106 t.Fatal(err)
107 }
108
109 if err = Encode(io.Discard, img, test.opts); err != nil {
110 t.Fatal(err)
111 }
112 }
113
114 time.Sleep(2 * time.Second)
115 }
116
117 t.Log("Done...")
118 }
119
120 func BenchmarkEncode(b *testing.B) {
121 for _, test := range testCases {
122 b.Run(test.name, func(b *testing.B) {
123 r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
124 if err != nil {
125 b.Fatal(err)
126 }
127
128 img, _, err := image.Decode(r)
129 if err != nil {
130 b.Fatal(err)
131 }
132
133 // Encode will convert to NRGBA if needed. Do that here so we
134 // don't get those numbers included in the below.
135 imgrgba := libwebp.ConvertToNRGBA(img)
136
137 b.ResetTimer()
138 for i := 0; i < b.N; i++ {
139 if err = Encode(io.Discard, imgrgba, test.opts); err != nil {
140 b.Fatal(err)
141 }
142 }
143 })
144 }
145 }
146
147 // Just to have something to compare with.
148 func BenchmarkEncodeJpeg(b *testing.B) {
149 r, err := os.Open("../test_data/images/source.jpg")
150 if err != nil {
151 b.Fatal(err)
152 }
153
154 img, err := jpeg.Decode(r)
155 if err != nil {
156 b.Fatal(err)
157 }
158
159 opts := &jpeg.Options{
160 Quality: 75,
161 }
162
163 for i := 0; i < b.N; i++ {
164 if err = jpeg.Encode(io.Discard, img, opts); err != nil {
165 b.Fatal(err)
166 }
167 }
168 }
169
170 // usesFMA indicates whether "fused multiply and add" (FMA) instruction is
171 // used. The command "grep FMADD go/test/codegen/floats.go" can help keep
172 // the FMA-using architecture list updated.
173 var usesFMA = runtime.GOARCH == "s390x" ||
174 runtime.GOARCH == "ppc64" ||
175 runtime.GOARCH == "ppc64le" ||
176 runtime.GOARCH == "arm64"
177
178 func decodeWebp(t *testing.T, r io.Reader) *image.NRGBA {
179 img, err := webp.Decode(r)
180 if err != nil {
181 t.Fatal(err)
182 }
183
184 b := img.Bounds()
185 m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
186 draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
187
188 return m
189 }
190
191 // goldenEqual compares two NRGBA images.
192 // A small tolerance is allowed on architectures using "fused multiply and add"
193 // (FMA) instruction to accommodate for floating-point rounding differences
194 // with control golden images that were generated on amd64 architecture.
195 // See https://golang.org/ref/spec#Floating_point_operators
196 // and https://github.com/gohugoio/hugo/issues/6387 for more information.
197 //
198 // Borrowed from https://github.com/disintegration/gift/blob/a999ff8d5226e5ab14b64a94fca07c4ac3f357cf/gift_test.go#L598-L625
199 // Copyright (c) 2014-2019 Grigory Dryapak
200 // Licensed under the MIT License.
201 func goldenEqual(img1, img2 *image.NRGBA) bool {
202 maxDiff := 0
203 if usesFMA {
204 maxDiff = 1
205 }
206 if !img1.Rect.Eq(img2.Rect) {
207 return false
208 }
209 if len(img1.Pix) != len(img2.Pix) {
210 return false
211 }
212 for i := 0; i < len(img1.Pix); i++ {
213 diff := int(img1.Pix[i]) - int(img2.Pix[i])
214 if diff < 0 {
215 diff = -diff
216 }
217 if diff > maxDiff {
218 return false
219 }
220 }
221 return true
222 }