File: encode_test.go

package info (click to toggle)
golang-github-bep-gowebp 0.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,588 kB
  • sloc: ansic: 75,826; sh: 1,085; makefile: 579; python: 350; cpp: 262
file content (224 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package libwebp

import (
	"bytes"
	"flag"
	"image"
	"image/draw"
	"image/jpeg"
	_ "image/jpeg"
	_ "image/png"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/bep/gowebp/internal/libwebp"

	"github.com/bep/gowebp/libwebp/webpoptions"
	"golang.org/x/image/webp"
)

type testCase struct {
	name      string
	inputFile string
	opts      webpoptions.EncodingOptions
}

var testCases = []testCase{
	{"lossy", "sunset.jpg", webpoptions.EncodingOptions{Quality: 75, EncodingPreset: webpoptions.EncodingPresetPhoto, UseSharpYuv: true}},
	{"lossless", "source.jpg", webpoptions.EncodingOptions{}},
	{"bw", "bw-gopher.png", webpoptions.EncodingOptions{Quality: 75}},
	{"transparent-32", "fuzzy-cirlcle.png", webpoptions.EncodingOptions{Quality: 75}},
}

func TestEncode(t *testing.T) {
	writeGolden := false
	for _, test := range testCases {
		t.Run(test.name, func(t *testing.T) {
			r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
			if err != nil {
				t.Fatal(err)
			}

			img, _, err := image.Decode(r)
			if err != nil {
				t.Fatal(err)
			}

			targetName := strings.TrimSuffix(test.inputFile, filepath.Ext(test.inputFile)) + "-" + test.name + ".webp"

			targetFilename := filepath.Join("../test_data/images/golden", targetName)
			b := &bytes.Buffer{}
			var w io.Writer = b

			if writeGolden {
				f, err := os.Create(targetFilename)
				if err != nil {
					t.Fatal(err)
				}
				w = f
				defer f.Close()
			}

			if err = Encode(w, img, test.opts); err != nil {
				t.Fatal(err)
			}

			if !writeGolden {
				f, err := os.Open(targetFilename)
				if err != nil {
					t.Fatal(err)
				}
				defer f.Close()

				img1 := decodeWebp(t, f)
				img2 := decodeWebp(t, b)

				if !goldenEqual(img1, img2) {
					t.Fatal("images are different")
				}

			}
		})
	}
}

var longrunning = flag.Bool("longrunning", false, "Enable long running tests.")

// go test -v ./libwebp --longrunning
func TestEncodeLongRunning(t *testing.T) {
	if !(*longrunning) {
		t.Skip("Skip long running test...")
	}
	t.Log("Start...")
	for i := 0; i < 60; i++ {
		for _, test := range testCases {
			r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
			if err != nil {
				t.Fatal(err)
			}

			img, _, err := image.Decode(r)
			if err != nil {
				t.Fatal(err)
			}

			if err = Encode(ioutil.Discard, img, test.opts); err != nil {
				t.Fatal(err)
			}
		}

		time.Sleep(2 * time.Second)
	}

	t.Log("Done...")
}

func BenchmarkEncode(b *testing.B) {
	for _, test := range testCases {
		b.Run(test.name, func(b *testing.B) {
			r, err := os.Open(filepath.Join("../test_data/images", test.inputFile))
			if err != nil {
				b.Fatal(err)
			}

			img, _, err := image.Decode(r)
			if err != nil {
				b.Fatal(err)
			}

			// Encode will convert to NRGBA if needed. Do that here so we
			// don't get those numbers included in the below.
			imgrgba := libwebp.ConvertToNRGBA(img)

			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				if err = Encode(ioutil.Discard, imgrgba, test.opts); err != nil {
					b.Fatal(err)
				}
			}
		})
	}
}

// Just to have something to compare with.
func BenchmarkEncodeJpeg(b *testing.B) {
	r, err := os.Open("../test_data/images/source.jpg")
	if err != nil {
		b.Fatal(err)
	}

	img, err := jpeg.Decode(r)
	if err != nil {
		b.Fatal(err)
	}

	opts := &jpeg.Options{
		Quality: 75,
	}

	for i := 0; i < b.N; i++ {
		if err = jpeg.Encode(ioutil.Discard, img, opts); err != nil {
			b.Fatal(err)
		}
	}
}

// usesFMA indicates whether "fused multiply and add" (FMA) instruction is
// used.  The command "grep FMADD go/test/codegen/floats.go" can help keep
// the FMA-using architecture list updated.
var usesFMA = runtime.GOARCH == "s390x" ||
	runtime.GOARCH == "ppc64" ||
	runtime.GOARCH == "ppc64le" ||
	runtime.GOARCH == "arm64"

func decodeWebp(t *testing.T, r io.Reader) *image.NRGBA {
	img, err := webp.Decode(r)
	if err != nil {
		t.Fatal(err)
	}

	b := img.Bounds()
	m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
	draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)

	return m
}

// goldenEqual compares two NRGBA images.
// A small tolerance is allowed on architectures using "fused multiply and add"
// (FMA) instruction to accommodate for floating-point rounding differences
// with control golden images that were generated on amd64 architecture.
// See https://golang.org/ref/spec#Floating_point_operators
// and https://github.com/gohugoio/hugo/issues/6387 for more information.
//
// Borrowed from https://github.com/disintegration/gift/blob/a999ff8d5226e5ab14b64a94fca07c4ac3f357cf/gift_test.go#L598-L625
// Copyright (c) 2014-2019 Grigory Dryapak
// Licensed under the MIT License.
func goldenEqual(img1, img2 *image.NRGBA) bool {
	maxDiff := 0
	if usesFMA {
		maxDiff = 1
	}
	if !img1.Rect.Eq(img2.Rect) {
		return false
	}
	if len(img1.Pix) != len(img2.Pix) {
		return false
	}
	for i := 0; i < len(img1.Pix); i++ {
		diff := int(img1.Pix[i]) - int(img2.Pix[i])
		if diff < 0 {
			diff = -diff
		}
		if diff > maxDiff {
			return false
		}
	}
	return true
}