Commit 8b624f60 authored by Nigel Tao's avatar Nigel Tao

image/jpeg: decode progressive JPEGs.

To be clear, this supports decoding the bytes on the wire into an
in-memory image. There is no API change: jpeg.Decode will still not
return until the entire image is decoded.

The code is obviously more complicated, and costs around 10% in
performance on baseline JPEGs. The processSOS code could be cleaned up a
bit, and maybe some of that loss can be reclaimed, but I'll leave that
for follow-up CLs, to keep the diff for this one as small as possible.

Before:
BenchmarkDecode	    1000	   2855637 ns/op	  21.64 MB/s
After:
BenchmarkDecodeBaseline	     500	   3178960 ns/op	  19.44 MB/s
BenchmarkDecodeProgressive	     500	   4082640 ns/op	  15.14 MB/s

Fixes #3976.

The test data was generated by:
# Create intermediate files; cjpeg on Ubuntu 10.04 can't read PNG.
convert video-001.png video-001.bmp
convert video-005.gray.png video-005.gray.pgm
# Create new test files.
cjpeg -quality 100 -sample 1x1,1x1,1x1 -progressive video-001.bmp > video-001.progressive.jpeg
cjpeg -quality 50 -sample 2x2,1x1,1x1 video-001.bmp > video-001.q50.420.jpeg
cjpeg -quality 50 -sample 2x1,1x1,1x1 video-001.bmp > video-001.q50.422.jpeg
cjpeg -quality 50 -sample 1x1,1x1,1x1 video-001.bmp > video-001.q50.444.jpeg
cjpeg -quality 50 -sample 2x2,1x1,1x1 -progressive video-001.bmp > video-001.q50.420.progressive.jpeg
cjpeg -quality 50 -sample 2x1,1x1,1x1 -progressive video-001.bmp > video-001.q50.422.progressive.jpeg
cjpeg -quality 50 -sample 1x1,1x1,1x1 -progressive video-001.bmp > video-001.q50.444.progressive.jpeg
cjpeg -quality 50 video-005.gray.pgm > video-005.gray.q50.jpeg
cjpeg -quality 50 -progressive video-005.gray.pgm > video-005.gray.q50.progressive.jpeg
# Delete intermediate files.
rm video-001.bmp video-005.gray.pgm

