-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwriter_test.go
140 lines (128 loc) · 3 KB
/
writer_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
// Copyright 2011 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 strogonoff
import (
"bytes"
"image"
"image/png"
"io/ioutil"
"rand"
"os"
"testing"
)
var testCase = []struct {
filename string
quality int
tolerance int64
}{
//{"testdata/video-001.png", 1, 24 << 8}, // -> too small to encode text
{"testdata/video-001.png", 20, 12 << 8},
{"testdata/video-001.png", 60, 8 << 8},
{"testdata/video-001.png", 80, 6 << 8},
{"testdata/video-001.png", 90, 4 << 8},
{"testdata/video-001.png", 100, 2 << 8},
}
func delta(u0, u1 uint32) int64 {
d := int64(u0) - int64(u1)
if d < 0 {
return -d
}
return d
}
func readPng(filename string) (image.Image, os.Error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return png.Decode(f)
}
func TestWriteToDisk(t *testing.T) {
tc := testCase[len(testCase)-1]
msg := "Hello world!";
i, err := readPng(tc.filename)
if err != nil {
t.Error(tc.filename, err)
}
f, err := os.Create("testdata/output.jpeg")
if err != nil {
t.Error(tc.filename, err)
}
err = Encode(f, i, msg, &Options{Quality: tc.quality})
if err != nil {
t.Error(tc.filename, err)
}
}
func TestWriter(t *testing.T) {
for _, tc := range testCase {
msg := "Hello world; or こんにちは 世界!";
// Read the image.
m0, err := readPng(tc.filename)
if err != nil {
t.Error(tc.filename, err)
continue
}
// Encode that image as JPEG.
buf := bytes.NewBuffer(nil)
err = Encode(buf, m0, msg, &Options{Quality: tc.quality})
if err != nil {
t.Error(tc.filename, err)
continue
}
// Decode that JPEG.
m1, data, err := DecodeAndRead(buf)
if data != msg {
t.Error("Got wrong message back:", data)
}
if err != nil {
t.Error(tc.filename, err)
continue
}
// Compute the average delta in RGB space.
b := m0.Bounds()
var sum, n int64
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
c0 := m0.At(x, y)
c1 := m1.At(x, y)
r0, g0, b0, _ := c0.RGBA()
r1, g1, b1, _ := c1.RGBA()
sum += delta(r0, r1)
sum += delta(g0, g1)
sum += delta(b0, b1)
n += 3
}
}
// Compare the average delta to the tolerance level.
if sum/n > tc.tolerance {
t.Errorf("%s, quality=%d: average delta is too high", tc.filename, tc.quality)
continue
}
}
}
func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(640, 480)
// Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds()
rnd := rand.New(rand.NewSource(123))
for y := bo.Min.Y; y < bo.Max.Y; y++ {
for x := bo.Min.X; x < bo.Max.X; x++ {
img.Set(x, y, image.RGBAColor{
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
255})
}
}
if !img.Opaque() {
panic("expected image to be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
options := &Options{Quality: 90}
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, "Hello World!", options)
}
}