github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/image/jpeg/writer_test.go (about) 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 5 package jpeg 6 7 import ( 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. 22 var 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 33 func 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. 46 var 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 71 func 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 100 var 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 113 func delta(u0, u1 uint32) int64 { 114 d := int64(u0) - int64(u1) 115 if d < 0 { 116 return -d 117 } 118 return d 119 } 120 121 func 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 130 func 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 // averageDelta returns the average delta in RGB space. The two images must 164 // have the same bounds. 165 func averageDelta(m0, m1 image.Image) int64 { 166 b := m0.Bounds() 167 var sum, n int64 168 for y := b.Min.Y; y < b.Max.Y; y++ { 169 for x := b.Min.X; x < b.Max.X; x++ { 170 c0 := m0.At(x, y) 171 c1 := m1.At(x, y) 172 r0, g0, b0, _ := c0.RGBA() 173 r1, g1, b1, _ := c1.RGBA() 174 sum += delta(r0, r1) 175 sum += delta(g0, g1) 176 sum += delta(b0, b1) 177 n += 3 178 } 179 } 180 return sum / n 181 } 182 183 func BenchmarkEncode(b *testing.B) { 184 b.StopTimer() 185 img := image.NewRGBA(image.Rect(0, 0, 640, 480)) 186 bo := img.Bounds() 187 rnd := rand.New(rand.NewSource(123)) 188 for y := bo.Min.Y; y < bo.Max.Y; y++ { 189 for x := bo.Min.X; x < bo.Max.X; x++ { 190 img.SetRGBA(x, y, color.RGBA{ 191 uint8(rnd.Intn(256)), 192 uint8(rnd.Intn(256)), 193 uint8(rnd.Intn(256)), 194 255, 195 }) 196 } 197 } 198 b.SetBytes(640 * 480 * 4) 199 b.StartTimer() 200 options := &Options{Quality: 90} 201 for i := 0; i < b.N; i++ { 202 Encode(ioutil.Discard, img, options) 203 } 204 }