forked from hhrutter/tiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter_test.go
166 lines (151 loc) · 4.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
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
// Copyright 2012 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 tiff
import (
"bytes"
"image"
"io/ioutil"
"os"
"testing"
)
var roundtripTests = []struct {
filename string
opts *Options
}{
{"g4test_1.tiff", nil},
{"g4test_2.tiff", nil},
{"video-001.tiff", nil},
{"video-001-16bit.tiff", nil},
{"video-001-gray.tiff", nil},
{"video-001-gray-16bit.tiff", nil},
{"video-001-paletted.tiff", nil},
{"bw-packbits.tiff", nil},
{"video-001.tiff", &Options{Predictor: true}},
{"video-001.tiff", &Options{Compression: Deflate}},
{"video-001.tiff", &Options{Predictor: true, Compression: Deflate}},
{"video-001.tiff", &Options{Compression: LZW}},
{"video-001.tiff", &Options{Predictor: true, Compression: LZW}},
{"video-001-16bit.tiff", &Options{Predictor: true, Compression: LZW}},
{"video-001-gray.tiff", &Options{Predictor: true, Compression: LZW}},
{"video-001-gray-16bit.tiff", &Options{Predictor: true, Compression: LZW}},
{"go-aqua-cmyk.tiff", nil},
{"go-aqua-cmyk.tiff", &Options{Compression: LZW}},
{"go-aqua-cmyk.tiff", &Options{Predictor: true, Compression: LZW}},
{"zookeeper-cmyk.tiff", nil},
{"zookeeper-cmyk.tiff", &Options{Compression: LZW}},
{"zookeeper-cmyk.tiff", &Options{Predictor: true, Compression: LZW}},
}
func openImage(filename string) (image.Image, error) {
f, err := os.Open(testdataDir + filename)
if err != nil {
return nil, err
}
defer f.Close()
return Decode(f)
}
func TestRoundtrip(t *testing.T) {
for _, rt := range roundtripTests {
img, err := openImage(rt.filename)
if err != nil {
t.Fatal(err)
}
out := new(bytes.Buffer)
err = Encode(out, img, rt.opts)
if err != nil {
t.Fatal(err)
}
img2, err := Decode(&buffer{buf: out.Bytes()})
if err != nil {
t.Fatal(err)
}
compare(t, img, img2)
}
}
// TestRoundtrip2 tests that encoding and decoding an image whose
// origin is not (0, 0) gives the same thing.
func TestRoundtrip2(t *testing.T) {
m0 := image.NewRGBA(image.Rect(3, 4, 9, 8))
for i := range m0.Pix {
m0.Pix[i] = byte(i)
}
out := new(bytes.Buffer)
if err := Encode(out, m0, nil); err != nil {
t.Fatal(err)
}
m1, err := Decode(&buffer{buf: out.Bytes()})
if err != nil {
t.Fatal(err)
}
compare(t, m0, m1)
}
func delta(u0, u1 uint32) int64 {
d := int64(u0) - int64(u1)
if d < 0 {
return -d
}
return d
}
// averageDelta returns the average delta in RGB space. The two images must
// have the same bounds.
func averageDelta(m0, m1 image.Image) int64 {
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
}
}
return sum / n
}
// TestRoundtrip3 tests that encoding and decoding an image use JPEG compression.
func TestRoundtrip3(t *testing.T) {
roundtripTests := []string{
"bw-jpeg.tiff",
"video-001-jpeg.tiff",
}
for _, rt := range roundtripTests {
img, err := openImage(rt)
if err != nil {
t.Fatal(err)
}
out := new(bytes.Buffer)
err = Encode(out, img, &Options{Compression: JPEG})
if err != nil {
t.Fatal(err)
}
img2, err := Decode(&buffer{buf: out.Bytes()})
if err != nil {
t.Fatal(err)
}
want := int64(6 << 8)
if got := averageDelta(img, img2); got > want {
t.Errorf("average delta too high; got %d, want <= %d", got, want)
}
}
}
func benchmarkEncode(b *testing.B, name string, pixelSize int) {
img, err := openImage(name)
if err != nil {
b.Fatal(err)
}
s := img.Bounds().Size()
b.SetBytes(int64(s.X * s.Y * pixelSize))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, nil)
}
}
func BenchmarkEncode(b *testing.B) { benchmarkEncode(b, "video-001.tiff", 4) }
func BenchmarkEncodePaletted(b *testing.B) { benchmarkEncode(b, "video-001-paletted.tiff", 1) }
func BenchmarkEncodeGray(b *testing.B) { benchmarkEncode(b, "video-001-gray.tiff", 1) }
func BenchmarkEncodeGray16(b *testing.B) { benchmarkEncode(b, "video-001-gray-16bit.tiff", 2) }
func BenchmarkEncodeRGBA(b *testing.B) { benchmarkEncode(b, "video-001.tiff", 4) }
func BenchmarkEncodeRGBA64(b *testing.B) { benchmarkEncode(b, "video-001-16bit.tiff", 8) }