1// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package jpeg
6
7import (
8	"bytes"
9	"fmt"
10	"image"
11	"image/color"
12	"image/png"
13	"io/ioutil"
14	"math/rand"
15	"os"
16	"testing"
17)
18
19// zigzag maps from the natural ordering to the zig-zag ordering. For example,
20// zigzag[0*8 + 3] is the zig-zag sequence number of the element in the fourth
21// column and first row.
22var zigzag = [blockSize]int{
23	0, 1, 5, 6, 14, 15, 27, 28,
24	2, 4, 7, 13, 16, 26, 29, 42,
25	3, 8, 12, 17, 25, 30, 41, 43,
26	9, 11, 18, 24, 31, 40, 44, 53,
27	10, 19, 23, 32, 39, 45, 52, 54,
28	20, 22, 33, 38, 46, 51, 55, 60,
29	21, 34, 37, 47, 50, 56, 59, 61,
30	35, 36, 48, 49, 57, 58, 62, 63,
31}
32
33func TestZigUnzig(t *testing.T) {
34	for i := 0; i < blockSize; i++ {
35		if unzig[zigzag[i]] != i {
36			t.Errorf("unzig[zigzag[%d]] == %d", i, unzig[zigzag[i]])
37		}
38		if zigzag[unzig[i]] != i {
39			t.Errorf("zigzag[unzig[%d]] == %d", i, zigzag[unzig[i]])
40		}
41	}
42}
43
44// unscaledQuantInNaturalOrder are the unscaled quantization tables in
45// natural (not zig-zag) order, as specified in section K.1.
46var unscaledQuantInNaturalOrder = [nQuantIndex][blockSize]byte{
47	// Luminance.
48	{
49		16, 11, 10, 16, 24, 40, 51, 61,
50		12, 12, 14, 19, 26, 58, 60, 55,
51		14, 13, 16, 24, 40, 57, 69, 56,
52		14, 17, 22, 29, 51, 87, 80, 62,
53		18, 22, 37, 56, 68, 109, 103, 77,
54		24, 35, 55, 64, 81, 104, 113, 92,
55		49, 64, 78, 87, 103, 121, 120, 101,
56		72, 92, 95, 98, 112, 100, 103, 99,
57	},
58	// Chrominance.
59	{
60		17, 18, 24, 47, 99, 99, 99, 99,
61		18, 21, 26, 66, 99, 99, 99, 99,
62		24, 26, 56, 99, 99, 99, 99, 99,
63		47, 66, 99, 99, 99, 99, 99, 99,
64		99, 99, 99, 99, 99, 99, 99, 99,
65		99, 99, 99, 99, 99, 99, 99, 99,
66		99, 99, 99, 99, 99, 99, 99, 99,
67		99, 99, 99, 99, 99, 99, 99, 99,
68	},
69}
70
71func TestUnscaledQuant(t *testing.T) {
72	bad := false
73	for i := quantIndex(0); i < nQuantIndex; i++ {
74		for zig := 0; zig < blockSize; zig++ {
75			got := unscaledQuant[i][zig]
76			want := unscaledQuantInNaturalOrder[i][unzig[zig]]
77			if got != want {
78				t.Errorf("i=%d, zig=%d: got %d, want %d", i, zig, got, want)
79				bad = true
80			}
81		}
82	}
83	if bad {
84		names := [nQuantIndex]string{"Luminance", "Chrominance"}
85		buf := &bytes.Buffer{}
86		for i, name := range names {
87			fmt.Fprintf(buf, "// %s.\n{\n", name)
88			for zig := 0; zig < blockSize; zig++ {
89				fmt.Fprintf(buf, "%d, ", unscaledQuantInNaturalOrder[i][unzig[zig]])
90				if zig%8 == 7 {
91					buf.WriteString("\n")
92				}
93			}
94			buf.WriteString("},\n")
95		}
96		t.Logf("expected unscaledQuant values:\n%s", buf.String())
97	}
98}
99
100var testCase = []struct {
101	filename  string
102	quality   int
103	tolerance int64
104}{
105	{"../testdata/video-001.png", 1, 24 << 8},
106	{"../testdata/video-001.png", 20, 12 << 8},
107	{"../testdata/video-001.png", 60, 8 << 8},
108	{"../testdata/video-001.png", 80, 6 << 8},
109	{"../testdata/video-001.png", 90, 4 << 8},
110	{"../testdata/video-001.png", 100, 2 << 8},
111}
112
113func delta(u0, u1 uint32) int64 {
114	d := int64(u0) - int64(u1)
115	if d < 0 {
116		return -d
117	}
118	return d
119}
120
121func readPng(filename string) (image.Image, error) {
122	f, err := os.Open(filename)
123	if err != nil {
124		return nil, err
125	}
126	defer f.Close()
127	return png.Decode(f)
128}
129
130func TestWriter(t *testing.T) {
131	for _, tc := range testCase {
132		// Read the image.
133		m0, err := readPng(tc.filename)
134		if err != nil {
135			t.Error(tc.filename, err)
136			continue
137		}
138		// Encode that image as JPEG.
139		var buf bytes.Buffer
140		err = Encode(&buf, m0, &Options{Quality: tc.quality})
141		if err != nil {
142			t.Error(tc.filename, err)
143			continue
144		}
145		// Decode that JPEG.
146		m1, err := Decode(&buf)
147		if err != nil {
148			t.Error(tc.filename, err)
149			continue
150		}
151		if m0.Bounds() != m1.Bounds() {
152			t.Errorf("%s, bounds differ: %v and %v", tc.filename, m0.Bounds(), m1.Bounds())
153			continue
154		}
155		// Compare the average delta to the tolerance level.
156		if averageDelta(m0, m1) > tc.tolerance {
157			t.Errorf("%s, quality=%d: average delta is too high", tc.filename, tc.quality)
158			continue
159		}
160	}
161}
162
163// TestWriteGrayscale tests that a grayscale images survives a round-trip
164// through encode/decode cycle.
165func TestWriteGrayscale(t *testing.T) {
166	m0 := image.NewGray(image.Rect(0, 0, 32, 32))
167	for i := range m0.Pix {
168		m0.Pix[i] = uint8(i)
169	}
170	var buf bytes.Buffer
171	if err := Encode(&buf, m0, nil); err != nil {
172		t.Fatal(err)
173	}
174	m1, err := Decode(&buf)
175	if err != nil {
176		t.Fatal(err)
177	}
178	if m0.Bounds() != m1.Bounds() {
179		t.Fatalf("bounds differ: %v and %v", m0.Bounds(), m1.Bounds())
180	}
181	if _, ok := m1.(*image.Gray); !ok {
182		t.Errorf("got %T, want *image.Gray", m1)
183	}
184	// Compare the average delta to the tolerance level.
185	want := int64(2 << 8)
186	if got := averageDelta(m0, m1); got > want {
187		t.Errorf("average delta too high; got %d, want <= %d", got, want)
188	}
189}
190
191// averageDelta returns the average delta in RGB space. The two images must
192// have the same bounds.
193func averageDelta(m0, m1 image.Image) int64 {
194	b := m0.Bounds()
195	var sum, n int64
196	for y := b.Min.Y; y < b.Max.Y; y++ {
197		for x := b.Min.X; x < b.Max.X; x++ {
198			c0 := m0.At(x, y)
199			c1 := m1.At(x, y)
200			r0, g0, b0, _ := c0.RGBA()
201			r1, g1, b1, _ := c1.RGBA()
202			sum += delta(r0, r1)
203			sum += delta(g0, g1)
204			sum += delta(b0, b1)
205			n += 3
206		}
207	}
208	return sum / n
209}
210
211func BenchmarkEncode(b *testing.B) {
212	b.StopTimer()
213	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
214	bo := img.Bounds()
215	rnd := rand.New(rand.NewSource(123))
216	for y := bo.Min.Y; y < bo.Max.Y; y++ {
217		for x := bo.Min.X; x < bo.Max.X; x++ {
218			img.SetRGBA(x, y, color.RGBA{
219				uint8(rnd.Intn(256)),
220				uint8(rnd.Intn(256)),
221				uint8(rnd.Intn(256)),
222				255,
223			})
224		}
225	}
226	b.SetBytes(640 * 480 * 4)
227	b.StartTimer()
228	options := &Options{Quality: 90}
229	for i := 0; i < b.N; i++ {
230		Encode(ioutil.Discard, img, options)
231	}
232}
233