R=r
CC=golang-dev
https://golang.org/cl/6684046
parent f2045aad
......@@ -31,6 +31,7 @@ var imageTests = []imageTest{
{"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
// JPEG is a lossy format and hence needs a non-zero tolerance.
{"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
{"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8},
// Grayscale images.
{"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
{"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
......
......@@ -15,9 +15,9 @@ const maxNumValues = 256
// Bit stream for the Huffman decoder.
// The n least significant bits of a form the unread bits, to be read in MSB to LSB order.
type bits struct {
a int // accumulator.
n int // the number of unread bits in a.
m int // mask. m==1<<(n-1) when n>0, with m==0 when n==0.
a uint32 // accumulator.
m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0.
n int // the number of unread bits in a.
}
// Huffman table decoder, specified in section C.
......@@ -39,7 +39,7 @@ func (d *decoder) ensureNBits(n int) error {
if err != nil {
return err
}
d.b.a = d.b.a<<8 | int(c)
d.b.a = d.b.a<<8 | uint32(c)
d.b.n += 8
if d.b.m == 0 {
d.b.m = 1 << 7
......@@ -69,7 +69,7 @@ func (d *decoder) receiveExtend(t uint8) (int, error) {
d.b.n -= int(t)
d.b.m >>= t
s := 1 << t
x := (d.b.a >> uint8(d.b.n)) & (s - 1)
x := int(d.b.a>>uint8(d.b.n)) & (s - 1)
if x < s>>1 {
x += ((-1) << t) + 1
}
......@@ -92,8 +92,7 @@ func (d *decoder) processDHT(n int) error {
return FormatError("bad Tc value")
}
th := d.tmp[0] & 0x0f
const isBaseline = true // Progressive mode is not yet supported.
if th > maxTh || isBaseline && th > 1 {
if th > maxTh || !d.progressive && th > 1 {
return FormatError("bad Th value")
}
h := &d.huff[tc][th]
......@@ -185,3 +184,28 @@ func (d *decoder) decodeHuffman(h *huffman) (uint8, error) {
}
return 0, FormatError("bad Huffman code")
}
func (d *decoder) decodeBit() (bool, error) {
if d.b.n == 0 {
err := d.ensureNBits(1)
if err != nil {
return false, err
}
}
ret := d.b.a&d.b.m != 0
d.b.n--
d.b.m >>= 1
return ret, nil
}
func (d *decoder) decodeBits(n int) (uint32, error) {
err := d.ensureNBits(n)
if err != nil {
return 0, err
}
ret := d.b.a >> uint(d.b.n-n)
ret &= (1 << uint(n)) - 1
d.b.n -= n
d.b.m >>= uint(n)
return ret, nil
}
This diff is collapsed.
// 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 jpeg
import (
"bytes"
"fmt"
"image"
"io/ioutil"
"os"
"testing"
)
// TestDecodeProgressive tests that decoding the baseline and progressive
// versions of the same image result in exactly the same pixel data, in YCbCr
// space for color images, and Y space for grayscale images.
func TestDecodeProgressive(t *testing.T) {
testCases := []string{
"../testdata/video-001",
"../testdata/video-001.q50.420",
"../testdata/video-001.q50.422",
"../testdata/video-001.q50.444",
"../testdata/video-005.gray.q50",
}
for _, tc := range testCases {
m0, err := decodeFile(tc + ".jpeg")
if err != nil {
t.Errorf("%s: %v", tc+".jpeg", err)
continue
}
m1, err := decodeFile(tc + ".progressive.jpeg")
if err != nil {
t.Errorf("%s: %v", tc+".progressive.jpeg", err)
continue
}
if m0.Bounds() != m1.Bounds() {
t.Errorf("%s: bounds differ: %v and %v", tc, m0.Bounds(), m1.Bounds())
continue
}
switch m0 := m0.(type) {
case *image.YCbCr:
m1 := m1.(*image.YCbCr)
if err := check(m0.Bounds(), m0.Y, m1.Y, m0.YStride, m1.YStride); err != nil {
t.Errorf("%s (Y): %v", tc, err)
continue
}
if err := check(m0.Bounds(), m0.Cb, m1.Cb, m0.CStride, m1.CStride); err != nil {
t.Errorf("%s (Cb): %v", tc, err)
continue
}
if err := check(m0.Bounds(), m0.Cr, m1.Cr, m0.CStride, m1.CStride); err != nil {
t.Errorf("%s (Cr): %v", tc, err)
continue
}
case *image.Gray:
m1 := m1.(*image.Gray)
if err := check(m0.Bounds(), m0.Pix, m1.Pix, m0.Stride, m1.Stride); err != nil {
t.Errorf("%s: %v", tc, err)
continue
}
default:
t.Errorf("%s: unexpected image type %T", tc, m0)
continue
}
}
}
func decodeFile(filename string) (image.Image, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Decode(f)
}
// check checks that the two pix data are equal, within the given bounds.
func check(bounds image.Rectangle, pix0, pix1 []byte, stride0, stride1 int) error {
if len(pix0) != len(pix1) {
return fmt.Errorf("len(pix) %d and %d differ", len(pix0), len(pix1))
}
if stride0 != stride1 {
return fmt.Errorf("strides %d and %d differ", stride0, stride1)
}
if stride0%8 != 0 {
return fmt.Errorf("stride %d is not a multiple of 8", stride0)
}
// Compare the two pix data, one 8x8 block at a time.
for y := 0; y < len(pix0)/stride0; y += 8 {
for x := 0; x < stride0; x += 8 {
if x >= bounds.Max.X || y >= bounds.Max.Y {
// We don't care if the two pix data differ if the 8x8 block is
// entirely outside of the image's bounds. For example, this can
// occur with a 4:2:0 chroma subsampling and a 1x1 image. Baseline
// decoding works on the one 16x16 MCU as a whole; progressive
// decoding's first pass works on that 16x16 MCU as a whole but
// refinement passes only process one 8x8 block within the MCU.
continue
}
for j := 0; j < 8; j++ {
for i := 0; i < 8; i++ {
index := (y+j)*stride0 + (x + i)
if pix0[index] != pix1[index] {
return fmt.Errorf("blocks at (%d, %d) differ:\n%sand\n%s", x, y,
pixString(pix0, stride0, x, y),
pixString(pix1, stride1, x, y),
)
}
}
}
}
}
return nil
}
func pixString(pix []byte, stride, x, y int) string {
s := bytes.NewBuffer(nil)
for j := 0; j < 8; j++ {
fmt.Fprintf(s, "\t")
for i := 0; i < 8; i++ {
fmt.Fprintf(s, "%02x ", pix[(y+j)*stride+(x+i)])
}
fmt.Fprintf(s, "\n")
}
return s.String()
}
func benchmarkDecode(b *testing.B, filename string) {
b.StopTimer()
data, err := ioutil.ReadFile(filename)
if err != nil {
b.Fatal(err)
}
cfg, err := DecodeConfig(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(cfg.Width * cfg.Height * 4))
b.StartTimer()
for i := 0; i < b.N; i++ {
Decode(bytes.NewReader(data))
}
}
func BenchmarkDecodeBaseline(b *testing.B) {
benchmarkDecode(b, "../testdata/video-001.jpeg")
}
func BenchmarkDecodeProgressive(b *testing.B) {
benchmarkDecode(b, "../testdata/video-001.progressive.jpeg")
}
// 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 jpeg
// refine decodes a successive approximation refinement block, as specified in
// section G.1.2.
func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int) error {
// Refining a DC component is trivial.
if zigStart == 0 {
if zigEnd != 0 {
panic("unreachable")
}
bit, err := d.decodeBit()
if err != nil {
return err
}
if bit {
b[0] |= delta
}
return nil
}
// Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
zig := zigStart
if d.eobRun == 0 {
loop:
for ; zig <= zigEnd; zig++ {
z := 0
value, err := d.decodeHuffman(h)
if err != nil {
return err
}
val0 := value >> 4
val1 := value & 0x0f
switch val1 {
case 0:
if val0 != 0x0f {
d.eobRun = uint16(1 << val0)
if val0 != 0 {
bits, err := d.decodeBits(int(val0))
if err != nil {
return err
}
d.eobRun |= uint16(bits)
}
break loop
}
case 1:
z = delta
bit, err := d.decodeBit()
if err != nil {
return err
}
if !bit {
z = -z
}
default:
return FormatError("unexpected Huffman code")
}
zig, err = d.refineNonZeroes(b, zig, zigEnd, int(val0), delta)
if err != nil {
return err
}
if zig > zigEnd {
return FormatError("too many coefficients")
}
if z != 0 {
b[unzig[zig]] = z
}
}
}
if d.eobRun > 0 {
d.eobRun--
if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
return err
}
}
return nil
}
// refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
// the first nz zero entries are skipped over.
func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int) (int, error) {
for ; zig <= zigEnd; zig++ {
u := unzig[zig]
if b[u] == 0 {
if nz == 0 {
break
}
nz--
continue
}
bit, err := d.decodeBit()
if err != nil {
return 0, err
}
if !bit {
continue
}
if b[u] >= 0 {
b[u] += delta
} else {
b[u] -= delta
}
}
return zig, nil
}
......@@ -171,23 +171,6 @@ func TestWriter(t *testing.T) {
}
}
func BenchmarkDecode(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("../testdata/video-001.jpeg")
if err != nil {
b.Fatal(err)
}
cfg, err := DecodeConfig(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(cfg.Width * cfg.Height * 4))
b.StartTimer()
for i := 0; i < b.N; i++ {
Decode(bytes.NewReader(data))
}
}
func BenchmarkEncode(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment