Home | History | Annotate | Download | only in jpeg
      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 // TestWriteGrayscale tests that a grayscale images survives a round-trip
    164 // through encode/decode cycle.
    165 func 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.
    193 func 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 
    211 func TestEncodeYCbCr(t *testing.T) {
    212 	bo := image.Rect(0, 0, 640, 480)
    213 	imgRGBA := image.NewRGBA(bo)
    214 	// Must use 444 subsampling to avoid lossy RGBA to YCbCr conversion.
    215 	imgYCbCr := image.NewYCbCr(bo, image.YCbCrSubsampleRatio444)
    216 	rnd := rand.New(rand.NewSource(123))
    217 	// Create identical rgba and ycbcr images.
    218 	for y := bo.Min.Y; y < bo.Max.Y; y++ {
    219 		for x := bo.Min.X; x < bo.Max.X; x++ {
    220 			col := color.RGBA{
    221 				uint8(rnd.Intn(256)),
    222 				uint8(rnd.Intn(256)),
    223 				uint8(rnd.Intn(256)),
    224 				255,
    225 			}
    226 			imgRGBA.SetRGBA(x, y, col)
    227 			yo := imgYCbCr.YOffset(x, y)
    228 			co := imgYCbCr.COffset(x, y)
    229 			cy, ccr, ccb := color.RGBToYCbCr(col.R, col.G, col.B)
    230 			imgYCbCr.Y[yo] = cy
    231 			imgYCbCr.Cb[co] = ccr
    232 			imgYCbCr.Cr[co] = ccb
    233 		}
    234 	}
    235 
    236 	// Now check that both images are identical after an encode.
    237 	var bufRGBA, bufYCbCr bytes.Buffer
    238 	Encode(&bufRGBA, imgRGBA, nil)
    239 	Encode(&bufYCbCr, imgYCbCr, nil)
    240 	if !bytes.Equal(bufRGBA.Bytes(), bufYCbCr.Bytes()) {
    241 		t.Errorf("RGBA and YCbCr encoded bytes differ")
    242 	}
    243 }
    244 
    245 func BenchmarkEncodeRGBA(b *testing.B) {
    246 	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
    247 	bo := img.Bounds()
    248 	rnd := rand.New(rand.NewSource(123))
    249 	for y := bo.Min.Y; y < bo.Max.Y; y++ {
    250 		for x := bo.Min.X; x < bo.Max.X; x++ {
    251 			img.SetRGBA(x, y, color.RGBA{
    252 				uint8(rnd.Intn(256)),
    253 				uint8(rnd.Intn(256)),
    254 				uint8(rnd.Intn(256)),
    255 				255,
    256 			})
    257 		}
    258 	}
    259 	b.SetBytes(640 * 480 * 4)
    260 	b.ReportAllocs()
    261 	b.ResetTimer()
    262 	options := &Options{Quality: 90}
    263 	for i := 0; i < b.N; i++ {
    264 		Encode(ioutil.Discard, img, options)
    265 	}
    266 }
    267 
    268 func BenchmarkEncodeYCbCr(b *testing.B) {
    269 	img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420)
    270 	bo := img.Bounds()
    271 	rnd := rand.New(rand.NewSource(123))
    272 	for y := bo.Min.Y; y < bo.Max.Y; y++ {
    273 		for x := bo.Min.X; x < bo.Max.X; x++ {
    274 			cy := img.YOffset(x, y)
    275 			ci := img.COffset(x, y)
    276 			img.Y[cy] = uint8(rnd.Intn(256))
    277 			img.Cb[ci] = uint8(rnd.Intn(256))
    278 			img.Cr[ci] = uint8(rnd.Intn(256))
    279 		}
    280 	}
    281 	b.SetBytes(640 * 480 * 3)
    282 	b.ReportAllocs()
    283 	b.ResetTimer()
    284 	options := &Options{Quality: 90}
    285 	for i := 0; i < b.N; i++ {
    286 		Encode(ioutil.Discard, img, options)
    287 	}
    288 }
    289